From e423d5cf9ef22aa1f35c91ea126b71443a27972a Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 26 Jan 2024 15:45:19 +0100 Subject: [PATCH] add a test to verify the transition from legacy to contract --- core/vm/instructions_test.go | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 807073336d..5f481a7fbe 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -18,6 +18,7 @@ package vm import ( "bytes" + "encoding/binary" "encoding/json" "fmt" "math/big" @@ -791,6 +792,79 @@ func TestBlobHash(t *testing.T) { } } +func TestBlockHashEip2935(t *testing.T) { + type testcase struct { + name string + idx uint64 + expect common.Hash + hashes []common.Hash + } + var ( + statedb, _ = state.New(types.EmptyRootHash, state.NewDatabase(rawdb.NewMemoryDatabase()), nil) + expect = common.HexToHash("0x1234") + getHash = func(uint64) common.Hash { + return expect + } + newUint64 = func(val uint64) *uint64 { return &val } + blockContext = BlockContext{BlockNumber: big.NewInt(100), GetHash: getHash, Time: 0} + chainConfig = ¶ms.ChainConfig{ + ChainID: big.NewInt(1337), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + ArrowGlacierBlock: big.NewInt(0), + GrayGlacierBlock: big.NewInt(0), + ShanghaiTime: newUint64(0), + PragueTime: newUint64(0), + TerminalTotalDifficulty: big.NewInt(0), + TerminalTotalDifficultyPassed: true, + } + env = NewEVM(blockContext, TxContext{}, statedb, chainConfig, Config{}) + stack = newstack() + pc = uint64(0) + evmInterpreter = env.interpreter + callBlockHash10 = func(t *testing.T, name string) { + stack.push(uint256.NewInt(10)) + opBlockhash(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) + if len(stack.data) != 1 { + t.Errorf("Expected one item on stack got %d: ", len(stack.data)) + } + actual := stack.pop() + expected, overflow := uint256.FromBig(new(big.Int).SetBytes(expect.Bytes())) + if overflow { + t.Errorf("invalid overflow") + } + if actual.Cmp(expected) != 0 { + t.Errorf("%s: expected %x, got %x", name, expected, actual) + } + } + ) + + // insert 500 block hashes, starting from genesis. + for i := uint64(0); i < 500; i++ { + var blockNumber common.Hash + binary.BigEndian.PutUint64(blockNumber[24:], i) + statedb.SetState(params.HistoryStorageAddress, blockNumber, expect) + } + + // simulate a call to BLOCKHASH at block 100, i.e. less than 256 + // blocks after a genesis with eip 2935 activated. + callBlockHash10(t, "legacy") + + // now do the same thing much later, after all blocks have + // been inserted. + env.Context.BlockNumber.SetInt64(500) + callBlockHash10(t, "contract") +} + func TestOpMCopy(t *testing.T) { // Test cases from https://eips.ethereum.org/EIPS/eip-5656#test-cases for i, tc := range []struct {