github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/snowman/block/block_context_vm.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package block
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/snow/consensus/snowman"
    10  )
    11  
    12  // Context defines the block context that will be optionally provided by the
    13  // proposervm to an underlying vm.
    14  type Context struct {
    15  	// PChainHeight is the height that this block will use to verify it's state.
    16  	// In the proposervm, blocks verify the proposer based on the P-chain height
    17  	// recorded in the parent block. The P-chain height provided here is also
    18  	// the parent's P-chain height, not this block's P-chain height.
    19  	//
    20  	// Because PreForkBlocks and PostForkOptions do not verify their execution
    21  	// against the P-chain's state, this context is undefined for those blocks.
    22  	PChainHeight uint64
    23  }
    24  
    25  // BuildBlockWithContextChainVM defines the interface a ChainVM can optionally
    26  // implement to consider the P-Chain height when building blocks.
    27  type BuildBlockWithContextChainVM interface {
    28  	// Attempt to build a new block given that the P-Chain height is
    29  	// [blockCtx.PChainHeight].
    30  	//
    31  	// This method will be called if and only if the proposervm is activated.
    32  	// Otherwise [BuildBlock] will be called.
    33  	BuildBlockWithContext(ctx context.Context, blockCtx *Context) (snowman.Block, error)
    34  }
    35  
    36  // WithVerifyContext defines the interface a Block can optionally implement to
    37  // consider the P-Chain height when verifying itself.
    38  //
    39  // As with all Blocks, it is guaranteed for verification to be called in
    40  // topological order.
    41  //
    42  // If the status of the block is Accepted or Rejected; VerifyWithContext will
    43  // never be called.
    44  type WithVerifyContext interface {
    45  	// Returns true if [VerifyWithContext] should be called.
    46  	// Returns false if [Verify] should be called.
    47  	//
    48  	// This method will be called if and only if the proposervm is activated.
    49  	// Otherwise [Verify] will be called.
    50  	ShouldVerifyWithContext(context.Context) (bool, error)
    51  
    52  	// Verify that the state transition this block would make if accepted is
    53  	// valid. If the state transition is invalid, a non-nil error should be
    54  	// returned.
    55  	//
    56  	// It is guaranteed that the Parent has been successfully verified.
    57  	//
    58  	// This method may be called again with a different context.
    59  	//
    60  	// If nil is returned, it is guaranteed that either Accept or Reject will be
    61  	// called on this block, unless the VM is shut down.
    62  	//
    63  	// Note: During `Accept` the block context is not provided. This implies
    64  	// that the block context provided here can not be used to alter any
    65  	// potential state transition that assumes network agreement. The block
    66  	// context should only be used to determine the validity of the block.
    67  	VerifyWithContext(context.Context, *Context) error
    68  }