Skip to content

Commit

Permalink
[vms/platformvm] Add VerifyWithContext to Blocks (#3236)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Aug 1, 2024
1 parent 8f41c9d commit b28844b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
32 changes: 28 additions & 4 deletions vms/platformvm/block/executor/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (

"github.com/ava-labs/avalanchego/snow/consensus/snowman"
"github.com/ava-labs/avalanchego/vms/platformvm/block"

smblock "github.com/ava-labs/avalanchego/snow/engine/snowman/block"
)

var (
_ snowman.Block = (*Block)(nil)
_ snowman.OracleBlock = (*Block)(nil)
_ snowman.Block = (*Block)(nil)
_ snowman.OracleBlock = (*Block)(nil)
_ smblock.WithVerifyContext = (*Block)(nil)
)

// Exported for testing in platformvm package.
Expand All @@ -22,19 +25,40 @@ type Block struct {
manager *manager
}

func (b *Block) Verify(context.Context) error {
func (*Block) ShouldVerifyWithContext(context.Context) (bool, error) {
return true, nil
}

func (b *Block) VerifyWithContext(_ context.Context, ctx *smblock.Context) error {
pChainHeight := uint64(0)
if ctx != nil {
pChainHeight = ctx.PChainHeight
}

blkID := b.ID()
if _, ok := b.manager.blkIDToState[blkID]; ok {
if blkState, ok := b.manager.blkIDToState[blkID]; ok {
if !blkState.verifiedHeights.Contains(pChainHeight) {
// PlatformVM blocks are currently valid regardless of the ProposerVM's
// PChainHeight. If this changes, those validity checks should be done prior
// to adding [pChainHeight] to [verifiedHeights].
blkState.verifiedHeights.Add(pChainHeight)
}

// This block has already been verified.
return nil
}

return b.Visit(&verifier{
backend: b.manager.backend,
txExecutorBackend: b.manager.txExecutorBackend,
pChainHeight: pChainHeight,
})
}

func (b *Block) Verify(ctx context.Context) error {
return b.VerifyWithContext(ctx, nil)
}

func (b *Block) Accept(context.Context) error {
return b.Visit(b.manager.acceptor)
}
Expand Down
7 changes: 4 additions & 3 deletions vms/platformvm/block/executor/block_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type blockState struct {
onAcceptState state.Diff
onAcceptFunc func()

inputs set.Set[ids.ID]
timestamp time.Time
atomicRequests map[ids.ID]*atomic.Requests
inputs set.Set[ids.ID]
timestamp time.Time
atomicRequests map[ids.ID]*atomic.Requests
verifiedHeights set.Set[uint64]
}
34 changes: 20 additions & 14 deletions vms/platformvm/block/executor/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
type verifier struct {
*backend
txExecutorBackend *executor.Backend
pChainHeight uint64
}

func (v *verifier) BanffAbortBlock(b *block.BanffAbortBlock) error {
Expand Down Expand Up @@ -233,9 +234,10 @@ func (v *verifier) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {

onAcceptState: atomicExecutor.OnAccept,

inputs: atomicExecutor.Inputs,
timestamp: atomicExecutor.OnAccept.GetTimestamp(),
atomicRequests: atomicExecutor.AtomicRequests,
inputs: atomicExecutor.Inputs,
timestamp: atomicExecutor.OnAccept.GetTimestamp(),
atomicRequests: atomicExecutor.AtomicRequests,
verifiedHeights: set.Of(v.pChainHeight),
}
return nil
}
Expand Down Expand Up @@ -345,9 +347,10 @@ func (v *verifier) abortBlock(b block.Block) error {

blkID := b.ID()
v.blkIDToState[blkID] = &blockState{
statelessBlock: b,
onAcceptState: onAbortState,
timestamp: onAbortState.GetTimestamp(),
statelessBlock: b,
onAcceptState: onAbortState,
timestamp: onAbortState.GetTimestamp(),
verifiedHeights: set.Of(v.pChainHeight),
}
return nil
}
Expand All @@ -362,9 +365,10 @@ func (v *verifier) commitBlock(b block.Block) error {

blkID := b.ID()
v.blkIDToState[blkID] = &blockState{
statelessBlock: b,
onAcceptState: onCommitState,
timestamp: onCommitState.GetTimestamp(),
statelessBlock: b,
onAcceptState: onCommitState,
timestamp: onCommitState.GetTimestamp(),
verifiedHeights: set.Of(v.pChainHeight),
}
return nil
}
Expand Down Expand Up @@ -415,8 +419,9 @@ func (v *verifier) proposalBlock(
// It is safe to use [b.onAbortState] here because the timestamp will
// never be modified by an Apricot Abort block and the timestamp will
// always be the same as the Banff Proposal Block.
timestamp: onAbortState.GetTimestamp(),
atomicRequests: atomicRequests,
timestamp: onAbortState.GetTimestamp(),
atomicRequests: atomicRequests,
verifiedHeights: set.Of(v.pChainHeight),
}
return nil
}
Expand All @@ -441,9 +446,10 @@ func (v *verifier) standardBlock(
onAcceptState: onAcceptState,
onAcceptFunc: onAcceptFunc,

timestamp: onAcceptState.GetTimestamp(),
inputs: inputs,
atomicRequests: atomicRequests,
timestamp: onAcceptState.GetTimestamp(),
inputs: inputs,
atomicRequests: atomicRequests,
verifiedHeights: set.Of(v.pChainHeight),
}
return nil
}
Expand Down

0 comments on commit b28844b

Please sign in to comment.