Skip to content

Commit

Permalink
feat(prospective-parachains): implements GetBackableCandidates (#4374)
Browse files Browse the repository at this point in the history
Co-authored-by: EclesioMeloJunior <[email protected]>
  • Loading branch information
DanielDDHM and EclesioMeloJunior authored Jan 14, 2025
1 parent 3225678 commit e9f3d8d
Show file tree
Hide file tree
Showing 2 changed files with 444 additions and 1 deletion.
87 changes: 86 additions & 1 deletion dot/parachain/prospective-parachains/prospective-parachains.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import (

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 ProspectiveParachains struct {
SubsystemToOverseer chan<- any
View *view
}

type view struct {
activeLeaves map[common.Hash]bool
perRelayParent map[common.Hash]*relayParentData
}

type relayParentData struct {
fragmentChains map[parachaintypes.ParaID]*fragmentChain
}

// Name returns the name of the subsystem
Expand Down Expand Up @@ -57,7 +68,7 @@ func (pp *ProspectiveParachains) processMessage(msg any) {
case CandidateBacked:
panic("not implemented yet: see issue #4309")
case GetBackableCandidates:
panic("not implemented yet: see issue #4310")
pp.getBackableCandidates(msg)
case GetHypotheticalMembership:
panic("not implemented yet: see issue #4311")
case GetMinimumRelayParents:
Expand All @@ -80,3 +91,77 @@ func (*ProspectiveParachains) ProcessBlockFinalizedSignal(parachaintypes.BlockFi
// NOTE: this subsystem does not process block finalized signal
return nil
}

func (pp *ProspectiveParachains) getBackableCandidates(
msg GetBackableCandidates,
) {
// Extract details from the message
relayParentHash := msg.RelayParentHash
paraId := msg.ParaId
requestedQty := msg.RequestedQty
ancestors := msg.Ancestors
responseChan := msg.Response

// Check if the relay parent is active
if _, exists := pp.View.activeLeaves[relayParentHash]; !exists {
logger.Debugf(
"Requested backable candidates for inactive relay-parent. "+
"RelayParentHash: %v, ParaId: %v",
relayParentHash, paraId,
)
responseChan <- []parachaintypes.CandidateHashAndRelayParent{}
return
}

// Retrieve data for the relay parent
data, ok := pp.View.perRelayParent[relayParentHash]
if !ok {
logger.Debugf(
"Requested backable candidates for nonexistent relay-parent. "+
"RelayParentHash: %v, ParaId: %v",
relayParentHash, paraId,
)
responseChan <- []parachaintypes.CandidateHashAndRelayParent{}
return
}

// Retrieve the fragment chain for the ParaID
chain, ok := data.fragmentChains[paraId]
if !ok {
logger.Debugf(
"Requested backable candidates for inactive ParaID. "+
"RelayParentHash: %v, ParaId: %v",
relayParentHash, paraId,
)
responseChan <- []parachaintypes.CandidateHashAndRelayParent{}
return
}

// Retrieve backable candidates from the fragment chain
backableCandidates := chain.findBackableChain(ancestors, requestedQty)
if len(backableCandidates) == 0 {
logger.Debugf(
"No backable candidates found. RelayParentHash: %v, ParaId: %v, Ancestors: %v",
relayParentHash, paraId, ancestors,
)
responseChan <- []parachaintypes.CandidateHashAndRelayParent{}
return
}

logger.Debugf(
"Found backable candidates: %v. RelayParentHash: %v, ParaId: %v, Ancestors: %v",
backableCandidates, relayParentHash, paraId, ancestors,
)

// Convert backable candidates to the expected response format
candidateHashes := make([]parachaintypes.CandidateHashAndRelayParent, len(backableCandidates))
for i, candidate := range backableCandidates {
candidateHashes[i] = parachaintypes.CandidateHashAndRelayParent{
CandidateHash: candidate.candidateHash,
CandidateRelayParent: candidate.realyParentHash,
}
}

// Send the result through the response channel
responseChan <- candidateHashes
}
Loading

0 comments on commit e9f3d8d

Please sign in to comment.