Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(lib/runtime): implement ext_offchain_timestamp and ext_offchain_sleep_until #1806

Merged
merged 37 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d8e58af
Issue#1508 First commit
omar391 Sep 23, 2021
8aa681f
Changed interface name + Added testcase
omar391 Sep 24, 2021
b4e37e3
Removed leftover comments
omar391 Sep 24, 2021
17074a4
Updated host api runtime + Added testcase
omar391 Sep 29, 2021
349be93
add support for polkadot runtime v0.9.10
noot Sep 30, 2021
2ac20ef
cleanup
noot Sep 30, 2021
6b5eee1
Merge branch 'development' of github.com:ChainSafe/gossamer into noot…
noot Sep 30, 2021
844d8fc
Merge branch 'development' into noot/runtime-v0.9.10
noot Oct 1, 2021
a7e5fe4
Merge commit '844d8fce1c4ced24f175a0b3b8fdc3d4bf6f48f5' into omar/off…
omar391 Oct 1, 2021
9d1d400
Removed testcases temporarily
omar391 Oct 5, 2021
86f8685
Updated base Go version to 1.17 to resolve time.UnixMilli issues
omar391 Oct 5, 2021
3698b5b
Merge remote-tracking branch 'origin/development' into omar/offchain
omar391 Oct 5, 2021
92bd920
Updated go.mod
omar391 Oct 5, 2021
8805471
Removed unused context
omar391 Oct 5, 2021
2f9e737
Updated go-version to 1.17.x in github workflows
omar391 Oct 6, 2021
4ee104e
Updated go version in the Dockerfile
omar391 Oct 7, 2021
85b6174
Merge branch 'development' into omar/offchain
noot Oct 7, 2021
8d1a302
Merge remote-tracking branch 'origin/development' into omar/offchain
omar391 Oct 11, 2021
0e6ef50
Merge branch 'omar/offchain' of https://github.com/ChainSafe/gossamer…
omar391 Oct 11, 2021
36f53e0
added .devcontainer in the gitignore
omar391 Oct 15, 2021
1eee3ac
Merge branch 'development' into omar/offchain
omar391 Oct 15, 2021
2c75ba5
Linked related issue for the unimplemented unit test
omar391 Oct 15, 2021
acf619c
Merge branch 'development' into omar/offchain
noot Oct 18, 2021
70a34f2
Merge branch 'development' into omar/offchain
noot Oct 19, 2021
9d0e095
Merge branch 'development' into omar/offchain
omar391 Oct 20, 2021
c53335e
Merge remote-tracking branch 'origin/development' into omar/offchain
omar391 Oct 21, 2021
8e6a01e
Merge remote-tracking branch 'origin/development' into omar/offchain
omar391 Oct 29, 2021
9872f2c
Fixed CGO issues
omar391 Nov 1, 2021
49ef7d2
Fixed CGO issues
omar391 Nov 1, 2021
de896a6
Merge remote-tracking branch 'origin/development' into omar/offchain
omar391 Nov 1, 2021
b7cd008
Merge branch 'development' into omar/offchain
omar391 Nov 2, 2021
120859e
Fixed lint issues
omar391 Nov 2, 2021
332f054
Merge branch 'development' into omar/offchain
omar391 Nov 4, 2021
6b1e120
Merge branch 'development' into omar/offchain
omar391 Nov 10, 2021
8f60a53
Merge branch 'development' into omar/offchain
omar391 Nov 12, 2021
459f4a8
Fixed unit tests
omar391 Nov 12, 2021
d1a06b3
Merge branch 'development' into omar/offchain
omar391 Nov 12, 2021
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
7 changes: 4 additions & 3 deletions lib/runtime/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const (
POLKADOT_RUNTIME_URL = "https://github.com/noot/polkadot/blob/noot/v0.8.25/polkadot_runtime.wasm?raw=true"

// v0.9 test API wasm
HOST_API_TEST_RUNTIME = "hostapi_runtime"
HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm"
HOST_API_TEST_RUNTIME_URL = "https://github.com/ChainSafe/polkadot-spec/blob/b94d8c58ad6ea8bf827b0cae1645a999719c2bc7/test/runtimes/hostapi/hostapi_runtime.compact.wasm?raw=true"
HOST_API_TEST_RUNTIME = "hostapi_runtime"
HOST_API_TEST_RUNTIME_FP = "hostapi_runtime.compact.wasm"
// HOST_API_TEST_RUNTIME_URL = "https://github.com/ChainSafe/polkadot-spec/blob/b94d8c58ad6ea8bf827b0cae1645a999719c2bc7/test/runtimes/hostapi/hostapi_runtime.compact.wasm?raw=true"
HOST_API_TEST_RUNTIME_URL = "https://github.com/ChainSafe/polkadot-spec/blob/omar/offchain/test/runtimes/hostapi/hostapi_runtime.compact.wasm?raw=true"

// v0.8 substrate runtime with modified name and babe C=(1, 1)
DEV_RUNTIME = "dev_runtime"
Expand Down
16 changes: 11 additions & 5 deletions lib/runtime/wasmer/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import (
"math/big"
"math/rand"
"reflect"
"time"
"unsafe"

"github.com/ChainSafe/gossamer/internal/log"
Expand Down Expand Up @@ -1699,16 +1700,21 @@ func ext_offchain_submit_transaction_version_1(context unsafe.Pointer, data C.in
}

//export ext_offchain_timestamp_version_1
func ext_offchain_timestamp_version_1(context unsafe.Pointer) C.int64_t {
func ext_offchain_timestamp_version_1(_ unsafe.Pointer) C.int64_t {
logger.Trace("[ext_offchain_timestamp_version_1] executing...")
logger.Warn("[ext_offchain_timestamp_version_1] unimplemented")
return 0

now := time.Now().Unix()
return C.int64_t(now)
}

//export ext_offchain_sleep_until_version_1
func ext_offchain_sleep_until_version_1(_ unsafe.Pointer, deadline C.int64_t) {
logger.Trace("executing...")
logger.Warn("unimplemented")
logger.Trace("[ext_offchain_sleep_until_version_1] executing...")

dur := time.Until(time.UnixMilli(int64(deadline)))
if dur > 0 {
time.Sleep(dur)
}
}

//export ext_offchain_http_request_start_version_1
Expand Down
33 changes: 31 additions & 2 deletions lib/runtime/wasmer/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"sort"
"testing"
"time"

"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/internal/log"
Expand Down Expand Up @@ -47,6 +48,35 @@ func TestMain(m *testing.M) {
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"]
require.True(t, ok)

res, err := runtimeFunc(0, 0)
require.NoError(t, err)

offset, length := runtime.Int64ToPointerAndSize(res.ToI64())
data := inst.load(offset, length)
var timestamp int64
err = scale.Unmarshal(data, &timestamp)
require.NoError(t, err)

expected := time.Now().Unix()
require.GreaterOrEqual(t, expected, timestamp)
}

func Test_ext_offchain_sleep_until_version_1(t *testing.T) {
inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME)

input := time.Now().UnixMilli()
enc, err := scale.Marshal(input)
require.NoError(t, err)

_, err = inst.Exec("rtm_ext_offchain_sleep_until_version_1", enc) //auto conversion to i64
require.NoError(t, err)
}

omar391 marked this conversation as resolved.
Show resolved Hide resolved
func Test_ext_hashing_blake2_128_version_1(t *testing.T) {
t.Parallel()
inst := NewTestInstance(t, runtime.HOST_API_TEST_RUNTIME)
Expand Down Expand Up @@ -1454,8 +1484,7 @@ func Test_ext_default_child_storage_storage_kill_version_2_limit_none(t *testing
encChildKey, err := scale.Marshal(testChildKey)
require.NoError(t, err)

var val *[]byte // nolint
val = nil
var val *[]byte
optLimit, err := scale.Marshal(val)
require.NoError(t, err)

Expand Down