-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencoding.go
More file actions
298 lines (238 loc) · 7.83 KB
/
encoding.go
File metadata and controls
298 lines (238 loc) · 7.83 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex
import (
"context"
"encoding/binary"
"errors"
"fmt"
"github.com/ava-labs/simplex/record"
)
type QuorumRecord struct {
QC []byte
Vote []byte
}
func (qr *QuorumRecord) FromBytes(buff []byte) error {
if len(buff) < 4 {
return errors.New("buffer too small to contain vote length")
}
voteLen := binary.BigEndian.Uint32(buff[0:4])
if len(buff) < int(4+voteLen) {
return errors.New("buffer too small to contain vote")
}
if int(voteLen) > len(buff)-4 {
return errors.New("vote length exceeds buffer size")
}
qr.Vote = make([]byte, voteLen)
copy(qr.Vote, buff[4:4+voteLen])
qr.QC = make([]byte, len(buff)-int(4+voteLen))
copy(qr.QC, buff[4+voteLen:])
return nil
}
func (qr *QuorumRecord) Bytes() []byte {
voteLenBuff := make([]byte, 4)
binary.BigEndian.PutUint32(voteLenBuff, uint32(len(qr.Vote)))
buff := make([]byte, 4+len(qr.QC)+len(qr.Vote))
copy(buff[0:4], voteLenBuff)
copy(buff[4:4+len(qr.Vote)], qr.Vote)
copy(buff[4+len(qr.Vote):], qr.QC)
return buff
}
func FinalizationFromRecord(record []byte, qd QCDeserializer) (Finalization, error) {
qcBytes, finalization, err := parseFinalizationRecord(record)
if err != nil {
return Finalization{}, err
}
qc, err := qd.DeserializeQuorumCertificate(qcBytes)
if err != nil {
return Finalization{}, err
}
return Finalization{
Finalization: finalization,
QC: qc,
}, nil
}
func parseFinalizationRecord(payload []byte) ([]byte, ToBeSignedFinalization, error) {
payload = payload[2:]
var nr QuorumRecord
if err := nr.FromBytes(payload); err != nil {
return nil, ToBeSignedFinalization{}, err
}
var finalization ToBeSignedFinalization
if err := finalization.FromBytes(nr.Vote); err != nil {
return nil, ToBeSignedFinalization{}, err
}
return nr.QC, finalization, nil
}
func NewQuorumRecord(qc []byte, rawVote []byte, recordType uint16) []byte {
var qr QuorumRecord
qr.QC = qc
qr.Vote = rawVote
payload := qr.Bytes()
buff := make([]byte, len(payload)+2)
binary.BigEndian.PutUint16(buff, recordType)
copy(buff[2:], payload)
return buff
}
// ParseNotarizationRecordBytes parses a notarization record into the bytes of the QC and the vote
func ParseNotarizationRecord(r []byte) ([]byte, ToBeSignedVote, error) {
recordType := binary.BigEndian.Uint16(r)
if recordType != record.NotarizationRecordType {
return nil, ToBeSignedVote{}, fmt.Errorf("expected record type %d, got %d", record.NotarizationRecordType, recordType)
}
record := r[2:]
var nr QuorumRecord
if err := nr.FromBytes(record); err != nil {
return nil, ToBeSignedVote{}, err
}
var vote ToBeSignedVote
if err := vote.FromBytes(nr.Vote); err != nil {
return nil, ToBeSignedVote{}, err
}
return nr.QC, vote, nil
}
func NotarizationFromRecord(record []byte, qd QCDeserializer) (Notarization, error) {
qcBytes, vote, err := ParseNotarizationRecord(record)
if err != nil {
return Notarization{}, err
}
qc, err := qd.DeserializeQuorumCertificate(qcBytes)
if err != nil {
return Notarization{}, err
}
return Notarization{
Vote: vote,
QC: qc,
}, nil
}
func BlockRecord(bh BlockHeader, blockData []byte) []byte {
mdBytes := bh.Bytes()
buff := make([]byte, len(mdBytes)+len(blockData)+2)
binary.BigEndian.PutUint16(buff, record.BlockRecordType)
copy(buff[2:], mdBytes)
copy(buff[2+BlockHeaderLen:], blockData)
return buff
}
func BlockFromRecord(ctx context.Context, blockDeserializer BlockDeserializer, record []byte) (Block, error) {
_, payload, err := ParseBlockRecord(record)
if err != nil {
return nil, err
}
return blockDeserializer.DeserializeBlock(ctx, payload)
}
func ParseBlockRecord(buff []byte) (BlockHeader, []byte, error) {
recordType := binary.BigEndian.Uint16(buff)
if recordType != record.BlockRecordType {
return BlockHeader{}, nil, fmt.Errorf("expected record type %d, got %d", record.BlockRecordType, recordType)
}
buff = buff[2:]
if len(buff) < BlockHeaderLen {
return BlockHeader{}, nil, fmt.Errorf("buffer too small, expected %d bytes", BlockHeaderLen+2)
}
var bh BlockHeader
if err := bh.FromBytes(buff[:BlockHeaderLen]); err != nil {
return BlockHeader{}, nil, fmt.Errorf("failed to deserialize block metadata: %w", err)
}
buff = buff[BlockHeaderLen:]
if len(buff) == 0 {
return BlockHeader{}, nil, fmt.Errorf("buffer too small, expected block data but gone none")
}
return bh, buff, nil
}
func ParseEmptyNotarizationRecord(buff []byte) ([]byte, ToBeSignedEmptyVote, error) {
recordType := binary.BigEndian.Uint16(buff[:2])
if recordType != record.EmptyNotarizationRecordType {
return nil, ToBeSignedEmptyVote{}, fmt.Errorf("expected record type %d, got %d", record.NotarizationRecordType, recordType)
}
record := buff[2:]
var nr QuorumRecord
if err := nr.FromBytes(record); err != nil {
return nil, ToBeSignedEmptyVote{}, err
}
var vote ToBeSignedEmptyVote
if err := vote.FromBytes(nr.Vote); err != nil {
return nil, ToBeSignedEmptyVote{}, err
}
return nr.QC, vote, nil
}
func NewEmptyVoteRecord(emptyVote ToBeSignedEmptyVote) []byte {
payload := emptyVote.Bytes()
buff := make([]byte, len(payload)+2)
binary.BigEndian.PutUint16(buff, record.EmptyVoteRecordType)
copy(buff[2:], payload)
return buff
}
func ParseEmptyVoteRecord(rawEmptyVote []byte) (ToBeSignedEmptyVote, error) {
if len(rawEmptyVote) < 2 {
return ToBeSignedEmptyVote{}, errors.New("expected at least two bytes")
}
recordType := binary.BigEndian.Uint16(rawEmptyVote[:2])
if recordType != record.EmptyVoteRecordType {
return ToBeSignedEmptyVote{}, fmt.Errorf("expected record type %d, got %d", record.EmptyVoteRecordType, recordType)
}
var emptyVote ToBeSignedEmptyVote
if err := emptyVote.FromBytes(rawEmptyVote[2:]); err != nil {
return ToBeSignedEmptyVote{}, err
}
return emptyVote, nil
}
func BlockRecordRetentionTerm(record []byte) (uint64, error) {
if len(record) < 19 {
return 0, fmt.Errorf("record too short to extract round, expected at least 19 bytes, got %d", len(record))
}
var pos int
// First 2 bytes are for record type
pos += 2
// The next 9 bytes are for version and epoch.
pos += 9
// The next 8 bytes are for round.
round := binary.BigEndian.Uint64(record[pos : pos+8])
return round, nil
}
func QuorumRecordRetentionTerm(record []byte) (uint64, error) {
if len(record) < 23 {
return 0, fmt.Errorf("record too short to extract round, expected at least 23 bytes, got %d", len(record))
}
var pos int
// First 2 bytes are for record type
pos += 2
// Next 4 bytes are for the size of the vote
pos += 4
// The next 9 bytes are for version and epoch.
pos += 9
// The next 8 bytes are for round.
round := binary.BigEndian.Uint64(record[pos : pos+8])
return round, nil
}
func EmptyVoteRecordRetentionTerm(record []byte) (uint64, error) {
if len(record) < 19 {
return 0, fmt.Errorf("record too short to extract round, expected at least 23 bytes, got %d", len(record))
}
var pos int
// First 2 bytes are for record type
pos += 2
// The next 9 bytes are for version and epoch.
pos += 9
// The next 8 bytes are for round.
round := binary.BigEndian.Uint64(record[pos : pos+8])
return round, nil
}
type WALRetentionReader struct{}
func (wrr *WALRetentionReader) RetentionTerm(entry []byte) (uint64, error) {
if len(entry) < 2 {
return 0, fmt.Errorf("entry too short to extract record type, expected at least 2 bytes, got %d", len(entry))
}
recordType := binary.BigEndian.Uint16(entry[:2])
switch recordType {
case record.BlockRecordType:
return BlockRecordRetentionTerm(entry)
case record.NotarizationRecordType:
return QuorumRecordRetentionTerm(entry)
case record.EmptyNotarizationRecordType:
return QuorumRecordRetentionTerm(entry)
case record.EmptyVoteRecordType:
return EmptyVoteRecordRetentionTerm(entry)
default:
return 0, fmt.Errorf("unknown record type %d for retention term extraction", recordType)
}
}