forked from Factom-Asset-Tokens/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dblock.go
412 lines (351 loc) · 11.6 KB
/
dblock.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package factom
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math"
"sort"
"time"
)
var (
aBlockChainID = Bytes32{31: 0x0a}
ecBlockChainID = Bytes32{31: 0x0c}
fBlockChainID = Bytes32{31: 0x0f}
)
// ABlockChainID returns the ChainID of the Admin Block Chain, 0x00..0a.
func ABlockChainID() Bytes32 { return aBlockChainID }
// ECBlockChainID returns the ChainID of the Entry Credit Block Chain,
// 0x00..0c.
func ECBlockChainID() Bytes32 { return ecBlockChainID }
// FBlockChainID returns the ChainID of the Factoid Block Chain, 0x00..0f.
func FBlockChainID() Bytes32 { return fBlockChainID }
// DBlock represents a Factom Directory Block.
type DBlock struct {
// Computed
KeyMR *Bytes32
FullHash *Bytes32
// Unmarshaled
NetworkID NetworkID
BodyMR *Bytes32
PrevKeyMR *Bytes32
PrevFullHash *Bytes32
Height uint32
Timestamp time.Time
FBlock FBlock
// DBlock.Get populates EBlocks with their ChainID, KeyMR, Timestamp,
// and Height.
EBlocks []EBlock
// marshalBinaryCache is the binary data of the DBlock. It is cached by
// UnmarshalBinary so it can be re-used by MarshalBinary.
marshalBinaryCache []byte
}
// ClearMarshalBinaryCache discards the cached MarshalBinary data.
//
// Subsequent calls to MarshalBinary will re-construct the data from the fields
// of the DBlock.
func (db *DBlock) ClearMarshalBinaryCache() {
db.marshalBinaryCache = nil
}
// IsPopulated returns true if db has already been populated by a successful
// call to Get.
func (db DBlock) IsPopulated() bool {
return len(db.EBlocks) > 0 &&
db.KeyMR != nil &&
db.FullHash != nil &&
db.BodyMR != nil &&
db.PrevKeyMR != nil &&
db.PrevFullHash != nil &&
db.FBlock.KeyMR != nil &&
!db.Timestamp.IsZero()
}
// Get queries factomd for the Directory Block at db.Height. After a
// successful call, the EBlocks will all have their ChainID and KeyMR, but not
// their Entries. Call Get on the EBlocks individually to populate their
// Entries.
func (db *DBlock) Get(ctx context.Context, c *Client) (err error) {
if db.IsPopulated() {
return nil
}
// Normally query by height.
method := "dblock-by-height"
params := interface{}(struct {
Height uint32 `json:"height"`
}{db.Height})
var res struct {
Data Bytes `json:"rawdata"`
// DBlock nesting required for factomd API.
DBlock struct {
// Use a double pointer so that the db.KeyMR pointer
// will be populated during unmarshalling.
KeyMR **Bytes32 `json:"keymr"`
} `json:"dblock"`
}
res.DBlock.KeyMR = &db.KeyMR
result := interface{}(&res)
// If a KeyMR is specified, query for that DBlock specifically.
if db.KeyMR != nil {
method = "raw-data"
params = struct {
Hash *Bytes32 `json:"hash"`
}{Hash: db.KeyMR}
// Use a typecase to overwrite the JSON field names. Only Data
// will be populated.
result = (*struct {
Data Bytes `json:"data"`
DBlock struct {
KeyMR **Bytes32 `json:"-"`
} `json:"-"`
})(&res)
}
if err := c.FactomdRequest(ctx, method, params, result); err != nil {
return err
}
return db.UnmarshalBinary(res.Data)
}
// DBlockHeaderSize is the exact length of a DBlock header.
const DBlockHeaderSize = 1 + // [Version byte (0x00)]
4 + // NetworkID
32 + // BodyMR
32 + // PrevKeyMR
32 + // PrevFullHash
4 + // Timestamp
4 + // DB Height
4 // EBlock Count
// DBlockEBlockSize is the exact length of an EBlock within a DBlock.
const DBlockEBlockSize = 32 + // ChainID
32 // KeyMR
// DBlockMinBodySize is the minimum length of the body of a DBlock, which must
// include at least an Admin Block, EC Block, and FCT Block.
const DBlockMinBodySize = DBlockEBlockSize + // Admin Block
DBlockEBlockSize + // EC Block
DBlockEBlockSize // FCT Block
// DBlockMaxBodySize is the maximum length of the body of a DBlock, which is
// determined by the largest EBlock count that can be stored in 4 bytes.
const DBlockMaxBodySize = math.MaxUint32 * DBlockEBlockSize
// DBlockMinTotalSize is the minumum total length of a DBlock.
const DBlockMinTotalSize = DBlockHeaderSize + DBlockMinBodySize
// DBlockMaxTotalSize is the maximum length of a DBlock.
const DBlockMaxTotalSize = DBlockHeaderSize + DBlockMaxBodySize
// UnmarshalBinary unmarshals raw DBlock data, verifies the BodyMR, and
// populates db.FullHash and db.KeyMR, if nil. If db.KeyMR is populated, it is
// verified. The following format is expected for data.
//
// Header
// [Version byte (0x00)] +
// [NetworkID (4 bytes)] +
// [BodyMR (Bytes32)] +
// [PrevKeyMR (Bytes32)] +
// [PrevFullHash (Bytes32)] +
// [Timestamp (4 bytes)] +
// [DB Height (4 bytes)] +
// [EBlock Count (4 bytes)]
//
// Body
// [Admin Block ChainID (Bytes32{31:0x0a})] +
// [Admin Block LookupHash (Bytes32)] +
// [EC Block ChainID (Bytes32{31:0x0c})] +
// [EC Block HeaderHash (Bytes32)] +
// [FCT Block ChainID (Bytes32{31:0x0f})] +
// [FCT Block KeyMR (Bytes32)] +
// [ChainID 0 (Bytes32)] +
// [KeyMR 0 (Bytes32)] +
// ... +
// [ChainID N (Bytes32)] +
// [KeyMR N (Bytes32)] +
//
// https://github.com/FactomProject/FactomDocs/blob/master/factomDataStructureDetails.md#directory-block
func (db *DBlock) UnmarshalBinary(data []byte) error {
if uint64(len(data)) < DBlockMinTotalSize ||
uint64(len(data)) > DBlockMaxTotalSize {
return fmt.Errorf("invalid length")
}
// There is currently only one DBlock encoding version.
if data[0] != 0x00 {
return fmt.Errorf("invalid version byte")
}
i := 1
// Parse the Header
i += copy(db.NetworkID[:], data[i:])
db.BodyMR = new(Bytes32)
i += copy(db.BodyMR[:], data[i:])
db.PrevKeyMR = new(Bytes32)
i += copy(db.PrevKeyMR[:], data[i:])
db.PrevFullHash = new(Bytes32)
i += copy(db.PrevFullHash[:], data[i:])
db.Timestamp = time.Unix(int64(binary.BigEndian.Uint32(data[i:i+4]))*60, 0)
i += 4
db.Height = binary.BigEndian.Uint32(data[i : i+4])
i += 4
// eBlockCount defines how many EBlock ChainID|KeyMRs are in the
// DBlock.
eBlockCount := int(binary.BigEndian.Uint32(data[i : i+4]))
i += 4
// Ensure we have enough data left to read all of the EBlocks.
if eBlockCount*DBlockEBlockSize > len(data[i:]) {
return fmt.Errorf("insufficient length")
}
// elements are used to compute the BodyMR.
elements := make([][]byte, eBlockCount)
// prevChainID is used to ensure that ChainIDs are in ascending order.
var prevChainID *Bytes32
// Parse the EBlocks.
db.EBlocks = make([]EBlock, eBlockCount-1) // less one for the FBlock
for ebi := range elements {
// Ensure that ChainIDs are in ascending order.
if prevChainID != nil &&
bytes.Compare(data[i:i+len(prevChainID)],
prevChainID[:]) <= 0 {
return fmt.Errorf("out of order or duplicate Chain ID")
}
elements[ebi] = data[i : i+2*len(Bytes32{})]
// Populate ChainID, KeyMR, Timestamp, and Height.
var chainID, keyMR Bytes32
i += copy(chainID[:], data[i:])
prevChainID = &chainID
i += copy(keyMR[:], data[i:])
if chainID == fBlockChainID {
db.FBlock.KeyMR = &keyMR
db.FBlock.Timestamp = db.Timestamp
db.FBlock.Height = db.Height
continue
}
var offset int
if db.FBlock.KeyMR != nil {
offset++
}
eb := &db.EBlocks[ebi-offset]
eb.ChainID = &chainID
eb.KeyMR = &keyMR
eb.Timestamp = db.Timestamp
eb.Height = db.Height
}
// Verify BodyMR.
bodyMR, err := ComputeDBlockBodyMR(elements)
if err != nil {
return err
}
if *db.BodyMR != bodyMR {
return fmt.Errorf("invalid BodyMR")
}
// Compute KeyMR
headerHash := ComputeDBlockHeaderHash(data)
keyMR := ComputeKeyMR(&headerHash, &bodyMR)
// Verify KeyMR, if set.
if db.KeyMR != nil {
if *db.KeyMR != keyMR {
return fmt.Errorf("invalid KeyMR")
}
} else {
// Populate KeyMR.
db.KeyMR = &keyMR
}
// Populate FullHash.
db.FullHash = new(Bytes32)
*db.FullHash = ComputeFullHash(data)
db.marshalBinaryCache = data
return nil
}
// MarshalBinary returns the raw DBlock data for db. This will return an error
// if !db.IsPopulated() or if there are more than math.MaxUint32 EBlocks. The
// data format is as follows.
//
// Header
// [Version byte (0x00)] +
// [NetworkID (4 bytes)] +
// [BodyMR (Bytes32)] +
// [PrevKeyMR (Bytes32)] +
// [PrevFullHash (Bytes32)] +
// [Timestamp (4 bytes)] +
// [DB Height (4 bytes)] +
// [EBlock Count (4 bytes)]
//
// Body
// [Admin Block ChainID (Bytes32{31:0x0a})] +
// [Admin Block LookupHash (Bytes32)] +
// [EC Block ChainID (Bytes32{31:0x0c})] +
// [EC Block HeaderHash (Bytes32)] +
// [FCT Block ChainID (Bytes32{31:0x0f})] +
// [FCT Block KeyMR (Bytes32)] +
// [ChainID 0 (Bytes32)] +
// [KeyMR 0 (Bytes32)] +
// ... +
// [ChainID N (Bytes32)] +
// [KeyMR N (Bytes32)] +
//
// https://github.com/FactomProject/FactomDocs/blob/master/factomDataStructureDetails.md#directory-block
func (db DBlock) MarshalBinary() ([]byte, error) {
if !db.IsPopulated() {
return nil, fmt.Errorf("not populated")
}
if db.marshalBinaryCache != nil {
return db.marshalBinaryCache, nil
}
totalSize := db.MarshalBinaryLen()
if uint64(totalSize) > DBlockMaxTotalSize {
return nil, fmt.Errorf("too many EBlocks")
}
data := make([]byte, totalSize)
i := 1 // Skip version byte
i += copy(data[i:], db.NetworkID[:])
i += copy(data[i:], db.BodyMR[:])
i += copy(data[i:], db.PrevKeyMR[:])
i += copy(data[i:], db.PrevFullHash[:])
binary.BigEndian.PutUint32(data[i:], uint32(db.Timestamp.Unix()/60))
i += 4
binary.BigEndian.PutUint32(data[i:], db.Height)
i += 4
binary.BigEndian.PutUint32(data[i:], uint32(len(db.EBlocks)+1))
i += 4
var fblockInserted bool
for _, eb := range db.EBlocks {
if !fblockInserted &&
bytes.Compare(fBlockChainID[:], eb.ChainID[:]) < 0 {
i += copy(data[i:], fBlockChainID[:])
i += copy(data[i:], db.FBlock.KeyMR[:])
fblockInserted = true
}
i += copy(data[i:], eb.ChainID[:])
i += copy(data[i:], eb.KeyMR[:])
}
return data, nil
}
// MarshalBinaryLen returns the length of the binary encoding of db,
// DBlockHeaderSize + (len(db.EBlocks)+1)*DBlockEBlockSize
func (db DBlock) MarshalBinaryLen() int {
return DBlockHeaderSize + (len(db.EBlocks)+1)*DBlockEBlockSize
}
// EBlock efficiently finds and returns the *EBlock in db.EBlocks for the given
// chainID, if it exists. Otherwise, EBlock returns nil.
//
// This assumes that db.EBlocks ordered by ascending ChainID.
func (db DBlock) EBlock(chainID Bytes32) *EBlock {
ei := sort.Search(len(db.EBlocks), func(i int) bool {
return bytes.Compare(db.EBlocks[i].ChainID[:], chainID[:]) >= 0
})
if ei < len(db.EBlocks) && *db.EBlocks[ei].ChainID == chainID {
return &db.EBlocks[ei]
}
return nil
}