diff --git a/client/client.go b/client/client.go index 359da9127..0f8cfb195 100644 --- a/client/client.go +++ b/client/client.go @@ -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 ( @@ -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 @@ -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 } diff --git a/gethrpc/client.go b/gethrpc/client.go index 76a658cc8..baf746898 100644 --- a/gethrpc/client.go +++ b/gethrpc/client.go @@ -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 { diff --git a/main_test.go b/main_test.go index a47da20a0..3630ed2f8 100644 --- a/main_test.go +++ b/main_test.go @@ -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() { @@ -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) } @@ -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 } diff --git a/rpc/author/pending_extrinsics.go b/rpc/author/pending_extrinsics.go index 47b4b27f6..164c25962 100644 --- a/rpc/author/pending_extrinsics.go +++ b/rpc/author/pending_extrinsics.go @@ -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 @@ -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 } diff --git a/rpc/author/submit_and_watch_extrinsic.go b/rpc/author/submit_and_watch_extrinsic.go index 1a151ef94..8a9f16918 100644 --- a/rpc/author/submit_and_watch_extrinsic.go +++ b/rpc/author/submit_and_watch_extrinsic.go @@ -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. @@ -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 } diff --git a/rpc/author/submit_extrinsic.go b/rpc/author/submit_extrinsic.go index 7a7ba2f23..3c51099e1 100644 --- a/rpc/author/submit_extrinsic.go +++ b/rpc/author/submit_extrinsic.go @@ -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 } diff --git a/rpc/author/submit_extrinsic_test.go b/rpc/author/submit_extrinsic_test.go index 456b25797..e19d10f3e 100644 --- a/rpc/author/submit_extrinsic_test.go +++ b/rpc/author/submit_extrinsic_test.go @@ -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" ) @@ -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 diff --git a/rpc/offchain/get_local_storage.go b/rpc/offchain/get_local_storage.go index 918c5a054..ece195d46 100644 --- a/rpc/offchain/get_local_storage.go +++ b/rpc/offchain/get_local_storage.go @@ -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 ... @@ -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 } diff --git a/rpc/state/get_child_keys.go b/rpc/state/get_child_keys.go index 19802fe41..b87d3e89c 100644 --- a/rpc/state/get_child_keys.go +++ b/rpc/state/get_child_keys.go @@ -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 @@ -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 } diff --git a/rpc/state/get_child_keys_test.go b/rpc/state/get_child_keys_test.go index 5cf0fa6ef..9fd3671d5 100644 --- a/rpc/state/get_child_keys_test.go +++ b/rpc/state/get_child_keys_test.go @@ -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) } diff --git a/rpc/state/get_child_storage.go b/rpc/state/get_child_storage.go index a19739668..a15b4fff8 100644 --- a/rpc/state/get_child_storage.go +++ b/rpc/state/get_child_storage.go @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/rpc/state/get_child_storage_hash_test.go b/rpc/state/get_child_storage_hash_test.go index 6295f7bde..a26ade1ac 100644 --- a/rpc/state/get_child_storage_hash_test.go +++ b/rpc/state/get_child_storage_hash_test.go @@ -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) } diff --git a/rpc/state/get_child_storage_test.go b/rpc/state/get_child_storage_test.go index ef8e44726..680140b3a 100644 --- a/rpc/state/get_child_storage_test.go +++ b/rpc/state/get_child_storage_test.go @@ -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 diff --git a/rpc/state/get_keys.go b/rpc/state/get_keys.go index 46f2cf337..d2fe18d91 100644 --- a/rpc/state/get_keys.go +++ b/rpc/state/get_keys.go @@ -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 @@ -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 } diff --git a/rpc/state/get_keys_test.go b/rpc/state/get_keys_test.go index d7d7a2b26..144483b4b 100644 --- a/rpc/state/get_keys_test.go +++ b/rpc/state/get_keys_test.go @@ -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) } diff --git a/rpc/state/get_metadata.go b/rpc/state/get_metadata.go index e82cb7c84..e392933cf 100644 --- a/rpc/state/get_metadata.go +++ b/rpc/state/get_metadata.go @@ -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 @@ -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 } diff --git a/rpc/state/get_storage.go b/rpc/state/get_storage.go index 058435b6a..7c889be67 100644 --- a/rpc/state/get_storage.go +++ b/rpc/state/get_storage.go @@ -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 @@ -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. @@ -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 @@ -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 } diff --git a/rpc/state/get_storage_hash_test.go b/rpc/state/get_storage_hash_test.go index 711170990..f631cb53a 100644 --- a/rpc/state/get_storage_hash_test.go +++ b/rpc/state/get_storage_hash_test.go @@ -20,23 +20,24 @@ 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_GetStorageHashLatest(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString("0x3a636f6465")) + key := types.NewStorageKey(codec.MustHexDecodeString("0x3a636f6465")) hash, err := testState.GetStorageHashLatest(key) assert.NoError(t, err) var expected types.Hash - copy(expected[:], types.MustHexDecodeString(mockSrv.storageHashHex)) + copy(expected[:], codec.MustHexDecodeString(mockSrv.storageHashHex)) assert.Equal(t, expected, hash) } func TestState_GetStorageHash(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString("0x3a636f6465")) + key := types.NewStorageKey(codec.MustHexDecodeString("0x3a636f6465")) hash, err := testState.GetStorageHash(key, mockSrv.blockHashLatest) assert.NoError(t, err) var expected types.Hash - copy(expected[:], types.MustHexDecodeString(mockSrv.storageHashHex)) + copy(expected[:], codec.MustHexDecodeString(mockSrv.storageHashHex)) assert.Equal(t, expected, hash) } diff --git a/rpc/state/get_storage_size_test.go b/rpc/state/get_storage_size_test.go index 727a48f79..2001bf1a7 100644 --- a/rpc/state/get_storage_size_test.go +++ b/rpc/state/get_storage_size_test.go @@ -20,18 +20,19 @@ 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_GetStorageSizeLatest(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString("0x3a636f6465")) + key := types.NewStorageKey(codec.MustHexDecodeString("0x3a636f6465")) size, err := testState.GetStorageSizeLatest(key) assert.NoError(t, err) assert.Equal(t, mockSrv.storageSize, size) } func TestState_GetStorageSize(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString("0x3a636f6465")) + key := types.NewStorageKey(codec.MustHexDecodeString("0x3a636f6465")) size, err := testState.GetStorageSize(key, mockSrv.blockHashLatest) assert.NoError(t, err) assert.Equal(t, mockSrv.storageSize, size) diff --git a/rpc/state/get_storage_test.go b/rpc/state/get_storage_test.go index 74a7e3336..78fee8b46 100644 --- a/rpc/state/get_storage_test.go +++ b/rpc/state/get_storage_test.go @@ -20,12 +20,13 @@ 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_GetStorageLatest(t *testing.T) { var decoded types.U64 - ok, err := testState.GetStorageLatest(types.MustHexDecodeString(mockSrv.storageKeyHex), &decoded) + ok, err := testState.GetStorageLatest(codec.MustHexDecodeString(mockSrv.storageKeyHex), &decoded) assert.NoError(t, err) assert.True(t, ok) assert.Equal(t, types.U64(0x5d892db8), decoded) @@ -33,7 +34,7 @@ func TestState_GetStorageLatest(t *testing.T) { func TestState_GetStorage(t *testing.T) { var decoded types.U64 - ok, err := testState.GetStorage(types.MustHexDecodeString(mockSrv.storageKeyHex), &decoded, mockSrv.blockHashLatest) + ok, err := testState.GetStorage(codec.MustHexDecodeString(mockSrv.storageKeyHex), &decoded, mockSrv.blockHashLatest) assert.NoError(t, err) assert.True(t, ok) assert.Equal(t, types.U64(0x5d892db8), decoded) @@ -47,13 +48,13 @@ func TestState_GetStorageEmpty(t *testing.T) { } func TestState_GetStorageRawLatest(t *testing.T) { - data, err := testState.GetStorageRawLatest(types.MustHexDecodeString(mockSrv.storageKeyHex)) + data, err := testState.GetStorageRawLatest(codec.MustHexDecodeString(mockSrv.storageKeyHex)) assert.NoError(t, err) assert.Equal(t, mockSrv.storageDataHex, data.Hex()) } func TestState_GetStorageRaw(t *testing.T) { - data, err := testState.GetStorageRaw(types.MustHexDecodeString(mockSrv.storageKeyHex), mockSrv.blockHashLatest) + data, err := testState.GetStorageRaw(codec.MustHexDecodeString(mockSrv.storageKeyHex), mockSrv.blockHashLatest) assert.NoError(t, err) assert.Equal(t, mockSrv.storageDataHex, data.Hex()) } diff --git a/rpc/state/query_storage_at_test.go b/rpc/state/query_storage_at_test.go index e72e234e1..8d6550801 100644 --- a/rpc/state/query_storage_at_test.go +++ b/rpc/state/query_storage_at_test.go @@ -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_QueryStorageAtLatest(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString(mockSrv.storageKeyHex)) + key := types.NewStorageKey(codec.MustHexDecodeString(mockSrv.storageKeyHex)) data, err := testState.QueryStorageAtLatest([]types.StorageKey{key}) assert.NoError(t, err) assert.Equal(t, mockSrv.storageChangeSets, data) } func TestState_QueryStorageAt(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString(mockSrv.storageKeyHex)) - hash := types.NewHash(types.MustHexDecodeString("0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe")) + key := types.NewStorageKey(codec.MustHexDecodeString(mockSrv.storageKeyHex)) + hash := types.NewHash(codec.MustHexDecodeString("0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe")) data, err := testState.QueryStorageAt([]types.StorageKey{key}, hash) assert.NoError(t, err) assert.Equal(t, mockSrv.storageChangeSets, data) diff --git a/rpc/state/query_storage_test.go b/rpc/state/query_storage_test.go index 1d0f45719..d47dfd778 100644 --- a/rpc/state/query_storage_test.go +++ b/rpc/state/query_storage_test.go @@ -20,20 +20,21 @@ 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_QueryStorageLatest(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString(mockSrv.storageKeyHex)) - hash := types.NewHash(types.MustHexDecodeString("0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe")) + key := types.NewStorageKey(codec.MustHexDecodeString(mockSrv.storageKeyHex)) + hash := types.NewHash(codec.MustHexDecodeString("0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe")) data, err := testState.QueryStorageLatest([]types.StorageKey{key}, hash) assert.NoError(t, err) assert.Equal(t, mockSrv.storageChangeSets, data) } func TestState_QueryStorage(t *testing.T) { - key := types.NewStorageKey(types.MustHexDecodeString(mockSrv.storageKeyHex)) - hash := types.NewHash(types.MustHexDecodeString("0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe")) + key := types.NewStorageKey(codec.MustHexDecodeString(mockSrv.storageKeyHex)) + hash := types.NewHash(codec.MustHexDecodeString("0xdd1816b6f6889f46e23b0d6750bc441af9dad0fda8bae90677c1708d01035fbe")) data, err := testState.QueryStorage([]types.StorageKey{key}, hash, mockSrv.blockHashLatest) assert.NoError(t, err) assert.Equal(t, mockSrv.storageChangeSets, data) diff --git a/rpc/state/state_test.go b/rpc/state/state_test.go index 7a1d8b19d..f5bc92826 100644 --- a/rpc/state/state_test.go +++ b/rpc/state/state_test.go @@ -24,6 +24,7 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/client" "github.com/centrifuge/go-substrate-rpc-client/v4/rpcmocksrv" "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) var testState State @@ -189,8 +190,8 @@ var mockSrv = MockSrv{ childStorageTrieKeyHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0", childStorageTrieValueHex: "0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d09c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a921000000", //nolint:lll childStorageTrieValue: ChildStorageTrieTestVal{ - Key: types.NewHash(types.MustHexDecodeString("0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0")), //nolint:lll - OtherID: types.NewHash(types.MustHexDecodeString("0x9c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a9")), //nolint:lll + Key: types.NewHash(codec.MustHexDecodeString("0x81914b11321c39f8728981888024196b616142cc0369234775b20b539aaf29d0")), //nolint:lll + OtherID: types.NewHash(codec.MustHexDecodeString("0x9c1705d98d059a2d7f5faa89277ee5d0a38cc455f8b5fdf38fda471e988cb8a9")), //nolint:lll Value: types.NewU32(0x21), }, childStorageTrieSize: 68, diff --git a/rpc/system/system_test.go b/rpc/system/system_test.go index 5609b6b21..ecff246a9 100644 --- a/rpc/system/system_test.go +++ b/rpc/system/system_test.go @@ -20,6 +20,8 @@ import ( "os" "testing" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + "github.com/centrifuge/go-substrate-rpc-client/v4/client" "github.com/centrifuge/go-substrate-rpc-client/v4/rpcmocksrv" "github.com/centrifuge/go-substrate-rpc-client/v4/types" @@ -92,7 +94,7 @@ var mockSrv = MockSrv{ name: "test-node", networkState: types.NetworkState{PeerID: "my-peer-id"}, peers: []types.PeerInfo{{PeerID: "another-peer-id", Roles: "Role", ProtocolVersion: 42, - BestHash: types.NewHash(types.MustHexDecodeString("0xabcd")), BestNumber: 420}}, + BestHash: types.NewHash(codec.MustHexDecodeString("0xabcd")), BestNumber: 420}}, properties: types.ChainProperties{IsTokenDecimals: true, AsTokenDecimals: 18, IsTokenSymbol: true, AsTokenSymbol: "GSRPCCOIN"}, version: "My version", diff --git a/signature/signature_test.go b/signature/signature_test.go index 8d54c9e43..340813f88 100644 --- a/signature/signature_test.go +++ b/signature/signature_test.go @@ -21,7 +21,7 @@ import ( "testing" . "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" ) @@ -39,7 +39,7 @@ func TestKeyRingPairFromSecretPhrase_SubstrateAddress(t *testing.T) { assert.Equal(t, KeyringPair{ URI: testSecretPhrase, Address: testAddressSS58, - PublicKey: types.MustHexDecodeString(testPubKey), + PublicKey: codec.MustHexDecodeString(testPubKey), }, p) } @@ -50,7 +50,7 @@ func TestKeyRingPairFromSecretPhrase_PolkadotAddress(t *testing.T) { assert.Equal(t, KeyringPair{ URI: testSecretPhrase, Address: testPolkadotAddressSS58, - PublicKey: types.MustHexDecodeString(testPubKey), + PublicKey: codec.MustHexDecodeString(testPubKey), }, p) } @@ -61,7 +61,7 @@ func TestKeyRingPairFromSecretPhrase_KusamaAddress(t *testing.T) { assert.Equal(t, KeyringPair{ URI: testSecretPhrase, Address: testKusamaAddressSS58, - PublicKey: types.MustHexDecodeString(testPubKey), + PublicKey: codec.MustHexDecodeString(testPubKey), }, p) } @@ -77,7 +77,7 @@ func TestKeyringPairFromSecretSeed(t *testing.T) { assert.Equal(t, KeyringPair{ URI: testSecretSeed, Address: testAddressSS58, - PublicKey: types.MustHexDecodeString(testPubKey), + PublicKey: codec.MustHexDecodeString(testPubKey), }, p) } @@ -88,7 +88,7 @@ func TestKeyringPairFromSecretSeedAndNetwork(t *testing.T) { assert.Equal(t, KeyringPair{ URI: testSecretSeed, Address: testAddressSS58, - PublicKey: types.MustHexDecodeString(testPubKey), + PublicKey: codec.MustHexDecodeString(testPubKey), }, p) } diff --git a/teste2e/author_submit_and_watch_extrinsic_test.go b/teste2e/author_submit_and_watch_extrinsic_test.go index 272c27b63..1e97cafa0 100644 --- a/teste2e/author_submit_and_watch_extrinsic_test.go +++ b/teste2e/author_submit_and_watch_extrinsic_test.go @@ -24,6 +24,7 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/rpc/author" "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" ) @@ -101,7 +102,7 @@ func TestAuthor_SubmitAndWatchExtrinsic(t *testing.T) { fmt.Printf("%#v\n", status) if status.IsInBlock { - assert.False(t, types.Eq(status.AsInBlock, types.ExtrinsicStatus{}.AsInBlock), + assert.False(t, codec.Eq(status.AsInBlock, types.ExtrinsicStatus{}.AsInBlock), "expected AsFinalized not to be empty") return } diff --git a/teste2e/chain_subscribe_beefy_justifications_test.go b/teste2e/chain_subscribe_beefy_justifications_test.go index 28a8fbfa1..8af964308 100644 --- a/teste2e/chain_subscribe_beefy_justifications_test.go +++ b/teste2e/chain_subscribe_beefy_justifications_test.go @@ -25,6 +25,7 @@ import ( gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/config" "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" "github.com/stretchr/testify/assert" ) @@ -54,7 +55,7 @@ func TestChain_SubscribeBeefyJustifications(t *testing.T) { fmt.Printf("encoded msg: %#v\n", msg) s := &types.SignedCommitment{} - err := types.DecodeFromHex(msg.(string), s) + err := codec.DecodeFromHex(msg.(string), s) if err != nil { panic(err) } diff --git a/teste2e/state_subscribe_storage_test.go b/teste2e/state_subscribe_storage_test.go index cc55d583a..c06dc6c87 100644 --- a/teste2e/state_subscribe_storage_test.go +++ b/teste2e/state_subscribe_storage_test.go @@ -24,6 +24,7 @@ import ( gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" "github.com/centrifuge/go-substrate-rpc-client/v4/config" "github.com/centrifuge/go-substrate-rpc-client/v4/types" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" "github.com/stretchr/testify/assert" ) @@ -90,7 +91,7 @@ func TestState_SubscribeStorage_Events(t *testing.T) { case set := <-sub.Chan(): fmt.Printf("%#v\n", set) 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 } diff --git a/types/account_id.go b/types/account_id.go index 9e5604d3a..b49ee0bde 100644 --- a/types/account_id.go +++ b/types/account_id.go @@ -16,7 +16,15 @@ package types -import "github.com/centrifuge/go-substrate-rpc-client/v4/scale" +import ( + "bytes" + "encoding/json" + "errors" + "strings" + + "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + "github.com/ethereum/go-ethereum/common/hexutil" +) type OptionAccountID struct { option @@ -56,12 +64,74 @@ func (o *OptionAccountID) Unwrap() (ok bool, value AccountID) { return o.hasValue, o.value } +const ( + AccountIDLen = 32 +) + // AccountID represents a public key (an 32 byte array) -type AccountID [32]byte +type AccountID [AccountIDLen]byte + +func (a *AccountID) ToBytes() []byte { + if a == nil { + return nil + } + + b := a[:] + + return b +} + +func (a *AccountID) ToHexString() string { + if a == nil { + return "" + } + + return hexutil.Encode(a.ToBytes()) +} + +func (a *AccountID) Equal(accountID *AccountID) bool { + return bytes.Equal(a.ToBytes(), accountID.ToBytes()) +} + +func (a AccountID) MarshalJSON() ([]byte, error) { + return json.Marshal(a.ToHexString()) +} + +func (a *AccountID) UnmarshalJSON(data []byte) error { + accID, err := NewAccountIDFromHexString(strings.Trim(string(data), "\"")) + + if err != nil { + return err + } + + *a = *accID + + return nil +} + +var ( + ErrInvalidAccountIDBytes = errors.New("invalid account ID bytes") +) // NewAccountID creates a new AccountID type -func NewAccountID(b []byte) AccountID { +func NewAccountID(b []byte) (*AccountID, error) { + if len(b) != AccountIDLen { + return nil, ErrInvalidAccountIDBytes + } + a := AccountID{} + copy(a[:], b) - return a + + return &a, nil +} + +func NewAccountIDFromHexString(accountIDHex string) (*AccountID, error) { + b, err := hexutil.Decode(accountIDHex) + + if err != nil { + return nil, err + } + + return NewAccountID(b) } diff --git a/types/account_id_test.go b/types/account_id_test.go index 00629d4aa..ce7e5b10a 100644 --- a/types/account_id_test.go +++ b/types/account_id_test.go @@ -17,18 +17,20 @@ package types_test import ( + "encoding/json" + "fmt" "testing" - "github.com/stretchr/testify/assert" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" ) var ( - optionAccountIDFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(o *OptionAccountID, c fuzz.Continue) { + optionAccountIDFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(o *OptionAccountID, c fuzz.Continue) { if c.RandBool() { *o = NewOptionAccountIDEmpty() return @@ -41,28 +43,41 @@ var ( } ) +func newTestAccountID() AccountID { + accID, err := NewAccountID(testAccountIDBytes) + + if err != nil { + panic(fmt.Errorf("couldn't create test account ID: %w", err)) + } + + return *accID +} + func TestOptionAccountID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionAccountID](t, 100, optionAccountIDFuzzOpts...) - assertEncodeEmptyObj[OptionAccountID](t, 1) + AssertRoundTripFuzz[OptionAccountID](t, 100, optionAccountIDFuzzOpts...) + AssertEncodeEmptyObj[OptionAccountID](t, 1) } +var testAccountIDBytes = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32} + func TestOptionAccountID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ - {NewOptionAccountID(NewAccountID([]byte{171, 18, 52})), MustHexDecodeString("0x01ab12340000000000000000000000000000000000000000000000000000000000")}, + AssertEncode(t, []EncodingAssert{ + {NewOptionAccountID(newTestAccountID()), MustHexDecodeString("0x010102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")}, {NewOptionAccountIDEmpty(), MustHexDecodeString("0x00")}, }) } func TestOptionAccountID_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ - {MustHexDecodeString("0x01ab12340000000000000000000000000000000000000000000000000000000000"), NewOptionAccountID(NewAccountID([]byte{171, 18, 52}))}, + AssertDecode(t, []DecodingAssert{ + {MustHexDecodeString("0x010102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"), NewOptionAccountID(newTestAccountID())}, {MustHexDecodeString("0x00"), NewOptionAccountIDEmpty()}, }) } func TestOptionAccountID_OptionMethods(t *testing.T) { o := NewOptionAccountIDEmpty() - o.SetSome(NewAccountID([]byte("acc-id"))) + + o.SetSome(newTestAccountID()) ok, v := o.Unwrap() assert.True(t, ok) @@ -70,62 +85,93 @@ func TestOptionAccountID_OptionMethods(t *testing.T) { o.SetNone() + var a AccountID + ok, v = o.Unwrap() assert.False(t, ok) - assert.Equal(t, NewAccountID([]byte{}), v) + assert.Equal(t, a, v) } func TestAccountID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[AccountID](t, 100, withNilChance(0.01)) - assertDecodeNilData[AccountID](t) - assertEncodeEmptyObj[AccountID](t, 32) + AssertRoundTripFuzz[AccountID](t, 100, WithNilChance(0.01)) + AssertDecodeNilData[AccountID](t) + AssertEncodeEmptyObj[AccountID](t, 32) } func TestAccountID_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ - {NewAccountID([]byte{}), 32}, - {NewAccountID([]byte{7, 6, 5, 4, 3, 2, 1, 0}), 32}, + AssertEncodedLength(t, []EncodedLengthAssert{ + {newTestAccountID(), 32}, }) } func TestAccountID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ - {NewAccountID([]byte{0, 0, 0}), MustHexDecodeString("0x0000000000000000000000000000000000000000000000000000000000000000")}, //nolint:lll - {NewAccountID([]byte{171, 18, 52}), MustHexDecodeString("0xab12340000000000000000000000000000000000000000000000000000000000")}, //nolint:lll + AssertEncode(t, []EncodingAssert{ + {newTestAccountID(), MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20")}, //nolint:lll }) } func TestAccountID_Hash(t *testing.T) { - assertHash(t, []hashAssert{ - {NewAccountID([]byte{0, 42, 254}), MustHexDecodeString( - "0x7834db8eb04aefe8272c32d8160ce4fa3cb31fc95882e5bd53860715731c8198")}, - {NewAccountID([]byte{0, 0}), MustHexDecodeString( - "0x89eb0d6a8a691dae2cd15ed0369931ce0a949ecafa5c3f93f8121833646e15c3")}, + AssertHash(t, []HashAssert{ + { + newTestAccountID(), + MustHexDecodeString("0x441edc56cebc8e285d02267aa650819f15add7b06ef9b41b2690128dce655924"), + }, }) } func TestAccountID_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ - {NewAccountID([]byte{0, 0, 0}), "0x0000000000000000000000000000000000000000000000000000000000000000"}, - {NewAccountID([]byte{171, 18, 52}), "0xab12340000000000000000000000000000000000000000000000000000000000"}, + AssertEncodeToHex(t, []EncodeToHexAssert{ + {newTestAccountID(), "0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"}, }) } func TestAccountID_String(t *testing.T) { - assertString(t, []stringAssert{ - {NewAccountID([]byte{0, 0, 0}), "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]"}, - {NewAccountID([]byte{171, 18, 52}), "[171 18 52 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]"}, + AssertString(t, []StringAssert{ + {newTestAccountID(), "[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32]"}, }) } func TestAccountID_Eq(t *testing.T) { - assertEq(t, []eqAssert{ - {NewAccountID([]byte{1, 0, 0}), NewAccountID([]byte{1, 0}), true}, - {NewAccountID([]byte{0, 0, 1}), NewAccountID([]byte{0, 1}), false}, - {NewAccountID([]byte{0, 0, 0}), NewAccountID([]byte{0, 0}), true}, - {NewAccountID([]byte{12, 48, 255}), NewAccountID([]byte{12, 48, 255}), true}, - {NewAccountID([]byte{0}), NewAccountID([]byte{0}), true}, - {NewAccountID([]byte{1}), NewBool(true), false}, - {NewAccountID([]byte{0}), NewBool(false), false}, + b := testAccountIDBytes[:31] + b = append(b, 11) + + accID, err := NewAccountID(b) + assert.NoError(t, err) + + AssertEq(t, []EqAssert{ + {newTestAccountID(), newTestAccountID(), true}, + {newTestAccountID(), accID, false}, }) } + +func TestAccountID_ToBytes(t *testing.T) { + accID, err := NewAccountID(testAccountIDBytes) + assert.NoError(t, err) + + assert.Equal(t, accID.ToBytes(), testAccountIDBytes) +} + +func TestAccountID_ToHexString(t *testing.T) { + accID, err := NewAccountID(testAccountIDBytes) + assert.NoError(t, err) + + hb, err := Hex(testAccountIDBytes) + assert.NoError(t, err) + + assert.Equal(t, hb, accID.ToHexString()) +} + +func TestAccountID_JSONMarshalUnmarshal(t *testing.T) { + accID, err := NewAccountID(testAccountIDBytes) + assert.NoError(t, err) + + b, err := json.Marshal(accID) + assert.NoError(t, err) + + var res AccountID + + err = json.Unmarshal(b, &res) + assert.NoError(t, err) + + assert.True(t, accID.Equal(&res)) +} diff --git a/types/account_index_test.go b/types/account_index_test.go index 5c062f76f..cd990e08e 100644 --- a/types/account_index_test.go +++ b/types/account_index_test.go @@ -20,47 +20,49 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestAccountIndex_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[AccountIndex](t, 100) - assertDecodeNilData[AccountID](t) - assertEncodeEmptyObj[AccountID](t, 32) + AssertRoundTripFuzz[AccountIndex](t, 100) + AssertDecodeNilData[AccountID](t) + AssertEncodeEmptyObj[AccountID](t, 32) } func TestAccountIndex_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewAccountIndex(336794129), 4}, }) } func TestAccountIndex_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewAccountIndex(336794129), MustHexDecodeString("0x11121314")}, }) } func TestAccountIndex_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewAccountIndex(336794129), MustHexDecodeString( "0xa6730c0d3a95e0ff2068fa9a6ecf82c42c494c8c2cdd65379c898a4b88dd7138")}, }) } func TestAccountIndex_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewAccountIndex(336794129), "0x11121314"}, }) } func TestAccountIndex_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewAccountIndex(336794129), "336794129"}, }) } func TestAccountIndex_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewAccountIndex(336794129), NewAccountIndex(336794129), true}, {NewAccountIndex(336794129), NewAccountIndex(12), false}, {NewAccountIndex(336794129), NewBool(false), false}, diff --git a/types/account_info_test.go b/types/account_info_test.go index 55a7926eb..59c501825 100644 --- a/types/account_info_test.go +++ b/types/account_info_test.go @@ -20,47 +20,49 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestAccountInfoV4_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[AccountInfoV4](t, 100) - assertDecodeNilData[AccountInfoV4](t) - assertEncodeEmptyObj[AccountInfoV4](t, 9) + AssertRoundTripFuzz[AccountInfoV4](t, 100) + AssertDecodeNilData[AccountInfoV4](t) + AssertEncodeEmptyObj[AccountInfoV4](t, 9) } func TestAccountInfoV4_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewAccountInfoV4([]byte{1, 2, 3}, 13), 12}, }) } func TestAccountInfoV4_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewAccountInfoV4([]byte{1, 2, 3}, 13), MustHexDecodeString("0x0c0102030d00000000000000")}, }) } func TestAccountInfoV4_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewAccountInfoV4([]byte{1, 2, 3}, 13), MustHexDecodeString( "0x4fac0dfeb9b4efd2518c762e7d097fafaffaf8d56a2e784f9fc9919c22277804")}, }) } func TestAccountInfoV4_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewAccountInfoV4([]byte{1, 2, 3}, 13), "0x0c0102030d00000000000000"}, }) } func TestAccountInfoV4_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewAccountInfoV4([]byte{1, 2, 3}, 13), "{[1 2 3] 13}"}, }) } func TestAccountInfoV4_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewAccountInfoV4([]byte{1, 2, 3}, 13), NewAccountInfoV4([]byte{1, 2, 3}, 13), true}, {NewAccountInfoV4([]byte{1, 2, 3}, 13), NewAccountInfoV4([]byte{1, 2, 2}, 13), false}, {NewAccountInfoV4([]byte{1, 2, 3}, 13), NewBool(false), false}, diff --git a/types/address.go b/types/address.go index 11c16b2fb..a4b5072a9 100644 --- a/types/address.go +++ b/types/address.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // Address is a wrapper around an AccountId or an AccountIndex. It is encoded with a prefix in case of an AccountID. @@ -32,20 +33,26 @@ type Address struct { } // NewAddressFromAccountID creates an Address from the given AccountID (public key) -func NewAddressFromAccountID(b []byte) Address { +func NewAddressFromAccountID(b []byte) (Address, error) { + accountID, err := NewAccountID(b) + + if err != nil { + return Address{}, nil + } + return Address{ IsAccountID: true, - AsAccountID: NewAccountID(b), - } + AsAccountID: *accountID, + }, nil } // NewAddressFromHexAccountID creates an Address from the given hex string that contains an AccountID (public key) func NewAddressFromHexAccountID(str string) (Address, error) { - b, err := HexDecodeString(str) + b, err := codec.HexDecodeString(str) if err != nil { return Address{}, err } - return NewAddressFromAccountID(b), nil + return NewAddressFromAccountID(b) } // NewAddressFromAccountIndex creates an Address from the given AccountIndex @@ -68,7 +75,11 @@ func (a *Address) Decode(decoder scale.Decoder) error { if err != nil { return err } - a.AsAccountID = NewAccountID(append([]byte{b}, sm[:]...)) // Push b back to the front + accountID, err := NewAccountID(append([]byte{b}, sm[:]...)) // Push b back to the front + if err != nil { + return err + } + a.AsAccountID = *accountID a.IsAccountID = true return nil } diff --git a/types/address_test.go b/types/address_test.go index fc015743c..28c04d776 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -19,15 +19,16 @@ package types_test import ( "bytes" "encoding/binary" + "fmt" "testing" - "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - - fuzz "github.com/google/gofuzz" - "github.com/btcsuite/btcutil/base58" "github.com/centrifuge/go-substrate-rpc-client/v4/hash" + "github.com/centrifuge/go-substrate-rpc-client/v4/scale" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) @@ -46,8 +47,8 @@ func TestChecksum(t *testing.T) { } var ( - addressFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(a *Address, c fuzz.Continue) { + addressFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(a *Address, c fuzz.Continue) { if c.RandBool() { a.IsAccountID = true c.Fuzz(&a.AsAccountID) @@ -61,22 +62,27 @@ var ( } ) +func newTestAddress() Address { + addr, err := NewAddressFromAccountID(testAccountIDBytes) + + if err != nil { + panic(fmt.Errorf("couldn't create test address: %w", err)) + } + + return addr +} + func TestAddress_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Address](t, 100, addressFuzzOpts...) - assertDecodeNilData[Address](t) - assertEncodeEmptyObj[Address](t, 1) + AssertRoundTripFuzz[Address](t, 100, addressFuzzOpts...) + AssertDecodeNilData[Address](t) + AssertEncodeEmptyObj[Address](t, 1) } func TestAddress_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ - {NewAddressFromAccountID([]byte{ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }), []byte{ - 255, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }}, + AssertEncode(t, []EncodingAssert{ + {newTestAddress(), + MustHexDecodeString("0xff0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f0b"), + }, {NewAddressFromAccountIndex(binary.BigEndian.Uint32([]byte{ 17, 18, 19, 20, })), []byte{ @@ -94,14 +100,10 @@ func TestAddress_Encode(t *testing.T) { func TestAddress_EncodeWithOptions(t *testing.T) { SetSerDeOptions(SerDeOptions{NoPalletIndices: true}) defer SetSerDeOptions(SerDeOptions{NoPalletIndices: false}) - assertEncode(t, []encodingAssert{ - {NewAddressFromAccountID([]byte{ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }), []byte{ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }}, + AssertEncode(t, []EncodingAssert{ + {newTestAddress(), + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f0b"), + }, {NewAddressFromAccountIndex(binary.BigEndian.Uint32([]byte{ 17, 18, 19, 20, })), []byte{ @@ -111,15 +113,10 @@ func TestAddress_EncodeWithOptions(t *testing.T) { } func TestAddress_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ - {[]byte{ - 255, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }, NewAddressFromAccountID([]byte{ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - })}, + AssertDecode(t, []DecodingAssert{ + {MustHexDecodeString("0xff0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f0b"), + newTestAddress(), + }, {[]byte{ 253, 20, 19, 18, 17, // order is reversed because scale uses little endian }, NewAddressFromAccountIndex(binary.BigEndian.Uint32([]byte{ @@ -144,20 +141,9 @@ func TestAddress_Decode(t *testing.T) { func TestAddress_DecodeWithOptions(t *testing.T) { SetSerDeOptions(SerDeOptions{NoPalletIndices: true}) defer SetSerDeOptions(SerDeOptions{NoPalletIndices: false}) - assertDecode(t, []decodingAssert{ - {[]byte{ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }, NewAddressFromAccountID([]byte{ - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - })}, - {[]byte{ - 254, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - }, NewAddressFromAccountID([]byte{ - 254, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, - })}, + AssertDecode(t, []DecodingAssert{ + {MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f0b"), + newTestAddress(), + }, }) } diff --git a/types/balance_status_test.go b/types/balance_status_test.go index f8aa97663..07f5d36e4 100644 --- a/types/balance_status_test.go +++ b/types/balance_status_test.go @@ -20,16 +20,16 @@ import ( "bytes" "testing" - fuzz "github.com/google/gofuzz" - "github.com/centrifuge/go-substrate-rpc-client/v4/scale" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) var ( - balanceStatusFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(b *BalanceStatus, c fuzz.Continue) { + balanceStatusFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(b *BalanceStatus, c fuzz.Continue) { *b = BalanceStatus(c.Intn(2)) }), } @@ -42,7 +42,7 @@ func TestBalanceStatusEncodeDecode(t *testing.T) { err := decoder.Decode(&bs0) assert.Error(t, err) - assertRoundTripFuzz[BalanceStatus](t, 100, balanceStatusFuzzOpts...) - assertDecodeNilData[BalanceStatus](t) - assertEncodeEmptyObj[BalanceStatus](t, 1) + AssertRoundTripFuzz[BalanceStatus](t, 100, balanceStatusFuzzOpts...) + AssertDecodeNilData[BalanceStatus](t) + AssertEncodeEmptyObj[BalanceStatus](t, 1) } diff --git a/types/beefy.go b/types/beefy.go index 2d63bc997..1ee1fa0f8 100644 --- a/types/beefy.go +++ b/types/beefy.go @@ -19,6 +19,7 @@ package types import ( "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // PayloadItem ... @@ -193,7 +194,7 @@ func makeChunks(slice []byte, chunkSize int) [][]byte { // UnmarshalText deserializes hex string into a SignedCommitment. // Used for decoding JSON-RPC subscription messages (beefy_subscribeJustifications) func (s *SignedCommitment) UnmarshalText(text []byte) error { - return DecodeFromHex(string(text), s) + return codec.DecodeFromHex(string(text), s) } func (o OptionalSignedCommitment) Encode(encoder scale.Encoder) error { diff --git a/types/beefy_test.go b/types/beefy_test.go index 85a18166f..61fe45deb 100644 --- a/types/beefy_test.go +++ b/types/beefy_test.go @@ -20,9 +20,10 @@ import ( "fmt" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) @@ -41,7 +42,7 @@ func TestBeefySignature(t *testing.T) { assert.True(t, sig.IsSome()) ok, _ := sig.Unwrap() assert.True(t, ok) - assertRoundtrip(t, sig) + AssertRoundtrip(t, sig) } func makeCommitment() (*Commitment, error) { @@ -84,7 +85,7 @@ func makeLargeCommitment() (*Commitment, error) { func TestCommitment_Encode(t *testing.T) { c, err := makeCommitment() assert.NoError(t, err) - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {c, MustHexDecodeString("0x046d68343048656c6c6f20576f726c6421050000000000000000000000")}, }) } @@ -100,10 +101,10 @@ func TestCommitment_Decode(t *testing.T) { c, err := makeCommitment() assert.NoError(t, err) - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { - input: MustHexDecodeString("0x046d68343048656c6c6f20576f726c6421050000000000000000000000"), - expected: *c, + Input: MustHexDecodeString("0x046d68343048656c6c6f20576f726c6421050000000000000000000000"), + Expected: *c, }, }) } @@ -112,7 +113,7 @@ func TestCommitment_EncodeDecode(t *testing.T) { c, err := makeCommitment() assert.NoError(t, err) - assertRoundtrip(t, *c) + AssertRoundtrip(t, *c) } func TestSignedCommitment_Decode(t *testing.T) { @@ -129,10 +130,10 @@ func TestSignedCommitment_Decode(t *testing.T) { }, } - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { - input: MustHexDecodeString("0x046d68343048656c6c6f20576f726c642105000000000000000000000004300400000008558455ad81279df0795cc985580e4fb75d72d948d1107b2ac80a09abed4da8480c746cc321f2319a5e99a830e314d10dd3cd68ce3dc0c33c86e99bcb7816f9ba012d6e1f8105c337a86cdd9aaacdc496577f3db8c55ef9e6fd48f2c5c05a2274707491635d8ba3df64f324575b7b2a34487bca2324b6a0046395a71681be3d0c2a00"), - expected: s, + Input: MustHexDecodeString("0x046d68343048656c6c6f20576f726c642105000000000000000000000004300400000008558455ad81279df0795cc985580e4fb75d72d948d1107b2ac80a09abed4da8480c746cc321f2319a5e99a830e314d10dd3cd68ce3dc0c33c86e99bcb7816f9ba012d6e1f8105c337a86cdd9aaacdc496577f3db8c55ef9e6fd48f2c5c05a2274707491635d8ba3df64f324575b7b2a34487bca2324b6a0046395a71681be3d0c2a00"), + Expected: s, }, }) } @@ -157,18 +158,18 @@ func TestSignedCommitment_EncodeDecode(t *testing.T) { }, } - assertRoundtrip(t, s) + AssertRoundtrip(t, s) } func TestBeefySignature_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[BeefySignature](t, 100) - assertDecodeNilData[BeefySignature](t) - assertEncodeEmptyObj[BeefySignature](t, 65) + AssertRoundTripFuzz[BeefySignature](t, 100) + AssertDecodeNilData[BeefySignature](t) + AssertEncodeEmptyObj[BeefySignature](t, 65) } var ( - optionBeefySignatureFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(o *OptionBeefySignature, c fuzz.Continue) { + optionBeefySignatureFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(o *OptionBeefySignature, c fuzz.Continue) { if c.RandBool() { *o = NewOptionBeefySignatureEmpty() return @@ -183,6 +184,6 @@ var ( ) func TestOptionBeefySignature_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionBeefySignature](t, 100, optionBeefySignatureFuzzOpts...) - assertEncodeEmptyObj[OptionBeefySignature](t, 1) + AssertRoundTripFuzz[OptionBeefySignature](t, 100, optionBeefySignatureFuzzOpts...) + AssertEncodeEmptyObj[OptionBeefySignature](t, 1) } diff --git a/types/body_test.go b/types/body_test.go index 9b95efaf7..978d904c0 100644 --- a/types/body_test.go +++ b/types/body_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -49,8 +50,8 @@ var ( IsJudicial: true, } - bodyIDFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(b *BodyID, c fuzz.Continue) { + bodyIDFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(b *BodyID, c fuzz.Continue) { switch c.Intn(7) { case 0: b.IsUnit = true @@ -74,13 +75,13 @@ var ( ) func TestBodyID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[BodyID](t, 1000, bodyIDFuzzOpts...) - assertDecodeNilData[BodyID](t) - assertEncodeEmptyObj[BodyID](t, 0) + AssertRoundTripFuzz[BodyID](t, 1000, bodyIDFuzzOpts...) + AssertDecodeNilData[BodyID](t) + AssertEncodeEmptyObj[BodyID](t, 0) } func TestBodyID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {bodyID1, MustHexDecodeString("0x00")}, {bodyID2, MustHexDecodeString("0x010404")}, {bodyID3, MustHexDecodeString("0x0206000000")}, @@ -92,7 +93,7 @@ func TestBodyID_Encode(t *testing.T) { } func TestBodyID_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), bodyID1}, {MustHexDecodeString("0x010404"), bodyID2}, {MustHexDecodeString("0x0206000000"), bodyID3}, @@ -127,8 +128,8 @@ var ( MoreThanProportionDenom: 7, } - bodyPartFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(b *BodyPart, c fuzz.Continue) { + bodyPartFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(b *BodyPart, c fuzz.Continue) { switch c.Intn(5) { case 0: b.IsVoice = true @@ -153,13 +154,13 @@ var ( ) func TestBodyPart_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[BodyPart](t, 1000, bodyPartFuzzOpts...) - assertDecodeNilData[BodyPart](t) - assertEncodeEmptyObj[BodyPart](t, 0) + AssertRoundTripFuzz[BodyPart](t, 1000, bodyPartFuzzOpts...) + AssertDecodeNilData[BodyPart](t) + AssertEncodeEmptyObj[BodyPart](t, 0) } func TestBodyPart_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {bodyPart1, MustHexDecodeString("0x00")}, {bodyPart2, MustHexDecodeString("0x0103000000")}, {bodyPart3, MustHexDecodeString("0x020100000002000000")}, @@ -169,7 +170,7 @@ func TestBodyPart_Encode(t *testing.T) { } func TestBodyPart_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), bodyPart1}, {MustHexDecodeString("0x0103000000"), bodyPart2}, {MustHexDecodeString("0x020100000002000000"), bodyPart3}, diff --git a/types/bool_test.go b/types/bool_test.go index 4da794789..8b1e7075e 100644 --- a/types/bool_test.go +++ b/types/bool_test.go @@ -20,58 +20,60 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestBool_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Bool](t, 100) - assertDecodeNilData[Bool](t) - assertEncodeEmptyObj[Bool](t, 1) + AssertRoundTripFuzz[Bool](t, 100) + AssertDecodeNilData[Bool](t) + AssertEncodeEmptyObj[Bool](t, 1) } func TestBool_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewBool(true), 1}, {NewBool(false), 1}, }) } func TestBool_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewBool(true), []byte{0x01}}, {NewBool(false), []byte{0x00}}, }) } func TestBool_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {[]byte{0x01}, NewBool(true)}, {[]byte{0x00}, NewBool(false)}, }) } func TestBool_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewBool(true), MustHexDecodeString("0xee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25")}, {NewBool(false), MustHexDecodeString("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")}, }) } func TestBool_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewBool(true), "0x01"}, {NewBool(false), "0x00"}, }) } func TestBool_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewBool(true), "true"}, {NewBool(false), "false"}, }) } func TestBool_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewBool(true), NewBool(true), true}, {NewBool(false), NewBool(true), false}, {NewBool(false), NewBool(false), true}, diff --git a/types/bytes_test.go b/types/bytes_test.go index c0c43dc1e..e6c7281ec 100644 --- a/types/bytes_test.go +++ b/types/bytes_test.go @@ -21,18 +21,19 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) func TestBytes_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Bytes](t, 100) - assertEncodeEmptyObj[Bytes](t, 1) + AssertRoundTripFuzz[Bytes](t, 100) + AssertEncodeEmptyObj[Bytes](t, 1) } func TestBytes_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewBytes(MustHexDecodeString("0x00")), 2}, {NewBytes(MustHexDecodeString("0xab1234")), 4}, {NewBytes(MustHexDecodeString("0x0001")), 3}, @@ -40,7 +41,7 @@ func TestBytes_EncodedLength(t *testing.T) { } func TestBytes_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewBytes([]byte{0, 0, 0}), MustHexDecodeString("0x0c000000")}, {NewBytes([]byte{171, 18, 52}), MustHexDecodeString("0x0cab1234")}, {NewBytes([]byte{0, 1}), MustHexDecodeString("0x080001")}, @@ -49,7 +50,7 @@ func TestBytes_Encode(t *testing.T) { } func TestBytes_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewBytes([]byte{0, 42, 254}), MustHexDecodeString( "0xabf7fe6eb94e0816bf2db57abb296d012f7cb9ddfe59ebf52f9c2770f49a0a46")}, {NewBytes([]byte{0, 0}), MustHexDecodeString( @@ -58,7 +59,7 @@ func TestBytes_Hash(t *testing.T) { } func TestBytes_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewBytes([]byte{0, 0, 0}), "0x0c000000"}, {NewBytes([]byte{171, 18, 52}), "0x0cab1234"}, {NewBytes([]byte{0, 1}), "0x080001"}, @@ -67,7 +68,7 @@ func TestBytes_Hex(t *testing.T) { } func TestBytes_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewBytes([]byte{0, 0, 0}), "[0 0 0]"}, {NewBytes([]byte{171, 18, 52}), "[171 18 52]"}, {NewBytes([]byte{0, 1}), "[0 1]"}, @@ -76,7 +77,7 @@ func TestBytes_String(t *testing.T) { } func TestBytes_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewBytes([]byte{1, 0, 0}), NewBytes([]byte{1, 0}), false}, {NewBytes([]byte{0, 0, 1}), NewBytes([]byte{0, 1}), false}, {NewBytes([]byte{0, 0, 0}), NewBytes([]byte{0, 0}), false}, @@ -88,26 +89,26 @@ func TestBytes_Eq(t *testing.T) { } func TestBytes8_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes8([8]byte{})) - assertRoundtrip(t, NewBytes8([8]byte{0, 1, 2, 3, 4, 5, 6, 7})) + AssertRoundtrip(t, NewBytes8([8]byte{})) + AssertRoundtrip(t, NewBytes8([8]byte{0, 1, 2, 3, 4, 5, 6, 7})) } func TestBytes8_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewBytes8([8]byte{}), 8}, {NewBytes8([8]byte{7, 6, 5, 4, 3, 2, 1, 0}), 8}, }) } func TestBytes8_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewBytes8([8]byte{0, 0, 0}), MustHexDecodeString("0x0000000000000000")}, {NewBytes8([8]byte{171, 18, 52}), MustHexDecodeString("0xab12340000000000")}, }) } func TestBytes8_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewBytes8([8]byte{0, 42, 254}), MustHexDecodeString( "0xb327a728de8af3187dd7eaeb74bfff9e9c37eda8c472c7459d51e1fc450faf74")}, {NewBytes8([8]byte{0, 0}), MustHexDecodeString( @@ -116,21 +117,21 @@ func TestBytes8_Hash(t *testing.T) { } func TestBytes8_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewBytes8([8]byte{0, 0, 0}), "0x0000000000000000"}, {NewBytes8([8]byte{171, 18, 52}), "0xab12340000000000"}, }) } func TestBytes8_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewBytes8([8]byte{0, 0, 0}), "[0 0 0 0 0 0 0 0]"}, {NewBytes8([8]byte{171, 18, 52}), "[171 18 52 0 0 0 0 0]"}, }) } func TestBytes8_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewBytes8([8]byte{1, 0, 0}), NewBytes8([8]byte{1, 0}), true}, {NewBytes8([8]byte{0, 0, 1}), NewBytes8([8]byte{0, 1}), false}, {NewBytes8([8]byte{0, 0, 0}), NewBytes8([8]byte{0, 0}), true}, @@ -142,35 +143,35 @@ func TestBytes8_Eq(t *testing.T) { } func TestBytes16_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes16([16]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes16([16]byte{0, 255, 0, 42})) } func TestBytes32_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes32([32]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes32([32]byte{0, 255, 0, 42})) } func TestBytes64_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes64([64]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes64([64]byte{0, 255, 0, 42})) } func TestBytes128_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes128([128]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes128([128]byte{0, 255, 0, 42})) } func TestBytes256_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes256([256]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes256([256]byte{0, 255, 0, 42})) } func TestBytes512_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes512([512]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes512([512]byte{0, 255, 0, 42})) } func TestBytes1024_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes1024([1024]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes1024([1024]byte{0, 255, 0, 42})) } func TestBytes2048_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewBytes2048([2048]byte{0, 255, 0, 42})) + AssertRoundtrip(t, NewBytes2048([2048]byte{0, 255, 0, 42})) } func TestBytesBare_Decode(t *testing.T) { diff --git a/types/candidate_receipt_test.go b/types/candidate_receipt_test.go index 1707ddfc2..9a943fdf0 100644 --- a/types/candidate_receipt_test.go +++ b/types/candidate_receipt_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( @@ -45,13 +47,13 @@ var ( ) func TestCandidateReceipt_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[CandidateReceipt](t, 1000) - assertDecodeNilData[CandidateReceipt](t) - assertEncodeEmptyObj[CandidateReceipt](t, 324) + AssertRoundTripFuzz[CandidateReceipt](t, 1000) + AssertDecodeNilData[CandidateReceipt](t) + AssertEncodeEmptyObj[CandidateReceipt](t, 324) } func TestCandidateReceipt_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testCandidateReceipt, MustHexDecodeString("0x0b0000006861736800000000000000000000000000000000000000000000000000000000010203040506070809000000000000000000000000000000000000000000000068617368320000000000000000000000000000000000000000000000000000006861736833000000000000000000000000000000000000000000000000000000686173683400000000000000000000000000000000000000000000000000000009080706050400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000686173683500000000000000000000000000000000000000000000000000000068617368360000000000000000000000000000000000000000000000000000006861736837000000000000000000000000000000000000000000000000000000"), @@ -60,7 +62,7 @@ func TestCandidateReceipt_Encode(t *testing.T) { } func TestCandidateReceipt_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x0b0000006861736800000000000000000000000000000000000000000000000000000000010203040506070809000000000000000000000000000000000000000000000068617368320000000000000000000000000000000000000000000000000000006861736833000000000000000000000000000000000000000000000000000000686173683400000000000000000000000000000000000000000000000000000009080706050400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000686173683500000000000000000000000000000000000000000000000000000068617368360000000000000000000000000000000000000000000000000000006861736837000000000000000000000000000000000000000000000000000000"), testCandidateReceipt, diff --git a/types/chain_properties_test.go b/types/chain_properties_test.go index 9e78afaef..045a80fb9 100644 --- a/types/chain_properties_test.go +++ b/types/chain_properties_test.go @@ -20,6 +20,7 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var testChainProperties1 = ChainProperties{} @@ -30,19 +31,19 @@ var testChainProperties2 = ChainProperties{ } func TestChainProperties_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testChainProperties1) - assertRoundtrip(t, testChainProperties2) + AssertRoundtrip(t, testChainProperties1) + AssertRoundtrip(t, testChainProperties2) } func TestChainProperties_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testChainProperties1, []byte{0x0, 0x0, 0x0}}, {testChainProperties2, []byte{0x01, 0x01, 0x01, 0x12, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x46, 0x4f, 0x4f}}, }) } func TestChainProperties_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {[]byte{0x0, 0x0, 0x0}, testChainProperties1}, {[]byte{0x01, 0x01, 0x01, 0x12, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x46, 0x4f, 0x4f}, testChainProperties2}, }) diff --git a/types/class_metadata_test.go b/types/class_metadata_test.go index b9430e012..f05229f16 100644 --- a/types/class_metadata_test.go +++ b/types/class_metadata_test.go @@ -21,6 +21,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( @@ -32,19 +34,19 @@ var ( ) func TestClassMetadata_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[ClassMetadata](t, 1000) - assertDecodeNilData[ClassMetadata](t) - assertEncodeEmptyObj[ClassMetadata](t, 18) + AssertRoundTripFuzz[ClassMetadata](t, 1000) + AssertDecodeNilData[ClassMetadata](t) + AssertEncodeEmptyObj[ClassMetadata](t, 18) } func TestClassMetadata_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testClassMetadata, MustHexDecodeString("0x7b000000000000000000000000000000106461746101")}, }) } func TestClassMetadata_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x7b000000000000000000000000000000106461746101"), testClassMetadata}, }) } diff --git a/types/codec.go b/types/codec/codec.go similarity index 93% rename from types/codec.go rename to types/codec/codec.go index d8380c2f4..af34458fc 100644 --- a/types/codec.go +++ b/types/codec/codec.go @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package types +package codec import ( "bytes" @@ -24,7 +24,6 @@ import ( "strings" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - "golang.org/x/crypto/blake2b" ) // Hexer interface is implemented by any type that has a Hex() function returning a string @@ -76,15 +75,6 @@ func EncodedLength(value interface{}) (int, error) { return buffer.Len(), nil } -// GetHash returns a hash of the value -func GetHash(value interface{}) (Hash, error) { - enc, err := Encode(value) - if err != nil { - return Hash{}, err - } - return blake2b.Sum256(enc), err -} - // Eq compares the value of the input to see if there is a match func Eq(one, other interface{}) bool { return reflect.DeepEqual(one, other) diff --git a/types/codec_test.go b/types/codec/codec_test.go similarity index 98% rename from types/codec_test.go rename to types/codec/codec_test.go index d367789a0..3cd575555 100644 --- a/types/codec_test.go +++ b/types/codec/codec_test.go @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package types +package codec import ( "testing" diff --git a/types/class_details.go b/types/collection_details.go similarity index 93% rename from types/class_details.go rename to types/collection_details.go index cf62ecf76..8caab90b9 100644 --- a/types/class_details.go +++ b/types/collection_details.go @@ -18,7 +18,7 @@ package types import "github.com/centrifuge/go-substrate-rpc-client/v4/scale" -type ClassDetails struct { +type CollectionDetails struct { Owner AccountID Issuer AccountID Admin AccountID @@ -31,7 +31,7 @@ type ClassDetails struct { IsFrozen bool } -func (c *ClassDetails) Decode(decoder scale.Decoder) error { +func (c *CollectionDetails) Decode(decoder scale.Decoder) error { if err := decoder.Decode(&c.Owner); err != nil { return err } @@ -64,7 +64,7 @@ func (c *ClassDetails) Decode(decoder scale.Decoder) error { return decoder.Decode(&c.IsFrozen) } -func (c ClassDetails) Encode(encoder scale.Encoder) error { +func (c CollectionDetails) Encode(encoder scale.Encoder) error { if err := encoder.Encode(c.Owner); err != nil { return err } diff --git a/types/class_details_test.go b/types/collection_details_test.go similarity index 51% rename from types/class_details_test.go rename to types/collection_details_test.go index 0b01a98a0..ac51d8eea 100644 --- a/types/class_details_test.go +++ b/types/collection_details_test.go @@ -21,14 +21,16 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( - testClassDetails = ClassDetails{ - Owner: NewAccountID([]byte("acc_id")), - Issuer: NewAccountID([]byte("acc_id2")), - Admin: NewAccountID([]byte("acc_id3")), - Freezer: NewAccountID([]byte("acc_id4")), + testClassDetails = CollectionDetails{ + Owner: newTestAccountID(), + Issuer: newTestAccountID(), + Admin: newTestAccountID(), + Freezer: newTestAccountID(), TotalDeposit: NewU128(*big.NewInt(123)), FreeHolding: true, Instances: 4, @@ -39,24 +41,24 @@ var ( ) func TestClassDetails_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[ClassDetails](t, 1000) - assertDecodeNilData[ClassDetails](t) - assertEncodeEmptyObj[ClassDetails](t, 158) + AssertRoundTripFuzz[CollectionDetails](t, 1000) + AssertDecodeNilData[CollectionDetails](t) + AssertEncodeEmptyObj[CollectionDetails](t, 158) } func TestClassDetails_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testClassDetails, - MustHexDecodeString("0x6163635f696400000000000000000000000000000000000000000000000000006163635f696432000000000000000000000000000000000000000000000000006163635f696433000000000000000000000000000000000000000000000000006163635f696434000000000000000000000000000000000000000000000000007b0000000000000000000000000000000104000000050000000600000001"), + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f207b0000000000000000000000000000000104000000050000000600000001"), }, }) } func TestClassDetails_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { - MustHexDecodeString("0x6163635f696400000000000000000000000000000000000000000000000000006163635f696432000000000000000000000000000000000000000000000000006163635f696433000000000000000000000000000000000000000000000000006163635f696434000000000000000000000000000000000000000000000000007b0000000000000000000000000000000104000000050000000600000001"), + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f207b0000000000000000000000000000000104000000050000000600000001"), testClassDetails, }, }) diff --git a/types/data_test.go b/types/data_test.go index df4ba0a4f..b0a838779 100644 --- a/types/data_test.go +++ b/types/data_test.go @@ -20,16 +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/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) func TestData_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Data](t, 100) - assertEncodeEmptyObj[Data](t, 0) + AssertRoundTripFuzz[Data](t, 100) + AssertEncodeEmptyObj[Data](t, 0) } func TestData_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {Data([]byte{12, 251, 42}), 3}, {Data([]byte{}), 0}, }) @@ -52,7 +54,7 @@ func TestData_Decode(t *testing.T) { } func TestData_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {Data([]byte{0, 42, 254}), MustHexDecodeString( "0x537db36f5b5970b679a28a3df8d219317d658014fb9c3d409c0c799d8ecf149d")}, {Data([]byte{}), MustHexDecodeString( @@ -61,19 +63,19 @@ func TestData_Hash(t *testing.T) { } func TestData_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {Data([]byte{0, 0, 0}), "0x000000"}, {Data([]byte{171, 18, 52}), "0xab1234"}, {Data([]byte{0, 1}), "0x0001"}, {Data([]byte{18, 52, 86}), "0x123456"}, }) - assertEqual(t, NewData([]byte{0, 0, 0}).Hex(), "0x000000") - assertEqual(t, NewData([]byte{171, 18, 52}).Hex(), "0xab1234") + AssertEqual(t, NewData([]byte{0, 0, 0}).Hex(), "0x000000") + AssertEqual(t, NewData([]byte{171, 18, 52}).Hex(), "0xab1234") } func TestData_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {Data([]byte{0, 0, 0}), "[0 0 0]"}, {Data([]byte{171, 18, 52}), "[171 18 52]"}, {Data([]byte{0, 1}), "[0 1]"}, @@ -82,7 +84,7 @@ func TestData_String(t *testing.T) { } func TestData_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {Data([]byte{1, 0, 0}), Data([]byte{1, 0}), false}, {Data([]byte{0, 0, 1}), Data([]byte{0, 1}), false}, {Data([]byte{0, 0, 0}), Data([]byte{0, 0}), false}, diff --git a/types/digest.go b/types/digest.go index bee389680..9ddabc17f 100644 --- a/types/digest.go +++ b/types/digest.go @@ -18,6 +18,8 @@ package types import ( "encoding/json" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // Digest contains logs @@ -33,7 +35,7 @@ func (d *Digest) UnmarshalJSON(bz []byte) error { } *d = make([]DigestItem, len(tmp.Logs)) for i, log := range tmp.Logs { - err := DecodeFromHex(log, &(*d)[i]) + err := codec.DecodeFromHex(log, &(*d)[i]) if err != nil { return err } @@ -46,7 +48,7 @@ func (d Digest) MarshalJSON() ([]byte, error) { logs := make([]string, len(d)) var err error for i, di := range d { - logs[i], err = EncodeToHex(di) + logs[i], err = codec.EncodeToHex(di) if err != nil { return nil, err } diff --git a/types/digest_item_test.go b/types/digest_item_test.go index fedd9cd5c..c329c208f 100644 --- a/types/digest_item_test.go +++ b/types/digest_item_test.go @@ -21,16 +21,15 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - "github.com/stretchr/testify/assert" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" ) var ( - changesTrieSignalFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(c *ChangesTrieSignal, fc fuzz.Continue) { + changesTrieSignalFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(c *ChangesTrieSignal, fc fuzz.Continue) { c.IsNewConfiguration = true fc.Fuzz(&c.AsNewConfiguration) @@ -40,8 +39,8 @@ var ( ) func TestChangesTrieSignal_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[ChangesTrieSignal](t, 100, changesTrieSignalFuzzOpts...) - assertDecodeNilData[ChangesTrieSignal](t) + AssertRoundTripFuzz[ChangesTrieSignal](t, 100, changesTrieSignalFuzzOpts...) + AssertDecodeNilData[ChangesTrieSignal](t) var c ChangesTrieSignal @@ -58,10 +57,10 @@ var testDigestItem1 = DigestItem{IsOther: true, AsOther: NewBytes([]byte{0xab})} var testDigestItem2 = DigestItem{IsChangesTrieRoot: true, AsChangesTrieRoot: NewHash([]byte{0x01, 0x02, 0x03})} var ( - digestItemFuzzOpts = combineFuzzOpts( + digestItemFuzzOpts = CombineFuzzOpts( changesTrieSignalFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(d *DigestItem, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(d *DigestItem, c fuzz.Continue) { vals := []int{0, 2, 4, 5, 6, 7} r := c.Intn(len(vals)) @@ -91,26 +90,26 @@ var ( ) func TestDigestItem_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testDigestItem1) - assertRoundtrip(t, testDigestItem2) - assertRoundtrip(t, DigestItem{ + AssertRoundtrip(t, testDigestItem1) + AssertRoundtrip(t, testDigestItem2) + AssertRoundtrip(t, DigestItem{ IsPreRuntime: true, AsPreRuntime: PreRuntime{}, }) - assertRoundtrip(t, DigestItem{ + AssertRoundtrip(t, DigestItem{ IsConsensus: true, AsConsensus: Consensus{}, }) - assertRoundtrip(t, DigestItem{ + AssertRoundtrip(t, DigestItem{ IsSeal: true, AsSeal: Seal{}, }) - assertRoundtrip(t, DigestItem{ + AssertRoundtrip(t, DigestItem{ IsChangesTrieSignal: true, AsChangesTrieSignal: ChangesTrieSignal{IsNewConfiguration: true, AsNewConfiguration: NewBytes([]byte{1, 2, 3})}, }) - assertRoundTripFuzz[DigestItem](t, 100, digestItemFuzzOpts...) - assertDecodeNilData[DigestItem](t) - assertEncodeEmptyObj[DigestItem](t, 0) + AssertRoundTripFuzz[DigestItem](t, 100, digestItemFuzzOpts...) + AssertDecodeNilData[DigestItem](t) + AssertEncodeEmptyObj[DigestItem](t, 0) } diff --git a/types/digest_of.go b/types/digest_of.go index 5827f348f..877118c65 100644 --- a/types/digest_of.go +++ b/types/digest_of.go @@ -18,6 +18,8 @@ package types import ( "encoding/json" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // DigestOf contains logs @@ -33,7 +35,7 @@ func (d *DigestOf) UnmarshalJSON(bz []byte) error { } *d = make([]DigestItem, len(tmp.Logs)) for i, log := range tmp.Logs { - err := DecodeFromHex(log, &(*d)[i]) + err := codec.DecodeFromHex(log, &(*d)[i]) if err != nil { return err } @@ -46,7 +48,7 @@ func (d DigestOf) MarshalJSON() ([]byte, error) { logs := make([]string, len(d)) var err error for i, di := range d { - logs[i], err = EncodeToHex(di) + logs[i], err = codec.EncodeToHex(di) if err != nil { return nil, err } diff --git a/types/digest_of_test.go b/types/digest_of_test.go index 69b5a8f55..b31ef9544 100644 --- a/types/digest_of_test.go +++ b/types/digest_of_test.go @@ -20,33 +20,35 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestDigestOf_EncodeDecode(t *testing.T) { - assertRoundtrip(t, DigestOf{testDigestItem1, testDigestItem2}) - assertRoundTripFuzz[DigestOf](t, 100, digestItemFuzzOpts...) + AssertRoundtrip(t, DigestOf{testDigestItem1, testDigestItem2}) + AssertRoundTripFuzz[DigestOf](t, 100, digestItemFuzzOpts...) } func TestDigestOf_JSONMarshalUnmarshal(t *testing.T) { d := DigestOf{testDigestItem1, testDigestItem2} - assertJSONRoundTrip(t, &d) + AssertJSONRoundTrip(t, &d) } func TestDigestOf_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { - input: DigestOf{testDigestItem1, testDigestItem2}, - expected: MustHexDecodeString( + Input: DigestOf{testDigestItem1, testDigestItem2}, + Expected: MustHexDecodeString( "0x080004ab020102030000000000000000000000000000000000000000000000000000000000"), }, //nolint:lll }) } func TestDigestOf_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { - input: MustHexDecodeString( + Input: MustHexDecodeString( "0x080004ab020102030000000000000000000000000000000000000000000000000000000000"), //nolint:lll - expected: DigestOf{testDigestItem1, testDigestItem2}}, + Expected: DigestOf{testDigestItem1, testDigestItem2}}, }) } diff --git a/types/digest_test.go b/types/digest_test.go index 2092e5352..089ef5cfb 100644 --- a/types/digest_test.go +++ b/types/digest_test.go @@ -20,32 +20,34 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestDigest_EncodeDecode(t *testing.T) { - assertRoundtrip(t, Digest{testDigestItem1, testDigestItem2}) - assertRoundTripFuzz[Digest](t, 100, digestItemFuzzOpts...) + AssertRoundtrip(t, Digest{testDigestItem1, testDigestItem2}) + AssertRoundTripFuzz[Digest](t, 100, digestItemFuzzOpts...) } func TestDigest_JSONMarshalUnmarshal(t *testing.T) { d := Digest{testDigestItem1, testDigestItem2} - assertJSONRoundTrip(t, &d) + AssertJSONRoundTrip(t, &d) } func TestDigest_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { - input: Digest{testDigestItem1, testDigestItem2}, - expected: MustHexDecodeString( + Input: Digest{testDigestItem1, testDigestItem2}, + Expected: MustHexDecodeString( "0x080004ab020102030000000000000000000000000000000000000000000000000000000000")}, //nolint:lll }) } func TestDigest_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { - input: MustHexDecodeString( + Input: MustHexDecodeString( "0x080004ab020102030000000000000000000000000000000000000000000000000000000000"), //nolint:lll - expected: Digest{testDigestItem1, testDigestItem2}}, + Expected: Digest{testDigestItem1, testDigestItem2}}, }) } diff --git a/types/dispatch_result_with_post_info_test.go b/types/dispatch_result_with_post_info_test.go index 11718689b..f89974d09 100644 --- a/types/dispatch_result_with_post_info_test.go +++ b/types/dispatch_result_with_post_info_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -49,12 +50,12 @@ var ( }, } - dispatchResultWithPostInfoFuzzOpts = combineFuzzOpts( + dispatchResultWithPostInfoFuzzOpts = CombineFuzzOpts( optionWeightFuzzOpts, paysFuzzOpts, dispatchErrorFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(d *DispatchResultWithPostInfo, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(d *DispatchResultWithPostInfo, c fuzz.Continue) { if c.RandBool() { d.IsOk = true c.Fuzz(&d.Ok) @@ -69,20 +70,20 @@ var ( ) func TestDispatchResultWithPostInfo_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[DispatchResultWithPostInfo](t, 1000, dispatchResultWithPostInfoFuzzOpts...) - assertDecodeNilData[DispatchResultWithPostInfo](t) - assertEncodeEmptyObj[DispatchResultWithPostInfo](t, 0) + AssertRoundTripFuzz[DispatchResultWithPostInfo](t, 1000, dispatchResultWithPostInfoFuzzOpts...) + AssertDecodeNilData[DispatchResultWithPostInfo](t) + AssertEncodeEmptyObj[DispatchResultWithPostInfo](t, 0) } func TestDispatchResultWithPostInfo_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testDispatchResultWithPostInfo1, MustHexDecodeString("0x00017b0000000000000000")}, {testDispatchResultWithPostInfo2, MustHexDecodeString("0x0101c8010000000000000100")}, }) } func TestDispatchResultWithPostInfo_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00017b0000000000000000"), testDispatchResultWithPostInfo1}, {MustHexDecodeString("0x0101c8010000000000000100"), testDispatchResultWithPostInfo2}, }) diff --git a/types/dispute_test.go b/types/dispute_test.go index d9fa289c9..22e1a8777 100644 --- a/types/dispute_test.go +++ b/types/dispute_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -42,8 +43,8 @@ var ( ) var ( - disputeLocationFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(d *DisputeLocation, c fuzz.Continue) { + disputeLocationFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(d *DisputeLocation, c fuzz.Continue) { if c.RandBool() { d.IsLocal = true return @@ -55,30 +56,30 @@ var ( ) func TestDisputeLocation_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testDisputeLocation1) - assertRoundtrip(t, testDisputeLocation2) - assertRoundTripFuzz[DisputeLocation](t, 100, disputeLocationFuzzOpts...) - assertDecodeNilData[DisputeLocation](t) - assertEncodeEmptyObj[DisputeLocation](t, 0) + AssertRoundtrip(t, testDisputeLocation1) + AssertRoundtrip(t, testDisputeLocation2) + AssertRoundTripFuzz[DisputeLocation](t, 100, disputeLocationFuzzOpts...) + AssertDecodeNilData[DisputeLocation](t) + AssertEncodeEmptyObj[DisputeLocation](t, 0) } func TestDisputeLocation_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testDisputeLocation1, MustHexDecodeString("0x00")}, {testDisputeLocation2, MustHexDecodeString("0x01")}, }) } func TestDisputeLocation_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testDisputeLocation1}, {MustHexDecodeString("0x01"), testDisputeLocation2}, }) } var ( - disputeResultFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(d *DisputeResult, c fuzz.Continue) { + disputeResultFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(d *DisputeResult, c fuzz.Continue) { if c.RandBool() { d.IsValid = true return @@ -91,22 +92,22 @@ var ( ) func TestDisputeResult_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testDisputeResult1) - assertRoundtrip(t, testDisputeResult2) - assertRoundTripFuzz[DisputeResult](t, 100, disputeResultFuzzOpts...) - assertDecodeNilData[DisputeResult](t) - assertEncodeEmptyObj[DisputeResult](t, 0) + AssertRoundtrip(t, testDisputeResult1) + AssertRoundtrip(t, testDisputeResult2) + AssertRoundTripFuzz[DisputeResult](t, 100, disputeResultFuzzOpts...) + AssertDecodeNilData[DisputeResult](t) + AssertEncodeEmptyObj[DisputeResult](t, 0) } func TestDisputeResult_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testDisputeResult1, MustHexDecodeString("0x00")}, {testDisputeResult2, MustHexDecodeString("0x01")}, }) } func TestDisputeResult_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testDisputeResult1}, {MustHexDecodeString("0x01"), testDisputeResult2}, }) diff --git a/types/election_compute_test.go b/types/election_compute_test.go index 73d0af3e4..cb4b0605e 100644 --- a/types/election_compute_test.go +++ b/types/election_compute_test.go @@ -20,24 +20,25 @@ import ( "bytes" "testing" - fuzz "github.com/google/gofuzz" - "github.com/centrifuge/go-substrate-rpc-client/v4/scale" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) var ( - electionComputeFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(e *ElectionCompute, c fuzz.Continue) { + electionComputeFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(e *ElectionCompute, c fuzz.Continue) { *e = ElectionCompute(c.Intn(3)) }), } - optionElectionComputeFuzzOpts = combineFuzzOpts( + optionElectionComputeFuzzOpts = CombineFuzzOpts( electionComputeFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(o *OptionElectionCompute, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(o *OptionElectionCompute, c fuzz.Continue) { if c.RandBool() { *o = NewOptionElectionComputeEmpty() return @@ -54,12 +55,12 @@ var ( ) func TestOptionElectionCompute_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionElectionCompute](t, 100, optionElectionComputeFuzzOpts...) - assertEncodeEmptyObj[ElectionCompute](t, 1) + AssertRoundTripFuzz[OptionElectionCompute](t, 100, optionElectionComputeFuzzOpts...) + AssertEncodeEmptyObj[ElectionCompute](t, 1) } func TestOptionElectionCompute_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionElectionCompute(NewElectionCompute(byte(0))), MustHexDecodeString("0x0100")}, {NewOptionElectionCompute(NewElectionCompute(byte(1))), MustHexDecodeString("0x0101")}, {NewOptionElectionCompute(NewElectionCompute(byte(2))), MustHexDecodeString("0x0102")}, @@ -68,7 +69,7 @@ func TestOptionElectionCompute_Encode(t *testing.T) { } func TestOptionElectionCompute_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0100"), NewOptionElectionCompute(NewElectionCompute(byte(0)))}, {MustHexDecodeString("0x0101"), NewOptionElectionCompute(NewElectionCompute(byte(1)))}, {MustHexDecodeString("0x0102"), NewOptionElectionCompute(NewElectionCompute(byte(2)))}, @@ -98,7 +99,7 @@ func TestElectionComputeEncodeDecode(t *testing.T) { err := decoder.Decode(&ec0) assert.Error(t, err) - assertRoundTripFuzz[ElectionCompute](t, 100, electionComputeFuzzOpts...) - assertDecodeNilData[ElectionCompute](t) - assertEncodeEmptyObj[ElectionCompute](t, 1) + AssertRoundTripFuzz[ElectionCompute](t, 100, electionComputeFuzzOpts...) + AssertDecodeNilData[ElectionCompute](t) + AssertEncodeEmptyObj[ElectionCompute](t, 1) } diff --git a/types/errors_test.go b/types/errors_test.go index 4e5bf7a2b..4feeacf59 100644 --- a/types/errors_test.go +++ b/types/errors_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -69,8 +70,8 @@ var ( }, } - tokenErrorFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(t *TokenError, c fuzz.Continue) { + tokenErrorFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(t *TokenError, c fuzz.Continue) { switch c.Intn(7) { case 0: t.IsNoFunds = true @@ -90,8 +91,8 @@ var ( }), } - arithmeticErrorFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(a *ArithmeticError, c fuzz.Continue) { + arithmeticErrorFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(a *ArithmeticError, c fuzz.Continue) { switch c.Intn(3) { case 0: a.IsUnderflow = true @@ -103,20 +104,20 @@ var ( }), } - transactionalErrorFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(t *TransactionalError, c fuzz.Continue) { + transactionalErrorFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(t *TransactionalError, c fuzz.Continue) { r := c.RandBool() t.IsLimitReached = r t.IsNoLayer = !r }), } - dispatchErrorFuzzOpts = combineFuzzOpts( + dispatchErrorFuzzOpts = CombineFuzzOpts( tokenErrorFuzzOpts, arithmeticErrorFuzzOpts, transactionalErrorFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(d *DispatchError, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(d *DispatchError, c fuzz.Continue) { switch c.Intn(10) { case 0: d.IsOther = true @@ -153,13 +154,13 @@ var ( ) func TestDispatchError_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[DispatchError](t, 1000, dispatchErrorFuzzOpts...) - assertDecodeNilData[DispatchError](t) - assertEncodeEmptyObj[DispatchError](t, 0) + AssertRoundTripFuzz[DispatchError](t, 1000, dispatchErrorFuzzOpts...) + AssertDecodeNilData[DispatchError](t) + AssertEncodeEmptyObj[DispatchError](t, 0) } func TestDispatchError_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testDispatchError1, MustHexDecodeString("0x00")}, {testDispatchError2, MustHexDecodeString("0x01")}, {testDispatchError3, MustHexDecodeString("0x02")}, @@ -174,7 +175,7 @@ func TestDispatchError_Encode(t *testing.T) { } func TestDispatchError_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testDispatchError1}, {MustHexDecodeString("0x01"), testDispatchError2}, {MustHexDecodeString("0x02"), testDispatchError3}, diff --git a/types/event_record.go b/types/event_record.go index 77f3b2c53..b21deedfc 100644 --- a/types/event_record.go +++ b/types/event_record.go @@ -517,6 +517,8 @@ func (e EventRecordsRaw) DecodeEventRecords(m *Metadata, t interface{}) error { return fmt.Errorf("unable to find event with EventID %v in metadata for event #%v: %s", id, i, err) } + // fmt.Printf("event #%v is in module %v with event name %v\n", i, moduleName, eventName) + log.Debug(fmt.Sprintf("event #%v is in module %v with event name %v", i, moduleName, eventName)) // check whether name for eventID exists in t diff --git a/types/event_record_test.go b/types/event_record_test.go index 74da1fb2f..a23d53d78 100644 --- a/types/event_record_test.go +++ b/types/event_record_test.go @@ -23,10 +23,10 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) @@ -58,16 +58,16 @@ var exampleEventFinEnc = []byte{0x1, 0x10, 0x27, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} //nolint:lll var ( - eventSystemExtrinsicSuccessFuzzOpts = combineFuzzOpts( + eventSystemExtrinsicSuccessFuzzOpts = CombineFuzzOpts( phaseFuzzOpts, dispatchInfoFuzzOpts, ) ) func TestEventSystemExtrinsicSuccess_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[EventSystemExtrinsicSuccess](t, 100, eventSystemExtrinsicSuccessFuzzOpts...) - assertDecodeNilData[EventSystemExtrinsicSuccess](t) - assertEncodeEmptyObj[EventSystemExtrinsicSuccess](t, 9) + AssertRoundTripFuzz[EventSystemExtrinsicSuccess](t, 100, eventSystemExtrinsicSuccessFuzzOpts...) + AssertDecodeNilData[EventSystemExtrinsicSuccess](t) + AssertEncodeEmptyObj[EventSystemExtrinsicSuccess](t, 9) } func TestEventSystemExtrinsicSuccess_Encode(t *testing.T) { @@ -318,14 +318,14 @@ func TestEventRecordsRaw_Decode(t *testing.T) { } func TestDispatchError(t *testing.T) { - assertRoundTripFuzz[DispatchError](t, 1000, dispatchErrorFuzzOpts...) - assertDecodeNilData[DispatchError](t) - assertEncodeEmptyObj[DispatchError](t, 0) + AssertRoundTripFuzz[DispatchError](t, 1000, dispatchErrorFuzzOpts...) + AssertDecodeNilData[DispatchError](t) + AssertEncodeEmptyObj[DispatchError](t, 0) } var ( - phaseFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(p *Phase, c fuzz.Continue) { + phaseFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(p *Phase, c fuzz.Continue) { switch c.Intn(3) { case 0: p.IsApplyExtrinsic = true @@ -340,7 +340,7 @@ var ( ) func TestPhase(t *testing.T) { - assertRoundTripFuzz[Phase](t, 100, phaseFuzzOpts...) - assertDecodeNilData[Phase](t) - assertEncodeEmptyObj[Phase](t, 0) + AssertRoundTripFuzz[Phase](t, 100, phaseFuzzOpts...) + AssertDecodeNilData[Phase](t) + AssertEncodeEmptyObj[Phase](t, 0) } diff --git a/types/events.go b/types/events.go index e138b27ce..f35cb4160 100644 --- a/types/events.go +++ b/types/events.go @@ -2396,31 +2396,6 @@ type EventPreimageRequested struct { Topics []Hash } -type ProxyType byte - -const ( - Any ProxyType = 0 - NonTransfer ProxyType = 1 - Governance ProxyType = 2 - Staking ProxyType = 3 -) - -func (pt *ProxyType) Decode(decoder scale.Decoder) error { - b, err := decoder.ReadOneByte() - vb := ProxyType(b) - switch vb { - case Any, NonTransfer, Governance, Staking: - *pt = vb - default: - return fmt.Errorf("unknown ProxyType enum: %v", vb) - } - return err -} - -func (pt ProxyType) Encode(encoder scale.Encoder) error { - return encoder.PushByte(byte(pt)) -} - // EventProxyProxyExecuted is emitted when a proxy was executed correctly, with the given [result] type EventProxyProxyExecuted struct { Phase Phase @@ -2434,7 +2409,7 @@ type EventProxyAnonymousCreated struct { Phase Phase Anonymous AccountID Who AccountID - ProxyType ProxyType + ProxyType U8 DisambiguationIndex U16 Topics []Hash } @@ -2444,7 +2419,7 @@ type EventProxyProxyAdded struct { Phase Phase Delegator AccountID Delegatee AccountID - ProxyType ProxyType + ProxyType U8 Delay U32 Topics []Hash } @@ -2454,7 +2429,7 @@ type EventProxyProxyRemoved struct { Phase Phase Delegator AccountID Delegatee AccountID - ProxyType ProxyType + ProxyType U8 BlockNumber U32 Topics []Hash } @@ -2701,191 +2676,191 @@ type EventChildBountiesCanceled struct { // EventUniquesApprovalCancelled is emitted when an approval for a delegate account to transfer the instance of // an asset class was cancelled by its owner type EventUniquesApprovalCancelled struct { - Phase Phase - ClassID U64 - InstanceID U128 - Owner AccountID - Delegate AccountID - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Owner AccountID + Delegate AccountID + Topics []Hash } // EventUniquesApprovedTransfer is emitted when an `instance` of an asset `class` has been approved by the `owner` // for transfer by a `delegate`. type EventUniquesApprovedTransfer struct { - Phase Phase - ClassID U64 - InstanceID U128 - Owner AccountID - Delegate AccountID - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Owner AccountID + Delegate AccountID + Topics []Hash } // EventUniquesAssetStatusChanged is emitted when an asset `class` has had its attributes changed by the `Force` origin type EventUniquesAssetStatusChanged struct { - Phase Phase - ClassID U64 - Topics []Hash + Phase Phase + CollectionID U64 + Topics []Hash } // EventUniquesAttributeCleared is emitted when an attribute metadata has been cleared for an asset class or instance type EventUniquesAttributeCleared struct { - Phase Phase - ClassID U64 - MaybeInstance OptionU32 - Key Bytes - Topics []Hash + Phase Phase + CollectionID U64 + MaybeItem Option[U128] + Key Bytes + Topics []Hash } // EventUniquesAttributeSet is emitted when a new attribute metadata has been set for an asset class or instance type EventUniquesAttributeSet struct { - Phase Phase - ClassID U64 - MaybeInstance OptionU32 - Key Bytes - Value Bytes - Topics []Hash + Phase Phase + CollectionID U64 + MaybeItem Option[U128] + Key Bytes + Value Bytes + Topics []Hash } // EventUniquesBurned is emitted when an asset `instance` was destroyed type EventUniquesBurned struct { - Phase Phase - ClassID U64 - InstanceID U128 - Owner AccountID - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Owner AccountID + Topics []Hash } // EventUniquesClassFrozen is emitted when some asset `class` was frozen type EventUniquesClassFrozen struct { - Phase Phase - ClassID U64 - Topics []Hash + Phase Phase + CollectionID U64 + Topics []Hash } // EventUniquesClassMetadataCleared is emitted when metadata has been cleared for an asset class type EventUniquesClassMetadataCleared struct { - Phase Phase - ClassID U64 - Topics []Hash + Phase Phase + CollectionID U64 + Topics []Hash } // EventUniquesClassMetadataSet is emitted when new metadata has been set for an asset class type EventUniquesClassMetadataSet struct { - Phase Phase - ClassID U64 - Data Bytes - IsFrozen Bool - Topics []Hash + Phase Phase + CollectionID U64 + Data Bytes + IsFrozen Bool + Topics []Hash } // EventUniquesClassThawed is emitted when some asset `class` was thawed type EventUniquesClassThawed struct { - Phase Phase - ClassID U64 - Topics []Hash + Phase Phase + CollectionID U64 + Topics []Hash } // EventUniquesCreated is emitted when an asset class was created type EventUniquesCreated struct { - Phase Phase - ClassID U64 - Creator AccountID - Owner AccountID - Topics []Hash + Phase Phase + CollectionID U64 + Creator AccountID + Owner AccountID + Topics []Hash } // EventUniquesDestroyed is emitted when an asset `class` was destroyed type EventUniquesDestroyed struct { - Phase Phase - ClassID U64 - Topics []Hash + Phase Phase + CollectionID U64 + Topics []Hash } // EventUniquesForceCreated is emitted when an asset class was force-created type EventUniquesForceCreated struct { - Phase Phase - ClassID U64 - Owner AccountID - Topics []Hash + Phase Phase + CollectionID U64 + Owner AccountID + Topics []Hash } // EventUniquesFrozen is emitted when some asset `instance` was frozen type EventUniquesFrozen struct { - Phase Phase - ClassID U64 - InstanceID U128 - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Topics []Hash } // EventUniquesIssued is emitted when an asset instance was issued type EventUniquesIssued struct { - Phase Phase - ClassID U64 - InstanceID U128 - Owner AccountID - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Owner AccountID + Topics []Hash } // EventUniquesMetadataCleared is emitted when metadata has been cleared for an asset instance type EventUniquesMetadataCleared struct { - Phase Phase - ClassID U64 - InstanceID U128 - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Topics []Hash } // EventUniquesMetadataSet is emitted when metadata has been set for an asset instance type EventUniquesMetadataSet struct { - Phase Phase - ClassID U64 - InstanceID U128 - Data Bytes - IsFrozen Bool - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Data Bytes + IsFrozen Bool + Topics []Hash } // EventUniquesOwnerChanged is emitted when the owner changed type EventUniquesOwnerChanged struct { - Phase Phase - ClassID U64 - NewOwner AccountID - Topics []Hash + Phase Phase + CollectionID U64 + NewOwner AccountID + Topics []Hash } // EventUniquesRedeposited is emitted when metadata has been cleared for an asset instance type EventUniquesRedeposited struct { - Phase Phase - ClassID U64 - SuccessfulInstances []U128 - Topics []Hash + Phase Phase + CollectionID U64 + SuccessfulItems []U128 + Topics []Hash } // EventUniquesTeamChanged is emitted when the management team changed type EventUniquesTeamChanged struct { - Phase Phase - ClassID U64 - Issuer AccountID - Admin AccountID - Freezer AccountID - Topics []Hash + Phase Phase + CollectionID U64 + Issuer AccountID + Admin AccountID + Freezer AccountID + Topics []Hash } // EventUniquesThawed is emitted when some asset instance was thawed type EventUniquesThawed struct { - Phase Phase - ClassID U64 - InstanceID U128 - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + Topics []Hash } // EventUniquesTransferred is emitted when some asset instance was transferred type EventUniquesTransferred struct { - Phase Phase - ClassID U64 - InstanceID U128 - From AccountID - To AccountID - Topics []Hash + Phase Phase + CollectionID U64 + ItemID U128 + From AccountID + To AccountID + Topics []Hash } // EventUMPInvalidFormat is emitted when the upward message is invalid XCM. diff --git a/types/events_test.go b/types/events_test.go index f28c10acb..ea65189d5 100644 --- a/types/events_test.go +++ b/types/events_test.go @@ -22,21 +22,22 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/scale" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) var ( - dispatchInfoFuzzOpts = combineFuzzOpts( + dispatchInfoFuzzOpts = CombineFuzzOpts( dispatchClassFuzzOpts, paysFuzzOpts, ) ) func TestDispatchInfo_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[DispatchInfo](t, 100, dispatchInfoFuzzOpts...) - assertDecodeNilData[DispatchInfo](t) - assertEncodeEmptyObj[DispatchInfo](t, 8) + AssertRoundTripFuzz[DispatchInfo](t, 100, dispatchInfoFuzzOpts...) + AssertDecodeNilData[DispatchInfo](t) + AssertEncodeEmptyObj[DispatchInfo](t, 8) } func TestVoteThreshold_Decoder(t *testing.T) { @@ -63,10 +64,10 @@ func TestVoteThreshold_Encode(t *testing.T) { } var ( - dispatchResultFuzzOpts = combineFuzzOpts( + dispatchResultFuzzOpts = CombineFuzzOpts( dispatchErrorFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(d *DispatchResult, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(d *DispatchResult, c fuzz.Continue) { if c.RandBool() { d.Ok = true return @@ -79,37 +80,14 @@ var ( ) func TestDispatchResult_Decode(t *testing.T) { - assertRoundTripFuzz[DispatchResult](t, 100, dispatchResultFuzzOpts...) - assertDecodeNilData[DispatchResult](t) - assertEncodeEmptyObj[DispatchResult](t, 1) -} - -func TestProxyTypeEncodeDecode(t *testing.T) { - // encode - pt := Governance - var buf bytes.Buffer - encoder := scale.NewEncoder(&buf) - assert.NoError(t, encoder.Encode(pt)) - assert.Equal(t, buf.Len(), 1) - assert.Equal(t, buf.Bytes(), []byte{2}) - - //decode - decoder := scale.NewDecoder(bytes.NewReader(buf.Bytes())) - pt0 := ProxyType(0) - err := decoder.Decode(&pt0) - assert.NoError(t, err) - assert.Equal(t, pt0, Governance) - - //decode error - decoder = scale.NewDecoder(bytes.NewReader([]byte{5})) - pt0 = ProxyType(0) - err = decoder.Decode(&pt0) - assert.Error(t, err) + AssertRoundTripFuzz[DispatchResult](t, 100, dispatchResultFuzzOpts...) + AssertDecodeNilData[DispatchResult](t) + AssertEncodeEmptyObj[DispatchResult](t, 1) } var ( - paysFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(p *Pays, c fuzz.Continue) { + paysFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(p *Pays, c fuzz.Continue) { r := c.RandBool() p.IsYes = r p.IsNo = !r @@ -118,14 +96,14 @@ var ( ) func TestPaysEncodeDecode(t *testing.T) { - assertRoundTripFuzz[Pays](t, 1000, paysFuzzOpts...) - assertDecodeNilData[Pays](t) - assertEncodeEmptyObj[Pays](t, 0) + AssertRoundTripFuzz[Pays](t, 1000, paysFuzzOpts...) + AssertDecodeNilData[Pays](t) + AssertEncodeEmptyObj[Pays](t, 0) } var ( - dispatchClassFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(d *DispatchClass, c fuzz.Continue) { + dispatchClassFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(d *DispatchClass, c fuzz.Continue) { switch c.Intn(3) { case 0: d.IsNormal = true @@ -139,30 +117,30 @@ var ( ) func TestDispatchClassEncodeDecode(t *testing.T) { - assertRoundTripFuzz[DispatchClass](t, 100, dispatchClassFuzzOpts...) - assertDecodeNilData[DispatchClass](t) - assertEncodeEmptyObj[DispatchClass](t, 0) + AssertRoundTripFuzz[DispatchClass](t, 100, dispatchClassFuzzOpts...) + AssertDecodeNilData[DispatchClass](t) + AssertEncodeEmptyObj[DispatchClass](t, 0) } var ( - democracyConvictionFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(d *DemocracyConviction, c fuzz.Continue) { + democracyConvictionFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(d *DemocracyConviction, c fuzz.Continue) { *d = DemocracyConviction(c.Intn(7)) }), } ) func TestDemocracyConviction_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[DemocracyConviction](t, 100, democracyConvictionFuzzOpts...) - assertDecodeNilData[DemocracyConviction](t) - assertEncodeEmptyObj[DemocracyConviction](t, 1) + AssertRoundTripFuzz[DemocracyConviction](t, 100, democracyConvictionFuzzOpts...) + AssertDecodeNilData[DemocracyConviction](t) + AssertEncodeEmptyObj[DemocracyConviction](t, 1) } var ( - voteAccountVoteFuzzOpts = combineFuzzOpts( + voteAccountVoteFuzzOpts = CombineFuzzOpts( democracyConvictionFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(v *VoteAccountVote, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(v *VoteAccountVote, c fuzz.Continue) { if c.RandBool() { v.IsStandard = true c.Fuzz(&v.AsStandard) @@ -177,21 +155,21 @@ var ( ) func TestVoteAccountVote_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[VoteAccountVote](t, 100, voteAccountVoteFuzzOpts...) - assertDecodeNilData[VoteAccountVote](t) - assertEncodeEmptyObj[VoteAccountVote](t, 0) + AssertRoundTripFuzz[VoteAccountVote](t, 100, voteAccountVoteFuzzOpts...) + AssertDecodeNilData[VoteAccountVote](t) + AssertEncodeEmptyObj[VoteAccountVote](t, 0) } var ( - schedulerLookupErrorFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(e *SchedulerLookupError, c fuzz.Continue) { + schedulerLookupErrorFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(e *SchedulerLookupError, c fuzz.Continue) { *e = SchedulerLookupError(c.Intn(2)) }), } ) func TestSchedulerLookupError_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[SchedulerLookupError](t, 100, schedulerLookupErrorFuzzOpts...) - assertDecodeNilData[SchedulerLookupError](t) - assertEncodeEmptyObj[SchedulerLookupError](t, 1) + AssertRoundTripFuzz[SchedulerLookupError](t, 100, schedulerLookupErrorFuzzOpts...) + AssertDecodeNilData[SchedulerLookupError](t) + AssertEncodeEmptyObj[SchedulerLookupError](t, 1) } diff --git a/types/example_enum_test.go b/types/example_enum_test.go index 76a2b758c..665031da3 100644 --- a/types/example_enum_test.go +++ b/types/example_enum_test.go @@ -21,7 +21,7 @@ import ( "reflect" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // PhaseEnum is an enum example. Since Go has no enums, it is implemented as a struct with flags for each diff --git a/types/example_struct_test.go b/types/example_struct_test.go index 7efe2e9cf..c9445c30d 100644 --- a/types/example_struct_test.go +++ b/types/example_struct_test.go @@ -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" ) func ExampleExampleStruct() { diff --git a/types/example_tuple_test.go b/types/example_tuple_test.go index 73cfffc1d..303966a1a 100644 --- a/types/example_tuple_test.go +++ b/types/example_tuple_test.go @@ -20,7 +20,7 @@ import ( "fmt" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" - + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" "golang.org/x/crypto/blake2b" ) diff --git a/types/example_vec_any_test.go b/types/example_vec_any_test.go index cf08e05cf..6ad8cde22 100644 --- a/types/example_vec_any_test.go +++ b/types/example_vec_any_test.go @@ -21,7 +21,7 @@ import ( "reflect" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // MyVal is a custom type that is used to hold arbitrarily encoded data. In this example, we encode uint8s with a 0x00 diff --git a/types/example_vec_test.go b/types/example_vec_test.go index c41186853..e99476d91 100644 --- a/types/example_vec_test.go +++ b/types/example_vec_test.go @@ -20,7 +20,7 @@ import ( "fmt" "reflect" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) func ExampleExampleVec_simple() { diff --git a/types/execution_result_test.go b/types/execution_result_test.go index b7ddd9f03..67e5e3bf9 100644 --- a/types/execution_result_test.go +++ b/types/execution_result_test.go @@ -19,11 +19,11 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" ) var ( @@ -36,10 +36,10 @@ var ( executionResultFuzzOpts = xcmErrorFuzzOpts - optionExecutionResultFuzzOpts = combineFuzzOpts( + optionExecutionResultFuzzOpts = CombineFuzzOpts( executionResultFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(o *OptionExecutionResult, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(o *OptionExecutionResult, c fuzz.Continue) { if c.RandBool() { *o = NewOptionExecutionResultEmpty() return @@ -56,18 +56,18 @@ var ( ) func TestOptionExecutionResult_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionExecutionResult](t, 1000, optionExecutionResultFuzzOpts...) + AssertRoundTripFuzz[OptionExecutionResult](t, 1000, optionExecutionResultFuzzOpts...) } func TestOptionExecutionResult_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionExecutionResult(testExecutionResult), MustHexDecodeString("0x010100000000")}, {NewOptionExecutionResultEmpty(), MustHexDecodeString("0x00")}, }) } func TestOptionExecutionResult_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x010100000000"), NewOptionExecutionResult(testExecutionResult)}, {MustHexDecodeString("0x00"), NewOptionBytesEmpty()}, }) @@ -89,19 +89,19 @@ func TestOptionExecutionResult_OptionMethods(t *testing.T) { } func TestExecutionResult_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[ExecutionResult](t, 1000, executionResultFuzzOpts...) - assertDecodeNilData[ExecutionResult](t) - assertEncodeEmptyObj[ExecutionResult](t, 4) + AssertRoundTripFuzz[ExecutionResult](t, 1000, executionResultFuzzOpts...) + AssertDecodeNilData[ExecutionResult](t) + AssertEncodeEmptyObj[ExecutionResult](t, 4) } func TestExecutionResult_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testExecutionResult, MustHexDecodeString("0x0100000000")}, }) } func TestExecutionResult_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0100000000"), testExecutionResult}, }) } diff --git a/types/extrinsic.go b/types/extrinsic.go index bc2be89af..ecb437f47 100644 --- a/types/extrinsic.go +++ b/types/extrinsic.go @@ -26,6 +26,7 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/scale" "github.com/centrifuge/go-substrate-rpc-client/v4/signature" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) const ( @@ -71,38 +72,38 @@ func (e *Extrinsic) UnmarshalJSON(bz []byte) error { // extrinsics didn't have the length, cater for both approaches. This is very // inconsistent with any other `Vec` implementation var l UCompact - err := DecodeFromHex(tmp, &l) + err := codec.DecodeFromHex(tmp, &l) if err != nil { return err } - prefix, err := EncodeToHex(l) + prefix, err := codec.EncodeToHex(l) if err != nil { return err } // determine whether length prefix is there if strings.HasPrefix(tmp, prefix) { - return DecodeFromHex(tmp, e) + return codec.DecodeFromHex(tmp, e) } // not there, prepend with compact encoded length prefix - dec, err := HexDecodeString(tmp) + dec, err := codec.HexDecodeString(tmp) if err != nil { return err } length := NewUCompactFromUInt(uint64(len(dec))) - bprefix, err := Encode(length) + bprefix, err := codec.Encode(length) if err != nil { return err } bprefix = append(bprefix, dec...) - return Decode(bprefix, e) + return codec.Decode(bprefix, e) } // MarshalJSON returns a JSON encoded byte array of Extrinsic func (e Extrinsic) MarshalJSON() ([]byte, error) { - s, err := EncodeToHex(e) + s, err := codec.EncodeToHex(e) if err != nil { return nil, err } @@ -125,7 +126,7 @@ func (e *Extrinsic) Sign(signer signature.KeyringPair, o SignatureOptions) error return fmt.Errorf("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(), e.Type()) } - mb, err := Encode(e.Method) + mb, err := codec.Encode(e.Method) if err != nil { return err } @@ -148,7 +149,11 @@ func (e *Extrinsic) Sign(signer signature.KeyringPair, o SignatureOptions) error TransactionVersion: o.TransactionVersion, } - signerPubKey := NewMultiAddressFromAccountID(signer.PublicKey) + signerPubKey, err := NewMultiAddressFromAccountID(signer.PublicKey) + + if err != nil { + return err + } sig, err := payload.Sign(signer) if err != nil { @@ -267,7 +272,7 @@ func NewCall(m *Metadata, call string, args ...interface{}) (Call, error) { var a []byte for _, arg := range args { - e, err := Encode(arg) + e, err := codec.Encode(arg) if err != nil { return Call{}, err } diff --git a/types/extrinsic_era_test.go b/types/extrinsic_era_test.go index ea03528bf..1e8645834 100644 --- a/types/extrinsic_era_test.go +++ b/types/extrinsic_era_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) @@ -42,8 +43,8 @@ func TestExtrinsicEra_Mortal(t *testing.T) { } var ( - extrinsicEraFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(e *ExtrinsicEra, c fuzz.Continue) { + extrinsicEraFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(e *ExtrinsicEra, c fuzz.Continue) { if c.RandBool() { e.IsImmortalEra = true return @@ -62,7 +63,7 @@ func TestExtrinsicEra_EncodeDecode(t *testing.T) { var e ExtrinsicEra err := DecodeFromHex("0x4e9c", &e) assert.NoError(t, err) - assertRoundtrip(t, e) + AssertRoundtrip(t, e) - assertRoundTripFuzz[ExtrinsicEra](t, 1000, extrinsicEraFuzzOpts...) + AssertRoundTripFuzz[ExtrinsicEra](t, 1000, extrinsicEraFuzzOpts...) } diff --git a/types/extrinsic_payload.go b/types/extrinsic_payload.go index 7327aa0d7..2a9894f8e 100644 --- a/types/extrinsic_payload.go +++ b/types/extrinsic_payload.go @@ -21,6 +21,7 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/scale" "github.com/centrifuge/go-substrate-rpc-client/v4/signature" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // ExtrinsicPayloadV3 is a signing payload for an Extrinsic. For the final encoding, it is variable length based on @@ -40,7 +41,7 @@ type ExtrinsicPayloadV3 struct { // Sign the extrinsic payload with the given derivation path func (e ExtrinsicPayloadV3) Sign(signer signature.KeyringPair) (Signature, error) { - b, err := Encode(e) + b, err := codec.Encode(e) if err != nil { return Signature{}, err } @@ -102,7 +103,7 @@ type ExtrinsicPayloadV4 struct { // Sign the extrinsic payload with the given derivation path func (e ExtrinsicPayloadV4) Sign(signer signature.KeyringPair) (Signature, error) { - b, err := Encode(e) + b, err := codec.Encode(e) if err != nil { return Signature{}, err } diff --git a/types/extrinsic_payload_test.go b/types/extrinsic_payload_test.go index 2604e9903..768e393c8 100644 --- a/types/extrinsic_payload_test.go +++ b/types/extrinsic_payload_test.go @@ -21,10 +21,10 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - fuzz "github.com/google/gofuzz" - "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" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) diff --git a/types/extrinsic_signature_test.go b/types/extrinsic_signature_test.go index d86f03f16..8e6164f88 100644 --- a/types/extrinsic_signature_test.go +++ b/types/extrinsic_signature_test.go @@ -21,13 +21,14 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) var ( - extrinsicSignatureV3FuzzOpts = combineFuzzOpts( + extrinsicSignatureV3FuzzOpts = CombineFuzzOpts( addressFuzzOpts, extrinsicEraFuzzOpts, ) @@ -45,13 +46,13 @@ func TestExtrinsicSignatureV3_EncodeDecode(t *testing.T) { assert.Equal(t, sig, sigDec) - assertRoundTripFuzz[ExtrinsicSignatureV3](t, 1000, extrinsicSignatureV3FuzzOpts...) - assertDecodeNilData[ExtrinsicSignatureV3](t) - assertEncodeEmptyObj[ExtrinsicSignatureV3](t, 69) + AssertRoundTripFuzz[ExtrinsicSignatureV3](t, 1000, extrinsicSignatureV3FuzzOpts...) + AssertDecodeNilData[ExtrinsicSignatureV3](t) + AssertEncodeEmptyObj[ExtrinsicSignatureV3](t, 69) } var ( - extrinsicSignatureV4FuzzOpts = combineFuzzOpts( + extrinsicSignatureV4FuzzOpts = CombineFuzzOpts( multiAddressFuzzOpts, multiSignatureFuzzOpts, extrinsicEraFuzzOpts, @@ -71,8 +72,8 @@ func TestExtrinsicSignatureV4_EncodeDecode(t *testing.T) { assert.Equal(t, sig, sigDec) - assertRoundTripFuzz[ExtrinsicSignatureV4](t, 1000, extrinsicSignatureV4FuzzOpts...) - assertDecodeNilData[ExtrinsicSignatureV4](t) + AssertRoundTripFuzz[ExtrinsicSignatureV4](t, 1000, extrinsicSignatureV4FuzzOpts...) + AssertDecodeNilData[ExtrinsicSignatureV4](t) var ( ext ExtrinsicSignatureV4 diff --git a/types/extrinsic_status_test.go b/types/extrinsic_status_test.go index eb048aec8..fa2132104 100644 --- a/types/extrinsic_status_test.go +++ b/types/extrinsic_status_test.go @@ -20,9 +20,10 @@ import ( "encoding/json" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) @@ -38,8 +39,8 @@ var testExtrinsicStatus8 = ExtrinsicStatus{IsDropped: true} var testExtrinsicStatus9 = ExtrinsicStatus{IsInvalid: true} var ( - extrinsicStatusFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(e *ExtrinsicStatus, c fuzz.Continue) { + extrinsicStatusFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(e *ExtrinsicStatus, c fuzz.Continue) { switch c.Intn(10) { case 0: e.IsFuture = true @@ -73,23 +74,23 @@ var ( ) func TestExtrinsicStatus_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testExtrinsicStatus0) - assertRoundtrip(t, testExtrinsicStatus1) - assertRoundtrip(t, testExtrinsicStatus2) - assertRoundtrip(t, testExtrinsicStatus3) - assertRoundtrip(t, testExtrinsicStatus4) - assertRoundtrip(t, testExtrinsicStatus5) - assertRoundtrip(t, testExtrinsicStatus6) - assertRoundtrip(t, testExtrinsicStatus7) - assertRoundtrip(t, testExtrinsicStatus8) - assertRoundtrip(t, testExtrinsicStatus9) - assertRoundTripFuzz[ExtrinsicStatus](t, 1000, extrinsicStatusFuzzOpts...) - assertDecodeNilData[ExtrinsicStatus](t) - assertEncodeEmptyObj[ExtrinsicStatus](t, 0) + AssertRoundtrip(t, testExtrinsicStatus0) + AssertRoundtrip(t, testExtrinsicStatus1) + AssertRoundtrip(t, testExtrinsicStatus2) + AssertRoundtrip(t, testExtrinsicStatus3) + AssertRoundtrip(t, testExtrinsicStatus4) + AssertRoundtrip(t, testExtrinsicStatus5) + AssertRoundtrip(t, testExtrinsicStatus6) + AssertRoundtrip(t, testExtrinsicStatus7) + AssertRoundtrip(t, testExtrinsicStatus8) + AssertRoundtrip(t, testExtrinsicStatus9) + AssertRoundTripFuzz[ExtrinsicStatus](t, 1000, extrinsicStatusFuzzOpts...) + AssertDecodeNilData[ExtrinsicStatus](t) + AssertEncodeEmptyObj[ExtrinsicStatus](t, 0) } func TestExtrinsicStatus_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testExtrinsicStatus0, []byte{0x00}}, {testExtrinsicStatus1, []byte{0x01}}, {testExtrinsicStatus2, MustHexDecodeString("0x020c10546869730869732462726f616463617374")}, @@ -104,7 +105,7 @@ func TestExtrinsicStatus_Encode(t *testing.T) { } func TestExtrinsicStatus_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {[]byte{0x00}, testExtrinsicStatus0}, {[]byte{0x01}, testExtrinsicStatus1}, {MustHexDecodeString("0x020c10546869730869732462726f616463617374"), testExtrinsicStatus2}, diff --git a/types/extrinsic_test.go b/types/extrinsic_test.go index 8088b7db8..58e61ac14 100644 --- a/types/extrinsic_test.go +++ b/types/extrinsic_test.go @@ -22,6 +22,8 @@ import ( "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/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) @@ -65,10 +67,12 @@ func TestExtrinsic_Signed_EncodeDecode(t *testing.T) { } func TestExtrinsic_Sign(t *testing.T) { - c, err := NewCall(ExamplaryMetadataV4, - "balances.transfer", NewAddressFromAccountID(MustHexDecodeString( - "0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")), - NewUCompactFromUInt(6969)) + c, err := NewCall( + ExamplaryMetadataV4, + "balances.transfer", + newTestAddress(), + NewUCompactFromUInt(6969), + ) assert.NoError(t, err) ext := NewExtrinsic(c) @@ -184,15 +188,17 @@ func ExampleExtrinsic() { } func TestExtrinsic_JSONMarshalUnmarshal(t *testing.T) { - c, err := NewCall(ExamplaryMetadataV4, - "balances.transfer", NewAddressFromAccountID(MustHexDecodeString( - "0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")), - NewUCompactFromUInt(6969)) + c, err := NewCall( + ExamplaryMetadataV4, + "balances.transfer", + newTestAddress(), + NewUCompactFromUInt(6969), + ) assert.NoError(t, err) ext := NewExtrinsic(c) - assertJSONRoundTrip(t, &ext) + AssertJSONRoundTrip(t, &ext) } func TestCall(t *testing.T) { diff --git a/types/generic.go b/types/generic.go new file mode 100644 index 000000000..20a9a7b8a --- /dev/null +++ b/types/generic.go @@ -0,0 +1,48 @@ +package types + +import "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + +type Option[T any] struct { + hasValue bool + value T +} + +func NewOption[T any](t T) Option[T] { + return Option[T]{hasValue: true, value: t} +} + +func NewEmptyOption[T any]() Option[T] { + return Option[T]{hasValue: false} +} + +func (o *Option[T]) Decode(decoder scale.Decoder) error { + return decoder.DecodeOption(&o.hasValue, &o.value) +} + +func (o Option[T]) Encode(encoder scale.Encoder) error { + return encoder.EncodeOption(o.hasValue, o.value) +} + +// SetSome sets a value +func (o *Option[T]) SetSome(value T) { + o.hasValue = true + o.value = value +} + +// SetNone removes a value and marks it as missing +func (o *Option[T]) SetNone() { + o.hasValue = false + + var val T + + o.value = val +} + +// Unwrap returns a flag that indicates whether a value is present and the stored value +func (o *Option[T]) Unwrap() (ok bool, value T) { + return o.hasValue, o.value +} + +func (o *Option[T]) HasValue() bool { + return o.hasValue +} diff --git a/types/generic_test.go b/types/generic_test.go new file mode 100644 index 000000000..c0a2b4691 --- /dev/null +++ b/types/generic_test.go @@ -0,0 +1,83 @@ +package types_test + +import ( + "testing" + + . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" +) + +var ( + optionFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(o *Option[U64], c fuzz.Continue) { + if c.RandBool() { + *o = NewEmptyOption[U64]() + return + } + + var u U64 + + c.Fuzz(&u) + + *o = NewOption[U64](u) + }), + } +) + +func TestOption_EncodeDecode(t *testing.T) { + AssertRoundTripFuzz[Option[U64]](t, 100, optionFuzzOpts...) + AssertEncodeEmptyObj[Option[U64]](t, 1) + + testOptionEncodeLen[U64](11, t) + + accountID := newTestAccountID() + testOptionEncodeLen[AccountID](accountID, t) + + testOptionEncodeLen[ItemDetails](testInstanceDetails, t) +} + +func testOptionEncodeLen[T any](testVal T, t *testing.T) { + valEnc, err := Encode(testVal) + assert.NoError(t, err) + + opt := NewOption[T](testVal) + optEnc, err := Encode(opt) + + assert.Equal(t, len(optEnc), len(valEnc)+1) +} + +func TestOption_OptionMethods(t *testing.T) { + testOptionMethods[U64](11, t) + + accountID := newTestAccountID() + + testOptionMethods[*AccountID](&accountID, t) + testOptionMethods[AccountID](accountID, t) + + testOptionMethods[*ItemDetails](&testInstanceDetails, t) + testOptionMethods[ItemDetails](testInstanceDetails, t) +} + +func testOptionMethods[T any](testVal T, t *testing.T) { + o := NewEmptyOption[T]() + + var emptyVal T + + hasValue, value := o.Unwrap() + assert.False(t, hasValue) + assert.Equal(t, emptyVal, value) + + o.SetSome(testVal) + + hasValue, value = o.Unwrap() + assert.True(t, hasValue) + assert.Equal(t, testVal, value) + + o.SetNone() + hasValue, value = o.Unwrap() + assert.False(t, hasValue) + assert.Equal(t, emptyVal, value) +} diff --git a/types/hash.go b/types/hash.go index c7abfc0a6..804db86e8 100644 --- a/types/hash.go +++ b/types/hash.go @@ -19,6 +19,8 @@ package types import ( "encoding/json" "fmt" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // H160 is a hash containing 160 bits (20 bytes), typically used in blocks, extrinsics and as a sane default @@ -78,7 +80,7 @@ func NewHash(b []byte) Hash { // NewHashFromHexString creates a new Hash type from a hex string func NewHashFromHexString(s string) (Hash, error) { - bz, err := HexDecodeString(s) + bz, err := codec.HexDecodeString(s) if err != nil { return Hash{}, err } diff --git a/types/hash_test.go b/types/hash_test.go index a43bf6c30..f9b182661 100644 --- a/types/hash_test.go +++ b/types/hash_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) var hash20 = []byte{ @@ -29,30 +30,30 @@ var hash20 = []byte{ } func TestH160_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewH160(hash20)) - assertRoundTripFuzz[H160](t, 100) - assertDecodeNilData[H160](t) - assertEncodeEmptyObj[H160](t, 20) + AssertRoundtrip(t, NewH160(hash20)) + AssertRoundTripFuzz[H160](t, 100) + AssertDecodeNilData[H160](t) + AssertEncodeEmptyObj[H160](t, 20) } func TestH160_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewH160(hash20), 20}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewH160(hash20), 20}}) } func TestH160_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewH160(hash20), MustHexDecodeString("0x0102030405060708090001020304050607080900")}, }) } func TestH160_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewH160(hash20), MustHexDecodeString("0xdb34a42220dae1c95e29ee2e97d09995887790554cb1ac302680934ffc636b82")}, }) } func TestH160_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewH160(hash20), "0x0102030405060708090001020304050607080900"}, }) @@ -60,13 +61,13 @@ func TestH160_Hex(t *testing.T) { } func TestH160_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewH160(hash20), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0]"}, }) } func TestH160_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewH160(hash20), NewH160(hash20), true}, {NewH160(hash20), NewBytes(hash20), false}, {NewH160(hash20), NewBool(false), false}, @@ -79,30 +80,30 @@ var hash32 = []byte{ } func TestH256_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewH256(hash32)) - assertRoundTripFuzz[H256](t, 100) - assertDecodeNilData[H256](t) - assertEncodeEmptyObj[H256](t, 32) + AssertRoundtrip(t, NewH256(hash32)) + AssertRoundTripFuzz[H256](t, 100) + AssertDecodeNilData[H256](t) + AssertEncodeEmptyObj[H256](t, 32) } func TestH256_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewH256(hash32), 32}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewH256(hash32), 32}}) } func TestH256_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewH256(hash32), MustHexDecodeString("0x0102030405060708090001020304050607080900010203040506070809000102")}, }) } func TestH256_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewH256(hash32), MustHexDecodeString("0x95248da71ae3de701a70523b32a1e5a982de1e49dcf9891e188748e8f9189a2c")}, }) } func TestH256_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewH256(hash32), "0x0102030405060708090001020304050607080900010203040506070809000102"}, }) @@ -111,13 +112,13 @@ func TestH256_Hex(t *testing.T) { } func TestH256_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewH256(hash32), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2]"}, }) } func TestH256_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewH256(hash32), NewH256(hash32), true}, {NewH256(hash32), NewBytes(hash32), false}, {NewH256(hash32), NewBool(false), false}, @@ -125,44 +126,44 @@ func TestH256_Eq(t *testing.T) { } func TestHash_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewHash(hash32)) - assertRoundTripFuzz[Hash](t, 100) - assertDecodeNilData[Hash](t) - assertEncodeEmptyObj[Hash](t, 32) + AssertRoundtrip(t, NewHash(hash32)) + AssertRoundTripFuzz[Hash](t, 100) + AssertDecodeNilData[Hash](t) + AssertEncodeEmptyObj[Hash](t, 32) } func TestHash_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewHash(hash32), 32}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewHash(hash32), 32}}) } func TestHash_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewHash(hash32), MustHexDecodeString("0x0102030405060708090001020304050607080900010203040506070809000102")}, }) } func TestHash_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewHash(hash32), MustHexDecodeString("0x95248da71ae3de701a70523b32a1e5a982de1e49dcf9891e188748e8f9189a2c")}, }) } func TestHash_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewHash(hash32), "0x0102030405060708090001020304050607080900010203040506070809000102"}, }) - assertEqual(t, "0x0102030405060708090001020304050607080900010203040506070809000102", NewHash(hash32).Hex()) + AssertEqual(t, "0x0102030405060708090001020304050607080900010203040506070809000102", NewHash(hash32).Hex()) } func TestHash_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewHash(hash32), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2]"}, }) } func TestHash_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewHash(hash32), NewHash(hash32), true}, {NewHash(hash32), NewBytes(hash32), false}, {NewHash(hash32), NewBool(false), false}, @@ -182,42 +183,42 @@ var hash65 = []byte{ } func TestH512_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewH512(hash64)) - assertRoundTripFuzz[H512](t, 100) - assertDecodeNilData[H512](t) - assertEncodeEmptyObj[H512](t, 64) + AssertRoundtrip(t, NewH512(hash64)) + AssertRoundTripFuzz[H512](t, 100) + AssertDecodeNilData[H512](t) + AssertEncodeEmptyObj[H512](t, 64) } func TestH512_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewH512(hash64), 64}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewH512(hash64), 64}}) } func TestH512_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewH512(hash64), MustHexDecodeString("0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304")}, //nolint:lll }) } func TestH512_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewH512(hash64), MustHexDecodeString("0x893a41fa8d4e6447fe2d74a3ae529b1f1a13f3ac5a194907bf19e78e084a0ef6")}, }) } func TestH512_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewH512(hash64), "0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304"}, //nolint:lll }) } func TestH512_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewH512(hash64), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4]"}, //nolint:lll }) } func TestH512_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewH512(hash64), NewH512(hash64), true}, {NewH512(hash64), NewBytes(hash64), false}, {NewH512(hash64), NewBool(false), false}, diff --git a/types/header_test.go b/types/header_test.go index 9af085a92..8d40a23b5 100644 --- a/types/header_test.go +++ b/types/header_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var exampleHeader = Header{ @@ -37,13 +39,13 @@ var exampleHeader = Header{ } func TestBlockNumber_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[BlockNumber](t, 100) - assertEncodeEmptyObj[BlockNumber](t, 1) + AssertRoundTripFuzz[BlockNumber](t, 100) + AssertEncodeEmptyObj[BlockNumber](t, 1) } func TestBlockNumber_JSONMarshalUnmarshal(t *testing.T) { b := BlockNumber(1) - assertJSONRoundTrip(t, &b) + AssertJSONRoundTrip(t, &b) } var ( @@ -51,30 +53,30 @@ var ( ) func TestHeader_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleHeader) - assertRoundTripFuzz[Header](t, 100, headerFuzzOpts...) - assertDecodeNilData[Header](t) - assertEncodeEmptyObj[Header](t, 98) + AssertRoundtrip(t, exampleHeader) + AssertRoundTripFuzz[Header](t, 100, headerFuzzOpts...) + AssertDecodeNilData[Header](t) + AssertEncodeEmptyObj[Header](t, 98) } func TestHeader_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{exampleHeader, 162}}) + AssertEncodedLength(t, []EncodedLengthAssert{{exampleHeader, 162}}) } func TestHeader_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {exampleHeader, MustHexDecodeString("0x0102030405000000000000000000000000000000000000000000000000000000a802030405060000000000000000000000000000000000000000000000000000000304050607000000000000000000000000000000000000000000000000000000140008040502060700000000000000000000000000000000000000000000000000000000000004090000000c0a0b0c050b0000000c0c0d0e060d0000000c0e0f10")}, //nolint:lll }) } func TestHeader_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {exampleHeader, "0x0102030405000000000000000000000000000000000000000000000000000000a802030405060000000000000000000000000000000000000000000000000000000304050607000000000000000000000000000000000000000000000000000000140008040502060700000000000000000000000000000000000000000000000000000000000004090000000c0a0b0c050b0000000c0c0d0e060d0000000c0e0f10"}, //nolint:lll }) } func TestHeader_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {exampleHeader, exampleHeader, true}, {exampleHeader, NewBytes(hash64), false}, {exampleHeader, NewBool(false), false}, diff --git a/types/health_test.go b/types/health_test.go index 494855178..9af7a8cdd 100644 --- a/types/health_test.go +++ b/types/health_test.go @@ -20,19 +20,21 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestHealth_EncodeDecode(t *testing.T) { - assertRoundtrip(t, Health{3, false, true}) - assertRoundtrip(t, Health{1, true, true}) - assertRoundtrip(t, Health{0, true, false}) - assertRoundTripFuzz[Health](t, 100) - assertDecodeNilData[Health](t) - assertEncodeEmptyObj[Health](t, 10) + AssertRoundtrip(t, Health{3, false, true}) + AssertRoundtrip(t, Health{1, true, true}) + AssertRoundtrip(t, Health{0, true, false}) + AssertRoundTripFuzz[Health](t, 100) + AssertDecodeNilData[Health](t) + AssertEncodeEmptyObj[Health](t, 10) } func TestHealth_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {Health{3, false, true}, MustHexDecodeString("0x03000000000000000001")}, {Health{1, true, true}, MustHexDecodeString("0x01000000000000000101")}, {Health{0, true, false}, MustHexDecodeString("0x00000000000000000100")}, @@ -40,7 +42,7 @@ func TestHealth_Encode(t *testing.T) { } func TestHealth_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x03000000000000000001"), Health{3, false, true}}, {MustHexDecodeString("0x01000000000000000101"), Health{1, true, true}}, {MustHexDecodeString("0x00000000000000000100"), Health{0, true, false}}, diff --git a/types/hrmp_test.go b/types/hrmp_test.go index 2d1e7ec9e..f843ae9a1 100644 --- a/types/hrmp_test.go +++ b/types/hrmp_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( @@ -30,19 +32,19 @@ var ( ) func TestHRMPChannelID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[HRMPChannelID](t, 1000) - assertDecodeNilData[HRMPChannelID](t) - assertEncodeEmptyObj[HRMPChannelID](t, 8) + AssertRoundTripFuzz[HRMPChannelID](t, 1000) + AssertDecodeNilData[HRMPChannelID](t) + AssertEncodeEmptyObj[HRMPChannelID](t, 8) } func TestHRMPChannelID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testHRMPChannelID, MustHexDecodeString("0x0b0000002d000000")}, }) } func TestHRMPChannelID_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0b0000002d000000"), testHRMPChannelID}, }) } diff --git a/types/int_test.go b/types/int_test.go index c943a255e..1edafa11f 100644 --- a/types/int_test.go +++ b/types/int_test.go @@ -21,45 +21,47 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) func TestI8_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewI8(0)) - assertRoundtrip(t, NewI8(12)) - assertRoundtrip(t, NewI8(-12)) + AssertRoundtrip(t, NewI8(0)) + AssertRoundtrip(t, NewI8(12)) + AssertRoundtrip(t, NewI8(-12)) } func TestI8_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewI8(-13), 1}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewI8(-13), 1}}) } func TestI8_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewI8(-29), MustHexDecodeString("0xe3")}, }) } func TestI8_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewI8(-29), MustHexDecodeString("0xb683f1b6c99388ff3443b35a0051eeaafdc5e364e771bdfc72c7fd5d2be800bc")}, }) } func TestI8_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewI8(-29), "0xe3"}, }) } func TestI8_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewI8(-29), "-29"}, }) } func TestI8_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewI8(23), NewI8(23), true}, {NewI8(-23), NewI8(23), false}, {NewI8(23), NewU8(23), false}, @@ -69,45 +71,45 @@ func TestI8_Eq(t *testing.T) { func TestI8_JSONMarshalUnmarshal(t *testing.T) { i := NewI8(1) - assertJSONRoundTrip(t, &i) + AssertJSONRoundTrip(t, &i) } func TestI16_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewI16(0)) - assertRoundtrip(t, NewI16(12)) - assertRoundtrip(t, NewI16(-12)) + AssertRoundtrip(t, NewI16(0)) + AssertRoundtrip(t, NewI16(12)) + AssertRoundtrip(t, NewI16(-12)) } func TestI16_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewI16(-13), 2}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewI16(-13), 2}}) } func TestI16_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewI16(-29), MustHexDecodeString("0xe3ff")}, }) } func TestI16_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewI16(-29), MustHexDecodeString("0x39fbf34f574b72d1815c602a2fe95b7af4b5dfd7bc92a2fc0824aa55f8b9d7b2")}, }) } func TestI16_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewI16(-29), "0xe3ff"}, }) } func TestI16_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewI16(-29), "-29"}, }) } func TestI16_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewI16(23), NewI16(23), true}, {NewI16(-23), NewI16(23), false}, {NewI16(23), NewU16(23), false}, @@ -117,45 +119,45 @@ func TestI16_Eq(t *testing.T) { func TestI16_JSONMarshalUnmarshal(t *testing.T) { i := NewI16(1) - assertJSONRoundTrip(t, &i) + AssertJSONRoundTrip(t, &i) } func TestI32_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewI32(0)) - assertRoundtrip(t, NewI32(12)) - assertRoundtrip(t, NewI32(-12)) + AssertRoundtrip(t, NewI32(0)) + AssertRoundtrip(t, NewI32(12)) + AssertRoundtrip(t, NewI32(-12)) } func TestI32_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewI32(-13), 4}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewI32(-13), 4}}) } func TestI32_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewI32(-29), MustHexDecodeString("0xe3ffffff")}, }) } func TestI32_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewI32(-29), MustHexDecodeString("0x6ef9d4772b9d657bfa727862d9690d5bf8b9045943279e95d3ae0743684f1b95")}, }) } func TestI32_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewI32(-29), "0xe3ffffff"}, }) } func TestI32_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewI32(-29), "-29"}, }) } func TestI32_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewI32(23), NewI32(23), true}, {NewI32(-23), NewI32(23), false}, {NewI32(23), NewU32(23), false}, @@ -165,45 +167,45 @@ func TestI32_Eq(t *testing.T) { func TestI32_JSONMarshalUnmarshal(t *testing.T) { i := NewI32(1) - assertJSONRoundTrip(t, &i) + AssertJSONRoundTrip(t, &i) } func TestI64_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewI64(0)) - assertRoundtrip(t, NewI64(12)) - assertRoundtrip(t, NewI64(-12)) + AssertRoundtrip(t, NewI64(0)) + AssertRoundtrip(t, NewI64(12)) + AssertRoundtrip(t, NewI64(-12)) } func TestI64_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewI64(-13), 8}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewI64(-13), 8}}) } func TestI64_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewI64(-29), MustHexDecodeString("0xe3ffffffffffffff")}, }) } func TestI64_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewI64(-29), MustHexDecodeString("0x4d42db2aa4a23bde81a3ad3705220affaa457c56a0135080c71db7783fec8f44")}, }) } func TestI64_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewI64(-29), "0xe3ffffffffffffff"}, }) } func TestI64_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewI64(-29), "-29"}, }) } func TestI64_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewI64(23), NewI64(23), true}, {NewI64(-23), NewI64(23), false}, {NewI64(23), NewU64(23), false}, @@ -213,26 +215,26 @@ func TestI64_Eq(t *testing.T) { func TestI64_JSONMarshalUnmarshal(t *testing.T) { i := NewI64(1) - assertJSONRoundTrip(t, &i) + AssertJSONRoundTrip(t, &i) } func TestI128_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewI128(*big.NewInt(0))) - assertRoundtrip(t, NewI128(*big.NewInt(12))) - assertRoundtrip(t, NewI128(*big.NewInt(-12))) + AssertRoundtrip(t, NewI128(*big.NewInt(0))) + AssertRoundtrip(t, NewI128(*big.NewInt(12))) + AssertRoundtrip(t, NewI128(*big.NewInt(-12))) bigPos := big.NewInt(0) bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) - assertRoundtrip(t, NewI128(*bigPos)) + AssertRoundtrip(t, NewI128(*bigPos)) bigNeg := big.NewInt(0) bigNeg.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) bigNeg.Neg(bigNeg) - assertRoundtrip(t, NewI128(*bigNeg)) + AssertRoundtrip(t, NewI128(*bigNeg)) } func TestI128_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewI128(*big.NewInt(-13)), 16}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewI128(*big.NewInt(-13)), 16}}) } func TestI128_Encode(t *testing.T) { @@ -246,7 +248,7 @@ func TestI128_Encode(t *testing.T) { d := big.NewInt(0).SetBytes([]byte{127, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15}) d.Neg(d) - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewI128(*big.NewInt(0)), MustHexDecodeString("0x00000000000000000000000000000000")}, {NewI128(*big.NewInt(29)), MustHexDecodeString("0x1d000000000000000000000000000000")}, {NewI128(*big.NewInt(-29)), MustHexDecodeString("0xe3ffffffffffffffffffffffffffffff")}, @@ -268,7 +270,7 @@ func TestI128_Decode(t *testing.T) { d := big.NewInt(0).SetBytes([]byte{127, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 15}) d.Neg(d) - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00000000000000000000000000000000"), NewI128(*big.NewInt(0))}, {MustHexDecodeString("0x1d000000000000000000000000000000"), NewI128(*big.NewInt(29))}, {MustHexDecodeString("0xe3ffffffffffffffffffffffffffffff"), NewI128(*big.NewInt(-29))}, @@ -280,26 +282,26 @@ func TestI128_Decode(t *testing.T) { } func TestI128_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewI128(*big.NewInt(-29)), MustHexDecodeString( "0x7f8f93dd36321a50796a2e88df3bc7238abad58361c2051009bc457a000c4de9")}, }) } func TestI128_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewI128(*big.NewInt(-29)), "0xe3ffffffffffffffffffffffffffffff"}, }) } func TestI128_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewI128(*big.NewInt(-29)), "-29"}, }) } func TestI128_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewI128(*big.NewInt(23)), NewI128(*big.NewInt(23)), true}, {NewI128(*big.NewInt(-23)), NewI128(*big.NewInt(23)), false}, {NewI128(*big.NewInt(23)), NewU64(23), false}, @@ -308,24 +310,24 @@ func TestI128_Eq(t *testing.T) { } func TestI256_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewI256(*big.NewInt(0))) - assertRoundtrip(t, NewI256(*big.NewInt(12))) - assertRoundtrip(t, NewI256(*big.NewInt(-12))) + AssertRoundtrip(t, NewI256(*big.NewInt(0))) + AssertRoundtrip(t, NewI256(*big.NewInt(12))) + AssertRoundtrip(t, NewI256(*big.NewInt(-12))) bigPos := big.NewInt(0) bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) - assertRoundtrip(t, NewI256(*bigPos)) + AssertRoundtrip(t, NewI256(*bigPos)) bigNeg := big.NewInt(0) bigNeg.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) bigNeg.Neg(bigNeg) - assertRoundtrip(t, NewI256(*bigNeg)) + AssertRoundtrip(t, NewI256(*bigNeg)) } func TestI256_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewI256(*big.NewInt(-13)), 32}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewI256(*big.NewInt(-13)), 32}}) } func TestI256_Encode(t *testing.T) { @@ -343,7 +345,7 @@ func TestI256_Encode(t *testing.T) { 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) d.Neg(d) - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewI256(*big.NewInt(0)), MustHexDecodeString( "0x0000000000000000000000000000000000000000000000000000000000000000")}, {NewI256(*big.NewInt(29)), MustHexDecodeString( @@ -372,7 +374,7 @@ func TestI256_Decode(t *testing.T) { 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) d.Neg(d) - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0000000000000000000000000000000000000000000000000000000000000000"), NewI256(*big.NewInt(0))}, {MustHexDecodeString("0x1d00000000000000000000000000000000000000000000000000000000000000"), @@ -387,26 +389,26 @@ func TestI256_Decode(t *testing.T) { } func TestI256_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewI256(*big.NewInt(-29)), MustHexDecodeString( "0xca6ae1636199279abc3e15e366ea463cf06829e7816e1ad08c0c15c158dfeba6")}, }) } func TestI256_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewI256(*big.NewInt(-29)), "0xe3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, }) } func TestI256_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewI256(*big.NewInt(-29)), "-29"}, }) } func TestI256_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewI256(*big.NewInt(23)), NewI256(*big.NewInt(23)), true}, {NewI256(*big.NewInt(-23)), NewI256(*big.NewInt(23)), false}, {NewI256(*big.NewInt(23)), NewI128(*big.NewInt(23)), false}, diff --git a/types/instance_details.go b/types/item_details.go similarity index 89% rename from types/instance_details.go rename to types/item_details.go index ba0f92500..086d63480 100644 --- a/types/instance_details.go +++ b/types/item_details.go @@ -18,14 +18,14 @@ package types import "github.com/centrifuge/go-substrate-rpc-client/v4/scale" -type InstanceDetails struct { +type ItemDetails struct { Owner AccountID Approved OptionAccountID IsFrozen bool Deposit U128 } -func (i *InstanceDetails) Decode(decoder scale.Decoder) error { +func (i *ItemDetails) Decode(decoder scale.Decoder) error { if err := decoder.Decode(&i.Owner); err != nil { return err } @@ -39,7 +39,7 @@ func (i *InstanceDetails) Decode(decoder scale.Decoder) error { return decoder.Decode(&i.Deposit) } -func (i InstanceDetails) Encode(encoder scale.Encoder) error { +func (i ItemDetails) Encode(encoder scale.Encoder) error { if err := encoder.Encode(i.Owner); err != nil { return err } diff --git a/types/instance_details_test.go b/types/item_details_test.go similarity index 57% rename from types/instance_details_test.go rename to types/item_details_test.go index 7059a6fb3..8cf8ab86e 100644 --- a/types/instance_details_test.go +++ b/types/item_details_test.go @@ -20,23 +20,24 @@ import ( "math/big" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( - testInstanceDetails = InstanceDetails{ - Owner: NewAccountID([]byte("acc_id")), - Approved: NewOptionAccountID(NewAccountID([]byte("acc_id2"))), + testInstanceDetails = ItemDetails{ + Owner: newTestAccountID(), + Approved: NewOptionAccountID(newTestAccountID()), IsFrozen: true, Deposit: NewU128(*big.NewInt(123)), } - instanceDetailsFuzzOpts = combineFuzzOpts( + instanceDetailsFuzzOpts = CombineFuzzOpts( optionAccountIDFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(i *InstanceDetails, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(i *ItemDetails, c fuzz.Continue) { c.Fuzz(&i.Owner) c.Fuzz(&i.Approved) c.Fuzz(&i.IsFrozen) @@ -47,24 +48,24 @@ var ( ) func TestInstanceDetails_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[InstanceDetails](t, 1000, instanceDetailsFuzzOpts...) - assertDecodeNilData[InstanceDetails](t) - assertEncodeEmptyObj[InstanceDetails](t, 50) + AssertRoundTripFuzz[ItemDetails](t, 1000, instanceDetailsFuzzOpts...) + AssertDecodeNilData[ItemDetails](t) + AssertEncodeEmptyObj[ItemDetails](t, 50) } func TestInstanceDetails_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testInstanceDetails, - MustHexDecodeString("0x6163635f69640000000000000000000000000000000000000000000000000000016163635f69643200000000000000000000000000000000000000000000000000017b000000000000000000000000000000"), + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20010102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20017b000000000000000000000000000000"), }, }) } func TestInstanceDetails_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { - MustHexDecodeString("0x6163635f69640000000000000000000000000000000000000000000000000000016163635f69643200000000000000000000000000000000000000000000000000017b000000000000000000000000000000"), + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20010102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20017b000000000000000000000000000000"), testInstanceDetails, }, }) diff --git a/types/instance_metadata.go b/types/item_metadata.go similarity index 88% rename from types/instance_metadata.go rename to types/item_metadata.go index 730b90079..0d21204e8 100644 --- a/types/instance_metadata.go +++ b/types/item_metadata.go @@ -18,13 +18,13 @@ package types import "github.com/centrifuge/go-substrate-rpc-client/v4/scale" -type InstanceMetadata struct { +type ItemMetadata struct { Deposit U128 Data Bytes IsFrozen bool } -func (i *InstanceMetadata) Decode(decoder scale.Decoder) error { +func (i *ItemMetadata) Decode(decoder scale.Decoder) error { if err := decoder.Decode(&i.Deposit); err != nil { return err } @@ -36,7 +36,7 @@ func (i *InstanceMetadata) Decode(decoder scale.Decoder) error { return decoder.Decode(&i.IsFrozen) } -func (i InstanceMetadata) Encode(encoder scale.Encoder) error { +func (i ItemMetadata) Encode(encoder scale.Encoder) error { if err := encoder.Encode(i.Deposit); err != nil { return err } diff --git a/types/instance_metadata_test.go b/types/item_metadata_test.go similarity index 78% rename from types/instance_metadata_test.go rename to types/item_metadata_test.go index 204e634dc..3a024b72b 100644 --- a/types/instance_metadata_test.go +++ b/types/item_metadata_test.go @@ -21,10 +21,12 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( - testInstanceMetadata = InstanceMetadata{ + testInstanceMetadata = ItemMetadata{ Deposit: NewU128(*big.NewInt(1234)), Data: Bytes("some_data"), IsFrozen: true, @@ -32,19 +34,19 @@ var ( ) func TestInstanceMetadata_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[InstanceMetadata](t, 1000) - assertDecodeNilData[InstanceMetadata](t) - assertEncodeEmptyObj[InstanceMetadata](t, 18) + AssertRoundTripFuzz[ItemMetadata](t, 1000) + AssertDecodeNilData[ItemMetadata](t) + AssertEncodeEmptyObj[ItemMetadata](t, 18) } func TestInstanceMetadata_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testInstanceMetadata, MustHexDecodeString("0xd204000000000000000000000000000024736f6d655f6461746101")}, }) } func TestInstanceMetadata_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0xd204000000000000000000000000000024736f6d655f6461746101"), testInstanceMetadata}, }) } diff --git a/types/junction_v0_test.go b/types/junction_v0_test.go index 196cb1256..d69400599 100644 --- a/types/junction_v0_test.go +++ b/types/junction_v0_test.go @@ -20,9 +20,10 @@ import ( "math/big" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -78,12 +79,12 @@ var ( }, } - junctionV0FuzzOpts = combineFuzzOpts( + junctionV0FuzzOpts = CombineFuzzOpts( networkIDFuzzOpts, bodyIDFuzzOpts, bodyPartFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(j *JunctionV0, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(j *JunctionV0, c fuzz.Continue) { switch c.Intn(10) { case 0: j.IsParent = true @@ -136,13 +137,13 @@ var ( ) func TestJunctionV0_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[JunctionV0](t, 1000, junctionV0FuzzOpts...) - assertDecodeNilData[JunctionV0](t) - assertEncodeEmptyObj[JunctionV0](t, 0) + AssertRoundTripFuzz[JunctionV0](t, 1000, junctionV0FuzzOpts...) + AssertDecodeNilData[JunctionV0](t) + AssertEncodeEmptyObj[JunctionV0](t, 0) } func TestJunctionV0_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testJunction1, MustHexDecodeString("0x00")}, {testJunction2, MustHexDecodeString("0x010b000000")}, {testJunction3, MustHexDecodeString("0x02000c010203")}, @@ -157,7 +158,7 @@ func TestJunctionV0_Encode(t *testing.T) { } func TestJunctionV0_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testJunction1}, {MustHexDecodeString("0x010b000000"), testJunction2}, {MustHexDecodeString("0x02000c010203"), testJunction3}, diff --git a/types/junction_v1_test.go b/types/junction_v1_test.go index f259aadef..65384d8f5 100644 --- a/types/junction_v1_test.go +++ b/types/junction_v1_test.go @@ -20,9 +20,10 @@ import ( "math/big" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -75,12 +76,12 @@ var ( }, } - junctionV1FuzzOpts = combineFuzzOpts( + junctionV1FuzzOpts = CombineFuzzOpts( networkIDFuzzOpts, bodyIDFuzzOpts, bodyPartFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(j *JunctionV1, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(j *JunctionV1, c fuzz.Continue) { switch c.Intn(9) { case 0: j.IsParachain = true @@ -131,13 +132,13 @@ var ( ) func TestJunctionV1_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[JunctionV1](t, 1000, junctionV1FuzzOpts...) - assertDecodeNilData[JunctionV1](t) - assertEncodeEmptyObj[JunctionV1](t, 0) + AssertRoundTripFuzz[JunctionV1](t, 1000, junctionV1FuzzOpts...) + AssertDecodeNilData[JunctionV1](t) + AssertEncodeEmptyObj[JunctionV1](t, 0) } func TestJunctionV1_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testJunctionV1n1, MustHexDecodeString("0x002c")}, {testJunctionV1n2, MustHexDecodeString("0x01000c010203")}, {testJunctionV1n3, MustHexDecodeString("0x02001000000000000000")}, @@ -151,7 +152,7 @@ func TestJunctionV1_Encode(t *testing.T) { } func TestJunctionV1_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x002c"), testJunctionV1n1}, {MustHexDecodeString("0x01000c010203"), testJunctionV1n2}, {MustHexDecodeString("0x02001000000000000000"), testJunctionV1n3}, @@ -243,10 +244,10 @@ var ( }, } - junctionsV1FuzzOpts = combineFuzzOpts( + junctionsV1FuzzOpts = CombineFuzzOpts( junctionV1FuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(j *JunctionsV1, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(j *JunctionsV1, c fuzz.Continue) { switch c.Intn(9) { case 0: j.IsHere = true @@ -289,13 +290,13 @@ var ( ) func TestJunctionsV1_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[JunctionsV1](t, 1000, junctionsV1FuzzOpts...) - assertDecodeNilData[JunctionsV1](t) - assertEncodeEmptyObj[JunctionsV1](t, 0) + AssertRoundTripFuzz[JunctionsV1](t, 1000, junctionsV1FuzzOpts...) + AssertDecodeNilData[JunctionsV1](t) + AssertEncodeEmptyObj[JunctionsV1](t, 0) } func TestJunctionsV1_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testJunctionsV1n1, MustHexDecodeString("0x00"), @@ -336,7 +337,7 @@ func TestJunctionsV1_Encode(t *testing.T) { } func TestJunctionsV1_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x00"), testJunctionsV1n1, diff --git a/types/lottery_call_index_test.go b/types/lottery_call_index_test.go index 5b297f864..465d2cd32 100644 --- a/types/lottery_call_index_test.go +++ b/types/lottery_call_index_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( @@ -30,19 +32,19 @@ var ( ) func TestLotteryCallIndex_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[LotteryCallIndex](t, 100) - assertDecodeNilData[LotteryCallIndex](t) - assertEncodeEmptyObj[LotteryCallIndex](t, 2) + AssertRoundTripFuzz[LotteryCallIndex](t, 100) + AssertDecodeNilData[LotteryCallIndex](t) + AssertEncodeEmptyObj[LotteryCallIndex](t, 2) } func TestLotteryCallIndex_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testLotteryCallIndex, MustHexDecodeString("0x3420")}, }) } func TestLotteryCallIndex_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x3420"), testLotteryCallIndex}, }) } diff --git a/types/metadata.go b/types/metadata.go index b7bd6c714..74e6c25bb 100644 --- a/types/metadata.go +++ b/types/metadata.go @@ -208,7 +208,7 @@ func NewMetadataError(variant Si1Variant) *MetadataError { } } -func (m *Metadata) FindError(moduleIndex U8, errorIndex U8) (*MetadataError, error) { +func (m *Metadata) FindError(moduleIndex U8, errorIndex [4]U8) (*MetadataError, error) { if m.Version != 14 { return nil, fmt.Errorf("invalid metadata version %d", m.Version) } diff --git a/types/metadataV10_test.go b/types/metadataV10_test.go index 3bcfec6d6..2081d290a 100644 --- a/types/metadataV10_test.go +++ b/types/metadataV10_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) @@ -115,7 +117,7 @@ var exampleDoubleMapTypeV10 = DoubleMapTypeV10{ } func TestMetadataV10_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleMetadataV10) + AssertRoundtrip(t, exampleMetadataV10) } func TestFindEventNamesForEventIDV10(t *testing.T) { diff --git a/types/metadataV11_test.go b/types/metadataV11_test.go index 728a6316d..edb18185e 100644 --- a/types/metadataV11_test.go +++ b/types/metadataV11_test.go @@ -20,6 +20,7 @@ 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" ) diff --git a/types/metadataV12_test.go b/types/metadataV12_test.go index 81c879564..3d8ab5fb9 100644 --- a/types/metadataV12_test.go +++ b/types/metadataV12_test.go @@ -20,6 +20,7 @@ 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" ) diff --git a/types/metadataV13_test.go b/types/metadataV13_test.go index 76e1fac7e..b7c2a144a 100644 --- a/types/metadataV13_test.go +++ b/types/metadataV13_test.go @@ -20,6 +20,7 @@ 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" ) diff --git a/types/metadataV14.go b/types/metadataV14.go index 57598d7d4..2faba5797 100644 --- a/types/metadataV14.go +++ b/types/metadataV14.go @@ -144,7 +144,7 @@ func (m *MetadataV14) FindStorageEntryMetadata(module string, fn string) (Storag return nil, fmt.Errorf("module %v not found in metadata", module) } -func (m *MetadataV14) FindError(moduleIndex U8, errorIndex U8) (*MetadataError, error) { +func (m *MetadataV14) FindError(moduleIndex U8, errorIndex [4]U8) (*MetadataError, error) { for _, mod := range m.Pallets { if int(mod.Index) == int(moduleIndex) { if mod.HasErrors { @@ -160,12 +160,12 @@ func (m *MetadataV14) FindError(moduleIndex U8, errorIndex U8) (*MetadataError, } for _, variant := range errType.Def.Variant.Variants { - if variant.Index == errorIndex { + if variant.Index == errorIndex[0] { return NewMetadataError(variant), nil } } - return nil, fmt.Errorf("error at index %d not found", errorIndex) + return nil, fmt.Errorf("error at index 0x%x not found", errorIndex) } return nil, fmt.Errorf("module %d has no errors", moduleIndex) diff --git a/types/metadataV14_test.go b/types/metadataV14_test.go index 00b0faa7e..8a67373a3 100644 --- a/types/metadataV14_test.go +++ b/types/metadataV14_test.go @@ -21,6 +21,7 @@ import ( "github.com/centrifuge/go-substrate-rpc-client/v4/types" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" "github.com/stretchr/testify/assert" ) @@ -298,17 +299,17 @@ func TestMetadataV14_FindError(t *testing.T) { assert.NoError(t, err) // System - SpecVersionNeedsToIncrease - metaErr, err := meta.FindError(0, 1) + metaErr, err := meta.FindError(0, [4]U8{1}) assert.NoError(t, err) assert.NotNil(t, metaErr) // System - no error at index - metaErr, err = meta.FindError(0, 6) + metaErr, err = meta.FindError(0, [4]U8{6}) assert.Error(t, err) assert.Nil(t, metaErr) // No module at index - metaErr, err = meta.FindError(200, 0) + metaErr, err = meta.FindError(200, [4]U8{0}) assert.Error(t, err) assert.Nil(t, metaErr) } diff --git a/types/metadataV4_test.go b/types/metadataV4_test.go index efb95da93..0381b3d8d 100644 --- a/types/metadataV4_test.go +++ b/types/metadataV4_test.go @@ -20,7 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" - + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) @@ -135,47 +136,47 @@ func TestMetadataV4_Decode(t *testing.T) { } func TestMetadataV4_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleMetadataV4) + AssertRoundtrip(t, exampleMetadataV4) } func TestCallIndex_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleCallIndex) + AssertRoundtrip(t, exampleCallIndex) } func TestModuleMetadataV4_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleModuleMetadataV42) + AssertRoundtrip(t, exampleModuleMetadataV42) } func TestStorageFunctionMetadataV4Type_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleStorageFunctionMetadataV4Type) + AssertRoundtrip(t, exampleStorageFunctionMetadataV4Type) } func TestStorageFunctionMetadataV4Map_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleStorageFunctionMetadataV4Map) + AssertRoundtrip(t, exampleStorageFunctionMetadataV4Map) } func TestStorageFunctionMetadataV4DoubleMap_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleStorageFunctionMetadataV4DoubleMap) + AssertRoundtrip(t, exampleStorageFunctionMetadataV4DoubleMap) } func TestFunctionMetadataV4_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleFunctionMetadataV4) + AssertRoundtrip(t, exampleFunctionMetadataV4) } func TestEventMetadataV4_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleEventMetadataV4) + AssertRoundtrip(t, exampleEventMetadataV4) } func TestMapTypeV4_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleMapTypeV4) + AssertRoundtrip(t, exampleMapTypeV4) } func TestDoubleMapTypeV4_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleDoubleMapTypeV4) + AssertRoundtrip(t, exampleDoubleMapTypeV4) } func TestFunctionArgumentMetadata_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleFunctionArgumentMetadata) + AssertRoundtrip(t, exampleFunctionArgumentMetadata) } func TestFindEventNamesForEventIDV4(t *testing.T) { diff --git a/types/metadataV7_test.go b/types/metadataV7_test.go index acc1d986e..6f987967a 100644 --- a/types/metadataV7_test.go +++ b/types/metadataV7_test.go @@ -19,9 +19,9 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) var exampleMetadataV7 = Metadata{ @@ -113,7 +113,7 @@ var exampleModuleConstantMetadataV6 = ModuleConstantMetadataV6{ } func TestMetadataV7_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleMetadataV7) + AssertRoundtrip(t, exampleMetadataV7) } func TestFindEventNamesForEventIDV7(t *testing.T) { diff --git a/types/metadataV8_test.go b/types/metadataV8_test.go index d93ea8a00..2ee051865 100644 --- a/types/metadataV8_test.go +++ b/types/metadataV8_test.go @@ -20,6 +20,7 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) @@ -75,7 +76,7 @@ var exampleErrorMetadataV8 = ErrorMetadataV8{ } func TestMetadataV8_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleMetadataV8) + AssertRoundtrip(t, exampleMetadataV8) } func TestFindEventNamesForEventIDV8(t *testing.T) { diff --git a/types/metadataV9_test.go b/types/metadataV9_test.go index 1eb4e15af..29499553e 100644 --- a/types/metadataV9_test.go +++ b/types/metadataV9_test.go @@ -20,6 +20,7 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) @@ -34,7 +35,7 @@ var exampleRuntimeMetadataV9 = MetadataV9{ } func TestMetadataV9_EncodeDecode(t *testing.T) { - assertRoundtrip(t, exampleMetadataV9) + AssertRoundtrip(t, exampleMetadataV9) } func TestFindEventNamesForEventIDV9(t *testing.T) { diff --git a/types/migration_compute_test.go b/types/migration_compute_test.go index 1643129d9..cc6b21675 100644 --- a/types/migration_compute_test.go +++ b/types/migration_compute_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -32,8 +33,8 @@ var ( IsAuto: true, } - migrationComputeFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(m *MigrationCompute, c fuzz.Continue) { + migrationComputeFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(m *MigrationCompute, c fuzz.Continue) { r := c.RandBool() m.IsSigned = r m.IsAuto = !r @@ -42,20 +43,20 @@ var ( ) func TestMigrationCompute_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MigrationCompute](t, 100, migrationComputeFuzzOpts...) - assertDecodeNilData[MigrationCompute](t) - assertEncodeEmptyObj[MigrationCompute](t, 0) + AssertRoundTripFuzz[MigrationCompute](t, 100, migrationComputeFuzzOpts...) + AssertDecodeNilData[MigrationCompute](t) + AssertEncodeEmptyObj[MigrationCompute](t, 0) } func TestMigrationCompute_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testMigrationCompute1, MustHexDecodeString("0x00")}, {testMigrationCompute2, MustHexDecodeString("0x01")}, }) } func TestMigrationCompute_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testMigrationCompute1}, {MustHexDecodeString("0x01"), testMigrationCompute2}, }) diff --git a/types/mmr.go b/types/mmr.go index 6559368af..8abe91837 100644 --- a/types/mmr.go +++ b/types/mmr.go @@ -2,6 +2,8 @@ package types import ( "encoding/json" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // GenerateMMRProofResponse contains the generate proof rpc response @@ -21,20 +23,20 @@ func (d *GenerateMMRProofResponse) UnmarshalJSON(bz []byte) error { if err := json.Unmarshal(bz, &tmp); err != nil { return err } - err := DecodeFromHex(tmp.BlockHash, &d.BlockHash) + err := codec.DecodeFromHex(tmp.BlockHash, &d.BlockHash) if err != nil { return err } var encodedLeaf MMREncodableOpaqueLeaf - err = DecodeFromHex(tmp.Leaf, &encodedLeaf) + err = codec.DecodeFromHex(tmp.Leaf, &encodedLeaf) if err != nil { return err } - err = Decode(encodedLeaf, &d.Leaf) + err = codec.Decode(encodedLeaf, &d.Leaf) if err != nil { return err } - err = DecodeFromHex(tmp.Proof, &d.Proof) + err = codec.DecodeFromHex(tmp.Proof, &d.Proof) if err != nil { return err } diff --git a/types/mmr_test.go b/types/mmr_test.go index cceabd5b6..4ba78fdbf 100644 --- a/types/mmr_test.go +++ b/types/mmr_test.go @@ -21,6 +21,7 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestGenerateMMRProofResponse_Unmarshal(t *testing.T) { @@ -31,10 +32,10 @@ func TestGenerateMMRProofResponse_Unmarshal(t *testing.T) { panic(err) } - expected := GenerateMMRProofResponse{BlockHash:H256{0x52, 0xd9, 0x17, 0xb5, 0x37, 0x96, 0xb6, 0x71, 0xeb, 0x6f, 0x9f, 0x54, 0x97, 0x87, 0x8c, 0xd7, 0xba, 0xcd, 0x1e, 0x8b, 0xaf, 0xd4, 0x10, 0x4, 0x68, 0x7f, 0x67, 0xd2, 0x93, 0x8b, 0x7b, 0x13}, Leaf:MMRLeaf{Version:0x0, ParentNumberAndHash:ParentNumberAndHash{ParentNumber:0x7d0, Hash:Hash{0x24, 0x72, 0x4d, 0x31, 0x86, 0x26, 0x5a, 0x4c, 0x1d, 0xa0, 0x58, 0xd5, 0x32, 0xf7, 0xe8, 0x6, 0xcb, 0x7b, 0x6d, 0xc, 0x5b, 0xe6, 0xee, 0xcf, 0x75, 0x3f, 0x35, 0xad, 0xdc, 0x53, 0xb5, 0x46}}, BeefyNextAuthoritySet:BeefyNextAuthoritySet{ID:0x1, Len:0x3, Root:H256{0x42, 0xb6, 0x39, 0x41, 0xec, 0x63, 0x6f, 0x52, 0x30, 0x3b, 0x3c, 0x33, 0xf5, 0x33, 0x49, 0x83, 0xd, 0x8a, 0x46, 0x6e, 0x94, 0x56, 0xd2, 0x5d, 0x22, 0xb2, 0x8f, 0x4b, 0xb0, 0xad, 0x3, 0x65}}, ParachainHeads:H256{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, Proof:MMRProof{LeafIndex:0x7d0, LeafCount:0x8d9, Items:[]H256{H256{0xef, 0x3c, 0x78, 0xf3, 0xfe, 0xbf, 0xaf, 0xa3, 0x19, 0xc7, 0x7a, 0xad, 0xb0, 0x80, 0x10, 0x99, 0xda, 0x3, 0xa7, 0x75, 0x89, 0x8f, 0x48, 0x63, 0x92, 0x10, 0x6a, 0x15, 0x68, 0x8c, 0x3, 0x54}, H256{0xc7, 0x1a, 0xed, 0x1e, 0xdc, 0x46, 0x76, 0x73, 0x57, 0xf1, 0xf6, 0xdd, 0x87, 0x1, 0xf7, 0x97, 0x4d, 0xd2, 0xae, 0x39, 0xe6, 0xf1, 0x78, 0xf9, 0xde, 0x1f, 0x6b, 0x84, 0x2, 0x68, 0x3e, 0xf7}, H256{0x8f, 0x20, 0x48, 0xbf, 0x97, 0x3a, 0x2f, 0x8b, 0x85, 0x2a, 0x4b, 0x49, 0x97, 0xd0, 0xb8, 0x63, 0x71, 0x11, 0x64, 0x1d, 0x88, 0xa5, 0xae, 0x86, 0x70, 0x26, 0x4, 0x17, 0xa1, 0xf4, 0xcc, 0xfc}, H256{0x2a, 0x5a, 0x56, 0xbc, 0x75, 0x17, 0xb0, 0x4d, 0x99, 0x31, 0xc8, 0x2a, 0x24, 0x66, 0xc, 0x8f, 0xa8, 0x6f, 0xd3, 0x36, 0x11, 0xb6, 0x96, 0x4c, 0x32, 0x1, 0x66, 0x95, 0x37, 0xd7, 0x85, 0x1a}, H256{0x17, 0xf8, 0x14, 0xe3, 0x7d, 0x71, 0x84, 0x1d, 0x69, 0x41, 0x28, 0x16, 0x69, 0x2a, 0x8c, 0x3b, 0x66, 0x15, 0x57, 0x2b, 0x79, 0x96, 0x74, 0xc9, 0xb6, 0x87, 0x9e, 0x54, 0x7b, 0x1f, 0x65, 0x11}, H256{0xaf, 0x7b, 0xe8, 0x7e, 0xa1, 0x22, 0xcb, 0x70, 0xf6, 0xda, 0x87, 0x18, 0xcb, 0x1, 0x7a, 0xcf, 0x56, 0xc0, 0xdc, 0x8f, 0xb0, 0xf0, 0x93, 0xed, 0x30, 0xd, 0xbb, 0xef, 0x98, 0xd6, 0x32, 0x32}, H256{0x9b, 0x93, 0xaa, 0x8, 0xf3, 0x9a, 0x2f, 0xd0, 0xcf, 0x2e, 0x51, 0xc1, 0x2f, 0x8b, 0x3, 0x70, 0xb9, 0xf3, 0xe5, 0xd, 0x33, 0x95, 0xa2, 0x0, 0x17, 0xe3, 0xf2, 0x49, 0x91, 0xc9, 0xb6, 0x3}, H256{0x95, 0x1e, 0xd7, 0x6d, 0xf7, 0xfa, 0xd6, 0xc1, 0x9c, 0xad, 0xe3, 0x3, 0x79, 0x53, 0x45, 0xa7, 0xa9, 0x67, 0x29, 0xfa, 0x33, 0x7a, 0xe8, 0x2d, 0x74, 0xb0, 0x3f, 0x2b, 0x3, 0x9e, 0xe5, 0xe3}, H256{0xb0, 0xc3, 0x4f, 0x63, 0xd8, 0xf8, 0xe4, 0x3a, 0xd, 0xb7, 0x54, 0xf4, 0xc0, 0xf4, 0x2d, 0xc8, 0x4d, 0x98, 0xa2, 0x6c, 0x51, 0xed, 0xea, 0x93, 0x19, 0x71, 0x20, 0x63, 0x98, 0xa8, 0xf, 0x5e}, H256{0xbe, 0xe8, 0xc4, 0xab, 0x19, 0xa7, 0x35, 0x8a, 0xdc, 0x89, 0xd2, 0xc4, 0x25, 0x8f, 0xb6, 0x1d, 0x97, 0x95, 0x17, 0xd3, 0x63, 0xee, 0x33, 0x8, 0x6c, 0xc5, 0xa9, 0xe6, 0x3c, 0x7e, 0xed, 0x27}, H256{0x67, 0x4f, 0x1c, 0xa1, 0xd3, 0x58, 0x5e, 0xb7, 0x7c, 0x1c, 0x28, 0x1f, 0xf2, 0x10, 0x57, 0xdf, 0xdf, 0x89, 0x22, 0x7d, 0xbe, 0x9f, 0x6a, 0x52, 0x27, 0x53, 0xd2, 0x6c, 0xfc, 0xf2, 0xf2, 0x71}, H256{0xe2, 0x7f, 0x93, 0x42, 0x2d, 0x11, 0x2e, 0xf7, 0x63, 0xd5, 0x2, 0x97, 0xd, 0xa6, 0x40, 0xb4, 0x16, 0xc1, 0xd3, 0xa0, 0x4c, 0x93, 0x73, 0xb1, 0xb, 0xee, 0xf6, 0x76, 0x6, 0x8c, 0xee, 0xec}}}} + expected := GenerateMMRProofResponse{BlockHash: H256{0x52, 0xd9, 0x17, 0xb5, 0x37, 0x96, 0xb6, 0x71, 0xeb, 0x6f, 0x9f, 0x54, 0x97, 0x87, 0x8c, 0xd7, 0xba, 0xcd, 0x1e, 0x8b, 0xaf, 0xd4, 0x10, 0x4, 0x68, 0x7f, 0x67, 0xd2, 0x93, 0x8b, 0x7b, 0x13}, Leaf: MMRLeaf{Version: 0x0, ParentNumberAndHash: ParentNumberAndHash{ParentNumber: 0x7d0, Hash: Hash{0x24, 0x72, 0x4d, 0x31, 0x86, 0x26, 0x5a, 0x4c, 0x1d, 0xa0, 0x58, 0xd5, 0x32, 0xf7, 0xe8, 0x6, 0xcb, 0x7b, 0x6d, 0xc, 0x5b, 0xe6, 0xee, 0xcf, 0x75, 0x3f, 0x35, 0xad, 0xdc, 0x53, 0xb5, 0x46}}, BeefyNextAuthoritySet: BeefyNextAuthoritySet{ID: 0x1, Len: 0x3, Root: H256{0x42, 0xb6, 0x39, 0x41, 0xec, 0x63, 0x6f, 0x52, 0x30, 0x3b, 0x3c, 0x33, 0xf5, 0x33, 0x49, 0x83, 0xd, 0x8a, 0x46, 0x6e, 0x94, 0x56, 0xd2, 0x5d, 0x22, 0xb2, 0x8f, 0x4b, 0xb0, 0xad, 0x3, 0x65}}, ParachainHeads: H256{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}, Proof: MMRProof{LeafIndex: 0x7d0, LeafCount: 0x8d9, Items: []H256{H256{0xef, 0x3c, 0x78, 0xf3, 0xfe, 0xbf, 0xaf, 0xa3, 0x19, 0xc7, 0x7a, 0xad, 0xb0, 0x80, 0x10, 0x99, 0xda, 0x3, 0xa7, 0x75, 0x89, 0x8f, 0x48, 0x63, 0x92, 0x10, 0x6a, 0x15, 0x68, 0x8c, 0x3, 0x54}, H256{0xc7, 0x1a, 0xed, 0x1e, 0xdc, 0x46, 0x76, 0x73, 0x57, 0xf1, 0xf6, 0xdd, 0x87, 0x1, 0xf7, 0x97, 0x4d, 0xd2, 0xae, 0x39, 0xe6, 0xf1, 0x78, 0xf9, 0xde, 0x1f, 0x6b, 0x84, 0x2, 0x68, 0x3e, 0xf7}, H256{0x8f, 0x20, 0x48, 0xbf, 0x97, 0x3a, 0x2f, 0x8b, 0x85, 0x2a, 0x4b, 0x49, 0x97, 0xd0, 0xb8, 0x63, 0x71, 0x11, 0x64, 0x1d, 0x88, 0xa5, 0xae, 0x86, 0x70, 0x26, 0x4, 0x17, 0xa1, 0xf4, 0xcc, 0xfc}, H256{0x2a, 0x5a, 0x56, 0xbc, 0x75, 0x17, 0xb0, 0x4d, 0x99, 0x31, 0xc8, 0x2a, 0x24, 0x66, 0xc, 0x8f, 0xa8, 0x6f, 0xd3, 0x36, 0x11, 0xb6, 0x96, 0x4c, 0x32, 0x1, 0x66, 0x95, 0x37, 0xd7, 0x85, 0x1a}, H256{0x17, 0xf8, 0x14, 0xe3, 0x7d, 0x71, 0x84, 0x1d, 0x69, 0x41, 0x28, 0x16, 0x69, 0x2a, 0x8c, 0x3b, 0x66, 0x15, 0x57, 0x2b, 0x79, 0x96, 0x74, 0xc9, 0xb6, 0x87, 0x9e, 0x54, 0x7b, 0x1f, 0x65, 0x11}, H256{0xaf, 0x7b, 0xe8, 0x7e, 0xa1, 0x22, 0xcb, 0x70, 0xf6, 0xda, 0x87, 0x18, 0xcb, 0x1, 0x7a, 0xcf, 0x56, 0xc0, 0xdc, 0x8f, 0xb0, 0xf0, 0x93, 0xed, 0x30, 0xd, 0xbb, 0xef, 0x98, 0xd6, 0x32, 0x32}, H256{0x9b, 0x93, 0xaa, 0x8, 0xf3, 0x9a, 0x2f, 0xd0, 0xcf, 0x2e, 0x51, 0xc1, 0x2f, 0x8b, 0x3, 0x70, 0xb9, 0xf3, 0xe5, 0xd, 0x33, 0x95, 0xa2, 0x0, 0x17, 0xe3, 0xf2, 0x49, 0x91, 0xc9, 0xb6, 0x3}, H256{0x95, 0x1e, 0xd7, 0x6d, 0xf7, 0xfa, 0xd6, 0xc1, 0x9c, 0xad, 0xe3, 0x3, 0x79, 0x53, 0x45, 0xa7, 0xa9, 0x67, 0x29, 0xfa, 0x33, 0x7a, 0xe8, 0x2d, 0x74, 0xb0, 0x3f, 0x2b, 0x3, 0x9e, 0xe5, 0xe3}, H256{0xb0, 0xc3, 0x4f, 0x63, 0xd8, 0xf8, 0xe4, 0x3a, 0xd, 0xb7, 0x54, 0xf4, 0xc0, 0xf4, 0x2d, 0xc8, 0x4d, 0x98, 0xa2, 0x6c, 0x51, 0xed, 0xea, 0x93, 0x19, 0x71, 0x20, 0x63, 0x98, 0xa8, 0xf, 0x5e}, H256{0xbe, 0xe8, 0xc4, 0xab, 0x19, 0xa7, 0x35, 0x8a, 0xdc, 0x89, 0xd2, 0xc4, 0x25, 0x8f, 0xb6, 0x1d, 0x97, 0x95, 0x17, 0xd3, 0x63, 0xee, 0x33, 0x8, 0x6c, 0xc5, 0xa9, 0xe6, 0x3c, 0x7e, 0xed, 0x27}, H256{0x67, 0x4f, 0x1c, 0xa1, 0xd3, 0x58, 0x5e, 0xb7, 0x7c, 0x1c, 0x28, 0x1f, 0xf2, 0x10, 0x57, 0xdf, 0xdf, 0x89, 0x22, 0x7d, 0xbe, 0x9f, 0x6a, 0x52, 0x27, 0x53, 0xd2, 0x6c, 0xfc, 0xf2, 0xf2, 0x71}, H256{0xe2, 0x7f, 0x93, 0x42, 0x2d, 0x11, 0x2e, 0xf7, 0x63, 0xd5, 0x2, 0x97, 0xd, 0xa6, 0x40, 0xb4, 0x16, 0xc1, 0xd3, 0xa0, 0x4c, 0x93, 0x73, 0xb1, 0xb, 0xee, 0xf6, 0x76, 0x6, 0x8c, 0xee, 0xec}}}} var unmarshalled GenerateMMRProofResponse json.Unmarshal(marshalled, &unmarshalled) - assertEqual(t, unmarshalled, expected) + AssertEqual(t, unmarshalled, expected) } diff --git a/types/moment_test.go b/types/moment_test.go index 7d2d65a51..500fc7eeb 100644 --- a/types/moment_test.go +++ b/types/moment_test.go @@ -22,16 +22,16 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" - "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) func TestMoment_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewMoment(time.Unix(1575470205, 874000000000))) - assertRoundtrip(t, NewMoment(time.Unix(0, 0))) + AssertRoundtrip(t, NewMoment(time.Unix(1575470205, 874000000000))) + AssertRoundtrip(t, NewMoment(time.Unix(0, 0))) m := new(Moment) @@ -47,36 +47,36 @@ func TestMoment_EncodeDecode(t *testing.T) { } func TestMoment_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewMoment(time.Unix(12345, 0)), 8}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewMoment(time.Unix(12345, 0)), 8}}) } func TestMoment_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewMoment(time.Unix(12345, 0)), MustHexDecodeString("0xa85ebc0000000000")}, }) } func TestMoment_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewMoment(time.Unix(12345, 0)), MustHexDecodeString( "0x388c23ace057ec800ef437dcc68bfbcd2b1a22fe79aceb623d7d21f7bda56848")}, }) } func TestMoment_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewMoment(time.Unix(12345, 0)), "0xa85ebc0000000000"}, }) } func TestMoment_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewMoment(time.Unix(12345, 0).UTC()), "1970-01-01 03:25:45 +0000 UTC"}, }) } func TestMoment_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewMoment(time.Unix(12345, 0)), NewMoment(time.Unix(12345, 0)), true}, {NewMoment(time.Unix(12345, 0)), NewBool(false), false}, }) diff --git a/types/multi_address.go b/types/multi_address.go index 63e415483..f4da28e83 100644 --- a/types/multi_address.go +++ b/types/multi_address.go @@ -18,6 +18,7 @@ package types import ( "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) type MultiAddress struct { @@ -34,20 +35,25 @@ type MultiAddress struct { } // NewMultiAddressFromAccountID creates an Address from the given AccountID (public key) -func NewMultiAddressFromAccountID(b []byte) MultiAddress { +func NewMultiAddressFromAccountID(b []byte) (MultiAddress, error) { + accountID, err := NewAccountID(b) + if err != nil { + return MultiAddress{}, err + } + return MultiAddress{ IsID: true, - AsID: NewAccountID(b), - } + AsID: *accountID, + }, nil } // NewMultiAddressFromHexAccountID creates an Address from the given hex string that contains an AccountID (public key) func NewMultiAddressFromHexAccountID(str string) (MultiAddress, error) { - b, err := HexDecodeString(str) + b, err := codec.HexDecodeString(str) if err != nil { return MultiAddress{}, err } - return NewMultiAddressFromAccountID(b), nil + return NewMultiAddressFromAccountID(b) } func (m MultiAddress) Encode(encoder scale.Encoder) error { diff --git a/types/multi_address_test.go b/types/multi_address_test.go index 2ebd0ff01..ee3276bad 100644 --- a/types/multi_address_test.go +++ b/types/multi_address_test.go @@ -17,18 +17,20 @@ package types_test import ( + "fmt" "testing" - fuzz "github.com/google/gofuzz" - "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/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" ) var ( - multiAddressFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(m *MultiAddress, c fuzz.Continue) { + multiAddressFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(m *MultiAddress, c fuzz.Continue) { switch c.Intn(5) { case 0: m.IsID = true @@ -50,33 +52,43 @@ var ( } ) +func newTestMultiAddress() MultiAddress { + ma, err := NewMultiAddressFromAccountID(testAccountIDBytes) + + if err != nil { + panic(fmt.Errorf("couldn't create test multi address: %w", err)) + } + + return ma +} + func TestMultiAddress_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiAddress](t, 100, multiAddressFuzzOpts...) - assertDecodeNilData[MultiAddress](t) + AssertRoundTripFuzz[MultiAddress](t, 100, multiAddressFuzzOpts...) + AssertDecodeNilData[MultiAddress](t) } func TestNewMultiAddressFromAccountID(t *testing.T) { - assertRoundtrip(t, NewMultiAddressFromAccountID(signature.TestKeyringPairAlice.PublicKey)) + AssertRoundtrip(t, newTestMultiAddress()) _, err := NewMultiAddressFromHexAccountID("123!") assert.Error(t, err) addr, err := NewMultiAddressFromHexAccountID(HexEncodeToString(signature.TestKeyringPairAlice.PublicKey)) assert.NoError(t, err) - assertRoundtrip(t, addr) - assertRoundtrip(t, MultiAddress{ + AssertRoundtrip(t, addr) + AssertRoundtrip(t, MultiAddress{ IsIndex: true, AsIndex: 100, }) - assertRoundtrip(t, MultiAddress{ + AssertRoundtrip(t, MultiAddress{ IsRaw: true, AsRaw: []byte{1, 2, 3}, }) - assertRoundtrip(t, MultiAddress{ + AssertRoundtrip(t, MultiAddress{ IsAddress32: true, AsAddress32: [32]byte{}, }) - assertRoundtrip(t, MultiAddress{ + AssertRoundtrip(t, MultiAddress{ IsAddress20: true, AsAddress20: [20]byte{}, }) diff --git a/types/multi_location_v0_test.go b/types/multi_location_v0_test.go index 7636bc88f..dce6e348e 100644 --- a/types/multi_location_v0_test.go +++ b/types/multi_location_v0_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -102,10 +103,10 @@ var ( testJunction8, }, } - multiLocationV0FuzzOpts = combineFuzzOpts( + multiLocationV0FuzzOpts = CombineFuzzOpts( junctionV0FuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(m *MultiLocationV0, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(m *MultiLocationV0, c fuzz.Continue) { switch c.Intn(9) { case 0: m.IsNull = true @@ -148,13 +149,13 @@ var ( ) func TestMultiLocationV0_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiLocationV0](t, 1000, multiLocationV0FuzzOpts...) - assertDecodeNilData[MultiLocationV0](t) - assertEncodeEmptyObj[MultiLocationV0](t, 0) + AssertRoundTripFuzz[MultiLocationV0](t, 1000, multiLocationV0FuzzOpts...) + AssertDecodeNilData[MultiLocationV0](t) + AssertEncodeEmptyObj[MultiLocationV0](t, 0) } func TestMultiLocationV0_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testMultiLocationV0n1, MustHexDecodeString("0x00"), @@ -195,7 +196,7 @@ func TestMultiLocationV0_Encode(t *testing.T) { } func TestMultiLocationV0_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x00"), testMultiLocationV0n1, diff --git a/types/multi_location_v1_test.go b/types/multi_location_v1_test.go index c623a8cf4..1a3de2d7c 100644 --- a/types/multi_location_v1_test.go +++ b/types/multi_location_v1_test.go @@ -19,11 +19,11 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" ) var ( @@ -34,10 +34,10 @@ var ( }, } - optionMultiLocationV1FuzzOpts = combineFuzzOpts( + optionMultiLocationV1FuzzOpts = CombineFuzzOpts( junctionsV1FuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(o *OptionMultiLocationV1, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(o *OptionMultiLocationV1, c fuzz.Continue) { if c.RandBool() { *o = NewOptionMultiLocationV1Empty() return @@ -54,19 +54,19 @@ var ( ) func TestOptionMultiLocation_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionMultiLocationV1](t, 1000, optionMultiLocationV1FuzzOpts...) - assertEncodeEmptyObj[OptionMultiLocationV1](t, 1) + AssertRoundTripFuzz[OptionMultiLocationV1](t, 1000, optionMultiLocationV1FuzzOpts...) + AssertEncodeEmptyObj[OptionMultiLocationV1](t, 1) } func TestOptionMultiLocation_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionMultiLocationV1(testMultiLocation), MustHexDecodeString("0x010100")}, {NewOptionMultiLocationV1Empty(), MustHexDecodeString("0x00")}, }) } func TestOptionMultiLocation_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x010100"), NewOptionMultiLocationV1(testMultiLocation)}, {MustHexDecodeString("0x00"), NewOptionMultiLocationV1Empty()}, }) @@ -99,19 +99,19 @@ var ( ) func TestMultiLocationV1_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiLocationV1](t, 100, multiLocationV1FuzzOpts...) - assertDecodeNilData[MultiLocationV1](t) - assertEncodeEmptyObj[MultiLocationV1](t, 1) + AssertRoundTripFuzz[MultiLocationV1](t, 100, multiLocationV1FuzzOpts...) + AssertDecodeNilData[MultiLocationV1](t) + AssertEncodeEmptyObj[MultiLocationV1](t, 1) } func TestMultiLocationV1_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testMultiLocationV1n1, MustHexDecodeString("0x0408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807")}, }) } func TestMultiLocationV1_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807"), testMultiLocationV1n1}, }) } @@ -126,11 +126,11 @@ var ( MultiLocationV1: testMultiLocationV1n1, } - versionedMultiLocationFuzzOpts = combineFuzzOpts( + versionedMultiLocationFuzzOpts = CombineFuzzOpts( multiLocationV0FuzzOpts, multiLocationV1FuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(v *VersionedMultiLocation, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(v *VersionedMultiLocation, c fuzz.Continue) { if c.RandBool() { v.IsV0 = true c.Fuzz(&v.MultiLocationV0) @@ -146,20 +146,20 @@ var ( ) func TestVersionedMultiLocation_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[VersionedMultiLocation](t, 1000, versionedMultiLocationFuzzOpts...) - assertDecodeNilData[VersionedMultiLocation](t) - assertEncodeEmptyObj[VersionedMultiLocation](t, 0) + AssertRoundTripFuzz[VersionedMultiLocation](t, 1000, versionedMultiLocationFuzzOpts...) + AssertDecodeNilData[VersionedMultiLocation](t) + AssertEncodeEmptyObj[VersionedMultiLocation](t, 0) } func TestVersionedMultiLocation_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testVersionMultiLocation1, MustHexDecodeString("0x000800010b00000002000c010203030010000000000000000403000504062a00000000000000000000000000000007080608")}, {testVersionMultiLocation2, MustHexDecodeString("0x010408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807")}, }) } func TestVersionedMultiLocation_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x000800010b00000002000c010203030010000000000000000403000504062a00000000000000000000000000000007080608"), testVersionMultiLocation1}, {MustHexDecodeString("0x010408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807"), testVersionMultiLocation2}, }) diff --git a/types/multi_signature_test.go b/types/multi_signature_test.go index ba99e750e..84c8f070b 100644 --- a/types/multi_signature_test.go +++ b/types/multi_signature_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var testMultiSig1 = MultiSignature{IsEd25519: true, AsEd25519: NewSignature(hash64)} @@ -29,8 +30,8 @@ var testMultiSig2 = MultiSignature{IsSr25519: true, AsSr25519: NewSignature(hash var testMultiSig3 = MultiSignature{IsEcdsa: true, AsEcdsa: NewEcdsaSignature(hash65)} var ( - multiSignatureFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(m *MultiSignature, c fuzz.Continue) { + multiSignatureFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(m *MultiSignature, c fuzz.Continue) { switch c.Intn(3) { case 0: m.IsEd25519 = true @@ -47,16 +48,16 @@ var ( ) func TestMultiSignature_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testMultiSig1) - assertRoundtrip(t, testMultiSig2) - assertRoundtrip(t, testMultiSig3) - assertRoundTripFuzz[MultiSignature](t, 100, multiSignatureFuzzOpts...) - assertDecodeNilData[MultiSignature](t) - assertEncodeEmptyObj[MultiSignature](t, 0) + AssertRoundtrip(t, testMultiSig1) + AssertRoundtrip(t, testMultiSig2) + AssertRoundtrip(t, testMultiSig3) + AssertRoundTripFuzz[MultiSignature](t, 100, multiSignatureFuzzOpts...) + AssertDecodeNilData[MultiSignature](t) + AssertEncodeEmptyObj[MultiSignature](t, 0) } func TestMultiSignature_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testMultiSig1, MustHexDecodeString("0x0001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304")}, //nolint:lll {testMultiSig2, MustHexDecodeString("0x0101020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304")}, //nolint:lll {testMultiSig3, MustHexDecodeString("0x020102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405")}, //nolint:lll @@ -64,7 +65,7 @@ func TestMultiSignature_Encode(t *testing.T) { } func TestMultiSignature_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304"), testMultiSig1}, //nolint:lll {MustHexDecodeString("0x0101020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304"), testMultiSig2}, //nolint:lll {MustHexDecodeString("0x020102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405"), testMultiSig3}, //nolint:lll diff --git a/types/network_id_test.go b/types/network_id_test.go index 4d776567c..cd1bd7658 100644 --- a/types/network_id_test.go +++ b/types/network_id_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -39,8 +40,8 @@ var ( IsKusama: true, } - networkIDFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(n *NetworkID, c fuzz.Continue) { + networkIDFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(n *NetworkID, c fuzz.Continue) { switch c.Intn(4) { case 0: n.IsAny = true @@ -58,13 +59,13 @@ var ( ) func TestNetworkID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[NetworkID](t, 100, networkIDFuzzOpts...) - assertDecodeNilData[NetworkID](t) - assertEncodeEmptyObj[NetworkID](t, 0) + AssertRoundTripFuzz[NetworkID](t, 100, networkIDFuzzOpts...) + AssertDecodeNilData[NetworkID](t) + AssertEncodeEmptyObj[NetworkID](t, 0) } func TestNetworkID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testNetworkID1, MustHexDecodeString("0x00")}, {testNetworkID2, MustHexDecodeString("0x011001020304")}, {testNetworkID3, MustHexDecodeString("0x02")}, @@ -73,7 +74,7 @@ func TestNetworkID_Encode(t *testing.T) { } func TestNetworkID_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testNetworkID1}, {MustHexDecodeString("0x011001020304"), testNetworkID2}, {MustHexDecodeString("0x02"), testNetworkID3}, diff --git a/types/network_state_test.go b/types/network_state_test.go index f0b1b154b..06437a0fe 100644 --- a/types/network_state_test.go +++ b/types/network_state_test.go @@ -20,22 +20,24 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestNetworkState_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NetworkState{NewText("ab123")}) - assertRoundTripFuzz[NetworkState](t, 100) - assertEncodeEmptyObj[NetworkState](t, 1) + AssertRoundtrip(t, NetworkState{NewText("ab123")}) + AssertRoundTripFuzz[NetworkState](t, 100) + AssertEncodeEmptyObj[NetworkState](t, 1) } func TestNetworkState_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NetworkState{NewText("ab123")}, MustHexDecodeString("0x146162313233")}, }) } func TestNetworkState_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x146162313233"), NetworkState{NewText("ab123")}}, }) } diff --git a/types/null_test.go b/types/null_test.go index 687e7378f..f81be4252 100644 --- a/types/null_test.go +++ b/types/null_test.go @@ -20,45 +20,47 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestNull_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewNull()) + AssertRoundtrip(t, NewNull()) } func TestNull_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewNull(), 0}, }) } func TestNull_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewNull(), MustHexDecodeString("0x")}, }) } func TestNull_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewNull(), MustHexDecodeString( "0x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8")}, }) } func TestNull_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewNull(), ""}, }) } func TestNull_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewNull(), ""}, }) } func TestNull_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewNull(), NewNull(), true}, {NewNull(), NewBytes([]byte{}), false}, {NewNull(), NewBool(true), false}, diff --git a/types/option.go b/types/option.go index 82e6b9375..cfbcdcd6b 100644 --- a/types/option.go +++ b/types/option.go @@ -25,7 +25,7 @@ func (o option) IsNone() bool { return !o.hasValue } -// IsNone returns true if a value is present +// IsSome returns true if a value is present func (o option) IsSome() bool { return o.hasValue } diff --git a/types/option_bool_test.go b/types/option_bool_test.go index 118614e1a..9332330b6 100644 --- a/types/option_bool_test.go +++ b/types/option_bool_test.go @@ -19,19 +19,20 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) func TestOptionBool_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBool(NewBool(true))) - assertRoundtrip(t, NewOptionBool(NewBool(false))) - assertRoundtrip(t, NewOptionBoolEmpty()) + AssertRoundtrip(t, NewOptionBool(NewBool(true))) + AssertRoundtrip(t, NewOptionBool(NewBool(false))) + AssertRoundtrip(t, NewOptionBoolEmpty()) } func TestOptionBool_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewOptionBool(NewBool(false)), 1}, {NewOptionBool(NewBool(true)), 1}, {NewOptionBoolEmpty(), 1}, @@ -39,7 +40,7 @@ func TestOptionBool_EncodedLength(t *testing.T) { } func TestOptionBool_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionBool(NewBool(false)), MustHexDecodeString("0x02")}, {NewOptionBool(NewBool(true)), MustHexDecodeString("0x01")}, {NewOptionBoolEmpty(), MustHexDecodeString("0x00")}, @@ -47,7 +48,7 @@ func TestOptionBool_Encode(t *testing.T) { } func TestOptionBool_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewOptionBool(NewBool(true)), MustHexDecodeString( "0xee155ace9c40292074cb6aff8c9ccdd273c81648ff1149ef36bcea6ebb8a3e25")}, {NewOptionBool(NewBool(false)), MustHexDecodeString( @@ -58,7 +59,7 @@ func TestOptionBool_Hash(t *testing.T) { } func TestOptionBool_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewOptionBool(NewBool(true)), NewBool(true), false}, {NewOptionBool(NewBool(false)), NewOptionBool(NewBool(false)), true}, {NewOptionBoolEmpty(), NewOptionBoolEmpty(), true}, diff --git a/types/option_bytes_test.go b/types/option_bytes_test.go index 8628916ea..08ab0e6cc 100644 --- a/types/option_bytes_test.go +++ b/types/option_bytes_test.go @@ -19,19 +19,20 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) func TestOptionBytes8_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes8(NewBytes8([8]byte{12}))) - assertRoundtrip(t, NewOptionBytes8(NewBytes8([8]byte{}))) - assertRoundtrip(t, NewOptionBytes8Empty()) + AssertRoundtrip(t, NewOptionBytes8(NewBytes8([8]byte{12}))) + AssertRoundtrip(t, NewOptionBytes8(NewBytes8([8]byte{}))) + AssertRoundtrip(t, NewOptionBytes8Empty()) } func TestOptionBytes8_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewOptionBytes8(NewBytes8([8]byte{})), 9}, {NewOptionBytes8(NewBytes8([8]byte{7, 6, 5, 4, 3, 2, 1, 0})), 9}, {NewOptionBytes8Empty(), 1}, @@ -39,7 +40,7 @@ func TestOptionBytes8_EncodedLength(t *testing.T) { } func TestOptionBytes8_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionBytes8(NewBytes8([8]byte{0, 0, 0})), MustHexDecodeString("0x010000000000000000")}, {NewOptionBytes8(NewBytes8([8]byte{171, 18, 52})), MustHexDecodeString("0x01ab12340000000000")}, {NewOptionBytes8Empty(), MustHexDecodeString("0x00")}, @@ -47,7 +48,7 @@ func TestOptionBytes8_Encode(t *testing.T) { } func TestOptionBytes8_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewOptionBytes8(NewBytes8([8]byte{0, 42, 254})), MustHexDecodeString( "0x80c0970f2247ec2333c8f805187dcb036be18aa08ab8a738debaefa8d8f78a52")}, {NewOptionBytes8(NewBytes8([8]byte{0, 0})), MustHexDecodeString( @@ -58,7 +59,7 @@ func TestOptionBytes8_Hash(t *testing.T) { } func TestOptionBytes8_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewOptionBytes8(NewBytes8([8]byte{1, 0, 0})), NewBytes8([8]byte{1, 0}), false}, {NewOptionBytes8(NewBytes8([8]byte{0, 0, 1})), NewOptionBytes8(NewBytes8([8]byte{0, 0, 1})), true}, {NewOptionBytes8Empty(), NewOptionBytes8Empty(), true}, @@ -87,13 +88,13 @@ func TestOptionBytes8(t *testing.T) { } func TestOptionBytes_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes(NewBytes([]byte{12}))) - assertRoundtrip(t, NewOptionBytes(NewBytes([]byte{2}))) - assertRoundtrip(t, NewOptionBytesEmpty()) + AssertRoundtrip(t, NewOptionBytes(NewBytes([]byte{12}))) + AssertRoundtrip(t, NewOptionBytes(NewBytes([]byte{2}))) + AssertRoundtrip(t, NewOptionBytesEmpty()) } func TestOptionBytes_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionBytes(NewBytes([]byte{0, 0, 0})), MustHexDecodeString("0x010c000000")}, {NewOptionBytes(NewBytes([]byte{171, 1, 52})), MustHexDecodeString("0x010cab0134")}, {NewOptionBytesEmpty(), MustHexDecodeString("0x00")}, @@ -101,7 +102,7 @@ func TestOptionBytes_Encode(t *testing.T) { } func TestOptionBytes_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x010c000000"), NewOptionBytes(NewBytes([]byte{0, 0, 0}))}, {MustHexDecodeString("0x010cab0134"), NewOptionBytes(NewBytes([]byte{171, 1, 52}))}, {MustHexDecodeString("0x00"), NewOptionBytesEmpty()}, @@ -259,49 +260,49 @@ func TestOptionBytes2048_OptionMethods(t *testing.T) { } func TestOptionBytes16_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes16(NewBytes16([16]byte{12}))) - assertRoundtrip(t, NewOptionBytes16(NewBytes16([16]byte{}))) - assertRoundtrip(t, NewOptionBytes16Empty()) + AssertRoundtrip(t, NewOptionBytes16(NewBytes16([16]byte{12}))) + AssertRoundtrip(t, NewOptionBytes16(NewBytes16([16]byte{}))) + AssertRoundtrip(t, NewOptionBytes16Empty()) } func TestOptionBytes32_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes32(NewBytes32([32]byte{12}))) - assertRoundtrip(t, NewOptionBytes32(NewBytes32([32]byte{}))) - assertRoundtrip(t, NewOptionBytes32Empty()) + AssertRoundtrip(t, NewOptionBytes32(NewBytes32([32]byte{12}))) + AssertRoundtrip(t, NewOptionBytes32(NewBytes32([32]byte{}))) + AssertRoundtrip(t, NewOptionBytes32Empty()) } func TestOptionBytes64_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes64(NewBytes64([64]byte{12}))) - assertRoundtrip(t, NewOptionBytes64(NewBytes64([64]byte{}))) - assertRoundtrip(t, NewOptionBytes64Empty()) + AssertRoundtrip(t, NewOptionBytes64(NewBytes64([64]byte{12}))) + AssertRoundtrip(t, NewOptionBytes64(NewBytes64([64]byte{}))) + AssertRoundtrip(t, NewOptionBytes64Empty()) } func TestOptionBytes128_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes128(NewBytes128([128]byte{12}))) - assertRoundtrip(t, NewOptionBytes128(NewBytes128([128]byte{}))) - assertRoundtrip(t, NewOptionBytes128Empty()) + AssertRoundtrip(t, NewOptionBytes128(NewBytes128([128]byte{12}))) + AssertRoundtrip(t, NewOptionBytes128(NewBytes128([128]byte{}))) + AssertRoundtrip(t, NewOptionBytes128Empty()) } func TestOptionBytes256_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes256(NewBytes256([256]byte{12}))) - assertRoundtrip(t, NewOptionBytes256(NewBytes256([256]byte{}))) - assertRoundtrip(t, NewOptionBytes256Empty()) + AssertRoundtrip(t, NewOptionBytes256(NewBytes256([256]byte{12}))) + AssertRoundtrip(t, NewOptionBytes256(NewBytes256([256]byte{}))) + AssertRoundtrip(t, NewOptionBytes256Empty()) } func TestOptionBytes512_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes512(NewBytes512([512]byte{12}))) - assertRoundtrip(t, NewOptionBytes512(NewBytes512([512]byte{}))) - assertRoundtrip(t, NewOptionBytes512Empty()) + AssertRoundtrip(t, NewOptionBytes512(NewBytes512([512]byte{12}))) + AssertRoundtrip(t, NewOptionBytes512(NewBytes512([512]byte{}))) + AssertRoundtrip(t, NewOptionBytes512Empty()) } func TestOptionBytes1024_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes1024(NewBytes1024([1024]byte{12}))) - assertRoundtrip(t, NewOptionBytes1024(NewBytes1024([1024]byte{}))) - assertRoundtrip(t, NewOptionBytes1024Empty()) + AssertRoundtrip(t, NewOptionBytes1024(NewBytes1024([1024]byte{12}))) + AssertRoundtrip(t, NewOptionBytes1024(NewBytes1024([1024]byte{}))) + AssertRoundtrip(t, NewOptionBytes1024Empty()) } func TestOptionBytes2048_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionBytes2048(NewBytes2048([2048]byte{12}))) - assertRoundtrip(t, NewOptionBytes2048(NewBytes2048([2048]byte{}))) - assertRoundtrip(t, NewOptionBytes2048Empty()) + AssertRoundtrip(t, NewOptionBytes2048(NewBytes2048([2048]byte{12}))) + AssertRoundtrip(t, NewOptionBytes2048(NewBytes2048([2048]byte{}))) + AssertRoundtrip(t, NewOptionBytes2048Empty()) } diff --git a/types/option_hash_test.go b/types/option_hash_test.go index d20cc8c2a..92881f9fe 100644 --- a/types/option_hash_test.go +++ b/types/option_hash_test.go @@ -19,9 +19,9 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) func TestOptionHash_OptionMethods(t *testing.T) { @@ -85,21 +85,21 @@ func TestOptionH512_OptionMethods(t *testing.T) { } func TestOptionH160_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionH160(NewH160(hash20))) - assertRoundtrip(t, NewOptionH160Empty()) + AssertRoundtrip(t, NewOptionH160(NewH160(hash20))) + AssertRoundtrip(t, NewOptionH160Empty()) } func TestOptionH256_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionH256(NewH256(hash32))) - assertRoundtrip(t, NewOptionH256Empty()) + AssertRoundtrip(t, NewOptionH256(NewH256(hash32))) + AssertRoundtrip(t, NewOptionH256Empty()) } func TestOptionH512_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionH512(NewH512(hash64))) - assertRoundtrip(t, NewOptionH512Empty()) + AssertRoundtrip(t, NewOptionH512(NewH512(hash64))) + AssertRoundtrip(t, NewOptionH512Empty()) } func TestOptionHash_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionHash(NewHash(hash32))) - assertRoundtrip(t, NewOptionHashEmpty()) + AssertRoundtrip(t, NewOptionHash(NewHash(hash32))) + AssertRoundtrip(t, NewOptionHashEmpty()) } diff --git a/types/option_int_test.go b/types/option_int_test.go index 8f8e40891..f0bbe887c 100644 --- a/types/option_int_test.go +++ b/types/option_int_test.go @@ -19,9 +19,9 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/stretchr/testify/assert" ) func TestOptionI8_OptionMethods(t *testing.T) { @@ -85,25 +85,25 @@ func TestOptionI64_OptionMethods(t *testing.T) { } func TestOptionI8_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionI8(NewI8(7))) - assertRoundtrip(t, NewOptionI8(NewI8(0))) - assertRoundtrip(t, NewOptionI8Empty()) + AssertRoundtrip(t, NewOptionI8(NewI8(7))) + AssertRoundtrip(t, NewOptionI8(NewI8(0))) + AssertRoundtrip(t, NewOptionI8Empty()) } func TestOptionI16_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionI16(NewI16(14))) - assertRoundtrip(t, NewOptionI16(NewI16(0))) - assertRoundtrip(t, NewOptionI16Empty()) + AssertRoundtrip(t, NewOptionI16(NewI16(14))) + AssertRoundtrip(t, NewOptionI16(NewI16(0))) + AssertRoundtrip(t, NewOptionI16Empty()) } func TestOptionI32_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionI32(NewI32(21))) - assertRoundtrip(t, NewOptionI32(NewI32(0))) - assertRoundtrip(t, NewOptionI32Empty()) + AssertRoundtrip(t, NewOptionI32(NewI32(21))) + AssertRoundtrip(t, NewOptionI32(NewI32(0))) + AssertRoundtrip(t, NewOptionI32Empty()) } func TestOptionI64_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionI64(NewI64(28))) - assertRoundtrip(t, NewOptionI64(NewI64(0))) - assertRoundtrip(t, NewOptionI64Empty()) + AssertRoundtrip(t, NewOptionI64(NewI64(28))) + AssertRoundtrip(t, NewOptionI64(NewI64(0))) + AssertRoundtrip(t, NewOptionI64Empty()) } diff --git a/types/option_uint_test.go b/types/option_uint_test.go index 637d9b9a7..b058d99d0 100644 --- a/types/option_uint_test.go +++ b/types/option_uint_test.go @@ -20,16 +20,15 @@ import ( "math/big" "testing" - "github.com/stretchr/testify/assert" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" ) var ( - optionU128FuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(o *OptionU128, c fuzz.Continue) { + optionU128FuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(o *OptionU128, c fuzz.Continue) { if c.RandBool() { *o = NewOptionU128Empty() return @@ -45,8 +44,8 @@ var ( ) func TestOptionU8_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionU128](t, 100, optionU128FuzzOpts...) - assertEncodeEmptyObj[OptionU128](t, 1) + AssertRoundTripFuzz[OptionU128](t, 100, optionU128FuzzOpts...) + AssertEncodeEmptyObj[OptionU128](t, 1) } func TestOptionU8_OptionMethods(t *testing.T) { @@ -125,25 +124,25 @@ func TestOptionU128_OptionMethods(t *testing.T) { } func TestOptionU16_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionU16(NewU16(14))) - assertRoundtrip(t, NewOptionU16(NewU16(0))) - assertRoundtrip(t, NewOptionU16Empty()) + AssertRoundtrip(t, NewOptionU16(NewU16(14))) + AssertRoundtrip(t, NewOptionU16(NewU16(0))) + AssertRoundtrip(t, NewOptionU16Empty()) } func TestOptionU32_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionU32(NewU32(21))) - assertRoundtrip(t, NewOptionU32(NewU32(0))) - assertRoundtrip(t, NewOptionU32Empty()) + AssertRoundtrip(t, NewOptionU32(NewU32(21))) + AssertRoundtrip(t, NewOptionU32(NewU32(0))) + AssertRoundtrip(t, NewOptionU32Empty()) } func TestOptionU64_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionU64(NewU64(28))) - assertRoundtrip(t, NewOptionU64(NewU64(0))) - assertRoundtrip(t, NewOptionU64Empty()) + AssertRoundtrip(t, NewOptionU64(NewU64(28))) + AssertRoundtrip(t, NewOptionU64(NewU64(0))) + AssertRoundtrip(t, NewOptionU64Empty()) } func TestOptionU128_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewOptionU128(NewU128(*big.NewInt(123)))) - assertRoundtrip(t, NewOptionU128(NewU128(*big.NewInt(0)))) - assertRoundtrip(t, NewOptionU128Empty()) + AssertRoundtrip(t, NewOptionU128(NewU128(*big.NewInt(123)))) + AssertRoundtrip(t, NewOptionU128(NewU128(*big.NewInt(0)))) + AssertRoundtrip(t, NewOptionU128Empty()) } diff --git a/types/origin_test.go b/types/origin_test.go index 46a15de25..1cc399268 100644 --- a/types/origin_test.go +++ b/types/origin_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) // newOrigin creates a new Origin type. This function is not exported by purpose – Origin should be ignored and not be @@ -29,43 +31,43 @@ func newOrigin() Origin { } func TestOrigin_EncodeDecode(t *testing.T) { - assertRoundtrip(t, newOrigin()) - assertEncodeEmptyObj[Origin](t, 0) + AssertRoundtrip(t, newOrigin()) + AssertEncodeEmptyObj[Origin](t, 0) } func TestOrigin_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {newOrigin(), 0}, }) } func TestOrigin_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {newOrigin(), MustHexDecodeString("0x")}, }) } func TestOrigin_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {newOrigin(), MustHexDecodeString( "0x0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8")}, }) } func TestOrigin_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {newOrigin(), ""}, }) } func TestOrigin_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {newOrigin(), ""}, }) } func TestOrigin_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {newOrigin(), newOrigin(), true}, {newOrigin(), NewBytes([]byte{}), false}, {newOrigin(), NewBool(true), false}, diff --git a/types/outcome_test.go b/types/outcome_test.go index 492ca3223..d4f04fbd4 100644 --- a/types/outcome_test.go +++ b/types/outcome_test.go @@ -19,9 +19,10 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -43,10 +44,10 @@ var ( }, } - outcomeFuzzOpts = combineFuzzOpts( + outcomeFuzzOpts = CombineFuzzOpts( xcmErrorFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(o *Outcome, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(o *Outcome, c fuzz.Continue) { switch c.Intn(3) { case 0: o.IsComplete = true @@ -69,13 +70,13 @@ var ( ) func TestOutcome_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Outcome](t, 100, outcomeFuzzOpts...) - assertDecodeNilData[Outcome](t) - assertEncodeEmptyObj[Outcome](t, 0) + AssertRoundTripFuzz[Outcome](t, 100, outcomeFuzzOpts...) + AssertDecodeNilData[Outcome](t) + AssertEncodeEmptyObj[Outcome](t, 0) } func TestOutcome_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testOutcome1, MustHexDecodeString("0x007b00000000000000")}, {testOutcome2, MustHexDecodeString("0x01360000000000000000")}, {testOutcome3, MustHexDecodeString("0x0201")}, @@ -83,7 +84,7 @@ func TestOutcome_Encode(t *testing.T) { } func TestOutcome_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x007b00000000000000"), testOutcome1}, {MustHexDecodeString("0x01360000000000000000"), testOutcome2}, {MustHexDecodeString("0x0201"), testOutcome3}, diff --git a/types/peer_info_test.go b/types/peer_info_test.go index ac9e87f9e..59999e729 100644 --- a/types/peer_info_test.go +++ b/types/peer_info_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var testPeerInfo = PeerInfo{ @@ -31,14 +33,14 @@ var testPeerInfo = PeerInfo{ } func TestPeerInfo_EncodeDecode(t *testing.T) { - assertRoundtrip(t, testPeerInfo) - assertRoundTripFuzz[PeerInfo](t, 100) - assertDecodeNilData[PeerInfo](t) - assertEncodeEmptyObj[PeerInfo](t, 42) + AssertRoundtrip(t, testPeerInfo) + AssertRoundTripFuzz[PeerInfo](t, 100) + AssertDecodeNilData[PeerInfo](t) + AssertEncodeEmptyObj[PeerInfo](t, 42) } func TestPeerInfo_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testPeerInfo, MustHexDecodeString("0x20616263313233343528736f6d6520726f6c65737b000000abcd00000000000000000000000000000000000000000000000000000000000039050000"), @@ -47,7 +49,7 @@ func TestPeerInfo_Encode(t *testing.T) { } func TestPeerInfo_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x20616263313233343528736f6d6520726f6c65737b000000abcd00000000000000000000000000000000000000000000000000000000000039050000"), testPeerInfo, diff --git a/types/proxy.go b/types/proxy.go new file mode 100644 index 000000000..5ba4ca08d --- /dev/null +++ b/types/proxy.go @@ -0,0 +1,54 @@ +package types + +import "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + +type ProxyDefinition struct { + Delegate AccountID + ProxyType U8 + Delay U32 +} + +func (p *ProxyDefinition) Decode(decoder scale.Decoder) error { + if err := decoder.Decode(&p.Delegate); err != nil { + return err + } + + if err := decoder.Decode(&p.ProxyType); err != nil { + return err + } + + return decoder.Decode(&p.Delay) +} + +func (p ProxyDefinition) Encode(encoder scale.Encoder) error { + if err := encoder.Encode(p.Delegate); err != nil { + return err + } + + if err := encoder.Encode(p.ProxyType); err != nil { + return err + } + + return encoder.Encode(p.Delay) +} + +type ProxyStorageEntry struct { + ProxyDefinitions []ProxyDefinition + Balance U128 +} + +func (p *ProxyStorageEntry) Decode(decoder scale.Decoder) error { + if err := decoder.Decode(&p.ProxyDefinitions); err != nil { + return err + } + + return decoder.Decode(&p.Balance) +} + +func (p ProxyStorageEntry) Encode(encoder scale.Encoder) error { + if err := encoder.Encode(p.ProxyDefinitions); err != nil { + return err + } + + return encoder.Encode(p.Balance) +} diff --git a/types/proxy_test.go b/types/proxy_test.go new file mode 100644 index 000000000..fcb167476 --- /dev/null +++ b/types/proxy_test.go @@ -0,0 +1,89 @@ +package types_test + +import ( + "math/big" + "testing" + + . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" +) + +var ( + proxyDefinition1 = ProxyDefinition{ + Delegate: newTestAccountID(), + ProxyType: 6, + Delay: 3, + } + proxyDefinition2 = ProxyDefinition{ + Delegate: newTestAccountID(), + ProxyType: 9, + Delay: 0, + } +) + +func TestProxyDefinition_EncodeDecode(t *testing.T) { + AssertRoundTripFuzz[ProxyDefinition](t, 1000) + AssertDecodeNilData[ProxyDefinition](t) + AssertEncodeEmptyObj[ProxyDefinition](t, 37) +} + +func TestProxyDefinition_Encode(t *testing.T) { + AssertEncode(t, []EncodingAssert{ + { + proxyDefinition1, + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200603000000"), + }, + { + proxyDefinition2, + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200900000000"), + }, + }) +} + +func TestProxyDefinition_Decode(t *testing.T) { + AssertDecode(t, []DecodingAssert{ + { + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200603000000"), + proxyDefinition1, + }, + { + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200900000000"), + proxyDefinition2, + }, + }) +} + +var ( + proxyStorageEntry1 = ProxyStorageEntry{ + ProxyDefinitions: []ProxyDefinition{ + proxyDefinition1, + proxyDefinition2, + }, + Balance: NewU128(*big.NewInt(1234)), + } +) + +func TestProxyStorageEntry_EncodeDecode(t *testing.T) { + AssertRoundTripFuzz[ProxyStorageEntry](t, 1000) + AssertDecodeNilData[ProxyStorageEntry](t) + AssertEncodeEmptyObj[ProxyStorageEntry](t, 17) +} + +func TestProxyStorageEntry_Encode(t *testing.T) { + AssertEncode(t, []EncodingAssert{ + { + proxyStorageEntry1, + MustHexDecodeString("0x080102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2006030000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200900000000d2040000000000000000000000000000"), + }, + }) +} + +func TestProxyStorageEntry_Decode(t *testing.T) { + AssertDecode(t, []DecodingAssert{ + { + MustHexDecodeString("0x080102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2006030000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200900000000d2040000000000000000000000000000"), + proxyStorageEntry1, + }, + }) +} diff --git a/types/runtime_version_test.go b/types/runtime_version_test.go index 8a84686fc..cf2d34bc3 100644 --- a/types/runtime_version_test.go +++ b/types/runtime_version_test.go @@ -20,6 +20,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) @@ -61,5 +63,5 @@ func TestRuntimeVersionAPI_Encode_Decode(t *testing.T) { func TestRuntimeVersionAPI_JSONMarshalUnmarshal(t *testing.T) { r := exampleRuntimeVersionAPI - assertJSONRoundTrip(t, &r) + AssertJSONRoundTrip(t, &r) } diff --git a/types/sale_test.go b/types/sale_test.go index 37350fb59..9a76c0dd1 100644 --- a/types/sale_test.go +++ b/types/sale_test.go @@ -20,9 +20,10 @@ import ( "math/big" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -33,19 +34,19 @@ var ( ) func TestTranche_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Tranche](t, 100) - assertDecodeNilData[Tranche](t) - assertEncodeEmptyObj[Tranche](t, 24) + AssertRoundTripFuzz[Tranche](t, 100) + AssertDecodeNilData[Tranche](t) + AssertEncodeEmptyObj[Tranche](t, 24) } func TestTranche_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testTranche, MustHexDecodeString("0x430100000000000004050603010302040000000000000000")}, }) } func TestTranche_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x430100000000000004050603010302040000000000000000"), testTranche}, }) } @@ -72,8 +73,8 @@ var ( PermissionedCurrency: PermissionedCurrency{}, } - currencyIDFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(cID *CurrencyID, c fuzz.Continue) { + currencyIDFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(cID *CurrencyID, c fuzz.Continue) { switch c.Intn(6) { case 0: cID.IsNative = true @@ -97,13 +98,13 @@ var ( ) func TestCurrencyID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[CurrencyID](t, 1000, currencyIDFuzzOpts...) - assertDecodeNilData[CurrencyID](t) - assertEncodeEmptyObj[CurrencyID](t, 0) + AssertRoundTripFuzz[CurrencyID](t, 1000, currencyIDFuzzOpts...) + AssertDecodeNilData[CurrencyID](t) + AssertEncodeEmptyObj[CurrencyID](t, 0) } func TestCurrencyID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testCurrencyID1, MustHexDecodeString("0x00")}, {testCurrencyID2, MustHexDecodeString("0x01")}, {testCurrencyID3, MustHexDecodeString("0x02430100000000000004050603010302040000000000000000")}, @@ -114,7 +115,7 @@ func TestCurrencyID_Encode(t *testing.T) { } func TestCurrencyID_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testCurrencyID1}, {MustHexDecodeString("0x01"), testCurrencyID2}, {MustHexDecodeString("0x02430100000000000004050603010302040000000000000000"), testCurrencyID3}, @@ -132,44 +133,50 @@ var ( ) func TestPrice_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Price](t, 100, currencyIDFuzzOpts...) - assertDecodeNilData[Price](t) - assertEncodeEmptyObj[Price](t, 16) + AssertRoundTripFuzz[Price](t, 100, currencyIDFuzzOpts...) + AssertDecodeNilData[Price](t) + AssertEncodeEmptyObj[Price](t, 16) } func TestPrice_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testPrice, MustHexDecodeString("0x037b000000000000000000000000000000")}, }) } func TestPrice_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x037b000000000000000000000000000000"), testPrice}, }) } var ( testSale = Sale{ - Seller: NewAccountID([]byte("acc_id")), + Seller: newTestAccountID(), Price: testPrice, } ) func TestSale_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Sale](t, 100, currencyIDFuzzOpts...) - assertDecodeNilData[Sale](t) - assertEncodeEmptyObj[Sale](t, 48) + AssertRoundTripFuzz[Sale](t, 100, currencyIDFuzzOpts...) + AssertDecodeNilData[Sale](t) + AssertEncodeEmptyObj[Sale](t, 48) } func TestSale_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ - {testSale, MustHexDecodeString("0x6163635f69640000000000000000000000000000000000000000000000000000037b000000000000000000000000000000")}, + AssertEncode(t, []EncodingAssert{ + { + testSale, + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20037b000000000000000000000000000000"), + }, }) } func TestSale_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ - {MustHexDecodeString("0x6163635f69640000000000000000000000000000000000000000000000000000037b000000000000000000000000000000"), testSale}, + AssertDecode(t, []DecodingAssert{ + { + MustHexDecodeString("0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20037b000000000000000000000000000000"), + testSale, + }, }) } diff --git a/types/signature_test.go b/types/signature_test.go index 5860636ed..31b8dff1f 100644 --- a/types/signature_test.go +++ b/types/signature_test.go @@ -20,47 +20,49 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestSignature_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewSignature(hash64)) - assertRoundTripFuzz[Signature](t, 100) - assertDecodeNilData[Signature](t) - assertEncodeEmptyObj[Signature](t, 64) + AssertRoundtrip(t, NewSignature(hash64)) + AssertRoundTripFuzz[Signature](t, 100) + AssertDecodeNilData[Signature](t) + AssertEncodeEmptyObj[Signature](t, 64) } func TestSignature_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewSignature(hash64), 64}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewSignature(hash64), 64}}) } func TestSignature_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewSignature(hash64), MustHexDecodeString("0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304")}, //nolint:lll }) } func TestSignature_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewSignature(hash64), MustHexDecodeString("0x893a41fa8d4e6447fe2d74a3ae529b1f1a13f3ac5a194907bf19e78e084a0ef6")}, }) } func TestSignature_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewSignature(hash64), "0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304"}, //nolint:lll }) - assertEqual(t, "0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304", NewSignature(hash64).Hex()) + AssertEqual(t, "0x01020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405060708090001020304", NewSignature(hash64).Hex()) } func TestSignature_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewSignature(hash64), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4]"}, //nolint:lll }) } func TestSignature_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewSignature(hash64), NewSignature(hash64), true}, {NewSignature(hash64), NewBytes(hash64), false}, {NewSignature(hash64), NewBool(false), false}, @@ -68,45 +70,45 @@ func TestSignature_Eq(t *testing.T) { } func TestEcdsaSignature_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewEcdsaSignature(hash65)) - assertRoundTripFuzz[EcdsaSignature](t, 100) - assertDecodeNilData[EcdsaSignature](t) - assertEncodeEmptyObj[EcdsaSignature](t, 65) + AssertRoundtrip(t, NewEcdsaSignature(hash65)) + AssertRoundTripFuzz[EcdsaSignature](t, 100) + AssertDecodeNilData[EcdsaSignature](t) + AssertEncodeEmptyObj[EcdsaSignature](t, 65) } func TestEcdsaSignature_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewEcdsaSignature(hash65), 65}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewEcdsaSignature(hash65), 65}}) } func TestEcdsaSignature_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewEcdsaSignature(hash65), MustHexDecodeString("0x0102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405")}, //nolint:lll }) } func TestEcdsaSignature_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewEcdsaSignature(hash65), MustHexDecodeString("0x6149c91c60d1e3789ff09916fce05f2bb7a2af74b45824173072cac546d0f580")}, }) } func TestEcdsaSignature_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewEcdsaSignature(hash65), "0x0102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405"}, //nolint:lll }) - assertEqual(t, "0x0102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405", NewEcdsaSignature(hash65).Hex()) + AssertEqual(t, "0x0102030405060708090001020304050607080900010203040506070809000102030405060708090001020304050607080900010203040506070809000102030405", NewEcdsaSignature(hash65).Hex()) } func TestEcdsaSignature_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewEcdsaSignature(hash65), "[1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5]"}, //nolint:lll }) } func TestEcdsaSignature_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewEcdsaSignature(hash65), NewEcdsaSignature(hash65), true}, {NewEcdsaSignature(hash65), NewBytes(hash65), false}, {NewEcdsaSignature(hash65), NewBool(false), false}, diff --git a/types/storage_change_set.go b/types/storage_change_set.go index 7b3b12dad..4f71cccb6 100644 --- a/types/storage_change_set.go +++ b/types/storage_change_set.go @@ -19,6 +19,8 @@ package types import ( "encoding/json" "fmt" + + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" ) // StorageChangeSet contains changes from storage subscriptions @@ -43,14 +45,14 @@ func (r *KeyValueOption) UnmarshalJSON(b []byte) error { return fmt.Errorf("expected at least one entry for KeyValueOption") case 2: r.HasStorageData = true - data, err := HexDecodeString(tmp[1]) + data, err := codec.HexDecodeString(tmp[1]) if err != nil { return err } r.StorageData = data fallthrough case 1: - key, err := HexDecodeString(tmp[0]) + key, err := codec.HexDecodeString(tmp[0]) if err != nil { return err } diff --git a/types/storage_change_set_test.go b/types/storage_change_set_test.go index aff69d3fe..26803be90 100644 --- a/types/storage_change_set_test.go +++ b/types/storage_change_set_test.go @@ -21,6 +21,7 @@ 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" ) diff --git a/types/storage_data_raw_test.go b/types/storage_data_raw_test.go index a1a49853e..762f672d2 100644 --- a/types/storage_data_raw_test.go +++ b/types/storage_data_raw_test.go @@ -20,11 +20,13 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) func TestStorageDataRaw_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewStorageDataRaw([]byte{12, 251, 42}), 3}, {NewStorageDataRaw([]byte{}), 0}, }) @@ -47,7 +49,7 @@ func TestStorageDataRaw_Decode(t *testing.T) { } func TestStorageDataRaw_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewStorageDataRaw([]byte{0, 42, 254}), MustHexDecodeString( "0x537db36f5b5970b679a28a3df8d219317d658014fb9c3d409c0c799d8ecf149d")}, {NewStorageDataRaw([]byte{}), MustHexDecodeString( @@ -56,7 +58,7 @@ func TestStorageDataRaw_Hash(t *testing.T) { } func TestStorageDataRaw_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewStorageDataRaw([]byte{0, 0, 0}), "0x000000"}, {NewStorageDataRaw([]byte{171, 18, 52}), "0xab1234"}, {NewStorageDataRaw([]byte{0, 1}), "0x0001"}, @@ -65,7 +67,7 @@ func TestStorageDataRaw_Hex(t *testing.T) { } func TestStorageDataRaw_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewStorageDataRaw([]byte{0, 0, 0}), "[0 0 0]"}, {NewStorageDataRaw([]byte{171, 18, 52}), "[171 18 52]"}, {NewStorageDataRaw([]byte{0, 1}), "[0 1]"}, @@ -74,7 +76,7 @@ func TestStorageDataRaw_String(t *testing.T) { } func TestStorageDataRaw_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewStorageDataRaw([]byte{1, 0, 0}), NewStorageDataRaw([]byte{1, 0}), false}, {NewStorageDataRaw([]byte{0, 0, 1}), NewStorageDataRaw([]byte{0, 1}), false}, {NewStorageDataRaw([]byte{0, 0, 0}), NewStorageDataRaw([]byte{0, 0}), false}, diff --git a/types/storage_key_test.go b/types/storage_key_test.go index 4774092ec..78784d5cc 100644 --- a/types/storage_key_test.go +++ b/types/storage_key_test.go @@ -23,9 +23,10 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/hash" - "github.com/centrifuge/go-substrate-rpc-client/v4/xxhash" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + "github.com/centrifuge/go-substrate-rpc-client/v4/xxhash" "github.com/stretchr/testify/assert" ) @@ -367,7 +368,7 @@ func TestCreateStorageKeyDoubleMapV9(t *testing.T) { } func TestStorageKey_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewStorageKey(MustHexDecodeString("0x00")), 1}, {NewStorageKey(MustHexDecodeString("0xab1234")), 3}, {NewStorageKey(MustHexDecodeString("0x0001")), 2}, @@ -375,7 +376,7 @@ func TestStorageKey_EncodedLength(t *testing.T) { } func TestStorageKey_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewStorageKey([]byte{171, 18, 52}), MustHexDecodeString("0xab1234")}, {NewStorageKey([]byte{}), MustHexDecodeString("0x")}, }) @@ -390,7 +391,7 @@ func TestStorageKey_Decode(t *testing.T) { } func TestStorageKey_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewStorageKey([]byte{0, 42, 254}), MustHexDecodeString( "0x537db36f5b5970b679a28a3df8d219317d658014fb9c3d409c0c799d8ecf149d")}, {NewStorageKey([]byte{0, 0}), MustHexDecodeString( @@ -399,7 +400,7 @@ func TestStorageKey_Hash(t *testing.T) { } func TestStorageKey_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewStorageKey([]byte{0, 0, 0}), "0x000000"}, {NewStorageKey([]byte{171, 18, 52}), "0xab1234"}, {NewStorageKey([]byte{0, 1}), "0x0001"}, @@ -408,7 +409,7 @@ func TestStorageKey_Hex(t *testing.T) { } func TestStorageKey_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewStorageKey([]byte{0, 0, 0}), "[0 0 0]"}, {NewStorageKey([]byte{171, 18, 52}), "[171 18 52]"}, {NewStorageKey([]byte{0, 1}), "[0 1]"}, @@ -417,7 +418,7 @@ func TestStorageKey_String(t *testing.T) { } func TestStorageKey_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewStorageKey([]byte{1, 0, 0}), NewStorageKey([]byte{1, 0}), false}, {NewStorageKey([]byte{0, 0, 1}), NewStorageKey([]byte{0, 1}), false}, {NewStorageKey([]byte{0, 0, 0}), NewStorageKey([]byte{0, 0}), false}, diff --git a/types/tally_test.go b/types/tally_test.go index 2bb3a39fc..63b451a26 100644 --- a/types/tally_test.go +++ b/types/tally_test.go @@ -21,6 +21,8 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) var ( @@ -31,19 +33,19 @@ var ( ) func TestTally_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Tally](t, 100) - assertDecodeNilData[Tally](t) - assertEncodeEmptyObj[Tally](t, 32) + AssertRoundTripFuzz[Tally](t, 100) + AssertDecodeNilData[Tally](t) + AssertEncodeEmptyObj[Tally](t, 32) } func TestTally_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testTally, MustHexDecodeString("0x7b000000000000000000000000000000c8010000000000000000000000000000")}, }) } func TestTally_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x7b000000000000000000000000000000c8010000000000000000000000000000"), testTally}, }) } diff --git a/types/test/test-gen/client.go b/types/test/test-gen/client.go index 18b367f3f..61fa6f591 100644 --- a/types/test/test-gen/client.go +++ b/types/test/test-gen/client.go @@ -26,6 +26,8 @@ import ( "net/http" "time" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + "github.com/centrifuge/go-substrate-rpc-client/v4/types" gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" @@ -122,7 +124,7 @@ func (c *Client) getTestData(blockNumber int) (*TestData, error) { return nil, fmt.Errorf("couldn't get latest metadata: %w", err) } - encodedMetadata, err := types.Encode(meta) + encodedMetadata, err := codec.Encode(meta) if err != nil { return nil, fmt.Errorf("couldn't encode metadata: %w", err) diff --git a/types/test/test-gen/client_test.go b/types/test/test-gen/client_test.go index da3a3acf0..162865d10 100644 --- a/types/test/test-gen/client_test.go +++ b/types/test/test-gen/client_test.go @@ -27,6 +27,8 @@ import ( "testing" "time" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4" mockClient "github.com/centrifuge/go-substrate-rpc-client/v4/client/mocks" mockChain "github.com/centrifuge/go-substrate-rpc-client/v4/rpc/chain/mocks" @@ -115,11 +117,11 @@ func TestClient_GetTestData(t *testing.T) { defer ts.Close() var metadata types.Metadata - err := types.DecodeFromHex(types.MetadataV14Data, &metadata) + err := codec.DecodeFromHex(types.MetadataV14Data, &metadata) assert.EqualValues(t, metadata.Version, 14) assert.Nil(t, err) - encodedMeta, err := types.Encode(metadata) + encodedMeta, err := codec.Encode(metadata) assert.Nil(t, err) @@ -534,7 +536,7 @@ func TestClient_GetTestData_StorageKeyCreationError(t *testing.T) { defer ts.Close() var metadata types.Metadata - err := types.DecodeFromHex(types.MetadataV14Data, &metadata) + err := codec.DecodeFromHex(types.MetadataV14Data, &metadata) assert.EqualValues(t, metadata.Version, 14) assert.Nil(t, err) @@ -613,7 +615,7 @@ func TestClient_GetTestData_BlockHashError(t *testing.T) { defer ts.Close() var metadata types.Metadata - err := types.DecodeFromHex(types.MetadataV14Data, &metadata) + err := codec.DecodeFromHex(types.MetadataV14Data, &metadata) assert.EqualValues(t, metadata.Version, 14) assert.Nil(t, err) @@ -694,7 +696,7 @@ func TestClient_GetTestData_StorageError(t *testing.T) { defer ts.Close() var metadata types.Metadata - err := types.DecodeFromHex(types.MetadataV14Data, &metadata) + err := codec.DecodeFromHex(types.MetadataV14Data, &metadata) assert.EqualValues(t, metadata.Version, 14) assert.Nil(t, err) diff --git a/types/test/types_test.go b/types/test/types_test.go index 3f596f945..dac419c09 100644 --- a/types/test/types_test.go +++ b/types/test/types_test.go @@ -23,6 +23,8 @@ import ( "path" "testing" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + "github.com/centrifuge/go-substrate-rpc-client/v4/types" ) @@ -67,7 +69,7 @@ func TestTypesDecode(t *testing.T) { switch subdirEntry.Name() { case "meta_bytes": - if err := types.Decode(b, &metadata); err != nil { + if err := codec.Decode(b, &metadata); err != nil { t.Errorf("Couldn't decode Metadata - %s", err) } case "storage_bytes": diff --git a/types/test_utils/test_utils.go b/types/test_utils/test_utils.go new file mode 100644 index 000000000..e399819af --- /dev/null +++ b/types/test_utils/test_utils.go @@ -0,0 +1,252 @@ +// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls +// +// Copyright 2019 Centrifuge GmbH +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package testutils + +import ( + "bytes" + "fmt" + "reflect" + "testing" + + "github.com/centrifuge/go-substrate-rpc-client/v4/scale" + "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" + "golang.org/x/crypto/blake2b" +) + +type FuzzOpt func(f *fuzz.Fuzzer) + +func WithNilChance(p float64) FuzzOpt { + return func(f *fuzz.Fuzzer) { + f.NilChance(p) + } +} + +func WithFuzzFuncs(fuzzFns ...any) FuzzOpt { + return func(f *fuzz.Fuzzer) { + f.Funcs(fuzzFns...) + } +} + +func WithNumElements(atLeast, atMost int) FuzzOpt { + return func(f *fuzz.Fuzzer) { + f.NumElements(atLeast, atMost) + } +} + +func WithMaxDepth(depth int) FuzzOpt { + return func(f *fuzz.Fuzzer) { + f.MaxDepth(depth) + } +} + +func CombineFuzzOpts(optsSlice ...[]FuzzOpt) []FuzzOpt { + var o []FuzzOpt + + for _, opts := range optsSlice { + o = append(o, opts...) + } + + return o +} + +func AssertRoundTripFuzz[T any](t *testing.T, fuzzCount int, fuzzOpts ...FuzzOpt) { + f := fuzz.New().NilChance(0) + + for _, opt := range fuzzOpts { + opt(f) + } + + for i := 0; i < fuzzCount; i++ { + var fuzzData T + + f.Fuzz(&fuzzData) + + AssertRoundtrip(t, fuzzData) + } +} + +func AssertDecodeNilData[T any](t *testing.T) { + var obj T + + err := scale.NewDecoder(bytes.NewReader(nil)).Decode(&obj) + assert.NotNil(t, err) +} + +func AssertEncodeEmptyObj[T any](t *testing.T, expectedByteLen int) { + var obj T + + var buffer = bytes.Buffer{} + + err := scale.NewEncoder(&buffer).Encode(obj) + assert.Nil(t, err) + assert.Len(t, buffer.Bytes(), expectedByteLen) +} + +type EncodedLengthAssert struct { + Input interface{} + Expected int +} + +func AssertEqual(t *testing.T, a interface{}, b interface{}) { + if reflect.DeepEqual(a, b) { + return + } + t.Errorf("Received %#v (type %v), expected %#v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b)) +} + +func AssertRoundtrip(t *testing.T, value interface{}) { + var buffer = bytes.Buffer{} + err := scale.NewEncoder(&buffer).Encode(value) + assert.NoError(t, err) + target := reflect.New(reflect.TypeOf(value)) + err = scale.NewDecoder(&buffer).Decode(target.Interface()) + assert.NoError(t, err) + AssertEqual(t, target.Elem().Interface(), value) +} + +func AssertEncodedLength(t *testing.T, encodedLengthAsserts []EncodedLengthAssert) { + for _, test := range encodedLengthAsserts { + result, err := codec.EncodedLength(test.Input) + if err != nil { + t.Errorf("Encoded length error for input %v: %v\n", test.Input, err) + } + if result != test.Expected { + t.Errorf("Fail, input %v, expected %v, result %v\n", test.Input, test.Expected, result) + } + } +} + +type EncodingAssert struct { + Input interface{} + Expected []byte +} + +func AssertEncode(t *testing.T, encodingAsserts []EncodingAssert) { + for _, test := range encodingAsserts { + result, err := codec.Encode(test.Input) + if err != nil { + t.Errorf("Encoding error for input %v: %v\n", test.Input, err) + } + + if !bytes.Equal(result, test.Expected) { + t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.Input, test.Expected, result) + } + } +} + +type DecodingAssert struct { + Input []byte + Expected interface{} +} + +func AssertDecode(t *testing.T, decodingAsserts []DecodingAssert) { + for _, test := range decodingAsserts { + target := reflect.New(reflect.TypeOf(test.Expected)) + err := codec.Decode(test.Input, target.Interface()) + if err != nil { + t.Errorf("Encoding error for input %v: %v\n", test.Input, err) + } + AssertEqual(t, target.Elem().Interface(), test.Expected) + } +} + +type HashAssert struct { + Input interface{} + Expected []byte +} + +func AssertHash(t *testing.T, hashAsserts []HashAssert) { + for _, test := range hashAsserts { + enc, err := codec.Encode(test.Input) + + if err != nil { + t.Errorf("Couldn't encode input %v: %s", test.Input, err) + } + + result := blake2b.Sum256(enc) + + if !bytes.Equal(result[:], test.Expected) { + t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.Input, test.Expected, result) + } + } +} + +type EncodeToHexAssert struct { + Input interface{} + Expected string +} + +func AssertEncodeToHex(t *testing.T, encodeToHexAsserts []EncodeToHexAssert) { + for _, test := range encodeToHexAsserts { + result, err := codec.EncodeToHex(test.Input) + if err != nil { + t.Errorf("Hex error for input %v: %v\n", test.Input, err) + } + if result != test.Expected { + t.Errorf("Fail, input %v, expected %v, result %v\n", test.Input, test.Expected, result) + } + } +} + +type StringAssert struct { + Input interface{} + Expected string +} + +func AssertString(t *testing.T, stringAsserts []StringAssert) { + for _, test := range stringAsserts { + result := fmt.Sprintf("%v", test.Input) + if result != test.Expected { + t.Errorf("Fail, input %v, expected %v, result %v\n", test.Input, test.Expected, result) + } + } +} + +type EqAssert struct { + Input interface{} + Other interface{} + Expected bool +} + +func AssertEq(t *testing.T, eqAsserts []EqAssert) { + for _, test := range eqAsserts { + result := codec.Eq(test.Input, test.Other) + if result != test.Expected { + t.Errorf("Fail, input %v, other %v, expected %v, result %v\n", test.Input, test.Other, test.Expected, result) + } + } +} + +type jsonMarshalerUnmarshaler[T any] interface { + UnmarshalJSON([]byte) error + MarshalJSON() ([]byte, error) + + *T // helper type that allows us to instantiate a non-nil T +} + +func AssertJSONRoundTrip[T any, U jsonMarshalerUnmarshaler[T]](t *testing.T, value U) { + b, err := value.MarshalJSON() + assert.NoError(t, err) + + tt := U(new(T)) + err = tt.UnmarshalJSON(b) + assert.NoError(t, err) + + AssertEqual(t, value, tt) +} diff --git a/types/test_utils_test.go b/types/test_utils_test.go deleted file mode 100644 index 1322691ce..000000000 --- a/types/test_utils_test.go +++ /dev/null @@ -1,247 +0,0 @@ -// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls -// -// Copyright 2019 Centrifuge GmbH -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package types_test - -import ( - "bytes" - "fmt" - "reflect" - "testing" - - "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" - fuzz "github.com/google/gofuzz" - "github.com/stretchr/testify/assert" -) - -type fuzzOpt func(f *fuzz.Fuzzer) - -func withNilChance(p float64) fuzzOpt { - return func(f *fuzz.Fuzzer) { - f.NilChance(p) - } -} - -func withFuzzFuncs(fuzzFns ...any) fuzzOpt { - return func(f *fuzz.Fuzzer) { - f.Funcs(fuzzFns...) - } -} - -func withNumElements(atLeast, atMost int) fuzzOpt { - return func(f *fuzz.Fuzzer) { - f.NumElements(atLeast, atMost) - } -} - -func withMaxDepth(depth int) fuzzOpt { - return func(f *fuzz.Fuzzer) { - f.MaxDepth(depth) - } -} - -func combineFuzzOpts(optsSlice ...[]fuzzOpt) []fuzzOpt { - var o []fuzzOpt - - for _, opts := range optsSlice { - o = append(o, opts...) - } - - return o -} - -func assertRoundTripFuzz[T any](t *testing.T, fuzzCount int, fuzzOpts ...fuzzOpt) { - f := fuzz.New().NilChance(0) - - for _, opt := range fuzzOpts { - opt(f) - } - - for i := 0; i < fuzzCount; i++ { - var fuzzData T - - f.Fuzz(&fuzzData) - - assertRoundtrip(t, fuzzData) - } -} - -func assertDecodeNilData[T any](t *testing.T) { - var obj T - - err := scale.NewDecoder(bytes.NewReader(nil)).Decode(&obj) - assert.NotNil(t, err) -} - -func assertEncodeEmptyObj[T any](t *testing.T, expectedByteLen int) { - var obj T - - var buffer = bytes.Buffer{} - - err := scale.NewEncoder(&buffer).Encode(obj) - assert.Nil(t, err) - assert.Len(t, buffer.Bytes(), expectedByteLen) -} - -type encodedLengthAssert struct { - input interface{} - expected int -} - -func assertEqual(t *testing.T, a interface{}, b interface{}) { - if reflect.DeepEqual(a, b) { - return - } - t.Errorf("Received %#v (type %v), expected %#v (type %v)", a, reflect.TypeOf(a), b, reflect.TypeOf(b)) -} - -func assertRoundtrip(t *testing.T, value interface{}) { - var buffer = bytes.Buffer{} - err := scale.NewEncoder(&buffer).Encode(value) - assert.NoError(t, err) - target := reflect.New(reflect.TypeOf(value)) - err = scale.NewDecoder(&buffer).Decode(target.Interface()) - assert.NoError(t, err) - assertEqual(t, target.Elem().Interface(), value) -} - -func assertEncodedLength(t *testing.T, encodedLengthAsserts []encodedLengthAssert) { - for _, test := range encodedLengthAsserts { - result, err := EncodedLength(test.input) - if err != nil { - t.Errorf("Encoded length error for input %v: %v\n", test.input, err) - } - if result != test.expected { - t.Errorf("Fail, input %v, expected %v, result %v\n", test.input, test.expected, result) - } - } -} - -type encodingAssert struct { - input interface{} - expected []byte -} - -func assertEncode(t *testing.T, encodingAsserts []encodingAssert) { - for _, test := range encodingAsserts { - result, err := Encode(test.input) - if err != nil { - t.Errorf("Encoding error for input %v: %v\n", test.input, err) - } - - if !bytes.Equal(result, test.expected) { - t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.input, test.expected, result) - } - } -} - -type decodingAssert struct { - input []byte - expected interface{} -} - -func assertDecode(t *testing.T, decodingAsserts []decodingAssert) { - for _, test := range decodingAsserts { - target := reflect.New(reflect.TypeOf(test.expected)) - err := Decode(test.input, target.Interface()) - if err != nil { - t.Errorf("Encoding error for input %v: %v\n", test.input, err) - } - assertEqual(t, target.Elem().Interface(), test.expected) - } -} - -type hashAssert struct { - input interface{} - expected []byte -} - -func assertHash(t *testing.T, hashAsserts []hashAssert) { - for _, test := range hashAsserts { - result, err := GetHash(test.input) - if err != nil { - t.Errorf("Hash error for input %v: %v\n", test.input, err) - } - if !bytes.Equal(result[:], test.expected) { - t.Errorf("Fail, input %v, expected %#x, result %#x\n", test.input, test.expected, result) - } - } -} - -type encodeToHexAssert struct { - input interface{} - expected string -} - -func assertEncodeToHex(t *testing.T, encodeToHexAsserts []encodeToHexAssert) { - for _, test := range encodeToHexAsserts { - result, err := EncodeToHex(test.input) - if err != nil { - t.Errorf("Hex error for input %v: %v\n", test.input, err) - } - if result != test.expected { - t.Errorf("Fail, input %v, expected %v, result %v\n", test.input, test.expected, result) - } - } -} - -type stringAssert struct { - input interface{} - expected string -} - -func assertString(t *testing.T, stringAsserts []stringAssert) { - for _, test := range stringAsserts { - result := fmt.Sprintf("%v", test.input) - if result != test.expected { - t.Errorf("Fail, input %v, expected %v, result %v\n", test.input, test.expected, result) - } - } -} - -type eqAssert struct { - input interface{} - other interface{} - expected bool -} - -func assertEq(t *testing.T, eqAsserts []eqAssert) { - for _, test := range eqAsserts { - result := Eq(test.input, test.other) - if result != test.expected { - t.Errorf("Fail, input %v, other %v, expected %v, result %v\n", test.input, test.other, test.expected, result) - } - } -} - -type jsonMarshalerUnmarshaler[T any] interface { - UnmarshalJSON([]byte) error - MarshalJSON() ([]byte, error) - - *T // helper type that allows us to instantiate a non-nil T -} - -func assertJSONRoundTrip[T any, U jsonMarshalerUnmarshaler[T]](t *testing.T, value U) { - b, err := value.MarshalJSON() - assert.NoError(t, err) - - tt := U(new(T)) - err = tt.UnmarshalJSON(b) - assert.NoError(t, err) - - assertEqual(t, value, tt) -} diff --git a/types/text_test.go b/types/text_test.go index 7d23f8282..8dfdcbb1e 100644 --- a/types/text_test.go +++ b/types/text_test.go @@ -20,17 +20,19 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestString_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewText("")) - assertRoundtrip(t, NewText("My nice string")) - assertRoundTripFuzz[Text](t, 100) - assertEncodeEmptyObj[Text](t, 1) + AssertRoundtrip(t, NewText("")) + AssertRoundtrip(t, NewText("My nice string")) + AssertRoundTripFuzz[Text](t, 100) + AssertEncodeEmptyObj[Text](t, 1) } func TestString_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {NewText(""), 1}, {NewText("My nice string"), 15}, {NewText("ښ"), 3}, @@ -38,7 +40,7 @@ func TestString_EncodedLength(t *testing.T) { } func TestString_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewText(""), MustHexDecodeString("0x00")}, {NewText("My nice string"), MustHexDecodeString("0x384d79206e69636520737472696e67")}, {NewText("ښ"), MustHexDecodeString("0x08da9a")}, @@ -46,7 +48,7 @@ func TestString_Encode(t *testing.T) { } func TestString_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewText(""), MustHexDecodeString("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")}, {NewText("My nice string"), MustHexDecodeString( "0x30f31ff5f82596c990e5afd2b656db0ca30e1a5a9cc7eb5f83ee18731eecd453")}, @@ -54,7 +56,7 @@ func TestString_Hash(t *testing.T) { } func TestString_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewText(""), "0x00"}, {NewText("My nice string"), "0x384d79206e69636520737472696e67"}, {NewText("ښ"), "0x08da9a"}, @@ -62,7 +64,7 @@ func TestString_Hex(t *testing.T) { } func TestString_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewText(""), ""}, {NewText("My nice string"), "My nice string"}, {NewText("ښ"), "ښ"}, @@ -70,7 +72,7 @@ func TestString_String(t *testing.T) { } func TestString_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewText("My nice string"), NewText("My nice string"), true}, {NewText(""), NewText("23"), false}, {NewText("My nice string"), NewU8(23), false}, diff --git a/types/type_test.go b/types/type_test.go index bdeb66c20..c30b9a5f6 100644 --- a/types/type_test.go +++ b/types/type_test.go @@ -20,31 +20,33 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestType_EncodeDecode(t *testing.T) { - assertRoundtrip(t, Type("")) - assertRoundtrip(t, Type("Custom Type")) - assertRoundTripFuzz[Type](t, 100) - assertEncodeEmptyObj[Type](t, 1) + AssertRoundtrip(t, Type("")) + AssertRoundtrip(t, Type("Custom Type")) + AssertRoundTripFuzz[Type](t, 100) + AssertEncodeEmptyObj[Type](t, 1) } func TestType_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{ + AssertEncodedLength(t, []EncodedLengthAssert{ {Type(""), 1}, {Type("Custom Type"), 12}, }) } func TestType_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {Type(""), MustHexDecodeString("0x00")}, {Type("Custom Type"), MustHexDecodeString("0x2c437573746f6d2054797065")}, }) } func TestType_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {Type(""), MustHexDecodeString("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")}, {Type("Custom Type"), MustHexDecodeString( "0x7c2996c160a91b8479eae96ade3ad8b287edc22997bf209b7bc0ca04d3bc0725")}, @@ -52,21 +54,21 @@ func TestType_Hash(t *testing.T) { } func TestType_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {Type(""), "0x00"}, {Type("Custom Type"), "0x2c437573746f6d2054797065"}, }) } func TestType_Type(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {Type(""), ""}, {Type("Custom Type"), "Custom Type"}, }) } func TestType_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {Type("Custom Type"), Type("Custom Type"), true}, {Type(""), Type("23"), false}, {Type("Custom Type"), NewU8(23), false}, diff --git a/types/uint_test.go b/types/uint_test.go index 0e3cf9512..98a86c947 100644 --- a/types/uint_test.go +++ b/types/uint_test.go @@ -22,46 +22,47 @@ import ( "testing" "github.com/centrifuge/go-substrate-rpc-client/v4/scale" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" "github.com/stretchr/testify/assert" ) func TestU8_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewU8(0)) - assertRoundtrip(t, NewU8(12)) + AssertRoundtrip(t, NewU8(0)) + AssertRoundtrip(t, NewU8(12)) } func TestU8_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewU8(13), 1}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewU8(13), 1}}) } func TestU8_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewU8(29), MustHexDecodeString("0x1d")}, }) } func TestU8_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewU8(29), MustHexDecodeString("0x6a9843ae0195ae1e6f95c7fbd34a42414c77e243aa18a959b5912a1f0f391b54")}, }) } func TestU8_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewU8(29), "0x1d"}, }) } func TestU8_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewU8(29), "29"}, }) } func TestU8_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewU8(23), NewU8(23), true}, {NewU8(23), NewBool(false), false}, }) @@ -70,44 +71,44 @@ func TestU8_Eq(t *testing.T) { func TestU8_MarshalUnmarshal(t *testing.T) { u := NewU8(3) - assertJSONRoundTrip(t, &u) + AssertJSONRoundTrip(t, &u) } func TestU16_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewU16(0)) - assertRoundtrip(t, NewU16(12)) + AssertRoundtrip(t, NewU16(0)) + AssertRoundtrip(t, NewU16(12)) } func TestU16_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewU16(13), 2}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewU16(13), 2}}) } func TestU16_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewU16(29), MustHexDecodeString("0x1d00")}, }) } func TestU16_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewU16(29), MustHexDecodeString("0x4e59f743a8e19ecb3022652bdef4343e62793d1f7378a688a82741b5d029d3d5")}, }) } func TestU16_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewU16(29), "0x1d00"}, }) } func TestU16_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewU16(29), "29"}, }) } func TestU16_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewU16(23), NewU16(23), true}, {NewU16(23), NewBool(false), false}, }) @@ -116,44 +117,44 @@ func TestU16_Eq(t *testing.T) { func TestU16_MarshalUnmarshal(t *testing.T) { u := NewU16(3) - assertJSONRoundTrip(t, &u) + AssertJSONRoundTrip(t, &u) } func TestU32_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewU32(0)) - assertRoundtrip(t, NewU32(12)) + AssertRoundtrip(t, NewU32(0)) + AssertRoundtrip(t, NewU32(12)) } func TestU32_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewU32(13), 4}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewU32(13), 4}}) } func TestU32_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewU32(29), MustHexDecodeString("0x1d000000")}, }) } func TestU32_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewU32(29), MustHexDecodeString("0x60ebb66f09bc7fdd21772ab1ed0efb1fd1208e3f5cd20d2d9a29a2a79b6f953f")}, }) } func TestU32_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewU32(29), "0x1d000000"}, }) } func TestU32_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewU32(29), "29"}, }) } func TestU32_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewU32(23), NewU32(23), true}, {NewU32(23), NewBool(false), false}, }) @@ -162,44 +163,44 @@ func TestU32_Eq(t *testing.T) { func TestU32_MarshalUnmarshal(t *testing.T) { u := NewU32(3) - assertJSONRoundTrip(t, &u) + AssertJSONRoundTrip(t, &u) } func TestU64_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewU64(0)) - assertRoundtrip(t, NewU64(12)) + AssertRoundtrip(t, NewU64(0)) + AssertRoundtrip(t, NewU64(12)) } func TestU64_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewU64(13), 8}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewU64(13), 8}}) } func TestU64_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewU64(29), MustHexDecodeString("0x1d00000000000000")}, }) } func TestU64_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewU64(29), MustHexDecodeString("0x83e168a13a013e6d47b0778f046aaa05d6c01d6857d044d9e9b658a6d85eb865")}, }) } func TestU64_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewU64(29), "0x1d00000000000000"}, }) } func TestU64_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewU64(29), "29"}, }) } func TestU64_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewU64(23), NewU64(23), true}, {NewU64(23), NewBool(false), false}, }) @@ -208,7 +209,7 @@ func TestU64_Eq(t *testing.T) { func TestU64_MarshalUnmarshal(t *testing.T) { u := NewU64(3) - assertJSONRoundTrip(t, &u) + AssertJSONRoundTrip(t, &u) } func TestUCompact_EncodeDecode(t *testing.T) { @@ -285,18 +286,18 @@ func TestUCompact_EncodeNegative(t *testing.T) { } func TestU128_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewU128(*big.NewInt(0))) - assertRoundtrip(t, NewU128(*big.NewInt(12))) + AssertRoundtrip(t, NewU128(*big.NewInt(0))) + AssertRoundtrip(t, NewU128(*big.NewInt(12))) bigPos := big.NewInt(0) bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) - assertRoundtrip(t, NewU128(*bigPos)) + AssertRoundtrip(t, NewU128(*bigPos)) - assertDecodeNilData[U128](t) + AssertDecodeNilData[U128](t) } func TestU128_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewU128(*big.NewInt(13)), 16}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewU128(*big.NewInt(13)), 16}}) } func TestU128_Encode(t *testing.T) { @@ -304,7 +305,7 @@ func TestU128_Encode(t *testing.T) { b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) c := big.NewInt(0).SetBytes([]byte{255, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewU128(*big.NewInt(0)), MustHexDecodeString("0x00000000000000000000000000000000")}, {NewU128(*big.NewInt(29)), MustHexDecodeString("0x1d000000000000000000000000000000")}, {NewU128(*a), MustHexDecodeString("0x34120000000000000000000000000000")}, @@ -318,7 +319,7 @@ func TestU128_Decode(t *testing.T) { b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) c := big.NewInt(0).SetBytes([]byte{255, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}) - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00000000000000000000000000000000"), NewU128(*big.NewInt(0))}, {MustHexDecodeString("0x1d000000000000000000000000000000"), NewU128(*big.NewInt(29))}, {MustHexDecodeString("0x34120000000000000000000000000000"), NewU128(*a)}, @@ -328,26 +329,26 @@ func TestU128_Decode(t *testing.T) { } func TestU128_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewU128(*big.NewInt(29)), MustHexDecodeString( "0x139bd9153bbc4913d4161f7a5dd39912b5d22b57a8b557f0a24078a11f943174")}, }) } func TestU128_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewU128(*big.NewInt(29)), "0x1d000000000000000000000000000000"}, }) } func TestU128_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewU128(*big.NewInt(29)), "29"}, }) } func TestU128_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewU128(*big.NewInt(23)), NewU128(*big.NewInt(23)), true}, {NewU128(*big.NewInt(23)), NewU64(23), false}, {NewU128(*big.NewInt(23)), NewBool(false), false}, @@ -364,23 +365,23 @@ func TestU128_GobEncodeDecode(t *testing.T) { err = target.GobDecode(b) assert.NoError(t, err) - assertEqual(t, u, *target) + AssertEqual(t, u, *target) } func TestU256_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewU256(*big.NewInt(0))) - assertRoundtrip(t, NewU256(*big.NewInt(12))) + AssertRoundtrip(t, NewU256(*big.NewInt(0))) + AssertRoundtrip(t, NewU256(*big.NewInt(12))) bigPos := big.NewInt(0) bigPos.SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) - assertRoundtrip(t, NewU256(*bigPos)) + AssertRoundtrip(t, NewU256(*bigPos)) - assertDecodeNilData[U256](t) + AssertDecodeNilData[U256](t) } func TestU256_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewU256(*big.NewInt(13)), 32}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewU256(*big.NewInt(13)), 32}}) } func TestU256_Encode(t *testing.T) { @@ -389,7 +390,7 @@ func TestU256_Encode(t *testing.T) { b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewU256(*big.NewInt(0)), MustHexDecodeString( "0x0000000000000000000000000000000000000000000000000000000000000000")}, {NewU256(*big.NewInt(29)), MustHexDecodeString( @@ -405,7 +406,7 @@ func TestU256_Decode(t *testing.T) { b := big.NewInt(0).SetBytes([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 30, 31, 32}) - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x0000000000000000000000000000000000000000000000000000000000000000"), NewU256(*big.NewInt(0))}, {MustHexDecodeString("0x1d00000000000000000000000000000000000000000000000000000000000000"), @@ -416,26 +417,26 @@ func TestU256_Decode(t *testing.T) { } func TestU256_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewU256(*big.NewInt(29)), MustHexDecodeString( "0x92d6618c3e5941a74d1e805e1a8485469229f9a9c58145761bd9209bc2f4360d")}, }) } func TestU256_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewU256(*big.NewInt(29)), "0x1d00000000000000000000000000000000000000000000000000000000000000"}, }) } func TestU256_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewU256(*big.NewInt(29)), "29"}, }) } func TestU256_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewU256(*big.NewInt(23)), NewU256(*big.NewInt(23)), true}, {NewU256(*big.NewInt(23)), NewU128(*big.NewInt(23)), false}, {NewU256(*big.NewInt(23)), NewI256(*big.NewInt(23)), false}, diff --git a/types/usize_test.go b/types/usize_test.go index 94b4df6b0..e7583417f 100644 --- a/types/usize_test.go +++ b/types/usize_test.go @@ -20,49 +20,51 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestUSize_EncodeDecode(t *testing.T) { - assertRoundtrip(t, USize(0)) - assertRoundtrip(t, USize(12)) + AssertRoundtrip(t, USize(0)) + AssertRoundtrip(t, USize(12)) } func TestUsize_JSONMarshalUnmarshal(t *testing.T) { u := USize(11) - assertJSONRoundTrip(t, &u) + AssertJSONRoundTrip(t, &u) } func TestUSize_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{USize(13), 4}}) + AssertEncodedLength(t, []EncodedLengthAssert{{USize(13), 4}}) } func TestUSize_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {USize(29), MustHexDecodeString("0x1d000000")}, }) } func TestUSize_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {USize(29), MustHexDecodeString("0x60ebb66f09bc7fdd21772ab1ed0efb1fd1208e3f5cd20d2d9a29a2a79b6f953f")}, }) } func TestUSize_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {USize(29), "0x1d000000"}, }) } func TestUSize_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {USize(29), "29"}, }) } func TestUSize_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {USize(23), USize(23), true}, {USize(23), NewBool(false), false}, }) diff --git a/types/weight_multiplier_test.go b/types/weight_multiplier_test.go index 91b7441f3..72a628fb2 100644 --- a/types/weight_multiplier_test.go +++ b/types/weight_multiplier_test.go @@ -20,44 +20,46 @@ import ( "testing" . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" ) func TestWeightMultiplier_EncodeDecode(t *testing.T) { - assertRoundtrip(t, NewWeightMultiplier(0)) - assertRoundtrip(t, NewWeightMultiplier(12)) - assertRoundtrip(t, NewWeightMultiplier(-12)) + AssertRoundtrip(t, NewWeightMultiplier(0)) + AssertRoundtrip(t, NewWeightMultiplier(12)) + AssertRoundtrip(t, NewWeightMultiplier(-12)) } func TestWeightMultiplier_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewWeightMultiplier(-13), 8}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewWeightMultiplier(-13), 8}}) } func TestWeightMultiplier_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewWeightMultiplier(-29), MustHexDecodeString("0xe3ffffffffffffff")}, }) } func TestWeightMultiplier_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewWeightMultiplier(-29), MustHexDecodeString("0x4d42db2aa4a23bde81a3ad3705220affaa457c56a0135080c71db7783fec8f44")}, }) } func TestWeightMultiplier_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewWeightMultiplier(-29), "0xe3ffffffffffffff"}, }) } func TestWeightMultiplier_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewWeightMultiplier(-29), "-29"}, }) } func TestWeightMultiplier_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewWeightMultiplier(23), NewWeightMultiplier(23), true}, {NewWeightMultiplier(-23), NewWeightMultiplier(23), false}, {NewWeightMultiplier(23), NewU64(23), false}, diff --git a/types/weight_test.go b/types/weight_test.go index 864c05f9c..104f9de1f 100644 --- a/types/weight_test.go +++ b/types/weight_test.go @@ -19,16 +19,16 @@ package types_test import ( "testing" - "github.com/stretchr/testify/assert" - - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" + "github.com/stretchr/testify/assert" ) var ( - optionWeightFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(o *OptionWeight, c fuzz.Continue) { + optionWeightFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(o *OptionWeight, c fuzz.Continue) { if c.RandBool() { *o = NewOptionWeightEmpty() return @@ -44,12 +44,12 @@ var ( ) func TestOptionWeight_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OptionWeight](t, 100, optionWeightFuzzOpts...) - assertEncodeEmptyObj[OptionWeight](t, 1) + AssertRoundTripFuzz[OptionWeight](t, 100, optionWeightFuzzOpts...) + AssertEncodeEmptyObj[OptionWeight](t, 1) } func TestOptionWeight_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewOptionWeight(NewWeight(0)), MustHexDecodeString("0x010000000000000000")}, {NewOptionWeight(NewWeight(1)), MustHexDecodeString("0x010100000000000000")}, {NewOptionWeight(NewWeight(2)), MustHexDecodeString("0x010200000000000000")}, @@ -58,7 +58,7 @@ func TestOptionWeight_Encode(t *testing.T) { } func TestOptionWeight_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x010000000000000000"), NewOptionWeight(NewWeight(0))}, {MustHexDecodeString("0x010100000000000000"), NewOptionWeight(NewWeight(1))}, {MustHexDecodeString("0x010200000000000000"), NewOptionWeight(NewWeight(2))}, @@ -82,41 +82,41 @@ func TestOptionWeight_OptionMethods(t *testing.T) { } func TestWeight_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Weight](t, 100) - assertDecodeNilData[Weight](t) - assertEncodeEmptyObj[Weight](t, 8) + AssertRoundTripFuzz[Weight](t, 100) + AssertDecodeNilData[Weight](t) + AssertEncodeEmptyObj[Weight](t, 8) } func TestWeight_EncodedLength(t *testing.T) { - assertEncodedLength(t, []encodedLengthAssert{{NewWeight(13), 8}}) + AssertEncodedLength(t, []EncodedLengthAssert{{NewWeight(13), 8}}) } func TestWeight_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {NewWeight(29), MustHexDecodeString("0x1d00000000000000")}, }) } func TestWeight_Hash(t *testing.T) { - assertHash(t, []hashAssert{ + AssertHash(t, []HashAssert{ {NewWeight(29), MustHexDecodeString("0x83e168a13a013e6d47b0778f046aaa05d6c01d6857d044d9e9b658a6d85eb865")}, }) } func TestWeight_Hex(t *testing.T) { - assertEncodeToHex(t, []encodeToHexAssert{ + AssertEncodeToHex(t, []EncodeToHexAssert{ {NewWeight(29), "0x1d00000000000000"}, }) } func TestWeight_String(t *testing.T) { - assertString(t, []stringAssert{ + AssertString(t, []StringAssert{ {NewWeight(29), "29"}, }) } func TestWeight_Eq(t *testing.T) { - assertEq(t, []eqAssert{ + AssertEq(t, []EqAssert{ {NewWeight(23), NewWeight(23), true}, {NewWeight(23), NewBool(false), false}, }) diff --git a/types/xcm_error_test.go b/types/xcm_error_test.go index 4c1a5cb89..a6132dc58 100644 --- a/types/xcm_error_test.go +++ b/types/xcm_error_test.go @@ -19,14 +19,14 @@ package types_test import ( "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( - xcmErrorFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(x *XCMError, c fuzz.Continue) { + xcmErrorFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(x *XCMError, c fuzz.Continue) { switch c.Intn(26) { case 0: x.IsOverflow = true @@ -94,7 +94,7 @@ var ( ) func TestXCMError_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[XCMError](t, 1000, xcmErrorFuzzOpts...) - assertDecodeNilData[XCMError](t) - assertEncodeEmptyObj[XCMError](t, 0) + AssertRoundTripFuzz[XCMError](t, 1000, xcmErrorFuzzOpts...) + AssertDecodeNilData[XCMError](t) + AssertEncodeEmptyObj[XCMError](t, 0) } diff --git a/types/xcm_test.go b/types/xcm_test.go index 8c06ca6cb..a37682321 100644 --- a/types/xcm_test.go +++ b/types/xcm_test.go @@ -20,11 +20,10 @@ import ( "math/big" "testing" - fuzz "github.com/google/gofuzz" - . "github.com/centrifuge/go-substrate-rpc-client/v4/types" - - _ "github.com/google/gofuzz" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/codec" + . "github.com/centrifuge/go-substrate-rpc-client/v4/types/test_utils" + fuzz "github.com/google/gofuzz" ) var ( @@ -37,10 +36,10 @@ var ( AbstractKey: []U8{6, 7, 4}, } - assetIDFuzzOpts = combineFuzzOpts( + assetIDFuzzOpts = CombineFuzzOpts( multiLocationV1FuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(a *AssetID, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(a *AssetID, c fuzz.Continue) { if c.RandBool() { a.IsConcrete = true c.Fuzz(&a.MultiLocation) @@ -55,20 +54,20 @@ var ( ) func TestAssetID_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[AssetID](t, 100, assetIDFuzzOpts...) - assertDecodeNilData[AssetID](t) - assertEncodeEmptyObj[AssetID](t, 0) + AssertRoundTripFuzz[AssetID](t, 100, assetIDFuzzOpts...) + AssertDecodeNilData[AssetID](t) + AssertEncodeEmptyObj[AssetID](t, 0) } func TestAssetID_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testAssetID1, MustHexDecodeString("0x000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807")}, {testAssetID2, MustHexDecodeString("0x010c060704")}, }) } func TestAssetID_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807"), testAssetID1}, {MustHexDecodeString("0x010c060704"), testAssetID2}, }) @@ -103,8 +102,8 @@ var ( Blob: []U8{4, 5, 3, 2, 4, 5, 6, 2, 1}, } - assetInstanceFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(a *AssetInstance, c fuzz.Continue) { + assetInstanceFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(a *AssetInstance, c fuzz.Continue) { switch c.Intn(7) { case 0: a.IsUndefined = true @@ -138,13 +137,13 @@ var ( ) func TestAssetInstance_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[AssetInstance](t, 1000, assetInstanceFuzzOpts...) - assertDecodeNilData[AssetInstance](t) - assertEncodeEmptyObj[AssetInstance](t, 0) + AssertRoundTripFuzz[AssetInstance](t, 1000, assetInstanceFuzzOpts...) + AssertDecodeNilData[AssetInstance](t) + AssertEncodeEmptyObj[AssetInstance](t, 0) } func TestAssetInstance_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testAssetInstance1, MustHexDecodeString("0x00")}, {testAssetInstance2, MustHexDecodeString("0x017b000000000000000000000000000000")}, {testAssetInstance3, MustHexDecodeString("0x0201020304")}, @@ -156,7 +155,7 @@ func TestAssetInstance_Encode(t *testing.T) { } func TestAssetInstance_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testAssetInstance1}, {MustHexDecodeString("0x017b000000000000000000000000000000"), testAssetInstance2}, {MustHexDecodeString("0x0201020304"), testAssetInstance3}, @@ -177,10 +176,10 @@ var ( AssetInstance: testAssetInstance3, } - fungibilityFuzzOpts = combineFuzzOpts( + fungibilityFuzzOpts = CombineFuzzOpts( assetInstanceFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(f *Fungibility, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(f *Fungibility, c fuzz.Continue) { if c.RandBool() { f.IsFungible = true c.Fuzz(&f.Amount) @@ -195,20 +194,20 @@ var ( ) func TestFungibility_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Fungibility](t, 1000, fungibilityFuzzOpts...) - assertDecodeNilData[Fungibility](t) - assertEncodeEmptyObj[Fungibility](t, 0) + AssertRoundTripFuzz[Fungibility](t, 1000, fungibilityFuzzOpts...) + AssertDecodeNilData[Fungibility](t) + AssertEncodeEmptyObj[Fungibility](t, 0) } func TestFungibility_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testFungibility1, MustHexDecodeString("0x00ed01")}, {testFungibility2, MustHexDecodeString("0x010201020304")}, }) } func TestFungibility_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00ed01"), testFungibility1}, {MustHexDecodeString("0x010201020304"), testFungibility2}, }) @@ -224,17 +223,17 @@ var ( Fungibility: testFungibility2, } - multiAssetV1FuzzOpts = combineFuzzOpts(assetIDFuzzOpts, fungibilityFuzzOpts) + multiAssetV1FuzzOpts = CombineFuzzOpts(assetIDFuzzOpts, fungibilityFuzzOpts) ) func TestMultiAssetV1_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiAssetV1](t, 1000, multiAssetV1FuzzOpts...) - assertDecodeNilData[MultiAssetV1](t) - assertEncodeEmptyObj[MultiAssetV1](t, 0) + AssertRoundTripFuzz[MultiAssetV1](t, 1000, multiAssetV1FuzzOpts...) + AssertDecodeNilData[MultiAssetV1](t) + AssertEncodeEmptyObj[MultiAssetV1](t, 0) } func TestMultiAssetV1_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testMultiAssetV1, MustHexDecodeString("0x010c06070400ed01"), @@ -247,7 +246,7 @@ func TestMultiAssetV1_Encode(t *testing.T) { } func TestMultiAssetV1_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x010c06070400ed01"), testMultiAssetV1, @@ -264,12 +263,12 @@ var ( ) func TestMultiAssetsV1_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiAssetsV1](t, 100, multiAssetV1FuzzOpts...) - assertEncodeEmptyObj[MultiAssetsV1](t, 1) + AssertRoundTripFuzz[MultiAssetsV1](t, 100, multiAssetV1FuzzOpts...) + AssertEncodeEmptyObj[MultiAssetsV1](t, 1) } func TestMultiAssetsV1_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testMultiAssetsV1, MustHexDecodeString("0x08010c06070400ed01000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807010201020304"), @@ -278,7 +277,7 @@ func TestMultiAssetsV1_Encode(t *testing.T) { } func TestMultiAssetsV1_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x08010c06070400ed01000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807010201020304"), testMultiAssetsV1, @@ -336,11 +335,11 @@ var ( ConcreteNonFungibleInstance: testAssetInstance3, } - multiAssetV0FuzzOpts = combineFuzzOpts( + multiAssetV0FuzzOpts = CombineFuzzOpts( multiLocationV1FuzzOpts, assetInstanceFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(m *MultiAssetV0, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(m *MultiAssetV0, c fuzz.Continue) { switch c.Intn(12) { case 0: m.IsNone = true @@ -397,13 +396,13 @@ var ( ) func TestMultiAssetV0_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiAssetV0](t, 1000, multiAssetV0FuzzOpts...) - assertDecodeNilData[MultiAssetV0](t) - assertEncodeEmptyObj[MultiAssetV0](t, 0) + AssertRoundTripFuzz[MultiAssetV0](t, 1000, multiAssetV0FuzzOpts...) + AssertDecodeNilData[MultiAssetV0](t) + AssertEncodeEmptyObj[MultiAssetV0](t, 0) } func TestMultiAssetV0_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testMultiAssetV0n1, MustHexDecodeString("0x00")}, {testMultiAssetV0n2, MustHexDecodeString("0x01")}, {testMultiAssetV0n3, MustHexDecodeString("0x02")}, @@ -420,7 +419,7 @@ func TestMultiAssetV0_Encode(t *testing.T) { } func TestMultiAssetV0_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testMultiAssetV0n1}, {MustHexDecodeString("0x01"), testMultiAssetV0n2}, {MustHexDecodeString("0x02"), testMultiAssetV0n3}, @@ -450,11 +449,11 @@ var ( MultiAssetsV1: testMultiAssetsV1, } - versionedMultiAssetsFuzzOpts = combineFuzzOpts( + versionedMultiAssetsFuzzOpts = CombineFuzzOpts( multiAssetV0FuzzOpts, multiAssetV1FuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(v *VersionedMultiAssets, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(v *VersionedMultiAssets, c fuzz.Continue) { if c.RandBool() { v.IsV0 = true c.Fuzz(&v.MultiAssetsV0) @@ -469,13 +468,13 @@ var ( ) func TestVersionedMultiAssets_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[VersionedMultiAssets](t, 1000, versionedMultiAssetsFuzzOpts...) - assertDecodeNilData[VersionedMultiAssets](t) - assertEncodeEmptyObj[VersionedMultiAssets](t, 0) + AssertRoundTripFuzz[VersionedMultiAssets](t, 1000, versionedMultiAssetsFuzzOpts...) + AssertDecodeNilData[VersionedMultiAssets](t) + AssertEncodeEmptyObj[VersionedMultiAssets](t, 0) } func TestVersionedMultiAssets_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testVersionedMultiAssets1, MustHexDecodeString("0x000c00040c0405060a0408002c01000c010203020010000000000000000303000404052a00000000000000000000000000000006080608078e020000000000000000000000000000"), @@ -488,7 +487,7 @@ func TestVersionedMultiAssets_Encode(t *testing.T) { } func TestVersionedMultiAssets_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x000c00040c0405060a0408002c01000c010203020010000000000000000303000404052a00000000000000000000000000000006080608078e020000000000000000000000000000"), testVersionedMultiAssets1, @@ -517,11 +516,11 @@ var ( Version: NewU32(431), } - responseFuzzOpts = combineFuzzOpts( + responseFuzzOpts = CombineFuzzOpts( multiAssetV1FuzzOpts, executionResultFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(r *Response, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(r *Response, c fuzz.Continue) { switch c.Intn(4) { case 0: r.IsNull = true @@ -544,13 +543,13 @@ var ( ) func TestResponse_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Response](t, 1000, responseFuzzOpts...) - assertDecodeNilData[Response](t) - assertEncodeEmptyObj[Response](t, 0) + AssertRoundTripFuzz[Response](t, 1000, responseFuzzOpts...) + AssertDecodeNilData[Response](t) + AssertEncodeEmptyObj[Response](t, 0) } func TestResponse_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testResponse1, MustHexDecodeString("0x00")}, {testResponse2, MustHexDecodeString("0x0108010c06070400ed01000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807010201020304")}, {testResponse3, MustHexDecodeString("0x020100000000")}, @@ -559,7 +558,7 @@ func TestResponse_Encode(t *testing.T) { } func TestResponse_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testResponse1}, {MustHexDecodeString("0x0108010c06070400ed01000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807010201020304"), testResponse2}, {MustHexDecodeString("0x020100000000"), testResponse3}, @@ -581,8 +580,8 @@ var ( IsXcm: true, } - originKindFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(o *OriginKind, c fuzz.Continue) { + originKindFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(o *OriginKind, c fuzz.Continue) { switch c.Intn(4) { case 0: o.IsNative = true @@ -598,13 +597,13 @@ var ( ) func TestOriginKind_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[OriginKind](t, 1000, originKindFuzzOpts...) - assertDecodeNilData[OriginKind](t) - assertEncodeEmptyObj[OriginKind](t, 0) + AssertRoundTripFuzz[OriginKind](t, 1000, originKindFuzzOpts...) + AssertDecodeNilData[OriginKind](t) + AssertEncodeEmptyObj[OriginKind](t, 0) } func TestOriginKind_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testOriginKind1, MustHexDecodeString("0x00")}, {testOriginKind2, MustHexDecodeString("0x01")}, {testOriginKind3, MustHexDecodeString("0x02")}, @@ -613,7 +612,7 @@ func TestOriginKind_Encode(t *testing.T) { } func TestOriginKind_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testOriginKind1}, {MustHexDecodeString("0x01"), testOriginKind2}, {MustHexDecodeString("0x02"), testOriginKind3}, @@ -628,18 +627,18 @@ var ( ) func TestEncodedCall_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[EncodedCall](t, 100) - assertEncodeEmptyObj[EncodedCall](t, 1) + AssertRoundTripFuzz[EncodedCall](t, 100) + AssertEncodeEmptyObj[EncodedCall](t, 1) } func TestEncodedCall_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testEncodedCall, MustHexDecodeString("0x1005060709")}, }) } func TestEncodedCall_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x1005060709"), testEncodedCall}, }) } @@ -648,8 +647,8 @@ var ( testWildFungibility1 = WildFungibility{IsFungible: true} testWildFungibility2 = WildFungibility{IsNonFungible: true} - wildFungibilityFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(w *WildFungibility, c fuzz.Continue) { + wildFungibilityFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(w *WildFungibility, c fuzz.Continue) { if c.RandBool() { w.IsFungible = true return @@ -661,20 +660,20 @@ var ( ) func TestWildFungibility_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[WildFungibility](t, 100, wildFungibilityFuzzOpts...) - assertDecodeNilData[WildFungibility](t) - assertEncodeEmptyObj[WildFungibility](t, 0) + AssertRoundTripFuzz[WildFungibility](t, 100, wildFungibilityFuzzOpts...) + AssertDecodeNilData[WildFungibility](t) + AssertEncodeEmptyObj[WildFungibility](t, 0) } func TestWildFungibility_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testWildFungibility1, MustHexDecodeString("0x00")}, {testWildFungibility2, MustHexDecodeString("0x01")}, }) } func TestWildFungibility_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testWildFungibility1}, {MustHexDecodeString("0x01"), testWildFungibility2}, }) @@ -690,11 +689,11 @@ var ( Fun: testWildFungibility1, } - wildMultiAssetFuzzOpts = combineFuzzOpts( + wildMultiAssetFuzzOpts = CombineFuzzOpts( assetIDFuzzOpts, wildFungibilityFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(w *WildMultiAsset, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(w *WildMultiAsset, c fuzz.Continue) { if c.RandBool() { w.IsAll = true return @@ -709,20 +708,20 @@ var ( ) func TestWildMultiAsset_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[WildMultiAsset](t, 100, wildMultiAssetFuzzOpts...) - assertDecodeNilData[WildMultiAsset](t) - assertEncodeEmptyObj[WildMultiAsset](t, 0) + AssertRoundTripFuzz[WildMultiAsset](t, 100, wildMultiAssetFuzzOpts...) + AssertDecodeNilData[WildMultiAsset](t) + AssertEncodeEmptyObj[WildMultiAsset](t, 0) } func TestWildMultiAsset_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testWildMultiAsset1, MustHexDecodeString("0x00")}, {testWildMultiAsset2, MustHexDecodeString("0x01010c06070400")}, }) } func TestWildMultiAsset_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testWildMultiAsset1}, {MustHexDecodeString("0x01010c06070400"), testWildMultiAsset2}, }) @@ -737,11 +736,11 @@ var ( IsWild: true, WildMultiAsset: testWildMultiAsset1, } - multiAssetFilterFuzzOpts = combineFuzzOpts( + multiAssetFilterFuzzOpts = CombineFuzzOpts( multiAssetV1FuzzOpts, wildMultiAssetFuzzOpts, - []fuzzOpt{ - withFuzzFuncs(func(m *MultiAssetFilter, c fuzz.Continue) { + []FuzzOpt{ + WithFuzzFuncs(func(m *MultiAssetFilter, c fuzz.Continue) { if c.RandBool() { m.IsDefinite = true c.Fuzz(&m.MultiAssets) @@ -756,13 +755,13 @@ var ( ) func TestMultiAssetFilter_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[MultiAssetFilter](t, 100, multiAssetFilterFuzzOpts...) - assertDecodeNilData[MultiAssetFilter](t) - assertEncodeEmptyObj[MultiAssetFilter](t, 0) + AssertRoundTripFuzz[MultiAssetFilter](t, 100, multiAssetFilterFuzzOpts...) + AssertDecodeNilData[MultiAssetFilter](t) + AssertEncodeEmptyObj[MultiAssetFilter](t, 0) } func TestMultiAssetFilter_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ { testMultiAssetFilter1, MustHexDecodeString("0x0008010c06070400ed01000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807010201020304"), @@ -775,7 +774,7 @@ func TestMultiAssetFilter_Encode(t *testing.T) { } func TestMultiAssetFilter_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ { MustHexDecodeString("0x0008010c06070400ed01000408002c01000c010203020010000000000000000303000404052a0000000000000000000000000000000608060807010201020304"), testMultiAssetFilter1, @@ -795,8 +794,8 @@ var ( IsLimited: true, Limit: 54, } - weightLimitFuzzOpts = []fuzzOpt{ - withFuzzFuncs(func(w *WeightLimit, c fuzz.Continue) { + weightLimitFuzzOpts = []FuzzOpt{ + WithFuzzFuncs(func(w *WeightLimit, c fuzz.Continue) { if c.RandBool() { w.IsUnlimited = true return @@ -809,36 +808,36 @@ var ( ) func TestWeightLimit_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[WeightLimit](t, 100, weightLimitFuzzOpts...) - assertDecodeNilData[WeightLimit](t) - assertEncodeEmptyObj[WeightLimit](t, 0) + AssertRoundTripFuzz[WeightLimit](t, 100, weightLimitFuzzOpts...) + AssertDecodeNilData[WeightLimit](t) + AssertEncodeEmptyObj[WeightLimit](t, 0) } func TestWeightLimit_Encode(t *testing.T) { - assertEncode(t, []encodingAssert{ + AssertEncode(t, []EncodingAssert{ {testWeightLimit1, MustHexDecodeString("0x00")}, {testWeightLimit2, MustHexDecodeString("0x013600000000000000")}, }) } func TestWeightLimit_Decode(t *testing.T) { - assertDecode(t, []decodingAssert{ + AssertDecode(t, []DecodingAssert{ {MustHexDecodeString("0x00"), testWeightLimit1}, {MustHexDecodeString("0x013600000000000000"), testWeightLimit2}, }) } var ( - instructionFuzzOpts = combineFuzzOpts( + instructionFuzzOpts = CombineFuzzOpts( multiAssetV1FuzzOpts, responseFuzzOpts, multiLocationV1FuzzOpts, originKindFuzzOpts, multiAssetFilterFuzzOpts, weightLimitFuzzOpts, - []fuzzOpt{ - withNumElements(1, 3), - withFuzzFuncs(func(i *Instruction, c fuzz.Continue) { + []FuzzOpt{ + WithNumElements(1, 3), + WithFuzzFuncs(func(i *Instruction, c fuzz.Continue) { switch c.Intn(28) { case 0: i.IsWithdrawAsset = true @@ -1009,7 +1008,7 @@ var ( ) func TestInstruction_EncodeDecode(t *testing.T) { - assertRoundTripFuzz[Instruction](t, 1000, instructionFuzzOpts...) - assertDecodeNilData[Instruction](t) - assertEncodeEmptyObj[Instruction](t, 0) + AssertRoundTripFuzz[Instruction](t, 1000, instructionFuzzOpts...) + AssertDecodeNilData[Instruction](t) + AssertEncodeEmptyObj[Instruction](t, 0) }