-
Notifications
You must be signed in to change notification settings - Fork 671
/
proposal_block.go
106 lines (91 loc) · 2.52 KB
/
proposal_block.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package blocks
import (
"fmt"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)
var (
_ BanffBlock = (*BanffProposalBlock)(nil)
_ Block = (*ApricotProposalBlock)(nil)
)
type BanffProposalBlock struct {
Time uint64 `serialize:"true" json:"time"`
// Transactions is currently unused. This is populated so that introducing
// them in the future will not require a codec change.
//
// TODO: when Transactions is used, we must correctly verify and apply their
// changes.
Transactions []*txs.Tx `serialize:"true" json:"-"`
ApricotProposalBlock `serialize:"true"`
}
func (b *BanffProposalBlock) InitCtx(ctx *snow.Context) {
for _, tx := range b.Transactions {
tx.Unsigned.InitCtx(ctx)
}
b.ApricotProposalBlock.InitCtx(ctx)
}
func (b *BanffProposalBlock) Timestamp() time.Time {
return time.Unix(int64(b.Time), 0)
}
func (b *BanffProposalBlock) Visit(v Visitor) error {
return v.BanffProposalBlock(b)
}
func NewBanffProposalBlock(
timestamp time.Time,
parentID ids.ID,
height uint64,
tx *txs.Tx,
) (*BanffProposalBlock, error) {
blk := &BanffProposalBlock{
Time: uint64(timestamp.Unix()),
ApricotProposalBlock: ApricotProposalBlock{
CommonBlock: CommonBlock{
PrntID: parentID,
Hght: height,
},
Tx: tx,
},
}
return blk, initialize(blk)
}
type ApricotProposalBlock struct {
CommonBlock `serialize:"true"`
Tx *txs.Tx `serialize:"true" json:"tx"`
}
func (b *ApricotProposalBlock) initialize(bytes []byte) error {
b.CommonBlock.initialize(bytes)
if err := b.Tx.Sign(txs.Codec, nil); err != nil {
return fmt.Errorf("failed to initialize tx: %w", err)
}
return nil
}
func (b *ApricotProposalBlock) InitCtx(ctx *snow.Context) {
b.Tx.Unsigned.InitCtx(ctx)
}
func (b *ApricotProposalBlock) Txs() []*txs.Tx {
return []*txs.Tx{b.Tx}
}
func (b *ApricotProposalBlock) Visit(v Visitor) error {
return v.ApricotProposalBlock(b)
}
// NewApricotProposalBlock is kept for testing purposes only.
// Following Banff activation and subsequent code cleanup, Apricot Proposal blocks
// should be only verified (upon bootstrap), never created anymore
func NewApricotProposalBlock(
parentID ids.ID,
height uint64,
tx *txs.Tx,
) (*ApricotProposalBlock, error) {
blk := &ApricotProposalBlock{
CommonBlock: CommonBlock{
PrntID: parentID,
Hght: height,
},
Tx: tx,
}
return blk, initialize(blk)
}