forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecblock.go
More file actions
428 lines (357 loc) · 8.81 KB
/
ecblock.go
File metadata and controls
428 lines (357 loc) · 8.81 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
// 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"
"fmt"
"io"
)
const (
ECIDServerIndexNumber byte = iota
ECIDMinuteNumber
ECIDChainCommit
ECIDEntryCommit
ECIDBalanceIncrease
)
// The Entry Credit Block 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 *ECBlockHeader
Body *ECBlockBody
}
var _ Printable = (*ECBlock)(nil)
var _ BinaryMarshallable = (*ECBlock)(nil)
func (c *ECBlock) MarshalledSize() uint64 {
panic("Function not implemented")
return 0
}
func NewECBlock() *ECBlock {
e := new(ECBlock)
e.Header = NewECBlockHeader()
e.Body = NewECBlockBody()
return e
}
func NextECBlock(prev *ECBlock) (*ECBlock, error) {
e := NewECBlock()
var err error
e.Header.PrevHeaderHash, err = prev.HeaderHash()
if err != nil {
return nil, err
}
e.Header.PrevLedgerKeyMR, err = prev.Hash()
if err != nil {
return nil, err
}
e.Header.DBHeight = prev.Header.DBHeight + 1
return e, nil
}
func (e *ECBlock) AddEntry(entries ...ECBlockEntry) {
e.Body.Entries = append(e.Body.Entries, entries...)
}
func (e *ECBlock) Hash() (*Hash, error) {
p, err := e.MarshalBinary()
if err != nil {
return nil, err
}
return Sha(p), nil
}
func (e *ECBlock) HeaderHash() (*Hash, error) {
p, err := e.marshalHeaderBinary()
if err != nil {
return nil, err
}
return Sha(p), nil
}
func (e *ECBlock) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
// Header
if err := e.BuildHeader(); err != nil {
return buf.Bytes(), err
}
if p, err := e.marshalHeaderBinary(); err != nil {
return buf.Bytes(), err
} else {
buf.Write(p)
}
// Body of ECBlockEntries
if p, err := e.marshalBodyBinary(); err != nil {
return buf.Bytes(), err
} else {
buf.Write(p)
}
return buf.Bytes(), nil
}
func (e *ECBlock) BuildHeader() error {
// Marshal the Body
p, err := e.marshalBodyBinary()
if err != nil {
return err
}
e.Header.BodyHash = Sha(p)
e.Header.ObjectCount = uint64(len(e.Body.Entries))
e.Header.BodySize = uint64(len(p))
return nil
}
func (e *ECBlock) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
// Unmarshal Header
newData, err = e.unmarshalHeaderBinaryData(data)
if err != nil {
return
}
// Unmarshal Body
newData, err = e.unmarshalBodyBinaryData(newData)
if err != nil {
return
}
return
}
func (e *ECBlock) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
func (e *ECBlock) marshalBodyBinary() ([]byte, error) {
buf := new(bytes.Buffer)
for _, v := range e.Body.Entries {
p, err := v.MarshalBinary()
if err != nil {
return buf.Bytes(), err
}
buf.WriteByte(v.ECID())
buf.Write(p)
}
return buf.Bytes(), nil
}
func (e *ECBlock) marshalHeaderBinary() ([]byte, error) {
buf := new(bytes.Buffer)
// 32 byte ECChainID
buf.Write(e.Header.ECChainID.Bytes())
// 32 byte BodyHash
buf.Write(e.Header.BodyHash.Bytes())
// 32 byte Previous Header Hash
buf.Write(e.Header.PrevHeaderHash.Bytes())
// 32 byte Previous Full Hash
buf.Write(e.Header.PrevLedgerKeyMR.Bytes())
// 4 byte Directory Block Height
if err := binary.Write(buf, binary.BigEndian, e.Header.DBHeight); err != nil {
return buf.Bytes(), err
}
// variable Header Expansion Size
if err := EncodeVarInt(buf,
uint64(len(e.Header.HeaderExpansionArea))); err != nil {
return buf.Bytes(), err
}
// varable byte Header Expansion Area
buf.Write(e.Header.HeaderExpansionArea)
// 8 byte Object Count
if err := binary.Write(buf, binary.BigEndian, e.Header.ObjectCount); err != nil {
return buf.Bytes(), err
}
// 8 byte size of the Body
if err := binary.Write(buf, binary.BigEndian, e.Header.BodySize); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}
func (e *ECBlock) unmarshalBodyBinaryData(data []byte) (newData []byte, err error) {
buf := bytes.NewBuffer(data)
for i := uint64(0); i < e.Header.ObjectCount; i++ {
var id byte
id, err = buf.ReadByte()
if err != nil {
newData = buf.Bytes()
return
}
switch id {
case ECIDServerIndexNumber:
s := NewServerIndexNumber()
if buf.Len() < ServerIndexNumberSize {
err = io.EOF
newData = buf.Bytes()
return
}
_, err = s.UnmarshalBinaryData(buf.Next(ServerIndexNumberSize))
if err != nil {
newData = buf.Bytes()
return
}
e.Body.Entries = append(e.Body.Entries, s)
case ECIDMinuteNumber:
m := NewMinuteNumber()
if buf.Len() < MinuteNumberSize {
err = io.EOF
newData = buf.Bytes()
return
}
_, err = m.UnmarshalBinaryData(buf.Next(MinuteNumberSize))
if err != nil {
newData = buf.Bytes()
return
}
e.Body.Entries = append(e.Body.Entries, m)
case ECIDChainCommit:
if buf.Len() < CommitChainSize {
err = io.EOF
newData = buf.Bytes()
return
}
c := NewCommitChain()
_, err = c.UnmarshalBinaryData(buf.Next(CommitChainSize))
if err != nil {
return
}
e.Body.Entries = append(e.Body.Entries, c)
case ECIDEntryCommit:
if buf.Len() < CommitEntrySize {
err = io.EOF
newData = buf.Bytes()
return
}
c := NewCommitEntry()
_, err = c.UnmarshalBinaryData(buf.Next(CommitEntrySize))
if err != nil {
return
}
e.Body.Entries = append(e.Body.Entries, c)
case ECIDBalanceIncrease:
c := NewIncreaseBalance()
tmp, err := c.UnmarshalBinaryData(buf.Bytes())
if err != nil {
return tmp, err
}
e.Body.Entries = append(e.Body.Entries, c)
buf = bytes.NewBuffer(tmp)
default:
err = fmt.Errorf("Unsupported ECID: %x\n", id)
return
}
}
newData = buf.Bytes()
return
}
func (b *ECBlock) unmarshalBodyBinary(data []byte) (err error) {
_, err = b.unmarshalBodyBinaryData(data)
return
}
func (e *ECBlock) unmarshalHeaderBinaryData(data []byte) (newData []byte, err error) {
buf := bytes.NewBuffer(data)
hash := make([]byte, 32)
if _, err = buf.Read(hash); err != nil {
return
} else {
e.Header.ECChainID.SetBytes(hash)
}
if _, err = buf.Read(hash); err != nil {
return
} else {
e.Header.BodyHash.SetBytes(hash)
}
if _, err = buf.Read(hash); err != nil {
return
} else {
e.Header.PrevHeaderHash.SetBytes(hash)
}
if _, err = buf.Read(hash); err != nil {
return
} else {
e.Header.PrevLedgerKeyMR.SetBytes(hash)
}
if err = binary.Read(buf, binary.BigEndian, &e.Header.DBHeight); err != nil {
return
}
// read the Header Expansion Area
hesize, tmp := DecodeVarInt(buf.Bytes())
buf = bytes.NewBuffer(tmp)
e.Header.HeaderExpansionArea = make([]byte, hesize)
if _, err = buf.Read(e.Header.HeaderExpansionArea); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &e.Header.ObjectCount); err != nil {
return
}
if err = binary.Read(buf, binary.BigEndian, &e.Header.BodySize); err != nil {
return
}
newData = buf.Bytes()
return
}
func (e *ECBlock) unmarshalHeaderBinary(data []byte) error {
_, err := e.unmarshalHeaderBinaryData(data)
return err
}
func (e *ECBlock) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *ECBlock) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *ECBlock) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *ECBlock) Spew() string {
return Spew(e)
}
type ECBlockBody struct {
Entries []ECBlockEntry
}
var _ Printable = (*ECBlockBody)(nil)
func NewECBlockBody() *ECBlockBody {
b := new(ECBlockBody)
b.Entries = make([]ECBlockEntry, 0)
return b
}
func (e *ECBlockBody) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *ECBlockBody) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *ECBlockBody) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *ECBlockBody) Spew() string {
return Spew(e)
}
type ECBlockEntry interface {
Printable
ShortInterpretable
ECID() byte
MarshalBinary() ([]byte, error)
UnmarshalBinary(data []byte) error
Hash() *Hash
}
type ECBlockHeader struct {
ECChainID *Hash
BodyHash *Hash
PrevHeaderHash *Hash
PrevLedgerKeyMR *Hash
DBHeight uint32
HeaderExpansionArea []byte
ObjectCount uint64
BodySize uint64
}
var _ Printable = (*ECBlockHeader)(nil)
func NewECBlockHeader() *ECBlockHeader {
h := new(ECBlockHeader)
h.ECChainID = NewHash()
h.ECChainID.SetBytes(EC_CHAINID)
h.BodyHash = NewHash()
h.PrevHeaderHash = NewHash()
h.PrevLedgerKeyMR = NewHash()
h.HeaderExpansionArea = make([]byte, 0)
return h
}
func (e *ECBlockHeader) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *ECBlockHeader) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *ECBlockHeader) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *ECBlockHeader) Spew() string {
return Spew(e)
}