-
Notifications
You must be signed in to change notification settings - Fork 177
/
header.go
215 lines (185 loc) · 7.19 KB
/
header.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package flow
import (
"encoding/json"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/vmihailenco/msgpack/v4"
cborcodec "github.com/onflow/flow-go/model/encoding/cbor"
"github.com/onflow/flow-go/model/fingerprint"
)
// Header contains all meta-data for a block, as well as a hash representing
// the combined payload of the entire block. It is what consensus nodes agree
// on after validating the contents against the payload hash.
type Header struct {
// ChainID is a chain-specific value to prevent replay attacks.
ChainID ChainID
// ParentID is the ID of this block's parent.
ParentID Identifier
// Height is the height of the parent + 1
Height uint64
// PayloadHash is a hash of the payload of this block.
PayloadHash Identifier
// Timestamp is the time at which this block was proposed.
// The proposer can choose any time, so this should not be trusted as accurate.
Timestamp time.Time
// View number at which this block was proposed.
View uint64
// ParentView number at which parent block was proposed.
ParentView uint64
// ParentVoterIndices is a bitvector that represents all the voters for the parent block.
ParentVoterIndices []byte
// ParentVoterSigData is an aggregated signature over the parent block. Not a single cryptographic
// signature since the data represents cryptographic signatures serialized in some way (concatenation or other)
// A quorum certificate can be extracted from the header.
// This field is the SigData field of the extracted quorum certificate.
ParentVoterSigData []byte
// ProposerID is a proposer identifier for the block
ProposerID Identifier
// ProposerSigData is a signature of the proposer over the new block. Not a single cryptographic
// signature since the data represents cryptographic signatures serialized in some way (concatenation or other)
ProposerSigData []byte
// LastViewTC is a timeout certificate for previous view, it can be nil
// it has to be present if previous round ended with timeout.
LastViewTC *TimeoutCertificate
}
// Body returns the immutable part of the block header.
func (h Header) Body() interface{} {
return struct {
ChainID ChainID
ParentID Identifier
Height uint64
PayloadHash Identifier
Timestamp uint64
View uint64
ParentView uint64
ParentVoterIndices []byte
ParentVoterSigData []byte
ProposerID Identifier
LastViewTCID Identifier
}{
ChainID: h.ChainID,
ParentID: h.ParentID,
Height: h.Height,
PayloadHash: h.PayloadHash,
Timestamp: uint64(h.Timestamp.UnixNano()),
View: h.View,
ParentView: h.ParentView,
ParentVoterIndices: h.ParentVoterIndices,
ParentVoterSigData: h.ParentVoterSigData,
ProposerID: h.ProposerID,
LastViewTCID: h.LastViewTC.ID(),
}
}
// QuorumCertificate returns quorum certificate that is incorporated in the block header.
func (h Header) QuorumCertificate() *QuorumCertificate {
return &QuorumCertificate{
BlockID: h.ParentID,
View: h.ParentView,
SignerIndices: h.ParentVoterIndices,
SigData: h.ParentVoterSigData,
}
}
func (h Header) Fingerprint() []byte {
return fingerprint.Fingerprint(h.Body())
}
// ID returns a unique ID to singularly identify the header and its block
// within the flow system.
func (h Header) ID() Identifier {
return MakeID(h)
}
// Checksum returns the checksum of the header.
func (h Header) Checksum() Identifier {
return MakeID(h)
}
// MarshalJSON makes sure the timestamp is encoded in UTC.
func (h Header) MarshalJSON() ([]byte, error) {
// NOTE: this is just a sanity check to make sure that we don't get
// different encodings if someone forgets to use UTC timestamps
if h.Timestamp.Location() != time.UTC {
h.Timestamp = h.Timestamp.UTC()
}
// we use an alias to avoid endless recursion; the alias will not have the
// marshal function and encode like a raw header
type Encodable Header
return json.Marshal(struct {
Encodable
ID string
}{
Encodable: Encodable(h),
ID: h.ID().String(),
})
}
// UnmarshalJSON makes sure the timestamp is decoded in UTC.
func (h *Header) UnmarshalJSON(data []byte) error {
// we use an alias to avoid endless recursion; the alias will not have the
// unmarshal function and decode like a raw header
type Decodable *Header
err := json.Unmarshal(data, Decodable(h))
// NOTE: the timezone check is not required for JSON, as it already encodes
// timezones, but it doesn't hurt to add it in case someone messes with the
// raw encoded format
if h.Timestamp.Location() != time.UTC {
h.Timestamp = h.Timestamp.UTC()
}
return err
}
// MarshalCBOR makes sure the timestamp is encoded in UTC.
func (h Header) MarshalCBOR() ([]byte, error) {
// NOTE: this is just a sanity check to make sure that we don't get
// different encodings if someone forgets to use UTC timestamps
if h.Timestamp.Location() != time.UTC {
h.Timestamp = h.Timestamp.UTC()
}
// we use an alias to avoid endless recursion; the alias will not have the
// marshal function and encode like a raw header
type Encodable Header
return cborcodec.EncMode.Marshal(Encodable(h))
}
// UnmarshalCBOR makes sure the timestamp is decoded in UTC.
func (h *Header) UnmarshalCBOR(data []byte) error {
// we use an alias to avoid endless recursion; the alias will not have the
// unmarshal function and decode like a raw header
// NOTE: for some reason, the pointer alias works for JSON to not recurse,
// but msgpack will still recurse; we have to do an extra struct copy here
type Decodable Header
decodable := Decodable(*h)
err := cbor.Unmarshal(data, &decodable)
*h = Header(decodable)
// NOTE: the timezone check is not required for CBOR, as it already encodes
// timezones, but it doesn't hurt to add it in case someone messes with the
// raw encoded format
if h.Timestamp.Location() != time.UTC {
h.Timestamp = h.Timestamp.UTC()
}
return err
}
// MarshalMsgpack makes sure the timestamp is encoded in UTC.
func (h Header) MarshalMsgpack() ([]byte, error) {
// NOTE: this is just a sanity check to make sure that we don't get
// different encodings if someone forgets to use UTC timestamps
if h.Timestamp.Location() != time.UTC {
h.Timestamp = h.Timestamp.UTC()
}
// we use an alias to avoid endless recursion; the alias will not have the
// marshal function and encode like a raw header
type Encodable Header
return msgpack.Marshal(Encodable(h))
}
// UnmarshalMsgpack makes sure the timestamp is decoded in UTC.
func (h *Header) UnmarshalMsgpack(data []byte) error {
// we use an alias to avoid endless recursion; the alias will not have the
// unmarshal function and decode like a raw header
// NOTE: for some reason, the pointer alias works for JSON to not recurse,
// but msgpack will still recurse; we have to do an extra struct copy here
type Decodable Header
decodable := Decodable(*h)
err := msgpack.Unmarshal(data, &decodable)
*h = Header(decodable)
// NOTE: Msgpack unmarshals timestamps with the local timezone, which means
// that a block ID would suddenly be different after encoding and decoding
// on a machine with non-UTC local time
if h.Timestamp.Location() != time.UTC {
h.Timestamp = h.Timestamp.UTC()
}
return err
}