aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain
diff options
context:
space:
mode:
Diffstat (limited to 'ethchain')
-rw-r--r--ethchain/block_chain.go25
-rw-r--r--ethchain/state_manager.go8
2 files changed, 19 insertions, 14 deletions
diff --git a/ethchain/block_chain.go b/ethchain/block_chain.go
index 113e313ac..a5dcec438 100644
--- a/ethchain/block_chain.go
+++ b/ethchain/block_chain.go
@@ -40,13 +40,11 @@ func (bc *BlockChain) Genesis() *Block {
func (bc *BlockChain) NewBlock(coinbase []byte) *Block {
var root interface{}
- var lastBlockTime int64
hash := ZeroHash256
if bc.CurrentBlock != nil {
root = bc.CurrentBlock.state.Trie.Root
hash = bc.LastBlockHash
- lastBlockTime = bc.CurrentBlock.Time
}
block := CreateBlock(
@@ -61,15 +59,7 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block {
parent := bc.CurrentBlock
if parent != nil {
- diff := new(big.Int)
-
- adjust := new(big.Int).Rsh(parent.Difficulty, 10)
- if block.Time >= lastBlockTime+5 {
- diff.Sub(parent.Difficulty, adjust)
- } else {
- diff.Add(parent.Difficulty, adjust)
- }
- block.Difficulty = diff
+ block.Difficulty = CalcDifficulty(block, parent)
block.Number = new(big.Int).Add(bc.CurrentBlock.Number, ethutil.Big1)
block.GasLimit = block.CalcGasLimit(bc.CurrentBlock)
@@ -78,6 +68,19 @@ func (bc *BlockChain) NewBlock(coinbase []byte) *Block {
return block
}
+func CalcDifficulty(block, parent *Block) *big.Int {
+ diff := new(big.Int)
+
+ adjust := new(big.Int).Rsh(parent.Difficulty, 10)
+ if block.Time >= parent.Time+5 {
+ diff.Sub(parent.Difficulty, adjust)
+ } else {
+ diff.Add(parent.Difficulty, adjust)
+ }
+
+ return diff
+}
+
func (bc *BlockChain) Reset() {
AddTestNetFunds(bc.genesisBlock)
diff --git a/ethchain/state_manager.go b/ethchain/state_manager.go
index 93fd1ec58..589b99ac2 100644
--- a/ethchain/state_manager.go
+++ b/ethchain/state_manager.go
@@ -346,9 +346,6 @@ func (sm *StateManager) CalculateTD(block *Block) bool {
// an uncle or anything that isn't on the current block chain.
// Validation validates easy over difficult (dagger takes longer time = difficult)
func (sm *StateManager) ValidateBlock(block *Block) error {
- // TODO
- // 2. Check if the difficulty is correct
-
// Check each uncle's previous hash. In order for it to be valid
// is if it has the same block hash as the current
parent := sm.bc.GetBlock(block.PrevHash)
@@ -360,6 +357,11 @@ func (sm *StateManager) ValidateBlock(block *Block) error {
}
*/
+ expd := CalcDifficulty(block, parent)
+ if expd.Cmp(block.Difficulty) < 0 {
+ return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
+ }
+
diff := block.Time - parent.Time
if diff < 0 {
return ValidationError("Block timestamp less then prev block %v (%v - %v)", diff, block.Time, sm.bc.CurrentBlock.Time)