forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadminBlock.go
More file actions
559 lines (445 loc) · 12.1 KB
/
adminBlock.go
File metadata and controls
559 lines (445 loc) · 12.1 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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package common
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"sync"
)
// Administrative Chain
type AdminChain struct {
ChainID *Hash
Name [][]byte
NextBlock *AdminBlock
NextBlockHeight uint32
BlockMutex sync.Mutex
}
// Administrative Block
// This is a special block which accompanies this Directory Block.
// It contains the signatures and organizational data needed to validate previous and future Directory Blocks.
// This block is included in the DB body. It appears there with a pair of the Admin AdminChainID:SHA256 of the block.
// For more details, please go to:
// https://github.com/FactomProject/FactomDocs/blob/master/factomDataStructureDetails.md#administrative-block
type AdminBlock struct {
//Marshalized
Header *ABlockHeader
ABEntries []ABEntry //Interface
//Not Marshalized
fullHash *Hash //SHA512Half
partialHash *Hash //SHA256
}
var _ Printable = (*AdminBlock)(nil)
var _ BinaryMarshallable = (*AdminBlock)(nil)
func (ab *AdminBlock) LedgerKeyMR() (*Hash, error) {
if ab.fullHash == nil {
err := ab.buildFullBHash()
if err != nil {
return nil, err
}
}
return ab.fullHash, nil
}
func (ab *AdminBlock) PartialHash() (*Hash, error) {
if ab.partialHash == nil {
err := ab.buildPartialHash()
if err != nil {
return nil, err
}
}
return ab.partialHash, nil
}
// Create an empty Admin Block
func CreateAdminBlock(chain *AdminChain, prev *AdminBlock, cap uint) (b *AdminBlock, err error) {
if prev == nil && chain.NextBlockHeight != 0 {
return nil, errors.New("Previous block cannot be nil")
} else if prev != nil && chain.NextBlockHeight == 0 {
return nil, errors.New("Origin block cannot have a parent block")
}
b = new(AdminBlock)
b.Header = new(ABlockHeader)
b.Header.AdminChainID = chain.ChainID
if prev == nil {
b.Header.PrevLedgerKeyMR = NewHash()
} else {
b.Header.PrevLedgerKeyMR, err = prev.LedgerKeyMR()
if err != nil {
return
}
}
b.Header.DBHeight = chain.NextBlockHeight
b.ABEntries = make([]ABEntry, 0, cap)
return b, err
}
// Build the SHA512Half hash for the admin block
func (b *AdminBlock) buildFullBHash() (err error) {
var binaryAB []byte
binaryAB, err = b.MarshalBinary()
if err != nil {
return
}
b.fullHash = Sha512Half(binaryAB)
return
}
// Build the SHA256 hash for the admin block
func (b *AdminBlock) buildPartialHash() (err error) {
var binaryAB []byte
binaryAB, err = b.MarshalBinary()
if err != nil {
return
}
b.partialHash = Sha(binaryAB)
return
}
// Add an Admin Block entry to the block
func (b *AdminBlock) AddABEntry(e ABEntry) (err error) {
b.ABEntries = append(b.ABEntries, e)
return
}
// Add the end-of-minute marker into the admin block
func (b *AdminBlock) AddEndOfMinuteMarker(eomType byte) (err error) {
eOMEntry := &EndOfMinuteEntry{
entryType: TYPE_MINUTE_NUM,
EOM_Type: eomType}
b.AddABEntry(eOMEntry)
return
}
// Write out the AdminBlock to binary.
func (b *AdminBlock) MarshalBinary() (data []byte, err error) {
var buf bytes.Buffer
data, _ = b.Header.MarshalBinary()
buf.Write(data)
for i := uint32(0); i < b.Header.MessageCount; i++ {
data, _ := b.ABEntries[i].MarshalBinary()
buf.Write(data)
}
return buf.Bytes(), err
}
// Admin Block size
func (b *AdminBlock) MarshalledSize() uint64 {
var size uint64 = 0
size += b.Header.MarshalledSize()
for _, entry := range b.ABEntries {
size += entry.MarshalledSize()
}
return size
}
func (b *AdminBlock) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
}
}()
newData = data
h := new(ABlockHeader)
newData, err = h.UnmarshalBinaryData(newData)
if err != nil {
return
}
b.Header = h
b.ABEntries = make([]ABEntry, b.Header.MessageCount)
for i := uint32(0); i < b.Header.MessageCount; i++ {
if newData[0] == TYPE_DB_SIGNATURE {
b.ABEntries[i] = new(DBSignatureEntry)
} else if newData[0] == TYPE_MINUTE_NUM {
b.ABEntries[i] = new(EndOfMinuteEntry)
}
newData, err = b.ABEntries[i].UnmarshalBinaryData(newData)
if err != nil {
return
}
}
return
}
// Read in the binary into the Admin block.
func (b *AdminBlock) UnmarshalBinary(data []byte) (err error) {
_, err = b.UnmarshalBinaryData(data)
return
}
// Read in the binary into the Admin block.
func (b *AdminBlock) GetDBSignature() ABEntry {
for i := uint32(0); i < b.Header.MessageCount; i++ {
if b.ABEntries[i].Type() == TYPE_DB_SIGNATURE {
return b.ABEntries[i]
}
}
return nil
}
func (e *AdminBlock) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *AdminBlock) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *AdminBlock) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *AdminBlock) Spew() string {
return Spew(e)
}
// Admin Block Header
type ABlockHeader struct {
AdminChainID *Hash
PrevLedgerKeyMR *Hash
DBHeight uint32
HeaderExpansionSize uint64
HeaderExpansionArea []byte
MessageCount uint32
BodySize uint32
}
var _ Printable = (*ABlockHeader)(nil)
var _ BinaryMarshallable = (*ABlockHeader)(nil)
// Write out the ABlockHeader to binary.
func (b *ABlockHeader) MarshalBinary() (data []byte, err error) {
var buf bytes.Buffer
data, err = b.AdminChainID.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
data, err = b.PrevLedgerKeyMR.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
binary.Write(&buf, binary.BigEndian, b.DBHeight)
EncodeVarInt(&buf, b.HeaderExpansionSize)
buf.Write(b.HeaderExpansionArea)
binary.Write(&buf, binary.BigEndian, b.MessageCount)
binary.Write(&buf, binary.BigEndian, b.BodySize)
return buf.Bytes(), err
}
func (b *ABlockHeader) MarshalledSize() uint64 {
var size uint64 = 0
size += uint64(HASH_LENGTH) //AdminChainID
size += uint64(HASH_LENGTH) //PrevFullHash
size += 4 //DBHeight
size += VarIntLength(b.HeaderExpansionSize) //HeaderExpansionSize
size += b.HeaderExpansionSize //HeadderExpansionArea
size += 4 //MessageCount
size += 4 //BodySize
return size
}
func (b *ABlockHeader) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
}
}()
newData = data
b.AdminChainID = new(Hash)
newData, err = b.AdminChainID.UnmarshalBinaryData(newData)
if err != nil {
return
}
b.PrevLedgerKeyMR = new(Hash)
newData, err = b.PrevLedgerKeyMR.UnmarshalBinaryData(newData)
if err != nil {
return
}
b.DBHeight, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
b.HeaderExpansionSize, newData = DecodeVarInt(newData)
b.HeaderExpansionArea, newData = newData[:b.HeaderExpansionSize], newData[b.HeaderExpansionSize:]
b.MessageCount, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
b.BodySize, newData = binary.BigEndian.Uint32(newData[0:4]), newData[4:]
return
}
// Read in the binary into the ABlockHeader.
func (b *ABlockHeader) UnmarshalBinary(data []byte) (err error) {
_, err = b.UnmarshalBinaryData(data)
return
}
func (e *ABlockHeader) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *ABlockHeader) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *ABlockHeader) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *ABlockHeader) Spew() string {
return Spew(e)
}
// Generic admin block entry type
type ABEntry interface {
Printable
BinaryMarshallable
ShortInterpretable
Type() byte
Hash() *Hash
}
type Sig [64]byte
func (s *Sig) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(s[:])), nil
}
func (s *Sig) UnmarshalText(b []byte) error {
p, err := hex.DecodeString(string(b))
if err != nil {
return err
}
copy(s[:], p)
return nil
}
// DB Signature Entry -------------------------
type DBSignatureEntry struct {
entryType byte
IdentityAdminChainID *Hash
PubKey PublicKey
PrevDBSig *Sig
}
var _ ABEntry = (*DBSignatureEntry)(nil)
var _ BinaryMarshallable = (*DBSignatureEntry)(nil)
// Create a new DB Signature Entry
func NewDBSignatureEntry(identityAdminChainID *Hash, sig Signature) (e *DBSignatureEntry) {
e = new(DBSignatureEntry)
e.entryType = TYPE_DB_SIGNATURE
e.IdentityAdminChainID = identityAdminChainID
e.PubKey = sig.Pub
e.PrevDBSig = (*Sig)(sig.Sig)
return
}
func (e *DBSignatureEntry) Type() byte {
return e.entryType
}
func (e *DBSignatureEntry) MarshalBinary() (data []byte, err error) {
var buf bytes.Buffer
buf.Write([]byte{e.entryType})
data, err = e.IdentityAdminChainID.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
_, err = buf.Write(e.PubKey.Key[:])
if err != nil {
return nil, err
}
_, err = buf.Write(e.PrevDBSig[:])
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (e *DBSignatureEntry) MarshalledSize() uint64 {
var size uint64 = 0
size += 1 // Type (byte)
size += uint64(HASH_LENGTH)
size += uint64(HASH_LENGTH)
size += uint64(SIG_LENGTH)
return size
}
func (e *DBSignatureEntry) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
}
}()
newData = data
e.entryType, newData = newData[0], newData[1:]
e.IdentityAdminChainID = new(Hash)
newData, err = e.IdentityAdminChainID.UnmarshalBinaryData(newData)
if err != nil {
return
}
e.PubKey.Key = new([HASH_LENGTH]byte)
copy(e.PubKey.Key[:], newData[:HASH_LENGTH])
newData = newData[HASH_LENGTH:]
e.PrevDBSig = new(Sig)
copy(e.PrevDBSig[:], newData[:SIG_LENGTH])
newData = newData[SIG_LENGTH:]
return
}
func (e *DBSignatureEntry) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
func (e *DBSignatureEntry) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *DBSignatureEntry) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *DBSignatureEntry) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *DBSignatureEntry) Spew() string {
return Spew(e)
}
func (e *DBSignatureEntry) IsInterpretable() bool {
return false
}
func (e *DBSignatureEntry) Interpret() string {
return ""
}
func (e *DBSignatureEntry) Hash() *Hash {
bin, err := e.MarshalBinary()
if err != nil {
panic(err)
}
return Sha(bin)
}
type EndOfMinuteEntry struct {
entryType byte
EOM_Type byte
}
var _ Printable = (*EndOfMinuteEntry)(nil)
var _ BinaryMarshallable = (*EndOfMinuteEntry)(nil)
var _ ABEntry = (*EndOfMinuteEntry)(nil)
func (m *EndOfMinuteEntry) Type() byte {
return m.entryType
}
func (e *EndOfMinuteEntry) MarshalBinary() (data []byte, err error) {
var buf bytes.Buffer
buf.Write([]byte{e.entryType})
buf.Write([]byte{e.EOM_Type})
return buf.Bytes(), nil
}
func (e *EndOfMinuteEntry) MarshalledSize() uint64 {
var size uint64 = 0
size += 1 // Type (byte)
size += 1 // EOM_Type (byte)
return size
}
func (e *EndOfMinuteEntry) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Error unmarshalling: %v", r)
}
}()
newData = data
e.entryType, newData = newData[0], newData[1:]
e.EOM_Type, newData = newData[0], newData[1:]
return
}
func (e *EndOfMinuteEntry) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
func (e *EndOfMinuteEntry) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *EndOfMinuteEntry) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *EndOfMinuteEntry) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *EndOfMinuteEntry) Spew() string {
return Spew(e)
}
func (e *EndOfMinuteEntry) IsInterpretable() bool {
return true
}
func (e *EndOfMinuteEntry) Interpret() string {
return fmt.Sprintf("End of Minute %v", e.EOM_Type)
}
func (e *EndOfMinuteEntry) Hash() *Hash {
bin, err := e.MarshalBinary()
if err != nil {
panic(err)
}
return Sha(bin)
}