Fixed validation

This commit is contained in:
obscuren 2014-01-12 16:50:09 +01:00
parent 406adb4563
commit c3fabfe00a

View File

@ -24,11 +24,20 @@ func NewBlockChain() *BlockChain {
bc.TD = new(big.Int) bc.TD = new(big.Int)
bc.TD.SetBytes(ethutil.Config.Db.LastKnownTD()) bc.TD.SetBytes(ethutil.Config.Db.LastKnownTD())
// TODO get last block from the database
//bc.LastBlock = bc.genesisBlock
return bc return bc
} }
func (bc *BlockChain) HasBlock(hash string) bool { 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 { 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 // Set the new total difficulty back to the block chain
bm.bc.TD = td bm.bc.TD = td
if Debug {
log.Println("TD(block) =", td)
}
return true 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 // 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 // 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") 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 // 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") return errors.New("Block's nonce is invalid")
} }
log.Println("Block validation PASSED")
return nil return nil
} }