11package votecounter
22
33import (
4- "iter"
5-
64 "github.com/NethermindEth/juno/consensus/types"
75 "github.com/NethermindEth/juno/utils"
86)
@@ -42,23 +40,6 @@ func New[V types.Hashable[H], H types.Hash, A types.Addr](validators Validators[
4240 }
4341}
4442
45- func (v * VoteCounter [V , H , A ]) LoadFromMessages (messages iter.Seq2 [types.Message [V , H , A ], error ]) error {
46- for msg , err := range messages {
47- if err != nil {
48- return err
49- }
50- switch msg := msg .(type ) {
51- case * types.Proposal [V , H , A ]:
52- v .AddProposal (msg )
53- case * types.Prevote [H , A ]:
54- v .AddPrevote (msg )
55- case * types.Precommit [H , A ]:
56- v .AddPrecommit (msg )
57- }
58- }
59- return nil
60- }
61-
6243func (v * VoteCounter [V , H , A ]) StartNewHeight () {
6344 v .currentHeight ++
6445 v .totalVotingPower = v .validators .TotalVotingPower (v .currentHeight )
@@ -74,23 +55,26 @@ func (v *VoteCounter[V, H, A]) StartNewHeight() {
7455 }
7556}
7657
77- func (v * VoteCounter [V , H , A ]) getRoundDataAndVotingPower (header * types.MessageHeader [A ]) (* roundData [V , H , A ], types.VotingPower , bool ) {
78- if header .Height < v .currentHeight {
79- return nil , 0 , false
58+ func (v * VoteCounter [V , H , A ]) getRoundData (
59+ height types.Height ,
60+ round types.Round ,
61+ ) (* roundData [V , H , A ], bool ) {
62+ if height < v .currentHeight {
63+ return nil , false
8064 }
8165
82- votingPower := v .validators .ValidatorVotingPower (header .Height , & header .Sender )
83-
84- if header .Height == v .currentHeight {
85- return getOrCreateRoundData (v .roundData , header .Round ), votingPower , true
66+ var roundData roundMap [V , H , A ]
67+ if height == v .currentHeight {
68+ roundData = v .roundData
69+ } else {
70+ var ok bool
71+ if roundData , ok = v .futureMessages [height ]; ! ok {
72+ roundData = make (roundMap [V , H , A ])
73+ v .futureMessages [height ] = roundData
74+ }
8675 }
8776
88- futureRoundMap , ok := v .futureMessages [header .Height ]
89- if ! ok {
90- futureRoundMap = make (roundMap [V , H , A ])
91- v .futureMessages [header .Height ] = futureRoundMap
92- }
93- return getOrCreateRoundData (futureRoundMap , header .Round ), votingPower , true
77+ return getOrCreateRoundData (roundData , round ), true
9478}
9579
9680func getOrCreateRoundData [V types.Hashable [H ], H types.Hash , A types.Addr ](
@@ -106,7 +90,7 @@ func getOrCreateRoundData[V types.Hashable[H], H types.Hash, A types.Addr](
10690}
10791
10892func (v * VoteCounter [V , H , A ]) AddProposal (proposal * types.Proposal [V , H , A ]) bool {
109- roundData , votingPower , ok := v .getRoundDataAndVotingPower ( & proposal .MessageHeader )
93+ roundData , ok := v .getRoundData ( proposal .Height , proposal . Round )
11094 if ! ok {
11195 return false
11296 }
@@ -115,24 +99,30 @@ func (v *VoteCounter[V, H, A]) AddProposal(proposal *types.Proposal[V, H, A]) bo
11599 return false
116100 }
117101
102+ votingPower := v .validators .ValidatorVotingPower (proposal .Height , & proposal .Sender )
103+
118104 return roundData .setProposal (proposal , votingPower )
119105}
120106
121107func (v * VoteCounter [V , H , A ]) AddPrevote (prevote * types.Prevote [H , A ]) bool {
122- roundData , votingPower , ok := v .getRoundDataAndVotingPower ( & prevote .MessageHeader )
108+ roundData , ok := v .getRoundData ( prevote .Height , prevote . Round )
123109 if ! ok {
124110 return false
125111 }
126112
113+ votingPower := v .validators .ValidatorVotingPower (prevote .Height , & prevote .Sender )
114+
127115 return roundData .addVote ((* types.Vote [H , A ])(prevote ), votingPower , Prevote )
128116}
129117
130118func (v * VoteCounter [V , H , A ]) AddPrecommit (precommit * types.Precommit [H , A ]) bool {
131- roundData , votingPower , ok := v .getRoundDataAndVotingPower ( & precommit .MessageHeader )
119+ roundData , ok := v .getRoundData ( precommit .Height , precommit . Round )
132120 if ! ok {
133121 return false
134122 }
135123
124+ votingPower := v .validators .ValidatorVotingPower (precommit .Height , & precommit .Sender )
125+
136126 return roundData .addVote ((* types.Vote [H , A ])(precommit ), votingPower , Precommit )
137127}
138128
@@ -163,6 +153,19 @@ func (v *VoteCounter[V, H, A]) HasQuorumForAny(round types.Round, voteType VoteT
163153 return roundData .countAny (voteType ) >= v .quorumVotingPower
164154}
165155
156+ func (v * VoteCounter [V , H , A ]) HasFuturePrecommitQuorum (
157+ height types.Height ,
158+ round types.Round ,
159+ id * H ,
160+ ) bool {
161+ roundData , ok := v .getRoundData (height , round )
162+ if ! ok {
163+ return false
164+ }
165+
166+ return roundData .countVote (Precommit , id ) >= v .quorumVotingPower
167+ }
168+
166169func (v * VoteCounter [V , H , A ]) HasNonFaultyFutureMessage (round types.Round ) bool {
167170 roundData , ok := v .roundData [round ]
168171 if ! ok {
0 commit comments