Skip to content

Commit fd512fa

Browse files
fjlGustav Simonsson
authored andcommitted
Merge pull request #1711 from Gustav-Simonsson/timestamp_big_int
Add tests for uncle timestamps and refactor timestamp type (cherry picked from commit abce099)
1 parent dc3fb69 commit fd512fa

21 files changed

+380
-43
lines changed

cmd/evm/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ type VMEnv struct {
166166

167167
depth int
168168
Gas *big.Int
169-
time uint64
169+
time *big.Int
170170
logs []vm.StructLog
171171
}
172172

@@ -175,15 +175,15 @@ func NewEnv(state *state.StateDB, transactor common.Address, value *big.Int) *VM
175175
state: state,
176176
transactor: &transactor,
177177
value: value,
178-
time: uint64(time.Now().Unix()),
178+
time: big.NewInt(time.Now().Unix()),
179179
}
180180
}
181181

182182
func (self *VMEnv) State() *state.StateDB { return self.state }
183183
func (self *VMEnv) Origin() common.Address { return *self.transactor }
184184
func (self *VMEnv) BlockNumber() *big.Int { return common.Big0 }
185185
func (self *VMEnv) Coinbase() common.Address { return *self.transactor }
186-
func (self *VMEnv) Time() uint64 { return self.time }
186+
func (self *VMEnv) Time() *big.Int { return self.time }
187187
func (self *VMEnv) Difficulty() *big.Int { return common.Big1 }
188188
func (self *VMEnv) BlockHash() []byte { return make([]byte, 32) }
189189
func (self *VMEnv) Value() *big.Int { return self.value }

core/block_processor.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
203203
txs := block.Transactions()
204204

205205
// Block validation
206-
if err = ValidateHeader(sm.Pow, header, parent, false); err != nil {
206+
if err = ValidateHeader(sm.Pow, header, parent, false, false); err != nil {
207207
return
208208
}
209209

@@ -327,7 +327,7 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
327327
return UncleError("uncle[%d](%x)'s parent is not ancestor (%x)", i, hash[:4], uncle.ParentHash[0:4])
328328
}
329329

330-
if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash], true); err != nil {
330+
if err := ValidateHeader(sm.Pow, uncle, ancestors[uncle.ParentHash], true, true); err != nil {
331331
return ValidationError(fmt.Sprintf("uncle[%d](%x) header invalid: %v", i, hash[:4], err))
332332
}
333333
}
@@ -358,19 +358,25 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
358358

359359
// See YP section 4.3.4. "Block Header Validity"
360360
// Validates a block. Returns an error if the block is invalid.
361-
func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow bool) error {
361+
func ValidateHeader(pow pow.PoW, block *types.Header, parent *types.Block, checkPow, uncle bool) error {
362362
if big.NewInt(int64(len(block.Extra))).Cmp(params.MaximumExtraDataSize) == 1 {
363363
return fmt.Errorf("Block extra data too long (%d)", len(block.Extra))
364364
}
365365

366-
if block.Time > uint64(time.Now().Unix()) {
367-
return BlockFutureErr
366+
if uncle {
367+
if block.Time.Cmp(common.MaxBig) == 1 {
368+
return BlockTSTooBigErr
369+
}
370+
} else {
371+
if block.Time.Cmp(big.NewInt(time.Now().Unix())) == 1 {
372+
return BlockFutureErr
373+
}
368374
}
369-
if block.Time <= parent.Time() {
375+
if block.Time.Cmp(parent.Time()) != 1 {
370376
return BlockEqualTSErr
371377
}
372378

373-
expd := CalcDifficulty(block.Time, parent.Time(), parent.Number(), parent.Difficulty())
379+
expd := CalcDifficulty(block.Time.Uint64(), parent.Time().Uint64(), parent.Number(), parent.Difficulty())
374380
if expd.Cmp(block.Difficulty) != 0 {
375381
return fmt.Errorf("Difficulty check failed for block %v, %v", block.Difficulty, expd)
376382
}

core/block_processor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestNumber(t *testing.T) {
4848
statedb := state.New(chain.Genesis().Root(), chain.chainDb)
4949
header := makeHeader(chain.Genesis(), statedb)
5050
header.Number = big.NewInt(3)
51-
err := ValidateHeader(pow, header, chain.Genesis(), false)
51+
err := ValidateHeader(pow, header, chain.Genesis(), false, false)
5252
if err != BlockNumberErr {
5353
t.Errorf("expected block number error, got %q", err)
5454
}
5555

5656
header = makeHeader(chain.Genesis(), statedb)
57-
err = ValidateHeader(pow, header, chain.Genesis(), false)
57+
err = ValidateHeader(pow, header, chain.Genesis(), false, false)
5858
if err == BlockNumberErr {
5959
t.Errorf("didn't expect block number error")
6060
}

core/chain_makers.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,21 @@ func GenerateChain(parent *types.Block, db common.Database, n int, gen func(int,
166166
}
167167

168168
func makeHeader(parent *types.Block, state *state.StateDB) *types.Header {
169-
time := parent.Time() + 10 // block time is fixed at 10 seconds
169+
var time *big.Int
170+
if parent.Time() == nil {
171+
time = big.NewInt(10)
172+
} else {
173+
time = new(big.Int).Add(parent.Time(), big.NewInt(10)) // block time is fixed at 10 seconds
174+
}
170175
return &types.Header{
171176
Root: state.Root(),
172177
ParentHash: parent.Hash(),
173178
Coinbase: parent.Coinbase(),
174-
Difficulty: CalcDifficulty(time, parent.Time(), parent.Number(), parent.Difficulty()),
179+
Difficulty: CalcDifficulty(time.Uint64(), new(big.Int).Sub(time, big.NewInt(10)).Uint64(), parent.Number(), parent.Difficulty()),
175180
GasLimit: CalcGasLimit(parent),
176181
GasUsed: new(big.Int),
177182
Number: new(big.Int).Add(parent.Number(), common.Big1),
178-
Time: uint64(time),
183+
Time: time,
179184
}
180185
}
181186

core/chain_manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) {
596596
// Allow up to MaxFuture second in the future blocks. If this limit
597597
// is exceeded the chain is discarded and processed at a later time
598598
// if given.
599-
if max := uint64(time.Now().Unix()) + maxTimeFutureBlocks; block.Time() > max {
599+
max := big.NewInt(time.Now().Unix() + maxTimeFutureBlocks)
600+
if block.Time().Cmp(max) == 1 {
600601
return i, fmt.Errorf("%v: BlockFutureErr, %v > %v", BlockFutureErr, block.Time(), max)
601602
}
602603

core/error.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
)
2626

2727
var (
28-
BlockNumberErr = errors.New("block number invalid")
29-
BlockFutureErr = errors.New("block time is in the future")
30-
BlockEqualTSErr = errors.New("block time stamp equal to previous")
28+
BlockNumberErr = errors.New("block number invalid")
29+
BlockFutureErr = errors.New("block time is in the future")
30+
BlockTSTooBigErr = errors.New("block time too big")
31+
BlockEqualTSErr = errors.New("block time stamp equal to previous")
3132
)
3233

3334
// Parent error. In case a parent is unknown this error will be thrown

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func WriteGenesisBlock(chainDb common.Database, reader io.Reader) (*types.Block,
7373
difficulty := common.String2Big(genesis.Difficulty)
7474
block := types.NewBlock(&types.Header{
7575
Nonce: types.EncodeNonce(common.String2Big(genesis.Nonce).Uint64()),
76-
Time: common.String2Big(genesis.Timestamp).Uint64(),
76+
Time: common.String2Big(genesis.Timestamp),
7777
ParentHash: common.HexToHash(genesis.ParentHash),
7878
Extra: common.FromHex(genesis.ExtraData),
7979
GasLimit: common.String2Big(genesis.GasLimit),

core/types/block.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Header struct {
6060
Number *big.Int // The block number
6161
GasLimit *big.Int // Gas limit
6262
GasUsed *big.Int // Gas used
63-
Time uint64 // Creation time
63+
Time *big.Int // Creation time
6464
Extra []byte // Extra data
6565
MixDigest common.Hash // for quick difficulty verification
6666
Nonce BlockNonce
@@ -94,7 +94,7 @@ func (h *Header) UnmarshalJSON(data []byte) error {
9494
Coinbase string
9595
Difficulty string
9696
GasLimit string
97-
Time uint64
97+
Time *big.Int
9898
Extra string
9999
}
100100
dec := json.NewDecoder(bytes.NewReader(data))
@@ -210,6 +210,9 @@ func NewBlockWithHeader(header *Header) *Block {
210210

211211
func copyHeader(h *Header) *Header {
212212
cpy := *h
213+
if cpy.Time = new(big.Int); h.Time != nil {
214+
cpy.Time.Set(h.Time)
215+
}
213216
if cpy.Difficulty = new(big.Int); h.Difficulty != nil {
214217
cpy.Difficulty.Set(h.Difficulty)
215218
}
@@ -301,13 +304,13 @@ func (b *Block) Number() *big.Int { return new(big.Int).Set(b.header.Number)
301304
func (b *Block) GasLimit() *big.Int { return new(big.Int).Set(b.header.GasLimit) }
302305
func (b *Block) GasUsed() *big.Int { return new(big.Int).Set(b.header.GasUsed) }
303306
func (b *Block) Difficulty() *big.Int { return new(big.Int).Set(b.header.Difficulty) }
307+
func (b *Block) Time() *big.Int { return new(big.Int).Set(b.header.Time) }
304308

305309
func (b *Block) NumberU64() uint64 { return b.header.Number.Uint64() }
306310
func (b *Block) MixDigest() common.Hash { return b.header.MixDigest }
307311
func (b *Block) Nonce() uint64 { return binary.BigEndian.Uint64(b.header.Nonce[:]) }
308312
func (b *Block) Bloom() Bloom { return b.header.Bloom }
309313
func (b *Block) Coinbase() common.Address { return b.header.Coinbase }
310-
func (b *Block) Time() uint64 { return b.header.Time }
311314
func (b *Block) Root() common.Hash { return b.header.Root }
312315
func (b *Block) ParentHash() common.Hash { return b.header.ParentHash }
313316
func (b *Block) TxHash() common.Hash { return b.header.TxHash }

core/types/block_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestBlockEncoding(t *testing.T) {
4747
check("Root", block.Root(), common.HexToHash("ef1552a40b7165c3cd773806b9e0c165b75356e0314bf0706f279c729f51e017"))
4848
check("Hash", block.Hash(), common.HexToHash("0a5843ac1cb04865017cb35a57b50b07084e5fcee39b5acadade33149f4fff9e"))
4949
check("Nonce", block.Nonce(), uint64(0xa13a5a8c8f2bb1c4))
50-
check("Time", block.Time(), uint64(1426516743))
50+
check("Time", block.Time(), big.NewInt(1426516743))
5151
check("Size", block.Size(), common.StorageSize(len(blockEnc)))
5252

5353
tx1 := NewTransaction(0, common.HexToAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), big.NewInt(10), big.NewInt(50000), big.NewInt(10), nil)

core/vm/environment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Environment interface {
3333
BlockNumber() *big.Int
3434
GetHash(n uint64) common.Hash
3535
Coinbase() common.Address
36-
Time() uint64
36+
Time() *big.Int
3737
Difficulty() *big.Int
3838
GasLimit() *big.Int
3939
CanTransfer(from Account, balance *big.Int) bool

0 commit comments

Comments
 (0)