diff --git a/polkadot/runtime/common/src/lib.rs b/polkadot/runtime/common/src/lib.rs index 7d43a5eaf9..b2f56f1525 100644 --- a/polkadot/runtime/common/src/lib.rs +++ b/polkadot/runtime/common/src/lib.rs @@ -36,7 +36,8 @@ mod mock; #[cfg(test)] mod integration_tests; -use primitives::v1::{BlockNumber, ValidatorId, AssignmentId}; +use beefy_primitives::ecdsa::AuthorityId as BeefyId; +use primitives::v1::{AccountId, AssignmentId, BlockNumber, ValidatorId}; use sp_runtime::{Perquintill, Perbill, FixedPointNumber}; use frame_system::limits; use frame_support::{ @@ -190,6 +191,20 @@ impl OneSessionHandler for AssignmentSe fn on_disabled(_: usize) { } } +/// Generates a `BeefyId` from the given `AccountId`. The resulting `BeefyId` is +/// a dummy value and this is a utility function meant to be used when migration +/// session keys. +pub fn dummy_beefy_id_from_account_id(a: AccountId) -> BeefyId { + let mut id = BeefyId::default(); + let id_raw: &mut [u8] = id.as_mut(); + + // NOTE: AccountId is 32 bytes, whereas BeefyId is 33 bytes. + id_raw[1..].copy_from_slice(a.as_ref()); + id_raw[0..4].copy_from_slice(b"beef"); + + id +} + #[cfg(test)] mod multiplier_tests { use super::*; @@ -292,4 +307,15 @@ mod multiplier_tests { println!("block = {} multiplier {:?}", blocks, multiplier); } } + + #[test] + fn generate_dummy_unique_beefy_id_from_account_id() { + let acc1 = AccountId::new([0; 32]); + let acc2 = AccountId::new([1; 32]); + + let beefy_id1 = dummy_beefy_id_from_account_id(acc1); + let beefy_id2 = dummy_beefy_id_from_account_id(acc2); + + assert_ne!(beefy_id1, beefy_id2); + } } diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 06509f481a..487b2c198d 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -197,14 +197,7 @@ fn transform_session_keys(v: AccountId, old: OldSessionKeys) -> SessionKeys { para_validator: old.para_validator, para_assignment: old.para_assignment, authority_discovery: old.authority_discovery, - beefy: { - // We need to produce a dummy value that's unique for the validator. - let mut id = BeefyId::default(); - let id_raw: &mut [u8] = id.as_mut(); - id_raw.copy_from_slice(v.as_ref()); - id_raw[0..4].copy_from_slice(b"beef"); - id - }, + beefy: runtime_common::dummy_beefy_id_from_account_id(v), } } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 15c3615e28..c6b4edb02e 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -299,14 +299,7 @@ fn transform_session_keys(v: AccountId, old: OldSessionKeys) -> SessionKeys { para_validator: old.para_validator, para_assignment: old.para_assignment, authority_discovery: old.authority_discovery, - beefy: { - // We need to produce a dummy value that's unique for the validator. - let mut id = BeefyId::default(); - let id_raw: &mut [u8] = id.as_mut(); - id_raw.copy_from_slice(v.as_ref()); - id_raw[0..4].copy_from_slice(b"beef"); - id - }, + beefy: runtime_common::dummy_beefy_id_from_account_id(v), } }