github.com/MetalBlockchain/metalgo@v1.11.9/vms/avm/block/standard_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  	"time"
     9  
    10  	"github.com/MetalBlockchain/metalgo/codec"
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  	"github.com/MetalBlockchain/metalgo/snow"
    13  	"github.com/MetalBlockchain/metalgo/utils/hashing"
    14  	"github.com/MetalBlockchain/metalgo/vms/avm/txs"
    15  )
    16  
    17  var _ Block = (*StandardBlock)(nil)
    18  
    19  type StandardBlock struct {
    20  	// parent's ID
    21  	PrntID ids.ID `serialize:"true" json:"parentID"`
    22  	// This block's height. The genesis block is at height 0.
    23  	Hght uint64 `serialize:"true" json:"height"`
    24  	Time uint64 `serialize:"true" json:"time"`
    25  	Root ids.ID `serialize:"true" json:"merkleRoot"`
    26  	// List of transactions contained in this block.
    27  	Transactions []*txs.Tx `serialize:"true" json:"txs"`
    28  
    29  	BlockID ids.ID `json:"id"`
    30  	bytes   []byte
    31  }
    32  
    33  func (b *StandardBlock) initialize(bytes []byte, cm codec.Manager) error {
    34  	b.BlockID = hashing.ComputeHash256Array(bytes)
    35  	b.bytes = bytes
    36  	for _, tx := range b.Transactions {
    37  		if err := tx.Initialize(cm); err != nil {
    38  			return fmt.Errorf("failed to initialize tx: %w", err)
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  func (b *StandardBlock) InitCtx(ctx *snow.Context) {
    45  	for _, tx := range b.Transactions {
    46  		tx.Unsigned.InitCtx(ctx)
    47  	}
    48  }
    49  
    50  func (b *StandardBlock) ID() ids.ID {
    51  	return b.BlockID
    52  }
    53  
    54  func (b *StandardBlock) Parent() ids.ID {
    55  	return b.PrntID
    56  }
    57  
    58  func (b *StandardBlock) Height() uint64 {
    59  	return b.Hght
    60  }
    61  
    62  func (b *StandardBlock) Timestamp() time.Time {
    63  	return time.Unix(int64(b.Time), 0)
    64  }
    65  
    66  func (b *StandardBlock) MerkleRoot() ids.ID {
    67  	return b.Root
    68  }
    69  
    70  func (b *StandardBlock) Txs() []*txs.Tx {
    71  	return b.Transactions
    72  }
    73  
    74  func (b *StandardBlock) Bytes() []byte {
    75  	return b.bytes
    76  }
    77  
    78  func NewStandardBlock(
    79  	parentID ids.ID,
    80  	height uint64,
    81  	timestamp time.Time,
    82  	txs []*txs.Tx,
    83  	cm codec.Manager,
    84  ) (*StandardBlock, error) {
    85  	blk := &StandardBlock{
    86  		PrntID:       parentID,
    87  		Hght:         height,
    88  		Time:         uint64(timestamp.Unix()),
    89  		Transactions: txs,
    90  	}
    91  
    92  	// We serialize this block as a pointer so that it can be deserialized into
    93  	// a Block
    94  	var blkIntf Block = blk
    95  	bytes, err := cm.Marshal(CodecVersion, &blkIntf)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("couldn't marshal block: %w", err)
    98  	}
    99  
   100  	blk.BlockID = hashing.ComputeHash256Array(bytes)
   101  	blk.bytes = bytes
   102  	return blk, nil
   103  }