-
Notifications
You must be signed in to change notification settings - Fork 661
/
standard_block.go
92 lines (76 loc) · 2 KB
/
standard_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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package blocks
import (
"fmt"
"time"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/vms/avm/txs"
)
var _ Block = (*StandardBlock)(nil)
type StandardBlock struct {
// parent's ID
PrntID ids.ID `serialize:"true" json:"parentID"`
// This block's height. The genesis block is at height 0.
Hght uint64 `serialize:"true" json:"height"`
Time uint64 `serialize:"true" json:"time"`
Root ids.ID `serialize:"true" json:"merkleRoot"`
// List of transactions contained in this block.
Transactions []*txs.Tx `serialize:"true" json:"txs"`
BlockID ids.ID `json:"id"`
bytes []byte
}
func (b *StandardBlock) initialize(bytes []byte, cm codec.Manager) error {
b.BlockID = hashing.ComputeHash256Array(bytes)
b.bytes = bytes
for _, tx := range b.Transactions {
if err := tx.Initialize(cm); err != nil {
return fmt.Errorf("failed to initialize tx: %w", err)
}
}
return nil
}
func (b *StandardBlock) InitCtx(ctx *snow.Context) {
for _, tx := range b.Transactions {
tx.Unsigned.InitCtx(ctx)
}
}
func (b *StandardBlock) ID() ids.ID {
return b.BlockID
}
func (b *StandardBlock) Parent() ids.ID {
return b.PrntID
}
func (b *StandardBlock) Height() uint64 {
return b.Hght
}
func (b *StandardBlock) Timestamp() time.Time {
return time.Unix(int64(b.Time), 0)
}
func (b *StandardBlock) MerkleRoot() ids.ID {
return b.Root
}
func (b *StandardBlock) Txs() []*txs.Tx {
return b.Transactions
}
func (b *StandardBlock) Bytes() []byte {
return b.bytes
}
func NewStandardBlock(
parentID ids.ID,
height uint64,
timestamp time.Time,
txs []*txs.Tx,
cm codec.Manager,
) (*StandardBlock, error) {
blk := &StandardBlock{
PrntID: parentID,
Hght: height,
Time: uint64(timestamp.Unix()),
Transactions: txs,
}
return blk, initialize(blk, cm)
}