Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change utility::as_derivative index to u32 #2629

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading