-
Notifications
You must be signed in to change notification settings - Fork 662
/
stateless_vertex.go
108 lines (93 loc) · 3.76 KB
/
stateless_vertex.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
107
108
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package vertex
import (
"errors"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/components/verify"
)
const (
// maxNumParents is the max number of parents a vertex may have
maxNumParents = 128
// maxTxsPerVtx is the max number of transactions a vertex may have
maxTxsPerVtx = 128
)
var (
errBadVersion = errors.New("invalid version")
errBadEpoch = errors.New("invalid epoch")
errFutureField = errors.New("field specified in a previous version")
errTooManyparentIDs = fmt.Errorf("vertex contains more than %d parentIDs", maxNumParents)
errNoOperations = errors.New("vertex contains no operations")
errTooManyTxs = fmt.Errorf("vertex contains more than %d transactions", maxTxsPerVtx)
errTooManyRestrictions = fmt.Errorf("vertex contains more than %d restrictions", maxTxsPerVtx)
errInvalidParents = errors.New("vertex contains non-sorted or duplicated parentIDs")
errInvalidRestrictions = errors.New("vertex contains non-sorted or duplicated restrictions")
errInvalidTxs = errors.New("vertex contains non-sorted or duplicated transactions")
_ StatelessVertex = statelessVertex{}
)
type StatelessVertex interface {
verify.Verifiable
ID() ids.ID
Bytes() []byte
Version() uint16
ChainID() ids.ID
Height() uint64
Epoch() uint32
ParentIDs() []ids.ID
Txs() [][]byte
Restrictions() []ids.ID
}
type statelessVertex struct {
// This wrapper exists so that the function calls aren't ambiguous
innerStatelessVertex
// cache the ID of this vertex
id ids.ID
// cache the binary format of this vertex
bytes []byte
}
func (v statelessVertex) ID() ids.ID { return v.id }
func (v statelessVertex) Bytes() []byte { return v.bytes }
func (v statelessVertex) Version() uint16 { return v.innerStatelessVertex.Version }
func (v statelessVertex) ChainID() ids.ID { return v.innerStatelessVertex.ChainID }
func (v statelessVertex) Height() uint64 { return v.innerStatelessVertex.Height }
func (v statelessVertex) Epoch() uint32 { return v.innerStatelessVertex.Epoch }
func (v statelessVertex) ParentIDs() []ids.ID { return v.innerStatelessVertex.ParentIDs }
func (v statelessVertex) Txs() [][]byte { return v.innerStatelessVertex.Txs }
func (v statelessVertex) Restrictions() []ids.ID { return v.innerStatelessVertex.Restrictions }
type innerStatelessVertex struct {
Version uint16 `json:"version"`
ChainID ids.ID `serializeV0:"true" serializeV1:"true" json:"chainID"`
Height uint64 `serializeV0:"true" serializeV1:"true" json:"height"`
Epoch uint32 `serializeV0:"true" serializeV1:"true" json:"epoch"`
ParentIDs []ids.ID `serializeV0:"true" serializeV1:"true" len:"128" json:"parentIDs"`
Txs [][]byte `serializeV0:"true" serializeV1:"true" len:"128" json:"txs"`
Restrictions []ids.ID `serializeV1:"true" len:"128" json:"restrictions"`
}
func (v innerStatelessVertex) Verify() error {
switch {
case v.Version != 0:
return errBadVersion
case v.Epoch != 0:
return errBadEpoch
case len(v.Restrictions) != 0:
return errFutureField
// TODO: Remove the above checks once the apricot release is ready
case len(v.ParentIDs) > maxNumParents:
return errTooManyparentIDs
case len(v.Txs)+len(v.Restrictions) == 0:
return errNoOperations
case len(v.Txs) > maxTxsPerVtx:
return errTooManyTxs
case len(v.Restrictions) > maxTxsPerVtx:
return errTooManyRestrictions
case !ids.IsSortedAndUniqueIDs(v.ParentIDs):
return errInvalidParents
case !ids.IsSortedAndUniqueIDs(v.Restrictions):
return errInvalidRestrictions
case !IsSortedAndUniqueHashOf(v.Txs):
return errInvalidTxs
default:
return nil
}
}