From 2ed7c99ee1090bc3bb01621b7132c959bdac33c6 Mon Sep 17 00:00:00 2001 From: lmittmann <3458786+lmittmann@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:32:41 +0200 Subject: [PATCH] w3types: fix merge of account with empty code (#176) * added test: merge empty code * fixed account deep copy --------- Co-authored-by: lmittmann --- w3types/state.go | 2 +- w3types/state_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/w3types/state.go b/w3types/state.go index ae8747e9..d7a22c22 100644 --- a/w3types/state.go +++ b/w3types/state.go @@ -66,7 +66,7 @@ func (acc *Account) deepCopy() *Account { if acc.Balance != nil { newAcc.Balance = new(big.Int).Set(acc.Balance) } - if len(acc.Code) > 0 { + if acc.Code != nil { newAcc.Code = bytes.Clone(acc.Code) } if len(acc.Storage) > 0 { diff --git a/w3types/state_test.go b/w3types/state_test.go index 6123edde..8824287d 100644 --- a/w3types/state_test.go +++ b/w3types/state_test.go @@ -64,6 +64,24 @@ func TestStateMerge(t *testing.T) { StateSrc: w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{}: common.Hash{0x02}}}}, Want: w3types.State{common.Address{}: {Storage: w3types.Storage{common.Hash{}: common.Hash{0x02}}}}, }, + + // https://github.com/lmittmann/w3/pull/176 + { + Name: "empty-code", + StateDst: w3types.State{common.Address{}: {Code: []byte{}}}, + StateSrc: w3types.State{}, + Want: w3types.State{ + common.Address{}: {Code: []byte{}}, + }, + }, + { + Name: "empty-code2", + StateDst: w3types.State{}, + StateSrc: w3types.State{common.Address{}: {Code: []byte{}}}, + Want: w3types.State{ + common.Address{}: {Code: []byte{}}, + }, + }, } for _, test := range tests {