From 5f3bedf1f95659411dfa91b81963574a8557bc0c Mon Sep 17 00:00:00 2001 From: dhrubabasu <7675102+dhrubabasu@users.noreply.github.com> Date: Sun, 3 Dec 2023 09:41:32 -0500 Subject: [PATCH 1/4] `vms/platformvm`: Move `VerifyUniqueInputs` from `verifier` to `backend` --- vms/platformvm/block/executor/backend.go | 26 ++++++++++++++++++++ vms/platformvm/block/executor/verifier.go | 30 ++--------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/vms/platformvm/block/executor/backend.go b/vms/platformvm/block/executor/backend.go index fd0e75d0f664..afb675f8ebeb 100644 --- a/vms/platformvm/block/executor/backend.go +++ b/vms/platformvm/block/executor/backend.go @@ -8,6 +8,7 @@ import ( "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/snow" + "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" @@ -95,3 +96,28 @@ func (b *backend) getTimestamp(blkID ids.ID) time.Time { // so we just return the chain time. return b.state.GetTimestamp() } + +// VerifyUniqueInputs verifies that the inputs are not duplicated in the +// provided blk or any of its ancestors pinned in memory. +func (b *backend) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error { + if inputs.Len() == 0 { + return nil + } + + // Check for conflicts in ancestors. + for { + state, ok := b.blkIDToState[blkID] + if !ok { + // The parent state isn't pinned in memory. + // This means the parent must be accepted already. + return nil + } + + if state.inputs.Overlaps(inputs) { + return errConflictingParentTxs + } + + blk := state.statelessBlock + blkID = blk.Parent() + } +} diff --git a/vms/platformvm/block/executor/verifier.go b/vms/platformvm/block/executor/verifier.go index c4f25f992da8..d8ebd59020b6 100644 --- a/vms/platformvm/block/executor/verifier.go +++ b/vms/platformvm/block/executor/verifier.go @@ -9,7 +9,6 @@ import ( "github.com/ava-labs/avalanchego/chains/atomic" "github.com/ava-labs/avalanchego/ids" - "github.com/ava-labs/avalanchego/utils/set" "github.com/ava-labs/avalanchego/vms/platformvm/block" "github.com/ava-labs/avalanchego/vms/platformvm/state" "github.com/ava-labs/avalanchego/vms/platformvm/status" @@ -203,7 +202,7 @@ func (v *verifier) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error { atomicExecutor.OnAccept.AddTx(b.Tx, status.Committed) - if err := v.verifyUniqueInputs(b, atomicExecutor.Inputs); err != nil { + if err := v.VerifyUniqueInputs(b.Parent(), atomicExecutor.Inputs); err != nil { return err } @@ -441,7 +440,7 @@ func (v *verifier) standardBlock( } } - if err := v.verifyUniqueInputs(b, blkState.inputs); err != nil { + if err := v.VerifyUniqueInputs(b.Parent(), blkState.inputs); err != nil { return err } @@ -461,28 +460,3 @@ func (v *verifier) standardBlock( v.Mempool.Remove(b.Transactions) return nil } - -// verifyUniqueInputs verifies that the inputs of the given block are not -// duplicated in any of the parent blocks pinned in memory. -func (v *verifier) verifyUniqueInputs(block block.Block, inputs set.Set[ids.ID]) error { - if inputs.Len() == 0 { - return nil - } - - // Check for conflicts in ancestors. - for { - parentID := block.Parent() - parentState, ok := v.blkIDToState[parentID] - if !ok { - // The parent state isn't pinned in memory. - // This means the parent must be accepted already. - return nil - } - - if parentState.inputs.Overlaps(inputs) { - return errConflictingParentTxs - } - - block = parentState.statelessBlock - } -} From 65dda9804d03792f77be9d28e4a645cbecd89d79 Mon Sep 17 00:00:00 2001 From: dhrubabasu <7675102+dhrubabasu@users.noreply.github.com> Date: Sun, 3 Dec 2023 09:49:18 -0500 Subject: [PATCH 2/4] nit --- vms/platformvm/block/executor/backend.go | 3 +++ vms/platformvm/block/executor/verifier.go | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/vms/platformvm/block/executor/backend.go b/vms/platformvm/block/executor/backend.go index afb675f8ebeb..3f6eb3245417 100644 --- a/vms/platformvm/block/executor/backend.go +++ b/vms/platformvm/block/executor/backend.go @@ -4,6 +4,7 @@ package executor import ( + "errors" "time" "github.com/ava-labs/avalanchego/ids" @@ -14,6 +15,8 @@ import ( "github.com/ava-labs/avalanchego/vms/platformvm/txs/mempool" ) +var errConflictingParentTxs = errors.New("block contains a transaction that conflicts with a transaction in a parent block") + // Shared fields used by visitors. type backend struct { mempool.Mempool diff --git a/vms/platformvm/block/executor/verifier.go b/vms/platformvm/block/executor/verifier.go index d8ebd59020b6..df626e38aa12 100644 --- a/vms/platformvm/block/executor/verifier.go +++ b/vms/platformvm/block/executor/verifier.go @@ -25,7 +25,6 @@ var ( errIncorrectBlockHeight = errors.New("incorrect block height") errChildBlockEarlierThanParent = errors.New("proposed timestamp before current chain time") errConflictingBatchTxs = errors.New("block contains conflicting transactions") - errConflictingParentTxs = errors.New("block contains a transaction that conflicts with a transaction in a parent block") errOptionBlockTimestampNotMatchingParent = errors.New("option block proposed timestamp not matching parent block one") ) From 8ea9f83b91120d4d44cde4d21d9ea85889e114ab Mon Sep 17 00:00:00 2001 From: dhrubabasu <7675102+dhrubabasu@users.noreply.github.com> Date: Mon, 4 Dec 2023 11:29:51 -0500 Subject: [PATCH 3/4] comment nit --- vms/avm/block/executor/manager.go | 4 ++-- vms/platformvm/block/executor/backend.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vms/avm/block/executor/manager.go b/vms/avm/block/executor/manager.go index 48eea701bbd9..4ee6c37046ec 100644 --- a/vms/avm/block/executor/manager.go +++ b/vms/avm/block/executor/manager.go @@ -43,8 +43,8 @@ type Manager interface { // 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 - // provided blk or any of its ancestors pinned in memory. + // VerifyUniqueInputs returns nil iff no blocks in the inclusive + // ancestry of [blkID] consume an input in [inputs]. VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error } diff --git a/vms/platformvm/block/executor/backend.go b/vms/platformvm/block/executor/backend.go index 3f6eb3245417..e56c55e891dc 100644 --- a/vms/platformvm/block/executor/backend.go +++ b/vms/platformvm/block/executor/backend.go @@ -100,8 +100,8 @@ func (b *backend) getTimestamp(blkID ids.ID) time.Time { return b.state.GetTimestamp() } -// VerifyUniqueInputs verifies that the inputs are not duplicated in the -// provided blk or any of its ancestors pinned in memory. +// VerifyUniqueInputs returns nil iff no blocks in the inclusive +// ancestry of [blkID] consume an input in [inputs]. func (b *backend) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error { if inputs.Len() == 0 { return nil From 0f51e040cef2a49437c9f2be64f678202c321dc9 Mon Sep 17 00:00:00 2001 From: dhrubabasu <7675102+dhrubabasu@users.noreply.github.com> Date: Mon, 4 Dec 2023 12:51:37 -0500 Subject: [PATCH 4/4] unexport --- vms/platformvm/block/executor/backend.go | 4 ++-- vms/platformvm/block/executor/verifier.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vms/platformvm/block/executor/backend.go b/vms/platformvm/block/executor/backend.go index e56c55e891dc..4d915047f560 100644 --- a/vms/platformvm/block/executor/backend.go +++ b/vms/platformvm/block/executor/backend.go @@ -100,9 +100,9 @@ func (b *backend) getTimestamp(blkID ids.ID) time.Time { return b.state.GetTimestamp() } -// VerifyUniqueInputs returns nil iff no blocks in the inclusive +// verifyUniqueInputs returns nil iff no blocks in the inclusive // ancestry of [blkID] consume an input in [inputs]. -func (b *backend) VerifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error { +func (b *backend) verifyUniqueInputs(blkID ids.ID, inputs set.Set[ids.ID]) error { if inputs.Len() == 0 { return nil } diff --git a/vms/platformvm/block/executor/verifier.go b/vms/platformvm/block/executor/verifier.go index df626e38aa12..abc2aa4e257c 100644 --- a/vms/platformvm/block/executor/verifier.go +++ b/vms/platformvm/block/executor/verifier.go @@ -201,7 +201,7 @@ func (v *verifier) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error { atomicExecutor.OnAccept.AddTx(b.Tx, status.Committed) - if err := v.VerifyUniqueInputs(b.Parent(), atomicExecutor.Inputs); err != nil { + if err := v.verifyUniqueInputs(b.Parent(), atomicExecutor.Inputs); err != nil { return err } @@ -439,7 +439,7 @@ func (v *verifier) standardBlock( } } - if err := v.VerifyUniqueInputs(b.Parent(), blkState.inputs); err != nil { + if err := v.verifyUniqueInputs(b.Parent(), blkState.inputs); err != nil { return err }