From ce3ed80d6a6f6a0cf46deee62b9b9dcb9b80cef9 Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Mon, 17 Jul 2023 14:48:45 +0200 Subject: [PATCH 1/4] add FindStorage --- src/RpcServer/RpcServer.Blockchain.cs | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/RpcServer/RpcServer.Blockchain.cs b/src/RpcServer/RpcServer.Blockchain.cs index c09514107..81d28867c 100644 --- a/src/RpcServer/RpcServer.Blockchain.cs +++ b/src/RpcServer/RpcServer.Blockchain.cs @@ -197,6 +197,58 @@ protected virtual JToken GetStorage(JArray _params) return Convert.ToBase64String(item.Value.Span); } + [RpcMethod] + protected virtual JToken FindStorage(JArray _params) + { + using var snapshot = system.GetSnapshot(); + if (!int.TryParse(_params[0].AsString(), out int id)) + { + UInt160 hash = UInt160.Parse(_params[0].AsString()); + ContractState contract = NativeContract.ContractManagement.GetContract(snapshot, hash); + if (contract is null) throw new RpcException(-100, "Unknown contract"); + id = contract.Id; + } + + byte[] prefix = Convert.FromBase64String(_params[1].AsString()); + byte[] prefix_key = StorageKey.CreateSearchPrefix(id, prefix); + + if (!int.TryParse(_params[2].AsString(), out int start)) + { + start = 0; + } + + JObject json = new(); + JArray jarr = new(); + int pageSize = 50; + int i = 0; + + using (var iter = snapshot.Find(prefix_key).Skip(count: start).GetEnumerator()) + { + var hasMore = false; + while (iter.MoveNext()) + { + if (i == pageSize) + { + hasMore = true; + break; + } + + + JObject j = new(); + j["key"] = Convert.ToBase64String(iter.Current.Key.Key.Span); + j["value"] = Convert.ToBase64String(iter.Current.Value.Value.Span); + jarr.Add(j); + i++; + } + json["truncated"] = hasMore; + + } + + json["next"] = start + i; + json["results"] = jarr; + return json; + } + [RpcMethod] protected virtual JToken GetTransactionHeight(JArray _params) { From 2a28de848a6d6919d928f3b4e4db0b1c4d4290ba Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Wed, 2 Aug 2023 10:34:19 +0200 Subject: [PATCH 2/4] Make PageSize configurable in settings --- src/RpcServer/RpcServer.Blockchain.cs | 2 +- src/RpcServer/RpcServer.csproj | 2 ++ src/RpcServer/Settings.cs | 7 +++++-- src/RpcServer/config.json | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/RpcServer/RpcServer.Blockchain.cs b/src/RpcServer/RpcServer.Blockchain.cs index 81d28867c..0d2c662fb 100644 --- a/src/RpcServer/RpcServer.Blockchain.cs +++ b/src/RpcServer/RpcServer.Blockchain.cs @@ -219,7 +219,7 @@ protected virtual JToken FindStorage(JArray _params) JObject json = new(); JArray jarr = new(); - int pageSize = 50; + int pageSize = settings.FindStoragePageSize; int i = 0; using (var iter = snapshot.Find(prefix_key).Skip(count: start).GetEnumerator()) diff --git a/src/RpcServer/RpcServer.csproj b/src/RpcServer/RpcServer.csproj index 600eec6c3..897c0c8e7 100644 --- a/src/RpcServer/RpcServer.csproj +++ b/src/RpcServer/RpcServer.csproj @@ -3,6 +3,8 @@ Neo.Plugins.RpcServer Neo.Plugins + net6.0 + 10 diff --git a/src/RpcServer/Settings.cs b/src/RpcServer/Settings.cs index 8bf8d0c95..6d5b1202a 100644 --- a/src/RpcServer/Settings.cs +++ b/src/RpcServer/Settings.cs @@ -45,6 +45,7 @@ public record RpcServerSettings public string[] DisabledMethods { get; init; } public bool SessionEnabled { get; init; } public TimeSpan SessionExpirationTime { get; init; } + public int FindStoragePageSize { get; init; } public static RpcServerSettings Default { get; } = new RpcServerSettings { @@ -60,7 +61,8 @@ public record RpcServerSettings DisabledMethods = Array.Empty(), MaxConcurrentConnections = 40, SessionEnabled = false, - SessionExpirationTime = TimeSpan.FromSeconds(60) + SessionExpirationTime = TimeSpan.FromSeconds(60), + FindStoragePageSize = 50 }; public static RpcServerSettings Load(IConfigurationSection section) => new() @@ -80,7 +82,8 @@ public record RpcServerSettings DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get()).ToArray(), MaxConcurrentConnections = section.GetValue("MaxConcurrentConnections", Default.MaxConcurrentConnections), SessionEnabled = section.GetValue("SessionEnabled", Default.SessionEnabled), - SessionExpirationTime = TimeSpan.FromSeconds(section.GetValue("SessionExpirationTime", (int)Default.SessionExpirationTime.TotalSeconds)) + SessionExpirationTime = TimeSpan.FromSeconds(section.GetValue("SessionExpirationTime", (int)Default.SessionExpirationTime.TotalSeconds)), + FindStoragePageSize = section.GetValue("FindStoragePageSize", Default.FindStoragePageSize) }; } } diff --git a/src/RpcServer/config.json b/src/RpcServer/config.json index 89bc7f578..cfb60752f 100644 --- a/src/RpcServer/config.json +++ b/src/RpcServer/config.json @@ -17,7 +17,8 @@ "MaxStackSize": 65535, "DisabledMethods": [ "openwallet" ], "SessionEnabled": false, - "SessionExpirationTime": 60 + "SessionExpirationTime": 60, + "FindStoragePageSize": 50 } ] } From 78fc61fe3a580f1f4d0fe3cb6c68d9aab178c2ea Mon Sep 17 00:00:00 2001 From: Erik van den Brink Date: Thu, 3 Aug 2023 08:56:13 +0200 Subject: [PATCH 3/4] revert project file changes --- src/RpcServer/RpcServer.csproj | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/RpcServer/RpcServer.csproj b/src/RpcServer/RpcServer.csproj index 897c0c8e7..77a4c158e 100644 --- a/src/RpcServer/RpcServer.csproj +++ b/src/RpcServer/RpcServer.csproj @@ -3,8 +3,6 @@ Neo.Plugins.RpcServer Neo.Plugins - net6.0 - 10 - + From 62225ef53c7c6d8931cfc1c401a648812bdafd6a Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 21 Aug 2023 02:28:23 -0700 Subject: [PATCH 4/4] Remove empty lines --- src/RpcServer/RpcServer.Blockchain.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/RpcServer/RpcServer.Blockchain.cs b/src/RpcServer/RpcServer.Blockchain.cs index 0d2c662fb..9cc1abaaf 100644 --- a/src/RpcServer/RpcServer.Blockchain.cs +++ b/src/RpcServer/RpcServer.Blockchain.cs @@ -233,7 +233,6 @@ protected virtual JToken FindStorage(JArray _params) break; } - JObject j = new(); j["key"] = Convert.ToBase64String(iter.Current.Key.Key.Span); j["value"] = Convert.ToBase64String(iter.Current.Value.Value.Span); @@ -241,7 +240,6 @@ protected virtual JToken FindStorage(JArray _params) i++; } json["truncated"] = hasMore; - } json["next"] = start + i;