Skip to content

Commit 58bdce1

Browse files
committed
Fixed admin block having wrong PrevFullHash in relation to FactomProject/WorkItems#584 .
1 parent 9c693c4 commit 58bdce1

6 files changed

Lines changed: 132 additions & 57 deletions

File tree

common/adminBlock.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ type AdminBlock struct {
3434
ABEntries []ABEntry //Interface
3535

3636
//Not Marshalized
37-
ABHash *Hash
37+
abHash *Hash
38+
}
39+
40+
func (ab *AdminBlock) ABHash() (*Hash, error) {
41+
if ab.abHash == nil {
42+
err := ab.buildABHash()
43+
if err != nil {
44+
return nil, err
45+
}
46+
}
47+
return ab.abHash, nil
3848
}
3949

4050
// Create an empty Admin Block
@@ -53,11 +63,10 @@ func CreateAdminBlock(chain *AdminChain, prev *AdminBlock, cap uint) (b *AdminBl
5363
if prev == nil {
5464
b.Header.PrevFullHash = NewHash()
5565
} else {
56-
57-
if prev.ABHash == nil {
58-
prev.BuildABHash()
66+
b.Header.PrevFullHash, err = prev.ABHash()
67+
if err != nil {
68+
return
5969
}
60-
b.Header.PrevFullHash = prev.ABHash
6170
}
6271

6372
b.Header.DBHeight = chain.NextBlockHeight
@@ -67,11 +76,13 @@ func CreateAdminBlock(chain *AdminChain, prev *AdminBlock, cap uint) (b *AdminBl
6776
}
6877

6978
// Build the sha hash for the admin block
70-
func (b *AdminBlock) BuildABHash() (err error) {
71-
72-
binaryAB, _ := b.MarshalBinary()
73-
b.ABHash = Sha512Half(binaryAB)
74-
79+
func (b *AdminBlock) buildABHash() (err error) {
80+
var binaryAB []byte
81+
binaryAB, err = b.MarshalBinary()
82+
if err != nil {
83+
return
84+
}
85+
b.abHash = Sha512Half(binaryAB)
7586
return
7687
}
7788

common/adminBlock_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,55 @@ import (
99
. "github.com/FactomProject/FactomCode/common"
1010
)
1111

12+
func TestAdminBlockPreviousHash(t *testing.T) {
13+
fmt.Printf("\n---\nTestAdminBlockMarshalUnmarshal\n---\n")
14+
15+
block := new(AdminBlock)
16+
data, _ := hex.DecodeString("000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
17+
_, err := block.UnmarshalBinaryData(data)
18+
if err != nil {
19+
t.Error(err)
20+
}
21+
22+
abHash, err := block.ABHash()
23+
if err != nil {
24+
t.Error(err)
25+
}
26+
27+
t.Logf("Current hash - %s", abHash.String())
28+
29+
if abHash.String() != "0a9aa1efbe7d0e8d9c1d460d1c78e3e7b50f984e65a3f3ee7b73100a94189dbf" {
30+
t.Error("Invalid ABHash")
31+
}
32+
33+
aChain := new(AdminChain)
34+
aChain.NextBlockHeight = 1
35+
aChain.ChainID = block.Header.AdminChainID
36+
37+
block2, err := CreateAdminBlock(aChain, block, 5)
38+
if err != nil {
39+
t.Error(err)
40+
}
41+
42+
abHash2, err := block2.ABHash()
43+
if err != nil {
44+
t.Error(err)
45+
}
46+
47+
t.Logf("Second hash - %s", abHash2.String())
48+
t.Logf("Previous hash - %s", block2.Header.PrevFullHash.String())
49+
50+
marshalled, err := block2.MarshalBinary()
51+
if err != nil {
52+
t.Error(err)
53+
}
54+
t.Logf("Marshalled - %X", marshalled)
55+
56+
if block2.Header.PrevFullHash.String() != abHash.String() {
57+
t.Error("PrevFullHash does not match ABHash")
58+
}
59+
}
60+
1261
func TestAdminBlockMarshalUnmarshal(t *testing.T) {
1362
fmt.Printf("\n---\nTestAdminBlockMarshalUnmarshal\n---\n")
1463

common/directoryBlock.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func NewDBEntryFromABlock(b *AdminBlock) *DBEntry {
136136
e := &DBEntry{}
137137

138138
e.ChainID = b.Header.AdminChainID
139-
e.KeyMR = b.ABHash
139+
e.KeyMR, _ = b.ABHash()
140140

141141
return e
142142
}
@@ -385,7 +385,10 @@ func (c *DChain) AddABlockToDBEntry(b *AdminBlock) (err error) {
385385

386386
dbEntry := &DBEntry{}
387387
dbEntry.ChainID = b.Header.AdminChainID
388-
dbEntry.KeyMR = b.ABHash
388+
dbEntry.KeyMR, err = b.ABHash()
389+
if err != nil {
390+
return
391+
}
389392

390393
if len(c.NextBlock.DBEntries) < 3 {
391394
panic("2 DBEntries not initialized properly for block: " + string(c.NextDBHeight))

database/ldb/ablock.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ func (db *LevelDb) ProcessABlockBatch(block *common.AdminBlock) error {
2424
return err
2525
}
2626

27-
if block.ABHash == nil {
28-
block.ABHash = common.Sha(binaryBlock)
27+
abHash, err := block.ABHash()
28+
if err != nil {
29+
return err
2930
}
3031

3132
// Insert the binary factom block
3233
var key []byte = []byte{byte(TBL_AB)}
33-
key = append(key, block.ABHash.Bytes()...)
34+
key = append(key, abHash.Bytes()...)
3435
db.lbatch.Put(key, binaryBlock)
3536

3637
// Insert the admin block number cross reference
@@ -39,12 +40,12 @@ func (db *LevelDb) ProcessABlockBatch(block *common.AdminBlock) error {
3940
bytes := make([]byte, 4)
4041
binary.BigEndian.PutUint32(bytes, block.Header.DBHeight)
4142
key = append(key, bytes...)
42-
db.lbatch.Put(key, block.ABHash.Bytes())
43+
db.lbatch.Put(key, abHash.Bytes())
4344

4445
// Update the chain head reference
4546
key = []byte{byte(TBL_CHAIN_HEAD)}
4647
key = append(key, common.ADMIN_CHAINID...)
47-
db.lbatch.Put(key, block.ABHash.Bytes())
48+
db.lbatch.Put(key, abHash.Bytes())
4849

4950
err = db.lDb.Write(db.lbatch, db.wo)
5051
if err != nil {
@@ -94,7 +95,10 @@ func (db *LevelDb) FetchAllABlocks() (aBlocks []common.AdminBlock, err error) {
9495
return nil, err
9596
}
9697
//TODO: to be optimized??
97-
aBlock.ABHash = common.Sha(iter.Value())
98+
_, err = aBlock.ABHash()
99+
if err != nil {
100+
return nil, err
101+
}
98102

99103
aBlockSlice = append(aBlockSlice, aBlock)
100104

process/processor.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,17 @@ import (
1515
"encoding/binary"
1616
"errors"
1717
"fmt"
18-
cp "github.com/FactomProject/FactomCode/controlpanel"
19-
"github.com/FactomProject/FactomCode/anchor"
20-
"github.com/FactomProject/FactomCode/common"
18+
"github.com/FactomProject/FactomCode/anchor"
19+
"github.com/FactomProject/FactomCode/common"
2120
"github.com/FactomProject/FactomCode/consensus"
21+
cp "github.com/FactomProject/FactomCode/controlpanel"
2222
"github.com/FactomProject/FactomCode/database"
2323
"github.com/FactomProject/FactomCode/util"
2424
"github.com/FactomProject/btcd/wire"
2525
"github.com/FactomProject/factoid/block"
2626
"github.com/davecgh/go-spew/spew"
2727
"sort"
2828
"strconv"
29-
3029
)
3130

3231
var _ = (*block.FBlock)(nil)
@@ -304,21 +303,21 @@ func serveMsgRequest(msg wire.FtmInternalMsg) error {
304303

305304
plMgr.AddMyProcessListItem(msgEom, nil, msgEom.EOM_Type)
306305
}
307-
308-
cp.CP.AddUpdate(
309-
"MinMark", // tag
310-
"status", // Category
311-
"Progress", // Title
312-
fmt.Sprintf("End of Minute %v\n",msgEom.EOM_Type)+ // Message
313-
fmt.Sprintf("Directory Block Height %v",dchain.NextDBHeight),
314-
0)
315-
}
306+
307+
cp.CP.AddUpdate(
308+
"MinMark", // tag
309+
"status", // Category
310+
"Progress", // Title
311+
fmt.Sprintf("End of Minute %v\n", msgEom.EOM_Type)+ // Message
312+
fmt.Sprintf("Directory Block Height %v", dchain.NextDBHeight),
313+
0)
314+
}
316315

317316
case wire.CmdDirBlock:
318317
if nodeMode == common.SERVER_NODE {
319318
break
320319
}
321-
320+
322321
dirBlock, ok := msg.(*wire.MsgDirBlock)
323322
if ok {
324323
err := processDirBlock(dirBlock)
@@ -328,9 +327,9 @@ func serveMsgRequest(msg wire.FtmInternalMsg) error {
328327
} else {
329328
return errors.New("Error in processing msg:" + fmt.Sprintf("%+v", msg))
330329
}
331-
330+
332331
case wire.CmdFBlock:
333-
332+
334333
if nodeMode == common.SERVER_NODE {
335334
break
336335
}
@@ -978,12 +977,18 @@ func newAdminBlock(chain *common.AdminChain) *common.AdminBlock {
978977

979978
block.Header.MessageCount = uint32(len(block.ABEntries))
980979
block.Header.BodySize = uint32(block.MarshalledSize() - block.Header.MarshalledSize())
981-
block.BuildABHash()
980+
_, err := block.ABHash()
981+
if err != nil {
982+
panic(err)
983+
}
982984

983985
// Create the block and add a new block for new coming entries
984986
chain.BlockMutex.Lock()
985987
chain.NextBlockHeight++
986-
chain.NextBlock, _ = common.CreateAdminBlock(chain, block, 10)
988+
chain.NextBlock, err = common.CreateAdminBlock(chain, block, 10)
989+
if err != nil {
990+
panic(err)
991+
}
987992
chain.BlockMutex.Unlock()
988993

989994
//Store the block in db

process/syncup.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ package process
66

77
import (
88
"errors"
9-
cp "github.com/FactomProject/FactomCode/controlpanel"
10-
"github.com/FactomProject/FactomCode/common"
11-
"github.com/FactomProject/FactomCode/database"
9+
"github.com/FactomProject/FactomCode/common"
10+
cp "github.com/FactomProject/FactomCode/controlpanel"
11+
"github.com/FactomProject/FactomCode/database"
1212
"github.com/FactomProject/btcd/wire"
1313
"github.com/davecgh/go-spew/spew"
1414
"strconv"
@@ -27,12 +27,12 @@ func processDirBlock(msg *wire.MsgDirBlock) error {
2727
blk, _ := db.FetchDBlockByHeight(msg.DBlk.Header.DBHeight)
2828
if blk != nil {
2929
procLog.Info("DBlock already exists for height:" + string(msg.DBlk.Header.DBHeight))
30-
cp.CP.AddUpdate(
31-
"DBOverlap", // tag
32-
"warning", // Category
33-
"Directory Block Overlap", // Title
34-
"DBlock already exists for height:"+ string(msg.DBlk.Header.DBHeight), // Message
35-
0) // Expire
30+
cp.CP.AddUpdate(
31+
"DBOverlap", // tag
32+
"warning", // Category
33+
"Directory Block Overlap", // Title
34+
"DBlock already exists for height:"+string(msg.DBlk.Header.DBHeight), // Message
35+
0) // Expire
3636
return nil
3737
}
3838

@@ -43,13 +43,13 @@ func processDirBlock(msg *wire.MsgDirBlock) error {
4343
fMemPool.addBlockMsg(msg, strconv.Itoa(int(msg.DBlk.Header.DBHeight))) // store in mempool with the height as the key
4444

4545
procLog.Debug("SyncUp: MsgDirBlock DBHeight=", msg.DBlk.Header.DBHeight)
46-
cp.CP.AddUpdate(
47-
"DBSyncUp", // tag
48-
"Status", // Category
49-
"SyncUp:", // Title
50-
"MsgDirBlock DBHeigth=:"+ string(msg.DBlk.Header.DBHeight), // Message
51-
0) // Expire
52-
46+
cp.CP.AddUpdate(
47+
"DBSyncUp", // tag
48+
"Status", // Category
49+
"SyncUp:", // Title
50+
"MsgDirBlock DBHeigth=:"+string(msg.DBlk.Header.DBHeight), // Message
51+
0) // Expire
52+
5353
return nil
5454
}
5555

@@ -82,8 +82,11 @@ func processABlock(msg *wire.MsgABlock) error {
8282
}
8383

8484
//Add it to mem pool before saving it in db
85-
msg.ABlk.BuildABHash()
86-
fMemPool.addBlockMsg(msg, msg.ABlk.ABHash.String()) // store in mem pool with ABHash as key
85+
abHash, err := msg.ABlk.ABHash()
86+
if err != nil {
87+
return err
88+
}
89+
fMemPool.addBlockMsg(msg, abHash.String()) // store in mem pool with ABHash as key
8790

8891
procLog.Debug("SyncUp: MsgABlock DBHeight=", msg.ABlk.Header.DBHeight)
8992

@@ -196,7 +199,7 @@ func validateBlocksFromMemPool(b *common.DirectoryBlock, fMemPool *ftmMemPool, d
196199
if h.String() != common.GENESIS_DIR_BLOCK_HASH {
197200
// panic for milestone 1
198201
//panic("Genesis dir block is not as expected: " + h.String())
199-
procLog.Errorf("Genesis dir block is not as expected: " + h.String())
202+
procLog.Errorf("Genesis dir block is not as expected: " + h.String())
200203
}
201204
}
202205

@@ -349,12 +352,12 @@ func deleteBlocksFromMemPool(b *common.DirectoryBlock, fMemPool *ftmMemPool) err
349352
default:
350353
eBlkMsg, _ := fMemPool.blockpool[dbEntry.KeyMR.String()].(*wire.MsgEBlock)
351354
for _, ebEntry := range eBlkMsg.EBlk.Body.EBEntries {
352-
fMemPool.deleteBlockMsg(ebEntry.String())
355+
fMemPool.deleteBlockMsg(ebEntry.String())
353356
}
354357
fMemPool.deleteBlockMsg(dbEntry.KeyMR.String())
355358
}
356359
}
357-
fMemPool.deleteBlockMsg(strconv.Itoa(int(b.Header.DBHeight)))
360+
fMemPool.deleteBlockMsg(strconv.Itoa(int(b.Header.DBHeight)))
358361

359362
return nil
360363
}

0 commit comments

Comments
 (0)