1717package core
1818
1919import (
20+ "bytes"
2021 "errors"
2122 "fmt"
2223 "io/ioutil"
@@ -27,6 +28,7 @@ import (
2728 "testing"
2829 "time"
2930
31+ "github.com/holiman/uint256"
3032 "github.com/stretchr/testify/assert"
3133
3234 "github.com/scroll-tech/go-ethereum/common"
@@ -3789,3 +3791,94 @@ func TestCurieTransition(t *testing.T) {
37893791 }
37903792 }
37913793}
3794+
3795+ // TestEIP7702 deploys two delegation designations and calls them. It writes one
3796+ // value to storage which is verified after.
3797+ func TestEIP7702 (t * testing.T ) {
3798+ var (
3799+ config = * params .TestChainConfig
3800+ signer = types .LatestSigner (& config )
3801+ key1 , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
3802+ key2 , _ = crypto .HexToECDSA ("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
3803+ addr1 = crypto .PubkeyToAddress (key1 .PublicKey )
3804+ addr2 = crypto .PubkeyToAddress (key2 .PublicKey )
3805+ aa = common .HexToAddress ("0x000000000000000000000000000000000000aaaa" )
3806+ bb = common .HexToAddress ("0x000000000000000000000000000000000000bbbb" )
3807+ funds = new (big.Int ).Mul (common .Big1 , big .NewInt (params .Ether ))
3808+ )
3809+
3810+ gspec := & Genesis {
3811+ Config : & config ,
3812+ Alloc : GenesisAlloc {
3813+ addr1 : {Balance : funds },
3814+ addr2 : {Balance : funds },
3815+ aa : { // The address 0xAAAA calls into addr2
3816+ Code : common .Hex2Bytes ("6000600060006000600173703c4b2bd70c169f5717101caee543299fc946c75af1" ),
3817+ Nonce : 0 ,
3818+ Balance : big .NewInt (0 ),
3819+ },
3820+ bb : { // The address 0xBBBB sstores 42 into slot 42.
3821+ Code : common .Hex2Bytes ("6042604255" ),
3822+ Nonce : 0 ,
3823+ Balance : big .NewInt (0 ),
3824+ },
3825+ },
3826+ }
3827+
3828+ // Sign authorization tuples.
3829+ // The way the auths are combined, it becomes
3830+ // 1. tx -> addr1 which is delegated to 0xaaaa
3831+ // 2. addr1:0xaaaa calls into addr2:0xbbbb
3832+ // 3. addr2:0xbbbb writes to storage
3833+ auth1 , _ := types .SignSetCode (key1 , types.SetCodeAuthorization {
3834+ ChainID : * uint256 .MustFromBig (gspec .Config .ChainID ),
3835+ Address : aa ,
3836+ Nonce : 1 ,
3837+ })
3838+ auth2 , _ := types .SignSetCode (key2 , types.SetCodeAuthorization {
3839+ Address : bb ,
3840+ Nonce : 0 ,
3841+ })
3842+
3843+ db := rawdb .NewMemoryDatabase ()
3844+ genesis := gspec .MustCommit (db )
3845+ blocks , _ := GenerateChain (gspec .Config , genesis , ethash .NewFaker (), db , 1 , func (i int , b * BlockGen ) {
3846+ b .SetCoinbase (aa )
3847+ txdata := & types.SetCodeTx {
3848+ ChainID : uint256 .MustFromBig (gspec .Config .ChainID ),
3849+ Nonce : 0 ,
3850+ To : addr1 ,
3851+ Gas : 500000 ,
3852+ GasFeeCap : uint256 .MustFromBig (newGwei (5 )),
3853+ GasTipCap : uint256 .NewInt (2 ),
3854+ SetCodeAuthorizations : []types.SetCodeAuthorization {auth1 , auth2 },
3855+ }
3856+ tx := types .MustSignNewTx (key1 , signer , txdata )
3857+ b .AddTx (tx )
3858+ })
3859+ chain , _ := NewBlockChain (db , nil , gspec .Config , ethash .NewFaker (), vm.Config {}, nil , nil )
3860+ defer chain .Stop ()
3861+
3862+ if n , err := chain .InsertChain (blocks ); err != nil {
3863+ t .Fatalf ("block %d: failed to insert into chain: %v" , n , err )
3864+ }
3865+
3866+ // Verify delegation designations were deployed.
3867+ state , _ := chain .State ()
3868+ code , want := state .GetCode (addr1 ), types .AddressToDelegation (auth1 .Address )
3869+ if ! bytes .Equal (code , want ) {
3870+ t .Fatalf ("addr1 code incorrect: got %s, want %s" , common .Bytes2Hex (code ), common .Bytes2Hex (want ))
3871+ }
3872+ code , want = state .GetCode (addr2 ), types .AddressToDelegation (auth2 .Address )
3873+ if ! bytes .Equal (code , want ) {
3874+ t .Fatalf ("addr2 code incorrect: got %s, want %s" , common .Bytes2Hex (code ), common .Bytes2Hex (want ))
3875+ }
3876+ // Verify delegation executed the correct code.
3877+ var (
3878+ fortyTwo = common .BytesToHash ([]byte {0x42 })
3879+ actual = state .GetState (addr2 , fortyTwo )
3880+ )
3881+ if ! bytes .Equal (actual [:], fortyTwo [:]) {
3882+ t .Fatalf ("addr2 storage wrong: expected %d, got %d" , fortyTwo , actual )
3883+ }
3884+ }
0 commit comments