aboutsummaryrefslogtreecommitdiffstats
path: root/consensus
diff options
context:
space:
mode:
authorMartin Holst Swende <martin@swende.se>2019-04-03 04:28:48 +0800
committerPéter Szilágyi <peterke@gmail.com>2019-04-03 04:28:48 +0800
commit0b4fe8d1929ad64ed576f7560dde4179d71ecfcb (patch)
tree923d7b795c59ddb0e5d338f2b2e6536e00a58c88 /consensus
parente14f8a408c17fd6c57d769cd4635ad6cc8bde769 (diff)
downloadgo-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.tar
go-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.tar.gz
go-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.tar.bz2
go-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.tar.lz
go-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.tar.xz
go-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.tar.zst
go-tangerine-0b4fe8d1929ad64ed576f7560dde4179d71ecfcb.zip
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
Diffstat (limited to 'consensus')
-rw-r--r--consensus/clique/clique.go12
-rw-r--r--consensus/ethash/algorithm_test.go2
-rw-r--r--consensus/ethash/consensus.go21
-rw-r--r--consensus/ethash/consensus_test.go2
4 files changed, 16 insertions, 21 deletions
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 {