Skip to content

Commit

Permalink
Merge branch 'wss' of github.com:Liaojinghui/neo-modules into wss
Browse files Browse the repository at this point in the history
* 'wss' of github.com:Liaojinghui/neo-modules:
  RpcServer:  added GetContractState by contract id support (neo-project#813)
  rpc: add FindStorage (neo-project#805)
  • Loading branch information
Jim8y committed Sep 3, 2023
2 parents 0b2e51a + 1ec353b commit 14aad77
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/RpcClient/RpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ public async Task<ContractState> GetContractStateAsync(string hash)
return ContractStateFromJson((JObject)result);
}

/// <summary>
/// Queries contract information, according to the contract id.
/// </summary>
public async Task<ContractState> GetContractStateAsync(int id)
{
var result = await RpcSendAsync(GetRpcName(), id).ConfigureAwait(false);
return ContractStateFromJson((JObject)result);
}

public static ContractState ContractStateFromJson(JObject json)
{
return new ContractState
Expand Down
64 changes: 61 additions & 3 deletions src/RpcServer/RpcServer.Blockchain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,17 @@ protected virtual JToken GetBlockHeader(JArray _params)
[RpcMethod]
protected virtual JToken GetContractState(JArray _params)
{
UInt160 script_hash = ToScriptHash(_params[0].AsString());
ContractState contract = NativeContract.ContractManagement.GetContract(system.StoreView, script_hash);
return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract");
if (int.TryParse(_params[0].AsString(), out int contractId))
{
var contracts = NativeContract.ContractManagement.GetContractById(system.StoreView, contractId);
return contracts?.ToJson() ?? throw new RpcException(-100, "Unknown contract");
}
else
{
UInt160 script_hash = ToScriptHash(_params[0].AsString());
ContractState contract = NativeContract.ContractManagement.GetContract(system.StoreView, script_hash);
return contract?.ToJson() ?? throw new RpcException(-100, "Unknown contract");
}
}

private static UInt160 ToScriptHash(string keyword)
Expand Down Expand Up @@ -197,6 +205,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

0 comments on commit 14aad77

Please sign in to comment.