mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
ethclient/gethclient: add test for code in getProof
This commit is contained in:
parent
509a64ffb9
commit
1e55e52b26
1 changed files with 32 additions and 24 deletions
|
|
@ -39,11 +39,12 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
testAddr = crypto.PubkeyToAddress(testKey.PublicKey)
|
||||||
testSlot = common.HexToHash("0xdeadbeef")
|
testContract = common.HexToAddress("0xbeef")
|
||||||
testValue = crypto.Keccak256Hash(testSlot[:])
|
testSlot = common.HexToHash("0xdeadbeef")
|
||||||
testBalance = big.NewInt(2e15)
|
testValue = crypto.Keccak256Hash(testSlot[:])
|
||||||
|
testBalance = big.NewInt(2e15)
|
||||||
)
|
)
|
||||||
|
|
||||||
func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
|
func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
|
||||||
|
|
@ -78,8 +79,9 @@ func newTestBackend(t *testing.T) (*node.Node, []*types.Block) {
|
||||||
|
|
||||||
func generateTestChain() (*core.Genesis, []*types.Block) {
|
func generateTestChain() (*core.Genesis, []*types.Block) {
|
||||||
genesis := &core.Genesis{
|
genesis := &core.Genesis{
|
||||||
Config: params.AllEthashProtocolChanges,
|
Config: params.AllEthashProtocolChanges,
|
||||||
Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance, Storage: map[common.Hash]common.Hash{testSlot: testValue}}},
|
Alloc: core.GenesisAlloc{testAddr: {Balance: testBalance, Storage: map[common.Hash]common.Hash{testSlot: testValue}},
|
||||||
|
testContract: {Nonce: 1, Code: []byte{0x13, 0x37}}},
|
||||||
ExtraData: []byte("test genesis"),
|
ExtraData: []byte("test genesis"),
|
||||||
Timestamp: 9000,
|
Timestamp: 9000,
|
||||||
}
|
}
|
||||||
|
|
@ -103,8 +105,11 @@ func TestGethClient(t *testing.T) {
|
||||||
test func(t *testing.T)
|
test func(t *testing.T)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"TestGetProof",
|
"TestGetProof1",
|
||||||
func(t *testing.T) { testGetProof(t, client) },
|
func(t *testing.T) { testGetProof(t, client, testAddr) },
|
||||||
|
}, {
|
||||||
|
"TestGetProof2",
|
||||||
|
func(t *testing.T) { testGetProof(t, client, testContract) },
|
||||||
}, {
|
}, {
|
||||||
"TestGetProofCanonicalizeKeys",
|
"TestGetProofCanonicalizeKeys",
|
||||||
func(t *testing.T) { testGetProofCanonicalizeKeys(t, client) },
|
func(t *testing.T) { testGetProofCanonicalizeKeys(t, client) },
|
||||||
|
|
@ -201,38 +206,41 @@ func testAccessList(t *testing.T, client *rpc.Client) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetProof(t *testing.T, client *rpc.Client) {
|
func testGetProof(t *testing.T, client *rpc.Client, addr common.Address) {
|
||||||
ec := New(client)
|
ec := New(client)
|
||||||
ethcl := ethclient.NewClient(client)
|
ethcl := ethclient.NewClient(client)
|
||||||
result, err := ec.GetProof(context.Background(), testAddr, []string{testSlot.String()}, nil)
|
result, err := ec.GetProof(context.Background(), addr, []string{testSlot.String()}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !bytes.Equal(result.Address[:], testAddr[:]) {
|
if result.Address != addr {
|
||||||
t.Fatalf("unexpected address, want: %v got: %v", testAddr, result.Address)
|
t.Fatalf("unexpected address, have: %v want: %v", result.Address, addr)
|
||||||
}
|
}
|
||||||
// test nonce
|
// test nonce
|
||||||
nonce, _ := ethcl.NonceAt(context.Background(), result.Address, nil)
|
if nonce, _ := ethcl.NonceAt(context.Background(), addr, nil); result.Nonce != nonce {
|
||||||
if result.Nonce != nonce {
|
|
||||||
t.Fatalf("invalid nonce, want: %v got: %v", nonce, result.Nonce)
|
t.Fatalf("invalid nonce, want: %v got: %v", nonce, result.Nonce)
|
||||||
}
|
}
|
||||||
// test balance
|
// test balance
|
||||||
balance, _ := ethcl.BalanceAt(context.Background(), result.Address, nil)
|
if balance, _ := ethcl.BalanceAt(context.Background(), addr, nil); result.Balance.Cmp(balance) != 0 {
|
||||||
if result.Balance.Cmp(balance) != 0 {
|
|
||||||
t.Fatalf("invalid balance, want: %v got: %v", balance, result.Balance)
|
t.Fatalf("invalid balance, want: %v got: %v", balance, result.Balance)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test storage
|
// test storage
|
||||||
if len(result.StorageProof) != 1 {
|
if len(result.StorageProof) != 1 {
|
||||||
t.Fatalf("invalid storage proof, want 1 proof, got %v proof(s)", len(result.StorageProof))
|
t.Fatalf("invalid storage proof, want 1 proof, got %v proof(s)", len(result.StorageProof))
|
||||||
}
|
}
|
||||||
proof := result.StorageProof[0]
|
for _, proof := range result.StorageProof {
|
||||||
slotValue, _ := ethcl.StorageAt(context.Background(), testAddr, testSlot, nil)
|
if proof.Key != testSlot.String() {
|
||||||
if !bytes.Equal(slotValue, proof.Value.Bytes()) {
|
t.Fatalf("invalid storage proof key, want: %q, got: %q", testSlot.String(), proof.Key)
|
||||||
t.Fatalf("invalid storage proof value, want: %v, got: %v", slotValue, proof.Value.Bytes())
|
}
|
||||||
|
slotValue, _ := ethcl.StorageAt(context.Background(), addr, common.HexToHash(proof.Key), nil)
|
||||||
|
if have, want := common.BigToHash(proof.Value), common.BytesToHash(slotValue); have != want {
|
||||||
|
t.Fatalf("addr %x, invalid storage proof value: have: %v, want: %v", addr, have, want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if proof.Key != testSlot.String() {
|
// test code
|
||||||
t.Fatalf("invalid storage proof key, want: %q, got: %q", testSlot.String(), proof.Key)
|
code, _ := ethcl.CodeAt(context.Background(), addr, nil)
|
||||||
|
if have, want := result.CodeHash, crypto.Keccak256Hash(code); have != want {
|
||||||
|
t.Fatalf("codehash wrong, have %v want %v ", have, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue