Skip to content

Commit ae359f6

Browse files
committed
Made sure to capture errors in case we are unable to hash EBlock or ECBlock.
1 parent 9e143f2 commit ae359f6

8 files changed

Lines changed: 134 additions & 44 deletions

File tree

common/directoryBlock.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,30 @@ func (c *DBEntry) MarshalledSize() uint64 {
182182
return 0
183183
}
184184

185-
func NewDBEntry(eb *EBlock) *DBEntry {
185+
func NewDBEntry(eb *EBlock) (*DBEntry, error) {
186186
e := new(DBEntry)
187187

188188
e.ChainID = eb.Header.ChainID
189-
e.KeyMR = eb.KeyMR()
189+
var err error
190+
e.KeyMR, err = eb.KeyMR()
191+
if err!=nil {
192+
return nil, err
193+
}
190194

191-
return e
195+
return e, nil
192196
}
193197

194-
func NewDBEntryFromECBlock(cb *ECBlock) *DBEntry {
198+
func NewDBEntryFromECBlock(cb *ECBlock) (*DBEntry, error) {
195199
e := &DBEntry{}
196200

197201
e.ChainID = cb.Header.ECChainID
198-
e.KeyMR = cb.HeaderHash()
202+
var err error
203+
e.KeyMR, err = cb.HeaderHash()
204+
if err!=nil {
205+
return nil, err
206+
}
199207

200-
return e
208+
return e, nil
201209
}
202210

203211
func NewDBEntryFromABlock(b *AdminBlock) *DBEntry {
@@ -439,7 +447,10 @@ func CreateDBlock(chain *DChain, prev *DirectoryBlock, cap uint) (b *DirectoryBl
439447
// Add DBEntry from an Entry Block
440448
func (c *DChain) AddEBlockToDBEntry(eb *EBlock) (err error) {
441449

442-
dbEntry := NewDBEntry(eb)
450+
dbEntry, err := NewDBEntry(eb)
451+
if err!=nil {
452+
return err
453+
}
443454
c.BlockMutex.Lock()
444455
c.NextBlock.DBEntries = append(c.NextBlock.DBEntries, dbEntry)
445456
c.BlockMutex.Unlock()
@@ -450,7 +461,10 @@ func (c *DChain) AddEBlockToDBEntry(eb *EBlock) (err error) {
450461
// Add DBEntry from an Entry Credit Block
451462
func (c *DChain) AddECBlockToDBEntry(ecb *ECBlock) (err error) {
452463

453-
dbEntry := NewDBEntryFromECBlock(ecb)
464+
dbEntry, err := NewDBEntryFromECBlock(ecb)
465+
if err!=nil {
466+
return err
467+
}
454468

455469
if len(c.NextBlock.DBEntries) < 3 {
456470
panic("1 DBEntries not initialized properly for block: " + string(c.NextDBHeight))

common/eblock.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,22 @@ func (c *EBlock) MarshalledSize() uint64 {
3232
// Its PrevKeyMR and PrevLedgerKeyMR are populated by the provided previous Entry
3333
// Block. If The previous Entry Block is nil (the new Entry Block is first in
3434
// the Chain) the relevent Entry Block Header fields will contain zeroed Hashes.
35-
func MakeEBlock(echain *EChain, prev *EBlock) *EBlock {
35+
func MakeEBlock(echain *EChain, prev *EBlock) (*EBlock, error) {
3636
e := NewEBlock()
3737
e.Header.ChainID = echain.ChainID
3838
if prev != nil {
39-
e.Header.PrevKeyMR = prev.KeyMR()
40-
e.Header.PrevLedgerKeyMR = prev.Hash()
39+
var err error
40+
e.Header.PrevKeyMR, err = prev.KeyMR()
41+
if err!=nil {
42+
return nil, err
43+
}
44+
e.Header.PrevLedgerKeyMR, err = prev.Hash()
45+
if err!=nil {
46+
return nil, err
47+
}
4148
}
4249
e.Header.EBSequence = echain.NextBlockHeight
43-
return e
50+
return e, nil
4451
}
4552

4653
// NewEBlock returns a blank initialized Entry Block with all of its fields
@@ -81,27 +88,27 @@ func (e *EBlock) BuildHeader() error {
8188

8289
// Hash returns the simple Sha256 hash of the serialized Entry Block. Hash is
8390
// used to provide the PrevLedgerKeyMR to the next Entry Block in a Chain.
84-
func (e *EBlock) Hash() *Hash {
91+
func (e *EBlock) Hash() (*Hash, error) {
8592
p, err := e.MarshalBinary()
8693
if err != nil {
87-
return NewHash()
94+
return nil, err
8895
}
89-
return Sha(p)
96+
return Sha(p), nil
9097
}
9198

9299
// KeyMR returns the hash of the hash of the Entry Block Header concatinated
93100
// with the Merkle Root of the Entry Block Body. The Body Merkle Root is
94101
// calculated by the func (e *EBlockBody) MR() which is called by the func
95102
// (e *EBlock) BuildHeader().
96-
func (e *EBlock) KeyMR() *Hash {
103+
func (e *EBlock) KeyMR() (*Hash, error) {
97104
// Sha(Sha(header) + BodyMR)
98105
e.BuildHeader()
99106
header, err := e.marshalHeaderBinary()
100107
if err != nil {
101-
return NewHash()
108+
return nil, err
102109
}
103110
h := Sha(header)
104-
return Sha(append(h.Bytes(), e.Header.BodyMR.Bytes()...))
111+
return Sha(append(h.Bytes(), e.Header.BodyMR.Bytes()...)), nil
105112
}
106113

107114
// MarshalBinary returns the serialized binary form of the Entry Block.

common/ecblock.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,39 @@ func NewECBlock() *ECBlock {
4242
return e
4343
}
4444

45-
func NextECBlock(prev *ECBlock) *ECBlock {
45+
func NextECBlock(prev *ECBlock) (*ECBlock, error) {
4646
e := NewECBlock()
47-
e.Header.PrevHeaderHash = prev.HeaderHash()
48-
e.Header.PrevLedgerKeyMR = prev.Hash()
47+
var err error
48+
e.Header.PrevHeaderHash, err = prev.HeaderHash()
49+
if err!=nil {
50+
return nil, err
51+
}
52+
e.Header.PrevLedgerKeyMR, err = prev.Hash()
53+
if err!=nil {
54+
return nil, err
55+
}
4956
e.Header.DBHeight = prev.Header.DBHeight + 1
50-
return e
57+
return e, nil
5158
}
5259

5360
func (e *ECBlock) AddEntry(entries ...ECBlockEntry) {
5461
e.Body.Entries = append(e.Body.Entries, entries...)
5562
}
5663

57-
func (e *ECBlock) Hash() *Hash {
64+
func (e *ECBlock) Hash() (*Hash, error) {
5865
p, err := e.MarshalBinary()
5966
if err != nil {
60-
return NewHash()
67+
return nil, err
6168
}
62-
return Sha(p)
69+
return Sha(p), nil
6370
}
6471

65-
func (e *ECBlock) HeaderHash() *Hash {
72+
func (e *ECBlock) HeaderHash() (*Hash, error) {
6673
p, err := e.marshalHeaderBinary()
6774
if err != nil {
68-
return NewHash()
75+
return nil, err
6976
}
70-
return Sha(p)
77+
return Sha(p), nil
7178
}
7279

7380
func (e *ECBlock) MarshalBinary() ([]byte, error) {

database/ldb/eblock.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,28 @@ func (db *LevelDb) ProcessEBlockBatch(eblock *common.EBlock) error {
3030

3131
// Insert the binary entry block
3232
var key []byte = []byte{byte(TBL_EB)}
33-
key = append(key, eblock.Hash().Bytes()...)
33+
hash, err:=eblock.Hash()
34+
if err!=nil {
35+
return err
36+
}
37+
key = append(key, hash.Bytes()...)
3438
db.lbatch.Put(key, binaryEblock)
3539

3640
// Insert the entry block merkle root cross reference
3741
key = []byte{byte(TBL_EB_MR)}
38-
key = append(key, eblock.KeyMR().Bytes()...)
39-
binaryEBHash, _ := eblock.Hash().MarshalBinary()
42+
keyMR, err:=eblock.KeyMR()
43+
if err!=nil {
44+
return err
45+
}
46+
key = append(key, keyMR.Bytes()...)
47+
eBlockHash, err := eblock.Hash()
48+
if err!=nil {
49+
return err
50+
}
51+
binaryEBHash, err := eBlockHash.MarshalBinary()
52+
if err!=nil {
53+
return err
54+
}
4055
db.lbatch.Put(key, binaryEBHash)
4156

4257
// Insert the entry block number cross reference
@@ -50,7 +65,11 @@ func (db *LevelDb) ProcessEBlockBatch(eblock *common.EBlock) error {
5065
// Update the chain head reference
5166
key = []byte{byte(TBL_CHAIN_HEAD)}
5267
key = append(key, eblock.Header.ChainID.Bytes()...)
53-
db.lbatch.Put(key, eblock.KeyMR().Bytes())
68+
keyMR, err = eblock.KeyMR()
69+
if err!=nil {
70+
return err
71+
}
72+
db.lbatch.Put(key, keyMR.Bytes())
5473

5574
err = db.lDb.Write(db.lbatch, db.wo)
5675
if err != nil {

database/ldb/ecblock.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ func (db *LevelDb) ProcessECBlockBatch(block *common.ECBlock) error {
2525

2626
// Insert the binary factom block
2727
var key []byte = []byte{byte(TBL_CB)}
28-
key = append(key, block.HeaderHash().Bytes()...)
28+
hash, err:=block.HeaderHash()
29+
if err!=nil {
30+
return err
31+
}
32+
key = append(key, hash.Bytes()...)
2933
db.lbatch.Put(key, binaryBlock)
3034

3135
// Update the chain head reference
3236
key = []byte{byte(TBL_CHAIN_HEAD)}
3337
key = append(key, common.EC_CHAINID...)
34-
db.lbatch.Put(key, block.HeaderHash().Bytes())
38+
hash, err = block.HeaderHash()
39+
if err!=nil {
40+
return err
41+
}
42+
db.lbatch.Put(key, hash.Bytes())
3543

3644
err = db.lDb.Write(db.lbatch, db.wo)
3745
if err != nil {

process/init.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ func initECChain() {
100100
} else {
101101
// Entry Credit Chain should have the same height as the dir chain
102102
ecchain.NextBlockHeight = dchain.NextDBHeight
103-
ecchain.NextBlock = common.NextECBlock(&ecBlocks[ecchain.NextBlockHeight-1])
103+
var err error
104+
ecchain.NextBlock, err = common.NextECBlock(&ecBlocks[ecchain.NextBlockHeight-1])
105+
if err!=nil {
106+
panic(err)
107+
}
104108
}
105109

106110
// create a backup copy before processing entries
@@ -270,12 +274,19 @@ func initEChainFromDB(chain *common.EChain) {
270274
}
271275
}
272276

277+
var err error
273278
if len(*eBlocks) == 0 {
274279
chain.NextBlockHeight = 0
275-
chain.NextBlock = common.MakeEBlock(chain, nil)
280+
chain.NextBlock, err = common.MakeEBlock(chain, nil)
281+
if err!=nil {
282+
panic(err)
283+
}
276284
} else {
277285
chain.NextBlockHeight = uint32(len(*eBlocks))
278-
chain.NextBlock = common.MakeEBlock(chain, &(*eBlocks)[len(*eBlocks)-1])
286+
chain.NextBlock, err = common.MakeEBlock(chain, &(*eBlocks)[len(*eBlocks)-1])
287+
if err!=nil {
288+
panic(err)
289+
}
279290
}
280291

281292
// Initialize chain with the first entry (Name and rules) for non-server mode
@@ -425,13 +436,19 @@ func validateFBlockByMR(mr *common.Hash) error {
425436
// Validate Entry Block by merkle root
426437
func validateEBlockByMR(cid *common.Hash, mr *common.Hash) error {
427438

428-
eb, _ := db.FetchEBlockByMR(mr)
439+
eb, err := db.FetchEBlockByMR(mr)
440+
if err!=nil {
441+
return err
442+
}
429443

430444
if eb == nil {
431445
return errors.New("Entry block not found in db for merkle root: " + mr.String())
432446
}
433-
434-
if !mr.IsSameAs(eb.KeyMR()) {
447+
keyMR, err:=eb.KeyMR()
448+
if err!=nil {
449+
return err
450+
}
451+
if !mr.IsSameAs(keyMR) {
435452
return errors.New("Entry block's merkle root does not match with: " + mr.String())
436453
}
437454

process/processor.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,12 @@ func newEntryBlock(chain *common.EChain) *common.EBlock {
949949
block.Header.EntryCount = uint32(len(block.Body.EBEntries))
950950

951951
chain.NextBlockHeight++
952-
chain.NextBlock = common.MakeEBlock(chain, block)
952+
var err error
953+
chain.NextBlock, err = common.MakeEBlock(chain, block)
954+
if err!=nil {
955+
procLog.Debug("EntryBlock Error: " + err.Error())
956+
return nil
957+
}
953958

954959
//Store the block in db
955960
db.ProcessEBlockBatch(block)
@@ -972,7 +977,12 @@ func newEntryCreditBlock(chain *common.ECChain) *common.ECBlock {
972977
// Create the block and add a new block for new coming entries
973978
chain.BlockMutex.Lock()
974979
chain.NextBlockHeight++
975-
chain.NextBlock = common.NextECBlock(block)
980+
var err error
981+
chain.NextBlock, err = common.NextECBlock(block)
982+
if err!=nil {
983+
procLog.Debug("EntryCreditBlock Error: " + err.Error())
984+
return nil
985+
}
976986
chain.NextBlock.AddEntry(serverIndex)
977987
chain.BlockMutex.Unlock()
978988

process/syncup.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ func procesECBlock(msg *wire.MsgECBlock) error {
104104
}
105105

106106
//Add it to mem pool before saving it in db
107-
fMemPool.addBlockMsg(msg, msg.ECBlock.HeaderHash().String())
107+
hash, err:=msg.ECBlock.HeaderHash()
108+
if err!=nil {
109+
return err
110+
}
111+
fMemPool.addBlockMsg(msg, hash.String())
108112

109113
procLog.Debug("SyncUp: MsgCBlock DBHeight=", msg.ECBlock.Header.DBHeight)
110114

@@ -125,7 +129,11 @@ func processEBlock(msg *wire.MsgEBlock) error {
125129
}
126130
*/
127131
//Add it to mem pool before saving it in db
128-
fMemPool.addBlockMsg(msg, msg.EBlk.KeyMR().String()) // store it in mem pool with MR as the key
132+
keyMR, err:=msg.EBlk.KeyMR()
133+
if err!=nil{
134+
return err
135+
}
136+
fMemPool.addBlockMsg(msg, keyMR.String()) // store it in mem pool with MR as the key
129137

130138
procLog.Debug("SyncUp: MsgEBlock DBHeight=", msg.EBlk.Header.DBHeight)
131139

0 commit comments

Comments
 (0)