forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecblock.go
495 lines (425 loc) · 12.6 KB
/
ecblock.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
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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package entryCreditBlock
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
// ECBlock is an Entry Credit Block and consists of a header and a body. The body is composed
// of primarily Commits and Balance Increases with Minute Markers and Server
// Markers distributed throughout.
type ECBlock struct {
Header interfaces.IECBlockHeader `json:"header"` // The entry credit block header
Body interfaces.IECBlockBody `json:"body"` // The entry credit block body
}
var _ interfaces.Printable = (*ECBlock)(nil)
var _ interfaces.BinaryMarshallableAndCopyable = (*ECBlock)(nil)
var _ interfaces.IEntryCreditBlock = (*ECBlock)(nil)
var _ interfaces.DatabaseBlockWithEntries = (*ECBlock)(nil)
// Init initializes the header and body to new objects if they are nil
func (e *ECBlock) Init() {
if e.Header == nil {
h := new(ECBlockHeader)
h.Init()
e.Header = h
}
if e.Body == nil {
e.Body = new(ECBlockBody)
}
}
// IsSameAs returns true iff the input block is the same as this object
func (e *ECBlock) IsSameAs(b interfaces.IEntryCreditBlock) bool {
if e == nil || b == nil {
if e == nil && b == nil {
return true
}
return false
}
if e.Header.IsSameAs(b.GetHeader()) == false {
return false
}
if e.Body.IsSameAs(b.GetBody()) == false {
return false
}
return true
}
// UpdateState executes the EC transactions
func (e *ECBlock) UpdateState(state interfaces.IState) error {
if state == nil {
return fmt.Errorf("No State provided")
}
e.Init()
state.UpdateECs(e)
return nil
}
// String returns this object as a string (header and body included)
func (e *ECBlock) String() string {
str := e.GetHeader().String()
str = str + e.GetBody().String()
return str
}
// GetEntries returns the entries in this block
func (e *ECBlock) GetEntries() []interfaces.IECBlockEntry {
e.Init()
return e.GetBody().GetEntries()
}
// GetEntryByHash returns the entry whose hash matches the input hash, or whose signature hash matches the input hash.
// If no hash is found, returns nil
func (e *ECBlock) GetEntryByHash(hash interfaces.IHash) interfaces.IECBlockEntry {
if hash == nil {
return nil
}
txs := e.GetEntries()
for _, tx := range txs {
if hash.IsSameAs(tx.Hash()) {
return tx
}
if hash.IsSameAs(tx.GetSigHash()) {
return tx
}
}
return nil
}
// GetEntryHashes returns a list of hashes for each entry in the ECBlock that is either a balance increase, chain commit, or entry commit
func (e *ECBlock) GetEntryHashes() []interfaces.IHash {
entries := e.GetBody().GetEntries()
answer := make([]interfaces.IHash, 0, len(entries))
for _, entry := range entries {
if entry.ECID() == constants.ECIDBalanceIncrease ||
entry.ECID() == constants.ECIDChainCommit ||
entry.ECID() == constants.ECIDEntryCommit {
answer = append(answer, entry.Hash())
}
}
return answer
}
// GetEntrySigHashes returns a list of signature hashes for each entry in the ECBlock that is either a balance increase, chain commit,
// or entry commit
func (e *ECBlock) GetEntrySigHashes() []interfaces.IHash {
entries := e.GetBody().GetEntries()
answer := make([]interfaces.IHash, 0, len(entries))
for _, entry := range entries {
if entry.ECID() == constants.ECIDBalanceIncrease ||
entry.ECID() == constants.ECIDChainCommit ||
entry.ECID() == constants.ECIDEntryCommit {
sHash := entry.GetSigHash()
if sHash != nil {
answer = append(answer, sHash)
}
}
}
return answer
}
// GetBody returns the EC block body
func (e *ECBlock) GetBody() interfaces.IECBlockBody {
e.Init()
return e.Body
}
// GetHeader returns the EC block header
func (e *ECBlock) GetHeader() interfaces.IECBlockHeader {
e.Init()
return e.Header
}
// New returns a new EC block
func (e *ECBlock) New() interfaces.BinaryMarshallableAndCopyable {
block, _ := NextECBlock(nil)
return block
}
// GetDatabaseHeight returns the directory block height this EC block is part of
func (e *ECBlock) GetDatabaseHeight() uint32 {
return e.GetHeader().GetDBHeight()
}
// GetChainID returns the chain id of this EC block
func (e *ECBlock) GetChainID() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "ECBlock.GetChainID") }()
return e.GetHeader().GetECChainID()
}
// DatabasePrimaryIndex returns the hash of the header
func (e *ECBlock) DatabasePrimaryIndex() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "ECBlock.DatabasePrimaryIndex") }()
key, _ := e.HeaderHash()
return key
}
// DatabaseSecondaryIndex returns the full hash (header and body) of the object
func (e *ECBlock) DatabaseSecondaryIndex() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "ECBlock.DatabaseSecondaryIndex") }()
key, _ := e.GetFullHash()
return key
}
// AddEntry appends the input entries into the EC block
func (e *ECBlock) AddEntry(entries ...interfaces.IECBlockEntry) {
e.GetBody().SetEntries(append(e.GetBody().GetEntries(), entries...))
}
// GetHash returns the full hash (header and body) of the object
func (e *ECBlock) GetHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "ECBlock.GetHash") }()
h, _ := e.GetFullHash()
return h
}
// GetFullHash returns the full hash (header and body) of the object
func (e *ECBlock) GetFullHash() (interfaces.IHash, error) {
p, err := e.MarshalBinary()
if err != nil {
return nil, err
}
return primitives.Sha(p), nil
}
// HeaderHash returns the hash of the header
func (e *ECBlock) HeaderHash() (interfaces.IHash, error) {
if err := e.BuildHeader(); err != nil {
return nil, err
}
p, err := e.GetHeader().MarshalBinary()
if err != nil {
return nil, err
}
return primitives.Sha(p), nil
}
// MarshalBinary marshals the object (header and body)
func (e *ECBlock) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "ECBlock.MarshalBinary err:%v", *pe)
}
}(&err)
e.Init()
buf := primitives.NewBuffer(nil)
// Header
err = e.BuildHeader()
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(e.GetHeader())
if err != nil {
return nil, err
}
x := buf.DeepCopyBytes()
buf = primitives.NewBuffer(x)
// Body of ECBlockEntries
p, err := e.marshalBodyBinary()
if err != nil {
return nil, err
}
err = buf.Push(p)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
// BuildHeader sets all relevant header values based on the current body contents
func (e *ECBlock) BuildHeader() error {
e.Init()
// Marshal the Body
p, err := e.marshalBodyBinary()
if err != nil {
return err
}
header := e.GetHeader().(*ECBlockHeader)
header.BodyHash = primitives.Sha(p)
header.ObjectCount = uint64(len(e.GetBody().GetEntries()))
header.BodySize = uint64(len(p))
return nil
}
// UnmarshalECBlock unmarshals the input data into a new EC block
func UnmarshalECBlock(data []byte) (interfaces.IEntryCreditBlock, error) {
block, _ := NextECBlock(nil)
err := block.UnmarshalBinary(data)
if err != nil {
return nil, err
}
return block, nil
}
// UnmarshalBinaryData unmarshals the input data into this EC block
func (e *ECBlock) UnmarshalBinaryData(data []byte) ([]byte, error) {
buf := primitives.NewBuffer(data)
// Unmarshal Header
if e.GetHeader() == nil {
e.Header = NewECBlockHeader()
}
err := buf.PopBinaryMarshallable(e.GetHeader())
if err != nil {
return nil, err
}
// Unmarshal Body
newData, err := e.unmarshalBodyBinaryData(buf.DeepCopyBytes())
if err != nil {
return nil, err
}
return newData, err
}
// UnmarshalBinary unmarshals the input data into this EC block
func (e *ECBlock) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
// marshalBodyBinary marshals only the body of the EC block
func (e *ECBlock) marshalBodyBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "ECBlock.marshalBodyBinary err:%v", *pe)
}
}(&err)
e.Init()
buf := primitives.NewBuffer(nil)
entries := e.GetBody().GetEntries()
for _, v := range entries {
err := buf.PushByte(v.ECID())
if err != nil {
return nil, err
}
err = buf.PushBinaryMarshallable(v)
if err != nil {
return nil, err
}
}
return buf.DeepCopyBytes(), nil
}
// unmarshalBodyBinaryData unmarshals the input data into the body (no header info)
func (e *ECBlock) unmarshalBodyBinaryData(data []byte) ([]byte, error) {
var err error
e.Init()
// No longer use buffers due to the lots of copying of the same data
//buf := primitives.NewBuffer(data)
newData := data
allentries := make([]interfaces.IECBlockEntry, e.GetHeader().GetObjectCount())
for i := uint64(0); i < e.GetHeader().GetObjectCount(); i++ {
id := newData[0]
newData = newData[1:]
switch id {
case constants.ECIDServerIndexNumber:
s := NewServerIndexNumber()
newData, err = s.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
allentries[i] = s
case constants.ECIDMinuteNumber:
m := NewMinuteNumber(0)
_, err = m.UnmarshalBinaryData(newData[:1])
if err != nil {
return nil, err
}
allentries[i] = m
newData = newData[1:]
case constants.ECIDChainCommit:
c := NewCommitChain()
_, err = c.UnmarshalBinaryData(newData[:200])
if err != nil {
return nil, err
}
allentries[i] = c
newData = newData[200:]
case constants.ECIDEntryCommit:
c := NewCommitEntry()
_, err = c.UnmarshalBinaryData(newData[:136])
if err != nil {
return nil, err
}
allentries[i] = c
newData = newData[136:]
case constants.ECIDBalanceIncrease:
c := NewIncreaseBalance()
newData, err = c.UnmarshalBinaryData(newData)
if err != nil {
return nil, err
}
allentries[i] = c
default:
err = fmt.Errorf("Unsupported ECID: %x\n", id)
return nil, err
}
}
e.Body.SetEntries(allentries)
//buf.Reset()
//buf.Write(newData)
return newData, nil
}
// unmarshalBodyBinary unmarshals the input data into the body (no header info)
func (e *ECBlock) unmarshalBodyBinary(data []byte) (err error) {
_, err = e.unmarshalBodyBinaryData(data)
return
}
// JSONByte returns the json encoded byte array
func (e *ECBlock) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(e)
}
// JSONString returns the json encoded string
func (e *ECBlock) JSONString() (string, error) {
return primitives.EncodeJSONString(e)
}
/********************************************************
* Support Functions
********************************************************/
// NewECBlock creates a new empty EC block
func NewECBlock() interfaces.IEntryCreditBlock {
e := new(ECBlock)
e.Header = NewECBlockHeader()
e.Body = NewECBlockBody()
return e
}
// NextECBlock creates a new EC block with header information filled in from the input previous block information
func NextECBlock(prev interfaces.IEntryCreditBlock) (interfaces.IEntryCreditBlock, error) {
e := NewECBlock()
// Handle the really unusual case of the first block.
if prev == nil {
e.GetHeader().SetPrevHeaderHash(primitives.NewZeroHash())
e.GetHeader().SetPrevFullHash(primitives.NewZeroHash())
e.GetHeader().SetDBHeight(0)
} else {
v, err := prev.HeaderHash()
if err != nil {
return nil, err
}
e.GetHeader().SetPrevHeaderHash(v)
v, err = prev.GetFullHash()
if err != nil {
return nil, err
}
e.GetHeader().SetPrevFullHash(v)
e.GetHeader().SetDBHeight(prev.GetHeader().GetDBHeight() + 1)
}
if err := e.(*ECBlock).BuildHeader(); err != nil {
return nil, err
}
return e, nil
}
// CheckBlockPairIntegrity checks that the input block is derived from the previous block via their header information
func CheckBlockPairIntegrity(block interfaces.IEntryCreditBlock, prev interfaces.IEntryCreditBlock) error {
if block == nil {
return fmt.Errorf("No block specified")
}
if prev == nil {
if block.GetHeader().GetPrevHeaderHash().IsZero() == false {
return fmt.Errorf("Invalid PrevHeaderHash")
}
if block.GetHeader().GetPrevFullHash().IsZero() == false {
return fmt.Errorf("Invalid PrevFullHash")
}
if block.GetHeader().GetDBHeight() != 0 {
return fmt.Errorf("Invalid DBHeight")
}
} else {
h, err := prev.HeaderHash()
if err != nil {
return err
}
if block.GetHeader().GetPrevHeaderHash().IsSameAs(h) == false {
return fmt.Errorf("Invalid PrevHeaderHash")
}
h, err = prev.GetFullHash()
if err != nil {
return err
}
if block.GetHeader().GetPrevFullHash().IsSameAs(h) == false {
return fmt.Errorf("Invalid PrevFullHash")
}
if block.GetHeader().GetDBHeight() != (prev.GetHeader().GetDBHeight() + 1) {
return fmt.Errorf("Invalid DBHeight")
}
}
return nil
}