diff --git a/dot/services.go b/dot/services.go index 1c6bbc78ea..76c2b8e707 100644 --- a/dot/services.go +++ b/dot/services.go @@ -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 diff --git a/lib/runtime/imports_old.go b/lib/runtime/imports_old.go index 6a7774ecd4..cf070c362a 100644 --- a/lib/runtime/imports_old.go +++ b/lib/runtime/imports_old.go @@ -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 } diff --git a/lib/runtime/imports_old_test.go b/lib/runtime/imports_old_test.go index 5824c92b9e..a993d22399 100644 --- a/lib/runtime/imports_old_test.go +++ b/lib/runtime/imports_old_test.go @@ -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" ) @@ -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()) + +} diff --git a/lib/runtime/runtime.go b/lib/runtime/runtime.go index ece6971499..f7f89c70f4 100644 --- a/lib/runtime/runtime.go +++ b/lib/runtime/runtime.go @@ -34,6 +34,7 @@ type Ctx struct { storage Storage allocator *FreeingBumpHeapAllocator keystore *keystore.GenericKeystore + validator bool } // Config represents a runtime configuration @@ -42,6 +43,7 @@ type Config struct { Keystore *keystore.GenericKeystore Imports func() (*wasm.Imports, error) LogLvl log.Lvl + Role byte } // Runtime struct @@ -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) diff --git a/lib/runtime/test_helpers.go b/lib/runtime/test_helpers.go index 8c4a4223fa..595c5f2a3c 100644 --- a/lib/runtime/test_helpers.go +++ b/lib/runtime/test_helpers.go @@ -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) {