1
1
package votecounter
2
2
3
3
import (
4
- "iter"
5
-
6
4
"github.com/NethermindEth/juno/consensus/types"
7
5
"github.com/NethermindEth/juno/utils"
8
6
)
@@ -42,23 +40,6 @@ func New[V types.Hashable[H], H types.Hash, A types.Addr](validators Validators[
42
40
}
43
41
}
44
42
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
-
62
43
func (v * VoteCounter [V , H , A ]) StartNewHeight () {
63
44
v .currentHeight ++
64
45
v .totalVotingPower = v .validators .TotalVotingPower (v .currentHeight )
@@ -74,23 +55,26 @@ func (v *VoteCounter[V, H, A]) StartNewHeight() {
74
55
}
75
56
}
76
57
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
80
64
}
81
65
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
+ }
86
75
}
87
76
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
94
78
}
95
79
96
80
func 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](
106
90
}
107
91
108
92
func (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 )
110
94
if ! ok {
111
95
return false
112
96
}
@@ -115,24 +99,30 @@ func (v *VoteCounter[V, H, A]) AddProposal(proposal *types.Proposal[V, H, A]) bo
115
99
return false
116
100
}
117
101
102
+ votingPower := v .validators .ValidatorVotingPower (proposal .Height , & proposal .Sender )
103
+
118
104
return roundData .setProposal (proposal , votingPower )
119
105
}
120
106
121
107
func (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 )
123
109
if ! ok {
124
110
return false
125
111
}
126
112
113
+ votingPower := v .validators .ValidatorVotingPower (prevote .Height , & prevote .Sender )
114
+
127
115
return roundData .addVote ((* types.Vote [H , A ])(prevote ), votingPower , Prevote )
128
116
}
129
117
130
118
func (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 )
132
120
if ! ok {
133
121
return false
134
122
}
135
123
124
+ votingPower := v .validators .ValidatorVotingPower (precommit .Height , & precommit .Sender )
125
+
136
126
return roundData .addVote ((* types.Vote [H , A ])(precommit ), votingPower , Precommit )
137
127
}
138
128
@@ -163,6 +153,19 @@ func (v *VoteCounter[V, H, A]) HasQuorumForAny(round types.Round, voteType VoteT
163
153
return roundData .countAny (voteType ) >= v .quorumVotingPower
164
154
}
165
155
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
+
166
169
func (v * VoteCounter [V , H , A ]) HasNonFaultyFutureMessage (round types.Round ) bool {
167
170
roundData , ok := v .roundData [round ]
168
171
if ! ok {
0 commit comments