Skip to content

Commit

Permalink
chore(dot/core): use westend-local as default test runtime (#3073)
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior authored Jan 30, 2023
1 parent 6cb1780 commit cf2eddc
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 32 deletions.
8 changes: 4 additions & 4 deletions dot/core/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
var stateSrvc *state.Service
testDatadirPath := t.TempDir()

gen, genesisTrie, genesisHeader := newTestGenesisWithTrieAndHeader(t)
gen, genesisTrie, genesisHeader := newWestendLocalWithTrieAndHeader(t)

if cfg.BlockState == nil || cfg.StorageState == nil ||
cfg.TransactionState == nil || cfg.CodeSubstitutedState == nil {
Expand Down Expand Up @@ -257,11 +257,11 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
return s
}

func newTestGenesisWithTrieAndHeader(t *testing.T) (
func newWestendLocalWithTrieAndHeader(t *testing.T) (
gen genesis.Genesis, genesisTrie trie.Trie, genesisHeader types.Header) {
t.Helper()

genesisPath := utils.GetGssmrV3SubstrateGenesisRawPathTest(t)
genesisPath := utils.GetWestendLocalRawGenesisPath(t)
genPtr, err := genesis.NewGenesisFromJSONRaw(genesisPath)
require.NoError(t, err)
gen = *genPtr
Expand All @@ -280,7 +280,7 @@ func newTestGenesisWithTrieAndHeader(t *testing.T) (
return gen, genesisTrie, genesisHeader
}

func getGssmrRuntimeCode(t *testing.T) (code []byte) {
func getWestendDevRuntimeCode(t *testing.T) (code []byte) {
t.Helper()

path := utils.GetWestendDevRawGenesisPath(t)
Expand Down
9 changes: 7 additions & 2 deletions dot/core/messages_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"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"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
Expand Down Expand Up @@ -165,7 +164,13 @@ func TestService_HandleTransactionMessage(t *testing.T) {
require.NoError(t, err)
rt.SetContextStorage(ts)

block := sync.BuildBlock(t, rt, genHeader, nil)
babeConfig, err := rt.BabeConfiguration()
require.NoError(t, err)

currentTimestamp := uint64(time.Now().UnixMilli())
currentSlot := currentTimestamp / babeConfig.SlotDuration

block := buildTestBlockWithoutExtrinsics(t, rt, genHeader, currentSlot, currentTimestamp)

err = s.handleBlock(block, ts)
require.NoError(t, err)
Expand Down
73 changes: 72 additions & 1 deletion dot/core/service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe/inherents"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -470,7 +471,13 @@ func TestService_HandleSubmittedExtrinsic(t *testing.T) {
require.NoError(t, err)
rt.SetContextStorage(ts)

block := sync.BuildBlock(t, rt, genHeader, nil)
babeConfig, err := rt.BabeConfiguration()
require.NoError(t, err)

currentTimestamp := uint64(time.Now().UnixMilli())
currentSlotNumber := currentTimestamp / babeConfig.SlotDuration

block := buildTestBlockWithoutExtrinsics(t, rt, genHeader, currentSlotNumber, currentTimestamp)

err = s.handleBlock(block, ts)
require.NoError(t, err)
Expand Down Expand Up @@ -640,3 +647,67 @@ func TestService_HandleRuntimeChangesAfterCodeSubstitutes(t *testing.T) {
rt.GetCodeHash(),
"expected different code hash after runtime update")
}

func buildTestBlockWithoutExtrinsics(t *testing.T, instance state.Runtime,
parentHeader *types.Header, slotNumber, timestamp uint64) *types.Block {
digest := types.NewDigest()
prd, err := types.NewBabeSecondaryPlainPreDigest(0, slotNumber).ToPreRuntimeDigest()
require.NoError(t, err)

err = digest.Add(*prd)
require.NoError(t, err)
header := &types.Header{
ParentHash: parentHeader.Hash(),
Number: parentHeader.Number + 1,
Digest: digest,
}

err = instance.InitializeBlock(header)
require.NoError(t, err)

inherentData := types.NewInherentData()
err = inherentData.SetInherent(types.Timstap0, timestamp)
require.NoError(t, err)

err = inherentData.SetInherent(types.Babeslot, uint64(1))
require.NoError(t, err)

parachainInherent := inherents.ParachainInherentData{
ParentHeader: *parentHeader,
}

err = inherentData.SetInherent(types.Parachn0, parachainInherent)
require.NoError(t, err)

err = inherentData.SetInherent(types.Newheads, []byte{0})
require.NoError(t, err)

encodedInherents, err := inherentData.Encode()
require.NoError(t, err)

inherentExts, err := instance.InherentExtrinsics(encodedInherents)
require.NoError(t, err)

var decodedInherents [][]byte
err = scale.Unmarshal(inherentExts, &decodedInherents)
require.NoError(t, err)

for _, inherent := range decodedInherents {
encoded, err := scale.Marshal(inherent)
require.NoError(t, err)

ret, err := instance.ApplyExtrinsic(encoded)
require.NoError(t, err)
require.Equal(t, ret, []byte{0, 0})
}

finalisedHeader, err := instance.FinalizeBlock()
require.NoError(t, err)

finalisedHeader.Number = header.Number
finalisedHeader.Hash()
return &types.Block{
Header: *finalisedHeader,
Body: types.Body(types.BytesArrayToExtrinsics(decodedInherents)),
}
}
2 changes: 1 addition & 1 deletion dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func Test_Service_handleCodeSubstitution(t *testing.T) {
t.Parallel()

errTest := errors.New("test error")
validRuntimeCode := getGssmrRuntimeCode(t)
validRuntimeCode := getWestendDevRuntimeCode(t)

testCases := map[string]struct {
serviceBuilder func(ctrl *gomock.Controller) *Service
Expand Down
24 changes: 0 additions & 24 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,6 @@ func KeystoreFilepaths(basepath string) ([]string, error) {
return keys, nil
}

// GetGssmrV3SubstrateGenesisRawPathTest gets the v3 substrate gssmr raw genesis path
// and fails the test if it cannot find it.
func GetGssmrV3SubstrateGenesisRawPathTest(t *testing.T) string {
t.Helper()
path, err := GetGssmrV3SubstrateGenesisRawPath()
require.NoError(t, err)
return path
}

// GetGssmrV3SubstrateGenesisRawPath gets the v3 substrate gssmr raw genesis path
// and returns an error if it cannot find it.
func GetGssmrV3SubstrateGenesisRawPath() (path string, err error) {
rootPath, err := GetProjectRootPath()
if err != nil {
return "", err
}
return filepath.Join(rootPath, "./chain/gssmr-v3substrate/genesis.json"), nil
}

// GetDevV3SubstrateGenesisPath gets the v3 substrate dev genesis path
func GetDevV3SubstrateGenesisPath(t *testing.T) string {
return filepath.Join(GetProjectRootPathTest(t), "./chain/dev-v3substrate/genesis.json")
}

// GetWestendDevHumanReadableGenesisPath gets the westend-dev human readable spec filepath
func GetWestendDevHumanReadableGenesisPath(t *testing.T) string {
t.Helper()
Expand Down

0 comments on commit cf2eddc

Please sign in to comment.