aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-01-12 23:50:09 +0800
committerobscuren <geffobscura@gmail.com>2014-01-12 23:50:09 +0800
commitc3fabfe00a81b548dceb8ecf1281087e51c5fddf (patch)
tree053f7d9e9d2a14f3fd3581eb90008c999db577d4
parent406adb4563d4664e4ce63a7786ea57f797c42e47 (diff)
downloaddexon-c3fabfe00a81b548dceb8ecf1281087e51c5fddf.tar.gz
dexon-c3fabfe00a81b548dceb8ecf1281087e51c5fddf.tar.zst
dexon-c3fabfe00a81b548dceb8ecf1281087e51c5fddf.zip
Fixed validation
-rw-r--r--block_manager.go23
1 files changed, 20 insertions, 3 deletions
diff --git a/block_manager.go b/block_manager.go
index 1a73a45f2..0c7410022 100644
--- a/block_manager.go
+++ b/block_manager.go
@@ -24,11 +24,20 @@ func NewBlockChain() *BlockChain {
bc.TD = new(big.Int)
bc.TD.SetBytes(ethutil.Config.Db.LastKnownTD())
+
+ // TODO get last block from the database
+ //bc.LastBlock = bc.genesisBlock
+
return bc
}
func (bc *BlockChain) HasBlock(hash string) bool {
- return bc.LastBlock.State().Get(hash) != ""
+ data, _ := ethutil.Config.Db.Get([]byte(hash))
+ return len(data) != 0
+}
+
+func (bc *BlockChain) GenesisBlock() *ethutil.Block {
+ return bc.genesisBlock
}
type BlockManager struct {
@@ -107,6 +116,10 @@ func (bm *BlockManager) CalculateTD(block *ethutil.Block) bool {
// Set the new total difficulty back to the block chain
bm.bc.TD = td
+ if Debug {
+ log.Println("TD(block) =", td)
+ }
+
return true
}
@@ -122,7 +135,8 @@ func (bm *BlockManager) ValidateBlock(block *ethutil.Block) error {
// Check if we have the parent hash, if it isn't known we discard it
// Reasons might be catching up or simply an invalid block
- if !bm.bc.HasBlock(block.PrevHash) {
+ if bm.bc.LastBlock != nil && block.PrevHash == "" &&
+ !bm.bc.HasBlock(block.PrevHash) {
return errors.New("Block's parent unknown")
}
@@ -139,10 +153,13 @@ func (bm *BlockManager) ValidateBlock(block *ethutil.Block) error {
}
// Verify the nonce of the block. Return an error if it's not valid
- if !DaggerVerify(ethutil.BigD(block.Hash()), block.Difficulty, block.Nonce) {
+ if bm.bc.LastBlock != nil && block.PrevHash == "" &&
+ !DaggerVerify(ethutil.BigD(block.Hash()), block.Difficulty, block.Nonce) {
return errors.New("Block's nonce is invalid")
}
+ log.Println("Block validation PASSED")
+
return nil
}