Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for the "blockHash" parameter #4387

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,25 @@
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Nethermind.Core;
using Nethermind.JsonRpc;
using Newtonsoft.Json;

namespace Nethermind.AccountAbstraction.Subscribe
namespace Nethermind.AccountAbstraction.Subscribe;

public class UserOperationSubscriptionParam : IJsonRpcParam
{
public class UserOperationSubscriptionParam : IJsonRpcParam
public Address[] EntryPoints { get; set; } = Array.Empty<Address>();
public bool IncludeUserOperations { get; set; }

public void ReadJson(JsonSerializer serializer, string jsonValue)
{
public Address[] EntryPoints { get; set; } = Array.Empty<Address>();
public bool IncludeUserOperations { get; set; }

public void FromJson(JsonSerializer serializer, string jsonValue)
{
UserOperationSubscriptionParam ep = serializer.Deserialize<UserOperationSubscriptionParam>(jsonValue.ToJsonTextReader())
?? throw new ArgumentException($"Invalid 'entryPoints' filter: {jsonValue}");
EntryPoints = ep.EntryPoints;
IncludeUserOperations = ep.IncludeUserOperations;
UserOperationSubscriptionParam ep = serializer.Deserialize<UserOperationSubscriptionParam>(jsonValue.ToJsonTextReader())
?? throw new ArgumentException($"Invalid 'entryPoints' filter: {jsonValue}");
EntryPoints = ep.EntryPoints;
IncludeUserOperations = ep.IncludeUserOperations;

}
}
}
82 changes: 67 additions & 15 deletions src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections;
using FluentAssertions;
using Nethermind.Blockchain.Find;
using Nethermind.JsonRpc.Data;
using Nethermind.JsonRpc.Modules.Eth;
using Newtonsoft.Json;
using NUnit.Framework;
Expand All @@ -32,39 +31,92 @@ public static IEnumerable JsonTests
get
{
yield return new TestCaseData("{}",
new Filter()
new Filter
{
Address = null,
FromBlock = BlockParameter.Latest,
ToBlock = BlockParameter.Latest,
Topics = Array.Empty<object>()
});

yield return new TestCaseData("{\"fromBlock\":\"earliest\",\"toBlock\":\"pending\",\"topics\":[null, \"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7\", [\"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001\", \"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7\"]]}",
new Filter()

yield return new TestCaseData(
JsonConvert.SerializeObject(
new
{
fromBlock = "earliest",
toBlock = "pending",
topics = new object?[]
{
null,
"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7",
new[]
{
"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001",
"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7"
}
}
}),
new Filter
{
Address = null,
FromBlock = BlockParameter.Earliest,
ToBlock = BlockParameter.Pending,
Topics = new object?[] { null, "0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7", new [] {"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001", "0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7" } }
Topics = new object?[]
{
null,
"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7",
new[]
{
"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001",
"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7"
}
}
});

yield return new TestCaseData("{\"address\":\"0xc2d77d118326c33bbe36ebeabf4f7ed6bc2dda5c\",\"fromBlock\":\"0x1143ade\",\"toBlock\":\"latest\",\"topics\":[\"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7\",null,null,\"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001\"]}]",
new Filter()
yield return new TestCaseData(
JsonConvert.SerializeObject(
new
{
address = "0xc2d77d118326c33bbe36ebeabf4f7ed6bc2dda5c",
fromBlock = "0x1143ade",
toBlock = "latest",
topics = new object?[]
{
"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7",
null,
null,
"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001"
}
}),
new Filter
{
Address = "0xc2d77d118326c33bbe36ebeabf4f7ed6bc2dda5c",
FromBlock = new BlockParameter(0x1143ade),
ToBlock = BlockParameter.Latest,
Topics = new object?[] { "0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7", null, null, "0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001" }
Topics = new object?[]
{
"0xe194ef610f9150a2db4110b3db5116fd623175dca3528d7ae7046a1042f84fe7",
null,
null,
"0x000500002bd87daa34d8ff0daf3465c96044d8f6667614850000000000000001"
}
});

var blockHash = "0x892a8b3ccc78359e059e67ec44c83bfed496721d48c2d1dd929d6e4cd6559d35";
var blockParam = BlockParameterConverter.GetBlockParameter(blockHash);

yield return new TestCaseData(
JsonConvert.SerializeObject(new { blockHash }),
new Filter
{
FromBlock = blockParam,
ToBlock = blockParam,
});
}
}

[TestCaseSource(nameof(JsonTests))]
public void FromJson_parses_correctly(string json, Filter expectation)
{
Filter filter = new();
filter.FromJson(new JsonSerializer(), json);
filter.ReadJson(JsonSerializer.CreateDefault(), json);
filter.Should().BeEquivalentTo(expectation);
}
}
11 changes: 5 additions & 6 deletions src/Nethermind/Nethermind.JsonRpc/IJsonRpcParam.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Demerzel Solutions Limited
// Copyright (c) 2021 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
Expand All @@ -16,10 +16,9 @@

using Newtonsoft.Json;

namespace Nethermind.JsonRpc
namespace Nethermind.JsonRpc;

public interface IJsonRpcParam
{
public interface IJsonRpcParam
{
void FromJson(JsonSerializer serializer, string jsonValue);
}
void ReadJson(JsonSerializer serializer, string jsonValue);
}
Loading