github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/proposal_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 = (*BanffProposalBlock)(nil) 17 _ Block = (*ApricotProposalBlock)(nil) 18 ) 19 20 type BanffProposalBlock struct { 21 Time uint64 `serialize:"true" json:"time"` 22 Transactions []*txs.Tx `serialize:"true" json:"txs"` 23 ApricotProposalBlock `serialize:"true"` 24 } 25 26 func (b *BanffProposalBlock) initialize(bytes []byte) error { 27 if err := b.ApricotProposalBlock.initialize(bytes); err != nil { 28 return err 29 } 30 for _, tx := range b.Transactions { 31 if err := tx.Initialize(txs.Codec); err != nil { 32 return fmt.Errorf("failed to initialize tx: %w", err) 33 } 34 } 35 return nil 36 } 37 38 func (b *BanffProposalBlock) InitCtx(ctx *snow.Context) { 39 for _, tx := range b.Transactions { 40 tx.Unsigned.InitCtx(ctx) 41 } 42 b.ApricotProposalBlock.InitCtx(ctx) 43 } 44 45 func (b *BanffProposalBlock) Timestamp() time.Time { 46 return time.Unix(int64(b.Time), 0) 47 } 48 49 func (b *BanffProposalBlock) Txs() []*txs.Tx { 50 l := len(b.Transactions) 51 txs := make([]*txs.Tx, l+1) 52 copy(txs, b.Transactions) 53 txs[l] = b.Tx 54 return txs 55 } 56 57 func (b *BanffProposalBlock) Visit(v Visitor) error { 58 return v.BanffProposalBlock(b) 59 } 60 61 func NewBanffProposalBlock( 62 timestamp time.Time, 63 parentID ids.ID, 64 height uint64, 65 proposalTx *txs.Tx, 66 decisionTxs []*txs.Tx, 67 ) (*BanffProposalBlock, error) { 68 blk := &BanffProposalBlock{ 69 Transactions: decisionTxs, 70 Time: uint64(timestamp.Unix()), 71 ApricotProposalBlock: ApricotProposalBlock{ 72 CommonBlock: CommonBlock{ 73 PrntID: parentID, 74 Hght: height, 75 }, 76 Tx: proposalTx, 77 }, 78 } 79 return blk, initialize(blk, &blk.CommonBlock) 80 } 81 82 type ApricotProposalBlock struct { 83 CommonBlock `serialize:"true"` 84 Tx *txs.Tx `serialize:"true" json:"tx"` 85 } 86 87 func (b *ApricotProposalBlock) initialize(bytes []byte) error { 88 b.CommonBlock.initialize(bytes) 89 if err := b.Tx.Initialize(txs.Codec); err != nil { 90 return fmt.Errorf("failed to initialize tx: %w", err) 91 } 92 return nil 93 } 94 95 func (b *ApricotProposalBlock) InitCtx(ctx *snow.Context) { 96 b.Tx.Unsigned.InitCtx(ctx) 97 } 98 99 func (b *ApricotProposalBlock) Txs() []*txs.Tx { 100 return []*txs.Tx{b.Tx} 101 } 102 103 func (b *ApricotProposalBlock) Visit(v Visitor) error { 104 return v.ApricotProposalBlock(b) 105 } 106 107 // NewApricotProposalBlock is kept for testing purposes only. 108 // Following Banff activation and subsequent code cleanup, Apricot Proposal blocks 109 // should be only verified (upon bootstrap), never created anymore 110 func NewApricotProposalBlock( 111 parentID ids.ID, 112 height uint64, 113 tx *txs.Tx, 114 ) (*ApricotProposalBlock, error) { 115 blk := &ApricotProposalBlock{ 116 CommonBlock: CommonBlock{ 117 PrntID: parentID, 118 Hght: height, 119 }, 120 Tx: tx, 121 } 122 return blk, initialize(blk, &blk.CommonBlock) 123 }