@@ -2376,11 +2376,9 @@ func TestDeleteRecreateSlots(t *testing.T) {
23762376 engine = ethash .NewFaker ()
23772377 db = rawdb .NewMemoryDatabase ()
23782378 // A sender who makes transactions, has some funds
2379- key , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
2380- address = crypto .PubkeyToAddress (key .PublicKey )
2381- funds = big .NewInt (1000000000 )
2382-
2383- aa = common .HexToAddress ("0x7217d81b76bdd8707601e959454e3d776aee5f43" )
2379+ key , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
2380+ address = crypto .PubkeyToAddress (key .PublicKey )
2381+ funds = big .NewInt (1000000000 )
23842382 bb = common .HexToAddress ("0x000000000000000000000000000000000000bbbb" )
23852383 aaStorage = make (map [common.Hash ]common.Hash ) // Initial storage in AA
23862384 aaCode = []byte {byte (vm .PC ), byte (vm .SELFDESTRUCT )} // Code for AA (simple selfdestruct)
@@ -2403,7 +2401,7 @@ func TestDeleteRecreateSlots(t *testing.T) {
24032401 byte (vm .PUSH1 ), 0x4 , // location
24042402 byte (vm .SSTORE ), // Set slot[4] = 1
24052403 // Slots are set, now return the code
2406- byte (vm .PUSH2 ), 0x88 , 0xff , // Push code on stack
2404+ byte (vm .PUSH2 ), byte ( vm . PC ), byte ( vm . SELFDESTRUCT ) , // Push code on stack
24072405 byte (vm .PUSH1 ), 0x0 , // memory start on stack
24082406 byte (vm .MSTORE ),
24092407 // Code is now in memory.
@@ -2428,6 +2426,10 @@ func TestDeleteRecreateSlots(t *testing.T) {
24282426 byte (vm .CREATE2 ),
24292427 }... )
24302428
2429+ initHash := crypto .Keccak256Hash (initCode )
2430+ aa := crypto .CreateAddress2 (bb , [32 ]byte {}, initHash [:])
2431+ t .Logf ("Destination address: %x\n " , aa )
2432+
24312433 gspec := & Genesis {
24322434 Config : params .TestChainConfig ,
24332435 Alloc : GenesisAlloc {
@@ -2563,3 +2565,196 @@ func TestDeleteRecreateAccount(t *testing.T) {
25632565 t .Errorf ("got %x exp %x" , got , exp )
25642566 }
25652567}
2568+
2569+ // TestDeleteRecreateSlotsAcrossManyBlocks tests multiple state-transition that contains both deletion
2570+ // and recreation of contract state.
2571+ // Contract A exists, has slots 1 and 2 set
2572+ // Tx 1: Selfdestruct A
2573+ // Tx 2: Re-create A, set slots 3 and 4
2574+ // Expected outcome is that _all_ slots are cleared from A, due to the selfdestruct,
2575+ // and then the new slots exist
2576+ func TestDeleteRecreateSlotsAcrossManyBlocks (t * testing.T ) {
2577+ var (
2578+ // Generate a canonical chain to act as the main dataset
2579+ engine = ethash .NewFaker ()
2580+ db = rawdb .NewMemoryDatabase ()
2581+ // A sender who makes transactions, has some funds
2582+ key , _ = crypto .HexToECDSA ("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" )
2583+ address = crypto .PubkeyToAddress (key .PublicKey )
2584+ funds = big .NewInt (1000000000 )
2585+ bb = common .HexToAddress ("0x000000000000000000000000000000000000bbbb" )
2586+ aaStorage = make (map [common.Hash ]common.Hash ) // Initial storage in AA
2587+ aaCode = []byte {byte (vm .PC ), byte (vm .SELFDESTRUCT )} // Code for AA (simple selfdestruct)
2588+ )
2589+ // Populate two slots
2590+ aaStorage [common .HexToHash ("01" )] = common .HexToHash ("01" )
2591+ aaStorage [common .HexToHash ("02" )] = common .HexToHash ("02" )
2592+
2593+ // The bb-code needs to CREATE2 the aa contract. It consists of
2594+ // both initcode and deployment code
2595+ // initcode:
2596+ // 1. Set slots 3=blocknum+1, 4=4,
2597+ // 2. Return aaCode
2598+
2599+ initCode := []byte {
2600+ byte (vm .PUSH1 ), 0x1 , //
2601+ byte (vm .NUMBER ), // value = number + 1
2602+ byte (vm .ADD ), //
2603+ byte (vm .PUSH1 ), 0x3 , // location
2604+ byte (vm .SSTORE ), // Set slot[3] = number + 1
2605+ byte (vm .PUSH1 ), 0x4 , // value
2606+ byte (vm .PUSH1 ), 0x4 , // location
2607+ byte (vm .SSTORE ), // Set slot[4] = 4
2608+ // Slots are set, now return the code
2609+ byte (vm .PUSH2 ), byte (vm .PC ), byte (vm .SELFDESTRUCT ), // Push code on stack
2610+ byte (vm .PUSH1 ), 0x0 , // memory start on stack
2611+ byte (vm .MSTORE ),
2612+ // Code is now in memory.
2613+ byte (vm .PUSH1 ), 0x2 , // size
2614+ byte (vm .PUSH1 ), byte (32 - 2 ), // offset
2615+ byte (vm .RETURN ),
2616+ }
2617+ if l := len (initCode ); l > 32 {
2618+ t .Fatalf ("init code is too long for a pushx, need a more elaborate deployer" )
2619+ }
2620+ bbCode := []byte {
2621+ // Push initcode onto stack
2622+ byte (vm .PUSH1 ) + byte (len (initCode )- 1 )}
2623+ bbCode = append (bbCode , initCode ... )
2624+ bbCode = append (bbCode , []byte {
2625+ byte (vm .PUSH1 ), 0x0 , // memory start on stack
2626+ byte (vm .MSTORE ),
2627+ byte (vm .PUSH1 ), 0x00 , // salt
2628+ byte (vm .PUSH1 ), byte (len (initCode )), // size
2629+ byte (vm .PUSH1 ), byte (32 - len (initCode )), // offset
2630+ byte (vm .PUSH1 ), 0x00 , // endowment
2631+ byte (vm .CREATE2 ),
2632+ }... )
2633+
2634+ initHash := crypto .Keccak256Hash (initCode )
2635+ aa := crypto .CreateAddress2 (bb , [32 ]byte {}, initHash [:])
2636+ t .Logf ("Destination address: %x\n " , aa )
2637+ gspec := & Genesis {
2638+ Config : params .TestChainConfig ,
2639+ Alloc : GenesisAlloc {
2640+ address : {Balance : funds },
2641+ // The address 0xAAAAA selfdestructs if called
2642+ aa : {
2643+ // Code needs to just selfdestruct
2644+ Code : aaCode ,
2645+ Nonce : 1 ,
2646+ Balance : big .NewInt (0 ),
2647+ Storage : aaStorage ,
2648+ },
2649+ // The contract BB recreates AA
2650+ bb : {
2651+ Code : bbCode ,
2652+ Balance : big .NewInt (1 ),
2653+ },
2654+ },
2655+ }
2656+ genesis := gspec .MustCommit (db )
2657+ var nonce uint64
2658+
2659+ type expectation struct {
2660+ exist bool
2661+ blocknum int
2662+ values map [int ]int
2663+ }
2664+ var current = & expectation {
2665+ exist : true , // exists in genesis
2666+ blocknum : 0 ,
2667+ values : map [int ]int {1 : 1 , 2 : 2 },
2668+ }
2669+ var expectations []* expectation
2670+ var newDestruct = func (e * expectation ) * types.Transaction {
2671+ tx , _ := types .SignTx (types .NewTransaction (nonce , aa ,
2672+ big .NewInt (0 ), 50000 , big .NewInt (1 ), nil ), types.HomesteadSigner {}, key )
2673+ nonce ++
2674+ if e .exist {
2675+ e .exist = false
2676+ e .values = nil
2677+ }
2678+ t .Logf ("block %d; adding destruct\n " , e .blocknum )
2679+ return tx
2680+ }
2681+ var newResurrect = func (e * expectation ) * types.Transaction {
2682+ tx , _ := types .SignTx (types .NewTransaction (nonce , bb ,
2683+ big .NewInt (0 ), 100000 , big .NewInt (1 ), nil ), types.HomesteadSigner {}, key )
2684+ nonce ++
2685+ if ! e .exist {
2686+ e .exist = true
2687+ e .values = map [int ]int {3 : e .blocknum + 1 , 4 : 4 }
2688+ }
2689+ t .Logf ("block %d; adding resurrect\n " , e .blocknum )
2690+ return tx
2691+ }
2692+
2693+ blocks , _ := GenerateChain (params .TestChainConfig , genesis , engine , db , 150 , func (i int , b * BlockGen ) {
2694+ var exp = new (expectation )
2695+ exp .blocknum = i + 1
2696+ exp .values = make (map [int ]int )
2697+ for k , v := range current .values {
2698+ exp .values [k ] = v
2699+ }
2700+ exp .exist = current .exist
2701+
2702+ b .SetCoinbase (common.Address {1 })
2703+ if i % 2 == 0 {
2704+ b .AddTx (newDestruct (exp ))
2705+ }
2706+ if i % 3 == 0 {
2707+ b .AddTx (newResurrect (exp ))
2708+ }
2709+ if i % 5 == 0 {
2710+ b .AddTx (newDestruct (exp ))
2711+ }
2712+ if i % 7 == 0 {
2713+ b .AddTx (newResurrect (exp ))
2714+ }
2715+ expectations = append (expectations , exp )
2716+ current = exp
2717+ })
2718+ // Import the canonical chain
2719+ diskdb := rawdb .NewMemoryDatabase ()
2720+ gspec .MustCommit (diskdb )
2721+ chain , err := NewBlockChain (diskdb , nil , params .TestChainConfig , engine , vm.Config {
2722+ //Debug: true,
2723+ //Tracer: vm.NewJSONLogger(nil, os.Stdout),
2724+ }, nil )
2725+ if err != nil {
2726+ t .Fatalf ("failed to create tester chain: %v" , err )
2727+ }
2728+ var asHash = func (num int ) common.Hash {
2729+ return common .BytesToHash ([]byte {byte (num )})
2730+ }
2731+ for i , block := range blocks {
2732+ blockNum := i + 1
2733+ if n , err := chain .InsertChain ([]* types.Block {block }); err != nil {
2734+ t .Fatalf ("block %d: failed to insert into chain: %v" , n , err )
2735+ }
2736+ statedb , _ := chain .State ()
2737+ // If all is correct, then slot 1 and 2 are zero
2738+ if got , exp := statedb .GetState (aa , common .HexToHash ("01" )), (common.Hash {}); got != exp {
2739+ t .Errorf ("block %d, got %x exp %x" , blockNum , got , exp )
2740+ }
2741+ if got , exp := statedb .GetState (aa , common .HexToHash ("02" )), (common.Hash {}); got != exp {
2742+ t .Errorf ("block %d, got %x exp %x" , blockNum , got , exp )
2743+ }
2744+ exp := expectations [i ]
2745+ if exp .exist {
2746+ if ! statedb .Exist (aa ) {
2747+ t .Fatalf ("block %d, expected %v to exist, it did not" , blockNum , aa )
2748+ }
2749+ for slot , val := range exp .values {
2750+ if gotValue , expValue := statedb .GetState (aa , asHash (slot )), asHash (val ); gotValue != expValue {
2751+ t .Fatalf ("block %d, slot %d, got %x exp %x" , blockNum , slot , gotValue , expValue )
2752+ }
2753+ }
2754+ } else {
2755+ if statedb .Exist (aa ) {
2756+ t .Fatalf ("block %d, expected %v to not exist, it did" , blockNum , aa )
2757+ }
2758+ }
2759+ }
2760+ }
0 commit comments