Skip to content

Commit

Permalink
feat(dot/parachain/backing): Implement a function to get session exec…
Browse files Browse the repository at this point in the history
…utor parameters (#3887)
  • Loading branch information
axaysagathiya committed Apr 25, 2024
1 parent 28656d6 commit dd08b61
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 75 deletions.
15 changes: 15 additions & 0 deletions dot/core/mock_runtime_instance_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions dot/parachain/backing/active_leaves_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
parachainutil "github.com/ChainSafe/gossamer/dot/parachain/util"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -253,7 +252,7 @@ func (cb *CandidateBacking) removeUnknownRelayParentsFromPerCandidate() {

// getProspectiveParachainsMode requests prospective parachains mode
// for a given relay parent based on the Runtime API version.
func getProspectiveParachainsMode(blockstate *state.BlockState, relayParent common.Hash,
func getProspectiveParachainsMode(blockstate BlockState, relayParent common.Hash,
) (parachaintypes.ProspectiveParachainsMode, error) {
var emptyMode parachaintypes.ProspectiveParachainsMode

Expand Down Expand Up @@ -286,7 +285,7 @@ func getProspectiveParachainsMode(blockstate *state.BlockState, relayParent comm

// Load the data necessary to do backing work on top of a relay-parent.
func constructPerRelayParentState(
blockstate *state.BlockState,
blockstate BlockState,
relayParent common.Hash,
keystore *keystore.Keystore,
mode parachaintypes.ProspectiveParachainsMode,
Expand Down
9 changes: 7 additions & 2 deletions dot/parachain/backing/candidate_backing.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
"sync"

parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/tidwall/btree"
)

Expand Down Expand Up @@ -82,7 +82,11 @@ type CandidateBacking struct {
implicitView ImplicitView
// The handle to the keystore used for signing.
keystore keystore.Keystore
BlockState *state.BlockState
BlockState BlockState
}

type BlockState interface {
GetRuntime(blockHash common.Hash) (instance runtime.Instance, err error)
}

type activeLeafState struct {
Expand Down Expand Up @@ -345,6 +349,7 @@ func (cb *CandidateBacking) handleStatementMessage(
}

return rpState.kickOffValidationWork(
cb.BlockState,
cb.SubSystemToOverseer,
chRelayParentAndCommand,
pc.persistedValidationData,
Expand Down
120 changes: 82 additions & 38 deletions dot/parachain/backing/candidate_backing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,90 @@
package backing

import (
_ "embed"

"errors"
"fmt"
"testing"

availabilitystore "github.com/ChainSafe/gossamer/dot/parachain/availability-store"
collatorprotocolmessages "github.com/ChainSafe/gossamer/dot/parachain/collator-protocol/messages"
parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/runtime"
wazero_runtime "github.com/ChainSafe/gossamer/lib/runtime/wazero"
"github.com/ChainSafe/gossamer/lib/trie"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/stretchr/testify/require"
gomock "go.uber.org/mock/gomock"
"gopkg.in/yaml.v3"
)

// go:embed ../../../lib/runtime/wazero/testdata/parachains_configuration_v190.yaml
var parachainsConfigV190TestDataRaw string

type Storage struct {
Name string `yaml:"name"`
Key string `yaml:"key"`
Value string `yaml:"value"`
}

type Data struct {
Storage []Storage `yaml:"storage"`
Expected map[string]string `yaml:"expected"`
Lookups map[string]any `yaml:"-"`
}

var parachainsConfigV190TestData Data

func init() {
err := yaml.Unmarshal([]byte(parachainsConfigV190TestDataRaw), &parachainsConfigV190TestData)
if err != nil {
fmt.Println("Error unmarshalling test data:", err)
return
}
parachainsConfigV190TestData.Lookups = make(map[string]any)

for _, s := range parachainsConfigV190TestData.Storage {
if s.Name != "" {
parachainsConfigV190TestData.Lookups[s.Name] = common.MustHexToBytes(s.Value)
}
}
}

func getParachainHostTrie(t *testing.T, testDataStorage []Storage) *trie.Trie {
t.Helper()

tt := trie.NewEmptyTrie()

for _, s := range testDataStorage {
key := common.MustHexToBytes(s.Key)
value := common.MustHexToBytes(s.Value)
err := tt.Put(key, value)
require.NoError(t, err)
}

return tt
}

func newTestBlockState(t *testing.T) *MockBlockState {
t.Helper()

tt := getParachainHostTrie(t, parachainsConfigV190TestData.Storage)
rt := wazero_runtime.NewTestInstanceWithTrie(t, runtime.WESTEND_RUNTIME_v190, tt)

ctrl := gomock.NewController(t)
mockBlockstate := NewMockBlockState(ctrl)

hash, err := common.HexToHash("0x0505050505050505050505050505050505050505050505050505050505050505")
require.NoError(t, err)

mockBlockstate.EXPECT().GetRuntime(hash).Return(rt, nil).AnyTimes()

return mockBlockstate
}

var tempSignature = common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll

func uint32ToParaIDPtr(t *testing.T, u uint32) *parachaintypes.ParaID {
Expand Down Expand Up @@ -591,15 +662,17 @@ func TestKickOffValidationWork(t *testing.T) {

go c.processChannels(subSystemToOverseer, chRelayParentAndCommand)

err := c.rpState.kickOffValidationWork(subSystemToOverseer, chRelayParentAndCommand, pvd, attesting)
err := c.rpState.kickOffValidationWork(nil, subSystemToOverseer, chRelayParentAndCommand, pvd, attesting)
require.NoError(t, err)
})
}
}

func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
func TestValidateAndMakeAvailable(t *testing.T) {
t.Parallel()

blockState := newTestBlockState(t)

var pvd parachaintypes.PersistedValidationData
candidateReceipt := getDummyCommittedCandidateReceipt(t).ToPlain()

Expand All @@ -610,11 +683,10 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
relayParent := getDummyHash(t, 5)

testCases := []struct {
description string
rpState perRelayParentState
expectedErr string
mockOverseer func(ch chan any)
mockExecutorParamsGetter executorParamsGetter
description string
rpState perRelayParentState
expectedErr string
mockOverseer func(ch chan any)
}{
{
description: "validation_process_already_started_for_candidate",
Expand All @@ -624,9 +696,8 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
candidateHash: true,
},
},
expectedErr: "",
mockOverseer: func(ch chan any) {},
mockExecutorParamsGetter: executorParamsAtRelayParent,
expectedErr: "",
mockOverseer: func(ch chan any) {},
},
{
description: "unable_to_get_validation_code",
Expand All @@ -647,30 +718,6 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
Err: errors.New("mock error getting validation code"),
}
},
mockExecutorParamsGetter: executorParamsAtRelayParent,
},
{
description: "unable_to_get_executor_params",
rpState: perRelayParentState{
issuedStatements: map[parachaintypes.CandidateHash]bool{},
awaitingValidation: map[parachaintypes.CandidateHash]bool{},
},
expectedErr: "getting executor params at relay parent: ",
mockOverseer: func(ch chan any) {
data := <-ch
req, ok := data.(parachaintypes.RuntimeApiMessageRequest)
if !ok {
t.Errorf("invalid overseer message type: %T\n", data)
}

req.RuntimeApiRequest.(parachaintypes.RuntimeApiRequestValidationCodeByHash).
Ch <- parachaintypes.OverseerFuncRes[parachaintypes.ValidationCode]{
Data: parachaintypes.ValidationCode{1, 2, 3},
}
},
mockExecutorParamsGetter: func(h common.Hash, c chan<- any) (parachaintypes.ExecutorParams, error) {
return parachaintypes.NewExecutorParams(), errors.New("mock error getting executor params")
},
},
{
description: "unable_to_get_validation_result",
Expand All @@ -696,7 +743,6 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
}
}
},
mockExecutorParamsGetter: executorParamsAtRelayParent,
},
{
description: "validation_result_is_invalid",
Expand Down Expand Up @@ -725,7 +771,6 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
}
}
},
mockExecutorParamsGetter: executorParamsAtRelayParent,
},
{
description: "validation_result_is_valid",
Expand Down Expand Up @@ -755,7 +800,6 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
}
}
},
mockExecutorParamsGetter: executorParamsAtRelayParent,
},
}

Expand All @@ -773,7 +817,7 @@ func TestBackgroundValidateAndMakeAvailable(t *testing.T) {
}(chRelayParentAndCommand)

err := c.rpState.validateAndMakeAvailable(
c.mockExecutorParamsGetter,
blockState,
subSystemToOverseer,
chRelayParentAndCommand,
candidateReceipt,
Expand Down
56 changes: 56 additions & 0 deletions dot/parachain/backing/mocks_blockstate_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dot/parachain/backing/mocks_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
package backing

//go:generate mockgen -destination=mocks_test.go -package=$GOPACKAGE . Table,ImplicitView
//go:generate mockgen -destination=mocks_blockstate_test.go -package=$GOPACKAGE . BlockState
Loading

0 comments on commit dd08b61

Please sign in to comment.