|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/ethereum/go-ethereum/consensus/beacon" |
| 10 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/ethereum/go-ethereum/core/vm" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/ethereum/go-ethereum/eth/tracers/logger" |
| 15 | + "github.com/ethereum/go-ethereum/params" |
| 16 | + "github.com/holiman/uint256" |
| 17 | +) |
| 18 | + |
| 19 | +func TestEIP7702(t *testing.T) { |
| 20 | + var ( |
| 21 | + aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa") |
| 22 | + bb = common.HexToAddress("0x000000000000000000000000000000000000bbbb") |
| 23 | + engine = beacon.NewFaker() |
| 24 | + |
| 25 | + // A sender who makes transactions, has some funds |
| 26 | + key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 27 | + key2, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") |
| 28 | + addr1 = crypto.PubkeyToAddress(key1.PublicKey) |
| 29 | + addr2 = crypto.PubkeyToAddress(key2.PublicKey) |
| 30 | + funds = new(big.Int).Mul(common.Big1, big.NewInt(params.Ether)) |
| 31 | + config = *params.AllEthashProtocolChanges |
| 32 | + gspec = &Genesis{ |
| 33 | + Config: &config, |
| 34 | + Alloc: types.GenesisAlloc{ |
| 35 | + addr1: {Balance: funds}, |
| 36 | + addr2: {Balance: funds}, |
| 37 | + // The address 0xAAAA sstores 1 into slot 2. |
| 38 | + aa: { |
| 39 | + Code: []byte{ |
| 40 | + byte(vm.PC), // [0] |
| 41 | + byte(vm.DUP1), // [0,0] |
| 42 | + byte(vm.DUP1), // [0,0,0] |
| 43 | + byte(vm.DUP1), // [0,0,0,0] |
| 44 | + byte(vm.PUSH1), 0x01, // [0,0,0,0,1] (value) |
| 45 | + byte(vm.PUSH20), addr2[0], addr2[1], addr2[2], addr2[3], addr2[4], addr2[5], addr2[6], addr2[7], addr2[8], addr2[9], addr2[10], addr2[11], addr2[12], addr2[13], addr2[14], addr2[15], addr2[16], addr2[17], addr2[18], addr2[19], |
| 46 | + byte(vm.GAS), |
| 47 | + byte(vm.CALL), |
| 48 | + byte(vm.STOP), |
| 49 | + }, |
| 50 | + Nonce: 0, |
| 51 | + Balance: big.NewInt(0), |
| 52 | + }, |
| 53 | + // The address 0xBBBB sstores 42 into slot 42. |
| 54 | + bb: { |
| 55 | + Code: []byte{ |
| 56 | + byte(vm.PUSH1), 0x42, |
| 57 | + byte(vm.DUP1), |
| 58 | + byte(vm.SSTORE), |
| 59 | + byte(vm.STOP), |
| 60 | + }, |
| 61 | + Nonce: 0, |
| 62 | + Balance: big.NewInt(0), |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + gspec.Config.BerlinBlock = common.Big0 |
| 69 | + gspec.Config.LondonBlock = common.Big0 |
| 70 | + gspec.Config.TerminalTotalDifficulty = common.Big0 |
| 71 | + gspec.Config.TerminalTotalDifficultyPassed = true |
| 72 | + gspec.Config.ShanghaiTime = u64(0) |
| 73 | + gspec.Config.CancunTime = u64(0) |
| 74 | + gspec.Config.PragueTime = u64(0) |
| 75 | + signer := types.LatestSigner(gspec.Config) |
| 76 | + |
| 77 | + _, blocks, _ := GenerateChainWithGenesis(gspec, engine, 1, func(i int, b *BlockGen) { |
| 78 | + b.SetCoinbase(aa) |
| 79 | + // One transaction to Coinbase |
| 80 | + |
| 81 | + auth1, _ := types.SignAuth(&types.Authorization{ |
| 82 | + ChainID: new(big.Int).Set(gspec.Config.ChainID), |
| 83 | + Address: aa, |
| 84 | + Nonce: nil, |
| 85 | + }, key1) |
| 86 | + |
| 87 | + auth2, _ := types.SignAuth(&types.Authorization{ |
| 88 | + ChainID: new(big.Int).Set(gspec.Config.ChainID), |
| 89 | + Address: bb, |
| 90 | + Nonce: []uint64{0}, |
| 91 | + }, key2) |
| 92 | + |
| 93 | + txdata := &types.SetCodeTx{ |
| 94 | + ChainID: uint256.MustFromBig(gspec.Config.ChainID), |
| 95 | + Nonce: 0, |
| 96 | + To: &addr1, |
| 97 | + Gas: 500000, |
| 98 | + GasFeeCap: uint256.MustFromBig(newGwei(5)), |
| 99 | + GasTipCap: uint256.NewInt(2), |
| 100 | + AuthList: []*types.Authorization{auth1, auth2}, |
| 101 | + } |
| 102 | + tx := types.NewTx(txdata) |
| 103 | + tx, err := types.SignTx(tx, signer, key1) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("%s", err) |
| 106 | + } |
| 107 | + b.AddTx(tx) |
| 108 | + }) |
| 109 | + chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), nil, gspec, nil, engine, vm.Config{Tracer: logger.NewMarkdownLogger(&logger.Config{}, os.Stderr).Hooks()}, nil, nil) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("failed to create tester chain: %v", err) |
| 112 | + } |
| 113 | + defer chain.Stop() |
| 114 | + if n, err := chain.InsertChain(blocks); err != nil { |
| 115 | + t.Fatalf("block %d: failed to insert into chain: %v", n, err) |
| 116 | + } |
| 117 | + |
| 118 | + var ( |
| 119 | + state, _ = chain.State() |
| 120 | + fortyTwo = common.BytesToHash([]byte{0x42}) |
| 121 | + actual = state.GetState(addr2, fortyTwo) |
| 122 | + ) |
| 123 | + if actual.Cmp(fortyTwo) != 0 { |
| 124 | + t.Fatalf("addr2 storage wrong: expected %d, got %d", fortyTwo, actual) |
| 125 | + } |
| 126 | + if code := state.GetCode(addr1); code != nil { |
| 127 | + t.Fatalf("addr1 code not cleared: got %s", common.Bytes2Hex(code)) |
| 128 | + } |
| 129 | + if code := state.GetCode(addr2); code != nil { |
| 130 | + t.Fatalf("addr2 code not cleared: got %s", common.Bytes2Hex(code)) |
| 131 | + } |
| 132 | +} |
0 commit comments