@@ -3238,3 +3238,111 @@ func TestTransactionCountLimit(t *testing.T) {
32383238 t .Fatalf ("error mismatch: have: %v, want: %v" , err , consensus .ErrInvalidTxCount )
32393239 }
32403240}
3241+
3242+ func TestEIP3651 (t * testing.T ) {
3243+ var (
3244+ aa = common .HexToAddress ("0x000000000000000000000000000000000000aaaa" )
3245+ bb = common .HexToAddress ("0x000000000000000000000000000000000000bbbb" )
3246+ engine = ethash .NewFaker ()
3247+
3248+ // A sender who makes transactions, has some funds
3249+ key1 , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
3250+ key2 , _ = crypto .HexToECDSA ("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
3251+ addr1 = crypto .PubkeyToAddress (key1 .PublicKey )
3252+ addr2 = crypto .PubkeyToAddress (key2 .PublicKey )
3253+ funds = new (big.Int ).Mul (common .Big1 , big .NewInt (params .Ether ))
3254+ gspec = & Genesis {
3255+ Config : params .AllEthashProtocolChanges ,
3256+ Alloc : GenesisAlloc {
3257+ addr1 : {Balance : funds },
3258+ addr2 : {Balance : funds },
3259+ // The address 0xAAAA sloads 0x00 and 0x01
3260+ aa : {
3261+ Code : []byte {
3262+ byte (vm .PC ),
3263+ byte (vm .PC ),
3264+ byte (vm .SLOAD ),
3265+ byte (vm .SLOAD ),
3266+ },
3267+ Nonce : 0 ,
3268+ Balance : big .NewInt (0 ),
3269+ },
3270+ // The address 0xBBBB calls 0xAAAA
3271+ bb : {
3272+ Code : []byte {
3273+ byte (vm .PUSH1 ), 0 , // out size
3274+ byte (vm .DUP1 ), // out offset
3275+ byte (vm .DUP1 ), // out insize
3276+ byte (vm .DUP1 ), // in offset
3277+ byte (vm .PUSH2 ), // address
3278+ byte (0xaa ),
3279+ byte (0xaa ),
3280+ byte (vm .GAS ), // gas
3281+ byte (vm .DELEGATECALL ),
3282+ },
3283+ Nonce : 0 ,
3284+ Balance : big .NewInt (0 ),
3285+ },
3286+ },
3287+ }
3288+ )
3289+
3290+ gspec .Config .BerlinBlock = common .Big0
3291+ gspec .Config .LondonBlock = common .Big0
3292+ gspec .Config .ShanghaiBlock = common .Big0
3293+ signer := types .LatestSigner (gspec .Config )
3294+
3295+ _ , blocks , _ := GenerateChainWithGenesis (gspec , engine , 1 , func (i int , b * BlockGen ) {
3296+ b .SetCoinbase (aa )
3297+ // One transaction to Coinbase
3298+ txdata := & types.DynamicFeeTx {
3299+ ChainID : gspec .Config .ChainID ,
3300+ Nonce : 0 ,
3301+ To : & bb ,
3302+ Gas : 500000 ,
3303+ GasFeeCap : newGwei (5 ),
3304+ GasTipCap : big .NewInt (2 ),
3305+ AccessList : nil ,
3306+ Data : []byte {},
3307+ }
3308+ tx := types .NewTx (txdata )
3309+ tx , _ = types .SignTx (tx , signer , key1 )
3310+
3311+ b .AddTx (tx )
3312+ })
3313+ chain , err := NewBlockChain (rawdb .NewMemoryDatabase (), nil , gspec , nil , engine , vm.Config {Tracer : logger .NewMarkdownLogger (& logger.Config {}, os .Stderr )}, nil , nil )
3314+ if err != nil {
3315+ t .Fatalf ("failed to create tester chain: %v" , err )
3316+ }
3317+ if n , err := chain .InsertChain (blocks ); err != nil {
3318+ t .Fatalf ("block %d: failed to insert into chain: %v" , n , err )
3319+ }
3320+
3321+ block := chain .GetBlockByNumber (1 )
3322+
3323+ // 1+2: Ensure EIP-1559 access lists are accounted for via gas usage.
3324+ innerGas := vm .GasQuickStep * 2 + params .ColdSloadCostEIP2929 * 2
3325+ expectedGas := params .TxGas + 5 * vm .GasFastestStep + vm .GasQuickStep + 100 + innerGas // 100 because 0xaaaa is in access list
3326+ if block .GasUsed () != expectedGas {
3327+ t .Fatalf ("incorrect amount of gas spent: expected %d, got %d" , expectedGas , block .GasUsed ())
3328+ }
3329+
3330+ state , _ := chain .State ()
3331+
3332+ // 3: Ensure that miner received only the tx's tip.
3333+ actual := state .GetBalance (block .Coinbase ())
3334+ expected := new (big.Int ).Add (
3335+ new (big.Int ).SetUint64 (block .GasUsed ()* block .Transactions ()[0 ].GasTipCap ().Uint64 ()),
3336+ ethash .ConstantinopleBlockReward ,
3337+ )
3338+ if actual .Cmp (expected ) != 0 {
3339+ t .Fatalf ("miner balance incorrect: expected %d, got %d" , expected , actual )
3340+ }
3341+
3342+ // 4: Ensure the tx sender paid for the gasUsed * (tip + block baseFee).
3343+ actual = new (big.Int ).Sub (funds , state .GetBalance (addr1 ))
3344+ expected = new (big.Int ).SetUint64 (block .GasUsed () * (block .Transactions ()[0 ].GasTipCap ().Uint64 () + block .BaseFee ().Uint64 ()))
3345+ if actual .Cmp (expected ) != 0 {
3346+ t .Fatalf ("sender balance incorrect: expected %d, got %d" , expected , actual )
3347+ }
3348+ }
0 commit comments