Skip to content

Commit dd80d0d

Browse files
authored
testing(dot/core): rewrite dot/core service tests (part 2) (#2308)
* 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
1 parent d85a1db commit dd80d0d

File tree

12 files changed

+1068
-298
lines changed

12 files changed

+1068
-298
lines changed

.github/workflows/integration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
matrix:
2424
packages:
2525
[
26+
github.com/ChainSafe/gossamer/dot/core,
2627
github.com/ChainSafe/gossamer/dot/rpc/modules,
2728
github.com/ChainSafe/gossamer/lib/babe,
2829
]

dot/core/helpers_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"github.com/ChainSafe/gossamer/dot/digest"
1011
"github.com/ChainSafe/gossamer/dot/network"
11-
"github.com/ChainSafe/gossamer/dot/peerset"
1212
"github.com/ChainSafe/gossamer/dot/state"
13-
"github.com/ChainSafe/gossamer/dot/types"
1413
"github.com/ChainSafe/gossamer/internal/log"
1514
"github.com/ChainSafe/gossamer/lib/common"
1615
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
@@ -21,7 +20,6 @@ import (
2120
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
2221
"github.com/ChainSafe/gossamer/lib/utils"
2322
"github.com/golang/mock/gomock"
24-
"github.com/libp2p/go-libp2p-core/peer"
2523
"github.com/stretchr/testify/require"
2624
)
2725

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

3735
if cfg.DigestHandler == nil {
38-
digestHandler := NewMockDigestHandler(ctrl)
39-
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
40-
cfg.DigestHandler = digestHandler
36+
cfg.DigestHandler = &digest.Handler{} // only for nil check in NewService
4137
}
4238

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

128124
if cfg.Network == nil {
129-
net := NewMockNetwork(ctrl)
130-
net.EXPECT().GossipMessage(gomock.AssignableToTypeOf(new(network.TransactionMessage)))
131-
net.EXPECT().IsSynced().Return(true)
132-
net.EXPECT().ReportPeer(
133-
gomock.AssignableToTypeOf(peerset.ReputationChange{}),
134-
gomock.AssignableToTypeOf(peer.ID("")),
135-
)
136-
cfg.Network = net
125+
cfg.Network = new(network.Service) // only for nil check in NewService
137126
}
138127

139128
if cfg.CodeSubstitutes == nil {

dot/core/messages_integration_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import (
1111

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

1715
"github.com/ChainSafe/gossamer/dot/network"
16+
"github.com/ChainSafe/gossamer/dot/peerset"
1817
"github.com/ChainSafe/gossamer/dot/state"
1918
"github.com/ChainSafe/gossamer/dot/sync"
2019
"github.com/ChainSafe/gossamer/dot/types"
@@ -23,6 +22,10 @@ import (
2322
"github.com/ChainSafe/gossamer/lib/keystore"
2423
"github.com/ChainSafe/gossamer/lib/runtime"
2524
"github.com/ChainSafe/gossamer/pkg/scale"
25+
26+
"github.com/golang/mock/gomock"
27+
"github.com/libp2p/go-libp2p-core/peer"
28+
"github.com/stretchr/testify/require"
2629
)
2730

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

81+
digestHandler := NewMockDigestHandler(ctrl)
82+
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
83+
cfg.DigestHandler = digestHandler
84+
7885
s := NewTestService(t, cfg)
7986
err := s.Start()
8087
require.NoError(t, err)
@@ -130,9 +137,22 @@ func TestService_HandleTransactionMessage(t *testing.T) {
130137
telemetryMock := NewMockClient(ctrl)
131138
telemetryMock.EXPECT().SendMessage(gomock.Any()).AnyTimes()
132139

140+
digestHandler := NewMockDigestHandler(ctrl)
141+
digestHandler.EXPECT().HandleDigests(gomock.AssignableToTypeOf(new(types.Header)))
142+
143+
net := NewMockNetwork(ctrl)
144+
net.EXPECT().GossipMessage(gomock.AssignableToTypeOf(new(network.TransactionMessage))).AnyTimes()
145+
net.EXPECT().IsSynced().Return(true).AnyTimes()
146+
net.EXPECT().ReportPeer(
147+
gomock.AssignableToTypeOf(peerset.ReputationChange{}),
148+
gomock.AssignableToTypeOf(peer.ID("")),
149+
).AnyTimes()
150+
133151
cfg := &Config{
134152
Keystore: ks,
135153
TransactionState: state.NewTransactionState(telemetryMock),
154+
DigestHandler: digestHandler,
155+
Network: net,
136156
}
137157

138158
s := NewTestService(t, cfg)

dot/core/service.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package core
55

66
import (
7+
"bytes"
78
"context"
89
"errors"
910
"sync"
@@ -20,7 +21,8 @@ import (
2021
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
2122
"github.com/ChainSafe/gossamer/lib/services"
2223
"github.com/ChainSafe/gossamer/lib/transaction"
23-
"github.com/ChainSafe/gossamer/pkg/scale"
24+
cscale "github.com/centrifuge/go-substrate-rpc-client/v3/scale"
25+
ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types"
2426
)
2527

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

373375
for _, ext := range *body {
374376
logger.Tracef("validating transaction on re-org chain for extrinsic %s", ext)
375-
encExt, err := scale.Marshal(ext)
376-
if err != nil {
377-
return err
378-
}
379-
380-
// decode extrinsic and make sure it's not an inherent.
381-
decExt := &types.ExtrinsicData{}
382-
if err = decExt.DecodeVersion(encExt); err != nil {
377+
decExt := &ctypes.Extrinsic{}
378+
decoder := cscale.NewDecoder(bytes.NewReader(ext))
379+
if err = decoder.Decode(&decExt); err != nil {
383380
continue
384381
}
385382

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

391-
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, encExt...))
388+
externalExt := make(types.Extrinsic, 0, 1+len(ext))
389+
externalExt = append(externalExt, byte(types.TxnExternal))
390+
externalExt = append(externalExt, ext...)
392391
txv, err := rt.ValidateTransaction(externalExt)
393392
if err != nil {
394393
logger.Debugf("failed to validate transaction for extrinsic %s: %s", ext, err)
395394
continue
396395
}
397-
398-
vtx := transaction.NewValidTransaction(encExt, txv)
396+
vtx := transaction.NewValidTransaction(ext, txv)
399397
s.transactionState.AddToPool(vtx)
400398
}
401399
}

0 commit comments

Comments
 (0)