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

rpc: add FindStorage #805

Merged
merged 4 commits into from
Aug 21, 2023
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
50 changes: 50 additions & 0 deletions src/RpcServer/RpcServer.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,56 @@ 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 = settings.FindStoragePageSize;
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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/RpcServer/RpcServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<PackageId>Neo.Plugins.RpcServer</PackageId>
<RootNamespace>Neo.Plugins</RootNamespace>
</PropertyGroup>

</Project>
7 changes: 5 additions & 2 deletions src/RpcServer/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -60,7 +61,8 @@ public record RpcServerSettings
DisabledMethods = Array.Empty<string>(),
MaxConcurrentConnections = 40,
SessionEnabled = false,
SessionExpirationTime = TimeSpan.FromSeconds(60)
SessionExpirationTime = TimeSpan.FromSeconds(60),
FindStoragePageSize = 50
};

public static RpcServerSettings Load(IConfigurationSection section) => new()
Expand All @@ -80,7 +82,8 @@ public record RpcServerSettings
DisabledMethods = section.GetSection("DisabledMethods").GetChildren().Select(p => p.Get<string>()).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)
};
}
}
3 changes: 2 additions & 1 deletion src/RpcServer/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"MaxStackSize": 65535,
"DisabledMethods": [ "openwallet" ],
"SessionEnabled": false,
"SessionExpirationTime": 60
"SessionExpirationTime": 60,
"FindStoragePageSize": 50
}
]
}
Expand Down