From 8582cb20a37d574e6397f980f919c6dd4dfb24a0 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Thu, 9 Jun 2022 18:30:28 -0400 Subject: [PATCH] chore(tests): cache runtimes to work offline (#2577) - Download runtimes to ${TMP}/gossamer/runtimes/ depending on environment - Do not remove runtime files and re-use them if found - Merge `GetRuntimeVars` and `GetRuntimeBlob` into `GetRuntime` - Improve HTTP fetching code for runtime wasms - Remove `TestMain`-used `GetRuntimes` since we already have calls to `GetRuntime` wherever a runtime blob is needed. --- cmd/gossamer/config_test.go | 5 +- dot/core/service_integration_test.go | 23 +--- dot/node_integration_test.go | 5 +- dot/rpc/modules/author_integration_test.go | 22 +--- dot/rpc/subscription/listeners_test.go | 8 +- dot/sync/syncer_integeration_test.go | 15 --- lib/babe/babe_integration_test.go | 17 --- lib/runtime/constants.go | 2 - lib/runtime/life/test_helpers.go | 22 ++-- lib/runtime/test_helpers.go | 144 ++++++++++++--------- lib/runtime/wasmer/imports_test.go | 16 --- lib/runtime/wasmer/instance_test.go | 9 +- lib/runtime/wasmer/test_helpers.go | 24 ++-- 13 files changed, 125 insertions(+), 187 deletions(-) diff --git a/cmd/gossamer/config_test.go b/cmd/gossamer/config_test.go index 76f1c3ab58..0a744d5369 100644 --- a/cmd/gossamer/config_test.go +++ b/cmd/gossamer/config_test.go @@ -4,6 +4,7 @@ package main import ( + "context" "encoding/hex" "encoding/json" "errors" @@ -941,9 +942,7 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) { cfg := newTestConfig(t) cfg.Global.Name = globalName - runtimeFilePath := filepath.Join(t.TempDir(), "runtime") - _, testRuntimeURL := runtime.GetRuntimeVars(runtime.NODE_RUNTIME) - err := runtime.GetRuntimeBlob(runtimeFilePath, testRuntimeURL) + runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.NODE_RUNTIME) require.NoError(t, err) runtimeData, err := os.ReadFile(runtimeFilePath) require.NoError(t, err) diff --git a/dot/core/service_integration_test.go b/dot/core/service_integration_test.go index a37903c065..f60dc989cf 100644 --- a/dot/core/service_integration_test.go +++ b/dot/core/service_integration_test.go @@ -6,6 +6,7 @@ package core import ( + "context" "fmt" "math/big" "os" @@ -134,20 +135,6 @@ func generateTestValidRemarkTxns(t *testing.T, pubKey []byte, accInfo types.Acco return extBytes, rt } -func TestMain(m *testing.M) { - wasmFilePaths, err := runtime.GenerateRuntimeWasmFile() - if err != nil { - log.Errorf("failed to generate runtime wasm file: %s", err) - os.Exit(1) - } - - // Start all tests - code := m.Run() - - runtime.RemoveFiles(wasmFilePaths) - os.Exit(code) -} - func TestStartService(t *testing.T) { s := NewTestService(t, nil) @@ -695,7 +682,9 @@ func TestService_HandleRuntimeChanges(t *testing.T) { func TestService_HandleCodeSubstitutes(t *testing.T) { s := NewTestService(t, nil) - testRuntime, err := os.ReadFile(runtime.POLKADOT_RUNTIME_FP) + runtimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME) + require.NoError(t, err) + testRuntime, err := os.ReadFile(runtimeFilepath) require.NoError(t, err) // hash for known test code substitution @@ -745,7 +734,9 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) { require.NoError(t, err) require.Equal(t, codeHashBefore, parentRt.GetCodeHash()) // codeHash should remain unchanged after code substitute - testRuntime, err := os.ReadFile(runtime.POLKADOT_RUNTIME_FP) + runtimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME) + require.NoError(t, err) + testRuntime, err := os.ReadFile(runtimeFilepath) require.NoError(t, err) ts, err = s.storageState.TrieState(nil) diff --git a/dot/node_integration_test.go b/dot/node_integration_test.go index cb16036a60..dfa18c2b69 100644 --- a/dot/node_integration_test.go +++ b/dot/node_integration_test.go @@ -6,6 +6,7 @@ package dot import ( + "context" "encoding/hex" "encoding/json" "os" @@ -271,9 +272,7 @@ func TestNode_PersistGlobalName_WhenInitialize(t *testing.T) { // newTestGenesisAndRuntime create a new test runtime and a new test genesis // file with the test runtime stored in raw data and returns the genesis file func newTestGenesisAndRuntime(t *testing.T) (filename string) { - runtimeFilePath := filepath.Join(t.TempDir(), "runtime") - _, testRuntimeURL := runtime.GetRuntimeVars(runtime.NODE_RUNTIME) - err := runtime.GetRuntimeBlob(runtimeFilePath, testRuntimeURL) + runtimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.NODE_RUNTIME) require.NoError(t, err) runtimeData, err := os.ReadFile(runtimeFilePath) require.NoError(t, err) diff --git a/dot/rpc/modules/author_integration_test.go b/dot/rpc/modules/author_integration_test.go index 9930ece237..385243f763 100644 --- a/dot/rpc/modules/author_integration_test.go +++ b/dot/rpc/modules/author_integration_test.go @@ -6,6 +6,7 @@ package modules import ( + "context" "errors" "fmt" "os" @@ -53,16 +54,11 @@ func useInstanceFromGenesis(t *testing.T, rtStorage *storage.TrieState) (instanc } func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (instance runtime.Instance) { - testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(runtime.POLKADOT_RUNTIME_v0910) - err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL) + testRuntimeFilePath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME_v0910) require.NoError(t, err) - bytes, err := os.ReadFile(testRuntimeFilePath) require.NoError(t, err) - err = runtime.RemoveFiles([]string{testRuntimeFilePath}) - require.NoError(t, err) - rtStorage.Set(common.CodeKey, bytes) cfg := &wasmer.Config{} @@ -82,20 +78,6 @@ func useInstanceFromRuntimeV0910(t *testing.T, rtStorage *storage.TrieState) (in return runtimeInstance } -func TestMain(m *testing.M) { - wasmFilePaths, err := runtime.GenerateRuntimeWasmFile() - if err != nil { - log.Errorf("failed to generate runtime wasm file: %s", err) - os.Exit(1) - } - - // Start all tests - code := m.Run() - - runtime.RemoveFiles(wasmFilePaths) - os.Exit(code) -} - func TestAuthorModule_Pending_Integration(t *testing.T) { t.Parallel() integrationTestController := setupStateAndRuntime(t, t.TempDir(), nil) diff --git a/dot/rpc/subscription/listeners_test.go b/dot/rpc/subscription/listeners_test.go index 2e8a2f44e9..6576216e91 100644 --- a/dot/rpc/subscription/listeners_test.go +++ b/dot/rpc/subscription/listeners_test.go @@ -4,13 +4,13 @@ package subscription import ( + "context" "encoding/json" "fmt" "log" "net/http" "net/http/httptest" "os" - "path/filepath" "strings" "testing" "time" @@ -352,11 +352,9 @@ func TestRuntimeChannelListener_Listen(t *testing.T) { expectedInitialResponse.Params.Result = expectedInitialVersion instance := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME) - err := runtime.GetRuntimeBlob(runtime.POLKADOT_RUNTIME_FP, runtime.POLKADOT_RUNTIME_URL) + polkadotRuntimeFilepath, err := runtime.GetRuntime(context.Background(), runtime.POLKADOT_RUNTIME) require.NoError(t, err) - fp, err := filepath.Abs(runtime.POLKADOT_RUNTIME_FP) - require.NoError(t, err) - code, err := os.ReadFile(fp) + code, err := os.ReadFile(polkadotRuntimeFilepath) require.NoError(t, err) version, err := instance.CheckRuntimeVersion(code) require.NoError(t, err) diff --git a/dot/sync/syncer_integeration_test.go b/dot/sync/syncer_integeration_test.go index aac17d73e3..e495534d11 100644 --- a/dot/sync/syncer_integeration_test.go +++ b/dot/sync/syncer_integeration_test.go @@ -8,7 +8,6 @@ package sync import ( "errors" - "os" "path/filepath" "testing" @@ -28,20 +27,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestMain(m *testing.M) { - wasmFilePaths, err := runtime.GenerateRuntimeWasmFile() - if err != nil { - log.Errorf("failed to generate runtime wasm file: %s", err) - os.Exit(1) - } - - // Start all tests - code := m.Run() - - runtime.RemoveFiles(wasmFilePaths) - os.Exit(code) -} - func newMockFinalityGadget() *mocks.FinalityGadget { m := new(mocks.FinalityGadget) // using []uint8 instead of []byte: https://github.com/stretchr/testify/pull/969 diff --git a/lib/babe/babe_integration_test.go b/lib/babe/babe_integration_test.go index 1037eda302..df93914d12 100644 --- a/lib/babe/babe_integration_test.go +++ b/lib/babe/babe_integration_test.go @@ -6,7 +6,6 @@ package babe import ( - "os" "path/filepath" "testing" "time" @@ -153,22 +152,6 @@ func createTestService(t *testing.T, cfg *ServiceConfig) *Service { return babeService } -func TestMain(m *testing.M) { - wasmFilePaths, err := runtime.GenerateRuntimeWasmFile() - if err != nil { - log.Errorf("failed to generate runtime wasm file: %s", err) - os.Exit(1) - } - - logger = log.NewFromGlobal(log.SetLevel(defaultTestLogLvl)) - - // Start all tests - code := m.Run() - - runtime.RemoveFiles(wasmFilePaths) - os.Exit(code) -} - func newTestServiceSetupParameters(t *testing.T) (*Service, *state.EpochState, *types.BabeConfiguration) { ctrl := gomock.NewController(t) telemetryMock := NewMockClient(ctrl) diff --git a/lib/runtime/constants.go b/lib/runtime/constants.go index 378aacb556..675d4952f8 100644 --- a/lib/runtime/constants.go +++ b/lib/runtime/constants.go @@ -7,8 +7,6 @@ import ( "github.com/ChainSafe/gossamer/lib/common" ) -var runtimes = []string{HOST_API_TEST_RUNTIME, POLKADOT_RUNTIME, POLKADOT_RUNTIME_v0917, NODE_RUNTIME, DEV_RUNTIME} - //nolint:revive const ( // v0.9 substrate runtime diff --git a/lib/runtime/life/test_helpers.go b/lib/runtime/life/test_helpers.go index b0a521b8e6..2ed1340b63 100644 --- a/lib/runtime/life/test_helpers.go +++ b/lib/runtime/life/test_helpers.go @@ -4,7 +4,7 @@ package life import ( - "path/filepath" + "context" "testing" "github.com/ChainSafe/gossamer/internal/log" @@ -20,30 +20,24 @@ var DefaultTestLogLvl = log.Info // newTestInstance will create a new runtime instance using the given target runtime func newTestInstance(t *testing.T, targetRuntime string) *Instance { - return newTestInstanceWithTrie(t, runtime.HOST_API_TEST_RUNTIME, nil, DefaultTestLogLvl) + return newTestInstanceWithTrie(t, targetRuntime, nil, DefaultTestLogLvl) } // newTestInstanceWithTrie will create a new runtime instance with the supplied trie as the storage func newTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Level) *Instance { - fp, cfg := setupConfig(t, targetRuntime, tt, lvl, 0) - r, err := NewInstanceFromFile(fp, cfg) + testRuntimeFilePath, err := runtime.GetRuntime(context.Background(), targetRuntime) + require.NoError(t, err) + cfg := setupConfig(t, tt, lvl, 0) + r, err := NewInstanceFromFile(testRuntimeFilePath, 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 } -func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Level, role byte) (string, *Config) { - testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(targetRuntime) - - err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL) - require.Nil(t, err, "Fail: could not get runtime", "targetRuntime", targetRuntime) - +func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role byte) *Config { s, err := storage.NewTrieState(tt) require.NoError(t, err) - 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 @@ -56,5 +50,5 @@ func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Leve cfg.Network = new(runtime.TestRuntimeNetwork) cfg.Role = role cfg.Resolver = new(Resolver) - return fp, cfg + return cfg } diff --git a/lib/runtime/test_helpers.go b/lib/runtime/test_helpers.go index 8d437fa791..ac8c706986 100644 --- a/lib/runtime/test_helpers.go +++ b/lib/runtime/test_helpers.go @@ -5,10 +5,13 @@ package runtime import ( "context" + "errors" + "fmt" "io" "net/http" "os" "path" + "path/filepath" "testing" "time" @@ -42,66 +45,115 @@ func NewInMemoryDB(t *testing.T) chaindb.Database { return db } -// GetRuntimeVars returns the testRuntimeFilePath and testRuntimeURL -func GetRuntimeVars(targetRuntime string) (string, string) { - switch targetRuntime { +var ( + ErrRuntimeUnknown = errors.New("runtime is not known") + ErrHTTPStatusNotOK = errors.New("HTTP status code received is not OK") + ErrOpenRuntimeFile = errors.New("cannot open the runtime target file") +) + +// GetRuntime returns the runtime file path located in the +// /tmp/gossamer/runtimes directory (depending on OS and environment). +// If the file did not exist, the runtime WASM blob is downloaded to that file. +func GetRuntime(ctx context.Context, runtime string) ( + runtimePath string, err error) { + basePath := filepath.Join(os.TempDir(), "/gossamer/runtimes/") + const perm = os.FileMode(0777) + err = os.MkdirAll(basePath, perm) + if err != nil { + return "", fmt.Errorf("cannot create directory for runtimes: %w", err) + } + + var runtimeFilename, url string + switch runtime { case NODE_RUNTIME: - return GetAbsolutePath(NODE_RUNTIME_FP), NODE_RUNTIME_URL + runtimeFilename = NODE_RUNTIME_FP + url = NODE_RUNTIME_URL case NODE_RUNTIME_v098: - return GetAbsolutePath(NODE_RUNTIME_FP_v098), NODE_RUNTIME_URL_v098 + runtimeFilename = NODE_RUNTIME_FP_v098 + url = NODE_RUNTIME_URL_v098 case POLKADOT_RUNTIME_v0917: - return GetAbsolutePath(POLKADOT_RUNTIME_FP_v0917), POLKADOT_RUNTIME_URL_v0917 + runtimeFilename = POLKADOT_RUNTIME_FP_v0917 + url = POLKADOT_RUNTIME_URL_v0917 case POLKADOT_RUNTIME_v0910: - return GetAbsolutePath(POLKADOT_RUNTIME_FP_v0910), POLKADOT_RUNTIME_URL_v0910 + runtimeFilename = POLKADOT_RUNTIME_FP_v0910 + url = POLKADOT_RUNTIME_URL_v0910 case POLKADOT_RUNTIME: - return GetAbsolutePath(POLKADOT_RUNTIME_FP), POLKADOT_RUNTIME_URL + runtimeFilename = POLKADOT_RUNTIME_FP + url = POLKADOT_RUNTIME_URL case HOST_API_TEST_RUNTIME: - return GetAbsolutePath(HOST_API_TEST_RUNTIME_FP), HOST_API_TEST_RUNTIME_URL + runtimeFilename = HOST_API_TEST_RUNTIME_FP + url = HOST_API_TEST_RUNTIME_URL case DEV_RUNTIME: - return GetAbsolutePath(DEV_RUNTIME_FP), DEV_RUNTIME_URL + runtimeFilename = DEV_RUNTIME_FP + url = DEV_RUNTIME_URL default: - return "", "" + return "", fmt.Errorf("%w: %s", ErrRuntimeUnknown, runtime) } -} -// GetAbsolutePath returns the completePath for a given targetDir -func GetAbsolutePath(targetDir string) string { - dir, err := os.Getwd() + runtimePath = filepath.Join(basePath, runtimeFilename) + runtimePath, err = filepath.Abs(runtimePath) if err != nil { - panic("failed to get current working directory") + return "", fmt.Errorf("malformed relative path: %w", err) } - return path.Join(dir, targetDir) -} -// GetRuntimeBlob checks if the test wasm @testRuntimeFilePath exists and if not, it fetches it from @testRuntimeURL -func GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL string) error { - if utils.PathExists(testRuntimeFilePath) { - return nil + if utils.PathExists(runtimePath) { + return runtimePath, nil } - ctx, cancel := context.WithCancel(context.Background()) + const requestTimeout = 10 * time.Second + ctx, cancel := context.WithTimeout(ctx, requestTimeout) defer cancel() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, testRuntimeURL, nil) + request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return "", fmt.Errorf("cannot make HTTP request: %w", err) + } + + response, err := http.DefaultClient.Do(request) + if err != nil { + return "", fmt.Errorf("cannot get: %w", err) + } + + if response.StatusCode != http.StatusOK { + _ = response.Body.Close() + return "", fmt.Errorf("%w: %d %s", ErrHTTPStatusNotOK, + response.StatusCode, response.Status) + } + + const flag = os.O_TRUNC | os.O_CREATE | os.O_WRONLY + file, err := os.OpenFile(runtimePath, flag, perm) //nolint:gosec if err != nil { - return err + _ = response.Body.Close() + return "", fmt.Errorf("cannot open target destination file: %w", err) } - const runtimeReqTimout = time.Second * 30 + _, err = io.Copy(file, response.Body) + if err != nil { + _ = response.Body.Close() + return "", fmt.Errorf("cannot copy response body to %s: %w", + runtimePath, err) + } - httpcli := http.Client{Timeout: runtimeReqTimout} - resp, err := httpcli.Do(req) + err = file.Close() if err != nil { - return err + return "", fmt.Errorf("cannot close file: %w", err) } - respBody, err := io.ReadAll(resp.Body) + err = response.Body.Close() if err != nil { - return err + return "", fmt.Errorf("cannot close HTTP response body: %w", err) } - defer resp.Body.Close() //nolint:errcheck - return os.WriteFile(testRuntimeFilePath, respBody, os.ModePerm) + return runtimePath, nil +} + +// GetAbsolutePath returns the completePath for a given targetDir +func GetAbsolutePath(targetDir string) string { + dir, err := os.Getwd() + if err != nil { + panic("failed to get current working directory") + } + return path.Join(dir, targetDir) } // TestRuntimeNetwork ... @@ -143,32 +195,6 @@ func generateEd25519Signatures(t *testing.T, n int) []*crypto.SignatureInfo { return signs } -// GenerateRuntimeWasmFile generates all runtime wasm files. -func GenerateRuntimeWasmFile() ([]string, error) { - var wasmFilePaths []string - for _, rt := range runtimes { - testRuntimeFilePath, testRuntimeURL := GetRuntimeVars(rt) - err := GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL) - if err != nil { - return nil, err - } - - wasmFilePaths = append(wasmFilePaths, testRuntimeFilePath) - } - return wasmFilePaths, nil -} - -// RemoveFiles removes multiple files. -func RemoveFiles(files []string) error { - for _, file := range files { - err := os.Remove(file) - if err != nil { - return err - } - } - return nil -} - // NewTestExtrinsic builds a new extrinsic using centrifuge pkg func NewTestExtrinsic(t *testing.T, rt Instance, genHash, blockHash common.Hash, nonce uint64, call string, args ...interface{}) string { diff --git a/lib/runtime/wasmer/imports_test.go b/lib/runtime/wasmer/imports_test.go index bc4b93257e..12b5ec5397 100644 --- a/lib/runtime/wasmer/imports_test.go +++ b/lib/runtime/wasmer/imports_test.go @@ -7,13 +7,11 @@ import ( "bytes" "encoding/binary" "net/http" - "os" "sort" "testing" "time" "github.com/ChainSafe/chaindb" - "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/common/types" "github.com/ChainSafe/gossamer/lib/crypto" @@ -34,20 +32,6 @@ var testChildKey = []byte("childKey") var testKey = []byte("key") var testValue = []byte("value") -func TestMain(m *testing.M) { - wasmFilePaths, err := runtime.GenerateRuntimeWasmFile() - if err != nil { - log.Errorf("failed to generate runtime wasm file: %s", err) - os.Exit(1) - } - - // Start all tests - code := m.Run() - - runtime.RemoveFiles(wasmFilePaths) - os.Exit(code) -} - func Test_ext_offchain_timestamp_version_1(t *testing.T) { inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME) runtimeFunc, ok := inst.vm.Exports["rtm_ext_offchain_timestamp_version_1"] diff --git a/lib/runtime/wasmer/instance_test.go b/lib/runtime/wasmer/instance_test.go index 89b95d70eb..97b50c3bc5 100644 --- a/lib/runtime/wasmer/instance_test.go +++ b/lib/runtime/wasmer/instance_test.go @@ -4,8 +4,8 @@ package wasmer import ( + "context" "os" - "path/filepath" "testing" "github.com/ChainSafe/gossamer/lib/runtime" @@ -38,11 +38,10 @@ func TestPointerSize(t *testing.T) { func TestInstance_CheckRuntimeVersion(t *testing.T) { instance := NewTestInstance(t, runtime.NODE_RUNTIME) - err := runtime.GetRuntimeBlob(runtime.POLKADOT_RUNTIME_FP, runtime.POLKADOT_RUNTIME_URL) + polkadotRuntimeFilepath, err := runtime.GetRuntime( + context.Background(), runtime.POLKADOT_RUNTIME) require.NoError(t, err) - fp, err := filepath.Abs(runtime.POLKADOT_RUNTIME_FP) - require.NoError(t, err) - code, err := os.ReadFile(fp) + code, err := os.ReadFile(polkadotRuntimeFilepath) require.NoError(t, err) version, err := instance.CheckRuntimeVersion(code) require.NoError(t, err) diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index ab1f07458c..4ac3463ed3 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -4,7 +4,7 @@ package wasmer import ( - "path/filepath" + "context" "testing" "github.com/ChainSafe/gossamer/internal/log" @@ -23,30 +23,30 @@ var DefaultTestLogLvl = log.Info // NewTestInstance will create a new runtime instance using the given target runtime func NewTestInstance(t *testing.T, targetRuntime string) *Instance { + t.Helper() return NewTestInstanceWithTrie(t, targetRuntime, nil) } // 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) *Instance { - fp, cfg := setupConfig(t, targetRuntime, tt, DefaultTestLogLvl, 0) - r, err := NewInstanceFromFile(fp, cfg) + t.Helper() + + cfg := setupConfig(t, tt, DefaultTestLogLvl, 0) + runtimeFilepath, err := runtime.GetRuntime(context.Background(), targetRuntime) + require.NoError(t, err) + + r, err := NewInstanceFromFile(runtimeFilepath, 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 } -func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Level, role byte) (string, *Config) { - testRuntimeFilePath, testRuntimeURL := runtime.GetRuntimeVars(targetRuntime) - - err := runtime.GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL) - require.Nil(t, err, "Fail: could not get runtime", "targetRuntime", targetRuntime) +func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role byte) *Config { + t.Helper() s, err := storage.NewTrieState(tt) require.NoError(t, err) - 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 @@ -62,7 +62,7 @@ func setupConfig(t *testing.T, targetRuntime string, tt *trie.Trie, lvl log.Leve cfg.Network = new(runtime.TestRuntimeNetwork) cfg.Transaction = newTransactionStateMock() cfg.Role = role - return fp, cfg + return cfg } // NewTransactionStateMock create and return an runtime Transaction State interface mock