Skip to content

Commit a839bb2

Browse files
committed
feat: eth_getStorageAt standard empty response
1 parent e9f0b22 commit a839bb2

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

archive/client.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,18 @@ func (c *Client) GetStorageAt(
3434
address common.Address,
3535
position hexutil.Big,
3636
blockNr uint64,
37-
) (hexutil.Big, error) {
37+
) (hexutil.Bytes, error) {
3838
storageBytes, err := c.inner.StorageAt(
3939
ctx,
4040
address,
4141
common.BigToHash((*big.Int)(&position)),
4242
new(big.Int).SetUint64(blockNr),
4343
)
4444
if err != nil {
45-
return hexutil.Big{}, fmt.Errorf("archive: failed to query storage: %w", err)
45+
return nil, fmt.Errorf("archive: failed to query storage: %w", err)
4646
}
4747

48-
// Oh for fuck's sake.
49-
var storageBig big.Int
50-
storageBig.SetBytes(storageBytes)
51-
return hexutil.Big(storageBig), nil
48+
return storageBytes, nil
5249
}
5350

5451
func (c *Client) GetBalance(

rpc/eth/api.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type API interface {
5858
// GetBlockTransactionCountByNumber returns the number of transactions in the block.
5959
GetBlockTransactionCountByNumber(ctx context.Context, blockNum ethrpc.BlockNumber) (hexutil.Uint, error)
6060
// GetStorageAt returns the storage value at the provided position.
61-
GetStorageAt(ctx context.Context, address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (hexutil.Big, error)
61+
GetStorageAt(ctx context.Context, address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (hexutil.Bytes, error)
6262
// GetBalance returns the provided account's balance up to the provided block number.
6363
GetBalance(ctx context.Context, address common.Address, blockNrOrHash ethrpc.BlockNumberOrHash) (*hexutil.Big, error)
6464
// ChainId return the EIP-155 chain id for the current network.
@@ -232,13 +232,13 @@ func (api *publicAPI) GetBlockTransactionCountByNumber(ctx context.Context, bloc
232232
return hexutil.Uint(n), nil
233233
}
234234

235-
func (api *publicAPI) GetStorageAt(ctx context.Context, address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (hexutil.Big, error) {
235+
func (api *publicAPI) GetStorageAt(ctx context.Context, address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (hexutil.Bytes, error) {
236236
logger := api.Logger.With("method", "eth_getStorageAt", "address", address, "position", position, "block_or_hash", blockNrOrHash)
237237
logger.Debug("request")
238238

239239
round, err := api.getBlockRound(ctx, logger, blockNrOrHash)
240240
if err != nil {
241-
return hexutil.Big{}, err
241+
return hexutil.Bytes{}, err
242242
}
243243
if api.shouldQueryArchive(round) {
244244
return api.archiveClient.GetStorageAt(ctx, address, position, round)
@@ -253,12 +253,9 @@ func (api *publicAPI) GetStorageAt(ctx context.Context, address common.Address,
253253
res, err := ethmod.Storage(ctx, round, address[:], position256)
254254
if err != nil {
255255
logger.Error("failed to query storage", "err", err)
256-
return hexutil.Big{}, ErrInternalError
256+
return nil, ErrInternalError
257257
}
258-
// Some apps expect no leading zeros, so output as big integer.
259-
var resultBI big.Int
260-
resultBI.SetBytes(res)
261-
return hexutil.Big(resultBI), nil
258+
return res[:], nil
262259
}
263260

264261
func (api *publicAPI) GetBalance(ctx context.Context, address common.Address, blockNrOrHash ethrpc.BlockNumberOrHash) (*hexutil.Big, error) {

rpc/eth/metrics/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (m *metricsWrapper) GetLogs(ctx context.Context, filter filters.FilterCrite
286286
}
287287

288288
// GetStorageAt implements eth.API.
289-
func (m *metricsWrapper) GetStorageAt(ctx context.Context, address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (res hexutil.Big, err error) {
289+
func (m *metricsWrapper) GetStorageAt(ctx context.Context, address common.Address, position hexutil.Big, blockNrOrHash ethrpc.BlockNumberOrHash) (res hexutil.Bytes, err error) {
290290
r, s, f, i, d := metrics.GetAPIMethodMetrics("eth_getStorageAt")
291291
defer metrics.InstrumentCaller(r, s, f, i, d, &err)()
292292

tests/rpc/tx_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,44 @@ func TestEth_GetCode(t *testing.T) {
430430
require.NotEmpty(t, storedCode)
431431
}
432432

433+
func TestEth_GetStorageZero(t *testing.T) {
434+
ec := localClient(t, false)
435+
436+
code := common.FromHex(strings.TrimSpace(evmSolTestCompiledHex))
437+
438+
chainID, err := ec.ChainID(context.Background())
439+
require.Nil(t, err, "get chainid")
440+
441+
nonce, err := ec.NonceAt(context.Background(), tests.TestKey1.EthAddress, nil)
442+
require.Nil(t, err, "get nonce failed")
443+
t.Logf("got nonce: %v", nonce)
444+
445+
// Create transaction
446+
tx := types.NewContractCreation(nonce, big.NewInt(0), GasLimit, GasPrice, code)
447+
signer := types.LatestSignerForChainID(chainID)
448+
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), tests.TestKey1.Private)
449+
require.Nil(t, err, "sign tx")
450+
451+
signedTx, err := tx.WithSignature(signer, signature)
452+
require.Nil(t, err, "pack tx")
453+
454+
err = ec.SendTransaction(context.Background(), signedTx)
455+
require.Nil(t, err, "send transaction failed")
456+
457+
ctx, cancel := context.WithTimeout(context.Background(), ethTimeout)
458+
defer cancel()
459+
460+
receipt, err := waitTransaction(ctx, ec, signedTx.Hash())
461+
require.NoError(t, err)
462+
t.Logf("SignedTx hash: %s", signedTx.Hash().Hex())
463+
t.Logf("Contract address: %s", receipt.ContractAddress)
464+
465+
key := common.HexToHash("0x0")
466+
res, err := ec.StorageAt(context.Background(), receipt.ContractAddress, key, nil)
467+
require.NoError(t, err, "get storage at")
468+
require.Equal(t, len(res), 32, "storage at should return 32 bytes (even if zero)")
469+
}
470+
433471
func TestEth_Call(t *testing.T) {
434472
abidata := `
435473
[

0 commit comments

Comments
 (0)