From b7c6db3607e72065273572a6c3fd8d082ce2ada5 Mon Sep 17 00:00:00 2001 From: Ilya Koltsov Date: Tue, 26 Sep 2023 18:17:48 +0100 Subject: [PATCH 1/2] Add interfaces for get dictionary items RPC request (CMW-524) --- rpc/client.go | 4 +++- rpc/error.go | 7 ++++--- rpc/request.go | 32 ++++++++++++++++++++++---------- rpc/rpc_client.go | 14 +++++++++++++- 4 files changed, 42 insertions(+), 15 deletions(-) diff --git a/rpc/client.go b/rpc/client.go index 3a873dd..d1b76ec 100644 --- a/rpc/client.go +++ b/rpc/client.go @@ -54,10 +54,12 @@ type ClientInformational interface { // GetDeployFinalizedApproval returns Deploy with the finalized approvals substituted. GetDeployFinalizedApproval(ctx context.Context, hash string) (InfoGetDeployResult, error) // GetDictionaryItem returns an item from a Dictionary. - // Every dictionary has a seed URef, findable by using a dictionary_identifier. // The address of a stored value is the blake2b hash of the seed URef and the byte representation of the dictionary key. // If the param stateRootHash is nil, the client will make an additional RPC call to retrieve the latest stateRootHash. GetDictionaryItem(ctx context.Context, stateRootHash *string, uref, key string) (StateGetDictionaryResult, error) + // GetDictionaryItemByIdentifier returns an item from a Dictionary. + // Every dictionary has a seed URef, findable by using a dictionary_identifier. + GetDictionaryItemByIdentifier(ctx context.Context, stateRootHash *string, identifier ParamDictionaryIdentifier) (StateGetDictionaryResult, error) // GetStateItem allows to get item from the global state // If the param stateRootHash is nil, the client will make an additional RPC call to retrieve the latest stateRootHash. // Deprecated: use QueryGlobalStateByStateHash instead diff --git a/rpc/error.go b/rpc/error.go index 2852d91..a701e55 100644 --- a/rpc/error.go +++ b/rpc/error.go @@ -1,14 +1,15 @@ package rpc import ( + "encoding/json" "fmt" "net/http" ) type RpcError struct { - Code int `json:"code"` - Message string `json:"message"` - Data string `json:"data,omitempty"` + Code int `json:"code"` + Message string `json:"message"` + Data json.RawMessage `json:"data,omitempty"` } func (h *RpcError) Error() string { diff --git a/rpc/request.go b/rpc/request.go index 9d1c044..fcfc104 100644 --- a/rpc/request.go +++ b/rpc/request.go @@ -117,16 +117,28 @@ func NewParamBlockByHash(hash string) ParamBlockIdentifier { return ParamBlockIdentifier{BlockIdentifier: BlockIdentifier{Hash: hash}} } -func NewParamStateDictionaryItem(stateRootHash, uref, key string) map[string]interface{} { - return map[string]interface{}{ - "state_root_hash": stateRootHash, - "dictionary_identifier": map[string]interface{}{ - "URef": map[string]string{ - "dictionary_item_key": key, - "seed_uref": uref, - }, - }, - } +type ParamDictionaryIdentifier struct { + AccountNamedKey *AccountNamedKey `json:"AccountNamedKey,omitempty"` + ContractNamedKey *ParamDictionaryIdentifierContractNamedKey `json:"ContractNamedKey,omitempty"` + URef *ParamDictionaryIdentifierURef `json:"URef,omitempty"` + Dictionary *string `json:"Dictionary,omitempty"` +} + +type AccountNamedKey struct { + Key string `json:"key"` + DictionaryName string `json:"dictionary_name"` + DictionaryItemKey string `json:"dictionary_item_key"` +} + +type ParamDictionaryIdentifierContractNamedKey struct { + Key string `json:"key"` + DictionaryName string `json:"dictionary_name"` + DictionaryItemKey string `json:"dictionary_item_key"` +} + +type ParamDictionaryIdentifierURef struct { + DictionaryItemKey string `json:"dictionary_item_key"` + SeedUref string `json:"seed_uref"` } type SpeculativeExecParams struct { diff --git a/rpc/rpc_client.go b/rpc/rpc_client.go index f004d14..71110c2 100644 --- a/rpc/rpc_client.go +++ b/rpc/rpc_client.go @@ -98,6 +98,15 @@ func (c *client) GetAccountInfoByBlochHeight(ctx context.Context, blockHeight ui } func (c *client) GetDictionaryItem(ctx context.Context, stateRootHash *string, uref, key string) (StateGetDictionaryResult, error) { + return c.GetDictionaryItemByIdentifier(ctx, stateRootHash, ParamDictionaryIdentifier{ + URef: &ParamDictionaryIdentifierURef{ + DictionaryItemKey: key, + SeedUref: uref, + }, + }) +} + +func (c *client) GetDictionaryItemByIdentifier(ctx context.Context, stateRootHash *string, identifier ParamDictionaryIdentifier) (StateGetDictionaryResult, error) { if stateRootHash == nil { latestHashResult, err := c.GetStateRootHashLatest(ctx) if err != nil { @@ -107,7 +116,10 @@ func (c *client) GetDictionaryItem(ctx context.Context, stateRootHash *string, u stateRootHash = &latestHashString } var result StateGetDictionaryResult - return result, c.processRequest(ctx, MethodGetDictionaryItem, NewParamStateDictionaryItem(*stateRootHash, uref, key), &result) + return result, c.processRequest(ctx, MethodGetDictionaryItem, map[string]interface{}{ + "state_root_hash": *stateRootHash, + "dictionary_identifier": identifier, + }, &result) } func (c *client) GetAccountBalance(ctx context.Context, stateRootHash *string, purseURef string) (StateGetBalanceResult, error) { From b09c74308bf1a88260cf4dff1f1bcc63e88cbd91 Mon Sep 17 00:00:00 2001 From: Ilya Koltsov Date: Wed, 27 Sep 2023 10:09:27 +0100 Subject: [PATCH 2/2] Fix balanceValue data type (CMW-524) --- rpc/response.go | 4 ++-- tests/rpc/rpc_client_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rpc/response.go b/rpc/response.go index 99d32cd..84dfb00 100644 --- a/rpc/response.go +++ b/rpc/response.go @@ -24,8 +24,8 @@ type StateGetAuctionInfoResult struct { } type StateGetBalanceResult struct { - ApiVersion string `json:"api_version"` - BalanceValue uint64 `json:"balance_value,string"` + ApiVersion string `json:"api_version"` + BalanceValue clvalue.UInt512 `json:"balance_value"` } type StateGetAccountInfo struct { diff --git a/tests/rpc/rpc_client_test.go b/tests/rpc/rpc_client_test.go index 4af15cb..d6ca964 100644 --- a/tests/rpc/rpc_client_test.go +++ b/tests/rpc/rpc_client_test.go @@ -204,7 +204,7 @@ func Test_DefaultClient_GetStateBalance(t *testing.T) { "uref-7b12008bb757ee32caefb3f7a1f77d9f659ee7a4e21ad4950c4e0294000492eb-007", ) require.NoError(t, err) - assert.NotEmpty(t, result.BalanceValue) + assert.Equal(t, "93000000000", result.BalanceValue.String()) } func Test_DefaultClient_GetStateBalance_WithEmptyStateRootHash(t *testing.T) {