From 292bfac4147f3065d85bd267a34a408461f6e357 Mon Sep 17 00:00:00 2001 From: ordian Date: Wed, 9 Oct 2024 17:30:15 +0200 Subject: [PATCH] Fix u256 conversion in BABE (#5994) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/paritytech/polkadot-sdk/pull/5886#discussion_r1793423401 --------- Co-authored-by: Bastian Köcher Co-authored-by: Bastian Köcher --- prdoc/pr_5994.prdoc | 3 +++ .../client/consensus/babe/src/authorship.rs | 20 +++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 prdoc/pr_5994.prdoc diff --git a/prdoc/pr_5994.prdoc b/prdoc/pr_5994.prdoc new file mode 100644 index 000000000000..425653e52646 --- /dev/null +++ b/prdoc/pr_5994.prdoc @@ -0,0 +1,3 @@ +crates: + - name: sc-consensus-babe + bump: none diff --git a/substrate/client/consensus/babe/src/authorship.rs b/substrate/client/consensus/babe/src/authorship.rs index 06394b84f470..aa54da2a4434 100644 --- a/substrate/client/consensus/babe/src/authorship.rs +++ b/substrate/client/consensus/babe/src/authorship.rs @@ -109,7 +109,7 @@ pub(super) fn secondary_slot_author( } let rand = - U256::from_little_endian(&(randomness, slot).using_encoded(sp_crypto_hashing::blake2_256)); + U256::from_big_endian(&(randomness, slot).using_encoded(sp_crypto_hashing::blake2_256)); let authorities_len = U256::from(authorities.len()); let idx = rand % authorities_len; @@ -272,7 +272,9 @@ fn claim_primary_slot( #[cfg(test)] mod tests { use super::*; - use sp_consensus_babe::{AllowedSlots, AuthorityId, BabeEpochConfiguration, Epoch}; + use sp_consensus_babe::{ + AllowedSlots, AuthorityId, BabeEpochConfiguration, Epoch, RANDOMNESS_LENGTH, + }; use sp_core::{crypto::Pair as _, sr25519::Pair}; use sp_keystore::testing::MemoryKeystore; @@ -306,4 +308,18 @@ mod tests { epoch.authorities.push((valid_public_key.into(), 10)); assert_eq!(claim_slot(10.into(), &epoch, &keystore).unwrap().1, valid_public_key.into()); } + + #[test] + fn secondary_slot_author_selection_works() { + let authorities = (0..1000) + .map(|i| (AuthorityId::from(Pair::generate().0.public()), i)) + .collect::>(); + + let randomness = [3; RANDOMNESS_LENGTH]; + + assert_eq!( + *secondary_slot_author(100.into(), &authorities, randomness).unwrap(), + authorities[167].0 + ); + } }