From 4cbbd5231cca412802c1cdeed0075023634651b1 Mon Sep 17 00:00:00 2001 From: Vincent Geddes Date: Tue, 22 Jun 2021 18:25:51 +0200 Subject: [PATCH] Offchain RPC methods should not SCALE-encode keys & values (#171) --- rpc/offchain/get_local_storage.go | 19 ++----------------- rpc/offchain/get_local_storage_test.go | 10 ++++------ 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/rpc/offchain/get_local_storage.go b/rpc/offchain/get_local_storage.go index 446f6624f..6577e932f 100644 --- a/rpc/offchain/get_local_storage.go +++ b/rpc/offchain/get_local_storage.go @@ -20,12 +20,7 @@ const ( func (c *Offchain) LocalStorageGet(kind StorageKind, key []byte) (*types.StorageDataRaw, error) { var res string - kb, err := types.EncodeToHexString(key) - if err != nil { - return nil, fmt.Errorf("failed to encode key: %w", err) - } - - err = c.client.Call(&res, "offchain_localStorageGet", kind, kb) + err := c.client.Call(&res, "offchain_localStorageGet", kind, fmt.Sprintf("%#x", key)) if err != nil { return nil, err } @@ -47,17 +42,7 @@ func (c *Offchain) LocalStorageGet(kind StorageKind, key []byte) (*types.Storage func (c *Offchain) LocalStorageSet(kind StorageKind, key []byte, value []byte) error { var res string - kb, err := types.EncodeToHexString(key) - if err != nil { - return fmt.Errorf("failed to encode key: %w", err) - } - - vb, err := types.EncodeToHexString(value) - if err != nil { - return fmt.Errorf("failed to encode value: %w", err) - } - - err = c.client.Call(&res, "offchain_localStorageSet", kind, kb, vb) + err := c.client.Call(&res, "offchain_localStorageSet", kind, fmt.Sprintf("%#x", key), fmt.Sprintf("%#x", value)) if err != nil { return err } diff --git a/rpc/offchain/get_local_storage_test.go b/rpc/offchain/get_local_storage_test.go index 569eaeb01..f9d03017e 100644 --- a/rpc/offchain/get_local_storage_test.go +++ b/rpc/offchain/get_local_storage_test.go @@ -4,7 +4,6 @@ import ( "crypto/rand" "testing" - "github.com/Phala-Network/go-substrate-rpc-client/v3/types" "github.com/stretchr/testify/assert" ) @@ -14,18 +13,17 @@ func TestOffchain_LocalStorageGetSet(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 20, n) + value := []byte{0, 1, 2} + data, err := offchain.LocalStorageGet(Persistent, key) assert.NoError(t, err) assert.Empty(t, data) - err = offchain.LocalStorageSet(Persistent, key, key) + err = offchain.LocalStorageSet(Persistent, key, value) assert.NoError(t, err) data, err = offchain.LocalStorageGet(Persistent, key) assert.NoError(t, err) - got := make([]byte, 0, 20) - err = types.DecodeFromHexString(data.Hex(), &got) - assert.NoError(t, err) - assert.Equal(t, key, got) + assert.Equal(t, value, []byte(*data)) }