Skip to content
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
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func createRuntime(cfg *Config, st *state.Service, ks *keystore.GenericKeystore)
Keystore: ks,
Imports: runtime.RegisterImports_NodeRuntime,
LogLvl: lvl,
Role: cfg.Core.Roles,
}

// create runtime executor
Expand Down
7 changes: 6 additions & 1 deletion lib/runtime/imports_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,12 @@ func ext_secp256k1_ecdsa_recover(context unsafe.Pointer, msgData, sigData, pubke
//export ext_is_validator
func ext_is_validator(context unsafe.Pointer) int32 {
logger.Trace("[ext_is_validator] executing...")
logger.Warn("[ext_is_validator] Not yet implemented.")
instanceContext := wasm.IntoInstanceContext(context)

runtimeCtx := instanceContext.Data().(*Ctx)
if runtimeCtx.validator {
return 1
}
return 0
}

Expand Down
26 changes: 25 additions & 1 deletion lib/runtime/imports_old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/trie"

"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -1132,3 +1131,28 @@ func TestExt_set_child_storage(t *testing.T) {
t.Fatalf("Fail: got %x expected %x", res, value)
}
}

func TestExt_is_validator(t *testing.T) {
// test with validator
runtime := NewTestRuntimeWithRole(t, TEST_RUNTIME, byte(4))
// call wasm function
testFunc, ok := runtime.vm.Exports["test_ext_is_validator"]
if !ok {
t.Fatal("could not find exported function")
}
res, err := testFunc()
require.NoError(t, err)
require.Equal(t, int32(1), res.ToI32())

// test with non-validator
runtime = NewTestRuntimeWithRole(t, TEST_RUNTIME, byte(1))
// call wasm function
testFunc, ok = runtime.vm.Exports["test_ext_is_validator"]
if !ok {
t.Fatal("could not find exported function")
}
res, err = testFunc()
require.NoError(t, err)
require.Equal(t, int32(0), res.ToI32())

}
8 changes: 8 additions & 0 deletions lib/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Ctx struct {
storage Storage
allocator *FreeingBumpHeapAllocator
keystore *keystore.GenericKeystore
validator bool
}

// Config represents a runtime configuration
Expand All @@ -42,6 +43,7 @@ type Config struct {
Keystore *keystore.GenericKeystore
Imports func() (*wasm.Imports, error)
LogLvl log.Lvl
Role byte
}

// Runtime struct
Expand Down Expand Up @@ -92,10 +94,16 @@ func NewRuntime(code []byte, cfg *Config) (*Runtime, error) {

memAllocator := NewAllocator(instance.Memory, 0)

validator := false
if cfg.Role == byte(4) {
validator = true
}

runtimeCtx := &Ctx{
storage: cfg.Storage,
allocator: memAllocator,
keystore: cfg.Keystore,
validator: validator,
}

logger.Debug("NewRuntime", "runtimeCtx", runtimeCtx)
Expand Down
26 changes: 26 additions & 0 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ func NewTestRuntimeWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, l
return r
}

// NewTestRuntimeWithRole returns a test runtime with given role value
func NewTestRuntimeWithRole(t *testing.T, targetRuntime string, role byte) *Runtime {
testRuntimeFilePath, testRuntimeURL, importsFunc := GetRuntimeVars(targetRuntime)

_, err := GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL)
require.Nil(t, err, "Fail: could not get runtime", "targetRuntime", targetRuntime)

s := newTestRuntimeStorage(nil)

fp, err := filepath.Abs(testRuntimeFilePath)
require.Nil(t, err, "could not create testRuntimeFilePath", "targetRuntime", targetRuntime)

cfg := &Config{
Storage: s,
Keystore: keystore.NewGenericKeystore("test"),
Imports: importsFunc,
LogLvl: log.LvlInfo,
Role: role,
}

r, err := NewRuntimeFromFile(fp, cfg)
require.Nil(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
}

// exportRuntime writes the runtime to a file as a hex string.
// nolint (without this the linter complains that exportRuntime is unused (used in helper.test.go 28)
func exportRuntime(t *testing.T, targetRuntime string, outFp string) {
Expand Down