diff --git a/core/block_processor.go b/core/block_processor.go index e912c0b6e1..f50ebb55a2 100644 --- a/core/block_processor.go +++ b/core/block_processor.go @@ -386,7 +386,7 @@ func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, check return BlockEqualTSErr } - expd := CalcDifficulty(int64(block.Time), int64(parent.Time()), parent.Difficulty()) + expd := CalcDifficulty(block.Time, parent.Time(), parent.Difficulty()) if expd.Cmp(block.Difficulty) != 0 { return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd) } diff --git a/core/chain_makers.go b/core/chain_makers.go index 07670608a5..501fe7a921 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -171,7 +171,7 @@ func makeHeader(parent *types.Block, state *state.StateDB) *types.Header { Root: state.Root(), ParentHash: parent.Hash(), Coinbase: parent.Coinbase(), - Difficulty: CalcDifficulty(int64(time), int64(parent.Time()), parent.Difficulty()), + Difficulty: CalcDifficulty(time, parent.Time(), parent.Difficulty()), GasLimit: CalcGasLimit(parent), GasUsed: new(big.Int), Number: new(big.Int).Add(parent.Number(), common.Big1), diff --git a/core/chain_manager.go b/core/chain_manager.go index bd49bafc21..3c5eb0e8a9 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -611,7 +611,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { // Allow up to MaxFuture second in the future blocks. If this limit // is exceeded the chain is discarded and processed at a later time // if given. - if max := time.Now().Unix() + maxTimeFutureBlocks; int64(block.Time()) > max { + if max := uint64(time.Now().Unix()) + maxTimeFutureBlocks; block.Time() > max { return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max) } diff --git a/core/chain_util.go b/core/chain_util.go index 96c9a03d8f..7e3d8eba8e 100644 --- a/core/chain_util.go +++ b/core/chain_util.go @@ -31,10 +31,16 @@ import ( // CalcDifficulty is the difficulty adjustment algorithm. It returns // the difficulty that a new block b should have when created at time // given the parent block's time and difficulty. -func CalcDifficulty(time int64, parentTime int64, parentDiff *big.Int) *big.Int { +func CalcDifficulty(time, parentTime uint64, parentDiff *big.Int) *big.Int { diff := new(big.Int) adjust := new(big.Int).Div(parentDiff, params.DifficultyBoundDivisor) - if big.NewInt(time-parentTime).Cmp(params.DurationLimit) < 0 { + bigTime := new(big.Int) + bigParentTime := new(big.Int) + + bigTime.SetUint64(time) + bigParentTime.SetUint64(parentTime) + + if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 { diff.Add(parentDiff, adjust) } else { diff.Sub(parentDiff, adjust) diff --git a/miner/worker.go b/miner/worker.go index 79514b2311..0857217514 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -427,7 +427,7 @@ func (self *worker) commitNewWork() { header := &types.Header{ ParentHash: parent.Hash(), Number: num.Add(num, common.Big1), - Difficulty: core.CalcDifficulty(int64(tstamp), int64(parent.Time()), parent.Difficulty()), + Difficulty: core.CalcDifficulty(uint64(tstamp), parent.Time(), parent.Difficulty()), GasLimit: core.CalcGasLimit(parent), GasUsed: new(big.Int), Coinbase: self.coinbase,