Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offchain RPC methods should not SCALE-encode keys & values #171

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}