-
Notifications
You must be signed in to change notification settings - Fork 109
/
test_helpers.go
80 lines (67 loc) · 2.86 KB
/
test_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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 <http://www.gnu.org/licenses/>.
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.LvlTrace)
}
// 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
}