From e3477fc1e3ea3619a4b24c5ed1c3f6f9dd9c060a Mon Sep 17 00:00:00 2001 From: Daniel Olano Date: Tue, 5 Dec 2023 16:26:38 +0100 Subject: [PATCH] Change utility::as_derivative index to u32 --- substrate/frame/utility/src/lib.rs | 11 ++++++++--- substrate/frame/utility/src/tests.rs | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 7f963e3637d6f..6e5d925810d4b 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -266,7 +266,7 @@ pub mod pallet { })] pub fn as_derivative( origin: OriginFor, - index: u16, + index: u32, call: Box<::RuntimeCall>, ) -> DispatchResultWithPostInfo { let mut origin = origin; @@ -504,8 +504,13 @@ impl TypeId for IndexedUtilityPalletId { impl Pallet { /// Derive a derivative account ID from the owner account and the sub-account index. - pub fn derivative_account_id(who: T::AccountId, index: u16) -> T::AccountId { - let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256); + pub fn derivative_account_id(who: T::AccountId, index: u32) -> T::AccountId { + // Derive the same addresses as when index was u16 + let entropy = if index <= u16::MAX as u32 { + (b"modlpy/utilisuba", who, index as u16).using_encoded(blake2_256) + } else { + (b"modlpy/utilisuba", who, index).using_encoded(blake2_256) + }; Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref())) .expect("infinite length input; no invalid inputs for type; qed") } diff --git a/substrate/frame/utility/src/tests.rs b/substrate/frame/utility/src/tests.rs index 7b42fa511d100..8f3e2c97268d7 100644 --- a/substrate/frame/utility/src/tests.rs +++ b/substrate/frame/utility/src/tests.rs @@ -398,6 +398,15 @@ fn as_derivative_filters() { }); } +#[test] +fn as_derivative_u32_compat_with_old_u16() { + new_test_ext().execute_with(|| { + let sub_1_0 = Utility::derivative_account_id(1, 0); + const SAME_DERIVATIVE_AS_WITH_OLD_U16_INDEX: u64 = 13950085325555297514; + assert_eq!(sub_1_0, SAME_DERIVATIVE_AS_WITH_OLD_U16_INDEX); + }) +} + #[test] fn batch_with_root_works() { new_test_ext().execute_with(|| {