From 0b4fe8d1929ad64ed576f7560dde4179d71ecfcb Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Tue, 2 Apr 2019 22:28:48 +0200 Subject: all: simplify timestamps to uint64 (#19372) * all: simplify timestamps to uint64 * tests: update definitions * clef, faucet, mobile: leftover uint64 fixups * ethash: fix tests * graphql: update schema for timestamp * ethash: remove unused variable --- consensus/clique/clique.go | 12 ++++++------ consensus/ethash/algorithm_test.go | 2 +- consensus/ethash/consensus.go | 21 ++++++++------------- consensus/ethash/consensus_test.go | 2 +- 4 files changed, 16 insertions(+), 21 deletions(-) (limited to 'consensus') diff --git a/consensus/clique/clique.go b/consensus/clique/clique.go index 967a843de..6bdee6dd5 100644 --- a/consensus/clique/clique.go +++ b/consensus/clique/clique.go @@ -249,7 +249,7 @@ func (c *Clique) verifyHeader(chain consensus.ChainReader, header *types.Header, number := header.Number.Uint64() // Don't waste time checking blocks from the future - if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 { + if header.Time > uint64(time.Now().Unix()) { return consensus.ErrFutureBlock } // Checkpoint blocks need to enforce zero beneficiary @@ -321,7 +321,7 @@ func (c *Clique) verifyCascadingFields(chain consensus.ChainReader, header *type if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash { return consensus.ErrUnknownAncestor } - if parent.Time.Uint64()+c.config.Period > header.Time.Uint64() { + if parent.Time+c.config.Period > header.Time { return ErrInvalidTimestamp } // Retrieve the snapshot needed to verify this header and cache it @@ -540,9 +540,9 @@ func (c *Clique) Prepare(chain consensus.ChainReader, header *types.Header) erro if parent == nil { return consensus.ErrUnknownAncestor } - header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(c.config.Period)) - if header.Time.Int64() < time.Now().Unix() { - header.Time = big.NewInt(time.Now().Unix()) + header.Time = parent.Time + c.config.Period + if header.Time < uint64(time.Now().Unix()) { + header.Time = uint64(time.Now().Unix()) } return nil } @@ -607,7 +607,7 @@ func (c *Clique) Seal(chain consensus.ChainReader, block *types.Block, results c } } // Sweet, the protocol permits us to sign the block, wait for our time - delay := time.Unix(header.Time.Int64(), 0).Sub(time.Now()) // nolint: gosimple + delay := time.Unix(int64(header.Time), 0).Sub(time.Now()) // nolint: gosimple if header.Difficulty.Cmp(diffNoTurn) == 0 { // It's not our turn explicitly to sign, delay it a bit wiggle := time.Duration(len(snap.Signers)/2+1) * wiggleTime diff --git a/consensus/ethash/algorithm_test.go b/consensus/ethash/algorithm_test.go index c58479e28..cf8552f3a 100644 --- a/consensus/ethash/algorithm_test.go +++ b/consensus/ethash/algorithm_test.go @@ -716,7 +716,7 @@ func TestConcurrentDiskCacheGeneration(t *testing.T) { Difficulty: big.NewInt(167925187834220), GasLimit: 4015682, GasUsed: 0, - Time: big.NewInt(1488928920), + Time: 1488928920, Extra: []byte("www.bw.com"), MixDigest: common.HexToHash("0x3e140b0784516af5e5ec6730f2fb20cca22f32be399b9e4ad77d32541f798cd0"), Nonce: types.EncodeNonce(0xf400cd0006070c49), diff --git a/consensus/ethash/consensus.go b/consensus/ethash/consensus.go index 62e3f8fca..fb9a396ae 100644 --- a/consensus/ethash/consensus.go +++ b/consensus/ethash/consensus.go @@ -63,7 +63,6 @@ var ( // codebase, inherently breaking if the engine is swapped out. Please put common // error types into the consensus package. var ( - errLargeBlockTime = errors.New("timestamp too big") errZeroBlockTime = errors.New("timestamp equals parent's") errTooManyUncles = errors.New("too many uncles") errDuplicateUncle = errors.New("duplicate uncle") @@ -242,20 +241,16 @@ func (ethash *Ethash) verifyHeader(chain consensus.ChainReader, header, parent * return fmt.Errorf("extra-data too long: %d > %d", len(header.Extra), params.MaximumExtraDataSize) } // Verify the header's timestamp - if uncle { - if header.Time.Cmp(math.MaxBig256) > 0 { - return errLargeBlockTime - } - } else { - if header.Time.Cmp(big.NewInt(time.Now().Add(allowedFutureBlockTime).Unix())) > 0 { + if !uncle { + if header.Time > uint64(time.Now().Add(allowedFutureBlockTime).Unix()) { return consensus.ErrFutureBlock } } - if header.Time.Cmp(parent.Time) <= 0 { + if header.Time <= parent.Time { return errZeroBlockTime } // Verify the block's difficulty based in it's timestamp and parent's difficulty - expected := ethash.CalcDifficulty(chain, header.Time.Uint64(), parent) + expected := ethash.CalcDifficulty(chain, header.Time, parent) if expected.Cmp(header.Difficulty) != 0 { return fmt.Errorf("invalid difficulty: have %v, want %v", header.Difficulty, expected) @@ -349,7 +344,7 @@ func makeDifficultyCalculator(bombDelay *big.Int) func(time uint64, parent *type // ) + 2^(periodCount - 2) bigTime := new(big.Int).SetUint64(time) - bigParentTime := new(big.Int).Set(parent.Time) + bigParentTime := new(big.Int).SetUint64(parent.Time) // holds intermediate values to make the algo easier to read & audit x := new(big.Int) @@ -408,7 +403,7 @@ func calcDifficultyHomestead(time uint64, parent *types.Header) *big.Int { // ) + 2^(periodCount - 2) bigTime := new(big.Int).SetUint64(time) - bigParentTime := new(big.Int).Set(parent.Time) + bigParentTime := new(big.Int).SetUint64(parent.Time) // holds intermediate values to make the algo easier to read & audit x := new(big.Int) @@ -456,7 +451,7 @@ func calcDifficultyFrontier(time uint64, parent *types.Header) *big.Int { bigParentTime := new(big.Int) bigTime.SetUint64(time) - bigParentTime.Set(parent.Time) + bigParentTime.SetUint64(parent.Time) if bigTime.Sub(bigTime, bigParentTime).Cmp(params.DurationLimit) < 0 { diff.Add(parent.Difficulty, adjust) @@ -558,7 +553,7 @@ func (ethash *Ethash) Prepare(chain consensus.ChainReader, header *types.Header) if parent == nil { return consensus.ErrUnknownAncestor } - header.Difficulty = ethash.CalcDifficulty(chain, header.Time.Uint64(), parent) + header.Difficulty = ethash.CalcDifficulty(chain, header.Time, parent) return nil } diff --git a/consensus/ethash/consensus_test.go b/consensus/ethash/consensus_test.go index 438a99dd6..675737d9e 100644 --- a/consensus/ethash/consensus_test.go +++ b/consensus/ethash/consensus_test.go @@ -76,7 +76,7 @@ func TestCalcDifficulty(t *testing.T) { number := new(big.Int).Sub(test.CurrentBlocknumber, big.NewInt(1)) diff := CalcDifficulty(config, test.CurrentTimestamp, &types.Header{ Number: number, - Time: new(big.Int).SetUint64(test.ParentTimestamp), + Time: test.ParentTimestamp, Difficulty: test.ParentDifficulty, }) if diff.Cmp(test.CurrentDifficulty) != 0 { -- cgit v1.2.3