Skip to content

Commit 63eccab

Browse files
committed
merge from dev
2 parents 417941e + 711de7c commit 63eccab

9 files changed

Lines changed: 134 additions & 51 deletions

File tree

factomd/blocks_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
// "bytes"
5+
"fmt"
6+
"github.com/davecgh/go-spew/spew"
7+
"testing"
8+
"github.com/FactomProject/FactomCode/common"
9+
)
10+
11+
var dirBlockHeight uint32 = 241
12+
13+
func TestBlocks(t *testing.T) {
14+
fmt.Println("\nTest Blocks===========================================================================")
15+
16+
initDB()
17+
18+
// directory block ------------------
19+
dblk, _ := db.FetchDBlockByHeight(dirBlockHeight)
20+
fmt.Printf("dblk=%s\n", spew.Sdump(dblk))
21+
22+
// admin chain ------------------------
23+
achainid := new(common.Hash)
24+
achainid.SetBytes(common.ADMIN_CHAINID)
25+
26+
//EC chain ------------------------------------
27+
ecchainid := new(common.Hash)
28+
ecchainid.SetBytes(common.EC_CHAINID)
29+
30+
//factoid chain ------------------------------------
31+
fchainid := new(common.Hash)
32+
fchainid.SetBytes(common.FACTOID_CHAINID)
33+
34+
for _, dbEntry := range dblk.DBEntries {
35+
switch dbEntry.ChainID.String() {
36+
case ecchainid.String():
37+
ecblk, _ := db.FetchECBlockByHash(dbEntry.MerkleRoot)
38+
fmt.Printf("ecblk=%s\n", spew.Sdump(ecblk))
39+
case achainid.String():
40+
ablk, _ := db.FetchABlockByHash(dbEntry.MerkleRoot)
41+
fmt.Printf("ablk=%s\n", spew.Sdump(ablk))
42+
case fchainid.String():
43+
fblk, _ := db.FetchFBlockByHash(dbEntry.MerkleRoot)
44+
fmt.Printf("fblk=%s\n", spew.Sdump(fblk))
45+
default:
46+
eBlk, _ := db.FetchEBlockByMR(dbEntry.MerkleRoot)
47+
// validate every entry in EBlock
48+
for _, ebEntry := range eBlk.EBEntries {
49+
// continue if the entry arleady exists in db
50+
entry, _ := db.FetchEntryByHash(ebEntry.EntryHash)
51+
fmt.Printf("entryHash=%s", spew.Sdump(ebEntry.EntryHash))
52+
fmt.Printf("entry=%s\n", spew.Sdump(entry))
53+
}
54+
55+
}
56+
}
57+
58+
}

factomd/test/chainhead_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestDblock(t *testing.T) {
13-
fmt.Println("\nTest dblock===========================================================================")
13+
fmt.Println("\nTest chain head===========================================================================")
1414

1515
initDB()
1616

process/init.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ func initFctChain() {
182182
fchain.NextBlock = block.GetGenesisBlock(
183183
0, 1000000, 10, 200000000000)
184184
} else {
185-
fchain.NextBlockHeight = dchain.NextBlockHeight
186-
common.FactoidState.ProcessEndOfBlock2(dchain.NextBlockHeight)
187-
fchain.NextBlock = common.FactoidState.GetCurrentBlock()
185+
fchain.NextBlockHeight = dchain.NextBlockHeight
186+
common.FactoidState.ProcessEndOfBlock2(dchain.NextBlockHeight)
187+
fchain.NextBlock = common.FactoidState.GetCurrentBlock()
188188
}
189189

190190
exportFctChain(fchain)
@@ -229,6 +229,7 @@ func initializeECreditMap(block *common.ECBlock) {
229229
// Don't add the Increases to Factoid state, the Factoid processing will do that.
230230
default:
231231
fmt.Println("UNKNOWN\n")
232+
// panic("UNKNOWN - doesn't sound good, Jack please check...")
232233
}
233234
}
234235
}

process/mempool.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
package process
66

77
import (
8+
"errors"
9+
"github.com/FactomProject/FactomCode/common"
810
"github.com/FactomProject/btcd/wire"
9-
"github.com/FactomProject/FactomCode/common"
1011
"sync"
1112
"time"
12-
"errors"
1313
)
1414

1515
// ftmMemPool is used as a source of factom transactions
@@ -38,7 +38,7 @@ func (mp *ftmMemPool) addMsg(msg wire.Message, hash *wire.ShaHash) error {
3838
if len(mp.pool) > common.MAX_TX_POOL_SIZE {
3939
return errors.New("Transaction mem pool exceeds the limit.")
4040
}
41-
41+
4242
mp.pool[*hash] = msg
4343

4444
return nil
@@ -48,19 +48,19 @@ func (mp *ftmMemPool) addMsg(msg wire.Message, hash *wire.ShaHash) error {
4848
func (mp *ftmMemPool) addOrphanMsg(msg wire.Message, hash *wire.ShaHash) error {
4949

5050
if len(mp.orphans) > common.MAX_ORPHAN_SIZE {
51-
errors.New ("Ophan mem pool exceeds the limit.")
51+
errors.New("Ophan mem pool exceeds the limit.")
5252
}
53-
53+
5454
mp.orphans[*hash] = msg
5555

5656
return nil
5757
}
5858

5959
// Add a factom block message to the Mem pool
6060
func (mp *ftmMemPool) addBlockMsg(msg wire.Message, hash string) error {
61-
61+
6262
if len(mp.blockpool) > common.MAX_BLK_POOL_SIZE {
63-
errors.New ("Block mem pool exceeds the limit. Please restart.")
63+
errors.New("Block mem pool exceeds the limit. Please restart.")
6464
}
6565

6666
mp.blockpool[hash] = msg

process/processor.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ var (
7676

7777
// Get the configurations
7878
func LoadConfigurations(cfg *util.FactomdConfig) {
79+
util.Trace("LoadConf")
80+
7981
//setting the variables by the valued form the config file
8082
logLevel = cfg.Log.LogLevel
8183
dataStorePath = cfg.App.DataStorePath
@@ -86,7 +88,13 @@ func LoadConfigurations(cfg *util.FactomdConfig) {
8688

8789
FactomdUser = cfg.Btc.RpcUser
8890
FactomdPass = cfg.Btc.RpcPass
89-
91+
92+
util.Trace(logLevel)
93+
util.Trace(ldbpath)
94+
util.Trace(FactomdUser)
95+
util.Trace(FactomdPass)
96+
97+
util.Trace(cfg.Btc.WalletPassphrase)
9098
}
9199

92100
// Initialize the processor
@@ -629,7 +637,7 @@ func processFromOrphanPool() error {
629637
continue
630638
}
631639
delete(fMemPool.orphans, k)
632-
640+
633641
case wire.CmdCommitEntry:
634642
msgCommitEntry, _ := msg.(*wire.MsgCommitEntry)
635643
err := processCommitEntry(msgCommitEntry)
@@ -768,7 +776,7 @@ func buildGenesisBlocks() error {
768776
fmt.Println("Factoid genesis block hash:", FBlock.GetHash())
769777
exportFctChain(fchain)
770778
// Add transactions from genesis block to factoid balances
771-
common.FactoidState.AddTransactionBlock(FBlock)
779+
common.FactoidState.AddTransactionBlock(FBlock)
772780

773781
// Directory Block chain
774782
procLog.Debug("in buildGenesisBlocks")

process/syncup.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func processFBlock(msg *wire.MsgFBlock) error {
5353
key, _ := msg.SC.GetHash().MarshalText()
5454
//Add it to mem pool before saving it in db
5555
fMemPool.addBlockMsg(msg, string(key)) // stored in mem pool with the MR as the key
56-
57-
procLog.Debugf("SyncUp: MsgFBlock=%s\n", spew.Sdump(msg.SC))
56+
57+
procLog.Debugf("SyncUp: MsgFBlock=%s\n", spew.Sdump(msg.SC))
5858

5959
return nil
6060

@@ -89,11 +89,9 @@ func procesECBlock(msg *wire.MsgECBlock) error {
8989

9090
//Add it to mem pool before saving it in db
9191
fMemPool.addBlockMsg(msg, msg.ECBlock.KeyMR().String())
92-
93-
9492

9593
// for debugging??
96-
procLog.Debugf("SyncUp: msg.ECBlock.KeyMR().String()=%s\n", msg.ECBlock.KeyMR().String())
94+
procLog.Debugf("SyncUp: msg.ECBlock.KeyMR().String()=%s\n", msg.ECBlock.KeyMR().String())
9795
procLog.Debugf("SyncUp: MsgCBlock=%s\n", spew.Sdump(msg.ECBlock))
9896

9997
return nil
@@ -108,11 +106,11 @@ func processEBlock(msg *wire.MsgEBlock) error {
108106
if nodeMode == common.SERVER_NODE {
109107
return errors.New("Server received msg:" + msg.Command())
110108
}
111-
/*
112-
if msg.EBlk.Header.DBHeight >= dchain.NextBlockHeight || msg.EBlk.Header.DBHeight < 0 {
113-
return errors.New("MsgEBlock has an invalid DBHeight:" + strconv.Itoa(int(msg.EBlk.Header.DBHeight)))
114-
}
115-
*/
109+
/*
110+
if msg.EBlk.Header.DBHeight >= dchain.NextBlockHeight || msg.EBlk.Header.DBHeight < 0 {
111+
return errors.New("MsgEBlock has an invalid DBHeight:" + strconv.Itoa(int(msg.EBlk.Header.DBHeight)))
112+
}
113+
*/
116114
//Add it to mem pool before saving it in db
117115
msg.EBlk.BuildMerkleRoot()
118116
fMemPool.addBlockMsg(msg, msg.EBlk.MerkleRoot.String()) // store it in mem pool with MR as the key
@@ -203,21 +201,21 @@ func validateBlocksFromMemPool(b *common.DirectoryBlock, fMemPool *ftmMemPool, d
203201
return false
204202
}
205203
case achain.ChainID.String():
206-
if msg, ok := fMemPool.blockpool[dbEntry.MerkleRoot.String()]; !ok {
204+
if msg, ok := fMemPool.blockpool[dbEntry.MerkleRoot.String()]; !ok {
207205
return false
208206
} else {
209207
// validate signature of the previous dir block
210208
aBlkMsg, _ := msg.(*wire.MsgABlock)
211-
if !validateDBSignature(aBlkMsg.ABlk, dchain) {
209+
if !validateDBSignature(aBlkMsg.ABlk, dchain) {
212210
return false
213211
}
214212
}
215213
case fchain.ChainID.String():
216-
if _, ok := fMemPool.blockpool[dbEntry.MerkleRoot.String()]; !ok {
214+
if _, ok := fMemPool.blockpool[dbEntry.MerkleRoot.String()]; !ok {
217215
return false
218216
}
219217
default:
220-
if msg, ok := fMemPool.blockpool[dbEntry.MerkleRoot.String()]; !ok {
218+
if msg, ok := fMemPool.blockpool[dbEntry.MerkleRoot.String()]; !ok {
221219
return false
222220
} else {
223221
eBlkMsg, _ := msg.(*wire.MsgEBlock)
@@ -226,7 +224,7 @@ func validateBlocksFromMemPool(b *common.DirectoryBlock, fMemPool *ftmMemPool, d
226224
if _, foundInMemPool := fMemPool.blockpool[ebEntry.EntryHash.String()]; !foundInMemPool {
227225
// continue if the entry arleady exists in db
228226
entry, _ := db.FetchEntryByHash(ebEntry.EntryHash)
229-
if entry == nil {
227+
if entry == nil {
230228
return false
231229
}
232230
}

wsapi/wsapi.go

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -343,39 +343,57 @@ func handleChainHead(ctx *web.Context, chainid string) {
343343

344344
}
345345

346-
func handleEntryCreditBalance(ctx *web.Context, eckey string) {
347-
type ecbal struct {
348-
Balance uint32
349-
}
346+
type ecbal struct {
347+
Balance uint32
348+
}
350349

351-
b := new(ecbal)
352-
if bal, err := factomapi.ECBalance(eckey); err != nil {
353-
wsLog.Error(err)
354-
return
355-
} else {
356-
b.Balance = bal
357-
}
358-
if p, err := json.Marshal(b); err != nil {
359-
wsLog.Error(err)
360-
return
361-
} else {
362-
ctx.Write(p)
363-
}
350+
func handleEntryCreditBalance(ctx *web.Context, eckey string) {
351+
type ecbal struct {
352+
Response string
353+
Success bool
354+
}
355+
var b ecbal
356+
adr, err := hex.DecodeString(eckey)
357+
if err == nil && len(adr) != common.HASH_LENGTH {
358+
b = ecbal{Response: "Invalid Address", Success: false,}
359+
}
360+
if err == nil {
361+
if bal, err := factomapi.ECBalance(eckey); err != nil {
362+
wsLog.Error(err)
363+
return
364+
} else {
365+
str := fmt.Sprintf("%d",bal)
366+
b = ecbal{Response: str, Success: true,}
367+
}
368+
} else {
369+
b = ecbal{Response: err.Error(), Success: false,}
370+
}
371+
372+
if p, err := json.Marshal(b); err != nil {
373+
wsLog.Error(err)
374+
return
375+
} else {
376+
ctx.Write(p)
377+
}
364378

365379
}
366380

367381
func handleFactoidBalance(ctx *web.Context, eckey string) {
368382
type fbal struct {
369-
Balance int64
383+
Response string
384+
Success bool
370385
}
371386
var b fbal
372387
adr, err := hex.DecodeString(eckey)
373-
if err == nil && len(adr) == common.HASH_LENGTH {
388+
if err == nil && len(adr) != common.HASH_LENGTH {
389+
b = fbal{Response: "Invalid Address", Success: false,}
390+
}
391+
if err == nil {
374392
v := int64(common.FactoidState.GetBalance(fct.NewAddress(adr)))
375-
376-
b = fbal{Balance: v}
393+
str := fmt.Sprintf("%d",v)
394+
b = fbal{Response: str, Success: true,}
377395
} else {
378-
b = fbal{Balance: 0}
396+
b = fbal{Response: err.Error(), Success: false}
379397
}
380398

381399
if p, err := json.Marshal(b); err != nil {

0 commit comments

Comments
 (0)