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

Commit

Permalink
Revert "Make BEEFY payload extensible (#10307)"
Browse files Browse the repository at this point in the history
This reverts commit 8bf9836.
  • Loading branch information
niklasad1 committed Dec 2, 2021
1 parent a1de6df commit c17aee7
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 152 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 21 additions & 11 deletions client/beefy/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use wasm_timer::Instant;

use beefy_primitives::{
crypto::{Public, Signature},
VoteMessage,
MmrRootHash, VoteMessage,
};

use crate::keystore::BeefyKeystore;
Expand Down Expand Up @@ -142,7 +142,9 @@ where
sender: &PeerId,
mut data: &[u8],
) -> ValidationResult<B::Hash> {
if let Ok(msg) = VoteMessage::<NumberFor<B>, Public, Signature>::decode(&mut data) {
if let Ok(msg) =
VoteMessage::<MmrRootHash, NumberFor<B>, Public, Signature>::decode(&mut data)
{
let msg_hash = twox_64(data);
let round = msg.commitment.block_number;

Expand Down Expand Up @@ -176,7 +178,9 @@ where
fn message_expired<'a>(&'a self) -> Box<dyn FnMut(B::Hash, &[u8]) -> bool + 'a> {
let known_votes = self.known_votes.read();
Box::new(move |_topic, mut data| {
let msg = match VoteMessage::<NumberFor<B>, Public, Signature>::decode(&mut data) {
let msg = match VoteMessage::<MmrRootHash, NumberFor<B>, Public, Signature>::decode(
&mut data,
) {
Ok(vote) => vote,
Err(_) => return true,
};
Expand Down Expand Up @@ -210,7 +214,9 @@ where
return do_rebroadcast
}

let msg = match VoteMessage::<NumberFor<B>, Public, Signature>::decode(&mut data) {
let msg = match VoteMessage::<MmrRootHash, NumberFor<B>, Public, Signature>::decode(
&mut data,
) {
Ok(vote) => vote,
Err(_) => return true,
};
Expand All @@ -231,11 +237,9 @@ mod tests {
use sc_network_test::Block;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};

use beefy_primitives::{crypto::Signature, Commitment, MmrRootHash, VoteMessage, KEY_TYPE};

use crate::keystore::{tests::Keyring, BeefyKeystore};
use beefy_primitives::{
crypto::Signature, known_payload_ids, Commitment, MmrRootHash, Payload, VoteMessage,
KEY_TYPE,
};

use super::*;

Expand Down Expand Up @@ -341,7 +345,10 @@ mod tests {
}
}

fn sign_commitment<BN: Encode>(who: &Keyring, commitment: &Commitment<BN>) -> Signature {
fn sign_commitment<BN: Encode, P: Encode>(
who: &Keyring,
commitment: &Commitment<BN, P>,
) -> Signature {
let store: SyncCryptoStorePtr = std::sync::Arc::new(LocalKeystore::in_memory());
SyncCryptoStore::ecdsa_generate_new(&*store, KEY_TYPE, Some(&who.to_seed())).unwrap();
let beefy_keystore: BeefyKeystore = Some(store).into();
Expand All @@ -355,8 +362,11 @@ mod tests {
let sender = sc_network::PeerId::random();
let mut context = TestContext;

let payload = Payload::new(known_payload_ids::MMR_ROOT_ID, MmrRootHash::default().encode());
let commitment = Commitment { payload, block_number: 3_u64, validator_set_id: 0 };
let commitment = Commitment {
payload: MmrRootHash::default(),
block_number: 3_u64,
validator_set_id: 0,
};

let signature = sign_commitment(&Keyring::Alice, &commitment);

Expand Down
3 changes: 2 additions & 1 deletion client/beefy/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use sp_runtime::traits::{Block, NumberFor};
use parking_lot::Mutex;

/// Stream of signed commitments returned when subscribing.
pub type SignedCommitment<Block> = beefy_primitives::SignedCommitment<NumberFor<Block>>;
pub type SignedCommitment<Block> =
beefy_primitives::SignedCommitment<NumberFor<Block>, beefy_primitives::MmrRootHash>;

/// Stream of signed commitments returned when subscribing.
type SignedCommitmentStream<Block> = TracingUnboundedReceiver<SignedCommitment<Block>>;
Expand Down
36 changes: 18 additions & 18 deletions client/beefy/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ fn threshold(authorities: usize) -> usize {
authorities - faulty
}

pub(crate) struct Rounds<Payload, Number> {
rounds: BTreeMap<(Payload, Number), RoundTracker>,
pub(crate) struct Rounds<Hash, Number> {
rounds: BTreeMap<(Hash, Number), RoundTracker>,
validator_set: ValidatorSet<Public>,
}

impl<P, N> Rounds<P, N>
impl<H, N> Rounds<H, N>
where
P: Ord + Hash,
H: Ord + Hash,
N: Ord + AtLeast32BitUnsigned + MaybeDisplay,
{
pub(crate) fn new(validator_set: ValidatorSet<Public>) -> Self {
Expand All @@ -70,8 +70,8 @@ where

impl<H, N> Rounds<H, N>
where
H: Ord + Hash + Clone,
N: Ord + AtLeast32BitUnsigned + MaybeDisplay + Clone,
H: Ord + Hash,
N: Ord + AtLeast32BitUnsigned + MaybeDisplay,
{
pub(crate) fn validator_set_id(&self) -> ValidatorSetId {
self.validator_set.id
Expand All @@ -81,9 +81,9 @@ where
self.validator_set.validators.clone()
}

pub(crate) fn add_vote(&mut self, round: &(H, N), vote: (Public, Signature)) -> bool {
pub(crate) fn add_vote(&mut self, round: (H, N), vote: (Public, Signature)) -> bool {
if self.validator_set.validators.iter().any(|id| vote.0 == *id) {
self.rounds.entry(round.clone()).or_default().add_vote(vote)
self.rounds.entry(round).or_default().add_vote(vote)
} else {
false
}
Expand Down Expand Up @@ -179,29 +179,29 @@ mod tests {
let mut rounds = Rounds::<H256, NumberFor<Block>>::new(validators);

assert!(rounds.add_vote(
&(H256::from_low_u64_le(1), 1),
(H256::from_low_u64_le(1), 1),
(Keyring::Alice.public(), Keyring::Alice.sign(b"I am committed"))
));

assert!(!rounds.is_done(&(H256::from_low_u64_le(1), 1)));

// invalid vote
assert!(!rounds.add_vote(
&(H256::from_low_u64_le(1), 1),
(H256::from_low_u64_le(1), 1),
(Keyring::Dave.public(), Keyring::Dave.sign(b"I am committed"))
));

assert!(!rounds.is_done(&(H256::from_low_u64_le(1), 1)));

assert!(rounds.add_vote(
&(H256::from_low_u64_le(1), 1),
(H256::from_low_u64_le(1), 1),
(Keyring::Bob.public(), Keyring::Bob.sign(b"I am committed"))
));

assert!(!rounds.is_done(&(H256::from_low_u64_le(1), 1)));

assert!(rounds.add_vote(
&(H256::from_low_u64_le(1), 1),
(H256::from_low_u64_le(1), 1),
(Keyring::Charlie.public(), Keyring::Charlie.sign(b"I am committed"))
));

Expand All @@ -225,31 +225,31 @@ mod tests {

// round 1
rounds.add_vote(
&(H256::from_low_u64_le(1), 1),
(H256::from_low_u64_le(1), 1),
(Keyring::Alice.public(), Keyring::Alice.sign(b"I am committed")),
);
rounds.add_vote(
&(H256::from_low_u64_le(1), 1),
(H256::from_low_u64_le(1), 1),
(Keyring::Bob.public(), Keyring::Bob.sign(b"I am committed")),
);

// round 2
rounds.add_vote(
&(H256::from_low_u64_le(2), 2),
(H256::from_low_u64_le(2), 2),
(Keyring::Alice.public(), Keyring::Alice.sign(b"I am again committed")),
);
rounds.add_vote(
&(H256::from_low_u64_le(2), 2),
(H256::from_low_u64_le(2), 2),
(Keyring::Bob.public(), Keyring::Bob.sign(b"I am again committed")),
);

// round 3
rounds.add_vote(
&(H256::from_low_u64_le(3), 3),
(H256::from_low_u64_le(3), 3),
(Keyring::Alice.public(), Keyring::Alice.sign(b"I am still committed")),
);
rounds.add_vote(
&(H256::from_low_u64_le(3), 3),
(H256::from_low_u64_le(3), 3),
(Keyring::Bob.public(), Keyring::Bob.sign(b"I am still committed")),
);

Expand Down
15 changes: 7 additions & 8 deletions client/beefy/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ use sp_runtime::{

use beefy_primitives::{
crypto::{AuthorityId, Public, Signature},
known_payload_ids, BeefyApi, Commitment, ConsensusLog, MmrRootHash, Payload, SignedCommitment,
ValidatorSet, VersionedCommitment, VoteMessage, BEEFY_ENGINE_ID, GENESIS_AUTHORITY_SET_ID,
BeefyApi, Commitment, ConsensusLog, MmrRootHash, SignedCommitment, ValidatorSet,
VersionedCommitment, VoteMessage, BEEFY_ENGINE_ID, GENESIS_AUTHORITY_SET_ID,
};

use crate::{
Expand Down Expand Up @@ -79,7 +79,7 @@ where
/// Min delta in block numbers between two blocks, BEEFY should vote on
min_block_delta: u32,
metrics: Option<Metrics>,
rounds: round::Rounds<Payload, NumberFor<B>>,
rounds: round::Rounds<MmrRootHash, NumberFor<B>>,
finality_notifications: FinalityNotifications<B>,
/// Best block we received a GRANDPA notification for
best_grandpa_block: NumberFor<B>,
Expand Down Expand Up @@ -262,9 +262,8 @@ where
return
};

let payload = Payload::new(known_payload_ids::MMR_ROOT_ID, mmr_root.encode());
let commitment = Commitment {
payload,
payload: mmr_root,
block_number: notification.header.number(),
validator_set_id: self.rounds.validator_set_id(),
};
Expand Down Expand Up @@ -302,10 +301,10 @@ where
}
}

fn handle_vote(&mut self, round: (Payload, NumberFor<B>), vote: (Public, Signature)) {
fn handle_vote(&mut self, round: (MmrRootHash, NumberFor<B>), vote: (Public, Signature)) {
self.gossip_validator.note_round(round.1);

let vote_added = self.rounds.add_vote(&round, vote);
let vote_added = self.rounds.add_vote(round, vote);

if vote_added && self.rounds.is_done(&round) {
if let Some(signatures) = self.rounds.drop(&round) {
Expand Down Expand Up @@ -353,7 +352,7 @@ where
|notification| async move {
debug!(target: "beefy", "🥩 Got vote message: {:?}", notification);

VoteMessage::<NumberFor<B>, Public, Signature>::decode(
VoteMessage::<MmrRootHash, NumberFor<B>, Public, Signature>::decode(
&mut &notification.message[..],
)
.ok()
Expand Down
2 changes: 1 addition & 1 deletion primitives/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ sp-runtime = { version = "4.0.0-dev", path = "../runtime", default-features = fa
sp-std = { version = "4.0.0-dev", path = "../std", default-features = false }

[dev-dependencies]
hex = "0.4.3"
hex-literal = "0.3"

sp-keystore = { version = "0.10.0-dev", path = "../keystore" }

[features]
Expand Down
Loading

0 comments on commit c17aee7

Please sign in to comment.