-
Notifications
You must be signed in to change notification settings - Fork 11
/
types.go
165 lines (140 loc) · 3.86 KB
/
types.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package types
import (
"bytes"
"fmt"
"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/internal/libs/protoio"
tmbytes "github.com/dashpay/tenderdash/libs/bytes"
)
// IsZero returns true when the object is a zero-value or nil
func (m *BlockID) IsZero() bool {
return m == nil || (len(m.Hash) == 0 && m.PartSetHeader.IsZero() && len(m.StateID) == 0)
}
func (m *BlockID) ToCanonicalBlockID() *CanonicalBlockID {
if m == nil || m.IsZero() {
return nil
}
cbid := CanonicalBlockID{
Hash: m.Hash,
PartSetHeader: m.PartSetHeader.ToCanonicalPartSetHeader(),
}
return &cbid
}
func (m *PartSetHeader) ToCanonicalPartSetHeader() CanonicalPartSetHeader {
if m == nil || m.IsZero() {
return CanonicalPartSetHeader{}
}
cps := CanonicalPartSetHeader(*m)
return cps
}
// IsZero returns true when the object is a zero-value or nil
func (m *PartSetHeader) IsZero() bool {
return m == nil || len(m.Hash) == 0
}
// SignBytes represent data to be signed for the given vote.
// It's a 64-byte slice containing concatenation of:
// * Checksum of CanonicalVote
// * Checksum of StateID
func (m Vote) SignBytes(chainID string) ([]byte, error) {
pbVote, err := m.ToCanonicalVote(chainID)
if err != nil {
return nil, err
}
return tmbytes.MarshalFixedSize(pbVote)
}
// CanonicalizeVote transforms the given Vote to a CanonicalVote, which does
// not contain ValidatorIndex and ValidatorProTxHash fields.
func (m Vote) ToCanonicalVote(chainID string) (CanonicalVote, error) {
var (
blockIDBytes []byte
stateIDBytes []byte
err error
)
blockID := m.BlockID.ToCanonicalBlockID()
if blockID != nil {
if blockIDBytes, err = blockID.Checksum(); err != nil {
return CanonicalVote{}, err
}
stateIDBytes = m.BlockID.StateID
} else {
blockIDBytes = crypto.Checksum(nil)
stateIDBytes = crypto.Checksum(nil)
}
return CanonicalVote{
Type: m.Type,
Height: m.Height, // encoded as sfixed64
Round: int64(m.Round), // encoded as sfixed64
BlockID: blockIDBytes,
StateID: stateIDBytes,
ChainID: chainID,
}, nil
}
func (s StateID) signBytes() ([]byte, error) {
marshaled, err := protoio.MarshalDelimited(&s)
if err != nil {
return nil, err
}
return marshaled, nil
}
// Hash calculates hash of a StateID to be used in BlockID and other places.
// It will panic() in case of (very unlikely) error.
func (s StateID) Hash() (bz []byte) {
var err error
if bz, err = s.signBytes(); err != nil {
panic("cannot marshal: " + err.Error())
}
return crypto.Checksum(bz)
}
var zeroAppHash = make([]byte, crypto.DefaultAppHashSize)
func (s *StateID) IsZero() bool {
return s == nil ||
((len(s.AppHash) == 0 || bytes.Equal(s.AppHash, zeroAppHash)) &&
s.AppVersion == 0 &&
s.CoreChainLockedHeight == 0 &&
s.Height == 0 &&
s.Time == 0)
}
// Copy returns new StateID that is equal to this one
func (s StateID) Copy() StateID {
copied := s
copied.AppHash = make([]byte, len(s.AppHash))
copy(copied.AppHash, s.AppHash)
return copied
}
func (s StateID) String() string {
return fmt.Sprintf(
`v%d:h=%d,cl=%d,ah=%s,t=%d`,
s.AppVersion,
s.Height,
s.CoreChainLockedHeight,
tmbytes.HexBytes(s.AppHash).ShortString(),
s.Time,
)
}
// Equal returns true if the StateID matches the given StateID
func (s StateID) Equal(other StateID) bool {
left, err := s.signBytes()
if err != nil {
panic("cannot marshal stateID: " + err.Error())
}
right, err := other.signBytes()
if err != nil {
panic("cannot marshal stateID: " + err.Error())
}
return bytes.Equal(left, right)
}
// ValidateBasic performs basic validation.
func (s StateID) ValidateBasic() error {
if s.Time == 0 {
return fmt.Errorf("invalid stateID time %d", s.Time)
}
if len(s.AppHash) != crypto.DefaultAppHashSize {
return fmt.Errorf(
"invalid apphash %X len, expected: %d, got: %d",
s.AppHash,
crypto.DefaultAppHashSize,
len(s.AppHash),
)
}
return nil
}