Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Improve performance of parachain relay (paritytech#711)
Browse files Browse the repository at this point in the history
Co-authored-by: David Dunn <26876072+doubledup@users.noreply.github.com>
Co-authored-by: Alistair Singh <alistair.singh7@gmail.com>
  • Loading branch information
3 people authored Nov 1, 2022
1 parent dc50c3d commit 6d2f7c3
Show file tree
Hide file tree
Showing 6 changed files with 659 additions and 612 deletions.
48 changes: 22 additions & 26 deletions relayer/chain/relaychain/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,35 +155,24 @@ type ParaHead struct {
}

// Fetches heads for each parachain Id filtering out para threads.
func (conn *Connection) FetchParachainHeads(paraId uint32, relayChainBlockHash types.Hash) ([]ParaHead, *types.Header, error) {
func (conn *Connection) FetchParachainHeads(relayChainBlockHash types.Hash) ([]ParaHead, error) {
// Fetch para heads
paraHeads, err := conn.fetchParaHeads(relayChainBlockHash)
if err != nil {
log.WithError(err).Error("Cannot fetch para heads.")
return nil, nil, err
}

// Make sure our snowbridge para head is included
log.WithField("relayChainBlockHash", relayChainBlockHash).WithField("paraId", paraId).Info("Fetching parachain heads.")
if _, ok := paraHeads[paraId]; !ok {
return nil, nil, fmt.Errorf("snowbridge is not a registered parachain")
}

var snowbridgeHeader types.Header
if err := types.DecodeFromBytes(paraHeads[paraId].Data, &snowbridgeHeader); err != nil {
return nil, nil, fmt.Errorf("decode parachain header: %w", err)
return nil, err
}

// fetch ids of parachains (not including parathreads)
var parachainIDs []uint32
parachainsKey, err := types.CreateStorageKey(conn.Metadata(), "Paras", "Parachains", nil, nil)
if err != nil {
return nil, nil, err
return nil, err
}

_, err = conn.API().RPC.State.GetStorage(parachainsKey, &parachainIDs, relayChainBlockHash)
if err != nil {
return nil, nil, err
return nil, err
}

// filter out parathreads
Expand All @@ -193,37 +182,44 @@ func (conn *Connection) FetchParachainHeads(paraId uint32, relayChainBlockHash t
parachainHeads = append(parachainHeads, head)
}
}
return parachainHeads, &snowbridgeHeader, nil
return parachainHeads, nil
}

func (co *Connection) FetchFinalizedParaHead(relayBlockhash types.Hash, paraID uint32) (*types.Header, error) {
func (co *Connection) FetchParachainHead(relayBlockhash types.Hash, paraID uint32, header *types.Header) (bool, error) {
encodedParaID, err := types.EncodeToBytes(paraID)
if err != nil {
return nil, err
return false, err
}

storageKey, err := types.CreateStorageKey(co.Metadata(), "Paras", "Heads", encodedParaID, nil)
if err != nil {
return nil, err
return false, err
}

var headerBytes types.Bytes
ok, err := co.API().RPC.State.GetStorage(storageKey, &headerBytes, relayBlockhash)
if err != nil {
return nil, err
return false, err
}

if !ok {
return nil, fmt.Errorf("parachain head not found")
return false, nil
}

var header types.Header
if err := types.DecodeFromBytes(headerBytes, &header); err != nil {
log.WithError(err).Error("Failed to decode Header")
return nil, err
if err := types.DecodeFromBytes(headerBytes, header); err != nil {
return false, err
}

return &header, nil
return true, nil
}

func (co *Connection) IsParachainRegistered(relayBlockHash types.Hash, paraID uint32) (bool, error) {
var header types.Header
ok, err := co.FetchParachainHead(relayBlockHash, paraID, &header)
if err != nil {
return false, fmt.Errorf("fetch parachain header: %w", err)
}
return ok, nil
}

func (co *Connection) FetchMMRLeafCount(relayBlockhash types.Hash) (uint64, error) {
Expand Down
28 changes: 21 additions & 7 deletions relayer/cmd/parachain_head_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"errors"
"fmt"

log "github.com/sirupsen/logrus"
"github.com/snowfork/go-substrate-rpc-client/v4/types"
Expand Down Expand Up @@ -83,7 +84,7 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
"mmrProof": mmrProof,
}).Info("conn.GenerateProofForBlock")

paraId, _ := cmd.Flags().GetUint32("parachain-id")
paraID, _ := cmd.Flags().GetUint32("parachain-id")
parachainBlock, _ := cmd.Flags().GetUint64("parachain-block")

relayChainBlockHash, err := conn.API().RPC.Chain.GetBlockHash(relayChainBlock)
Expand All @@ -92,31 +93,44 @@ func ParachainHeadProofFn(cmd *cobra.Command, _ []string) error {
return err
}

paraHeadsAsSlice, parachainHeader, err := conn.FetchParachainHeads(paraId, relayChainBlockHash)
paraHeadsAsSlice, err := conn.FetchParachainHeads(relayChainBlockHash)
if err != nil {
log.WithError(err).Error("Cannot fetch parachain head.")
log.WithError(err).Error("Cannot fetch parachain headers")
return err
}

var parachainHeader types.Header
ok, err := conn.FetchParachainHead(relayChainBlockHash, paraID, &parachainHeader)
if err != nil {
log.WithError(err).Error("Cannot fetch our parachain header")
return err
}

if !ok {
log.WithError(err).Error("parachain is not registered")
return fmt.Errorf("parachain is not registered")
}

log.WithFields(log.Fields{
"paraHeadsAsSlice": paraHeadsAsSlice,
"parachainHeader": parachainHeader,
"paraId": paraId,
"paraId": paraID,
"relayChainBlockHash": relayChainBlockHash.Hex(),
}).Info("parachain.CreateParachainMerkleProof")

merkleProofData, err := parachain.CreateParachainMerkleProof(paraHeadsAsSlice, paraId)
merkleProofData, err := parachain.CreateParachainMerkleProof(paraHeadsAsSlice, paraID)
if err != nil {
log.WithError(err).Error("Cannot create merkle proof.")
return err
}
log.WithFields(log.Fields{
"paraHeadsAsSlice": paraHeadsAsSlice,
"paraId": paraId,
"paraId": paraID,
"merkleProofData": merkleProofData,
}).Info("parachain.CreateParachainMerkleProof")

log.WithFields(log.Fields{
"parachainId": paraId,
"parachainId": paraID,
"relaychainBlockHash": relayChainBlockHash.Hex(),
"relaychainBlockNumber": relayChainBlock,
"parachainBlockNumber": parachainBlock,
Expand Down
Loading

0 comments on commit 6d2f7c3

Please sign in to comment.