Skip to content

Commit

Permalink
Add VerifyTx to executor.Manager (#2293)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Nov 14, 2023
1 parent 29f86e9 commit 5dff153
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
4 changes: 2 additions & 2 deletions vms/avm/block/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions vms/platformvm/block/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -103,24 +102,14 @@ 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
// as if it was successfully added
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
}
Expand Down
32 changes: 29 additions & 3 deletions vms/platformvm/block/executor/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -62,7 +73,8 @@ func NewManager(
backend: backend,
addTxsToMempool: !txExecutorBackend.Config.PartialSyncPrimaryNetwork,
},
preferred: lastAccepted,
preferred: lastAccepted,
txExecutorBackend: txExecutorBackend,
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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,
})
}
15 changes: 15 additions & 0 deletions vms/platformvm/block/executor/mock_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5dff153

Please sign in to comment.