forked from cerc-io/plugeth
api/bind: add CallOpts.BlockHash to allow calling contracts at a specific block hash (#28084)
* api/bind: Add CallOpts.BlockHash to allow calling contracts at a specific block hash. * ethclient: Add BalanceAtHash, NonceAtHash and StorageAtHash functions
This commit is contained in:
@@ -370,6 +370,13 @@ func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNu
|
||||
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.
|
||||
// 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) {
|
||||
@@ -378,6 +385,13 @@ func (ec *Client) StorageAt(ctx context.Context, account common.Address, key com
|
||||
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.
|
||||
// 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) {
|
||||
@@ -386,6 +400,13 @@ func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumbe
|
||||
return result, err
|
||||
}
|
||||
|
||||
// CodeAtHash returns the contract code of the given account.
|
||||
func (ec *Client) CodeAtHash(ctx context.Context, account common.Address, blockHash common.Hash) ([]byte, error) {
|
||||
var result hexutil.Bytes
|
||||
err := ec.c.CallContext(ctx, &result, "eth_getCode", account, rpc.BlockNumberOrHashWithHash(blockHash, false))
|
||||
return result, err
|
||||
}
|
||||
|
||||
// NonceAt returns the account nonce of the given account.
|
||||
// The block number can be nil, in which case the nonce is taken from the latest known block.
|
||||
func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
|
||||
@@ -394,6 +415,13 @@ func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumb
|
||||
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
|
||||
|
||||
// FilterLogs executes a filter query.
|
||||
|
||||
@@ -584,6 +584,11 @@ func testCallContract(t *testing.T, client *rpc.Client) {
|
||||
func testAtFunctions(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)
|
||||
}
|
||||
|
||||
// send a transaction for some interesting pending status
|
||||
sendTransaction(ec)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
@@ -601,6 +606,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
@@ -613,6 +625,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
@@ -625,6 +644,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||
if err != nil {
|
||||
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{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
@@ -637,6 +663,13 @@ func testAtFunctions(t *testing.T, client *rpc.Client) {
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user