mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
ethclient: Add BalanceAtHash, NonceAtHash and StorageAtHash functions
This commit is contained in:
parent
441775638d
commit
b3f4daa6f7
2 changed files with 54 additions and 16 deletions
|
|
@ -370,6 +370,13 @@ func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNu
|
||||||
return (*big.Int)(&result), err
|
return (*big.Int)(&result), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BalanceAtHash returns the wei balance of the given account.
|
||||||
|
func (ec *Client) BalanceAtHash(ctx context.Context, account common.Address, blockHash common.Hash) (*big.Int, error) {
|
||||||
|
var result hexutil.Big
|
||||||
|
err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, rpc.BlockNumberOrHashWithHash(blockHash, false))
|
||||||
|
return (*big.Int)(&result), err
|
||||||
|
}
|
||||||
|
|
||||||
// StorageAt returns the value of key in the contract storage of the given account.
|
// StorageAt returns the value of key in the contract storage of the given account.
|
||||||
// The block number can be nil, in which case the value is taken from the latest known block.
|
// The block number can be nil, in which case the value is taken from the latest known block.
|
||||||
func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
|
func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
|
||||||
|
|
@ -378,6 +385,13 @@ func (ec *Client) StorageAt(ctx context.Context, account common.Address, key com
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StorageAtHash returns the value of key in the contract storage of the given account.
|
||||||
|
func (ec *Client) StorageAtHash(ctx context.Context, account common.Address, key common.Hash, blockHash common.Hash) ([]byte, error) {
|
||||||
|
var result hexutil.Bytes
|
||||||
|
err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, rpc.BlockNumberOrHashWithHash(blockHash, false))
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
// CodeAt returns the contract code of the given account.
|
// CodeAt returns the contract code of the given account.
|
||||||
// The block number can be nil, in which case the code is taken from the latest known block.
|
// The block number can be nil, in which case the code is taken from the latest known block.
|
||||||
func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
|
func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
|
||||||
|
|
@ -401,6 +415,13 @@ func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumb
|
||||||
return uint64(result), err
|
return uint64(result), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NonceAtHash returns the account nonce of the given account.
|
||||||
|
func (ec *Client) NonceAtHash(ctx context.Context, account common.Address, blockHash common.Hash) (uint64, error) {
|
||||||
|
var result hexutil.Uint64
|
||||||
|
err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, rpc.BlockNumberOrHashWithHash(blockHash, false))
|
||||||
|
return uint64(result), err
|
||||||
|
}
|
||||||
|
|
||||||
// Filters
|
// Filters
|
||||||
|
|
||||||
// FilterLogs executes a filter query.
|
// FilterLogs executes a filter query.
|
||||||
|
|
|
||||||
|
|
@ -281,9 +281,6 @@ func TestEthClient(t *testing.T) {
|
||||||
"CallContractAtHash": {
|
"CallContractAtHash": {
|
||||||
func(t *testing.T) { testCallContractAtHash(t, client) },
|
func(t *testing.T) { testCallContractAtHash(t, client) },
|
||||||
},
|
},
|
||||||
"CodeAtHash": {
|
|
||||||
func(t *testing.T) { testCodeAtHash(t, client) },
|
|
||||||
},
|
|
||||||
"AtFunctions": {
|
"AtFunctions": {
|
||||||
func(t *testing.T) { testAtFunctions(t, client) },
|
func(t *testing.T) { testAtFunctions(t, client) },
|
||||||
},
|
},
|
||||||
|
|
@ -556,19 +553,6 @@ func testCallContractAtHash(t *testing.T, client *rpc.Client) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testCodeAtHash(t *testing.T, client *rpc.Client) {
|
|
||||||
ec := NewClient(client)
|
|
||||||
|
|
||||||
block, err := ec.HeaderByNumber(context.Background(), big.NewInt(1))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("BlockByNumber error: %v", err)
|
|
||||||
}
|
|
||||||
// CodeAtHash
|
|
||||||
if _, err := ec.CodeAtHash(context.Background(), common.Address{}, block.Hash()); err != nil {
|
|
||||||
t.Fatalf("unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCallContract(t *testing.T, client *rpc.Client) {
|
func testCallContract(t *testing.T, client *rpc.Client) {
|
||||||
ec := NewClient(client)
|
ec := NewClient(client)
|
||||||
|
|
||||||
|
|
@ -599,6 +583,11 @@ func testCallContract(t *testing.T, client *rpc.Client) {
|
||||||
func testAtFunctions(t *testing.T, client *rpc.Client) {
|
func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||||
ec := NewClient(client)
|
ec := NewClient(client)
|
||||||
|
|
||||||
|
block, err := ec.HeaderByNumber(context.Background(), big.NewInt(1))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("BlockByNumber error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// send a transaction for some interesting pending status
|
// send a transaction for some interesting pending status
|
||||||
sendTransaction(ec)
|
sendTransaction(ec)
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
@ -616,6 +605,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
hashBalance, err := ec.BalanceAtHash(context.Background(), testAddr, block.Hash())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if balance.Cmp(hashBalance) == 0 {
|
||||||
|
t.Fatalf("unexpected balance at hash: %v %v", balance, hashBalance)
|
||||||
|
}
|
||||||
penBalance, err := ec.PendingBalanceAt(context.Background(), testAddr)
|
penBalance, err := ec.PendingBalanceAt(context.Background(), testAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
|
@ -628,6 +624,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
hashNonce, err := ec.NonceAtHash(context.Background(), testAddr, block.Hash())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if hashNonce == nonce {
|
||||||
|
t.Fatalf("unexpected nonce at hash: %v %v", nonce, hashNonce)
|
||||||
|
}
|
||||||
penNonce, err := ec.PendingNonceAt(context.Background(), testAddr)
|
penNonce, err := ec.PendingNonceAt(context.Background(), testAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
|
@ -640,6 +643,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
hashStorage, err := ec.StorageAtHash(context.Background(), testAddr, common.Hash{}, block.Hash())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(storage, hashStorage) {
|
||||||
|
t.Fatalf("unexpected storage at hash: %v %v", storage, hashStorage)
|
||||||
|
}
|
||||||
penStorage, err := ec.PendingStorageAt(context.Background(), testAddr, common.Hash{})
|
penStorage, err := ec.PendingStorageAt(context.Background(), testAddr, common.Hash{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
|
@ -652,6 +662,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
hashCode, err := ec.CodeAtHash(context.Background(), common.Address{}, block.Hash())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(code, hashCode) {
|
||||||
|
t.Fatalf("unexpected code at hash: %v %v", code, hashCode)
|
||||||
|
}
|
||||||
penCode, err := ec.PendingCodeAt(context.Background(), testAddr)
|
penCode, err := ec.PendingCodeAt(context.Background(), testAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected error: %v", err)
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue