Skip to content

Commit

Permalink
test: dot: test transfer extrinsic (#1448)
Browse files Browse the repository at this point in the history
  • Loading branch information
arijitAD authored Mar 22, 2021
1 parent 09df0c3 commit 68ac803
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 121 deletions.
12 changes: 4 additions & 8 deletions dot/rpc/modules/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type ExtrinsicOrHashRequest []ExtrinsicOrHash
type KeyInsertResponse []byte

// PendingExtrinsicsResponse is a bi-dimensional array of bytes for allocating the pending extrisics
type PendingExtrinsicsResponse [][]byte
type PendingExtrinsicsResponse []string

// RemoveExtrinsicsResponse is a array of hash used to Remove extrinsics
type RemoveExtrinsicsResponse []common.Hash
Expand Down Expand Up @@ -134,13 +134,9 @@ func (cm *AuthorModule) HasKey(r *http.Request, req *[]string, res *bool) error
// PendingExtrinsics Returns all pending extrinsics
func (cm *AuthorModule) PendingExtrinsics(r *http.Request, req *EmptyRequest, res *PendingExtrinsicsResponse) error {
pending := cm.txStateAPI.Pending()
resp := [][]byte{}
for _, tx := range pending {
enc, err := tx.Encode()
if err != nil {
return err
}
resp = append(resp, enc)
resp := make([]string, len(pending))
for idx, tx := range pending {
resp[idx] = common.BytesToHex(tx.Extrinsic)
}

*res = PendingExtrinsicsResponse(resp)
Expand Down
14 changes: 5 additions & 9 deletions dot/rpc/modules/author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func TestAuthorModule_Pending(t *testing.T) {
t.Fatal(err)
}

if !reflect.DeepEqual(*res, PendingExtrinsicsResponse([][]byte{})) {
t.Errorf("Fail: expected: %+v got: %+v\n", res, &[][]byte{})
if !reflect.DeepEqual(*res, PendingExtrinsicsResponse([]string{})) {
t.Errorf("Fail: expected: %+v got: %+v\n", *res, PendingExtrinsicsResponse([]string{}))
}

vtx := &transaction.ValidTransaction{
Expand All @@ -68,13 +68,9 @@ func TestAuthorModule_Pending(t *testing.T) {
t.Fatal(err)
}

expected, err := vtx.Encode()
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(*res, PendingExtrinsicsResponse([][]byte{expected})) {
t.Errorf("Fail: expected: %+v got: %+v\n", res, &[][]byte{expected})
expected := common.BytesToHex(vtx.Extrinsic)
if !reflect.DeepEqual(*res, PendingExtrinsicsResponse([]string{expected})) {
t.Errorf("Fail: expected: %+v got: %+v\n", res, PendingExtrinsicsResponse([]string{expected}))
}
}

Expand Down
1 change: 0 additions & 1 deletion lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ func (s *Service) playGrandpaRound() error {
// receive messages until current round is completable and previous round is finalizable
// and the last finalized block is greater than the best final candidate from the previous round
s.receiveMessages(func() bool {
//return false
if s.paused.Load().(bool) {
return true
}
Expand Down
6 changes: 3 additions & 3 deletions tests/rpc/rpc_02-author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ func TestAuthorSubmitExtrinsic(t *testing.T) {
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey, nil)
require.NoError(t, err)

var nonce uint32
ok, err := api.RPC.State.GetStorageLatest(key, &nonce)
var accInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accInfo)
require.NoError(t, err)
require.True(t, ok)

o := types.SignatureOptions{
BlockHash: genesisHash,
Era: types.ExtrinsicEra{IsImmortalEra: true},
GenesisHash: genesisHash,
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
Nonce: types.NewUCompactFromUInt(uint64(accInfo.Nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(0),
TransactionVersion: rv.TransactionVersion,
Expand Down
97 changes: 3 additions & 94 deletions tests/stress/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@
package stress

import (
"bytes"
"encoding/hex"
"fmt"
"math/rand"
"sync"
"testing"
"time"

"github.com/ChainSafe/gossamer/dot/rpc/modules"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime/extrinsic"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/tests/utils"

log "github.com/ChainSafe/log15"
Expand Down Expand Up @@ -109,16 +103,7 @@ func compareBlocksByNumber(t *testing.T, nodes []*utils.Node, num string) (map[c
}(node)
}

done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()

select {
case <-time.After(time.Second * 30):
case <-done:
}
wg.Wait()

var err error
if len(errs) != 0 {
Expand Down Expand Up @@ -240,8 +225,8 @@ func compareFinalizedHeadsWithRetry(t *testing.T, nodes []*utils.Node, round uin
return common.Hash{}, nil
}

func getPendingExtrinsics(t *testing.T, node *utils.Node) [][]byte { //nolint
respBody, err := utils.PostRPC(utils.AuthorSubmitExtrinsic, utils.NewEndpoint(node.RPCPort), "[]")
func getPendingExtrinsics(t *testing.T, node *utils.Node) []string { //nolint
respBody, err := utils.PostRPC(utils.AuthorPendingExtrinsics, utils.NewEndpoint(node.RPCPort), "[]")
require.NoError(t, err)

exts := new(modules.PendingExtrinsicsResponse)
Expand All @@ -250,79 +235,3 @@ func getPendingExtrinsics(t *testing.T, node *utils.Node) [][]byte { //nolint

return *exts
}

// submitExtrinsicAssertInclusion submits an extrinsic to a random node and asserts that the extrinsic was included in some block
// and that the nodes remain synced
func submitExtrinsicAssertInclusion(t *testing.T, nodes []*utils.Node, ext extrinsic.Extrinsic) { //nolint
tx, err := ext.Encode()
require.NoError(t, err)

txStr := hex.EncodeToString(tx)
logger.Info("submitting transaction", "tx", txStr)

// send extrinsic to random node
idx := rand.Intn(len(nodes)) //nolint
prevHeader := utils.GetChainHead(t, nodes[idx]) // get starting header so that we can lookup blocks by number later
respBody, err := utils.PostRPC(utils.AuthorSubmitExtrinsic, utils.NewEndpoint(nodes[idx].RPCPort), "\"0x"+txStr+"\"")
require.NoError(t, err)

var hash modules.ExtrinsicHashResponse
err = utils.DecodeRPC(t, respBody, &hash)
require.Nil(t, err)
log.Info("submitted transaction", "hash", hash, "node", nodes[idx].Key)
t.Logf("submitted transaction to node %s", nodes[idx].Key)

// wait for nodes to build block + sync, then get headers
time.Sleep(time.Second * 10)

for i := 0; i < maxRetries; i++ {
exts := getPendingExtrinsics(t, nodes[idx])
if len(exts) == 0 {
break
}

time.Sleep(time.Second)
}

header := utils.GetChainHead(t, nodes[idx])
logger.Info("got header from node", "header", header, "hash", header.Hash(), "node", nodes[idx].Key)

// search from child -> parent blocks for extrinsic
var resExts []types.Extrinsic
i := 0
for header.ExtrinsicsRoot == trie.EmptyHash && i != maxRetries {
// check all nodes, since it might have been included on any of the block producers
var block *types.Block

for j := 0; j < len(nodes); j++ {
block = utils.GetBlock(t, nodes[j], header.ParentHash)
if block == nil {
// couldn't get block, increment retry counter
i++
continue
}

header = block.Header
logger.Info("got block from node", "hash", header.Hash(), "node", nodes[j].Key)
logger.Debug("got block from node", "header", header, "body", block.Body, "hash", header.Hash(), "node", nodes[j].Key)

if block.Body != nil && !bytes.Equal(*(block.Body), []byte{0}) {
resExts, err = block.Body.AsExtrinsics()
require.NoError(t, err, block.Body)
break
}

if header.Hash() == prevHeader.Hash() && j == len(nodes)-1 {
t.Fatal("could not find extrinsic in any blocks")
}
}

if block != nil && block.Body != nil && !bytes.Equal(*(block.Body), []byte{0}) {
break
}
}

// assert that the extrinsic included is the one we submitted
require.Equal(t, 1, len(resExts), "did not find extrinsic in block on any node")
require.Equal(t, resExts[0], types.Extrinsic(tx))
}
Loading

0 comments on commit 68ac803

Please sign in to comment.