Skip to content

Commit

Permalink
Merge pull request #76 from make-software/feature/CMW-810/add_support…
Browse files Browse the repository at this point in the history
…_node_v1.5.5

Feature/cmw 810/add support node v1.5.5
  • Loading branch information
koltsov-iv authored Feb 1, 2024
2 parents b0ce6d8 + b7fa6ba commit 4e71f20
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 21 deletions.
3 changes: 3 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type ClientInformational interface {
// GetAccountInfoByBlochHeight returns a JSON representation of an Account from the network.
// The blockHeight must refer to a Block after the Account's creation, or the method will return an empty response.
GetAccountInfoByBlochHeight(ctx context.Context, blockHeight uint64, pub keypair.PublicKey) (StateGetAccountInfo, error)
// GetAccountInfo returns a JSON representation of an Account from the network.
// This is the most generic interface.
GetAccountInfo(ctx context.Context, blockIdentifier *ParamBlockIdentifier, accountIdentifier AccountIdentifier) (StateGetAccountInfo, error)

// GetBlockLatest returns the latest types.Block from the network.
GetBlockLatest(ctx context.Context) (ChainGetBlockResult, error)
Expand Down
24 changes: 15 additions & 9 deletions rpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

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

Expand Down Expand Up @@ -76,9 +77,9 @@ type ParamStateRootHash struct {
}

type ParamQueryGlobalState struct {
StateIdentifier ParamQueryGlobalStateID `json:"state_identifier"`
Key string `json:"key"`
Path []string `json:"path,omitempty"`
StateIdentifier *ParamQueryGlobalStateID `json:"state_identifier,omitempty"`
Key string `json:"key"`
Path []string `json:"path,omitempty"`
}

type ParamQueryGlobalStateID struct {
Expand All @@ -87,34 +88,39 @@ type ParamQueryGlobalStateID struct {
BlockHeight *uint64 `json:"BlockHeight,omitempty"`
}

func NewQueryGlobalStateParam(key string, path []string, id ParamQueryGlobalStateID) ParamQueryGlobalState {
func NewQueryGlobalStateParam(key string, path []string, id *ParamQueryGlobalStateID) ParamQueryGlobalState {
return ParamQueryGlobalState{StateIdentifier: id, Key: key, Path: path}
}

type ParamGetAccountInfoBalance struct {
PublicKey keypair.PublicKey `json:"public_key"`
AccountIdentifier string `json:"account_identifier"`
ParamBlockIdentifier
}

type AccountIdentifier struct {
AccountHash *key.AccountHash
PublicKey *keypair.PublicKey
}

type PutDeployRequest struct {
Deploy types.Deploy `json:"deploy"`
}

type BlockIdentifier struct {
Hash string `json:"Hash,omitempty"`
Hash *string `json:"Hash,omitempty"`
Height *uint64 `json:"Height,omitempty"`
}

type ParamBlockIdentifier struct {
BlockIdentifier BlockIdentifier `json:"block_identifier"`
BlockIdentifier *BlockIdentifier `json:"block_identifier"`
}

func NewParamBlockByHeight(height uint64) ParamBlockIdentifier {
return ParamBlockIdentifier{BlockIdentifier: BlockIdentifier{Height: &height}}
return ParamBlockIdentifier{BlockIdentifier: &BlockIdentifier{Height: &height}}
}

func NewParamBlockByHash(hash string) ParamBlockIdentifier {
return ParamBlockIdentifier{BlockIdentifier: BlockIdentifier{Hash: hash}}
return ParamBlockIdentifier{BlockIdentifier: &BlockIdentifier{Hash: &hash}}
}

type ParamDictionaryIdentifier struct {
Expand Down
35 changes: 23 additions & 12 deletions rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,52 @@ func (c *client) GetStateItem(ctx context.Context, stateRootHash *string, key st

func (c *client) QueryGlobalStateByBlockHash(ctx context.Context, blockHash, key string, path []string) (QueryGlobalStateResult, error) {
var result QueryGlobalStateResult
return result, c.processRequest(ctx, MethodQueryGlobalState, NewQueryGlobalStateParam(key, path, ParamQueryGlobalStateID{
return result, c.processRequest(ctx, MethodQueryGlobalState, NewQueryGlobalStateParam(key, path, &ParamQueryGlobalStateID{
BlockHash: blockHash,
}), &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{
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) {
var result QueryGlobalStateResult
if stateRootHash == nil {
latestHashResult, err := c.GetStateRootHashLatest(ctx)
if err != nil {
return QueryGlobalStateResult{}, err
}
latestHashString := latestHashResult.StateRootHash.String()
stateRootHash = &latestHashString
return result, c.processRequest(ctx, MethodQueryGlobalState, NewQueryGlobalStateParam(key, path, nil), &result)
}
var result QueryGlobalStateResult
return result, c.processRequest(ctx, MethodQueryGlobalState, NewQueryGlobalStateParam(key, path, ParamQueryGlobalStateID{
return result, c.processRequest(ctx, MethodQueryGlobalState, NewQueryGlobalStateParam(key, path, &ParamQueryGlobalStateID{
StateRootHash: *stateRootHash,
}), &result)
}

func (c *client) GetAccountInfoByBlochHash(ctx context.Context, blockHash string, pub keypair.PublicKey) (StateGetAccountInfo, error) {
var result StateGetAccountInfo
return result, c.processRequest(ctx, MethodGetStateAccount, ParamGetAccountInfoBalance{PublicKey: pub, ParamBlockIdentifier: NewParamBlockByHash(blockHash)}, &result)
return result, c.processRequest(ctx, MethodGetStateAccount, ParamGetAccountInfoBalance{AccountIdentifier: pub.String(), ParamBlockIdentifier: NewParamBlockByHash(blockHash)}, &result)
}

func (c *client) GetAccountInfoByBlochHeight(ctx context.Context, blockHeight uint64, pub keypair.PublicKey) (StateGetAccountInfo, error) {
var result StateGetAccountInfo
return result, c.processRequest(ctx, MethodGetStateAccount, ParamGetAccountInfoBalance{PublicKey: pub, ParamBlockIdentifier: NewParamBlockByHeight(blockHeight)}, &result)
return result, c.processRequest(ctx, MethodGetStateAccount, ParamGetAccountInfoBalance{AccountIdentifier: pub.String(), ParamBlockIdentifier: NewParamBlockByHeight(blockHeight)}, &result)
}

func (c *client) GetAccountInfo(ctx context.Context, blockIdentifier *ParamBlockIdentifier, accountIdentifier AccountIdentifier) (StateGetAccountInfo, error) {
if blockIdentifier == nil {
blockIdentifier = &ParamBlockIdentifier{}
}
var accountParam string
if accountIdentifier.AccountHash != nil {
accountParam = accountIdentifier.AccountHash.ToPrefixedString()
} else if accountIdentifier.PublicKey != nil {
accountParam = accountIdentifier.PublicKey.String()
} else {
return StateGetAccountInfo{}, fmt.Errorf("account identifier is empty")
}
var result StateGetAccountInfo
return result, c.processRequest(ctx, MethodGetStateAccount, ParamGetAccountInfoBalance{AccountIdentifier: accountParam, ParamBlockIdentifier: *blockIdentifier}, &result)
}

func (c *client) GetDictionaryItem(ctx context.Context, stateRootHash *string, uref, key string) (StateGetDictionaryResult, error) {
Expand Down
48 changes: 48 additions & 0 deletions tests/rpc/integration/rpc_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build integration
// +build integration

package integration

import (
"context"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func GetRpcClient() rpc.Client {
url, found := os.LookupEnv("NODE_URL")
if !found {
panic("NODE_URL env variable is not set")
}
return rpc.NewClient(rpc.NewHttpHandler(url, http.DefaultClient))
}

func Test_DefaultClient_GetAccountInfo_ByPublicKey(t *testing.T) {
pubKey, err := casper.NewPublicKey("01018525deae6091abccab6704a0fa44e12c495eec9e8fe6929862e1b75580e715")
require.NoError(t, err)
res, err := GetRpcClient().GetAccountInfo(context.Background(), nil, rpc.AccountIdentifier{PublicKey: &pubKey})
require.NoError(t, err)
assert.Equal(t, "account-hash-bf06bdb1616050cea5862333d1f4787718f1011c95574ba92378419eefeeee59", res.Account.AccountHash.ToPrefixedString())
}

func Test_DefaultClient_GetAccountInfo_ByAccountKey(t *testing.T) {
accountKey, err := casper.NewAccountHash("account-hash-bf06bdb1616050cea5862333d1f4787718f1011c95574ba92378419eefeeee59")
require.NoError(t, err)
res, err := GetRpcClient().GetAccountInfo(context.Background(), nil, rpc.AccountIdentifier{AccountHash: &accountKey})
require.NoError(t, err)
assert.Equal(t, "account-hash-bf06bdb1616050cea5862333d1f4787718f1011c95574ba92378419eefeeee59", res.Account.AccountHash.ToPrefixedString())
}

func Test_DefaultClient_QueryStateByStateHash(t *testing.T) {
accountKey := "account-hash-bf06bdb1616050cea5862333d1f4787718f1011c95574ba92378419eefeeee59"
res, err := GetRpcClient().QueryGlobalStateByStateHash(context.Background(), nil, accountKey, nil)
require.NoError(t, err)
assert.NotEmpty(t, res.StoredValue.Account.AccountHash)
}

0 comments on commit 4e71f20

Please sign in to comment.