Skip to content

Commit

Permalink
[Json RPC] Catch Trie exception on eth_getStorage call (#7878)
Browse files Browse the repository at this point in the history
  • Loading branch information
brbrr authored Dec 11, 2024
1 parent 051d752 commit d063af0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,18 @@ public async Task Eth_get_storage_at_default_block()
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x0000000000000000000000000000000000000000000000000000000000abcdef\",\"id\":67}"));
}

[Test]
public async Task Eth_get_storage_at_missing_trie_node()
{
using Context ctx = await Context.Create();
ctx.Test.StateDb.Clear();
BlockParameter? blockParameter = null;
BlockHeader? header = ctx.Test.BlockFinder.FindHeader(blockParameter);
string serialized = await ctx.Test.TestEthRpc("eth_getStorageAt", TestItem.AddressA.Bytes.ToHexString(true), "0x1");
var expected = $"{{\"jsonrpc\":\"2.0\",\"error\":{{\"code\":-32000,\"message\":\"missing trie node {header?.StateRoot} (path ) state {header?.StateRoot} is not available\"}},\"id\":67}}";
Assert.That(serialized, Is.EqualTo(expected));
}

[Test]
public async Task Eth_get_block_number()
{
Expand Down
13 changes: 11 additions & 2 deletions src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using Nethermind.State;
using Nethermind.State.Proofs;
using Nethermind.Synchronization.ParallelSync;
using Nethermind.Trie;
using Nethermind.TxPool;
using Nethermind.Wallet;
using Block = Nethermind.Core.Block;
Expand Down Expand Up @@ -178,8 +179,16 @@ public ResultWrapper<byte[]> eth_getStorageAt(Address address, UInt256 positionI
}

BlockHeader? header = searchResult.Object;
ReadOnlySpan<byte> storage = _stateReader.GetStorage(header!.StateRoot!, address, positionIndex);
return ResultWrapper<byte[]>.Success(storage.IsEmpty ? Bytes32.Zero.Unwrap() : storage!.PadLeft(32));
try
{
ReadOnlySpan<byte> storage = _stateReader.GetStorage(header!.StateRoot!, address, positionIndex);
return ResultWrapper<byte[]>.Success(storage.IsEmpty ? Bytes32.Zero.Unwrap() : storage!.PadLeft(32));
}
catch (MissingTrieNodeException e)
{
var hash = e.TrieNodeException.NodeHash;
return ResultWrapper<byte[]>.Fail($"missing trie node {hash} (path ) state {hash} is not available", ErrorCodes.InvalidInput);
}
}

public Task<ResultWrapper<UInt256>> eth_getTransactionCount(Address address, BlockParameter? blockParameter)
Expand Down

0 comments on commit d063af0

Please sign in to comment.