Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethclient/gethclient: fix bugs in override object encoding #25616

Merged
merged 12 commits into from
Sep 27, 2022
9 changes: 7 additions & 2 deletions ethclient/gethclient/gethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,21 @@ func toOverrideMap(overrides *map[common.Address]OverrideAccount) interface{} {
}
type overrideAccount struct {
Nonce hexutil.Uint64 `json:"nonce"`
Code hexutil.Bytes `json:"code"`
Code string `json:"code,omitempty"`
Balance *hexutil.Big `json:"balance"`
State map[common.Hash]common.Hash `json:"state"`
StateDiff map[common.Hash]common.Hash `json:"stateDiff"`
}
result := make(map[common.Address]overrideAccount)

for addr, override := range *overrides {
var code string
if override.Code != nil {
code = hexutil.Encode(override.Code)
}
result[addr] = overrideAccount{
Nonce: hexutil.Uint64(override.Nonce),
Code: override.Code,
Code: code,
Balance: (*hexutil.Big)(override.Balance),
State: override.State,
StateDiff: override.StateDiff,
Expand Down
83 changes: 83 additions & 0 deletions ethclient/gethclient/gethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package gethclient
import (
"bytes"
"context"
"encoding/json"
"math/big"
"testing"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
Expand Down Expand Up @@ -135,6 +137,12 @@ func TestGethClient(t *testing.T) {
}, {
"TestCallContract",
func(t *testing.T) { testCallContract(t, client) },
}, {
"testCodeOverride",
func(t *testing.T) { testCodeOverride(t) },
}, {
"testNoCodeOverride",
func(t *testing.T) { testNoCodeOverride(t) },
},
}
t.Parallel()
Expand Down Expand Up @@ -327,3 +335,78 @@ func testCallContract(t *testing.T, client *rpc.Client) {
t.Fatalf("unexpected error: %v", err)
}
}

func testNoCodeOverride(t *testing.T) {
override := OverrideAccount{}

mapAcc := make(map[common.Address]OverrideAccount)
mapAcc[testAddr] = override

encoded := toOverrideMap(&mapAcc)

type overrideAccount struct {
Nonce hexutil.Uint64 `json:"nonce"`
Code []byte `json:"code"`
Balance *hexutil.Big `json:"balance"`
State map[common.Hash]common.Hash `json:"state"`
StateDiff map[common.Hash]common.Hash `json:"stateDiff"`
}

var accounts map[common.Address]*overrideAccount
marshalled, err := json.Marshal(encoded)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

err = json.Unmarshal(marshalled, &accounts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

account := accounts[testAddr]
if account == nil {
t.Fatal("Empty account retrieved")
}

if account.Code != nil {
t.Fatal("Code should be empty")
}
}

func testCodeOverride(t *testing.T) {
override := OverrideAccount{}
override.Code = []byte{1}

mapAcc := make(map[common.Address]OverrideAccount)
mapAcc[testAddr] = override

encoded := toOverrideMap(&mapAcc)

type overrideAccount struct {
Nonce hexutil.Uint64 `json:"nonce"`
Code []byte `json:"code"`
Balance *hexutil.Big `json:"balance"`
State map[common.Hash]common.Hash `json:"state"`
StateDiff map[common.Hash]common.Hash `json:"stateDiff"`
}

var accounts map[common.Address]*overrideAccount
marshalled, err := json.Marshal(encoded)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

err = json.Unmarshal(marshalled, &accounts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

account := accounts[testAddr]
if account == nil {
t.Fatal("Empty account retrieved")
}

if account.Code == nil {
t.Fatal("Code should not be empty")
}
}