From 6c0cbcf48025f57c25f172b8954f7460083c6c92 Mon Sep 17 00:00:00 2001 From: Kanishka Date: Fri, 6 Oct 2023 13:19:54 +0530 Subject: [PATCH] address review comments --- dot/parachain/dispute/scraping/inclusion.go | 38 +++++++++++---------- dot/parachain/dispute/scraping/scraping.go | 13 ++++--- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/dot/parachain/dispute/scraping/inclusion.go b/dot/parachain/dispute/scraping/inclusion.go index 25d0b78bf59..b160afd69d2 100644 --- a/dot/parachain/dispute/scraping/inclusion.go +++ b/dot/parachain/dispute/scraping/inclusion.go @@ -57,25 +57,27 @@ func (i *Inclusions) RemoveUpToHeight(blockNumber uint32, candidatesModified []c func (i *Inclusions) Get(candidateHash common.Hash) []Inclusion { var inclusionsAsSlice []Inclusion blocksIncluding, ok := i.inner[candidateHash] - if ok { - // Convert the map to a sorted slice for iteration - var sortedKeys []uint32 - for h := range blocksIncluding { - sortedKeys = append(sortedKeys, h) - } - sort.Slice(sortedKeys, func(i, j int) bool { - return sortedKeys[i] < sortedKeys[j] - }) + if !ok { + return inclusionsAsSlice + } - // Extract inclusions as a slice of structs - for _, height := range sortedKeys { - blocksAtHeight := blocksIncluding[height] - for _, block := range blocksAtHeight { - inclusionsAsSlice = append(inclusionsAsSlice, Inclusion{ - BlockNumber: height, - BlockHash: block, - }) - } + // Convert the map to a sorted slice for iteration + var sortedKeys []uint32 + for h := range blocksIncluding { + sortedKeys = append(sortedKeys, h) + } + sort.Slice(sortedKeys, func(i, j int) bool { + return sortedKeys[i] < sortedKeys[j] + }) + + // Extract inclusions as a slice of structs + for _, height := range sortedKeys { + blocksAtHeight := blocksIncluding[height] + for _, block := range blocksAtHeight { + inclusionsAsSlice = append(inclusionsAsSlice, Inclusion{ + BlockNumber: height, + BlockHash: block, + }) } } diff --git a/dot/parachain/dispute/scraping/scraping.go b/dot/parachain/dispute/scraping/scraping.go index adc665e8bb0..d3de9d9e4af 100644 --- a/dot/parachain/dispute/scraping/scraping.go +++ b/dot/parachain/dispute/scraping/scraping.go @@ -70,7 +70,7 @@ func (cs *ChainScraper) ProcessActiveLeavesUpdate( } earliestBlockNumber := update.Activated.Number - uint32(len(ancestors)) - var blockNumbers []uint32 + blockNumbers := make([]uint32, 0, update.Activated.Number-earliestBlockNumber+1) for i := update.Activated.Number; i >= earliestBlockNumber; i-- { blockNumbers = append(blockNumbers, i) } @@ -129,6 +129,10 @@ func (cs *ChainScraper) ProcessCandidateEvents( return nil, fmt.Errorf("getting candidate events: %w", err) } + if events == nil { + return nil, nil + } + for _, event := range events.Types { candidateEvents = append(candidateEvents, parachainTypes.CandidateEvent(event)) } @@ -301,9 +305,8 @@ func getBlockAncestors( // saturatingSub returns the result of a - b, saturating at 0. func saturatingSub(a, b uint32) uint32 { - result := int(a) - int(b) - if result < 0 { - return 0 + if a > b { + return a - b } - return uint32(result) + return 0 }