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

RpcServer: added GetContractState by contract id support #813

Merged
merged 2 commits into from
Aug 30, 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
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);
Copy link
Member Author

@cschuchardt88 cschuchardt88 Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this doesn't use the "RpcContractState" class which is in the model folder. The function is using the function below it ("ContractStateFromJson", line 257). Should that be changed and updated? To use "RpcContractState" class? As you can see the new function and other one doesn't use the class; nothing does.

Copy link
Member

@superboyiii superboyiii Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use the same method name as GetContractStateAsync(string hash), otherwise it will get GetContractStateAsync(string hash) as default when GetRpcName(), then id will be an invaild param.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a overload for RcpClient and works fine. That's what we want to do. The method that was changed on RpcServer; GetContractState to allow contract id to be processed.

Example

var protocolSettings = ProtocolSettings.Load("config.json", true);
RpcClient rpcClient = new(new("http://127.0.0.1:20332"), "admin", "admin", protocolSettings);
var contractstate = await rpcClient.GetContractStateAsync(742).ConfigureAwait(false);
Console.WriteLine("ScriptHash is {0}.", contractstate.Hash);
Console.WriteLine("Press ENTER to exit . .  .");
Console.ReadLine();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just a fault on my code, it works as the expected!

}

public static ContractState ContractStateFromJson(JObject json)
{
return new ContractState
Expand Down
14 changes: 11 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