Skip to content

Commit

Permalink
testing(dot/core): rewrite dot/core service tests (part 2) (#2308)
Browse files Browse the repository at this point in the history
* add wasm file generation to core service tests

* WIP/codeSubstituteTest

* code substitution test

* test handle block

* handle BlockProduced test

* clean up and test maintainTxnPool

* test handleBlockAsync

* part1 of service tests

* clean up

* fix test naming

* wip/cr feedback

* define storage root tests in subtests

* parallel storage root tests

* remove gomack.Any() for getRuntime calls

* restructure handleCodeSub unit tests

* restructure handle block tests

* restructure tests and make subtests parallel

* reduce integration test diff

* CR feedback TODO rewrite code sub

* remove wasm files from core service tests

* reduce code duplication in tests

* CR feedback

* wip/reorg test

* maybe finished chainreorg test

* temp comment out reorg test to work on others. TODO/fix extrinsic sig

* test insert and has key

* test decode session keys

* fix core reorg logic and remove wrapper for centrifuge extrinsics

* test getRuntimeVersion

* wip/fix core integration tests

* wip/fix core integration tests

* wip/fix core integration tests

* test hadnle submitted extrinsic

* test get metadata

* test try query storage

* test query storage

* finish dot core unit tests

* wip

* wip temp

* wip/fix unit tests post rebase

* fix first broken core integration test

* clean up code before fix other tests

* wip/fix tests

* fix core unit tests TODO fix final integration test

* fix core service integration and unit tests

* wip/restructure tests

* rebase

* wip/refactor tests

* wip/test refactor

* finish tests

* update github workflows to test core integration tests

* fix integration tests

* wip/remove hardcoded test vals

* wip/CE feedback

* wip

* remove mocks from helper func

* remove comment

* remove hardcoded babeDigests

* PR feedback wip

* CR feedback

* use helper to get root path

* wip/CR feedback

* wip

* respond to feedback

* linting

* fix linting

* CR feedback resolved

* clean up

* wip/CR feedback

* wip/feedback

* resolve feedback

* remove anon struct from accountInfo
  • Loading branch information
jimjbrettj authored Mar 23, 2022
1 parent d85a1db commit dd80d0d
Show file tree
Hide file tree
Showing 12 changed files with 1,068 additions and 298 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
matrix:
packages:
[
github.com/ChainSafe/gossamer/dot/core,
github.com/ChainSafe/gossamer/dot/rpc/modules,
github.com/ChainSafe/gossamer/lib/babe,
]
Expand Down
17 changes: 3 additions & 14 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"path/filepath"
"testing"

"github.com/ChainSafe/gossamer/dot/digest"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"
)

Expand All @@ -35,9 +33,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
}

if cfg.DigestHandler == nil {
digestHandler := NewMockDigestHandler(ctrl)
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
cfg.DigestHandler = digestHandler
cfg.DigestHandler = &digest.Handler{} // only for nil check in NewService
}

if cfg.Keystore == nil {
Expand Down Expand Up @@ -126,14 +122,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
cfg.BlockState.StoreRuntime(cfg.BlockState.BestBlockHash(), cfg.Runtime)

if cfg.Network == nil {
net := NewMockNetwork(ctrl)
net.EXPECT().GossipMessage(gomock.AssignableToTypeOf(new(network.TransactionMessage)))
net.EXPECT().IsSynced().Return(true)
net.EXPECT().ReportPeer(
gomock.AssignableToTypeOf(peerset.ReputationChange{}),
gomock.AssignableToTypeOf(peer.ID("")),
)
cfg.Network = net
cfg.Network = new(network.Service) // only for nil check in NewService
}

if cfg.CodeSubstitutes == nil {
Expand Down
24 changes: 22 additions & 2 deletions dot/core/messages_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (

"github.com/centrifuge/go-substrate-rpc-client/v3/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
"github.com/ChainSafe/gossamer/dot/types"
Expand All @@ -23,6 +22,10 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/pkg/scale"

"github.com/golang/mock/gomock"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/require"
)

func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, nonce uint64) types.Extrinsic {
Expand Down Expand Up @@ -75,6 +78,10 @@ func TestService_HandleBlockProduced(t *testing.T) {
Keystore: keystore.NewGlobalKeystore(),
}

digestHandler := NewMockDigestHandler(ctrl)
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
cfg.DigestHandler = digestHandler

s := NewTestService(t, cfg)
err := s.Start()
require.NoError(t, err)
Expand Down Expand Up @@ -130,9 +137,22 @@ func TestService_HandleTransactionMessage(t *testing.T) {
telemetryMock := NewMockClient(ctrl)
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()

digestHandler := NewMockDigestHandler(ctrl)
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))

net := NewMockNetwork(ctrl)
net.EXPECT().GossipMessage(gomock.AssignableToTypeOf(new(network.TransactionMessage))).AnyTimes()
net.EXPECT().IsSynced().Return(true).AnyTimes()
net.EXPECT().ReportPeer(
gomock.AssignableToTypeOf(peerset.ReputationChange{}),
gomock.AssignableToTypeOf(peer.ID("")),
).AnyTimes()

cfg := &Config{
Keystore: ks,
TransactionState: state.NewTransactionState(telemetryMock),
DigestHandler: digestHandler,
Network: net,
}

s := NewTestService(t, cfg)
Expand Down
22 changes: 10 additions & 12 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package core

import (
"bytes"
"context"
"errors"
"sync"
Expand All @@ -20,7 +21,8 @@ import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/pkg/scale"
cscale "github.com/centrifuge/go-substrate-rpc-client/v3/scale"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types"
)

var (
Expand Down Expand Up @@ -372,14 +374,9 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {

for _, ext := range *body {
logger.Tracef("validating transaction on re-org chain for extrinsic %s", ext)
encExt, err := scale.Marshal(ext)
if err != nil {
return err
}

// decode extrinsic and make sure it's not an inherent.
decExt := &types.ExtrinsicData{}
if err = decExt.DecodeVersion(encExt); err != nil {
decExt := &ctypes.Extrinsic{}
decoder := cscale.NewDecoder(bytes.NewReader(ext))
if err = decoder.Decode(&decExt); err != nil {
continue
}

Expand All @@ -388,14 +385,15 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
continue
}

externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, encExt...))
externalExt := make(types.Extrinsic, 0, 1+len(ext))
externalExt = append(externalExt, byte(types.TxnExternal))
externalExt = append(externalExt, ext...)
txv, err := rt.ValidateTransaction(externalExt)
if err != nil {
logger.Debugf("failed to validate transaction for extrinsic %s: %s", ext, err)
continue
}

vtx := transaction.NewValidTransaction(encExt, txv)
vtx := transaction.NewValidTransaction(ext, txv)
s.transactionState.AddToPool(vtx)
}
}
Expand Down
Loading

0 comments on commit dd80d0d

Please sign in to comment.