-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Conversation
Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
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.
Thanks for the fixes, left a few additional suggestions.
primitives/beefy/src/commitment.rs
Outdated
/// Returns a raw payload under given `id`. If the `BeefyPayloadId` is not found in the payload | ||
/// `None` is returned. | ||
pub fn get_raw(&self, id: &BeefyPayloadId) -> Option<&Vec<u8>> { | ||
self.0.iter().find_map( |
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.
since this is sorted you can do binary_search
, however with such small number of elements it wouldn't really matter.
client/beefy/src/worker.rs
Outdated
let mut payload = Payload::default(); | ||
payload.push(known_payload_ids::MMR_ROOT_ID, mmr_root); |
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.
nit: we could have Payload::new
to simplify this and avoid marking payload
as mut
.
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.
like
let payload = Payload::new().push(known_payload_ids::MMR_ROOT_ID, mmr_root);
technically that requires modifying the push
method to take self
and return it so we can chain multiple calls to push
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.
Yeah, or just Payload::new(known_payload_ids::MMR_ROOT_ID, mmr_root);
- that additionally ensure that no empty payload can be created.
client/beefy/src/worker.rs
Outdated
self.gossip_validator.note_round(round.1); | ||
|
||
let vote_added = self.rounds.add_vote(round, vote); | ||
let vote_added = self.rounds.add_vote(round.clone(), vote); |
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.
I think it would be better to change add_vote
to clone internally only when the vote is actually going to be added.
primitives/beefy/src/commitment.rs
Outdated
/// Push a value that implements [`codec::Encode`] with a given id | ||
/// to the back of the payload vec. This method will internally sort the payload vec | ||
/// after every push. | ||
pub fn push<T: Encode>(&mut self, id: BeefyPayloadId, value: T) { |
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.
I was thinking about that kind of API as well when making a proposal, but the thing is that Vec<u8>
implements Encode
as well, so I think it might actually be error prone to have something like that (i.e. we might end up with double-encoded vectors).
I'd prefer to just have add_raw
method.
primitives/beefy/src/commitment.rs
Outdated
/// after every push. | ||
pub fn push<T: Encode>(&mut self, id: BeefyPayloadId, value: T) { | ||
self.0.push((id, value.encode())); | ||
self.0.sort_by(|(id_1, _), (id_2, _)| { |
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.
Since this is guaranteed to be sorted you can actually use binary_search
to find the right position instead of sorting the entire vector every time.
However given that we don't expect many values there performance wise it probably does not matter.
Using binary_search
though would allow you to easily detect duplicates (which we should do), so I'd suggest changing that.
Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
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.
Thank you! One last tiny change request :)
Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
bot merge |
* make BEEFY payload extensible * cargo fmt * cargo fmt * remove generic payload param in beefy-primitives * cargo fmt * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * adds Paylaod Type * remove hex * fix tests * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * use binary_search_by to sort * Payload::new() * fix tests * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * fix tests * cargo fmt * fix get_decoded * fix test Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
* make BEEFY payload extensible * cargo fmt * cargo fmt * remove generic payload param in beefy-primitives * cargo fmt * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * adds Paylaod Type * remove hex * fix tests * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * use binary_search_by to sort * Payload::new() * fix tests * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * fix tests * cargo fmt * fix get_decoded * fix test Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
* make BEEFY payload extensible * cargo fmt * cargo fmt * remove generic payload param in beefy-primitives * cargo fmt * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * adds Paylaod Type * remove hex * fix tests * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * use binary_search_by to sort * Payload::new() * fix tests * Apply suggestions from code review Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com> * fix tests * cargo fmt * fix get_decoded * fix test Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
closes paritytech/grandpa-bridge-gadget#198