github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/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/ids"
    11  	"github.com/MetalBlockchain/metalgo/snow"
    12  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    13  )
    14  
    15  var (
    16  	_ BanffBlock = (*BanffStandardBlock)(nil)
    17  	_ Block      = (*ApricotStandardBlock)(nil)
    18  )
    19  
    20  type BanffStandardBlock struct {
    21  	Time                 uint64 `serialize:"true" json:"time"`
    22  	ApricotStandardBlock `serialize:"true"`
    23  }
    24  
    25  func (b *BanffStandardBlock) Timestamp() time.Time {
    26  	return time.Unix(int64(b.Time), 0)
    27  }
    28  
    29  func (b *BanffStandardBlock) Visit(v Visitor) error {
    30  	return v.BanffStandardBlock(b)
    31  }
    32  
    33  func NewBanffStandardBlock(
    34  	timestamp time.Time,
    35  	parentID ids.ID,
    36  	height uint64,
    37  	txs []*txs.Tx,
    38  ) (*BanffStandardBlock, error) {
    39  	blk := &BanffStandardBlock{
    40  		Time: uint64(timestamp.Unix()),
    41  		ApricotStandardBlock: ApricotStandardBlock{
    42  			CommonBlock: CommonBlock{
    43  				PrntID: parentID,
    44  				Hght:   height,
    45  			},
    46  			Transactions: txs,
    47  		},
    48  	}
    49  	return blk, initialize(blk, &blk.CommonBlock)
    50  }
    51  
    52  type ApricotStandardBlock struct {
    53  	CommonBlock  `serialize:"true"`
    54  	Transactions []*txs.Tx `serialize:"true" json:"txs"`
    55  }
    56  
    57  func (b *ApricotStandardBlock) initialize(bytes []byte) error {
    58  	b.CommonBlock.initialize(bytes)
    59  	for _, tx := range b.Transactions {
    60  		if err := tx.Initialize(txs.Codec); err != nil {
    61  			return fmt.Errorf("failed to initialize tx: %w", err)
    62  		}
    63  	}
    64  	return nil
    65  }
    66  
    67  func (b *ApricotStandardBlock) InitCtx(ctx *snow.Context) {
    68  	for _, tx := range b.Transactions {
    69  		tx.Unsigned.InitCtx(ctx)
    70  	}
    71  }
    72  
    73  func (b *ApricotStandardBlock) Txs() []*txs.Tx {
    74  	return b.Transactions
    75  }
    76  
    77  func (b *ApricotStandardBlock) Visit(v Visitor) error {
    78  	return v.ApricotStandardBlock(b)
    79  }
    80  
    81  // NewApricotStandardBlock is kept for testing purposes only.
    82  // Following Banff activation and subsequent code cleanup, Apricot Standard blocks
    83  // should be only verified (upon bootstrap), never created anymore
    84  func NewApricotStandardBlock(
    85  	parentID ids.ID,
    86  	height uint64,
    87  	txs []*txs.Tx,
    88  ) (*ApricotStandardBlock, error) {
    89  	blk := &ApricotStandardBlock{
    90  		CommonBlock: CommonBlock{
    91  			PrntID: parentID,
    92  			Hght:   height,
    93  		},
    94  		Transactions: txs,
    95  	}
    96  	return blk, initialize(blk, &blk.CommonBlock)
    97  }