Skip to content

Verify avm mempool txs against the last accepted state #2569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions vms/avm/block/executor/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,27 +859,25 @@ func TestBlockReject(t *testing.T) {
mempool.EXPECT().Add(validTx).Return(nil) // Only add the one that passes verification
mempool.EXPECT().RequestBuildBlock()

preferredID := ids.GenerateTestID()
mockPreferredState := state.NewMockDiff(ctrl)
mockPreferredState.EXPECT().GetLastAccepted().Return(ids.GenerateTestID()).AnyTimes()
mockPreferredState.EXPECT().GetTimestamp().Return(time.Now()).AnyTimes()
lastAcceptedID := ids.GenerateTestID()
mockState := state.NewMockState(ctrl)
mockState.EXPECT().GetLastAccepted().Return(lastAcceptedID).AnyTimes()
mockState.EXPECT().GetTimestamp().Return(time.Now()).AnyTimes()

return &Block{
Block: mockBlock,
manager: &manager{
preferred: preferredID,
mempool: mempool,
metrics: metrics.NewMockMetrics(ctrl),
lastAccepted: lastAcceptedID,
mempool: mempool,
metrics: metrics.NewMockMetrics(ctrl),
backend: &executor.Backend{
Bootstrapped: true,
Ctx: &snow.Context{
Log: logging.NoLog{},
},
},
state: mockState,
blkIDToState: map[ids.ID]*blockState{
preferredID: {
onAcceptState: mockPreferredState,
},
blockID: {},
},
},
Expand Down Expand Up @@ -919,27 +917,25 @@ func TestBlockReject(t *testing.T) {
mempool.EXPECT().Add(tx2).Return(nil)
mempool.EXPECT().RequestBuildBlock()

preferredID := ids.GenerateTestID()
mockPreferredState := state.NewMockDiff(ctrl)
mockPreferredState.EXPECT().GetLastAccepted().Return(ids.GenerateTestID()).AnyTimes()
mockPreferredState.EXPECT().GetTimestamp().Return(time.Now()).AnyTimes()
lastAcceptedID := ids.GenerateTestID()
mockState := state.NewMockState(ctrl)
mockState.EXPECT().GetLastAccepted().Return(lastAcceptedID).AnyTimes()
mockState.EXPECT().GetTimestamp().Return(time.Now()).AnyTimes()

return &Block{
Block: mockBlock,
manager: &manager{
preferred: preferredID,
mempool: mempool,
metrics: metrics.NewMockMetrics(ctrl),
lastAccepted: lastAcceptedID,
mempool: mempool,
metrics: metrics.NewMockMetrics(ctrl),
backend: &executor.Backend{
Bootstrapped: true,
Ctx: &snow.Context{
Log: logging.NoLog{},
},
},
state: mockState,
blkIDToState: map[ids.ID]*blockState{
preferredID: {
onAcceptState: mockPreferredState,
},
blockID: {},
},
},
Expand Down
9 changes: 2 additions & 7 deletions vms/avm/block/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {
return err
}

stateDiff, err := state.NewDiff(m.preferred, m)
stateDiff, err := state.NewDiff(m.lastAccepted, m)
if err != nil {
return err
}
Expand All @@ -174,12 +174,7 @@ func (m *manager) VerifyTx(tx *txs.Tx) error {
State: stateDiff,
Tx: tx,
}
err = tx.Unsigned.Visit(executor)
if err != nil {
return err
}

return m.VerifyUniqueInputs(m.preferred, executor.Inputs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: BuildBlock still verifies unique inputs (https://github.com/ava-labs/avalanchego/blob/verify-against-accept/vms/avm/block/builder/builder.go#L141)

This PR only removes this check from the mempool verifier.

return tx.Unsigned.Visit(executor)
}

func (m *manager) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error {
Expand Down
68 changes: 9 additions & 59 deletions vms/avm/block/executor/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ func TestManagerVerifyTx(t *testing.T) {
expectedErr error
}

inputID := ids.GenerateTestID()
tests := []test{
{
name: "not bootstrapped",
Expand Down Expand Up @@ -161,20 +160,19 @@ func TestManagerVerifyTx(t *testing.T) {
}
},
managerF: func(ctrl *gomock.Controller) *manager {
preferred := ids.GenerateTestID()
lastAcceptedID := ids.GenerateTestID()

// These values don't matter for this test
state := state.NewMockState(ctrl)
state.EXPECT().GetLastAccepted().Return(preferred)
state.EXPECT().GetLastAccepted().Return(lastAcceptedID)
state.EXPECT().GetTimestamp().Return(time.Time{})

return &manager{
backend: &executor.Backend{
Bootstrapped: true,
},
state: state,
lastAccepted: preferred,
preferred: preferred,
lastAccepted: lastAcceptedID,
}
},
expectedErr: errTestSemanticVerifyFail,
Expand All @@ -194,69 +192,22 @@ func TestManagerVerifyTx(t *testing.T) {
}
},
managerF: func(ctrl *gomock.Controller) *manager {
preferred := ids.GenerateTestID()
lastAcceptedID := ids.GenerateTestID()

// These values don't matter for this test
state := state.NewMockState(ctrl)
state.EXPECT().GetLastAccepted().Return(preferred)
state.EXPECT().GetLastAccepted().Return(lastAcceptedID)
state.EXPECT().GetTimestamp().Return(time.Time{})

return &manager{
backend: &executor.Backend{
Bootstrapped: true,
},
state: state,
lastAccepted: preferred,
preferred: preferred,
}
},
expectedErr: errTestExecutionFail,
},
{
name: "non-unique inputs",
txF: func(ctrl *gomock.Controller) *txs.Tx {
unsigned := txs.NewMockUnsignedTx(ctrl)
// Syntactic verification passes
unsigned.EXPECT().Visit(gomock.Any()).Return(nil)
// Semantic verification passes
unsigned.EXPECT().Visit(gomock.Any()).Return(nil)
// Execution passes
unsigned.EXPECT().Visit(gomock.Any()).DoAndReturn(func(e *executor.Executor) error {
e.Inputs.Add(inputID)
return nil
})
return &txs.Tx{
Unsigned: unsigned,
}
},
managerF: func(ctrl *gomock.Controller) *manager {
lastAcceptedID := ids.GenerateTestID()

preferredID := ids.GenerateTestID()
preferred := block.NewMockBlock(ctrl)
preferred.EXPECT().Parent().Return(lastAcceptedID).AnyTimes()

// These values don't matter for this test
diffState := state.NewMockDiff(ctrl)
diffState.EXPECT().GetLastAccepted().Return(preferredID)
diffState.EXPECT().GetTimestamp().Return(time.Time{})

return &manager{
backend: &executor.Backend{
Bootstrapped: true,
},
blkIDToState: map[ids.ID]*blockState{
preferredID: {
statelessBlock: preferred,
onAcceptState: diffState,
importedInputs: set.Of(inputID),
},
},
lastAccepted: lastAcceptedID,
preferred: preferredID,
}
},
expectedErr: ErrConflictingParentTxs,
expectedErr: errTestExecutionFail,
},
{
name: "happy path",
Expand All @@ -273,20 +224,19 @@ func TestManagerVerifyTx(t *testing.T) {
}
},
managerF: func(ctrl *gomock.Controller) *manager {
preferred := ids.GenerateTestID()
lastAcceptedID := ids.GenerateTestID()

// These values don't matter for this test
state := state.NewMockState(ctrl)
state.EXPECT().GetLastAccepted().Return(preferred)
state.EXPECT().GetLastAccepted().Return(lastAcceptedID)
state.EXPECT().GetTimestamp().Return(time.Time{})

return &manager{
backend: &executor.Backend{
Bootstrapped: true,
},
state: state,
lastAccepted: preferred,
preferred: preferred,
lastAccepted: lastAcceptedID,
}
},
expectedErr: nil,
Expand Down