-
Notifications
You must be signed in to change notification settings - Fork 664
/
block.go
102 lines (84 loc) · 1.94 KB
/
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package snowmantest
import (
"cmp"
"context"
"time"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils"
)
const (
GenesisHeight uint64 = 0
GenesisUnixTimestamp int64 = 1
)
var (
_ utils.Sortable[*Block] = (*Block)(nil)
GenesisID = ids.GenerateTestID()
GenesisTimestamp = time.Unix(GenesisUnixTimestamp, 0)
GenesisBytes = GenesisID[:]
Genesis = BuildChain(1)[0]
)
func BuildChild(parent *Block) *Block {
blkID := ids.GenerateTestID()
return &Block{
TestDecidable: choices.TestDecidable{
IDV: blkID,
StatusV: choices.Processing,
},
ParentV: parent.ID(),
HeightV: parent.Height() + 1,
TimestampV: parent.Timestamp(),
BytesV: blkID[:],
}
}
func BuildChain(length int) []*Block {
if length == 0 {
return nil
}
genesis := &Block{
TestDecidable: choices.TestDecidable{
IDV: GenesisID,
StatusV: choices.Accepted,
},
HeightV: GenesisHeight,
TimestampV: GenesisTimestamp,
BytesV: GenesisBytes,
}
return append([]*Block{genesis}, BuildDescendants(genesis, length-1)...)
}
func BuildDescendants(parent *Block, length int) []*Block {
chain := make([]*Block, length)
for i := range chain {
parent = BuildChild(parent)
chain[i] = parent
}
return chain
}
type Block struct {
choices.TestDecidable
ParentV ids.ID
HeightV uint64
TimestampV time.Time
VerifyV error
BytesV []byte
}
func (b *Block) Parent() ids.ID {
return b.ParentV
}
func (b *Block) Height() uint64 {
return b.HeightV
}
func (b *Block) Timestamp() time.Time {
return b.TimestampV
}
func (b *Block) Verify(context.Context) error {
return b.VerifyV
}
func (b *Block) Bytes() []byte {
return b.BytesV
}
func (b *Block) Compare(other *Block) int {
return cmp.Compare(b.HeightV, other.HeightV)
}