aboutsummaryrefslogtreecommitdiffstats
path: root/ethchain/block_chain.go
diff options
context:
space:
mode:
Diffstat (limited to 'ethchain/block_chain.go')
-rw-r--r--ethchain/block_chain.go25
1 files changed, 14 insertions, 11 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)