Skip to content

Commit efde480

Browse files
committed
ecblock header changes and varint added
1 parent 63eccab commit efde480

7 files changed

Lines changed: 341 additions & 102 deletions

File tree

common/directoryBlock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func NewDBEntryFromECBlock(cb *ECBlock) *DBEntry {
132132
e.hash = cb.Header.BodyHash
133133

134134
e.ChainID = cb.Header.ECChainID
135-
e.MerkleRoot = cb.KeyMR()
135+
e.MerkleRoot = cb.Header.Hash()
136136

137137
return e
138138
}

common/ecblock.go

Lines changed: 76 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"encoding/binary"
1010
"fmt"
1111
"io"
12-
13-
"golang.org/x/crypto/sha3"
1412
)
1513

1614
const (
@@ -19,9 +17,6 @@ const (
1917
ECIDChainCommit
2018
ECIDEntryCommit
2119
ECIDBalanceIncrease
22-
23-
// ecBlockHeaderSize 32+32+32+32+4+32+32+8+8
24-
ecBlockHeaderSize = 212
2520
)
2621

2722
// The Entry Credit Block consists of a header and a body. The body is composed
@@ -41,8 +36,8 @@ func NewECBlock() *ECBlock {
4136

4237
func NextECBlock(prev *ECBlock) *ECBlock {
4338
e := NewECBlock()
44-
e.Header.PrevKeyMR = prev.KeyMR()
45-
e.Header.PrevHash3 = prev.Hash3()
39+
e.Header.PrevHeaderHash = prev.Header.Hash()
40+
e.Header.PrevFullHash = prev.Hash()
4641
e.Header.DBHeight = prev.Header.DBHeight + 1
4742
return e
4843
}
@@ -51,37 +46,11 @@ func (e *ECBlock) AddEntry(entries ...ECBlockEntry) {
5146
e.Body.Entries = append(e.Body.Entries, entries...)
5247
}
5348

54-
// Hash3 returns the sha3-256 checksum of the previous Entry Credit Block from
55-
// the Header to the end of the Body
56-
func (e *ECBlock) Hash3() *Hash {
57-
r := NewHash()
58-
49+
func (e *ECBlock) Hash() *Hash {
5950
p, err := e.MarshalBinary()
6051
if err != nil {
61-
return r
62-
}
63-
64-
sum := sha3.Sum256(p)
65-
r.SetBytes(sum[:])
66-
return r
67-
}
68-
69-
// KeyMR returns a hash of the serialized Block Header + the serialized Body.
70-
func (e *ECBlock) KeyMR() *Hash {
71-
r := NewHash()
72-
p := make([]byte, 0)
73-
74-
head, err := e.Header.MarshalBinary()
75-
if err != nil {
76-
return r
52+
return NewHash()
7753
}
78-
p = append(p, head...)
79-
body, err := e.Body.MarshalBinary()
80-
if err != nil {
81-
return r
82-
}
83-
p = append(p, body...)
84-
8554
return Sha(p)
8655
}
8756

@@ -126,13 +95,7 @@ func (e *ECBlock) UnmarshalBinary(data []byte) error {
12695
buf := bytes.NewBuffer(data)
12796

12897
// Unmarshal Header
129-
if p := buf.Next(ecBlockHeaderSize); len(p) != ecBlockHeaderSize {
130-
return fmt.Errorf("Entry Block is smaller than ecBlockHeaderSize")
131-
} else {
132-
if err := e.Header.UnmarshalBinary(p); err != nil {
133-
return err
134-
}
135-
}
98+
e.Header.readUnmarshal(buf)
13699

137100
// Unmarshal Body
138101
if err := e.Body.UnmarshalBinary(buf.Bytes()); err != nil {
@@ -223,31 +186,35 @@ type ECBlockEntry interface {
223186
}
224187

225188
type ECBlockHeader struct {
226-
ECChainID *Hash
227-
BodyHash *Hash
228-
PrevKeyMR *Hash
229-
PrevHash3 *Hash
230-
DBHeight uint32
231-
SegmentsMR *Hash
232-
BalanceCommit *Hash
233-
ObjectCount uint64
234-
BodySize uint64
189+
ECChainID *Hash
190+
BodyHash *Hash
191+
PrevHeaderHash *Hash
192+
PrevFullHash *Hash
193+
DBHeight uint32
194+
HeaderExpansionArea []byte
195+
ObjectCount uint64
196+
BodySize uint64
235197
}
236198

237199
func NewECBlockHeader() *ECBlockHeader {
238200
h := new(ECBlockHeader)
239201
h.ECChainID = NewHash()
240202
h.ECChainID.SetBytes(EC_CHAINID)
241203
h.BodyHash = NewHash()
242-
h.PrevKeyMR = NewHash()
243-
h.PrevHash3 = NewHash()
244-
h.DBHeight = 0
245-
h.SegmentsMR = NewHash()
246-
h.BalanceCommit = NewHash()
247-
h.ObjectCount = 0
204+
h.PrevHeaderHash = NewHash()
205+
h.PrevFullHash = NewHash()
206+
h.HeaderExpansionArea = make([]byte, 0)
248207
return h
249208
}
250209

210+
func (e *ECBlockHeader) Hash() *Hash {
211+
p, err := e.MarshalBinary()
212+
if err != nil {
213+
return NewHash()
214+
}
215+
return Sha(p)
216+
}
217+
251218
func (e *ECBlockHeader) MarshalBinary() ([]byte, error) {
252219
buf := new(bytes.Buffer)
253220

@@ -257,23 +224,26 @@ func (e *ECBlockHeader) MarshalBinary() ([]byte, error) {
257224
// 32 byte BodyHash
258225
buf.Write(e.BodyHash.Bytes())
259226

260-
// 32 byte Previous KeyMR
261-
buf.Write(e.PrevKeyMR.Bytes())
227+
// 32 byte Previous Header Hash
228+
buf.Write(e.PrevHeaderHash.Bytes())
262229

263-
// 32 byte Previous Hash
264-
buf.Write(e.PrevHash3.Bytes())
230+
// 32 byte Previous Full Hash
231+
buf.Write(e.PrevFullHash.Bytes())
265232

266233
// 4 byte Directory Block Height
267234
if err := binary.Write(buf, binary.BigEndian, e.DBHeight); err != nil {
268235
return buf.Bytes(), err
269236
}
237+
238+
// variable Header Expansion Size
239+
if _, err := WriteVarInt(buf,
240+
uint64(len(e.HeaderExpansionArea))); err != nil {
241+
return buf.Bytes(), err
242+
}
270243

271-
// 32 byte SegmentsMR
272-
buf.Write(e.SegmentsMR.Bytes())
273-
274-
// 32 byte Balance Commit
275-
buf.Write(e.BalanceCommit.Bytes())
276-
244+
// varable byte Header Expansion Area
245+
buf.Write(e.HeaderExpansionArea)
246+
277247
// 8 byte Object Count
278248
if err := binary.Write(buf, binary.BigEndian, e.ObjectCount); err != nil {
279249
return buf.Bytes(), err
@@ -289,55 +259,69 @@ func (e *ECBlockHeader) MarshalBinary() ([]byte, error) {
289259

290260
func (e *ECBlockHeader) UnmarshalBinary(data []byte) error {
291261
buf := bytes.NewBuffer(data)
262+
_, err := e.readUnmarshal(buf)
263+
if err != nil {
264+
return err
265+
}
266+
return nil
267+
}
268+
269+
// readUnmarshal is a private method to read the correct lenth of bytes from a
270+
// buffer and unmarshal the data
271+
func (e *ECBlockHeader) readUnmarshal(buf *bytes.Buffer) (n int, err error) {
292272
hash := make([]byte, 32)
293273

294-
if _, err := buf.Read(hash); err != nil {
295-
return err
274+
if x, err := buf.Read(hash); err != nil {
275+
return n+x, err
296276
} else {
297277
e.ECChainID.SetBytes(hash)
278+
n += x
298279
}
299280

300-
if _, err := buf.Read(hash); err != nil {
301-
return err
281+
if x, err := buf.Read(hash); err != nil {
282+
return n+x, err
302283
} else {
303284
e.BodyHash.SetBytes(hash)
285+
n += x
304286
}
305287

306-
if _, err := buf.Read(hash); err != nil {
307-
return err
288+
if x, err := buf.Read(hash); err != nil {
289+
return n+x, err
308290
} else {
309-
e.PrevKeyMR.SetBytes(hash)
291+
e.PrevHeaderHash.SetBytes(hash)
292+
n += x
310293
}
311294

312-
if _, err := buf.Read(hash); err != nil {
313-
return err
295+
if x, err := buf.Read(hash); err != nil {
296+
return n+x, err
314297
} else {
315-
e.PrevHash3.SetBytes(hash)
298+
e.PrevFullHash.SetBytes(hash)
299+
n += x
316300
}
317301

318302
if err := binary.Read(buf, binary.BigEndian, &e.DBHeight); err != nil {
319-
return err
320-
}
321-
322-
if _, err := buf.Read(hash); err != nil {
323-
return err
324-
} else {
325-
e.SegmentsMR.SetBytes(hash)
303+
return n, err
326304
}
305+
n += 4
327306

328-
if _, err := buf.Read(hash); err != nil {
329-
return err
307+
// read the Header Expansion Area
308+
hesize := ReadVarInt(buf)
309+
e.HeaderExpansionArea = make([]byte, hesize)
310+
if x, err := buf.Read(e.HeaderExpansionArea); err != nil {
311+
return n+x, err
330312
} else {
331-
e.BalanceCommit.SetBytes(hash)
313+
n += x
332314
}
333-
315+
334316
if err := binary.Read(buf, binary.BigEndian, &e.ObjectCount); err != nil {
335-
return err
317+
return n, err
336318
}
319+
n += 8
337320

338321
if err := binary.Read(buf, binary.BigEndian, &e.BodySize); err != nil {
339-
return err
322+
return n, err
340323
}
324+
n += 8
341325

342-
return nil
343-
}
326+
return n, nil
327+
}

common/ecblock_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ func TestECBlockMarshal(t *testing.T) {
5151
p, _ = hex.DecodeString("2222222222222222222222222222222222222222222222222222222222222222")
5252
ecb.Header.BodyHash.SetBytes(p)
5353
p, _ = hex.DecodeString("3333333333333333333333333333333333333333333333333333333333333333")
54-
ecb.Header.PrevKeyMR.SetBytes(p)
54+
ecb.Header.PrevHeaderHash.SetBytes(p)
5555
p, _ = hex.DecodeString("4444444444444444444444444444444444444444444444444444444444444444")
56-
ecb.Header.PrevHash3.SetBytes(p)
56+
ecb.Header.PrevFullHash.SetBytes(p)
5757
ecb.Header.DBHeight = 10
58-
p, _ = hex.DecodeString("5555555555555555555555555555555555555555555555555555555555555555")
59-
ecb.Header.SegmentsMR.SetBytes(p)
58+
ecb.Header.HeaderExpansionArea, _ = hex.DecodeString("5555555555555555555555555555555555555555555555555555555555555555")
6059
p, _ = hex.DecodeString("6666666666666666666666666666666666666666666666666666666666666666")
61-
ecb.Header.BalanceCommit.SetBytes(p)
6260
ecb.Header.ObjectCount = 0
6361

6462
// add the CommitChain to the ECBlock

0 commit comments

Comments
 (0)