diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index fbb5b36433..4d9f1bdb8a 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -33,7 +33,6 @@ import ( "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/runtime/life" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" - "github.com/ChainSafe/gossamer/lib/runtime/wasmtime" "github.com/ChainSafe/gossamer/lib/utils" log "github.com/ChainSafe/log15" @@ -597,8 +596,6 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC switch tomlCfg.WasmInterpreter { case wasmer.Name: cfg.WasmInterpreter = wasmer.Name - case wasmtime.Name: - cfg.WasmInterpreter = wasmtime.Name case life.Name: cfg.WasmInterpreter = life.Name case "": diff --git a/docs/docs/getting-started/overview/package-library.md b/docs/docs/getting-started/overview/package-library.md index e1e0c421ae..b2c83e84ad 100644 --- a/docs/docs/getting-started/overview/package-library.md +++ b/docs/docs/getting-started/overview/package-library.md @@ -92,7 +92,7 @@ Gossamer packages can be categorized into **four package types**: #### `lib/runtime` -- the **runtime package** contains various wasm interpreters used to interpret the runtime. It currently contains `life`, `wasmer`, and `wasmtime`; however, `wasmer` is the only interpreter that is fully supported at the moment. In the future, all interpreters will be fully supported. +- the **runtime package** contains various wasm interpreters used to interpret the runtime. It currently contains `life` and `wasmer`; however, `wasmer` is the only interpreter that is fully supported at the moment. In the future, all interpreters will be fully supported. #### `lib/scale` diff --git a/dot/services.go b/dot/services.go index 72dd8af42f..0895c8ec10 100644 --- a/dot/services.go +++ b/dot/services.go @@ -41,7 +41,6 @@ import ( "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/runtime/life" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" - "github.com/ChainSafe/gossamer/lib/runtime/wasmtime" "github.com/ChainSafe/gossamer/lib/utils" ) @@ -152,23 +151,6 @@ func createRuntime(cfg *Config, ns runtime.NodeStorage, st *state.Service, ks *k if err != nil { return nil, fmt.Errorf("failed to create runtime executor: %s", err) } - case wasmtime.Name: - rtCfg := &wasmtime.Config{ - Imports: wasmtime.ImportNodeRuntime, - } - rtCfg.Storage = ts - rtCfg.Keystore = ks - rtCfg.LogLvl = cfg.Log.RuntimeLvl - rtCfg.NodeStorage = ns - rtCfg.Network = net - rtCfg.Role = cfg.Core.Roles - rtCfg.CodeHash = codeHash - - // create runtime executor - rt, err = wasmtime.NewInstance(code, rtCfg) - if err != nil { - return nil, fmt.Errorf("failed to create runtime executor: %s", err) - } case life.Name: rtCfg := &life.Config{ Resolver: new(life.Resolver), diff --git a/go.mod b/go.mod index 0af0e48e85..26fa9c6c6c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/ChainSafe/log15 v1.0.0 github.com/OneOfOne/xxhash v1.2.5 github.com/btcsuite/btcutil v1.0.2 - github.com/bytecodealliance/wasmtime-go v0.20.0 github.com/centrifuge/go-substrate-rpc-client/v3 v3.0.0 github.com/chyeh/pubip v0.0.0-20170203095919-b7e679cf541c github.com/cosmos/go-bip39 v1.0.0 diff --git a/go.sum b/go.sum index e91f3fc75f..724b531e3e 100644 --- a/go.sum +++ b/go.sum @@ -113,8 +113,6 @@ github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= -github.com/bytecodealliance/wasmtime-go v0.20.0 h1:omD0VY6IwL60E8YMM8rgcmyGT8O+pqabHAwoPi5il6k= -github.com/bytecodealliance/wasmtime-go v0.20.0/go.mod h1:q320gUxqyI8yB+ZqRuaJOEnGkAnHh6WtJjMaT2CW4wI= github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/centrifuge/go-substrate-rpc-client/v3 v3.0.0 h1:nwmK0bGIN67YBkqW/nAhgyhkCOv1nrFS3otY0qoQFeE= diff --git a/lib/runtime/wasmtime/exports.go b/lib/runtime/wasmtime/exports.go deleted file mode 100644 index 1089436327..0000000000 --- a/lib/runtime/wasmtime/exports.go +++ /dev/null @@ -1,165 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package wasmtime - -import ( - "fmt" - "io" - - "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/runtime" - "github.com/ChainSafe/gossamer/lib/transaction" - "github.com/ChainSafe/gossamer/pkg/scale" -) - -// Metadata calls runtime function Metadata_metadata -func (in *Instance) Metadata() ([]byte, error) { - return in.exec(runtime.Metadata, []byte{}) -} - -// Version calls runtime function Core_Version -func (in *Instance) Version() (runtime.Version, error) { - res, err := in.exec(runtime.CoreVersion, []byte{}) - if err != nil { - return nil, err - } - - version := new(runtime.VersionData) - if err == io.EOF { - // TODO: kusama seems to use the legacy version format - lversion := &runtime.LegacyVersionData{} - err = lversion.Decode(res) - return lversion, err - } else if err != nil { - return nil, err - } - - return version, nil -} - -// BabeConfiguration gets the configuration data for BABE from the runtime -func (in *Instance) BabeConfiguration() (*types.BabeConfiguration, error) { - ret, err := in.exec(runtime.BabeAPIConfiguration, []byte{}) - if err != nil { - return nil, err - } - - var cfg types.BabeConfiguration - err = scale.Unmarshal(ret, &cfg) - if err != nil { - return nil, err - } - - return &cfg, nil -} - -// GrandpaAuthorities returns the genesis authorities from the runtime -func (in *Instance) GrandpaAuthorities() ([]types.Authority, error) { - ret, err := in.exec(runtime.GrandpaAuthorities, []byte{}) - if err != nil { - return nil, err - } - var gar []types.GrandpaAuthoritiesRaw - err = scale.Unmarshal(ret, &gar) - if err != nil { - return nil, err - } - - return types.GrandpaAuthoritiesRawToAuthorities(gar) -} - -// ValidateTransaction runs the extrinsic through runtime function TaggedTransactionQueue_validate_transaction and returns *Validity -func (in *Instance) ValidateTransaction(e types.Extrinsic) (*transaction.Validity, error) { - ret, err := in.exec(runtime.TaggedTransactionQueueValidateTransaction, e) - if err != nil { - return nil, err - } - - if ret[0] != 0 { - return nil, runtime.NewValidateTransactionError(ret) - } - - v := transaction.NewValidity(0, [][]byte{{}}, [][]byte{{}}, 0, false) - err = scale.Unmarshal(ret[1:], v) - - return v, err -} - -//nolint -// InitializeBlock calls runtime API function Core_initialize_block -func (in *Instance) InitializeBlock(header *types.Header) error { - encodedHeader, err := scale.Marshal(*header) - if err != nil { - return fmt.Errorf("cannot encode header: %w", err) - } - - _, err = in.exec(runtime.CoreInitializeBlock, encodedHeader) - return err -} - -// InherentExtrinsics calls runtime API function BlockBuilder_inherent_extrinsics -func (in *Instance) InherentExtrinsics(data []byte) ([]byte, error) { - return in.exec(runtime.BlockBuilderInherentExtrinsics, data) -} - -// ApplyExtrinsic calls runtime API function BlockBuilder_apply_extrinsic -func (in *Instance) ApplyExtrinsic(data types.Extrinsic) ([]byte, error) { - return in.exec(runtime.BlockBuilderApplyExtrinsic, data) -} - -//nolint -// FinalizeBlock calls runtime API function BlockBuilder_finalize_block -func (in *Instance) FinalizeBlock() (*types.Header, error) { - data, err := in.exec(runtime.BlockBuilderFinalizeBlock, []byte{}) - if err != nil { - return nil, err - } - - bh := types.NewEmptyHeader() - err = scale.Unmarshal(data, bh) - if err != nil { - return nil, err - } - - return bh, nil -} - -// ExecuteBlock calls runtime function Core_execute_block -func (in *Instance) ExecuteBlock(block *types.Block) ([]byte, error) { - b, err := block.DeepCopy() - if err != nil { - return nil, err - } - b.Header.Digest = types.NewDigest() - - bdEnc, err := b.Encode() - if err != nil { - return nil, err - } - - return in.Exec(runtime.CoreExecuteBlock, bdEnc) -} - -// DecodeSessionKeys decodes the given public session keys. Returns a list of raw public keys including their key type. -func (in *Instance) DecodeSessionKeys(enc []byte) ([]byte, error) { - return in.exec(runtime.DecodeSessionKeys, enc) -} - -func (in *Instance) CheckInherents() {} //nolint -func (in *Instance) RandomSeed() {} //nolint -func (in *Instance) OffchainWorker() {} //nolint -func (in *Instance) GenerateSessionKeys() {} //nolint diff --git a/lib/runtime/wasmtime/exports_test.go b/lib/runtime/wasmtime/exports_test.go deleted file mode 100644 index 889df1eeae..0000000000 --- a/lib/runtime/wasmtime/exports_test.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package wasmtime - -import ( - "os" - "testing" - - "github.com/ChainSafe/gossamer/lib/runtime" - - log "github.com/ChainSafe/log15" - "github.com/stretchr/testify/require" -) - -func TestMain(m *testing.M) { - wasmFilePaths, err := runtime.GenerateRuntimeWasmFile() - if err != nil { - log.Error("failed to generate runtime wasm file", err) - os.Exit(1) - } - - // Start all tests - code := m.Run() - - runtime.RemoveFiles(wasmFilePaths) - os.Exit(code) -} - -func TestConcurrentRuntimeCalls(t *testing.T) { - instance := NewTestInstance(t, runtime.NODE_RUNTIME) - - // execute 2 concurrent calls to the runtime - go func() { - _, _ = instance.exec(runtime.CoreVersion, []byte{}) - }() - go func() { - _, _ = instance.exec(runtime.CoreVersion, []byte{}) - }() -} - -func TestInstance_Version_NodeRuntime(t *testing.T) { - t.Skip() // TODO: currently fails, returns all 0 - - expected := runtime.NewVersionData( - []byte("node"), - []byte("substrate-node"), - 10, - 260, - 0, - nil, - 1, - ) - - instance := NewTestInstance(t, runtime.NODE_RUNTIME) - - version, err := instance.Version() - require.Nil(t, err) - - t.Logf("SpecName: %s\n", version.SpecName()) - t.Logf("ImplName: %s\n", version.ImplName()) - t.Logf("AuthoringVersion: %d\n", version.AuthoringVersion()) - t.Logf("SpecVersion: %d\n", version.SpecVersion()) - t.Logf("ImplVersion: %d\n", version.ImplVersion()) - t.Logf("TransactionVersion: %d\n", version.TransactionVersion()) - - require.Equal(t, 12, len(version.APIItems())) - require.Equal(t, expected.SpecName(), version.SpecName()) - require.Equal(t, expected.ImplName(), version.ImplName()) - require.Equal(t, expected.AuthoringVersion(), version.AuthoringVersion()) - require.Equal(t, expected.SpecVersion(), version.SpecVersion()) - require.Equal(t, expected.ImplVersion(), version.ImplVersion()) - require.Equal(t, expected.TransactionVersion(), version.TransactionVersion()) -} diff --git a/lib/runtime/wasmtime/imports.go b/lib/runtime/wasmtime/imports.go deleted file mode 100644 index 1df12d48dd..0000000000 --- a/lib/runtime/wasmtime/imports.go +++ /dev/null @@ -1,395 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package wasmtime - -import ( - "fmt" - - "github.com/bytecodealliance/wasmtime-go" -) - -// int64ToPointerAndSize converts an int64 into a int32 pointer and a int32 length -func int64ToPointerAndSize(in int64) (ptr, length int32) { - return int32(in), int32(in >> 32) -} - -// Convert 64bit wasm span descriptor to Go memory slice -func asMemorySlice(memory []byte, span int64) []byte { - ptr, size := int64ToPointerAndSize(span) - return memory[ptr : ptr+size] -} - -func ext_logging_log_version_1(c *wasmtime.Caller, level int32, target, msg int64) { - logger.Trace("[ext_logging_log_version_1] executing...") -} - -func ext_sandbox_instance_teardown_version_1(c *wasmtime.Caller, a int32) { - logger.Trace("[ext_sandbox_instance_teardown_version_1] executing...") -} - -func ext_sandbox_instantiate_version_1(c *wasmtime.Caller, a int32, x, y int64, z int32) int32 { - logger.Trace("[ext_sandbox_instantiate_version_1] executing...") - return 0 -} - -func ext_sandbox_invoke_version_1(c *wasmtime.Caller, a int32, x, y int64, z, d, e int32) int32 { - logger.Trace("[ext_sandbox_invoke_version_1] executing...") - return 0 -} - -func ext_sandbox_memory_get_version_1(c *wasmtime.Caller, a, z, d, e int32) int32 { - logger.Trace("[ext_sandbox_memory_get_version_1] executing...") - return 0 -} - -func ext_sandbox_memory_new_version_1(c *wasmtime.Caller, a, z int32) int32 { - logger.Trace("[ext_sandbox_memory_new_version_1] executing...") - return 0 -} - -func ext_sandbox_memory_set_version_1(c *wasmtime.Caller, a, z, d, e int32) int32 { - logger.Trace("[ext_sandbox_memory_set_version_1] executing...") - return 0 -} - -func ext_sandbox_memory_teardown_version_1(c *wasmtime.Caller, a int32) { - logger.Trace("[ext_sandbox_memory_teardown_version_1] executing...") -} - -func ext_crypto_ed25519_generate_version_1(c *wasmtime.Caller, a int32, z int64) int32 { - logger.Trace("[ext_crypto_ed25519_generate_version_1] executing...") - return 0 -} - -func ext_crypto_ed25519_verify_version_1(c *wasmtime.Caller, a int32, z int64, y int32) int32 { - logger.Trace("[ext_crypto_ed25519_verify_version_1] executing...") - return 0 -} - -func ext_crypto_finish_batch_verify_version_1(c *wasmtime.Caller) int32 { - logger.Trace("[ext_crypto_finish_batch_verify_version_1] executing...") - return 0 -} - -func ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(c *wasmtime.Caller, a, z int32) int64 { - logger.Trace("[ext_crypto_secp256k1_ecdsa_recover_compressed_version_1] executing...") - return 0 -} - -func ext_crypto_sr25519_generate_version_1(c *wasmtime.Caller, a int32, z int64) int32 { - logger.Trace("[ext_crypto_sr25519_generate_version_1] executing...") - return 0 -} - -func ext_crypto_sr25519_public_keys_version_1(c *wasmtime.Caller, a int32) int64 { - logger.Trace("[ext_crypto_sr25519_public_keys_version_1] executing...") - return 0 -} - -func ext_crypto_sr25519_sign_version_1(c *wasmtime.Caller, a, z int32, y int64) int64 { - logger.Trace("[ext_crypto_sr25519_sign_version_1] executing...") - return 0 -} - -func ext_crypto_sr25519_verify_version_2(c *wasmtime.Caller, a int32, z int64, y int32) int32 { - logger.Trace("[ext_crypto_sr25519_verify_version_2] executing...") - return 0 -} - -func ext_crypto_start_batch_verify_version_1(c *wasmtime.Caller) { - logger.Trace("[ext_crypto_start_batch_verify_version_1] executing...") -} - -func ext_trie_blake2_256_ordered_root_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_trie_blake2_256_ordered_root_version_1] executing...") - return 0 -} - -func ext_misc_print_hex_version_1(c *wasmtime.Caller, a int64) { - logger.Trace("[ext_misc_print_hex_version_1] executing...") -} - -func ext_misc_print_num_version_1(c *wasmtime.Caller, data int64) { - logger.Trace("[ext_misc_print_num_version_1] executing...") - logger.Info("[ext_print_num]", "message", fmt.Sprintf("%d", data)) -} - -func ext_misc_print_utf8_version_1(c *wasmtime.Caller, dataSpan int64) { - logger.Trace("[ext_misc_print_utf8_version_1] executing...") - m := c.GetExport("memory").Memory() - data := asMemorySlice(m.UnsafeData(), dataSpan) - logger.Info("[ext_print_utf8]", "message", data) -} - -func ext_misc_runtime_version_version_1(c *wasmtime.Caller, z int64) int64 { - logger.Trace("[ext_misc_runtime_version_version_1] executing...") - return 0 -} - -func ext_default_child_storage_clear_version_1(c *wasmtime.Caller, a, b int64) { - logger.Trace("[ext_default_child_storage_clear_version_1] executing...") -} - -func ext_default_child_storage_get_version_1(c *wasmtime.Caller, a, b int64) int64 { - logger.Trace("[ext_default_child_storage_get_version_1] executing...") - return 0 -} - -func ext_default_child_storage_read_version_1(c *wasmtime.Caller, childStorageKey, key, valueOut int64, offset int32) int64 { - logger.Trace("[ext_default_child_storage_read_version_1] executing...") - return 0 -} - -func ext_default_child_storage_root_version_1(c *wasmtime.Caller, z int64) int64 { - logger.Trace("[ext_default_child_storage_root_version_1] executing...") - return 0 -} - -func ext_default_child_storage_set_version_1(c *wasmtime.Caller, a, b, z int64) { - logger.Trace("[ext_default_child_storage_set_version_1] executing...") -} - -func ext_default_child_storage_storage_kill_version_1(c *wasmtime.Caller, a int64) { - logger.Trace("[ext_default_child_storage_storage_kill_version_1] executing...") -} - -func ext_default_child_storage_storage_kill_version_2(c *wasmtime.Caller, a, b int64) int32 { - logger.Trace("[ext_default_child_storage_storage_kill_version_2] executing...") - return 0 -} - -func ext_allocator_free_version_1(c *wasmtime.Caller, addr int32) { - logger.Trace("[ext_allocator_free_version_1] executing...") - err := ctx.Allocator.Deallocate(uint32(addr)) - if err != nil { - logger.Error("[ext_free]", "error", err) - } -} - -func ext_allocator_malloc_version_1(c *wasmtime.Caller, size int32) int32 { - logger.Trace("[ext_allocator_malloc_version_1] executing...") - res, err := ctx.Allocator.Allocate(uint32(size)) - if err != nil { - logger.Error("[ext_malloc]", "Error:", err) - } - return int32(res) -} - -func ext_hashing_blake2_128_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_hashing_blake2_128_version_1] executing...") - return 0 -} - -func ext_hashing_blake2_256_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_hashing_blake2_256_version_1] executing...") - return 0 -} - -func ext_hashing_keccak_256_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_hashing_keccak_256_version_1] executing...") - return 0 -} - -func ext_hashing_sha2_256_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_hashing_sha2_256_version_1] executing...") - return 0 -} - -func ext_hashing_twox_128_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_hashing_twox_128_version_1] executing...") - return 0 -} - -func ext_hashing_twox_64_version_1(c *wasmtime.Caller, z int64) int32 { - logger.Trace("[ext_hashing_twox_64_version_1] executing...") - return 0 -} - -func ext_offchain_is_validator_version_1(c *wasmtime.Caller) int32 { - logger.Trace("[ext_offchain_is_validator_version_1] executing...") - return 0 -} - -func ext_offchain_local_storage_compare_and_set_version_1(c *wasmtime.Caller, a int32, x, y, z int64) int32 { - logger.Trace("[ext_offchain_local_storage_compare_and_set_version_1] executing...") - return 0 -} - -func ext_offchain_local_storage_get_version_1(c *wasmtime.Caller, a int32, x int64) int64 { - logger.Trace("[ext_offchain_local_storage_get_version_1] executing...") - return 0 -} - -func ext_offchain_local_storage_set_version_1(c *wasmtime.Caller, a int32, x, y int64) { - logger.Trace("[ext_offchain_local_storage_set_version_1] executing...") -} - -func ext_offchain_network_state_version_1(c *wasmtime.Caller) int64 { - logger.Trace("[ext_offchain_network_state_version_1] executing...") - return 0 -} - -func ext_offchain_random_seed_version_1(c *wasmtime.Caller) int32 { - logger.Trace("[ext_offchain_random_seed_version_1] executing...") - return 0 -} - -func ext_offchain_submit_transaction_version_1(c *wasmtime.Caller, z int64) int64 { - logger.Trace("[ext_offchain_submit_transaction_version_1] executing...") - return 0 -} - -func ext_storage_append_version_1(c *wasmtime.Caller, a, b int64) { - logger.Trace("[ext_storage_append_version_1] executing...") -} - -func ext_storage_changes_root_version_1(c *wasmtime.Caller, z int64) int64 { - logger.Trace("[ext_storage_changes_root_version_1] executing...") - return 0 -} - -func ext_storage_clear_version_1(c *wasmtime.Caller, a int64) { - logger.Trace("[ext_storage_clear_version_1] executing...") -} - -func ext_storage_clear_prefix_version_1(c *wasmtime.Caller, a int64) { - logger.Trace("[ext_storage_clear_prefix_version_1] executing...") -} - -func ext_storage_commit_transaction_version_1(c *wasmtime.Caller) { - logger.Trace("[ext_storage_commit_transaction_version_1] executing...") -} - -func ext_storage_exists_version_1(c *wasmtime.Caller, keySpan int64) int32 { - logger.Trace("[ext_storage_exists_version_1] executing...") - return 0 -} - -func ext_storage_get_version_1(c *wasmtime.Caller, z int64) int64 { - logger.Trace("[ext_storage_get_version_1] executing...") - return 0 -} - -func ext_storage_next_key_version_1(c *wasmtime.Caller, z int64) int64 { - logger.Trace("[ext_storage_next_key_version_1] executing...") - return 0 -} - -func ext_storage_read_version_1(c *wasmtime.Caller, a, b int64, x int32) int64 { - logger.Trace("[ext_storage_read_version_1] executing...") - return 0 -} - -func ext_storage_rollback_transaction_version_1(c *wasmtime.Caller) { - logger.Trace("[ext_storage_rollback_transaction_version_1] executing...") -} - -func ext_storage_root_version_1(c *wasmtime.Caller) int64 { - logger.Trace("[ext_storage_root_version_1] executing...") - return 0 -} - -func ext_storage_set_version_1(c *wasmtime.Caller, a, b int64) { - logger.Trace("[ext_storage_set_version_1] executing...") -} - -func ext_storage_start_transaction_version_1(c *wasmtime.Caller) { - logger.Trace("[ext_storage_start_transaction_version_1] executing...") -} - -func ext_offchain_index_set_version_1(c *wasmtime.Caller, a, b int64) { - logger.Trace("[ext_offchain_index_set_version_1] executing...") -} - -// ImportNodeRuntime adds the imports for the v0.8 runtime to linker -func ImportNodeRuntime(store *wasmtime.Store, memory *wasmtime.Memory) (*wasmtime.Linker, error) { - fns := []struct { - name string - fn interface{} - }{ - {"ext_logging_log_version_1", ext_logging_log_version_1}, - {"ext_sandbox_instance_teardown_version_1", ext_sandbox_instance_teardown_version_1}, - {"ext_sandbox_instantiate_version_1", ext_sandbox_instantiate_version_1}, - {"ext_sandbox_invoke_version_1", ext_sandbox_invoke_version_1}, - {"ext_sandbox_memory_get_version_1", ext_sandbox_memory_get_version_1}, - {"ext_sandbox_memory_new_version_1", ext_sandbox_memory_new_version_1}, - {"ext_sandbox_memory_set_version_1", ext_sandbox_memory_set_version_1}, - {"ext_sandbox_memory_teardown_version_1", ext_sandbox_memory_teardown_version_1}, - {"ext_crypto_ed25519_generate_version_1", ext_crypto_ed25519_generate_version_1}, - {"ext_crypto_ed25519_verify_version_1", ext_crypto_ed25519_verify_version_1}, - {"ext_crypto_finish_batch_verify_version_1", ext_crypto_finish_batch_verify_version_1}, - {"ext_crypto_secp256k1_ecdsa_recover_compressed_version_1", ext_crypto_secp256k1_ecdsa_recover_compressed_version_1}, - {"ext_crypto_sr25519_public_keys_version_1", ext_crypto_sr25519_public_keys_version_1}, - {"ext_offchain_local_storage_set_version_1", ext_offchain_local_storage_set_version_1}, - {"ext_crypto_sr25519_sign_version_1", ext_crypto_sr25519_sign_version_1}, - {"ext_crypto_sr25519_verify_version_2", ext_crypto_sr25519_verify_version_2}, - {"ext_crypto_start_batch_verify_version_1", ext_crypto_start_batch_verify_version_1}, - {"ext_trie_blake2_256_ordered_root_version_1", ext_trie_blake2_256_ordered_root_version_1}, - {"ext_misc_print_hex_version_1", ext_misc_print_hex_version_1}, - {"ext_misc_print_num_version_1", ext_misc_print_num_version_1}, - {"ext_misc_print_utf8_version_1", ext_misc_print_utf8_version_1}, - {"ext_misc_runtime_version_version_1", ext_misc_runtime_version_version_1}, - {"ext_default_child_storage_clear_version_1", ext_default_child_storage_clear_version_1}, - {"ext_default_child_storage_get_version_1", ext_default_child_storage_get_version_1}, - {"ext_default_child_storage_read_version_1", ext_default_child_storage_read_version_1}, - {"ext_default_child_storage_root_version_1", ext_default_child_storage_root_version_1}, - {"ext_default_child_storage_set_version_1", ext_default_child_storage_set_version_1}, - {"ext_default_child_storage_storage_kill_version_1", ext_default_child_storage_storage_kill_version_1}, - {"ext_default_child_storage_storage_kill_version_2", ext_default_child_storage_storage_kill_version_2}, - {"ext_allocator_free_version_1", ext_allocator_free_version_1}, - {"ext_allocator_malloc_version_1", ext_allocator_malloc_version_1}, - {"ext_hashing_blake2_128_version_1", ext_hashing_blake2_128_version_1}, - {"ext_hashing_blake2_256_version_1", ext_hashing_blake2_256_version_1}, - {"ext_hashing_keccak_256_version_1", ext_hashing_keccak_256_version_1}, - {"ext_hashing_sha2_256_version_1", ext_hashing_sha2_256_version_1}, - {"ext_hashing_twox_128_version_1", ext_hashing_twox_128_version_1}, - {"ext_hashing_twox_64_version_1", ext_hashing_twox_64_version_1}, - {"ext_offchain_is_validator_version_1", ext_offchain_is_validator_version_1}, - {"ext_offchain_local_storage_compare_and_set_version_1", ext_offchain_local_storage_compare_and_set_version_1}, - {"ext_crypto_sr25519_generate_version_1", ext_crypto_sr25519_generate_version_1}, - {"ext_offchain_local_storage_get_version_1", ext_offchain_local_storage_get_version_1}, - {"ext_offchain_network_state_version_1", ext_offchain_network_state_version_1}, - {"ext_offchain_random_seed_version_1", ext_offchain_random_seed_version_1}, - {"ext_offchain_submit_transaction_version_1", ext_offchain_submit_transaction_version_1}, - {"ext_storage_append_version_1", ext_storage_append_version_1}, - {"ext_storage_changes_root_version_1", ext_storage_changes_root_version_1}, - {"ext_storage_clear_version_1", ext_storage_clear_version_1}, - {"ext_storage_clear_prefix_version_1", ext_storage_clear_prefix_version_1}, - {"ext_storage_commit_transaction_version_1", ext_storage_commit_transaction_version_1}, - {"ext_storage_exists_version_1", ext_storage_exists_version_1}, - {"ext_storage_get_version_1", ext_storage_get_version_1}, - {"ext_storage_next_key_version_1", ext_storage_next_key_version_1}, - {"ext_storage_read_version_1", ext_storage_read_version_1}, - {"ext_storage_rollback_transaction_version_1", ext_storage_rollback_transaction_version_1}, - {"ext_storage_root_version_1", ext_storage_root_version_1}, - {"ext_storage_set_version_1", ext_storage_set_version_1}, - {"ext_storage_start_transaction_version_1", ext_storage_start_transaction_version_1}, - {"ext_offchain_index_set_version_1", ext_offchain_index_set_version_1}, - } - - linker := wasmtime.NewLinker(store) - if err := linker.Define("env", "memory", memory); err != nil { - return nil, err - } - - for _, f := range fns { - if err := linker.DefineFunc("env", f.name, f.fn); err != nil { - return nil, err - } - } - return linker, nil -} diff --git a/lib/runtime/wasmtime/instance.go b/lib/runtime/wasmtime/instance.go deleted file mode 100644 index 210368e0f3..0000000000 --- a/lib/runtime/wasmtime/instance.go +++ /dev/null @@ -1,201 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package wasmtime - -import ( - "errors" - "os" - "runtime" - "sync" - - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/keystore" - gssmrruntime "github.com/ChainSafe/gossamer/lib/runtime" - - log "github.com/ChainSafe/log15" - "github.com/bytecodealliance/wasmtime-go" -) - -// Name represents the name of the interpreter -const Name = "wasmtime" - -var ( - _ gssmrruntime.Instance = (*Instance)(nil) - - ctx gssmrruntime.Context - logger = log.New("pkg", "runtime", "module", "go-wasmtime") -) - -// ImportsFunc returns a linker with the module imports -type ImportsFunc func(*wasmtime.Store, *wasmtime.Memory) (*wasmtime.Linker, error) - -// Config represents a wasmer configuration -type Config struct { - gssmrruntime.InstanceConfig - Imports ImportsFunc -} - -// Instance represents a v0.8 runtime go-wasmtime instance -type Instance struct { - vm *wasmtime.Instance - mu sync.Mutex - mem *wasmtime.Memory -} - -// GetCodeHash ... -func (in *Instance) GetCodeHash() common.Hash { - return common.Hash{} -} - -// NewInstanceFromFile instantiates a runtime from a .wasm file -func NewInstanceFromFile(fp string, cfg *Config) (*Instance, error) { - engine := wasmtime.NewEngine() - module, err := wasmtime.NewModuleFromFile(engine, fp) - if err != nil { - return nil, err - } - - return newInstanceFromModule(module, engine, cfg) -} - -// NewInstance instantiates a runtime from the given wasm bytecode -func NewInstance(code []byte, cfg *Config) (*Instance, error) { - engine := wasmtime.NewEngine() - module, err := wasmtime.NewModule(engine, code) - if err != nil { - return nil, err - } - - return newInstanceFromModule(module, engine, cfg) -} - -func newInstanceFromModule(module *wasmtime.Module, engine *wasmtime.Engine, cfg *Config) (*Instance, error) { - // if cfg.LogLvl set to < 0, then don't change package log level - if cfg.LogLvl >= 0 { - h := log.StreamHandler(os.Stdout, log.TerminalFormat()) - h = log.CallerFileHandler(h) - logger.SetHandler(log.LvlFilterHandler(cfg.LogLvl, h)) - } - store := wasmtime.NewStore(engine) - - lim := wasmtime.Limits{ - Min: 23, - Max: wasmtime.LimitsMaxNone, - } - mem := wasmtime.NewMemory(store, wasmtime.NewMemoryType(lim)) - - linker, err := cfg.Imports(store, mem) - if err != nil { - return nil, err - } - - instance, err := linker.Instantiate(module) - if err != nil { - return nil, err - } - - // TODO: use __heap_base - allocator := gssmrruntime.NewAllocator(Memory{mem}, 0) - - ctx = gssmrruntime.Context{ - Storage: cfg.Storage, - Allocator: allocator, - Keystore: cfg.Keystore, - Validator: cfg.Role == byte(4), - NodeStorage: cfg.NodeStorage, - Network: cfg.Network, - } - - return &Instance{ - vm: instance, - mem: mem, - }, nil -} - -// UpdateRuntimeCode ... -func (in *Instance) UpdateRuntimeCode(_ []byte) error { - return errors.New("unimplemented") -} - -// CheckRuntimeVersion ... -func (in *Instance) CheckRuntimeVersion(code []byte) (gssmrruntime.Version, error) { - return nil, errors.New("unimplemented") -} - -// SetContextStorage sets the runtime context's Storage -func (in *Instance) SetContextStorage(s gssmrruntime.Storage) { - ctx.Storage = s -} - -// Stop ... -func (in *Instance) Stop() {} - -// NodeStorage returns the context's NodeStorage -func (in *Instance) NodeStorage() gssmrruntime.NodeStorage { - return ctx.NodeStorage -} - -// Validator returns the context's Validator -func (in *Instance) Validator() bool { - return ctx.Validator -} - -// Keystore to get reference to runtime keystore -func (in *Instance) Keystore() *keystore.GlobalKeystore { - return ctx.Keystore -} - -// NetworkService returns the context's NetworkService -func (in *Instance) NetworkService() gssmrruntime.BasicNetwork { - return ctx.Network -} - -// Exec calls the given function with the given data -func (in *Instance) Exec(function string, data []byte) ([]byte, error) { - return in.exec(function, data) -} - -func (in *Instance) exec(function string, data []byte) ([]byte, error) { - in.mu.Lock() - defer in.mu.Unlock() - - ptr, err := ctx.Allocator.Allocate(uint32(len(data))) - if err != nil { - return nil, err - } - defer ctx.Allocator.Clear() - - memdata := in.mem.UnsafeData() - copy(memdata[ptr:ptr+uint32(len(data))], data) - - run := in.vm.GetExport(function).Func() - resi, err := run.Call(int32(ptr), int32(len(data))) - if err != nil { - return nil, err - } - - if resi == nil { - return []byte{}, err - } - - ret := resi.(int64) - length := int32(ret >> 32) - offset := int32(ret) - - runtime.KeepAlive(in.mem) - return memdata[offset : offset+length], nil -} diff --git a/lib/runtime/wasmtime/memory.go b/lib/runtime/wasmtime/memory.go deleted file mode 100644 index 1c71be3c48..0000000000 --- a/lib/runtime/wasmtime/memory.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package wasmtime - -import ( - "errors" - - "github.com/bytecodealliance/wasmtime-go" -) - -// Memory is a thin wrapper around wasmtime memory to support -// Gossamer runtime.Memory interface -type Memory struct { - memory *wasmtime.Memory -} - -// Data returns the memory's data -func (m Memory) Data() []byte { - return m.memory.UnsafeData() -} - -// Length returns the memory's length -func (m Memory) Length() uint32 { - return uint32(m.memory.DataSize()) -} - -// Grow ... -func (m Memory) Grow(numPages uint32) error { - return errors.New("wasmtime Memory.Grow unimplemented") -} diff --git a/lib/runtime/wasmtime/test_helpers.go b/lib/runtime/wasmtime/test_helpers.go deleted file mode 100644 index 39e04bf915..0000000000 --- a/lib/runtime/wasmtime/test_helpers.go +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2019 ChainSafe Systems (ON) Corp. -// This file is part of gossamer. -// -// The gossamer library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The gossamer library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the gossamer library. If not, see . - -package wasmtime - -import ( - "path/filepath" - "testing" - - "github.com/ChainSafe/gossamer/lib/keystore" - "github.com/ChainSafe/gossamer/lib/runtime" - "github.com/ChainSafe/gossamer/lib/runtime/storage" - "github.com/ChainSafe/gossamer/lib/trie" - - log "github.com/ChainSafe/log15" - "github.com/stretchr/testify/require" -) - -// NewTestInstance will create a new runtime instance using the given target runtime -func NewTestInstance(t *testing.T, targetRuntime string) *Instance { - return NewTestInstanceWithTrie(t, targetRuntime, nil, log.LvlInfo) -} - -// NewTestInstanceWithTrie will create a new runtime (polkadot/test) with the supplied trie as the storage -func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Lvl) *Instance { - testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(targetRuntime) - importsFunc := GetRuntimeImports(t, targetRuntime) - - _, err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL) - require.Nil(t, err, "Fail: could not get runtime", "targetRuntime", targetRuntime) - - fp, err := filepath.Abs(testRuntimeFilePath) - require.Nil(t, err, "could not create testRuntimeFilePath", "targetRuntime", targetRuntime) - - ns := runtime.NodeStorage{ - LocalStorage: runtime.NewInMemoryDB(t), - PersistentStorage: runtime.NewInMemoryDB(t), // we're using a local storage here since this is a test runtime - } - cfg := &Config{ - Imports: importsFunc, - } - cfg.Storage, err = storage.NewTrieState(tt) - require.NoError(t, err) - cfg.Keystore = keystore.NewGlobalKeystore() - cfg.LogLvl = lvl - cfg.NodeStorage = ns - cfg.Network = new(runtime.TestRuntimeNetwork) - - r, err := NewInstanceFromFile(fp, cfg) - require.NoError(t, err, "Got error when trying to create new VM", "targetRuntime", targetRuntime) - require.NotNil(t, r, "Could not create new VM instance", "targetRuntime", targetRuntime) - return r -} - -// GetRuntimeImports ... -func GetRuntimeImports(t *testing.T, targetRuntime string) ImportsFunc { - var imports ImportsFunc - - switch targetRuntime { - case runtime.NODE_RUNTIME: - imports = ImportNodeRuntime - default: - t.Fatalf("unknown runtime type %s", targetRuntime) - } - - return imports -}