@@ -1609,3 +1609,115 @@ func TestTransientStorageReset(t *testing.T) {
16091609 t .Fatalf ("Unexpected dirty storage slot" )
16101610 }
16111611}
1612+
1613+ func TestEIP3651 (t * testing.T ) {
1614+ var (
1615+ ConstantinopleBlockReward = big .NewInt (3e+18 ) // Block reward in wei for successfully mining a block upward from Constantinople
1616+
1617+ aa = common .HexToAddress ("0x000000000000000000000000000000000000aaaa" )
1618+ bb = common .HexToAddress ("0x000000000000000000000000000000000000bbbb" )
1619+ engine = ethash .NewFaker ()
1620+
1621+ // A sender who makes transactions, has some funds
1622+ key1 , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
1623+ key2 , _ = crypto .HexToECDSA ("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
1624+ addr1 = crypto .PubkeyToAddress (key1 .PublicKey )
1625+ addr2 = crypto .PubkeyToAddress (key2 .PublicKey )
1626+ funds = big .NewInt (8000000000000000 )
1627+ gspec = & Genesis {
1628+ Config : params .TestChainConfig ,
1629+ Alloc : GenesisAlloc {
1630+ addr1 : {Balance : funds },
1631+ addr2 : {Balance : funds },
1632+ // The address 0xAAAA sloads 0x00 and 0x01
1633+ aa : {
1634+ Code : []byte {
1635+ byte (vm .PC ),
1636+ byte (vm .PC ),
1637+ byte (vm .SLOAD ),
1638+ byte (vm .SLOAD ),
1639+ },
1640+ Nonce : 0 ,
1641+ Balance : big .NewInt (0 ),
1642+ },
1643+ // The address 0xBBBB calls 0xAAAA
1644+ bb : {
1645+ Code : []byte {
1646+ byte (vm .PUSH1 ), 0 , // out size
1647+ byte (vm .DUP1 ), // out offset
1648+ byte (vm .DUP1 ), // out insize
1649+ byte (vm .DUP1 ), // in offset
1650+ byte (vm .PUSH2 ), // address
1651+ byte (0xaa ),
1652+ byte (0xaa ),
1653+ byte (vm .GAS ), // gas
1654+ byte (vm .DELEGATECALL ),
1655+ },
1656+ Nonce : 0 ,
1657+ Balance : big .NewInt (0 ),
1658+ },
1659+ },
1660+ }
1661+ )
1662+
1663+ gspec .Config .Eip1559Block = common .Big0
1664+ signer := types .LatestSigner (gspec .Config )
1665+
1666+ _ , blocks , _ := GenerateChainWithGenesis (gspec , engine , 1 , func (i int , b * BlockGen ) {
1667+ b .SetCoinbase (aa )
1668+ // One transaction to Coinbase
1669+ txdata := & types.DynamicFeeTx {
1670+ ChainID : gspec .Config .ChainId ,
1671+ Nonce : 0 ,
1672+ To : & bb ,
1673+ Gas : 500000 ,
1674+ GasFeeCap : big .NewInt (12500000000 ),
1675+ GasTipCap : big .NewInt (0 ),
1676+ AccessList : nil ,
1677+ Data : []byte {},
1678+ }
1679+ tx := types .NewTx (txdata )
1680+ tx , _ = types .SignTx (tx , signer , key1 )
1681+
1682+ b .AddTx (tx )
1683+ })
1684+
1685+ diskdb := rawdb .NewMemoryDatabase ()
1686+ gspec .MustCommit (diskdb )
1687+
1688+ chain , err := NewBlockChain (diskdb , nil , gspec .Config , engine , vm.Config {})
1689+ if err != nil {
1690+ t .Fatalf ("failed to create tester chain: %v" , err )
1691+ }
1692+ if n , err := chain .InsertChain (blocks ); err != nil {
1693+ t .Fatalf ("block %d: failed to insert into chain: %v" , n , err )
1694+ }
1695+
1696+ block := chain .GetBlockByNumber (1 )
1697+
1698+ // 1+2: Ensure EIP-1559 access lists are accounted for via gas usage.
1699+ innerGas := vm .GasQuickStep * 2 + params .ColdSloadCostEIP2929 * 2
1700+ expectedGas := params .TxGas + 5 * vm .GasFastestStep + vm .GasQuickStep + 100 + innerGas // 100 because 0xaaaa is in access list
1701+ if block .GasUsed () != expectedGas {
1702+ t .Fatalf ("incorrect amount of gas spent: expected %d, got %d" , expectedGas , block .GasUsed ())
1703+ }
1704+
1705+ state , _ := chain .State ()
1706+
1707+ // 3: Ensure that miner received only the tx's tip.
1708+ actual := state .GetBalance (block .Coinbase ())
1709+ expected := new (big.Int ).Add (
1710+ new (big.Int ).SetUint64 (block .GasUsed ()* block .Transactions ()[0 ].GasTipCap ().Uint64 ()),
1711+ ConstantinopleBlockReward ,
1712+ )
1713+ if actual .Cmp (expected ) != 0 {
1714+ t .Fatalf ("miner balance incorrect: expected %d, got %d" , expected , actual )
1715+ }
1716+
1717+ // 4: Ensure the tx sender paid for the gasUsed * (tip + block baseFee).
1718+ actual = new (big.Int ).Sub (funds , state .GetBalance (addr1 ))
1719+ expected = new (big.Int ).SetUint64 (block .GasUsed () * (block .Transactions ()[0 ].GasTipCap ().Uint64 () + block .BaseFee ().Uint64 ()))
1720+ if actual .Cmp (expected ) != 0 {
1721+ t .Fatalf ("sender balance incorrect: expected %d, got %d" , expected , actual )
1722+ }
1723+ }
0 commit comments