-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Answer queries #65
Answer queries #65
Changes from 4 commits
feaaac7
75ec15b
2e9ade6
d055dad
c1ac719
b174a92
8674ae1
00c48c8
70197e3
72bb3d1
0d6d721
3d0f81b
0060027
c2c8b1c
4411de9
5d5a86a
296fd18
404186c
0e104ac
f234092
448c697
29a62dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,7 @@ impl<Vote: Eq, Signature: Eq> VoteMultiplicity<Vote, Signature> { | |
struct VoteTracker<Id: Hash + Eq, Vote, Signature> { | ||
votes: HashMap<Id, VoteMultiplicity<Vote, Signature>>, | ||
current_weight: u64, | ||
voted_at: Option<usize>, | ||
} | ||
|
||
/// Result of adding a vote. | ||
|
@@ -111,6 +112,7 @@ impl<Id: Hash + Eq + Clone, Vote: Clone + Eq, Signature: Clone + Eq> VoteTracker | |
VoteTracker { | ||
votes: HashMap::new(), | ||
current_weight: 0, | ||
voted_at: None, | ||
} | ||
} | ||
|
||
|
@@ -674,6 +676,30 @@ impl<Id, H, N, Signature> Round<Id, H, N, Signature> where | |
pub fn precommits(&self) -> Vec<(Id, Precommit<H, N>, Signature)> { | ||
self.precommit.votes() | ||
} | ||
|
||
/// Set the length of prevotes received at the moment of prevoting. | ||
pub fn set_prevote_idx(&mut self) -> bool { | ||
self.prevote.voted_at = Some(self.prevote.votes().len()); | ||
println!("set prevote idx to {:?}", self.prevote.voted_at); | ||
true | ||
} | ||
|
||
/// Set the length of precommits received at the moment of precommiting. | ||
pub fn set_precommit_idx(&mut self) -> bool { | ||
self.precommit.voted_at = Some(self.precommit.votes().len()); | ||
println!("set precommit idx to {:?}", self.precommit.voted_at); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
true | ||
} | ||
|
||
/// Get the length of prevotes received at the moment of prevoting. | ||
pub fn prevote_idx(&self) -> Option<usize> { | ||
self.prevote.voted_at | ||
} | ||
|
||
/// Get the length of precommits received at the moment of precommiting. | ||
pub fn precommit_idx(&self) -> Option<usize> { | ||
self.precommit.voted_at | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,12 @@ impl Voting { | |
} | ||
} | ||
|
||
pub struct HistoricalVotes<M> { | ||
seen: Vec<M>, | ||
prevote_idx: Option<usize>, | ||
precommit_idx: Option<usize>, | ||
} | ||
|
||
impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where | ||
H: Hash + Clone + Eq + Ord + ::std::fmt::Debug, | ||
N: Copy + BlockNumberOps + ::std::fmt::Debug, | ||
|
@@ -201,6 +207,16 @@ impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where | |
self.votes.voters() | ||
} | ||
|
||
/// Get the best block finalized in this round. | ||
pub(super) fn set_precommit_idx(&mut self, idx: usize) -> bool { | ||
self.votes.set_precommit_idx(idx) | ||
} | ||
|
||
/// Get the best block finalized in this round. | ||
pub(super) fn set_prevote_idx(&mut self, idx: usize) -> bool { | ||
self.votes.set_prevote_idx(idx) | ||
} | ||
|
||
/// Get the best block finalized in this round. | ||
pub(super) fn finalized(&self) -> Option<&(H, N)> { | ||
self.votes.finalized() | ||
|
@@ -256,7 +272,7 @@ impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where | |
} | ||
|
||
/// Return all imported votes for the round (prevotes and precommits). | ||
pub(super) fn votes(&self) -> Vec<SignedMessage<H, N, E::Signature, E::Id>> { | ||
pub(super) fn votes(&self) -> HistoricalVotes<SignedMessage<H, N, E::Signature, E::Id>> { | ||
let prevotes = self.votes.prevotes().into_iter().map(|(id, prevote, signature)| { | ||
SignedMessage { | ||
id, | ||
|
@@ -273,7 +289,13 @@ impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where | |
} | ||
}); | ||
|
||
prevotes.chain(precommits).collect() | ||
let prevotes_len = prevotes.len(); | ||
|
||
HistoricalVotes { | ||
seen: prevotes.chain(precommits).collect(), | ||
prevote_idx: self.votes.prevote_idx(), | ||
precommit_idx: self.votes.precommit_idx().map(|idx| idx + prevotes_len), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this logic is right. There is no guarantee that we see all prevotes before seeing any precommits. Since the estimate logic depends on both prevotes and precommits, we need an interleaved list where the indices are those we've voted at. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll fix it. I thought it shouldn't invalidate the votes. But it's ugly and not what was asked anyway. |
||
} | ||
} | ||
|
||
fn process_incoming(&mut self) -> Result<(), E::Error> { | ||
|
@@ -370,6 +392,7 @@ impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where | |
if let Some(prevote) = self.construct_prevote(last_round_state)? { | ||
debug!(target: "afg", "Casting prevote for round {}", self.votes.number()); | ||
self.env.prevoted(self.round_number(), prevote.clone())?; | ||
self.votes.set_prevote_idx(); | ||
self.outgoing.push(Message::Prevote(prevote)); | ||
} | ||
} | ||
|
@@ -422,6 +445,7 @@ impl<H, N, E: Environment<H, N>> VotingRound<H, N, E> where | |
debug!(target: "afg", "Casting precommit for round {}", self.votes.number()); | ||
let precommit = self.construct_precommit(); | ||
self.env.precommitted(self.round_number(), precommit.clone())?; | ||
self.votes.set_precommit_idx(); | ||
self.outgoing.push(Message::Precommit(precommit)); | ||
} | ||
self.state = Some(State::Precommitted); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stray println