Skip to content

Commit

Permalink
Parse BlockParameter correctly for eth_call (#6477)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jan 8, 2024
1 parent 9d1b4f6 commit 440a173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Nethermind/Nethermind.Blockchain/Find/BlockParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public BlockParameter(long number)

public BlockParameter(Hash256 blockHash, bool requireCanonical = false)
{
ArgumentNullException.ThrowIfNull(blockHash);

Type = BlockParameterType.BlockHash;
BlockHash = blockHash;
RequireCanonical = requireCanonical;
Expand Down Expand Up @@ -151,10 +153,17 @@ public override void Write(Utf8JsonWriter writer, BlockParameter value, JsonSeri
if (tokenType == JsonTokenType.StartObject)
{
bool requireCanonical = false;
bool readEndObject = false;
Hash256 blockHash = null;
for (int i = 0; i < 2; i++)
{
reader.Read();
if (reader.TokenType == JsonTokenType.EndObject)
{
readEndObject = true;
break;
}

if (reader.ValueTextEquals("requireCanonical"u8))
{
reader.Read();
Expand All @@ -168,7 +177,7 @@ public override void Write(Utf8JsonWriter writer, BlockParameter value, JsonSeri

BlockParameter parameter = new(blockHash, requireCanonical);

if (!reader.Read() || reader.TokenType != JsonTokenType.EndObject)
if ((!readEndObject && !reader.Read()) || reader.TokenType != JsonTokenType.EndObject)
{
ThrowInvalidFormatting();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ public async Task Eth_call_ok()
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"result\":\"0x\",\"id\":67}"));
}

[Test]
public async Task Eth_call_with_blockhash_ok()
{
using Context ctx = await Context.Create();
TransactionForRpc transaction = new(Keccak.Zero, 1L, 1, new Transaction());
transaction.From = TestItem.AddressA;
transaction.To = TestItem.AddressB;

string serialized =
await ctx.Test.TestEthRpc("eth_call", ctx.Test.JsonSerializer.Serialize(transaction), "{\"blockHash\":\"0xf0b3f69cbd4e1e8d9b0ef02ff5d1384d18e19d251a4052f5f90bab190c5e8937\"}");
Assert.That(serialized, Is.EqualTo("{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32001,\"message\":\"0xf0b3f69cbd4e1e8d9b0ef02ff5d1384d18e19d251a4052f5f90bab190c5e8937 could not be found\"},\"id\":67}"));
}

[Test]
public async Task Eth_call_create_tx_with_empty_data()
{
Expand Down

0 comments on commit 440a173

Please sign in to comment.