Skip to content

Commit

Permalink
Add proxy types (#292)
Browse files Browse the repository at this point in the history
* types: Add entities from the keystore pallet

* keystore: Change enum to uint alias

* account_id: Add helper methods

* account_id: Add equal method

* proxy: Add proxy storage types

* proxy: Add name and value maps for proxy type

* types: Check bytes length when creating account ID

* types: Add more tests

* proxy: Add anchor management proxy type

* proxy: Implement encode/decode for ProxyStorageEntry and ProxyDefinition

* keystore: Add keystore events

* types: Rename uniques pallet vars

* tests: Fix proxy tests

* types: Add generic option

* proxy: Update proxy types

* types: Use [4]U8 for module error

* metadata: Use [4]U8 for metadata.FindError

* types: Use U128 for item IDs for uniques pallet events

* types: Export testing functions

* types: Fix lint errors

* option: Add HasValue method

* client: Add extra check for closed connections

* header: Remove OptionBlockNumber
  • Loading branch information
cdamian authored Sep 23, 2022
1 parent c4bbd50 commit e6e34e3
Show file tree
Hide file tree
Showing 144 changed files with 2,218 additions and 1,695 deletions.
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:generate mockery --name Client

package client

import (
Expand All @@ -25,8 +23,11 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
gethrpc "github.com/centrifuge/go-substrate-rpc-client/v4/gethrpc"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

//go:generate mockery --name Client

type Client interface {
// Call makes the call to RPC method with the provided args
// args must be encoded in the format RPC understands
Expand Down Expand Up @@ -73,7 +74,7 @@ func CallWithBlockHash(c Client, target interface{}, method string, blockHash *t
}
return nil
}
hexHash, err := types.Hex(*blockHash)
hexHash, err := codec.Hex(*blockHash)
if err != nil {
return err
}
Expand Down
30 changes: 23 additions & 7 deletions gethrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,33 @@ func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error
}

func (c *Client) write(ctx context.Context, msg interface{}) error {
if err := c.checkWriteConn(ctx); err != nil {
return err
}

if err := c.writeConn.Write(ctx, msg); err != nil {
c.writeConn = nil
return err
}

return nil
}

func (c *Client) checkWriteConn(ctx context.Context) error {
// The previous write failed. Try to establish a new connection.
if c.writeConn == nil {
if err := c.reconnect(ctx); err != nil {
return err
}
return c.reconnect(ctx)
}
err := c.writeConn.Write(ctx, msg)
if err != nil {
c.writeConn = nil

// Extra check to ensure that connection is not closed.
select {
case <-c.writeConn.Closed():
return c.reconnect(ctx)
case <-ctx.Done():
return ctx.Err()
default:
return nil
}
return err
}

func (c *Client) reconnect(ctx context.Context) error {
Expand Down
5 changes: 3 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

func Example_simpleConnect() {
Expand Down Expand Up @@ -132,7 +133,7 @@ func Example_listenToBalanceChange() {
}

var acc types.AccountInfo
if err = types.Decode(chng.StorageData, &acc); err != nil {
if err = codec.Decode(chng.StorageData, &acc); err != nil {
panic(err)
}

Expand Down Expand Up @@ -295,7 +296,7 @@ func Example_displaySystemEvents() {
set := <-sub.Chan()
// inner loop for the changes within one of those notifications
for _, chng := range set.Changes {
if !types.Eq(chng.StorageKey, key) || !chng.HasStorageData {
if !codec.Eq(chng.StorageKey, key) || !chng.HasStorageData {
// skip, we are only interested in events with content
continue
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/author/pending_extrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package author

import (
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// PendingExtrinsics returns all pending extrinsics, potentially grouped by sender
Expand All @@ -30,7 +31,7 @@ func (a *author) PendingExtrinsics() ([]types.Extrinsic, error) {

xts := make([]types.Extrinsic, len(res))
for i, re := range res {
err = types.DecodeFromHex(re, &xts[i])
err = codec.DecodeFromHex(re, &xts[i])
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/author/submit_and_watch_extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
gethrpc "github.com/centrifuge/go-substrate-rpc-client/v4/gethrpc"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// ExtrinsicStatusSubscription is a subscription established through one of the Client's subscribe methods.
Expand Down Expand Up @@ -68,7 +69,7 @@ func (a *author) SubmitAndWatchExtrinsic(xt types.Extrinsic) (*ExtrinsicStatusSu

c := make(chan types.ExtrinsicStatus)

enc, err := types.EncodeToHex(xt)
enc, err := codec.EncodeToHex(xt)
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions rpc/author/submit_extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package author

import "github.com/centrifuge/go-substrate-rpc-client/v4/types"
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// SubmitExtrinsic will submit a fully formatted extrinsic for block inclusion
func (a *author) SubmitExtrinsic(xt types.Extrinsic) (types.Hash, error) {
enc, err := types.EncodeToHex(xt)
enc, err := codec.EncodeToHex(xt)
if err != nil {
return types.Hash{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/author/submit_extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -84,7 +85,7 @@ func TestAuthor_SubmitExtrinsic(t *testing.T) {
continue
}

hex, err := types.Hex(res)
hex, err := codec.Hex(res)
assert.NoError(t, err)
assert.NotEmpty(t, hex)
break
Expand Down
3 changes: 2 additions & 1 deletion rpc/offchain/get_local_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// StorageKind ...
Expand All @@ -45,7 +46,7 @@ func (c *offchain) LocalStorageGet(kind StorageKind, key []byte) (*types.Storage
return nil, nil
}

b, err := types.HexDecodeString(res)
b, err := codec.HexDecodeString(res)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion rpc/state/get_child_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// GetChildKeys retreives the keys with the given prefix of a specific child storage
Expand All @@ -42,7 +43,7 @@ func (s *state) getChildKeys(childStorageKey, prefix types.StorageKey, blockHash

keys := make([]types.StorageKey, len(res))
for i, r := range res {
err = types.DecodeFromHex(r, &keys[i])
err = codec.DecodeFromHex(r, &keys[i])
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions rpc/state/get_child_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ import (
"testing"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/stretchr/testify/assert"
)

var prefix = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))[:8]
var prefix = types.NewStorageKey(codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))[:8]

func TestState_GetChildKeysLatest(t *testing.T) {
keys, err := testState.GetChildKeysLatest(childStorageKey, prefix)
assert.NoError(t, err)
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
assert.Equal(t, []types.StorageKey{codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
}

func TestState_GetChildKeys(t *testing.T) {
keys, err := testState.GetChildKeys(childStorageKey, prefix, mockSrv.blockHashLatest)
assert.NoError(t, err)
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
assert.Equal(t, []types.StorageKey{codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
}
7 changes: 4 additions & 3 deletions rpc/state/get_child_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// GetChildStorage retreives the child storage for a key and decodes them into the provided interface. Ok is true if the
Expand All @@ -32,7 +33,7 @@ func (s *state) GetChildStorage(childStorageKey, key types.StorageKey, target in
if len(*raw) == 0 {
return false, nil
}
return true, types.Decode(*raw, target)
return true, codec.Decode(*raw, target)
}

// GetChildStorageLatest retreives the child storage for a key for the latest block height and decodes them into the
Expand All @@ -45,7 +46,7 @@ func (s *state) GetChildStorageLatest(childStorageKey, key types.StorageKey, tar
if len(*raw) == 0 {
return false, nil
}
return true, types.Decode(*raw, target)
return true, codec.Decode(*raw, target)
}

// GetChildStorageRaw retreives the child storage for a key as raw bytes, without decoding them
Expand All @@ -69,7 +70,7 @@ func (s *state) getChildStorageRaw(childStorageKey, key types.StorageKey, blockH
return nil, err
}

bz, err := types.HexDecodeString(res)
bz, err := codec.HexDecodeString(res)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions rpc/state/get_child_storage_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
"testing"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/stretchr/testify/assert"
)

func TestState_GetChildStorageHashLatest(t *testing.T) {
hash, err := testState.GetChildStorageHashLatest(childStorageKey, key)
assert.NoError(t, err)
assert.Equal(t, types.NewHash(types.MustHexDecodeString(mockSrv.childStorageTrieHashHex)), hash)
assert.Equal(t, types.NewHash(codec.MustHexDecodeString(mockSrv.childStorageTrieHashHex)), hash)
}

func TestState_GetChildStorageHash(t *testing.T) {
hash, err := testState.GetChildStorageHash(childStorageKey, key, mockSrv.blockHashLatest)
assert.NoError(t, err)
assert.Equal(t, types.NewHash(types.MustHexDecodeString(mockSrv.childStorageTrieHashHex)), hash)
assert.Equal(t, types.NewHash(codec.MustHexDecodeString(mockSrv.childStorageTrieHashHex)), hash)
}
5 changes: 3 additions & 2 deletions rpc/state/get_child_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"testing"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/stretchr/testify/assert"
)

var childStorageKey = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageKeyHex))
var key = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))
var childStorageKey = types.NewStorageKey(codec.MustHexDecodeString(mockSrv.childStorageKeyHex))
var key = types.NewStorageKey(codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))

func TestState_GetChildStorageLatest(t *testing.T) {
var decoded ChildStorageTrieTestVal
Expand Down
3 changes: 2 additions & 1 deletion rpc/state/get_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// GetKeys retreives the keys with the given prefix
Expand All @@ -40,7 +41,7 @@ func (s *state) getKeys(prefix types.StorageKey, blockHash *types.Hash) ([]types

keys := make([]types.StorageKey, len(res))
for i, r := range res {
err = types.DecodeFromHex(r, &keys[i])
err = codec.DecodeFromHex(r, &keys[i])
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions rpc/state/get_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ import (
"testing"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/stretchr/testify/assert"
)

func TestState_GetKeysLatest(t *testing.T) {
prefix := types.NewStorageKey(types.MustHexDecodeString(mockSrv.storageKeyHex))[:8]
prefix := types.NewStorageKey(codec.MustHexDecodeString(mockSrv.storageKeyHex))[:8]
keys, err := testState.GetKeysLatest(prefix)
assert.NoError(t, err)
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.storageKeyHex)}, keys)
assert.Equal(t, []types.StorageKey{codec.MustHexDecodeString(mockSrv.storageKeyHex)}, keys)
}

func TestState_GetKeys(t *testing.T) {
prefix := types.NewStorageKey(types.MustHexDecodeString(mockSrv.storageKeyHex))[:8]
prefix := types.NewStorageKey(codec.MustHexDecodeString(mockSrv.storageKeyHex))[:8]
keys, err := testState.GetKeys(prefix, mockSrv.blockHashLatest)
assert.NoError(t, err)
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.storageKeyHex)}, keys)
assert.Equal(t, []types.StorageKey{codec.MustHexDecodeString(mockSrv.storageKeyHex)}, keys)
}
3 changes: 2 additions & 1 deletion rpc/state/get_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// GetMetadata returns the metadata at the given block
Expand All @@ -39,6 +40,6 @@ func (s *state) getMetadata(blockHash *types.Hash) (*types.Metadata, error) {
}

var metadata types.Metadata
err = types.DecodeFromHex(res, &metadata)
err = codec.DecodeFromHex(res, &metadata)
return &metadata, err
}
7 changes: 4 additions & 3 deletions rpc/state/get_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

// GetStorage retreives the stored data and decodes them into the provided interface. Ok is true if the value is not
Expand All @@ -31,7 +32,7 @@ func (s *state) GetStorage(key types.StorageKey, target interface{}, blockHash t
if len(*raw) == 0 {
return false, nil
}
return true, types.Decode(*raw, target)
return true, codec.Decode(*raw, target)
}

// GetStorageLatest retreives the stored data for the latest block height and decodes them into the provided interface.
Expand All @@ -44,7 +45,7 @@ func (s *state) GetStorageLatest(key types.StorageKey, target interface{}) (ok b
if len(*raw) == 0 {
return false, nil
}
return true, types.Decode(*raw, target)
return true, codec.Decode(*raw, target)
}

// GetStorageRaw retreives the stored data as raw bytes, without decoding them
Expand All @@ -64,7 +65,7 @@ func (s *state) getStorageRaw(key types.StorageKey, blockHash *types.Hash) (*typ
return nil, err
}

bz, err := types.HexDecodeString(res)
bz, err := codec.HexDecodeString(res)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e6e34e3

Please sign in to comment.