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

lib/runtime: implement ext_submit_transaction #1120

Merged
merged 8 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions dot/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion lib/runtime/imports_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ import (
"reflect"
"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"
Expand Down Expand Up @@ -832,7 +834,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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove todo, it's good as is!

vtx := transaction.NewValidTransaction(ext, txv)

runtimeCtx.transaction.AddToPool(vtx)
return 0
}

Expand Down
21 changes: 21 additions & 0 deletions lib/runtime/imports_old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1391,3 +1391,24 @@ func TestExt_network_state(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expectedEnc, resData)
}

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()

testFunc, ok := runtime.vm.Exports["test_ext_submit_transaction"]
if !ok {
t.Fatal("could not find exported function")
}

dataPtr, err := runtime.ctx.allocator.Allocate(dataLen)
require.NoError(t, err)
copy(memory[dataPtr:dataPtr+dataLen], data)

res, err := testFunc(int32(dataPtr), int32(dataLen))
require.NoError(t, err)
require.Equal(t, int32(0), res.ToI32())
}
6 changes: 6 additions & 0 deletions lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions lib/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Ctx struct {
validator bool
nodeStorage NodeStorage
network BasicNetwork
transaction TransactionState
}

// Config represents a runtime configuration
Expand All @@ -63,6 +64,7 @@ type Config struct {
Role byte
NodeStorage NodeStorage
Network BasicNetwork
Transaction TransactionState
}

// Runtime struct
Expand Down Expand Up @@ -125,6 +127,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)
Expand Down
10 changes: 10 additions & 0 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -307,3 +309,11 @@ func (trn testRuntimeNetwork) NetworkState() common.NetworkState {
Multiaddrs: testAddrs,
}
}

type mockTransaction struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to mockTransactionState?

}

// AddToPool adds a transaction to the pool
func (mt *mockTransaction) AddToPool(vt *transaction.ValidTransaction) common.Hash {
return common.BytesToHash([]byte("test"))
}