From 82d7a64b5b7d3b3757328781794caf35143ddea9 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Sat, 3 Oct 2020 10:51:36 -0400 Subject: [PATCH 1/5] stub test for ext_submit_transaction --- lib/runtime/imports_old_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/runtime/imports_old_test.go b/lib/runtime/imports_old_test.go index a765f18d98..e932a48811 100644 --- a/lib/runtime/imports_old_test.go +++ b/lib/runtime/imports_old_test.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/binary" "encoding/hex" + "fmt" "math/big" "sort" "testing" @@ -1292,3 +1293,20 @@ func TestExt_network_state(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedEnc, resData) } + +func TestExt_submit_transaction(t *testing.T) { + runtime := NewTestRuntime(t, TEST_RUNTIME) + //memory := runtime.vm.Memory.Data() + + testFunc, ok := runtime.vm.Exports["test_ext_submit_transaction"] + if !ok { + t.Fatal("could not find exported function") + } + + writtenOutPtr, err := runtime.ctx.allocator.Allocate(4) + require.NoError(t, err) + + res, err := testFunc(int32(writtenOutPtr), int32(0)) + require.NoError(t, err) + fmt.Printf("result %v\n", res) +} From b34f1095a3d17c5c857382a1923cc144f1bb64d5 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Sat, 3 Oct 2020 12:00:30 -0400 Subject: [PATCH 2/5] implement ext_submit_transaction --- dot/core/service.go | 1 + dot/rpc/modules/author_test.go | 2 +- dot/services.go | 1 + lib/runtime/imports_old.go | 17 ++++++++++++++++- lib/runtime/imports_old_test.go | 13 ++++++++----- lib/runtime/interface.go | 6 ++++++ lib/runtime/runtime.go | 3 +++ lib/runtime/test_helpers.go | 10 ++++++++++ 8 files changed, 46 insertions(+), 7 deletions(-) diff --git a/dot/core/service.go b/dot/core/service.go index 8c9d84a35b..eacf6f1ea0 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -388,6 +388,7 @@ func (s *Service) handleRuntimeChanges(header *types.Header) error { LogLvl: -1, // don't change runtime package log level NodeStorage: s.rt.NodeStorage(), Network: s.rt.NetworkService(), + Transaction: s.transactionState, } s.rt, err = runtime.NewRuntime(code, cfg) diff --git a/dot/rpc/modules/author_test.go b/dot/rpc/modules/author_test.go index ec978f5a94..9276b04650 100644 --- a/dot/rpc/modules/author_test.go +++ b/dot/rpc/modules/author_test.go @@ -60,7 +60,7 @@ func TestAuthorModule_Pending(t *testing.T) { } func TestAuthorModule_SubmitExtrinsic(t *testing.T) { - t.Skip() + //t.Skip() // setup auth module txQueue := state.NewTransactionState() diff --git a/dot/services.go b/dot/services.go index 358c07b5cb..ca219c918c 100644 --- a/dot/services.go +++ b/dot/services.go @@ -91,6 +91,7 @@ func createRuntime(cfg *Config, st *state.Service, ks *keystore.GenericKeystore, NodeStorage: ns, Role: cfg.Core.Roles, Network: net, + Transaction: st.Transaction, } // create runtime executor diff --git a/lib/runtime/imports_old.go b/lib/runtime/imports_old.go index 2acd67ef1b..deaefe8af4 100644 --- a/lib/runtime/imports_old.go +++ b/lib/runtime/imports_old.go @@ -62,10 +62,12 @@ import ( "math/big" "unsafe" + "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/lib/scale" + "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/lib/trie" "github.com/OneOfOne/xxhash" @@ -798,7 +800,20 @@ func ext_network_state(context unsafe.Pointer, writtenOut int32) int32 { //export ext_submit_transaction func ext_submit_transaction(context unsafe.Pointer, data, len int32) int32 { logger.Trace("[ext_submit_transaction] executing...") - logger.Warn("[ext_submit_transaction] Not yet implemented.") + instanceContext := wasm.IntoInstanceContext(context) + memory := instanceContext.Memory().Data() + runtimeCtx := instanceContext.Data().(*Ctx) + + extBytes := memory[data : data+len] + + ext := types.Extrinsic(extBytes) + + // validate the transaction + txv := transaction.NewValidity(0, [][]byte{{}}, [][]byte{{}}, 0, false) + // todo determine how to validate transaction here, calling runtimeAPI.ValidateTransaction? + vtx := transaction.NewValidTransaction(ext, txv) + + runtimeCtx.transaction.AddToPool(vtx) return 0 } diff --git a/lib/runtime/imports_old_test.go b/lib/runtime/imports_old_test.go index e932a48811..3b3b762617 100644 --- a/lib/runtime/imports_old_test.go +++ b/lib/runtime/imports_old_test.go @@ -5,7 +5,6 @@ import ( "crypto/rand" "encoding/binary" "encoding/hex" - "fmt" "math/big" "sort" "testing" @@ -1295,18 +1294,22 @@ func TestExt_network_state(t *testing.T) { } func TestExt_submit_transaction(t *testing.T) { + // https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95 + var data = []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 5, 113, 87, 87, 40, 221, 120, 247, 252, 137, 201, 74, 231, 222, 101, 85, 108, 102, 39, 31, 190, 210, 14, 215, 124, 19, 160, 180, 203, 54, 110, 167, 163, 149, 45, 12, 108, 80, 221, 65, 238, 57, 237, 199, 16, 10, 33, 185, 8, 244, 184, 243, 139, 5, 87, 252, 245, 24, 225, 37, 154, 163, 142} + dataLen := uint32(len(data)) runtime := NewTestRuntime(t, TEST_RUNTIME) - //memory := runtime.vm.Memory.Data() + memory := runtime.vm.Memory.Data() testFunc, ok := runtime.vm.Exports["test_ext_submit_transaction"] if !ok { t.Fatal("could not find exported function") } - writtenOutPtr, err := runtime.ctx.allocator.Allocate(4) + dataPtr, err := runtime.ctx.allocator.Allocate(dataLen) require.NoError(t, err) + copy(memory[dataPtr:dataPtr+dataLen], data) - res, err := testFunc(int32(writtenOutPtr), int32(0)) + res, err := testFunc(int32(dataPtr), int32(dataLen)) require.NoError(t, err) - fmt.Printf("result %v\n", res) + require.Equal(t, 0, res) } diff --git a/lib/runtime/interface.go b/lib/runtime/interface.go index 718ac935af..58994627ad 100644 --- a/lib/runtime/interface.go +++ b/lib/runtime/interface.go @@ -18,6 +18,7 @@ package runtime import ( "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/lib/trie" ) @@ -47,3 +48,8 @@ type BasicStorage interface { Put(key []byte, value []byte) error Get(key []byte) ([]byte, error) } + +// TransactionState interface for adding transactions to pool +type TransactionState interface { + AddToPool(vt *transaction.ValidTransaction) common.Hash +} diff --git a/lib/runtime/runtime.go b/lib/runtime/runtime.go index 4d0dbea27a..a708efaeaf 100644 --- a/lib/runtime/runtime.go +++ b/lib/runtime/runtime.go @@ -49,6 +49,7 @@ type Ctx struct { validator bool nodeStorage NodeStorage network BasicNetwork + transaction TransactionState } // Config represents a runtime configuration @@ -60,6 +61,7 @@ type Config struct { Role byte NodeStorage NodeStorage Network BasicNetwork + Transaction TransactionState } // Runtime struct @@ -122,6 +124,7 @@ func NewRuntime(code []byte, cfg *Config) (*Runtime, error) { validator: validator, nodeStorage: cfg.NodeStorage, network: cfg.Network, + transaction: cfg.Transaction, } logger.Debug("NewRuntime", "runtimeCtx", runtimeCtx) diff --git a/lib/runtime/test_helpers.go b/lib/runtime/test_helpers.go index ed25ee5e64..58b2219223 100644 --- a/lib/runtime/test_helpers.go +++ b/lib/runtime/test_helpers.go @@ -29,6 +29,7 @@ import ( database "github.com/ChainSafe/chaindb" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/keystore" + "github.com/ChainSafe/gossamer/lib/transaction" "github.com/ChainSafe/gossamer/lib/trie" "github.com/ChainSafe/gossamer/lib/utils" log "github.com/ChainSafe/log15" @@ -68,6 +69,7 @@ func NewTestRuntimeWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, l LogLvl: lvl, NodeStorage: ns, Network: new(testRuntimeNetwork), + Transaction: new(mockTransaction), } r, err := NewRuntimeFromFile(fp, cfg) @@ -307,3 +309,11 @@ func (trn testRuntimeNetwork) NetworkState() common.NetworkState { Multiaddrs: testAddrs, } } + +type mockTransaction struct { +} + +// AddToPool adds a transaction to the pool +func (mt *mockTransaction) AddToPool(vt *transaction.ValidTransaction) common.Hash { + return common.BytesToHash([]byte("test")) +} From ba57fe17c4134cfd3f1eb7673706b36e9a0af253 Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Sat, 3 Oct 2020 12:03:41 -0400 Subject: [PATCH 3/5] fix test --- lib/runtime/imports_old_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime/imports_old_test.go b/lib/runtime/imports_old_test.go index 3b3b762617..631f565c46 100644 --- a/lib/runtime/imports_old_test.go +++ b/lib/runtime/imports_old_test.go @@ -1311,5 +1311,5 @@ func TestExt_submit_transaction(t *testing.T) { res, err := testFunc(int32(dataPtr), int32(dataLen)) require.NoError(t, err) - require.Equal(t, 0, res) + require.Equal(t, int32(0), res.ToI32()) } From 17ce42349c176bfbb1521556388f0c52efa9d94b Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Sun, 4 Oct 2020 09:44:44 -0400 Subject: [PATCH 4/5] fix tests --- dot/rpc/modules/author_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dot/rpc/modules/author_test.go b/dot/rpc/modules/author_test.go index 9276b04650..ec978f5a94 100644 --- a/dot/rpc/modules/author_test.go +++ b/dot/rpc/modules/author_test.go @@ -60,7 +60,7 @@ func TestAuthorModule_Pending(t *testing.T) { } func TestAuthorModule_SubmitExtrinsic(t *testing.T) { - //t.Skip() + t.Skip() // setup auth module txQueue := state.NewTransactionState() From 8c84c1de31c4799a34bf62e6ed76f966c5aef79d Mon Sep 17 00:00:00 2001 From: Edward Mack Date: Wed, 7 Oct 2020 10:48:55 -0400 Subject: [PATCH 5/5] address comments --- lib/runtime/wasmer/imports_old.go | 1 - lib/runtime/wasmer/test_helpers.go | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/runtime/wasmer/imports_old.go b/lib/runtime/wasmer/imports_old.go index 9bc8b7acf5..f73d74b2ec 100644 --- a/lib/runtime/wasmer/imports_old.go +++ b/lib/runtime/wasmer/imports_old.go @@ -845,7 +845,6 @@ func ext_submit_transaction(context unsafe.Pointer, data, len int32) int32 { // validate the transaction txv := transaction.NewValidity(0, [][]byte{{}}, [][]byte{{}}, 0, false) - // todo determine how to validate transaction here, calling runtimeAPI.ValidateTransaction? vtx := transaction.NewValidTransaction(ext, txv) runtimeCtx.Transaction.AddToPool(vtx) diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index 7f87de35a1..ea62c7c67f 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -71,7 +71,7 @@ func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie, LogLvl: lvl, NodeStorage: ns, Network: new(testRuntimeNetwork), - Transaction: new(mockTransaction), + Transaction: new(mockTransactionState), } r, err := NewInstanceFromFile(fp, cfg) @@ -312,10 +312,10 @@ func (trn testRuntimeNetwork) NetworkState() common.NetworkState { } } -type mockTransaction struct { +type mockTransactionState struct { } // AddToPool adds a transaction to the pool -func (mt *mockTransaction) AddToPool(vt *transaction.ValidTransaction) common.Hash { +func (mt *mockTransactionState) AddToPool(vt *transaction.ValidTransaction) common.Hash { return common.BytesToHash([]byte("test")) }