Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jun 16, 2021
1 parent a0ac8e7 commit d71ae70
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 17 deletions.
2 changes: 2 additions & 0 deletions dot/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ var (

// ErrNilDigestHandler is returned when the DigestHandler interface is nil
ErrNilDigestHandler = errors.New("cannot have nil DigestHandler")

errNilCodeSubstitutedState = errors.New("cannot have nil CodeSubstitutedStat")
)

// ErrNilChannel is returned if a channel is nil
Expand Down
1 change: 1 addition & 0 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ func (s *Service) GetRuntimeVersion(bhash *common.Hash) (runtime.Version, error)

// HandleSubmittedExtrinsic is used to send a Transaction message containing a Extrinsic @ext
func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {
logger.Crit("HandleSubmittedExtrinsic")
if s.net == nil {
return nil
}
Expand Down
10 changes: 7 additions & 3 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {

gen, genTrie, genHeader := newTestGenesisWithTrieAndHeader(t)

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

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

if cfg.CodeSubstitutedState == nil {
cfg.CodeSubstitutedState = stateSrvc.Base
}

if cfg.Runtime == nil {
rtCfg := &wasmer.Config{}
rtCfg.Storage, err = rtstorage.NewTrieState(genTrie)
Expand All @@ -129,7 +133,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
if cfg.CodeSubstitutes == nil {
cfg.CodeSubstitutes = make(map[common.Hash]string)

genesisData, err := stateSrvc.Base.LoadGenesisData() // nolint
genesisData, err := cfg.CodeSubstitutedState.(*state.BaseState).LoadGenesisData() //nolint
require.NoError(t, err)

for k, v := range genesisData.CodeSubstitutes {
Expand All @@ -142,7 +146,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
}

s, err := NewService(cfg)
require.Nil(t, err)
require.NoError(t, err)

if net, ok := cfg.Network.(*network.Service); ok {
net.SetTransactionHandler(s)
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/author.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (cm *AuthorModule) SubmitExtrinsic(r *http.Request, req *Extrinsic, res *Ex
return err
}
ext := types.Extrinsic(extBytes)
cm.logger.Trace("[rpc]", "extrinsic", ext)
cm.logger.Crit("[rpc]", "extrinsic", ext)

err = cm.coreAPI.HandleSubmittedExtrinsic(ext)
*res = ExtrinsicHashResponse(ext.Hash().String())
Expand Down
20 changes: 13 additions & 7 deletions dot/rpc/modules/author_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ func TestAuthorModule_SubmitExtrinsic_invalid_input(t *testing.T) {
txQueue := state.NewTransactionState()
auth := setupAuthModule(t, txQueue)

fmt.Println("setupAuthModule")

// create and submit extrinsic
ext := Extrinsic{fmt.Sprintf("%x", "1")}

res := new(ExtrinsicHashResponse)
fmt.Println("SubmitExtrinsic")

err := auth.SubmitExtrinsic(nil, &ext, res)
require.EqualError(t, err, "could not byteify non 0x prefixed string")
Expand Down Expand Up @@ -276,20 +279,23 @@ func newCoreService(t *testing.T, srvc *state.Service) *core.Service {
}

cfg := &core.Config{
Runtime: rt,
Keystore: ks,
TransactionState: srvc.Transaction,
BlockState: srvc.Block,
StorageState: srvc.Storage,
EpochState: srvc.Epoch,
Network: &mockNetwork{},
Runtime: rt,
Keystore: ks,
TransactionState: srvc.Transaction,
BlockState: srvc.Block,
StorageState: srvc.Storage,
EpochState: srvc.Epoch,
Network: &mockNetwork{},
CodeSubstitutedState: srvc.Base,
}

return core.NewTestService(t, cfg)
}

func setupAuthModule(t *testing.T, txq *state.TransactionState) *AuthorModule {
fmt.Println("calling setupAuthModule")
cs := newCoreService(t, nil)
fmt.Println("called newCoreService")
rt := wasmer.NewTestInstance(t, runtime.NODE_RUNTIME)
t.Cleanup(func() {
rt.Stop()
Expand Down
8 changes: 7 additions & 1 deletion dot/rpc/modules/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,13 @@ func TestChainGetFinalizedHeadByRound(t *testing.T) {
expected := genesisHeader.Hash()
require.Equal(t, common.BytesToHex(expected[:]), res)

testhash := common.Hash{1, 2, 3, 4}
header := &types.Header{
Number: big.NewInt(1),
}
err = state.Block.SetHeader(header)
require.NoError(t, err)

testhash := header.Hash()
err = state.Block.SetFinalizedHash(testhash, 77, 1)
require.NoError(t, err)

Expand Down
12 changes: 7 additions & 5 deletions dot/rpc/modules/dev_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe"
babemocks "github.com/ChainSafe/gossamer/lib/babe/mocks"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
Expand Down Expand Up @@ -47,11 +48,12 @@ func newBABEService(t *testing.T) *babe.Service {
tt.Put(common.MustHexToBytes("0x886726f904d8372fdabb7707870c2fad"), common.MustHexToBytes("0x24d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d01000000000000008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48010000000000000090b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe220100000000000000306721211d5404bd9da88e0204360a1a9ab8b87c66c1bc2fcdd37f3c2222cc200100000000000000e659a7a1628cdd93febc04a4e0646ea20e9f5f0ce097d9a05290d4a9e054df4e01000000000000001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c01000000000000004603307f855321776922daeea21ee31720388d097cdaac66f05a6f8462b317570100000000000000be1d9d59de1283380100550a7b024501cb62d6cc40e3db35fcc5cf341814986e01000000000000001206960f920a23f7f4c43cc9081ec2ed0721f31a9bef2c10fd7602e16e08a32c0100000000000000"))

cfg := &babe.ServiceConfig{
BlockState: bs,
EpochState: es,
Keypair: kr.Alice().(*sr25519.Keypair),
Runtime: rt,
IsDev: true,
BlockState: bs,
EpochState: es,
Keypair: kr.Alice().(*sr25519.Keypair),
Runtime: rt,
IsDev: true,
BlockImportHandler: new(babemocks.BlockImportHandler),
}

babe, err := babe.NewService(cfg)
Expand Down
3 changes: 3 additions & 0 deletions dot/state/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ func TestFinalizedHash(t *testing.T) {
require.Equal(t, testGenesisHeader.Hash(), h)

testhash := common.Hash{1, 2, 3, 4}
err = bs.db.Put(headerKey(testhash), []byte{})
require.NoError(t, err)

err = bs.SetFinalizedHash(testhash, 1, 1)
require.NoError(t, err)

Expand Down
3 changes: 3 additions & 0 deletions dot/sync/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func TestSyncer_HandleJustification(t *testing.T) {

just := []byte("testjustification")

err := syncer.blockState.SetHeader(header)
require.NoError(t, err)

syncer.handleJustification(header, just)

res, err := syncer.blockState.GetJustification(header.Hash())
Expand Down

0 comments on commit d71ae70

Please sign in to comment.