From 94c3bc2c53d5ea8fc7ca2acc315ed4e658f3624f Mon Sep 17 00:00:00 2001 From: David Hernando Date: Wed, 27 Dec 2023 11:53:25 +0100 Subject: [PATCH] Added changes to QueryGlobalState and GetAccountInfo methods to support changes in Casper node v1.5.5 Signed-off-by: David Hernando --- Casper.Network.SDK.Test/NctlGetXTest.cs | 20 +++++++ .../NctlQueryGlobalStateTest.cs | 10 +++- Casper.Network.SDK/JsonRpc/CasperMethods.cs | 52 +++++++++++++++++-- Casper.Network.SDK/NetCasperClient.cs | 36 ++++++++++--- 4 files changed, 108 insertions(+), 10 deletions(-) diff --git a/Casper.Network.SDK.Test/NctlGetXTest.cs b/Casper.Network.SDK.Test/NctlGetXTest.cs index 45e3213..5b740e4 100644 --- a/Casper.Network.SDK.Test/NctlGetXTest.cs +++ b/Casper.Network.SDK.Test/NctlGetXTest.cs @@ -54,6 +54,26 @@ public async Task GetAccountTest() var response = await _client.GetAccountInfo(_faucetKey.PublicKey, blockHeight); var accountInfo = response.Parse(); Assert.IsNotEmpty(accountInfo.Account.AccountHash.ToString()); + + var response2 = await _client.GetAccountInfo(_faucetKey.PublicKey.ToAccountHex(), blockHeight); + var accountInfo2 = response2.Parse(); + Assert.AreEqual(accountInfo.Account.AccountHash.ToString(), accountInfo2.Account.AccountHash.ToString()); + + var response3 = await _client.GetAccountInfo(new AccountHashKey(_faucetKey.PublicKey), blockHeight); + var accountInfo3 = response3.Parse(); + Assert.AreEqual(accountInfo.Account.AccountHash.ToString(), accountInfo3.Account.AccountHash.ToString()); + + var response4 = await _client.GetAccountInfo(_faucetKey.PublicKey, blockHash); + var accountInfo4 = response4.Parse(); + Assert.AreEqual(accountInfo.Account.AccountHash.ToString(), accountInfo4.Account.AccountHash.ToString()); + + var response5 = await _client.GetAccountInfo(_faucetKey.PublicKey.ToAccountHex(), blockHash); + var accountInfo5 = response5.Parse(); + Assert.AreEqual(accountInfo.Account.AccountHash.ToString(), accountInfo5.Account.AccountHash.ToString()); + + var response6 = await _client.GetAccountInfo(new AccountHashKey(_faucetKey.PublicKey), blockHash); + var accountInfo6 = response6.Parse(); + Assert.AreEqual(accountInfo.Account.AccountHash.ToString(), accountInfo6.Account.AccountHash.ToString()); var resp = await _client.GetAccountBalance(_faucetKey.PublicKey, stateRootHash); var accountBalance = resp.Parse(); diff --git a/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs b/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs index 6c96e3a..f1753d6 100644 --- a/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs +++ b/Casper.Network.SDK.Test/NctlQueryGlobalStateTest.cs @@ -16,11 +16,19 @@ public async Task QueryAccountHash() var accountHash = GlobalStateKey.FromString(_faucetKey.PublicKey.GetAccountHash()); var rpcResponse = await _client.QueryGlobalState(accountHash); - var account = rpcResponse.Parse().StoredValue.Account; + var result = rpcResponse.Parse(); + var account = result.StoredValue.Account; Assert.AreEqual(accountHash.ToHexString(), account.AccountHash.ToHexString()); _mainPurse = account.MainPurse; + + var rpcResponse2 = await _client.QueryGlobalState( + accountHash, + result.BlockHeader.StateRootHash); + var result2 = rpcResponse2.Parse(); + var account2 = result2.StoredValue.Account; + Assert.AreEqual(accountHash.ToHexString(), account2.AccountHash.ToHexString()); } [Test, Order(2)] diff --git a/Casper.Network.SDK/JsonRpc/CasperMethods.cs b/Casper.Network.SDK/JsonRpc/CasperMethods.cs index 5027d0d..7dfe949 100644 --- a/Casper.Network.SDK/JsonRpc/CasperMethods.cs +++ b/Casper.Network.SDK/JsonRpc/CasperMethods.cs @@ -1,6 +1,8 @@ +using System; using System.Collections.Generic; using Casper.Network.SDK.JsonRpc; using Casper.Network.SDK.Types; +using Org.BouncyCastle.Utilities.Encoders; namespace Casper.Network.SDK.JsonRpc { @@ -69,6 +71,48 @@ public class GetAccountInfo : RpcMethod /// /// The public key of the account. /// A block hash for which the information of the account is queried. Null for most recent information. + public GetAccountInfo(PublicKey publicKey, string blockHash = null) : base("state_get_account_info", blockHash) + { + this.Parameters.Add("account_identifier", publicKey); + } + + /// + /// Returns the information of an Account in the network. + /// + /// The public key of the account. + /// A block height for which the information of the account is queried. + public GetAccountInfo(PublicKey publicKey, int height) : base("state_get_account_info", height) + { + this.Parameters.Add("account_identifier", publicKey); + } + + /// + /// Returns the information of an Account in the network. + /// + /// The account hash of the account. + /// A block hash for which the information of the account is queried. Null for most recent information. + public GetAccountInfo(AccountHashKey accountHash, string blockHash = null) : base("state_get_account_info", blockHash) + { + this.Parameters.Add("account_identifier", accountHash.ToString()); + } + + /// + /// Returns the information of an Account in the network. + /// + /// The account hash of the account. + /// A block height for which the information of the account is queried. + public GetAccountInfo(AccountHashKey accountHash, int height) : base("state_get_account_info", height) + { + // this.Parameters.Add("account_identifier", Hex.ToHexString(accountHash.GetBytes())); + this.Parameters.Add("account_identifier", accountHash.ToString()); + } + + /// + /// Returns the information of an Account in the network. + /// + /// The public key of the account. + /// A block hash for which the information of the account is queried. Null for most recent information. + [Obsolete("For Casper node v1.5.5 or newer use the new method signature with PublicKey or AccountHashKey, ", false)] public GetAccountInfo(string publicKey, string blockHash = null) : base("state_get_account_info", blockHash) { this.Parameters.Add("public_key", publicKey); @@ -79,6 +123,7 @@ public GetAccountInfo(string publicKey, string blockHash = null) : base("state_g /// /// The public key of the account. /// A block height for which the information of the account is queried. + [Obsolete("For Casper node v1.5.5 or newer use the new method signature with PublicKey or AccountHashKey", false)] public GetAccountInfo(string publicKey, int height) : base("state_get_account_info", height) { this.Parameters.Add("public_key", publicKey); @@ -111,16 +156,17 @@ public class QueryGlobalState : RpcMethod /// A query to the global state that returns a stored value from the network. /// /// A global state key formatted as a string to query the value from the network. - /// A block hash, a block height or a state root hash value. + /// (Optional) A block hash, a block height or a state root hash value. /// The path components starting from the key as base (use '/' as separator). - public QueryGlobalState(string key, StateIdentifier stateIdentifier, string[] path = null) : base("query_global_state") + public QueryGlobalState(string key, StateIdentifier stateIdentifier = null, string[] path = null) : base("query_global_state") { this.Parameters = new Dictionary { - {"state_identifier", stateIdentifier.GetParam()}, {"path", path ?? new string[] { }}, {"key", key} }; + if(stateIdentifier != null) + this.Parameters.Add("state_identifier", stateIdentifier.GetParam()); } } diff --git a/Casper.Network.SDK/NetCasperClient.cs b/Casper.Network.SDK/NetCasperClient.cs index 507b214..d006453 100644 --- a/Casper.Network.SDK/NetCasperClient.cs +++ b/Casper.Network.SDK/NetCasperClient.cs @@ -114,7 +114,19 @@ public async Task> GetAuctionInfo(int blockHei public async Task> GetAccountInfo(PublicKey publicKey, string blockHash = null) { - var method = new GetAccountInfo(publicKey.ToAccountHex(), blockHash); + var method = new GetAccountInfo(publicKey, blockHash); + return await SendRpcRequestAsync(method); + } + + /// + /// Request the information of an Account in the network. + /// + /// The account hash of the account. + /// A block hash for which the information of the account is queried. Null for most recent information. + public async Task> GetAccountInfo(AccountHashKey accountHash, + string blockHash = null) + { + var method = new GetAccountInfo(accountHash, blockHash); return await SendRpcRequestAsync(method); } @@ -123,6 +135,7 @@ public async Task> GetAccountInfo(PublicKey pu /// /// The public key of the account formatted as a string. /// A block hash for which the information of the account is queried. Null for most recent information. + [Obsolete("For Casper node v1.5.5 or newer use the new method signature with PublicKey or AccountHashKey, ", false)] public async Task> GetAccountInfo(string publicKey, string blockHash = null) { @@ -137,7 +150,18 @@ public async Task> GetAccountInfo(string publi /// A block height for which the information of the account is queried. public async Task> GetAccountInfo(PublicKey publicKey, int blockHeight) { - var method = new GetAccountInfo(publicKey.ToAccountHex(), blockHeight); + var method = new GetAccountInfo(publicKey, blockHeight); + return await SendRpcRequestAsync(method); + } + + /// + /// Request the information of an Account in the network. + /// + /// The account hash of the account. + /// A block height for which the information of the account is queried. + public async Task> GetAccountInfo(AccountHashKey accountHash, int blockHeight) + { + var method = new GetAccountInfo(accountHash, blockHeight); return await SendRpcRequestAsync(method); } @@ -146,6 +170,7 @@ public async Task> GetAccountInfo(PublicKey pu /// /// The public key of the account formatted as an hex-string. /// A block height for which the information of the account is queried. + [Obsolete("For Casper node v1.5.5 or newer use the new method signature with PublicKey or AccountHashKey, ", false)] public async Task> GetAccountInfo(string publicKey, int blockHeight) { var method = new GetAccountInfo(publicKey, blockHeight); @@ -192,10 +217,9 @@ public async Task> QueryGlobalState(string k public async Task> QueryGlobalState(string key, string stateRootHash = null, string path = null) { - if (stateRootHash == null) - stateRootHash = await GetStateRootHash(); - - var method = new QueryGlobalState(key, StateIdentifier.WithStateRootHash(stateRootHash), path?.Split(new char[] {'/'})); + var method = new QueryGlobalState(key, + stateRootHash != null? StateIdentifier.WithStateRootHash(stateRootHash) : null, + path?.Split(new char[] {'/'})); return await SendRpcRequestAsync(method); }