github.com/koko1123/flow-go-1@v0.29.6/consensus/hotstuff/voteaggregator/pending_status.go (about)

     1  package voteaggregator
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/consensus/hotstuff/model"
     5  	"github.com/koko1123/flow-go-1/model/flow"
     6  )
     7  
     8  // PendingVotes stores all the pending votes for different block proposals
     9  type PendingVotes struct {
    10  	// maps block ID to pending status for that block
    11  	votes map[flow.Identifier]*PendingStatus
    12  }
    13  
    14  // PendingStatus keeps track of pending votes for the same block
    15  type PendingStatus struct {
    16  	// When receiving missing block, first received votes will be accumulated
    17  	orderedVotes []*model.Vote
    18  	// For avoiding duplicate votes
    19  	voteMap map[flow.Identifier]struct{}
    20  }
    21  
    22  // AddVote adds a vote as a pending vote
    23  // returns true if it can be added to a PendingStatus successfully
    24  // returns false otherwise
    25  func (pv *PendingVotes) AddVote(vote *model.Vote) bool {
    26  	status, exists := pv.votes[vote.BlockID]
    27  	if !exists {
    28  		status = NewPendingStatus()
    29  		pv.votes[vote.BlockID] = status
    30  	}
    31  	return status.AddVote(vote)
    32  }
    33  
    34  // AddVote adds a vote as a pending vote
    35  // returns false if it has been added before
    36  // returns true otherwise
    37  func (ps *PendingStatus) AddVote(vote *model.Vote) bool {
    38  	_, exists := ps.voteMap[vote.ID()]
    39  	if exists {
    40  		return false
    41  	}
    42  	ps.voteMap[vote.ID()] = struct{}{}
    43  	ps.orderedVotes = append(ps.orderedVotes, vote)
    44  	return true
    45  }
    46  
    47  // NewPendingVotes creates a PendingVotes instance
    48  func NewPendingVotes() *PendingVotes {
    49  	return &PendingVotes{votes: make(map[flow.Identifier]*PendingStatus)}
    50  }
    51  
    52  // NewPendingStatus creates a PendingStatus instance
    53  func NewPendingStatus() *PendingStatus {
    54  	return &PendingStatus{voteMap: make(map[flow.Identifier]struct{})}
    55  }