Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make BEEFY payload extensible #10307

Merged
merged 21 commits into from
Dec 1, 2021

Conversation

seunlanlege
Copy link
Contributor

@seunlanlege seunlanlege commented Nov 18, 2021

@seunlanlege seunlanlege changed the title Make BEEFY payload extensible closes paritytech/grandpa-bridge-gadget#198 Make BEEFY payload extensible Nov 18, 2021
@adoerr adoerr added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Nov 23, 2021
@adoerr adoerr requested a review from tomusdrw November 23, 2021 05:14
client/beefy/src/lib.rs Outdated Show resolved Hide resolved
client/beefy/src/lib.rs Outdated Show resolved Hide resolved
client/beefy/src/lib.rs Outdated Show resolved Hide resolved
client/beefy/src/lib.rs Outdated Show resolved Hide resolved
primitives/beefy/src/commitment.rs Outdated Show resolved Hide resolved
primitives/beefy/src/commitment.rs Outdated Show resolved Hide resolved
@tomusdrw tomusdrw removed the D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit label Nov 25, 2021
seunlanlege and others added 4 commits November 26, 2021 12:13
Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
Copy link
Contributor

@tomusdrw tomusdrw left a 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.

/// 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(
Copy link
Contributor

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.

primitives/beefy/src/commitment.rs Outdated Show resolved Hide resolved
primitives/beefy/src/commitment.rs Outdated Show resolved Hide resolved
Comment on lines 265 to 266
let mut payload = Payload::default();
payload.push(known_payload_ids::MMR_ROOT_ID, mmr_root);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

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);
Copy link
Contributor

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.

/// 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) {
Copy link
Contributor

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 Show resolved Hide resolved
/// 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, _)| {
Copy link
Contributor

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.

primitives/beefy/src/commitment.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@tomusdrw tomusdrw left a 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 :)

client/beefy/src/gossip.rs Outdated Show resolved Hide resolved
primitives/beefy/src/commitment.rs Outdated Show resolved Hide resolved
@tomusdrw
Copy link
Contributor

tomusdrw commented Dec 1, 2021

bot merge

@paritytech-processbot paritytech-processbot bot merged commit 8bf9836 into paritytech:master Dec 1, 2021
niklasad1 added a commit that referenced this pull request Dec 2, 2021
AurevoirXavier pushed a commit to darwinia-network/substrate that referenced this pull request Jan 27, 2022
* 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>
grishasobol pushed a commit to gear-tech/substrate that referenced this pull request Mar 28, 2022
* 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>
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* 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>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Change commitment payload to make it possible to easily add new items in the future.
3 participants