Skip to content

Commit

Permalink
Merge branch 'move-peer-timestamp-check' of github.com:ava-labs/avala…
Browse files Browse the repository at this point in the history
…nchego into move-peer-timestamp-check
  • Loading branch information
StephenButtolph committed Jan 18, 2024
2 parents 58b35b9 + aa09393 commit 7f5335d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 188 deletions.
32 changes: 27 additions & 5 deletions vms/platformvm/block/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,34 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {
return ErrChainNotSynced
}

return tx.Unsigned.Visit(&executor.MempoolTxVerifier{
Backend: m.txExecutorBackend,
ParentID: m.preferred,
StateVersions: m,
Tx: tx,
stateDiff, err := state.NewDiff(m.preferred, m)
if err != nil {
return err
}

nextBlkTime, _, err := executor.NextBlockTime(stateDiff, m.txExecutorBackend.Clk)
if err != nil {
return err
}

_, err = executor.AdvanceTimeTo(m.txExecutorBackend, stateDiff, nextBlkTime)
if err != nil {
return err
}

err = tx.Unsigned.Visit(&executor.StandardTxExecutor{
Backend: m.txExecutorBackend,
State: stateDiff,
Tx: tx,
})
// We ignore [errFutureStakeTime] here because the time will be advanced
// when this transaction is issued.
//
// TODO: Remove this check post-Durango.
if errors.Is(err, executor.ErrFutureStakeTime) {
return nil
}
return err
}

func (m *manager) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error {
Expand Down
16 changes: 6 additions & 10 deletions vms/platformvm/txs/executor/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,15 @@ func TestNewExportTx(t *testing.T) {
)
require.NoError(err)

fakedState, err := state.NewDiff(lastAcceptedID, env)
stateDiff, err := state.NewDiff(lastAcceptedID, env)
require.NoError(err)

fakedState.SetTimestamp(tt.timestamp)
stateDiff.SetTimestamp(tt.timestamp)

fakedParent := ids.GenerateTestID()
env.SetState(fakedParent, fakedState)

verifier := MempoolTxVerifier{
Backend: &env.backend,
ParentID: fakedParent,
StateVersions: env,
Tx: tx,
verifier := StandardTxExecutor{
Backend: &env.backend,
State: stateDiff,
Tx: tx,
}
require.NoError(tx.Unsigned.Visit(&verifier))
})
Expand Down
16 changes: 6 additions & 10 deletions vms/platformvm/txs/executor/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,15 @@ func TestNewImportTx(t *testing.T) {

require.Equal(env.config.TxFee, totalIn-totalOut)

fakedState, err := state.NewDiff(lastAcceptedID, env)
stateDiff, err := state.NewDiff(lastAcceptedID, env)
require.NoError(err)

fakedState.SetTimestamp(tt.timestamp)
stateDiff.SetTimestamp(tt.timestamp)

fakedParent := ids.GenerateTestID()
env.SetState(fakedParent, fakedState)

verifier := MempoolTxVerifier{
Backend: &env.backend,
ParentID: fakedParent,
StateVersions: env,
Tx: tx,
verifier := StandardTxExecutor{
Backend: &env.backend,
State: stateDiff,
Tx: tx,
}
require.NoError(tx.Unsigned.Visit(&verifier))
})
Expand Down
20 changes: 0 additions & 20 deletions vms/platformvm/txs/executor/standard_tx_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup func(*environment)
AP3Time time.Time
expectedExecutionErr error
expectedMempoolErr error
}

tests := []test{
Expand All @@ -194,7 +193,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: nil,
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrPeriodMismatch,
expectedMempoolErr: ErrPeriodMismatch,
},
{
description: fmt.Sprintf("delegator should not be added more than (%s) in the future", MaxFutureStartTime),
Expand All @@ -207,7 +205,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: nil,
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrFutureStakeTime,
expectedMempoolErr: nil,
},
{
description: "validator not in the current or pending validator sets",
Expand All @@ -220,7 +217,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: nil,
AP3Time: defaultGenesisTime,
expectedExecutionErr: database.ErrNotFound,
expectedMempoolErr: database.ErrNotFound,
},
{
description: "delegator starts before validator",
Expand All @@ -233,7 +229,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: addMinStakeValidator,
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrPeriodMismatch,
expectedMempoolErr: ErrPeriodMismatch,
},
{
description: "delegator stops before validator",
Expand All @@ -246,7 +241,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: addMinStakeValidator,
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrPeriodMismatch,
expectedMempoolErr: ErrPeriodMismatch,
},
{
description: "valid",
Expand All @@ -259,7 +253,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: addMinStakeValidator,
AP3Time: defaultGenesisTime,
expectedExecutionErr: nil,
expectedMempoolErr: nil,
},
{
description: "starts delegating at current timestamp",
Expand All @@ -272,7 +265,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: nil,
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrTimestampNotBeforeStartTime,
expectedMempoolErr: ErrTimestampNotBeforeStartTime,
},
{
description: "tx fee paying key has no funds",
Expand All @@ -297,7 +289,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
},
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrFlowCheckFailed,
expectedMempoolErr: ErrFlowCheckFailed,
},
{
description: "over delegation before AP3",
Expand All @@ -310,7 +301,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: addMaxStakeValidator,
AP3Time: defaultValidateEndTime,
expectedExecutionErr: nil,
expectedMempoolErr: nil,
},
{
description: "over delegation after AP3",
Expand All @@ -323,7 +313,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
setup: addMaxStakeValidator,
AP3Time: defaultGenesisTime,
expectedExecutionErr: ErrOverDelegated,
expectedMempoolErr: ErrOverDelegated,
},
}

Expand Down Expand Up @@ -363,15 +352,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) {
}
err = tx.Unsigned.Visit(&executor)
require.ErrorIs(err, tt.expectedExecutionErr)

mempoolExecutor := MempoolTxVerifier{
Backend: &freshTH.backend,
ParentID: lastAcceptedID,
StateVersions: freshTH,
Tx: tx,
}
err = tx.Unsigned.Visit(&mempoolExecutor)
require.ErrorIs(err, tt.expectedMempoolErr)
})
}
}
Expand Down
25 changes: 25 additions & 0 deletions vms/platformvm/txs/executor/state_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
Expand Down Expand Up @@ -57,6 +58,30 @@ func VerifyNewChainTime(
return nil
}

func NextBlockTime(state state.Chain, clk *mockable.Clock) (time.Time, bool, error) {
var (
timestamp = clk.Time()
parentTime = state.GetTimestamp()
)
if parentTime.After(timestamp) {
timestamp = parentTime
}
// [timestamp] = max(now, parentTime)

nextStakerChangeTime, err := GetNextStakerChangeTime(state)
if err != nil {
return time.Time{}, false, fmt.Errorf("failed getting next staker change time: %w", err)
}

// timeWasCapped means that [timestamp] was reduced to [nextStakerChangeTime]
timeWasCapped := !timestamp.Before(nextStakerChangeTime)
if timeWasCapped {
timestamp = nextStakerChangeTime
}
// [timestamp] = min(max(now, parentTime), nextStakerChangeTime)
return timestamp, timeWasCapped, nil
}

// AdvanceTimeTo applies all state changes to [parentState] resulting from
// advancing the chain time to [newChainTime].
// Returns true iff the validator set changed.
Expand Down
143 changes: 0 additions & 143 deletions vms/platformvm/txs/executor/tx_mempool_verifier.go

This file was deleted.

0 comments on commit 7f5335d

Please sign in to comment.