Skip to content

Commit

Permalink
Merge pull request #33 from make-software/CMW-328/support_rpc_v2
Browse files Browse the repository at this point in the history
Cmw 328/support rpc v2
  • Loading branch information
koltsov-iv authored Jul 18, 2023
2 parents 4697905 + 7089e01 commit f692204
Show file tree
Hide file tree
Showing 14 changed files with 731 additions and 1,101 deletions.
8 changes: 8 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type ClientInformational interface {
GetAccountBalance(ctx context.Context, stateRootHash *string, purseURef string) (StateGetBalanceResult, error)
// GetDeploy retrieves a Deploy from a network. It requires a deploy_hash to query the Deploy.
GetDeploy(ctx context.Context, hash string) (InfoGetDeployResult, error)
// 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.
Expand All @@ -63,6 +65,8 @@ type ClientInformational interface {

// QueryGlobalStateByBlockHash allows for you to query for a value stored under certain keys in global state.
QueryGlobalStateByBlockHash(ctx context.Context, blockHash, key string, path []string) (QueryGlobalStateResult, error)
// QueryGlobalStateByBlockHeight allows for you to query for a value stored under certain keys in global state.
QueryGlobalStateByBlockHeight(ctx context.Context, blockHeight uint64, key string, path []string) (QueryGlobalStateResult, error)
// QueryGlobalStateByStateHash allows for you to query for a value stored under certain keys in global state.
// If the param stateRootHash is nil, the client will make an additional RPC call to retrieve the latest stateRootHash.
QueryGlobalStateByStateHash(ctx context.Context, stateRootHash *string, key string, path []string) (QueryGlobalStateResult, error)
Expand Down Expand Up @@ -108,6 +112,10 @@ type ClientInformational interface {
// GetPeers return a list of peers connected to the node on a Casper network.
// The responses return information specific to the queried node, and as such, will vary.
GetPeers(ctx context.Context) (InfoGetPeerResult, error)
// QueryBalance queries for balances under a given PurseIdentifier
QueryBalance(ctx context.Context, identifier PurseIdentifier) (QueryBalanceResult, error)
// GetChainspec returns the raw bytes of the chainspec.toml, accounts.toml and global_state.toml files as read at node startup.
GetChainspec(ctx context.Context) (InfoGetChainspecResult, error)
}

// ClientTransactional contains the description of account_put_deploy,
Expand Down
19 changes: 19 additions & 0 deletions rpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const (
MethodGetStatus Method = "info_get_status"
MethodGetPeers Method = "info_get_peers"
MethodPutDeploy Method = "account_put_deploy"
MethodSpeculativeExec Method = "speculative_exec"
MethodQueryBalance Method = "query_balance"
MethodInfoGetChainspec Method = "info_get_chainspec"
)

// RpcRequest is a wrapper struct for an RPC call method that can be serialized to JSON.
Expand Down Expand Up @@ -81,6 +84,7 @@ type ParamQueryGlobalState struct {
type ParamQueryGlobalStateID struct {
StateRootHash string `json:"StateRootHash,omitempty"`
BlockHash string `json:"BlockHash,omitempty"`
BlockHeight uint64 `json:"BlockHeight,omitempty"`
}

func NewQueryGlobalStateParam(key string, path []string, id ParamQueryGlobalStateID) ParamQueryGlobalState {
Expand Down Expand Up @@ -124,3 +128,18 @@ func NewParamStateDictionaryItem(stateRootHash, uref, key string) map[string]int
},
}
}

type SpeculativeExecParams struct {
Deploy types.Deploy `json:"deploy"`
BlockIdentifier *BlockIdentifier `json:"block_identifier,omitempty"`
}

type PurseIdentifier struct {
MainPurseUnderPublicKey *keypair.PublicKey `json:"main_purse_under_public_key,omitempty"`
MainPurseUnderAccountHash *string `json:"main_purse_under_account_hash,omitempty"`
PurseUref *string `json:"purse_uref,omitempty"`
}

type QueryBalanceRequest struct {
PurseIdentifier PurseIdentifier `json:"purse_identifier"`
}
37 changes: 37 additions & 0 deletions rpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"

"github.com/make-software/casper-go-sdk/types"
"github.com/make-software/casper-go-sdk/types/clvalue"
"github.com/make-software/casper-go-sdk/types/key"
"github.com/make-software/casper-go-sdk/types/keypair"
)
Expand Down Expand Up @@ -52,6 +53,8 @@ type InfoGetDeployResult struct {
ApiVersion string `json:"api_version"`
Deploy types.Deploy `json:"deploy"`
ExecutionResults []types.DeployExecutionResult `json:"execution_results"`
BlockHash *key.Hash `json:"block_hash,omitempty"`
BlockHeight *uint64 `json:"block_height,omitempty"`
}

type ChainGetEraInfoResult struct {
Expand Down Expand Up @@ -146,6 +149,20 @@ type InfoGetStatusResult struct {
StartingStateRootHash string `json:"starting_state_root_hash"`
// Time that passed since the node has started. format "2months 20days 22h 3m 21s 512ms"
Uptime string `json:"uptime"`
// Indicating the node's current operating mode
ReactorState string `json:"reactor_state"`
// Indicating the time the node last made progress
LastProgress types.Timestamp `json:"last_progress"`
// Indicating the highest contiguous sequence of the block chain for which the node has complete data
AvailableBlockRange struct {
Low uint64 `json:"low"`
High uint64 `json:"high"`
} `json:"available_block_range"`
// Indicating the state of the block synchronizer component
BlockSync struct {
Historical string `json:"historical,omitempty"`
Forward string `json:"forward,omitempty"`
} `json:"block_sync"`
}

// NodeNextUpgrade contains the information about the next protocol upgrade.
Expand All @@ -160,3 +177,23 @@ type PutDeployResult struct {
ApiVersion string `json:"api_version"`
DeployHash key.Hash `json:"deploy_hash"`
}

type SpeculativeExecResult struct {
ApiVersion string `json:"api_version"`
DeployHash key.Hash `json:"deploy_hash"`
ExecutionResult types.ExecutionResultStatus `json:"execution_result"`
}

type QueryBalanceResult struct {
ApiVersion string `json:"api_version"`
Balance clvalue.UInt512 `json:"balance"`
}

type InfoGetChainspecResult struct {
ApiVersion string `json:"api_version"`
ChainspecBytes struct {
ChainspecBytes string `json:"chainspec_bytes,omitempty"`
MaybeGenesisAccountsBytes string `json:"maybe_genesis_accounts_bytes,omitempty"`
MaybeGlobalStateBytes string `json:"maybe_global_state_bytes,omitempty"`
} `json:"chainspec_bytes"`
}
25 changes: 25 additions & 0 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func (c *client) GetDeploy(ctx context.Context, hash string) (InfoGetDeployResul
}, &result)
}

func (c *client) GetDeployFinalizedApproval(ctx context.Context, hash string) (InfoGetDeployResult, error) {
var result InfoGetDeployResult
return result, c.processRequest(ctx, MethodGetDeploy, map[string]interface{}{
"deploy_hash": hash,
"finalized_approvals": true,
}, &result)
}

func (c *client) GetStateItem(ctx context.Context, stateRootHash *string, key string, path []string) (StateGetItemResult, error) {
if stateRootHash == nil {
latestHashResult, err := c.GetStateRootHashLatest(ctx)
Expand All @@ -57,6 +65,13 @@ func (c *client) QueryGlobalStateByBlockHash(ctx context.Context, blockHash, key
}), &result)
}

func (c *client) QueryGlobalStateByBlockHeight(ctx context.Context, blockHeight uint64, key string, path []string) (QueryGlobalStateResult, error) {
var result QueryGlobalStateResult
return result, c.processRequest(ctx, MethodQueryGlobalState, NewQueryGlobalStateParam(key, path, ParamQueryGlobalStateID{
BlockHeight: blockHeight,
}), &result)
}

func (c *client) QueryGlobalStateByStateHash(ctx context.Context, stateRootHash *string, key string, path []string) (QueryGlobalStateResult, error) {
if stateRootHash == nil {
latestHashResult, err := c.GetStateRootHashLatest(ctx)
Expand Down Expand Up @@ -221,6 +236,16 @@ func (c *client) PutDeploy(ctx context.Context, deploy types.Deploy) (PutDeployR
return result, c.processRequest(ctx, MethodPutDeploy, PutDeployRequest{Deploy: deploy}, &result)
}

func (c *client) QueryBalance(ctx context.Context, identifier PurseIdentifier) (QueryBalanceResult, error) {
var result QueryBalanceResult
return result, c.processRequest(ctx, MethodQueryBalance, QueryBalanceRequest{identifier}, &result)
}

func (c *client) GetChainspec(ctx context.Context) (InfoGetChainspecResult, error) {
var result InfoGetChainspecResult
return result, c.processRequest(ctx, MethodInfoGetChainspec, nil, &result)
}

func (c *client) processRequest(ctx context.Context, method Method, params interface{}, result any) error {
request := DefaultRpcRequest(method, params)
if reqID := GetReqIdCtx(ctx); reqID != "0" {
Expand Down
43 changes: 43 additions & 0 deletions rpc/speculative_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package rpc

import (
"context"
"encoding/json"
"fmt"

"github.com/make-software/casper-go-sdk/types"
)

type SpeculativeClient struct {
handler Handler
}

func NewSpeculativeClient(handler Handler) *SpeculativeClient {
return &SpeculativeClient{handler: handler}
}

func (c SpeculativeClient) SpeculativeExec(ctx context.Context, deploy types.Deploy, identifier *BlockIdentifier) (SpeculativeExecResult, error) {
var result SpeculativeExecResult
request := DefaultRpcRequest(MethodSpeculativeExec, SpeculativeExecParams{
Deploy: deploy,
BlockIdentifier: identifier,
})
if reqID := GetReqIdCtx(ctx); reqID != "0" {
request.ID = reqID
}
resp, err := c.handler.ProcessCall(ctx, request)
if err != nil {
return SpeculativeExecResult{}, err
}

if resp.Error != nil {
return SpeculativeExecResult{}, fmt.Errorf("rpc call failed, details: %w", resp.Error)
}

err = json.Unmarshal(resp.Result, &result)
if err != nil {
return SpeculativeExecResult{}, fmt.Errorf("%w, details: %s", ErrResultUnmarshal, err.Error())
}

return result, nil
}
65 changes: 65 additions & 0 deletions tests/data/deploy/deploy_with_transfer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"hash": "ccb36e4cbf6fa1c19406d571e36862060d35af708dab89ad6d9b937556f25c2c",
"header": {
"account": "0115394d1f395a87dfed4ab62bbfbc91b573bbb2bffb2c8ebb9c240c51d95bcc4d",
"timestamp": "2023-07-12T14:59:40.406Z",
"ttl": "30m",
"gas_price": 1,
"body_hash": "568593b2b7860ff60d085b80fd7c1666a8ac43b41ad15296fda594e0aabae0f0",
"dependencies": [],
"chain_name": "casper-net-1"
},
"payment": {
"ModuleBytes": {
"module_bytes": "",
"args": [
[
"amount",
{
"cl_type": "U512",
"bytes": "0440787d01",
"parsed": "25000000"
}
]
]
}
},
"session": {
"Transfer": {
"args": [
[
"amount",
{
"cl_type": "U512",
"bytes": "0400f90295",
"parsed": "2500000000"
}
],
[
"target",
{
"cl_type": "PublicKey",
"bytes": "01bf65587c501685ca5d0cb5293509fa4cecdb4503c7364084e18e14339e46a46f",
"parsed": "01bf65587c501685ca5d0cb5293509fa4cecdb4503c7364084e18e14339e46a46f"
}
],
[
"id",
{
"cl_type": {
"Option": "U64"
},
"bytes": "010100000000000000",
"parsed": 1
}
]
]
}
},
"approvals": [
{
"signer": "0115394d1f395a87dfed4ab62bbfbc91b573bbb2bffb2c8ebb9c240c51d95bcc4d",
"signature": "01299f8c15f04c14cf68706b8d34f520826113cb776826628661d4c796ddcf1b4ee36b9bcd0b597800ac0501cb26cfa54abf8c0e4eaf7b498d5c283c8f1caacb0e"
}
]
}
Loading

0 comments on commit f692204

Please sign in to comment.