Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

More Extensible Multiaddress Format #7380

Merged
11 commits merged into from
Nov 19, 2020
6 changes: 3 additions & 3 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use sp_runtime::{
transaction_validity::{TransactionValidity, TransactionSource},
};
use sp_runtime::traits::{
BlakeTwo256, Block as BlockT, IdentityLookup, Verify, IdentifyAccount, NumberFor, Saturating,
BlakeTwo256, Block as BlockT, AccountIdLookup, Verify, IdentifyAccount, NumberFor, Saturating,
};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Expand Down Expand Up @@ -141,7 +141,7 @@ impl frame_system::Trait for Runtime {
/// The aggregated dispatch type that is available for extrinsics.
type Call = Call;
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
type Lookup = IdentityLookup<AccountId>;
type Lookup = AccountIdLookup<AccountId>;
/// The index type for storing how many extrinsics an account has signed.
type Index = Index;
/// The index type for blocks.
Expand Down Expand Up @@ -287,7 +287,7 @@ construct_runtime!(
);

/// The address format for describing accounts.
pub type Address = AccountId;
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type as expected by this runtime.
Expand Down
2 changes: 1 addition & 1 deletion bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ fn deploying_wasm_contract_should_work() {
signed: Some((charlie(), signed_extra(2, 0))),
function: Call::Contracts(
pallet_contracts::Call::call::<Runtime>(
pallet_indices::address::Address::Id(addr.clone()),
sp_runtime::MultiAddress::Id(addr.clone()),
10,
500_000_000,
vec![0x00, 0x01, 0x02, 0x03]
Expand Down
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_version: 260,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
transaction_version: 2,
};

/// Native version.
Expand Down Expand Up @@ -936,7 +936,7 @@ construct_runtime!(
);

/// The address format for describing accounts.
pub type Address = <Indices as StaticLookup>::Source;
pub type Address = sp_runtime::MultiAddress<AccountId, AccountIndex>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change? The old code has less duplication. Or did it stop working for some reason?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was just me being explicit. If there is no different, i would personally prefer explicitness. Else, if there is good reason to keep it the same, i dont really care about this change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would always prefer writing things as generic as possible and thus reducing code churn when a refactoring is made. Ultimately it is your decision. I just pointed it out here because I thought you made this change by mistake.

/// Block header type as expected by this runtime.
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
/// Block type as expected by this runtime.
Expand Down
6 changes: 3 additions & 3 deletions bin/node/testing/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,15 @@ impl<'a> Iterator for BlockContentIterator<'a> {
BlockType::RandomTransfersKeepAlive => {
Call::Balances(
BalancesCall::transfer_keep_alive(
pallet_indices::address::Address::Id(receiver),
sp_runtime::MultiAddress::Id(receiver),
node_runtime::ExistentialDeposit::get() + 1,
)
)
},
BlockType::RandomTransfersReaping => {
Call::Balances(
BalancesCall::transfer(
pallet_indices::address::Address::Id(receiver),
sp_runtime::MultiAddress::Id(receiver),
// Transfer so that ending balance would be 1 less than existential deposit
// so that we kill the sender account.
100*DOLLARS - (node_runtime::ExistentialDeposit::get() - 1),
Expand Down Expand Up @@ -591,7 +591,7 @@ impl BenchKeyring {
}
}).into();
UncheckedExtrinsic {
signature: Some((pallet_indices::address::Address::Id(signed), signature, extra)),
signature: Some((sp_runtime::MultiAddress::Id(signed), signature, extra)),
function: payload.0,
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/node/testing/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn sign(xt: CheckedExtrinsic, spec_version: u32, tx_version: u32, genesis_ha
}
}).into();
UncheckedExtrinsic {
signature: Some((pallet_indices::address::Address::Id(signed), signature, extra)),
signature: Some((sp_runtime::MultiAddress::Id(signed), signature, extra)),
function: payload.0,
}
}
Expand Down
159 changes: 0 additions & 159 deletions frame/indices/src/address.rs

This file was deleted.

15 changes: 7 additions & 8 deletions frame/indices/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use sp_std::prelude::*;
use codec::Codec;
use sp_runtime::MultiAddress;
use sp_runtime::traits::{
StaticLookup, Member, LookupError, Zero, Saturating, AtLeast32Bit
};
Expand All @@ -30,15 +31,12 @@ use frame_support::dispatch::DispatchResult;
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
use frame_support::weights::Weight;
use frame_system::{ensure_signed, ensure_root};
use self::address::Address as RawAddress;

mod mock;
pub mod address;
mod tests;
mod benchmarking;
mod default_weights;

pub type Address<T> = RawAddress<<T as frame_system::Trait>::AccountId, <T as Trait>::AccountIndex>;
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;

pub trait WeightInfo {
Expand Down Expand Up @@ -295,24 +293,25 @@ impl<T: Trait> Module<T> {

/// Lookup an address to get an Id, if there's one there.
pub fn lookup_address(
a: address::Address<T::AccountId, T::AccountIndex>
a: MultiAddress<T::AccountId, T::AccountIndex>
) -> Option<T::AccountId> {
match a {
address::Address::Id(i) => Some(i),
address::Address::Index(i) => Self::lookup_index(i),
MultiAddress::Id(i) => Some(i),
MultiAddress::Index(i) => Self::lookup_index(i),
_ => None,
}
}
}

impl<T: Trait> StaticLookup for Module<T> {
type Source = address::Address<T::AccountId, T::AccountIndex>;
type Source = MultiAddress<T::AccountId, T::AccountIndex>;
type Target = T::AccountId;

fn lookup(a: Self::Source) -> Result<Self::Target, LookupError> {
Self::lookup_address(a).ok_or(LookupError)
}

fn unlookup(a: Self::Target) -> Self::Source {
address::Address::Id(a)
MultiAddress::Id(a)
}
}
4 changes: 4 additions & 0 deletions primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ pub mod traits;
pub mod transaction_validity;
pub mod random_number_generator;
mod runtime_string;
mod multiaddress;

pub use crate::runtime_string::*;

// Re-export Multiaddress
pub use multiaddress::MultiAddress;

/// Re-export these since they're only "kind of" generic.
pub use generic::{DigestItem, Digest};

Expand Down
60 changes: 60 additions & 0 deletions primitives/runtime/src/multiaddress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is part of Substrate.

// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! MultiAddress type is a wrapper for multiple downstream account formats.

use codec::{Encode, Decode};
use sp_std::vec::Vec;

/// A multi-format address wrapper for on-chain accounts.
#[derive(Encode, Decode, PartialEq, Eq, Clone, crate::RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Hash))]
pub enum MultiAddress<AccountId, AccountIndex> {
/// It's an account ID (pubkey).
Id(AccountId),
/// It's an account index.
Index(#[codec(compact)] AccountIndex),
/// It's some arbitrary raw bytes.
Raw(Vec<u8>),
/// It's a 32 byte representation.
Address32([u8; 32]),
/// Its a 20 byte representation.
Address20([u8; 20]),
}

#[cfg(feature = "std")]
impl<AccountId, AccountIndex> std::fmt::Display for MultiAddress<AccountId, AccountIndex>
where
AccountId: std::fmt::Debug,
AccountIndex: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<AccountId, AccountIndex> From<AccountId> for MultiAddress<AccountId, AccountIndex> {
fn from(a: AccountId) -> Self {
MultiAddress::Id(a)
}
}

impl<AccountId: Default, AccountIndex> Default for MultiAddress<AccountId, AccountIndex> {
fn default() -> Self {
MultiAddress::Id(Default::default())
}
}
Loading