github.com/lmittmann/w3@v0.20.0/w3types/state_test.go (about)

     1  package w3types_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/google/go-cmp/cmp/cmpopts"
     9  	"github.com/lmittmann/w3/w3types"
    10  )
    11  
    12  func TestStateMerge(t *testing.T) {
    13  	tests := []struct {
    14  		Name     string
    15  		StateDst w3types.State
    16  		StateSrc w3types.State
    17  		Want     w3types.State
    18  	}{
    19  		{
    20  			Name:     "empty",
    21  			StateDst: w3types.State{},
    22  			StateSrc: w3types.State{},
    23  			Want:     w3types.State{},
    24  		},
    25  		{
    26  			Name:     "empty-dst",
    27  			StateDst: w3types.State{},
    28  			StateSrc: w3types.State{common.Address{}: {}},
    29  			Want:     w3types.State{common.Address{}: {}},
    30  		},
    31  		{
    32  			Name:     "empty-src",
    33  			StateDst: w3types.State{common.Address{}: {}},
    34  			StateSrc: w3types.State{},
    35  			Want:     w3types.State{common.Address{}: {}},
    36  		},
    37  		{
    38  			Name:     "simple",
    39  			StateDst: w3types.State{common.Address{0x01}: {}},
    40  			StateSrc: w3types.State{common.Address{0x02}: {}},
    41  			Want: w3types.State{
    42  				common.Address{0x01}: {},
    43  				common.Address{0x02}: {},
    44  			},
    45  		},
    46  		{
    47  			Name:     "simple-conflict",
    48  			StateDst: w3types.State{common.Address{}: {Nonce: 1}},
    49  			StateSrc: w3types.State{common.Address{}: {Nonce: 2}},
    50  			Want:     w3types.State{common.Address{}: {Nonce: 2}},
    51  		},
    52  		{
    53  			Name:     "storage-simple",
    54  			StateDst: w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{0x01}: common.Hash{0x01}}}},
    55  			StateSrc: w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{0x02}: common.Hash{0x02}}}},
    56  			Want: w3types.State{common.Address{}: {Storage: w3types.Storage{
    57  				common.Hash{0x01}: common.Hash{0x01},
    58  				common.Hash{0x02}: common.Hash{0x02},
    59  			}}},
    60  		},
    61  		{
    62  			Name:     "storage-conflict",
    63  			StateDst: w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{}: common.Hash{0x01}}}},
    64  			StateSrc: w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{}: common.Hash{0x02}}}},
    65  			Want:     w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{}: common.Hash{0x02}}}},
    66  		},
    67  
    68  		// https://github.com/lmittmann/w3/pull/176
    69  		{
    70  			Name:     "empty-code",
    71  			StateDst: w3types.State{common.Address{}: {Code: []byte{}}},
    72  			StateSrc: w3types.State{},
    73  			Want:     w3types.State{common.Address{}: {Code: []byte{}}},
    74  		},
    75  		{
    76  			Name:     "empty-code2",
    77  			StateDst: w3types.State{},
    78  			StateSrc: w3types.State{common.Address{}: {Code: []byte{}}},
    79  			Want:     w3types.State{common.Address{}: {Code: []byte{}}},
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		t.Run(test.Name, func(t *testing.T) {
    85  			got := test.StateDst.Merge(test.StateSrc)
    86  			if diff := cmp.Diff(test.Want, got,
    87  				cmpopts.IgnoreUnexported(w3types.Account{}),
    88  			); diff != "" {
    89  				t.Fatalf("(-want, +got)\n%s", diff)
    90  			}
    91  		})
    92  	}
    93  }