Skip to content

Commit

Permalink
fix usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rebonat0 committed Jan 15, 2025
1 parent 3985deb commit f8e5e9e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 53 deletions.
53 changes: 15 additions & 38 deletions dot/parachain/prospective-parachains/prospective-parachains.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,22 @@ import (
"errors"

"github.com/ChainSafe/gossamer/dot/parachain/backing"
fragmentchain "github.com/ChainSafe/gossamer/dot/parachain/prospective-parachains/fragment-chain"
parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
)

var logger = log.NewFromGlobal(log.AddContext("pkg", "prospective_parachains"), log.SetLevel(log.Debug))

type RelayBlockViewData struct {
// The fragment chains for current and upcoming scheduled paras.
FragmentChains map[parachaintypes.ParaID]fragmentchain.FragmentChain
}

type View struct {
// Per relay parent fragment chains. These includes all relay parents under the implicit view.
PerRelayParent map[common.Hash]RelayBlockViewData
// The hashes of the currently active leaves. This is a subset of the keys in
// `per_relay_parent`.
ActiveLeaves map[common.Hash]struct{}
// The backing implicit view.
ImplicitView backing.ImplicitView
}

// Initialize with empty values.
func NewView() *View {
return &View{
PerRelayParent: make(map[common.Hash]RelayBlockViewData),
ActiveLeaves: make(map[common.Hash]struct{}),
ImplicitView: nil, // TODO: currently there's no implementation for ImplicitView, reference is: https://github.com/paritytech/polkadot-sdk/blob/028e61be43f05f6f6c88c5cca94160f8db075585/polkadot/node/subsystem-util/src/backing_implicit_view.rs#L40
func NewView() *view {
return &view{
perRelayParent: make(map[common.Hash]*relayParentData),
activeLeaves: make(map[common.Hash]bool),
implicitView: nil, // TODO: currently there's no implementation for ImplicitView, reference is: https://github.com/paritytech/polkadot-sdk/blob/028e61be43f05f6f6c88c5cca94160f8db075585/polkadot/node/subsystem-util/src/backing_implicit_view.rs#L40
}
}

// Get the fragment chains of this leaf.
func (v *View) GetFragmentChains(leaf common.Hash) map[parachaintypes.ParaID]fragmentchain.FragmentChain {
if viewData, ok := v.PerRelayParent[leaf]; ok {
return viewData.FragmentChains
}
return nil
}

type ProspectiveParachains struct {
SubsystemToOverseer chan<- any
View *view
Expand All @@ -53,6 +29,7 @@ type ProspectiveParachains struct {
type view struct {
activeLeaves map[common.Hash]bool
perRelayParent map[common.Hash]*relayParentData
implicitView backing.ImplicitView
}

type relayParentData struct {
Expand Down Expand Up @@ -122,7 +99,7 @@ func (pp *ProspectiveParachains) processMessage(msg any) {
}

func (pp *ProspectiveParachains) introduceSecondedCandidate(
view *View,
view *view,
request IntroduceSecondedCandidateRequest,
response chan bool,
) {
Expand All @@ -140,11 +117,11 @@ func (pp *ProspectiveParachains) introduceSecondedCandidate(

candidateHash := parachaintypes.CandidateHash{Value: hash}

entry, err := fragmentchain.NewCandidateEntry(
entry, err := newCandidateEntry(
candidateHash,
candidate,
pvd,
fragmentchain.Seconded,
seconded,
)

if err != nil {
Expand All @@ -153,24 +130,24 @@ func (pp *ProspectiveParachains) introduceSecondedCandidate(
return
}

added := make([]common.Hash, 0, len(view.PerRelayParent))
added := make([]common.Hash, 0, len(view.perRelayParent))
paraScheduled := false

for relayParent, rpData := range view.PerRelayParent {
chain, exists := rpData.FragmentChains[para]
for relayParent, rpData := range view.perRelayParent {
chain, exists := rpData.fragmentChains[para]

if !exists {
continue
}

_, isActiveLeaf := view.ActiveLeaves[relayParent]
_, isActiveLeaf := view.activeLeaves[relayParent]

paraScheduled = true

err = chain.TryAddingSecondedCandidate(entry)
err = chain.tryAddingSecondedCandidate(entry)

if err != nil {
if errors.Is(err, fragmentchain.ErrCandidateAlradyKnown) {
if errors.Is(err, errCandidateAlreadyKnown) {
logger.Tracef(
"attempting to introduce an already known candidate with hash: %s, para: %v relayParent: %v isActiveLeaf: %v",
candidateHash,
Expand Down
27 changes: 12 additions & 15 deletions dot/parachain/prospective-parachains/prospective_parachains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"context"
"testing"

fragmentchain "github.com/ChainSafe/gossamer/dot/parachain/prospective-parachains/fragment-chain"
parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
inclusionemulator "github.com/ChainSafe/gossamer/dot/parachain/util/inclusion-emulator"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -125,24 +123,23 @@ func TestFailedIntroduceSecondedCandidateWhenParentHeadAndHeadDataEquals(

prospectiveParachains := NewProspectiveParachains(subsystemToOverseer)

relayParent := inclusionemulator.RelayChainBlockInfo{
relayParent := relayChainBlockInfo{
Hash: candidateRelayParent,
Number: 0,
StorageRoot: common.Hash{0x00},
}

baseConstraints := &inclusionemulator.Constraints{
baseConstraints := &parachaintypes.Constraints{
RequiredParent: parachaintypes.HeadData{Data: []byte{byte(0)}},
MinRelayParentNumber: 0,
ValidationCodeHash: parachaintypes.ValidationCodeHash(common.Hash{0x03}),
}

scope, err := fragmentchain.NewScopeWithAncestors(relayParent, baseConstraints, nil, 10, nil)
scope, err := newScopeWithAncestors(relayParent, baseConstraints, nil, 10, nil)
assert.NoError(t, err)

prospectiveParachains.View.PerRelayParent[candidateRelayParent] = RelayBlockViewData{
FragmentChains: map[parachaintypes.ParaID]fragmentchain.FragmentChain{
paraId: *fragmentchain.NewFragmentChain(scope, fragmentchain.NewCandidateStorage()),
prospectiveParachains.View.perRelayParent[candidateRelayParent] = &relayParentData{
fragmentChains: map[parachaintypes.ParaID]*fragmentChain{
paraId: newFragmentChain(scope, newCandidateStorage()),
},
}
go prospectiveParachains.Run(context.Background(), overseerToSubsystem)
Expand Down Expand Up @@ -180,24 +177,24 @@ func TestHandleIntroduceSecondedCandidate(

prospectiveParachains := NewProspectiveParachains(subsystemToOverseer)

relayParent := inclusionemulator.RelayChainBlockInfo{
relayParent := relayChainBlockInfo{
Hash: candidateRelayParent,
Number: 0,
StorageRoot: common.Hash{0x00},
}

baseConstraints := &inclusionemulator.Constraints{
baseConstraints := &parachaintypes.Constraints{
RequiredParent: parachaintypes.HeadData{Data: []byte{byte(0)}},
MinRelayParentNumber: 0,
ValidationCodeHash: parachaintypes.ValidationCodeHash(common.Hash{0x03}),
}

scope, err := fragmentchain.NewScopeWithAncestors(relayParent, baseConstraints, nil, 10, nil)
scope, err := newScopeWithAncestors(relayParent, baseConstraints, nil, 10, nil)
assert.NoError(t, err)

prospectiveParachains.View.PerRelayParent[candidateRelayParent] = RelayBlockViewData{
FragmentChains: map[parachaintypes.ParaID]fragmentchain.FragmentChain{
paraId: *fragmentchain.NewFragmentChain(scope, fragmentchain.NewCandidateStorage()),
prospectiveParachains.View.perRelayParent[candidateRelayParent] = &relayParentData{
fragmentChains: map[parachaintypes.ParaID]*fragmentChain{
paraId: newFragmentChain(scope, newCandidateStorage()),
},
}
go prospectiveParachains.Run(context.Background(), overseerToSubsystem)
Expand Down

0 comments on commit f8e5e9e

Please sign in to comment.