Skip to content

Commit

Permalink
Offchain RPC methods should not SCALE-encode keys & values (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes authored Jun 22, 2021
1 parent db7f2af commit 4fec3b3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
19 changes: 2 additions & 17 deletions rpc/offchain/get_local_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions rpc/offchain/get_local_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/rand"
"testing"

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

Expand All @@ -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))
}

0 comments on commit 4fec3b3

Please sign in to comment.