-
Notifications
You must be signed in to change notification settings - Fork 129
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
feat: implement ext_offchain_index_set_version_1 for wasmer runtime #1739
Changes from 17 commits
c6d38ac
d88d707
99fe2c4
38c0813
cbdb7f2
53d11f5
9d5094b
caea8b0
6284063
9ddda6d
cdd0b08
8c4b89f
38c99ab
1cdf409
fcb1238
f9c9cdf
b1864dc
cc43aa9
defa63f
ee82422
b10fd88
a956531
0fc1adf
b70dc1c
8ae7638
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,19 +18,21 @@ package core | |
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
coremocks "github.com/ChainSafe/gossamer/dot/core/mocks" | ||
"github.com/ChainSafe/gossamer/dot/network" | ||
"github.com/ChainSafe/gossamer/dot/state" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/crypto/sr25519" | ||
"github.com/ChainSafe/gossamer/lib/genesis" | ||
"github.com/ChainSafe/gossamer/lib/keystore" | ||
"github.com/ChainSafe/gossamer/lib/runtime" | ||
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage" | ||
"github.com/ChainSafe/gossamer/lib/runtime/wasmer" | ||
"github.com/ChainSafe/gossamer/lib/utils" | ||
log "github.com/ChainSafe/log15" | ||
|
||
coremocks "github.com/ChainSafe/gossamer/dot/core/mocks" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
@@ -105,6 +107,13 @@ func NewTestService(t *testing.T, cfg *Config) *Service { | |
rtCfg.CodeHash, err = cfg.StorageState.LoadCodeHash(nil) | ||
require.NoError(t, err) | ||
|
||
nodeStorage := runtime.NodeStorage{} | ||
|
||
nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) | ||
require.NoError(t, err) | ||
|
||
rtCfg.NodeStorage = nodeStorage | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lgmt! just this pice of code that repeats in some places, would be nice to create a test function helper that receives a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created this:
however it seems that I could find a place to put this where it wouldn't cause a cycle import error in one of the files that used it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where did you place it? If you could not find a place to put there is no problem to keep the code as it is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried using it in core/test_helpers.go, then sync/test_helpers.go and babe/babe_test.go |
||
|
||
cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(gen, rtCfg) | ||
require.NoError(t, err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,12 @@ package sync | |
import ( | ||
"io/ioutil" | ||
"math/big" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/mock" | ||
|
||
"github.com/ChainSafe/gossamer/dot/state" | ||
syncmocks "github.com/ChainSafe/gossamer/dot/sync/mocks" | ||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/babe" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
|
@@ -34,11 +34,11 @@ import ( | |
"github.com/ChainSafe/gossamer/lib/runtime/wasmer" | ||
"github.com/ChainSafe/gossamer/lib/transaction" | ||
"github.com/ChainSafe/gossamer/lib/trie" | ||
"github.com/ChainSafe/gossamer/lib/utils" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
log "github.com/ChainSafe/log15" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
syncmocks "github.com/ChainSafe/gossamer/dot/sync/mocks" | ||
) | ||
|
||
// NewMockFinalityGadget create and return sync FinalityGadget interface mock | ||
|
@@ -100,6 +100,11 @@ func NewTestSyncer(t *testing.T, usePolkadotGenesis bool) *Service { | |
rtCfg.CodeHash, err = cfg.StorageState.LoadCodeHash(nil) | ||
require.NoError(t, err) | ||
|
||
nodeStorage := runtime.NodeStorage{} | ||
nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||
require.NoError(t, err) | ||
rtCfg.NodeStorage = nodeStorage | ||
|
||
instance, err := wasmer.NewRuntimeFromGenesis(gen, rtCfg) //nolint | ||
require.NoError(t, err) | ||
cfg.Runtime = instance | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,24 +21,25 @@ import ( | |
"io/ioutil" | ||
"math/big" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ChainSafe/gossamer/dot/core" | ||
"github.com/ChainSafe/gossamer/dot/state" | ||
"github.com/ChainSafe/gossamer/dot/types" | ||
"github.com/ChainSafe/gossamer/lib/babe/mocks" | ||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/lib/crypto/sr25519" | ||
"github.com/ChainSafe/gossamer/lib/genesis" | ||
"github.com/ChainSafe/gossamer/lib/runtime" | ||
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage" | ||
"github.com/ChainSafe/gossamer/lib/runtime/wasmer" | ||
"github.com/ChainSafe/gossamer/lib/trie" | ||
"github.com/ChainSafe/gossamer/lib/utils" | ||
log "github.com/ChainSafe/log15" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ChainSafe/gossamer/lib/babe/mocks" | ||
mock "github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
|
@@ -98,8 +99,9 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { | |
cfg.TransactionState = state.NewTransactionState() | ||
} | ||
|
||
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*") //nolint | ||
|
||
if cfg.BlockState == nil || cfg.StorageState == nil || cfg.EpochState == nil { | ||
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*") //nolint | ||
require.NoError(t, err) | ||
|
||
config := state.Config{ | ||
|
@@ -138,6 +140,11 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { | |
rtCfg.CodeHash, err = storageState.LoadCodeHash(nil) | ||
require.NoError(t, err) | ||
|
||
nodeStorage := runtime.NodeStorage{} | ||
nodeStorage.BaseDB, err = utils.SetupDatabase(filepath.Join(testDatadirPath, "offline_storage"), false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update. |
||
require.NoError(t, err) | ||
rtCfg.NodeStorage = nodeStorage | ||
|
||
cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(gen, rtCfg) | ||
require.NoError(t, err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,7 +133,7 @@ import ( | |
) | ||
|
||
//export ext_logging_log_version_1 | ||
func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetData C.int64_t, msgData C.int64_t) { | ||
func ext_logging_log_version_1(context unsafe.Pointer, level C.int32_t, targetData, msgData C.int64_t) { | ||
logger.Trace("[ext_logging_log_version_1] executing...") | ||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
||
|
@@ -319,7 +319,7 @@ func ext_crypto_ed25519_public_keys_version_1(context unsafe.Pointer, keyTypeID | |
} | ||
|
||
//export ext_crypto_ed25519_sign_version_1 | ||
func ext_crypto_ed25519_sign_version_1(context unsafe.Pointer, keyTypeID C.int32_t, key C.int32_t, msg C.int64_t) C.int64_t { | ||
func ext_crypto_ed25519_sign_version_1(context unsafe.Pointer, keyTypeID, key C.int32_t, msg C.int64_t) C.int64_t { | ||
logger.Debug("[ext_crypto_ed25519_sign_version_1] executing...") | ||
|
||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
@@ -872,7 +872,7 @@ func ext_misc_print_hex_version_1(context unsafe.Pointer, dataSpan C.int64_t) { | |
} | ||
|
||
//export ext_misc_print_num_version_1 | ||
func ext_misc_print_num_version_1(context unsafe.Pointer, data C.int64_t) { | ||
func ext_misc_print_num_version_1(_ unsafe.Pointer, data C.int64_t) { | ||
logger.Trace("[ext_misc_print_num_version_1] executing...") | ||
|
||
logger.Debug("[ext_misc_print_num_version_1]", "num", fmt.Sprintf("%d", int64(data))) | ||
|
@@ -932,7 +932,7 @@ func ext_misc_runtime_version_version_1(context unsafe.Pointer, dataSpan C.int64 | |
} | ||
|
||
//export ext_default_child_storage_read_version_1 | ||
func ext_default_child_storage_read_version_1(context unsafe.Pointer, childStorageKey C.int64_t, key C.int64_t, valueOut C.int64_t, offset C.int32_t) C.int64_t { | ||
func ext_default_child_storage_read_version_1(context unsafe.Pointer, childStorageKey, key, valueOut C.int64_t, offset C.int32_t) C.int64_t { | ||
logger.Debug("[ext_default_child_storage_read_version_1] executing...") | ||
|
||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
@@ -979,7 +979,7 @@ func ext_default_child_storage_clear_version_1(context unsafe.Pointer, childStor | |
} | ||
|
||
//export ext_default_child_storage_clear_prefix_version_1 | ||
func ext_default_child_storage_clear_prefix_version_1(context unsafe.Pointer, childStorageKey C.int64_t, prefixSpan C.int64_t) { | ||
func ext_default_child_storage_clear_prefix_version_1(context unsafe.Pointer, childStorageKey, prefixSpan C.int64_t) { | ||
logger.Debug("[ext_default_child_storage_clear_prefix_version_1] executing...") | ||
|
||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
@@ -996,7 +996,7 @@ func ext_default_child_storage_clear_prefix_version_1(context unsafe.Pointer, ch | |
} | ||
|
||
//export ext_default_child_storage_exists_version_1 | ||
func ext_default_child_storage_exists_version_1(context unsafe.Pointer, childStorageKey C.int64_t, key C.int64_t) C.int32_t { | ||
func ext_default_child_storage_exists_version_1(context unsafe.Pointer, childStorageKey, key C.int64_t) C.int32_t { | ||
logger.Debug("[ext_default_child_storage_exists_version_1] executing...") | ||
|
||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
@@ -1036,7 +1036,7 @@ func ext_default_child_storage_get_version_1(context unsafe.Pointer, childStorag | |
} | ||
|
||
//export ext_default_child_storage_next_key_version_1 | ||
func ext_default_child_storage_next_key_version_1(context unsafe.Pointer, childStorageKey C.int64_t, key C.int64_t) C.int64_t { | ||
func ext_default_child_storage_next_key_version_1(context unsafe.Pointer, childStorageKey, key C.int64_t) C.int64_t { | ||
logger.Debug("[ext_default_child_storage_next_key_version_1] executing...") | ||
|
||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
@@ -1348,7 +1348,18 @@ func ext_hashing_twox_64_version_1(context unsafe.Pointer, dataSpan C.int64_t) C | |
//export ext_offchain_index_set_version_1 | ||
func ext_offchain_index_set_version_1(context unsafe.Pointer, keySpan, valueSpan C.int64_t) { | ||
logger.Trace("[ext_offchain_index_set_version_1] executing...") | ||
logger.Warn("[ext_offchain_index_set_version_1] unimplemented") | ||
instanceContext := wasm.IntoInstanceContext(context) | ||
runtimeCtx := instanceContext.Data().(*runtime.Context) | ||
|
||
storageKey := asMemorySlice(instanceContext, keySpan) | ||
newValue := asMemorySlice(instanceContext, valueSpan) | ||
cp := make([]byte, len(newValue)) | ||
copy(cp, newValue) | ||
|
||
err := runtimeCtx.NodeStorage.BaseDB.Put(storageKey, cp) | ||
if err != nil { | ||
logger.Error("[ext_offchain_index_set_version_1] failed to set value in raw storage", "error", err) | ||
} | ||
} | ||
|
||
//export ext_offchain_local_storage_clear_version_1 | ||
|
@@ -1580,9 +1591,9 @@ func storageAppend(storage runtime.Storage, key, valueToAppend []byte) error { | |
} | ||
|
||
// append new length prefix to start of items array | ||
finalVal := append(lengthEnc, valueRes...) | ||
logger.Debug("[ext_storage_append_version_1]", "resulting value", fmt.Sprintf("0x%x", finalVal)) | ||
storage.Set(key, finalVal) | ||
lengthEnc = append(lengthEnc, valueRes...) | ||
logger.Debug("[ext_storage_append_version_1]", "resulting value", fmt.Sprintf("0x%x", lengthEnc)) | ||
storage.Set(key, lengthEnc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this updated? I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was updated because DeepSource was complaining that append was appending to a variable with a different name. Yes, finalVal is more descriptive, I've update to use that instead. |
||
return nil | ||
} | ||
|
||
|
@@ -1794,7 +1805,7 @@ func ext_storage_root_version_1(context unsafe.Pointer) C.int64_t { | |
} | ||
|
||
//export ext_storage_set_version_1 | ||
func ext_storage_set_version_1(context unsafe.Pointer, keySpan C.int64_t, valueSpan C.int64_t) { | ||
func ext_storage_set_version_1(context unsafe.Pointer, keySpan, valueSpan C.int64_t) { | ||
logger.Trace("[ext_storage_set_version_1] executing...") | ||
|
||
instanceContext := wasm.IntoInstanceContext(context) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -409,6 +409,27 @@ func Test_ext_storage_set_version_1(t *testing.T) { | |
require.Equal(t, testvalue, val) | ||
} | ||
|
||
func Test_ext_offline_index_set_version_1(t *testing.T) { | ||
// TODO this currently fails with error could nat find exported function, determine how else to test this | ||
t.Skip() | ||
inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if change from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @EclesioMeloJunior the |
||
|
||
testkey := []byte("noot") | ||
testvalue := []byte("washere") | ||
|
||
encKey, err := scale.Encode(testkey) | ||
require.NoError(t, err) | ||
encValue, err := scale.Encode(testvalue) | ||
require.NoError(t, err) | ||
|
||
_, err = inst.Exec("rtm_ext_offline_index_set_version_1", append(encKey, encValue...)) | ||
require.NoError(t, err) | ||
|
||
val, err := inst.ctx.NodeStorage.PersistentStorage.Get(testkey) | ||
require.NoError(t, err) | ||
require.Equal(t, testvalue, val) | ||
} | ||
|
||
func Test_ext_crypto_ed25519_generate_version_1(t *testing.T) { | ||
inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be preferred to use
stateSrvc.BaseDB
here ifstateSrvc != nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.