github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/block/executor/block.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package executor
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"github.com/ava-labs/avalanchego/snow/consensus/snowman"
    11  	"github.com/ava-labs/avalanchego/vms/platformvm/block"
    12  
    13  	smblock "github.com/ava-labs/avalanchego/snow/engine/snowman/block"
    14  )
    15  
    16  var (
    17  	_ snowman.Block             = (*Block)(nil)
    18  	_ snowman.OracleBlock       = (*Block)(nil)
    19  	_ smblock.WithVerifyContext = (*Block)(nil)
    20  )
    21  
    22  // Exported for testing in platformvm package.
    23  type Block struct {
    24  	block.Block
    25  	manager *manager
    26  }
    27  
    28  func (*Block) ShouldVerifyWithContext(context.Context) (bool, error) {
    29  	return true, nil
    30  }
    31  
    32  func (b *Block) VerifyWithContext(_ context.Context, ctx *smblock.Context) error {
    33  	pChainHeight := uint64(0)
    34  	if ctx != nil {
    35  		pChainHeight = ctx.PChainHeight
    36  	}
    37  
    38  	blkID := b.ID()
    39  	if blkState, ok := b.manager.blkIDToState[blkID]; ok {
    40  		if !blkState.verifiedHeights.Contains(pChainHeight) {
    41  			// PlatformVM blocks are currently valid regardless of the ProposerVM's
    42  			// PChainHeight. If this changes, those validity checks should be done prior
    43  			// to adding [pChainHeight] to [verifiedHeights].
    44  			blkState.verifiedHeights.Add(pChainHeight)
    45  		}
    46  
    47  		// This block has already been verified.
    48  		return nil
    49  	}
    50  
    51  	return b.Visit(&verifier{
    52  		backend:           b.manager.backend,
    53  		txExecutorBackend: b.manager.txExecutorBackend,
    54  		pChainHeight:      pChainHeight,
    55  	})
    56  }
    57  
    58  func (b *Block) Verify(ctx context.Context) error {
    59  	return b.VerifyWithContext(ctx, nil)
    60  }
    61  
    62  func (b *Block) Accept(context.Context) error {
    63  	return b.Visit(b.manager.acceptor)
    64  }
    65  
    66  func (b *Block) Reject(context.Context) error {
    67  	return b.Visit(b.manager.rejector)
    68  }
    69  
    70  func (b *Block) Timestamp() time.Time {
    71  	return b.manager.getTimestamp(b.ID())
    72  }
    73  
    74  func (b *Block) Options(context.Context) ([2]snowman.Block, error) {
    75  	options := options{
    76  		log:                     b.manager.ctx.Log,
    77  		primaryUptimePercentage: b.manager.txExecutorBackend.Config.UptimePercentage,
    78  		uptimes:                 b.manager.txExecutorBackend.Uptimes,
    79  		state:                   b.manager.backend.state,
    80  	}
    81  	if err := b.Block.Visit(&options); err != nil {
    82  		return [2]snowman.Block{}, err
    83  	}
    84  
    85  	return [2]snowman.Block{
    86  		b.manager.NewBlock(options.preferredBlock),
    87  		b.manager.NewBlock(options.alternateBlock),
    88  	}, nil
    89  }