Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions internal/ethapi/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ func (sim *simulator) activePrecompiles(base *types.Header) vm.PrecompiledContra
return vm.ActivePrecompiledContracts(rules)
}

// timestampIncrementForChain returns zero for Arbitrum because its 250ms block
// time is below the one-second precision of block timestamps.
func (sim *simulator) timestampIncrementForChain() uint64 {
if sim.chainConfig != nil && sim.chainConfig.IsArbitrum() {
return 0
}
return timestampIncrement
}

// sanitizeChain checks the chain integrity. Specifically it checks that
// block numbers and timestamp are strictly increasing, setting default values
// when necessary. Gaps in block numbers are filled with empty blocks.
Expand Down Expand Up @@ -430,7 +439,7 @@ func (sim *simulator) sanitizeChain(blocks []simBlock) ([]simBlock, error) {
// Assign block number to the empty blocks.
for i := uint64(0); i < gap.Uint64(); i++ {
n := new(big.Int).Add(prevNumber, big.NewInt(int64(i+1)))
t := prevTimestamp + timestampIncrement
t := prevTimestamp + sim.timestampIncrementForChain()
b := simBlock{
BlockOverrides: &override.BlockOverrides{
Number: (*hexutil.Big)(n),
Expand All @@ -446,7 +455,7 @@ func (sim *simulator) sanitizeChain(blocks []simBlock) ([]simBlock, error) {
prevNumber = block.BlockOverrides.Number.ToInt()
var t uint64
if block.BlockOverrides.Time == nil {
t = prevTimestamp + timestampIncrement
t = prevTimestamp + sim.timestampIncrementForChain()
block.BlockOverrides.Time = (*hexutil.Uint64)(&t)
} else {
t = uint64(*block.BlockOverrides.Time)
Expand Down
27 changes: 26 additions & 1 deletion internal/ethapi/simulate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/internal/ethapi/override"
"github.com/ethereum/go-ethereum/params"
)

func TestSimulateSanitizeBlockOrder(t *testing.T) {
arbConfig := *params.TestChainConfig
arbConfig.ArbitrumChainParams.EnableArbOS = true

type result struct {
number uint64
timestamp uint64
}
for i, tc := range []struct {
baseNumber int
baseTimestamp uint64
chainConfig *params.ChainConfig
blocks []simBlock
expected []result
err string
Expand Down Expand Up @@ -79,8 +84,28 @@ func TestSimulateSanitizeBlockOrder(t *testing.T) {
blocks: []simBlock{{BlockOverrides: &override.BlockOverrides{Number: newInt(11), Time: newUint64(60)}}, {BlockOverrides: &override.BlockOverrides{Number: newInt(13), Time: newUint64(72)}}},
expected: []result{{number: 11, timestamp: 60}, {number: 12, timestamp: 72}, {number: 13, timestamp: 72}},
},
{
baseNumber: 10,
baseTimestamp: 50,
chainConfig: &arbConfig,
blocks: []simBlock{
{},
{BlockOverrides: &override.BlockOverrides{Number: newInt(14), Time: newUint64(75)}},
{},
},
expected: []result{
{number: 11, timestamp: 50},
{number: 12, timestamp: 50},
{number: 13, timestamp: 50},
{number: 14, timestamp: 75},
{number: 15, timestamp: 75},
},
},
} {
sim := &simulator{base: &types.Header{Number: big.NewInt(int64(tc.baseNumber)), Time: tc.baseTimestamp}}
sim := &simulator{
base: &types.Header{Number: big.NewInt(int64(tc.baseNumber)), Time: tc.baseTimestamp},
chainConfig: tc.chainConfig,
}
res, err := sim.sanitizeChain(tc.blocks)
if err != nil {
if err.Error() == tc.err {
Expand Down