github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/atomic_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 block
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/MetalBlockchain/metalgo/ids"
    10  	"github.com/MetalBlockchain/metalgo/snow"
    11  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    12  )
    13  
    14  var _ Block = (*ApricotAtomicBlock)(nil)
    15  
    16  // ApricotAtomicBlock being accepted results in the atomic transaction contained
    17  // in the block to be accepted and committed to the chain.
    18  type ApricotAtomicBlock struct {
    19  	CommonBlock `serialize:"true"`
    20  	Tx          *txs.Tx `serialize:"true" json:"tx"`
    21  }
    22  
    23  func (b *ApricotAtomicBlock) initialize(bytes []byte) error {
    24  	b.CommonBlock.initialize(bytes)
    25  	if err := b.Tx.Initialize(txs.Codec); err != nil {
    26  		return fmt.Errorf("failed to initialize tx: %w", err)
    27  	}
    28  	return nil
    29  }
    30  
    31  func (b *ApricotAtomicBlock) InitCtx(ctx *snow.Context) {
    32  	b.Tx.Unsigned.InitCtx(ctx)
    33  }
    34  
    35  func (b *ApricotAtomicBlock) Txs() []*txs.Tx {
    36  	return []*txs.Tx{b.Tx}
    37  }
    38  
    39  func (b *ApricotAtomicBlock) Visit(v Visitor) error {
    40  	return v.ApricotAtomicBlock(b)
    41  }
    42  
    43  func NewApricotAtomicBlock(
    44  	parentID ids.ID,
    45  	height uint64,
    46  	tx *txs.Tx,
    47  ) (*ApricotAtomicBlock, error) {
    48  	blk := &ApricotAtomicBlock{
    49  		CommonBlock: CommonBlock{
    50  			PrntID: parentID,
    51  			Hght:   height,
    52  		},
    53  		Tx: tx,
    54  	}
    55  	return blk, initialize(blk, &blk.CommonBlock)
    56  }