Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib/runtime): use westend-dev spec file in TestNodeRuntime_ValidateTransaction #3047

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
1703b42
chore: wip need to include parachain inherents but cause cycle dep
EclesioMeloJunior Jan 16, 2023
cc284ea
Merge branch 'development' into eclesio/fix-TestNodeRuntime_ValidateT…
EclesioMeloJunior Jan 16, 2023
63990eb
Merge branch 'development' into eclesio/fix-TestNodeRuntime_ValidateT…
EclesioMeloJunior Jan 17, 2023
1716a66
chore: move parachain inherents and avoid cyclic deps
EclesioMeloJunior Jan 17, 2023
95cce86
chore: address lint warn
EclesioMeloJunior Jan 17, 2023
d08a926
chore: fix `TestInstance_ExecuteBlock_WestendRuntime`
EclesioMeloJunior Jan 17, 2023
a6ce3ff
chore: wip
EclesioMeloJunior Jan 17, 2023
c62d2fb
chore: fix `TestInstance_ApplyExtrinsic_WestendRuntime`
EclesioMeloJunior Jan 17, 2023
1d5a02f
Merge branch 'development' into eclesio/fix-TestNodeRuntime_ValidateT…
EclesioMeloJunior Jan 17, 2023
d4da80d
Merge branch 'development' into eclesio/fix-TestNodeRuntime_ValidateT…
EclesioMeloJunior Jan 17, 2023
a5ae3b0
chore: change `babe` for `inherents`
EclesioMeloJunior Jan 17, 2023
cbde3b0
chore: fix deepsource complaints
EclesioMeloJunior Jan 17, 2023
740e25d
Merge branch 'development' into eclesio/fix-TestNodeRuntime_ValidateT…
EclesioMeloJunior Jan 18, 2023
ad0bf63
chore: nit unneeded comment
EclesioMeloJunior Jan 18, 2023
78f2b50
chore: nit unneeded comment
EclesioMeloJunior Jan 18, 2023
d73a6f9
chore: replace `ret` to a better name
EclesioMeloJunior Jan 18, 2023
1820835
chore: skip deepsource warns
EclesioMeloJunior Jan 18, 2023
0117fa9
chore: remove the digest reset from `TestInstance_ExecuteBlock_Polkad…
EclesioMeloJunior Jan 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dot/rpc/modules/author_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions dot/sync/chain_processor_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}

Expand Down
3 changes: 2 additions & 1 deletion lib/babe/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -240,7 +241,7 @@ func buildBlockInherents(slot Slot, rt ExtrinsicHandler, parent *types.Header) (
return nil, err
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package babe
package inherents

import (
"fmt"
Expand Down Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
Expand All @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package babe
package inherents

import (
"testing"
Expand Down
93 changes: 47 additions & 46 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
EclesioMeloJunior marked this conversation as resolved.
Show resolved Hide resolved
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)),
}
}
80 changes: 27 additions & 53 deletions lib/runtime/wasmer/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package wasmer

import (
"bytes"
"encoding/json"
"fmt"
"math/big"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -422,7 +429,6 @@ func TestInstance_BabeConfiguration_NodeRuntime_NoAuthorities(t *testing.T) {
Randomness: [32]byte{},
SecondarySlots: 1,
}

require.Equal(t, expected, cfg)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down