Skip to content

Commit e6e34e3

Browse files
authored
Add proxy types (#292)
* 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
1 parent c4bbd50 commit e6e34e3

144 files changed

Lines changed: 2218 additions & 1695 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

client/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
//go:generate mockery --name Client
18-
1917
package client
2018

2119
import (
@@ -25,8 +23,11 @@ import (
2523
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
2624
gethrpc "github.com/centrifuge/go-substrate-rpc-client/v4/gethrpc"
2725
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
26+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2827
)
2928

29+
//go:generate mockery --name Client
30+
3031
type Client interface {
3132
// Call makes the call to RPC method with the provided args
3233
// args must be encoded in the format RPC understands
@@ -73,7 +74,7 @@ func CallWithBlockHash(c Client, target interface{}, method string, blockHash *t
7374
}
7475
return nil
7576
}
76-
hexHash, err := types.Hex(*blockHash)
77+
hexHash, err := codec.Hex(*blockHash)
7778
if err != nil {
7879
return err
7980
}

gethrpc/client.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,33 @@ func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error
480480
}
481481

482482
func (c *Client) write(ctx context.Context, msg interface{}) error {
483+
if err := c.checkWriteConn(ctx); err != nil {
484+
return err
485+
}
486+
487+
if err := c.writeConn.Write(ctx, msg); err != nil {
488+
c.writeConn = nil
489+
return err
490+
}
491+
492+
return nil
493+
}
494+
495+
func (c *Client) checkWriteConn(ctx context.Context) error {
483496
// The previous write failed. Try to establish a new connection.
484497
if c.writeConn == nil {
485-
if err := c.reconnect(ctx); err != nil {
486-
return err
487-
}
498+
return c.reconnect(ctx)
488499
}
489-
err := c.writeConn.Write(ctx, msg)
490-
if err != nil {
491-
c.writeConn = nil
500+
501+
// Extra check to ensure that connection is not closed.
502+
select {
503+
case <-c.writeConn.Closed():
504+
return c.reconnect(ctx)
505+
case <-ctx.Done():
506+
return ctx.Err()
507+
default:
508+
return nil
492509
}
493-
return err
494510
}
495511

496512
func (c *Client) reconnect(ctx context.Context) error {

main_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
2626
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
2727
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
28+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2829
)
2930

3031
func Example_simpleConnect() {
@@ -132,7 +133,7 @@ func Example_listenToBalanceChange() {
132133
}
133134

134135
var acc types.AccountInfo
135-
if err = types.Decode(chng.StorageData, &acc); err != nil {
136+
if err = codec.Decode(chng.StorageData, &acc); err != nil {
136137
panic(err)
137138
}
138139

@@ -295,7 +296,7 @@ func Example_displaySystemEvents() {
295296
set := <-sub.Chan()
296297
// inner loop for the changes within one of those notifications
297298
for _, chng := range set.Changes {
298-
if !types.Eq(chng.StorageKey, key) || !chng.HasStorageData {
299+
if !codec.Eq(chng.StorageKey, key) || !chng.HasStorageData {
299300
// skip, we are only interested in events with content
300301
continue
301302
}

rpc/author/pending_extrinsics.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package author
1818

1919
import (
2020
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
21+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2122
)
2223

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

3132
xts := make([]types.Extrinsic, len(res))
3233
for i, re := range res {
33-
err = types.DecodeFromHex(re, &xts[i])
34+
err = codec.DecodeFromHex(re, &xts[i])
3435
if err != nil {
3536
return nil, err
3637
}

rpc/author/submit_and_watch_extrinsic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
2424
gethrpc "github.com/centrifuge/go-substrate-rpc-client/v4/gethrpc"
2525
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
26+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2627
)
2728

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

6970
c := make(chan types.ExtrinsicStatus)
7071

71-
enc, err := types.EncodeToHex(xt)
72+
enc, err := codec.EncodeToHex(xt)
7273
if err != nil {
7374
return nil, err
7475
}

rpc/author/submit_extrinsic.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
package author
1818

19-
import "github.com/centrifuge/go-substrate-rpc-client/v4/types"
19+
import (
20+
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
21+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
22+
)
2023

2124
// SubmitExtrinsic will submit a fully formatted extrinsic for block inclusion
2225
func (a *author) SubmitExtrinsic(xt types.Extrinsic) (types.Hash, error) {
23-
enc, err := types.EncodeToHex(xt)
26+
enc, err := codec.EncodeToHex(xt)
2427
if err != nil {
2528
return types.Hash{}, err
2629
}

rpc/author/submit_extrinsic_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
2525
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
2626
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
27+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2728
"github.com/stretchr/testify/assert"
2829
)
2930

@@ -84,7 +85,7 @@ func TestAuthor_SubmitExtrinsic(t *testing.T) {
8485
continue
8586
}
8687

87-
hex, err := types.Hex(res)
88+
hex, err := codec.Hex(res)
8889
assert.NoError(t, err)
8990
assert.NotEmpty(t, hex)
9091
break

rpc/offchain/get_local_storage.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
23+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2324
)
2425

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

48-
b, err := types.HexDecodeString(res)
49+
b, err := codec.HexDecodeString(res)
4950
if err != nil {
5051
return nil, err
5152
}

rpc/state/get_child_keys.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package state
1919
import (
2020
"github.com/centrifuge/go-substrate-rpc-client/v4/client"
2121
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
22+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2223
)
2324

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

4344
keys := make([]types.StorageKey, len(res))
4445
for i, r := range res {
45-
err = types.DecodeFromHex(r, &keys[i])
46+
err = codec.DecodeFromHex(r, &keys[i])
4647
if err != nil {
4748
return nil, err
4849
}

rpc/state/get_child_keys_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ import (
2020
"testing"
2121

2222
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
23+
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
2324
"github.com/stretchr/testify/assert"
2425
)
2526

26-
var prefix = types.NewStorageKey(types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))[:8]
27+
var prefix = types.NewStorageKey(codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex))[:8]
2728

2829
func TestState_GetChildKeysLatest(t *testing.T) {
2930
keys, err := testState.GetChildKeysLatest(childStorageKey, prefix)
3031
assert.NoError(t, err)
31-
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
32+
assert.Equal(t, []types.StorageKey{codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
3233
}
3334

3435
func TestState_GetChildKeys(t *testing.T) {
3536
keys, err := testState.GetChildKeys(childStorageKey, prefix, mockSrv.blockHashLatest)
3637
assert.NoError(t, err)
37-
assert.Equal(t, []types.StorageKey{types.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
38+
assert.Equal(t, []types.StorageKey{codec.MustHexDecodeString(mockSrv.childStorageTrieKeyHex)}, keys)
3839
}

0 commit comments

Comments
 (0)