Skip to content

Commit

Permalink
Change utility::as_derivative index to u32
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Dec 5, 2023
1 parent a310df2 commit e3477fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions substrate/frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub mod pallet {
})]
pub fn as_derivative(
origin: OriginFor<T>,
index: u16,
index: u32,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResultWithPostInfo {
let mut origin = origin;
Expand Down Expand Up @@ -504,8 +504,13 @@ impl TypeId for IndexedUtilityPalletId {

impl<T: Config> Pallet<T> {
/// 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")
}
Expand Down
9 changes: 9 additions & 0 deletions substrate/frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down

0 comments on commit e3477fc

Please sign in to comment.