From 8d66bdf078d51e5b00b56156033b359bb84e4198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CGheisMohammadi=E2=80=9D?= <36589218+GheisMohammadi@users.noreply.github.com> Date: Thu, 28 Sep 2023 00:24:16 +0800 Subject: [PATCH] add getBlockByMaxVote to sync helper --- api/service/stagedstreamsync/helpers.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/api/service/stagedstreamsync/helpers.go b/api/service/stagedstreamsync/helpers.go index 75e504214..96c1c22b0 100644 --- a/api/service/stagedstreamsync/helpers.go +++ b/api/service/stagedstreamsync/helpers.go @@ -73,6 +73,27 @@ func checkGetBlockByHashesResult(blocks []*types.Block, hashes []common.Hash) er return nil } +func getBlockByMaxVote(blocks []*types.Block) (*types.Block, error) { + hashesVote := make(map[common.Hash]int) + maxVote := int(-1) + maxVotedBlockIndex := int(0) + + for i, block := range blocks { + if block == nil { + continue + } + hashesVote[block.Header().Hash()]++ + if hashesVote[block.Header().Hash()] > maxVote { + maxVote = hashesVote[block.Header().Hash()] + maxVotedBlockIndex = i + } + } + if maxVote < 0 { + return nil, ErrInvalidBlockBytes + } + return blocks[maxVotedBlockIndex], nil +} + func countHashMaxVote(m map[sttypes.StreamID]common.Hash, whitelist map[sttypes.StreamID]struct{}) (common.Hash, map[sttypes.StreamID]struct{}) { var ( voteM = make(map[common.Hash]int)