-
Notifications
You must be signed in to change notification settings - Fork 662
/
builder.go
90 lines (81 loc) · 2.07 KB
/
builder.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vertex
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/consensus/avalanche"
"github.com/ava-labs/avalanchego/snow/consensus/snowstorm"
"github.com/ava-labs/avalanchego/utils/hashing"
)
// Builder builds a vertex given a set of parentIDs and transactions.
type Builder interface {
// Build a new vertex from the contents of a vertex
BuildVtx(parentIDs []ids.ID, txs []snowstorm.Tx) (avalanche.Vertex, error)
// Build a new stop vertex from the parents
BuildStopVtx(parentIDs []ids.ID) (avalanche.Vertex, error)
}
// Build a new stateless vertex from the contents of a vertex
func Build(
chainID ids.ID,
height uint64,
parentIDs []ids.ID,
txs [][]byte,
) (StatelessVertex, error) {
return buildVtx(
chainID,
height,
parentIDs,
txs,
func(vtx innerStatelessVertex) error {
return vtx.verify()
},
false,
)
}
// Build a new stateless vertex from the contents of a vertex
func BuildStopVertex(chainID ids.ID, height uint64, parentIDs []ids.ID) (StatelessVertex, error) {
return buildVtx(
chainID,
height,
parentIDs,
nil,
func(vtx innerStatelessVertex) error {
return vtx.verifyStopVertex()
},
true,
)
}
func buildVtx(
chainID ids.ID,
height uint64,
parentIDs []ids.ID,
txs [][]byte,
verifyFunc func(innerStatelessVertex) error,
stopVertex bool,
) (StatelessVertex, error) {
ids.SortIDs(parentIDs)
SortHashOf(txs)
codecVer := codecVersion
if stopVertex {
// use new codec version for the "StopVertex"
codecVer = codecVersionWithStopVtx
}
innerVtx := innerStatelessVertex{
Version: codecVer,
ChainID: chainID,
Height: height,
Epoch: 0,
ParentIDs: parentIDs,
Txs: txs,
}
if err := verifyFunc(innerVtx); err != nil {
return nil, err
}
vtxBytes, err := c.Marshal(innerVtx.Version, innerVtx)
vtx := statelessVertex{
innerStatelessVertex: innerVtx,
id: hashing.ComputeHash256Array(vtxBytes),
bytes: vtxBytes,
}
return vtx, err
}