Skip to content

Commit d063af0

Browse files
authored
[Json RPC] Catch Trie exception on eth_getStorage call (#7878)
1 parent 051d752 commit d063af0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,18 @@ public async Task Eth_get_storage_at_default_block()
363363
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x0000000000000000000000000000000000000000000000000000000000abcdef\",\"id\":67}"));
364364
}
365365

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

src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
using Nethermind.State;
3434
using Nethermind.State.Proofs;
3535
using Nethermind.Synchronization.ParallelSync;
36+
using Nethermind.Trie;
3637
using Nethermind.TxPool;
3738
using Nethermind.Wallet;
3839
using Block = Nethermind.Core.Block;
@@ -178,8 +179,16 @@ public ResultWrapper<byte[]> eth_getStorageAt(Address address, UInt256 positionI
178179
}
179180

180181
BlockHeader? header = searchResult.Object;
181-
ReadOnlySpan<byte> storage = _stateReader.GetStorage(header!.StateRoot!, address, positionIndex);
182-
return ResultWrapper<byte[]>.Success(storage.IsEmpty ? Bytes32.Zero.Unwrap() : storage!.PadLeft(32));
182+
try
183+
{
184+
ReadOnlySpan<byte> storage = _stateReader.GetStorage(header!.StateRoot!, address, positionIndex);
185+
return ResultWrapper<byte[]>.Success(storage.IsEmpty ? Bytes32.Zero.Unwrap() : storage!.PadLeft(32));
186+
}
187+
catch (MissingTrieNodeException e)
188+
{
189+
var hash = e.TrieNodeException.NodeHash;
190+
return ResultWrapper<byte[]>.Fail($"missing trie node {hash} (path ) state {hash} is not available", ErrorCodes.InvalidInput);
191+
}
183192
}
184193

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

0 commit comments

Comments
 (0)