mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
111 lines
2.9 KiB
Go
111 lines
2.9 KiB
Go
// Copyright 2021 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package gethclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/XinFinOrg/XDPoSChain/common"
|
|
)
|
|
|
|
func TestOverrideAccountMarshal(t *testing.T) {
|
|
om := map[common.Address]OverrideAccount{
|
|
{0x11}: {
|
|
// Zero-valued nonce is not overridden, but simply dropped by the encoder.
|
|
Nonce: 0,
|
|
},
|
|
{0xaa}: {
|
|
Nonce: 5,
|
|
},
|
|
{0xbb}: {
|
|
Code: []byte{1},
|
|
},
|
|
{0xcc}: {
|
|
// 'code', 'balance', 'state' should be set when input is
|
|
// a non-nil but empty value.
|
|
Code: []byte{},
|
|
Balance: big.NewInt(0),
|
|
State: map[common.Hash]common.Hash{},
|
|
// For 'stateDiff' the behavior is different, empty map
|
|
// is ignored because it makes no difference.
|
|
StateDiff: map[common.Hash]common.Hash{},
|
|
},
|
|
}
|
|
|
|
marshalled, err := json.MarshalIndent(&om, "", " ")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
expected := `{
|
|
"0x1100000000000000000000000000000000000000": {},
|
|
"0xaa00000000000000000000000000000000000000": {
|
|
"nonce": "0x5"
|
|
},
|
|
"0xbb00000000000000000000000000000000000000": {
|
|
"code": "0x01"
|
|
},
|
|
"0xcc00000000000000000000000000000000000000": {
|
|
"code": "0x",
|
|
"balance": "0x0",
|
|
"state": {}
|
|
}
|
|
}`
|
|
|
|
if string(marshalled) != expected {
|
|
t.Error("wrong output:", string(marshalled))
|
|
t.Error("want:", expected)
|
|
}
|
|
}
|
|
|
|
func TestBlockOverridesMarshal(t *testing.T) {
|
|
for i, tt := range []struct {
|
|
bo BlockOverrides
|
|
want string
|
|
}{
|
|
{
|
|
bo: BlockOverrides{},
|
|
want: `{}`,
|
|
},
|
|
{
|
|
bo: BlockOverrides{
|
|
Coinbase: common.HexToAddress("0x1111111111111111111111111111111111111111"),
|
|
},
|
|
want: `{"coinbase":"0x1111111111111111111111111111111111111111"}`,
|
|
},
|
|
{
|
|
bo: BlockOverrides{
|
|
Number: big.NewInt(1),
|
|
Difficulty: big.NewInt(2),
|
|
Time: 3,
|
|
GasLimit: 4,
|
|
BaseFee: big.NewInt(5),
|
|
},
|
|
want: `{"number":"0x1","difficulty":"0x2","time":"0x3","gasLimit":"0x4","baseFee":"0x5"}`,
|
|
},
|
|
} {
|
|
marshalled, err := json.Marshal(&tt.bo)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(marshalled) != tt.want {
|
|
t.Errorf("Testcase #%d failed. expected\n%s\ngot\n%s", i, tt.want, string(marshalled))
|
|
}
|
|
}
|
|
}
|