github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/executor/rejector.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  	"go.uber.org/zap"
     8  
     9  	"github.com/MetalBlockchain/metalgo/vms/platformvm/block"
    10  )
    11  
    12  var _ block.Visitor = (*rejector)(nil)
    13  
    14  // rejector handles the logic for rejecting a block.
    15  // All errors returned by this struct are fatal and should result in the chain
    16  // being shutdown.
    17  type rejector struct {
    18  	*backend
    19  	addTxsToMempool bool
    20  }
    21  
    22  func (r *rejector) BanffAbortBlock(b *block.BanffAbortBlock) error {
    23  	return r.rejectBlock(b, "banff abort")
    24  }
    25  
    26  func (r *rejector) BanffCommitBlock(b *block.BanffCommitBlock) error {
    27  	return r.rejectBlock(b, "banff commit")
    28  }
    29  
    30  func (r *rejector) BanffProposalBlock(b *block.BanffProposalBlock) error {
    31  	return r.rejectBlock(b, "banff proposal")
    32  }
    33  
    34  func (r *rejector) BanffStandardBlock(b *block.BanffStandardBlock) error {
    35  	return r.rejectBlock(b, "banff standard")
    36  }
    37  
    38  func (r *rejector) ApricotAbortBlock(b *block.ApricotAbortBlock) error {
    39  	return r.rejectBlock(b, "apricot abort")
    40  }
    41  
    42  func (r *rejector) ApricotCommitBlock(b *block.ApricotCommitBlock) error {
    43  	return r.rejectBlock(b, "apricot commit")
    44  }
    45  
    46  func (r *rejector) ApricotProposalBlock(b *block.ApricotProposalBlock) error {
    47  	return r.rejectBlock(b, "apricot proposal")
    48  }
    49  
    50  func (r *rejector) ApricotStandardBlock(b *block.ApricotStandardBlock) error {
    51  	return r.rejectBlock(b, "apricot standard")
    52  }
    53  
    54  func (r *rejector) ApricotAtomicBlock(b *block.ApricotAtomicBlock) error {
    55  	return r.rejectBlock(b, "apricot atomic")
    56  }
    57  
    58  func (r *rejector) rejectBlock(b block.Block, blockType string) error {
    59  	blkID := b.ID()
    60  	defer r.free(blkID)
    61  
    62  	r.ctx.Log.Verbo(
    63  		"rejecting block",
    64  		zap.String("blockType", blockType),
    65  		zap.Stringer("blkID", blkID),
    66  		zap.Uint64("height", b.Height()),
    67  		zap.Stringer("parentID", b.Parent()),
    68  	)
    69  
    70  	if !r.addTxsToMempool {
    71  		return nil
    72  	}
    73  
    74  	for _, tx := range b.Txs() {
    75  		if err := r.Mempool.Add(tx); err != nil {
    76  			r.ctx.Log.Debug(
    77  				"failed to reissue tx",
    78  				zap.Stringer("txID", tx.ID()),
    79  				zap.Stringer("blkID", blkID),
    80  				zap.Error(err),
    81  			)
    82  		}
    83  	}
    84  
    85  	r.Mempool.RequestBuildBlock(false /*=emptyBlockPermitted*/)
    86  
    87  	return nil
    88  }