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

feat: dot/rpc: Add support for submit and watch extrinisic #1415

Merged
merged 32 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4ba1378
add check for call to websocket
edwardmack Feb 24, 2021
7cdd898
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Feb 25, 2021
f1b678e
stub rpc author_submitAndWatchExtrinsic
edwardmack Feb 25, 2021
980da58
add block import listener
edwardmack Feb 25, 2021
42c3eb6
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Feb 28, 2021
a066327
implement state_subscribeRuntimeVersion
edwardmack Mar 1, 2021
276e61b
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 2, 2021
b03cde1
add test for apply extrinsic
edwardmack Mar 3, 2021
3e690dc
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 3, 2021
028fc81
implement inBlock status for author_submitAndWatchExtrinsic
edwardmack Mar 3, 2021
954d97d
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 4, 2021
804b316
handle submit extrinsic in HandleSubmittedExtrinsic
edwardmack Mar 4, 2021
604a0e4
remove print lines
edwardmack Mar 4, 2021
96d93f7
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 5, 2021
a4ac97b
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 5, 2021
ba25aaa
add unit tests
edwardmack Mar 9, 2021
b46d5ee
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 9, 2021
463f522
add tests for apply extrinsic
edwardmack Mar 9, 2021
4e5a788
add test to build and apply extrinsic
edwardmack Mar 9, 2021
1d4a158
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 10, 2021
396e78b
add tests
edwardmack Mar 10, 2021
ea4c735
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 11, 2021
5d72c85
add subkey to tests.yml
edwardmack Mar 11, 2021
89d0f43
fix SubscriptionParams parameter
edwardmack Mar 11, 2021
fe7ee61
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 11, 2021
413f5bc
skip test failing in CI due to subkey setup
edwardmack Mar 11, 2021
59dd5d5
remove unused reference
edwardmack Mar 11, 2021
fc8d275
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 12, 2021
ed29076
address comments
edwardmack Mar 12, 2021
2785b45
remove unused comments
edwardmack Mar 12, 2021
5502907
change logging level for websocket issues
edwardmack Mar 12, 2021
8975f75
Merge branch 'development' into ed/author_submitAndWatchExtrinsic
edwardmack Mar 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
9 changes: 8 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('go.sum') }}
restore-keys: ${{ runner.os }}-go-

- name: Install Subkey
run: |
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
chmod +x $HOME/.local/bin/subkey
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Run unit tests
run: make test

Expand Down Expand Up @@ -147,4 +154,4 @@ jobs:
-
name: Run stress
run: |
docker run chainsafe/gossamer:test sh -c "make it-grandpa"
docker run chainsafe/gossamer:test sh -c "make it-grandpa"
14 changes: 14 additions & 0 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,20 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
return nil
}

// the transaction source is External
// validate the transaction
txv, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, ext...))
if err != nil {
return err
}

if s.isBlockProducer {
// add transaction to pool
vtx := transaction.NewValidTransaction(ext, txv)
s.transactionState.AddToPool(vtx)
edwardmack marked this conversation as resolved.
Show resolved Hide resolved
}

// broadcast transaction
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{ext}}
s.net.SendMessage(msg)
return nil
Expand Down
43 changes: 43 additions & 0 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,46 @@ func TestMaintainTransactionPool_BlockWithExtrinsics(t *testing.T) {
require.Equal(t, 1, len(res))
require.Equal(t, res[0], txs[1])
}

func TestService_GetRuntimeVersion(t *testing.T) {
s := NewTestService(t, nil)
rtExpected, err := s.rt.Version()
require.NoError(t, err)

rtv, err := s.GetRuntimeVersion(nil)
require.NoError(t, err)
require.Equal(t, rtExpected, rtv)
}

func TestService_IsBlockProducer(t *testing.T) {
cfg := &Config{
IsBlockProducer: false,
}
s := NewTestService(t, cfg)
bp := s.IsBlockProducer()
require.Equal(t, false, bp)
}

func TestService_HandleSubmittedExtrinsic(t *testing.T) {
s := NewTestService(t, nil)

parentHash := common.MustHexToHash("0x35a28a7dbaf0ba07d1485b0f3da7757e3880509edc8c31d0850cb6dd6219361d")
header, err := types.NewHeader(parentHash, big.NewInt(1), common.Hash{}, common.Hash{}, types.NewEmptyDigest())
require.NoError(t, err)

//initialize block header
err = s.rt.InitializeBlock(header)
require.NoError(t, err)

ext := types.Extrinsic(common.MustHexToBytes("0x410284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d015a3e258da3ea20581b68fe1264a35d1f62d6a0debb1a44e836375eb9921ba33e3d0f265f2da33c9ca4e10490b03918300be902fcb229f806c9cf99af4cc10f8c0000000600ff8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00c465f14670"))

err = s.HandleSubmittedExtrinsic(ext)
require.NoError(t, err)
}

func TestService_GetMetadata(t *testing.T) {
s := NewTestService(t, nil)
res, err := s.GetMetadata(nil)
require.NoError(t, err)
require.Greater(t, len(res), 10000)
}
17 changes: 11 additions & 6 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"

log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -128,10 +128,6 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
}
}

if cfg.Runtime == nil {
cfg.Runtime = wasmer.NewTestInstance(t, runtime.NODE_RUNTIME)
}

if cfg.Keystore == nil {
cfg.Keystore = keystore.NewGlobalKeystore()
kp, err := sr25519.GenerateKeypair()
Expand All @@ -155,11 +151,12 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
testDatadirPath, err := ioutil.TempDir("/tmp", "test-datadir-*")
require.NoError(t, err)

gen, genTrie, genHeader := newTestGenesisWithTrieAndHeader(t)

if cfg.BlockState == nil || cfg.StorageState == nil || cfg.TransactionState == nil || cfg.EpochState == nil {
stateSrvc = state.NewService(testDatadirPath, log.LvlInfo)
stateSrvc.UseMemDB()

gen, genTrie, genHeader := newTestGenesisWithTrieAndHeader(t)
err = stateSrvc.Initialize(gen, genHeader, genTrie)
require.Nil(t, err)

Expand All @@ -183,6 +180,14 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
cfg.EpochState = stateSrvc.Epoch
}

if cfg.Runtime == nil {
rtCfg := &wasmer.Config{}
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
require.NoError(t, err)
cfg.Runtime, err = wasmer.NewRuntimeFromGenesis(gen, rtCfg)
require.NoError(t, err)
}

if cfg.Network == nil {
config := &network.Config{
BasePath: testDatadirPath,
Expand Down
3 changes: 3 additions & 0 deletions dot/rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ type WSConn struct {
subscriptions map[int]Listener
storageAPI modules.StorageAPI
blockAPI modules.BlockAPI
runtimeAPI modules.RuntimeAPI
coreAPI modules.CoreAPI
txStateAPI modules.TransactionStateAPI
}

var logger log.Logger
Expand Down
28 changes: 3 additions & 25 deletions dot/rpc/modules/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/transaction"
log "github.com/ChainSafe/log15"
)

Expand Down Expand Up @@ -169,31 +168,10 @@ func (cm *AuthorModule) SubmitExtrinsic(r *http.Request, req *Extrinsic, res *Ex
if err != nil {
return err
}
cm.logger.Trace("[rpc]", "extrinsic", extBytes)
ext := types.Extrinsic(extBytes)
cm.logger.Trace("[rpc]", "extrinsic", ext)

// For RPC request the transaction source is External
ext := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, extBytes...))

// validate the transaction
txv, err := cm.runtimeAPI.ValidateTransaction(ext)
if err != nil {
cm.logger.Warn("failed to validate transaction", "ext", ext)
return err
}

vtx := transaction.NewValidTransaction(ext, txv)

if cm.coreAPI.IsBlockProducer() {
hash := cm.txStateAPI.AddToPool(vtx)
*res = ExtrinsicHashResponse(hash.String())
cm.logger.Trace("submitted extrinsic", "tx", vtx, "hash", hash.String())
}

//broadcast
err = cm.coreAPI.HandleSubmittedExtrinsic(ext)
if err != nil {
cm.logger.Trace("failed to submit extrinsic to network", "error", err)
}

*res = ExtrinsicHashResponse(ext.Hash().String())
return err
}
3 changes: 2 additions & 1 deletion dot/rpc/modules/author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
Expand All @@ -20,7 +21,7 @@ import (
)

// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
var testExt = []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}
var testExt = common.MustHexToBytes("0x410284ffd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01f8efbe48487e57a22abf7e3acd491b7f3528a33a111b1298601554863d27eb129eaa4e718e1365414ff3d028b62bebc651194c6b5001e5c2839b982757e08a8c0000000600ff8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00c465f14670")

// invalid transaction (above tx, with last byte changed)
//nolint
Expand Down
5 changes: 3 additions & 2 deletions dot/rpc/modules/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (sm *StateModule) GetRuntimeVersion(r *http.Request, req *StateRuntimeVersi
res.SpecVersion = rtVersion.SpecVersion()
res.ImplVersion = rtVersion.ImplVersion()
res.TransactionVersion = rtVersion.TransactionVersion()
res.Apis = convertAPIs(rtVersion.APIItems())
res.Apis = ConvertAPIs(rtVersion.APIItems())

return nil
}
Expand Down Expand Up @@ -404,7 +404,8 @@ func (sm *StateModule) SubscribeStorage(r *http.Request, req *StateStorageQueryR
return nil
}

func convertAPIs(in []*runtime.APIItem) []interface{} {
// ConvertAPIs runtime.APIItems to []interface
func ConvertAPIs(in []*runtime.APIItem) []interface{} {
ret := make([]interface{}, 0)
for _, item := range in {
encStr := hex.EncodeToString(item.Name[:])
Expand Down
Loading