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

feat: implementing answerProspectiveValidationDataRequest #4516

Open
wants to merge 5 commits into
base: feat/parachain
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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/parachain/prospective-parachains/fragment_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (f *fragmentChain) tryAddingSecondedCandidate(entry *candidateEntry) error
}

// getHeadDataByHash tries to get the full head data associated with this hash
func (f *fragmentChain) getHeadDataByHash(headDataHash common.Hash) (*parachaintypes.HeadData, error) { //nolint:unused
func (f *fragmentChain) getHeadDataByHash(headDataHash common.Hash) (*parachaintypes.HeadData, error) {
reqParent := f.scope.baseConstraints.RequiredParent
reqParentHash, err := reqParent.Hash()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions dot/parachain/prospective-parachains/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (GetMinimumRelayParents) isProspectiveParachainsMessage() {}
// some fragment chain).
type GetProspectiveValidationData struct {
ProspectiveValidationDataRequest
Sender chan parachaintypes.PersistedValidationData
Sender chan<- *parachaintypes.PersistedValidationData
}

// ProspectiveValidationDataRequest A request for the persisted validation data stored in the prospective
Expand All @@ -153,15 +153,15 @@ type ParentHeadData interface {
}

// ParentHeadDataHash Parent head-data hash.
type ParentHeadDataHash common.Hash
type OnlyHash common.Hash

func (ParentHeadDataHash) isParentHeadData() {}
func (OnlyHash) isParentHeadData() {}

type ParentHeadDataWithHash struct {
// This will be provided for collations with elastic scaling enabled.
Data parachaintypes.HeadData
// Parent head-data hash.
Hash ParentHeadDataHash
Hash common.Hash
}

func (ParentHeadDataWithHash) isParentHeadData() {}
74 changes: 73 additions & 1 deletion dot/parachain/prospective-parachains/prospective-parachains.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (pp *ProspectiveParachains) processMessage(msg any) {
// Directly use the msg since it's already of type GetMinimumRelayParents
pp.getMinimumRelayParents(msg.RelayChainBlockHash, msg.Sender)
case GetProspectiveValidationData:
panic("not implemented yet: see issue #4313")
pp.answerProspectiveValidationDataRequest(msg.ProspectiveValidationDataRequest, msg.Sender)
default:
logger.Errorf("%w: %T", parachaintypes.ErrUnknownOverseerMessage, msg)
}
Expand Down Expand Up @@ -298,3 +298,75 @@ func (pp *ProspectiveParachains) getBackableCandidates(
// Send the result through the response channel
responseChan <- candidateHashes
}

func (pp *ProspectiveParachains) answerProspectiveValidationDataRequest(
request ProspectiveValidationDataRequest,
response chan<- *parachaintypes.PersistedValidationData,
) {
var headData *parachaintypes.HeadData
var parentHeadDataHash common.Hash

// extracting informations from the request depending on the incoming type.
switch value := request.ParentHeadData.(type) {
case OnlyHash:
parentHeadDataHash = common.Hash(value)
case ParentHeadDataWithHash:
headData = &value.Data
parentHeadDataHash = value.Hash
}

var relayParentInfo *relayChainBlockInfo
var maxPovSize *uint32

// // iterate over active leaves
for leaf := range pp.View.activeLeaves {
relayBlockViewData, exists := pp.View.perRelayParent[leaf]

if !exists {
continue
}

fragmentChain, exists := relayBlockViewData.fragmentChains[request.ParaId]
if !exists {
continue
}

// stop the iteration once we retrieve all the informations
if headData != nil && relayParentInfo != nil && maxPovSize != nil {
KaioApeles marked this conversation as resolved.
Show resolved Hide resolved
break
}

if relayParentInfo == nil {
relayParentInfo = fragmentChain.scope.ancestor(request.CandidateRelayParent)
}

if headData == nil {
var err error
headData, err = fragmentChain.getHeadDataByHash(parentHeadDataHash)

if err != nil {
response <- nil
return
}
}

if maxPovSize == nil {
containAncestor := fragmentChain.scope.ancestor(request.CandidateRelayParent) != nil

if containAncestor {
maxPovSize = &fragmentChain.scope.baseConstraints.MaxPoVSize
}
}
}

if headData != nil && relayParentInfo != nil && maxPovSize != nil {
response <- &parachaintypes.PersistedValidationData{
ParentHead: *headData,
RelayParentNumber: uint32(relayParentInfo.Number),
RelayParentStorageRoot: relayParentInfo.StorageRoot,
MaxPovSize: *maxPovSize,
}
} else {
response <- nil
}
}
245 changes: 245 additions & 0 deletions dot/parachain/prospective-parachains/prospective_parachains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,248 @@ func TestGetBackableCandidates(t *testing.T) {
})
}
}

func TestAnswerProspectiveValidationDataRequest(t *testing.T) {
// Create a new prospective parachains instance
subsystemToOverseer := make(chan any)

t.Run("no_active_leaves", func(t *testing.T) {
view := &view{
activeLeaves: map[common.Hash]bool{},
}

// Create a prospective validation data request
request := ProspectiveValidationDataRequest{
ParaId: parachaintypes.ParaID(1),
CandidateRelayParent: common.Hash{0x01},
ParentHeadData: ParentHeadDataWithHash{
Hash: common.Hash{0x01},
Data: parachaintypes.HeadData{Data: []byte{0x01}},
},
}

pp := &ProspectiveParachains{
View: view,
}

sender := make(chan *parachaintypes.PersistedValidationData, 1)

// Execute the method under test
pp.answerProspectiveValidationDataRequest(request, sender)

result := <-sender

assert.Nil(t, result, "Expected result to be nil when no active leaves are present")
})

t.Run("with_active_and_only_hash", func(t *testing.T) {
reqParent := parachaintypes.HeadData{Data: []byte{0xc3}}
reqParentHash, err := reqParent.Hash()
assert.NoError(t, err)

baseConstraints := &parachaintypes.Constraints{
RequiredParent: reqParent,
MinRelayParentNumber: 0,
ValidationCodeHash: parachaintypes.ValidationCodeHash(common.Hash{0x03}),
MaxPoVSize: 3,
}

scope, err := newScopeWithAncestors(
relayChainBlockInfo{
Hash: common.Hash{0x01},
StorageRoot: common.Hash{0x01},
Number: 2,
},
baseConstraints,
nil,
3,
[]relayChainBlockInfo{
{
Hash: common.Hash{0x012},
StorageRoot: common.Hash{0x022},
Number: 1,
},
},
)

assert.NoError(t, err)

view := &view{
activeLeaves: map[common.Hash]bool{
{0x01}: true,
},
perRelayParent: map[common.Hash]*relayParentData{
{0x01}: {
fragmentChains: map[parachaintypes.ParaID]*fragmentChain{
parachaintypes.ParaID(1): newFragmentChain(scope, newCandidateStorage()),
},
},
},
}

// Create a prospective validation data request
request := ProspectiveValidationDataRequest{
ParaId: parachaintypes.ParaID(1),
CandidateRelayParent: common.Hash{0x01},
ParentHeadData: OnlyHash(reqParentHash),
}

pp := &ProspectiveParachains{
View: view,
}

sender := make(chan *parachaintypes.PersistedValidationData, 1)

// Execute the method under test
pp.answerProspectiveValidationDataRequest(request, sender)

result := <-sender

assert.Equal(t, result.ParentHead, reqParent)
assert.Equal(t, result.RelayParentNumber, uint32(2))
assert.Equal(t, result.RelayParentStorageRoot, common.Hash{0x01})
assert.Equal(t, result.MaxPovSize, uint32(3))
})

t.Run("with_active_and_with_head_data", func(t *testing.T) {
reqParent := parachaintypes.HeadData{Data: []byte{0xc3}}
reqParentHash, err := reqParent.Hash()
assert.NoError(t, err)

baseConstraints := &parachaintypes.Constraints{
RequiredParent: reqParent,
MinRelayParentNumber: 0,
ValidationCodeHash: parachaintypes.ValidationCodeHash(common.Hash{0x03}),
MaxPoVSize: 3,
}

scope, err := newScopeWithAncestors(
relayChainBlockInfo{
Hash: common.Hash{0x01},
StorageRoot: common.Hash{0x01},
Number: 2,
},
baseConstraints,
nil,
3,
[]relayChainBlockInfo{
{
Hash: common.Hash{0x012},
StorageRoot: common.Hash{0x022},
Number: 1,
},
},
)

assert.NoError(t, err)

view := &view{
activeLeaves: map[common.Hash]bool{
{0x01}: true,
},
perRelayParent: map[common.Hash]*relayParentData{
{0x01}: {
fragmentChains: map[parachaintypes.ParaID]*fragmentChain{
parachaintypes.ParaID(1): newFragmentChain(scope, newCandidateStorage()),
},
},
},
}

// Create a prospective validation data request
request := ProspectiveValidationDataRequest{
ParaId: parachaintypes.ParaID(1),
CandidateRelayParent: common.Hash{0x01},
ParentHeadData: ParentHeadDataWithHash{
Data: reqParent,
Hash: reqParentHash,
},
}

pp := &ProspectiveParachains{
View: view,
}

sender := make(chan *parachaintypes.PersistedValidationData, 1)

// Execute the method under test
pp.answerProspectiveValidationDataRequest(request, sender)

result := <-sender

assert.Equal(t, result.ParentHead, reqParent)
assert.Equal(t, result.RelayParentNumber, uint32(2))
assert.Equal(t, result.RelayParentStorageRoot, common.Hash{0x01})
assert.Equal(t, result.MaxPovSize, uint32(3))
})

t.Run("with_head_data_hash_doesnt_match", func(t *testing.T) {
reqParent := parachaintypes.HeadData{Data: []byte{0xc4}}

baseConstraints := &parachaintypes.Constraints{
RequiredParent: reqParent,
MinRelayParentNumber: 0,
ValidationCodeHash: parachaintypes.ValidationCodeHash(common.Hash{0x03}),
MaxPoVSize: 3,
}

scope, err := newScopeWithAncestors(
relayChainBlockInfo{
Hash: common.Hash{0x01},
StorageRoot: common.Hash{0x01},
Number: 2,
},
baseConstraints,
nil,
3,
[]relayChainBlockInfo{
{
Hash: common.Hash{0x012},
StorageRoot: common.Hash{0x022},
Number: 1,
},
},
)

assert.NoError(t, err)

view := &view{
activeLeaves: map[common.Hash]bool{
{0x01}: true,
},
perRelayParent: map[common.Hash]*relayParentData{
{0x01}: {
fragmentChains: map[parachaintypes.ParaID]*fragmentChain{
parachaintypes.ParaID(1): newFragmentChain(scope, newCandidateStorage()),
},
},
},
}

// Create a prospective validation data request
request := ProspectiveValidationDataRequest{
ParaId: parachaintypes.ParaID(1),
CandidateRelayParent: common.Hash{0x01},
ParentHeadData: ParentHeadDataWithHash{
Data: reqParent,
Hash: common.Hash{0xc3},
},
}

pp := &ProspectiveParachains{
View: view,
}

sender := make(chan *parachaintypes.PersistedValidationData, 1)

// Execute the method under test
pp.answerProspectiveValidationDataRequest(request, sender)

result := <-sender

assert.NotEqual(t, result.ParentHead, common.Hash{0xc3})
})

// Close the channels
close(subsystemToOverseer)
}
Loading