From 5dff15390e71c5ce73a48a27aeb904917b9bde62 Mon Sep 17 00:00:00 2001 From: Dhruba Basu <7675102+dhrubabasu@users.noreply.github.com> Date: Tue, 14 Nov 2023 12:47:06 -0800 Subject: [PATCH] Add `VerifyTx` to `executor.Manager` (#2293) --- vms/avm/block/executor/manager.go | 4 +-- vms/platformvm/block/builder/builder.go | 13 +------- vms/platformvm/block/executor/manager.go | 32 +++++++++++++++++-- vms/platformvm/block/executor/mock_manager.go | 15 +++++++++ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/vms/avm/block/executor/manager.go b/vms/avm/block/executor/manager.go index aa99ede6392f..dd9b8bfab400 100644 --- a/vms/avm/block/executor/manager.go +++ b/vms/avm/block/executor/manager.go @@ -39,8 +39,8 @@ type Manager interface { GetStatelessBlock(blkID ids.ID) (block.Block, error) NewBlock(block.Block) snowman.Block - // VerifyTx verifies that the transaction can be issued based on the - // currently preferred state. + // VerifyTx verifies that the transaction can be issued based on the currently + // preferred state. This should *not* be used to verify transactions in a block. VerifyTx(tx *txs.Tx) error // VerifyUniqueInputs verifies that the inputs are not duplicated in the diff --git a/vms/platformvm/block/builder/builder.go b/vms/platformvm/block/builder/builder.go index aecc4c358f10..4d8ad96d132a 100644 --- a/vms/platformvm/block/builder/builder.go +++ b/vms/platformvm/block/builder/builder.go @@ -36,7 +36,6 @@ var ( ErrEndOfTime = errors.New("program time is suspiciously far in the future") ErrNoPendingBlocks = errors.New("no pending blocks") - ErrChainNotSynced = errors.New("chain not synced") ) type Builder interface { @@ -103,10 +102,6 @@ func New( // AddUnverifiedTx verifies a transaction and attempts to add it to the mempool func (b *builder) AddUnverifiedTx(tx *txs.Tx) error { - if !b.txExecutorBackend.Bootstrapped.Get() { - return ErrChainNotSynced - } - txID := tx.ID() if b.Mempool.Has(txID) { // If the transaction is already in the mempool - then it looks the same @@ -114,13 +109,7 @@ func (b *builder) AddUnverifiedTx(tx *txs.Tx) error { return nil } - verifier := txexecutor.MempoolTxVerifier{ - Backend: b.txExecutorBackend, - ParentID: b.blkManager.Preferred(), // We want to build off of the preferred block - StateVersions: b.blkManager, - Tx: tx, - } - if err := tx.Unsigned.Visit(&verifier); err != nil { + if err := b.blkManager.VerifyTx(tx); err != nil { b.MarkDropped(txID, err) return err } diff --git a/vms/platformvm/block/executor/manager.go b/vms/platformvm/block/executor/manager.go index 1472e2f40b3d..9af9cbce2c4a 100644 --- a/vms/platformvm/block/executor/manager.go +++ b/vms/platformvm/block/executor/manager.go @@ -4,17 +4,24 @@ package executor import ( + "errors" + "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow/consensus/snowman" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/metrics" "github.com/ava-labs/avalanchego/vms/platformvm/state" + "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/vms/platformvm/txs/executor" "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" "github.com/ava-labs/avalanchego/vms/platformvm/validators" ) -var _ Manager = (*manager)(nil) +var ( + _ Manager = (*manager)(nil) + + ErrChainNotSynced = errors.New("chain not synced") +) type Manager interface { state.Versions @@ -28,6 +35,10 @@ type Manager interface { GetBlock(blkID ids.ID) (snowman.Block, error) GetStatelessBlock(blkID ids.ID) (block.Block, error) NewBlock(block.Block) snowman.Block + + // VerifyTx verifies that the transaction can be issued based on the currently + // preferred state. This should *not* be used to verify transactions in a block. + VerifyTx(tx *txs.Tx) error } func NewManager( @@ -62,7 +73,8 @@ func NewManager( backend: backend, addTxsToMempool: !txExecutorBackend.Config.PartialSyncPrimaryNetwork, }, - preferred: lastAccepted, + preferred: lastAccepted, + txExecutorBackend: txExecutorBackend, } } @@ -72,7 +84,8 @@ type manager struct { acceptor block.Visitor rejector block.Visitor - preferred ids.ID + preferred ids.ID + txExecutorBackend *executor.Backend } func (m *manager) GetBlock(blkID ids.ID) (snowman.Block, error) { @@ -103,3 +116,16 @@ func (m *manager) SetPreference(blockID ids.ID) (updated bool) { func (m *manager) Preferred() ids.ID { return m.preferred } + +func (m *manager) VerifyTx(tx *txs.Tx) error { + if !m.txExecutorBackend.Bootstrapped.Get() { + return ErrChainNotSynced + } + + return tx.Unsigned.Visit(&executor.MempoolTxVerifier{ + Backend: m.txExecutorBackend, + ParentID: m.preferred, + StateVersions: m, + Tx: tx, + }) +} diff --git a/vms/platformvm/block/executor/mock_manager.go b/vms/platformvm/block/executor/mock_manager.go index 8755325a0fdc..f5b2bcb3608c 100644 --- a/vms/platformvm/block/executor/mock_manager.go +++ b/vms/platformvm/block/executor/mock_manager.go @@ -14,6 +14,7 @@ import ( snowman "github.com/ava-labs/avalanchego/snow/consensus/snowman" block "github.com/ava-labs/avalanchego/vms/platformvm/block" state "github.com/ava-labs/avalanchego/vms/platformvm/state" + txs "github.com/ava-labs/avalanchego/vms/platformvm/txs" gomock "go.uber.org/mock/gomock" ) @@ -140,3 +141,17 @@ func (mr *MockManagerMockRecorder) SetPreference(arg0 interface{}) *gomock.Call mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetPreference", reflect.TypeOf((*MockManager)(nil).SetPreference), arg0) } + +// VerifyTx mocks base method. +func (m *MockManager) VerifyTx(arg0 *txs.Tx) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "VerifyTx", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// VerifyTx indicates an expected call of VerifyTx. +func (mr *MockManagerMockRecorder) VerifyTx(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "VerifyTx", reflect.TypeOf((*MockManager)(nil).VerifyTx), arg0) +}