diff --git a/dot/rpc/modules/author_integration_test.go b/dot/rpc/modules/author_integration_test.go index 1f8798ea11..1ff287eaf8 100644 --- a/dot/rpc/modules/author_integration_test.go +++ b/dot/rpc/modules/author_integration_test.go @@ -744,7 +744,7 @@ func setupStateAndPopulateTrieState(t *testing.T, basepath string, genesisHash := genesisHeader.Hash() state2test.Block.StoreRuntime(genesisHash, rt) - b := runtime.InitializeRuntimeToTest(t, rt, genesisHash) + b := runtime.InitializeRuntimeToTest(t, rt, &genesisHeader) err = state2test.Block.AddBlock(b) require.NoError(t, err) diff --git a/dot/sync/chain_processor_integration_test.go b/dot/sync/chain_processor_integration_test.go index 4b3c9b4d23..fc84b960a9 100644 --- a/dot/sync/chain_processor_integration_test.go +++ b/dot/sync/chain_processor_integration_test.go @@ -13,7 +13,7 @@ import ( "github.com/ChainSafe/gossamer/dot/network" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" - "github.com/ChainSafe/gossamer/lib/babe" + "github.com/ChainSafe/gossamer/lib/babe/inherents" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/common/variadic" "github.com/ChainSafe/gossamer/lib/transaction" @@ -49,7 +49,7 @@ func buildBlockWithSlotAndTimestamp(t *testing.T, instance state.Runtime, err = inherentData.SetInherent(types.Babeslot, currentSlot) require.NoError(t, err) - parachainInherent := babe.ParachainInherentData{ + parachainInherent := inherents.ParachainInherentData{ ParentHeader: *parent, } diff --git a/lib/babe/build.go b/lib/babe/build.go index f501dbc52e..ce8a36d3ce 100644 --- a/lib/babe/build.go +++ b/lib/babe/build.go @@ -10,6 +10,7 @@ import ( "time" "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/lib/babe/inherents" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" "github.com/ChainSafe/gossamer/lib/transaction" @@ -240,7 +241,7 @@ func buildBlockInherents(slot Slot, rt ExtrinsicHandler, parent *types.Header) ( return nil, err } - parachainInherent := ParachainInherentData{ + parachainInherent := inherents.ParachainInherentData{ ParentHeader: *parent, } diff --git a/lib/babe/parachain_inherents.go b/lib/babe/inherents/parachain_inherents.go similarity index 98% rename from lib/babe/parachain_inherents.go rename to lib/babe/inherents/parachain_inherents.go index c9ed2fb0d5..b1731abc91 100644 --- a/lib/babe/parachain_inherents.go +++ b/lib/babe/inherents/parachain_inherents.go @@ -1,7 +1,7 @@ // Copyright 2022 ChainSafe Systems (ON) // SPDX-License-Identifier: LGPL-3.0-only -package babe +package inherents import ( "fmt" @@ -105,7 +105,7 @@ func (validDisputeStatementKind) Index() uint { //skipcq return 0 } -func (validDisputeStatementKind) String() string { //skipcq:SCC-U1000 +func (validDisputeStatementKind) String() string { //skipcq return "valid dispute statement kind" } @@ -172,7 +172,7 @@ func (approvalChecking) Index() uint { //skipcq return 3 } -func (a approvalChecking) String() string { return "approval checking" } //skipcq:SCC-U1000 +func (approvalChecking) String() string { return "approval checking" } // invalidDisputeStatementKind is a kind of statements of invalidity on a candidate. type invalidDisputeStatementKind scale.VaryingDataType //skipcq @@ -182,7 +182,7 @@ func (invalidDisputeStatementKind) Index() uint { //skipcq return 1 } -func (invalidDisputeStatementKind) String() string { //skipcq:SCC-U1000 +func (invalidDisputeStatementKind) String() string { //skipcq return "invalid dispute statement kind" } diff --git a/lib/babe/parachain_inherents_test.go b/lib/babe/inherents/parachain_inherents_test.go similarity index 99% rename from lib/babe/parachain_inherents_test.go rename to lib/babe/inherents/parachain_inherents_test.go index 75d651d9f5..54b7eacb19 100644 --- a/lib/babe/parachain_inherents_test.go +++ b/lib/babe/inherents/parachain_inherents_test.go @@ -1,7 +1,7 @@ // Copyright 2022 ChainSafe Systems (ON) // SPDX-License-Identifier: LGPL-3.0-only -package babe +package inherents import ( "testing" diff --git a/lib/runtime/test_helpers.go b/lib/runtime/test_helpers.go index 7a98fd4022..b3e8e06627 100644 --- a/lib/runtime/test_helpers.go +++ b/lib/runtime/test_helpers.go @@ -17,12 +17,12 @@ import ( "github.com/ChainSafe/chaindb" "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/lib/babe/inherents" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto" "github.com/ChainSafe/gossamer/lib/crypto/ed25519" "github.com/ChainSafe/gossamer/lib/keystore" "github.com/ChainSafe/gossamer/lib/transaction" - "github.com/ChainSafe/gossamer/lib/trie" "github.com/ChainSafe/gossamer/lib/utils" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/centrifuge/go-substrate-rpc-client/v4/signature" @@ -304,79 +304,80 @@ type Metadataer interface { } // InitializeRuntimeToTest sets a new block using the runtime functions to set initial data into the host -func InitializeRuntimeToTest(t *testing.T, instance Instance, parentHash common.Hash) *types.Block { +func InitializeRuntimeToTest(t *testing.T, instance Instance, parentHeader *types.Header) *types.Block { t.Helper() - header := &types.Header{ - ParentHash: parentHash, - Number: 1, - Digest: types.NewDigest(), - } - - err := instance.InitializeBlock(header) + babeConfig, err := instance.BabeConfiguration() require.NoError(t, err) - idata := types.NewInherentData() - err = idata.SetInherent(types.Timstap0, uint64(1)) + slotDuration := babeConfig.SlotDuration + timestamp := uint64(time.Now().UnixMilli()) + currentSlot := timestamp / slotDuration + + babeDigest := types.NewBabeDigest() + err = babeDigest.Set(*types.NewBabePrimaryPreDigest(0, currentSlot, [32]byte{}, [64]byte{})) require.NoError(t, err) - err = idata.SetInherent(types.Babeslot, uint64(1)) + encodedBabeDigest, err := scale.Marshal(babeDigest) require.NoError(t, err) + preDigest := *types.NewBABEPreRuntimeDigest(encodedBabeDigest) - ienc, err := idata.Encode() + digest := types.NewDigest() + require.NoError(t, err) + err = digest.Add(preDigest) require.NoError(t, err) - // Call BlockBuilder_inherent_extrinsics which returns the inherents as extrinsics - inherentExts, err := instance.InherentExtrinsics(ienc) + header := &types.Header{ + ParentHash: parentHeader.Hash(), + Number: parentHeader.Number + 1, + Digest: digest, + } + + err = instance.InitializeBlock(header) require.NoError(t, err) - // decode inherent extrinsics - var exts [][]byte - err = scale.Unmarshal(inherentExts, &exts) + inherentData := types.NewInherentData() + err = inherentData.SetInherent(types.Timstap0, timestamp) require.NoError(t, err) - // apply each inherent extrinsic - for _, ext := range exts { - in, err := scale.Marshal(ext) - require.NoError(t, err) + err = inherentData.SetInherent(types.Babeslot, currentSlot) + require.NoError(t, err) - ret, err := instance.ApplyExtrinsic(append([]byte{1}, in...)) - require.NoError(t, err, in) - require.Equal(t, ret, []byte{0, 0}) + parachainInherent := inherents.ParachainInherentData{ + ParentHeader: *parentHeader, } - res, err := instance.FinalizeBlock() + err = inherentData.SetInherent(types.Parachn0, parachainInherent) require.NoError(t, err) - res.Number = header.Number + err = inherentData.SetInherent(types.Newheads, []byte{0}) + require.NoError(t, err) - babeDigest := types.NewBabeDigest() - err = babeDigest.Set(*types.NewBabePrimaryPreDigest(0, 1, [32]byte{}, [64]byte{})) + encodedInnherents, err := inherentData.Encode() require.NoError(t, err) - data, err := scale.Marshal(babeDigest) + + // Call BlockBuilder_inherent_extrinsics which returns the inherents as extrinsics + inherentExts, err := instance.InherentExtrinsics(encodedInnherents) require.NoError(t, err) - preDigest := *types.NewBABEPreRuntimeDigest(data) - digest := types.NewDigest() - err = digest.Add(preDigest) + var extrinsics [][]byte + err = scale.Unmarshal(inherentExts, &extrinsics) require.NoError(t, err) - res.Digest = digest - expected := &types.Header{ - ParentHash: header.ParentHash, - Number: 1, - Digest: digest, + for _, ext := range extrinsics { + encodedExtrinsic, err := scale.Marshal(ext) + require.NoError(t, err) + + wasmResult, err := instance.ApplyExtrinsic(encodedExtrinsic) + require.NoError(t, err, encodedExtrinsic) + require.Equal(t, wasmResult, []byte{0, 0}) } - require.Equal(t, expected.ParentHash, res.ParentHash) - require.Equal(t, expected.Number, res.Number) - require.Equal(t, expected.Digest, res.Digest) - require.False(t, res.StateRoot.IsEmpty()) - require.False(t, res.ExtrinsicsRoot.IsEmpty()) - require.NotEqual(t, trie.EmptyHash, res.StateRoot) + finalizedBlockHeader, err := instance.FinalizeBlock() + require.NoError(t, err) return &types.Block{ - Header: *res, - Body: *types.NewBody(types.BytesArrayToExtrinsics(exts)), + Header: *finalizedBlockHeader, + Body: *types.NewBody(types.BytesArrayToExtrinsics(extrinsics)), } } diff --git a/lib/runtime/wasmer/exports_test.go b/lib/runtime/wasmer/exports_test.go index 189f35f868..c7ead2732c 100644 --- a/lib/runtime/wasmer/exports_test.go +++ b/lib/runtime/wasmer/exports_test.go @@ -4,6 +4,7 @@ package wasmer import ( + "bytes" "encoding/json" "fmt" "math/big" @@ -296,7 +297,7 @@ func balanceKey(t *testing.T, pub []byte) []byte { } func TestNodeRuntime_ValidateTransaction(t *testing.T) { - genesisPath := utils.GetGssmrV3SubstrateGenesisRawPathTest(t) + genesisPath := utils.GetWestendDevRawGenesisPath(t) gen := genesisFromRawJSON(t, genesisPath) genTrie, err := NewTrieFromGenesis(gen) require.NoError(t, err) @@ -344,11 +345,17 @@ func TestNodeRuntime_ValidateTransaction(t *testing.T) { extHex := runtime.NewTestExtrinsic(t, rt, genesisHeader.Hash(), genesisHeader.Hash(), 0, "System.remark", []byte{0xab, 0xcd}) - extBytes := common.MustHexToBytes(extHex) - extBytes = append([]byte{byte(types.TxnExternal)}, extBytes...) + genesisHashBytes := genesisHeader.Hash().ToBytes() + + validateTransactionArguments := [][]byte{ + {byte(types.TxnExternal)}, + common.MustHexToBytes(extHex), + genesisHashBytes} + + extrinsicsBytes := bytes.Join(validateTransactionArguments, nil) - runtime.InitializeRuntimeToTest(t, rt, genesisHeader.Hash()) - _, err = rt.ValidateTransaction(extBytes) + runtime.InitializeRuntimeToTest(t, rt, genesisHeader) + _, err = rt.ValidateTransaction(extrinsicsBytes) require.NoError(t, err) } @@ -422,7 +429,6 @@ func TestInstance_BabeConfiguration_NodeRuntime_NoAuthorities(t *testing.T) { Randomness: [32]byte{}, SecondarySlots: 1, } - require.Equal(t, expected, cfg) } @@ -508,26 +514,19 @@ func TestInstance_InitializeBlock_PolkadotRuntime(t *testing.T) { require.NoError(t, err) } -func TestInstance_FinalizeBlock_NodeRuntime(t *testing.T) { - instance := NewTestInstance(t, runtime.NODE_RUNTIME) - runtime.InitializeRuntimeToTest(t, instance, common.Hash{}) -} - -func TestInstance_ExecuteBlock_NodeRuntime(t *testing.T) { - instance := NewTestInstance(t, runtime.NODE_RUNTIME) - block := runtime.InitializeRuntimeToTest(t, instance, common.Hash{}) +func TestInstance_ExecuteBlock_WestendRuntime(t *testing.T) { + instance := NewTestInstance(t, runtime.WESTEND_RUNTIME_v0929) + block := runtime.InitializeRuntimeToTest(t, instance, &types.Header{}) // reset state back to parent state before executing parentState := storage.NewTrieState(nil) instance.SetContextStorage(parentState) - block.Header.Digest = types.NewDigest() _, err := instance.ExecuteBlock(block) require.NoError(t, err) } -func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) { - t.Skip() // TODO: this fails with "syscall frame is no longer valid" (#1026) +func TestInstance_ApplyExtrinsic_WestendRuntime(t *testing.T) { genesisPath := utils.GetWestendDevRawGenesisPath(t) gen := genesisFromRawJSON(t, genesisPath) genTrie, err := NewTrieFromGenesis(gen) @@ -544,51 +543,27 @@ func TestInstance_ExecuteBlock_GossamerRuntime(t *testing.T) { instance, err := NewRuntimeFromGenesis(cfg) require.NoError(t, err) - block := runtime.InitializeRuntimeToTest(t, instance, common.Hash{}) - // reset state back to parent state before executing parentState := storage.NewTrieState(&genTrie) instance.SetContextStorage(parentState) - _, err = instance.ExecuteBlock(block) - require.NoError(t, err) -} - -func TestInstance_ApplyExtrinsic_GossamerRuntime(t *testing.T) { - t.Skip() // TODO: this fails with "syscall frame is no longer valid" (#1026) - genesisPath := utils.GetWestendDevRawGenesisPath(t) - gen := genesisFromRawJSON(t, genesisPath) - genTrie, err := NewTrieFromGenesis(gen) - require.NoError(t, err) - - // set state to genesis state - genState := storage.NewTrieState(&genTrie) - - cfg := Config{ - Storage: genState, - LogLvl: log.Critical, + genesisHeader := &types.Header{ + Number: 0, + StateRoot: genTrie.MustHash(), + } + header := &types.Header{ + ParentHash: genesisHeader.Hash(), + Number: 1, + Digest: types.NewDigest(), } - instance, err := NewRuntimeFromGenesis(cfg) - require.NoError(t, err) - - // reset state back to parent state before executing - parentState := storage.NewTrieState(&genTrie) - instance.SetContextStorage(parentState) - - parentHash := common.Hash{} - header := types.NewHeader(parentHash, common.Hash{}, common.Hash{}, 1, types.NewDigest()) err = instance.InitializeBlock(header) require.NoError(t, err) - extHex := runtime.NewTestExtrinsic(t, instance, parentHash, parentHash, + extHex := runtime.NewTestExtrinsic(t, instance, genesisHeader.Hash(), genesisHeader.Hash(), 0, "System.remark", []byte{0xab, 0xcd}) - extBytes := common.MustHexToBytes(extHex) - enc, err := scale.Marshal(extBytes) - require.NoError(t, err) - - res, err := instance.ApplyExtrinsic(enc) + res, err := instance.ApplyExtrinsic(common.MustHexToBytes(extHex)) require.NoError(t, err) require.Equal(t, []byte{0, 0}, res) } @@ -598,13 +573,12 @@ func TestInstance_ExecuteBlock_PolkadotRuntime(t *testing.T) { instance := NewTestInstance(t, runtime.POLKADOT_RUNTIME) - block := runtime.InitializeRuntimeToTest(t, instance, common.Hash{}) + block := runtime.InitializeRuntimeToTest(t, instance, &types.Header{}) // reset state back to parent state before executing parentState := storage.NewTrieState(nil) instance.SetContextStorage(parentState) - block.Header.Digest = types.NewDigest() _, err := instance.ExecuteBlock(block) require.NoError(t, err) }