Skip to content

Commit

Permalink
lib/runtime: implement ext_submit_transaction (#1120)
Browse files Browse the repository at this point in the history
* stub test for ext_submit_transaction

* implement ext_submit_transaction

* fix test

* fix tests

* address comments
  • Loading branch information
edwardmack authored Oct 8, 2020
1 parent 56dfb4e commit 1e9a7ac
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 15 deletions.
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9 h1:HD8gA2tkBy
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ChainSafe/chaindb v0.0.1 h1:A+11ulFG6R/IE6pEBy6j6hTTHTA3BvCVK8jklogW0Uw=
github.com/ChainSafe/chaindb v0.0.1/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/chaindb v0.1.0 h1:mvVC8oWkgEO2VOOdy55SpdhKeVjZnsDLlIIuOGYS4sQ=
github.com/ChainSafe/chaindb v0.1.0/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/chaindb v0.1.1-0.20201006203234-477aea6e1e64 h1:c9eEZ8DLgSLAV8a+WTDhEQvLCtF0l5f0GL8vMniOxcY=
github.com/ChainSafe/chaindb v0.1.1-0.20201006203234-477aea6e1e64/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/chaindb v0.1.1 h1:n12h2KZLgLSoOshCMG5XYoaZljJNSXQaV/fOCIVTJl4=
github.com/ChainSafe/chaindb v0.1.1/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/chaindb v0.1.2 h1:9zYNvop0GLIkoRFhzgCJVizhTxGWbD7M5BNGLjSqHvQ=
github.com/ChainSafe/chaindb v0.1.2/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/chaindb v0.1.3-0.20201006205605-8b0172911282 h1:Etc0yOcxRJBm61JMUwf2NoOKN9LEJM79h/d8kSyRfmg=
github.com/ChainSafe/chaindb v0.1.3-0.20201006205605-8b0172911282/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/chaindb v0.1.3 h1:yeZbB3J+xEWXLizxm3cuwhfV97NRbZnP2oB/2nKQw5A=
github.com/ChainSafe/chaindb v0.1.3/go.mod h1:FQQJbGzWe+rOa0aW/wSMz1qfIScauntW0HiynEIezuk=
github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg=
Expand Down
5 changes: 5 additions & 0 deletions lib/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,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
}
2 changes: 2 additions & 0 deletions lib/runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type InstanceConfig struct {
Role byte
NodeStorage NodeStorage
Network BasicNetwork
Transaction TransactionState
}

// Context is the context for the wasm interpreter's imported functions
Expand All @@ -81,6 +82,7 @@ type Context struct {
Validator bool
NodeStorage NodeStorage
Network BasicNetwork
Transaction TransactionState
}

// Version struct
Expand Down
16 changes: 15 additions & 1 deletion lib/runtime/wasmer/imports_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ 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/runtime"
"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 @@ -833,7 +835,19 @@ 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().(*runtime.Context)

extBytes := memory[data : data+len]

ext := types.Extrinsic(extBytes)

// validate the transaction
txv := transaction.NewValidity(0, [][]byte{{}}, [][]byte{{}}, 0, false)
vtx := transaction.NewValidTransaction(ext, txv)

runtimeCtx.Transaction.AddToPool(vtx)
return 0
}

Expand Down
21 changes: 21 additions & 0 deletions lib/runtime/wasmer/imports_old_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,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))
instance := NewTestInstance(t, runtime.TEST_RUNTIME)
memory := instance.vm.Memory.Data()

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

dataPtr, err := instance.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())
}
2 changes: 1 addition & 1 deletion lib/runtime/wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sync"

"github.com/ChainSafe/gossamer/lib/runtime"

log "github.com/ChainSafe/log15"
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
)
Expand Down Expand Up @@ -93,6 +92,7 @@ func NewInstance(code []byte, cfg *Config) (*Instance, error) {
Validator: cfg.Role == byte(4),
NodeStorage: cfg.NodeStorage,
Network: cfg.Network,
Transaction: cfg.Transaction,
}

logger.Debug("NewInstance", "runtimeCtx", runtimeCtx)
Expand Down
12 changes: 11 additions & 1 deletion lib/runtime/wasmer/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"testing"

database "github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/lib/trie"

log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"
wasm "github.com/wasmerio/go-ext-wasm/wasmer"
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie,
cfg.LogLvl = lvl
cfg.NodeStorage = ns
cfg.Network = new(runtime.TestRuntimeNetwork)
cfg.Transaction = new(mockTransactionState)

r, err := NewInstanceFromFile(fp, cfg)
require.Nil(t, err, "Got error when trying to create new VM", "targetRuntime", targetRuntime)
Expand Down Expand Up @@ -111,3 +113,11 @@ func GetRuntimeImports(targetRuntime string) func() (*wasm.Imports, error) {

return registerImports
}

type mockTransactionState struct {
}

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

0 comments on commit 1e9a7ac

Please sign in to comment.