Skip to content

Commit c7ba4cf

Browse files
shawntabrizishamb0
authored andcommitted
More Extensible Multiaddress Format (paritytech#7380)
* More extensible multiaddress format * update name * Don't depend on indices to define multiaddress type * Use MultiAddress in Node Template too! * reduce traits, fix build * support multiple `StaticLookup` * bump tx version * feedback
1 parent a4ff31e commit c7ba4cf

File tree

10 files changed

+125
-177
lines changed

10 files changed

+125
-177
lines changed

bin/node-template/runtime/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use sp_runtime::{
1313
transaction_validity::{TransactionValidity, TransactionSource},
1414
};
1515
use sp_runtime::traits::{
16-
BlakeTwo256, Block as BlockT, IdentityLookup, Verify, IdentifyAccount, NumberFor, Saturating,
16+
BlakeTwo256, Block as BlockT, AccountIdLookup, Verify, IdentifyAccount, NumberFor, Saturating,
1717
};
1818
use sp_api::impl_runtime_apis;
1919
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
@@ -148,7 +148,7 @@ impl frame_system::Trait for Runtime {
148148
/// The aggregated dispatch type that is available for extrinsics.
149149
type Call = Call;
150150
/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
151-
type Lookup = IdentityLookup<AccountId>;
151+
type Lookup = AccountIdLookup<AccountId, ()>;
152152
/// The index type for storing how many extrinsics an account has signed.
153153
type Index = Index;
154154
/// The index type for blocks.
@@ -293,7 +293,7 @@ construct_runtime!(
293293
);
294294

295295
/// The address format for describing accounts.
296-
pub type Address = AccountId;
296+
pub type Address = sp_runtime::MultiAddress<AccountId, ()>;
297297
/// Block header type as expected by this runtime.
298298
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
299299
/// Block type as expected by this runtime.

bin/node/executor/tests/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ fn deploying_wasm_contract_should_work() {
621621
signed: Some((charlie(), signed_extra(2, 0))),
622622
function: Call::Contracts(
623623
pallet_contracts::Call::call::<Runtime>(
624-
pallet_indices::address::Address::Id(addr.clone()),
624+
sp_runtime::MultiAddress::Id(addr.clone()),
625625
10,
626626
500_000_000,
627627
vec![0x00, 0x01, 0x02, 0x03]

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
111111
spec_version: 260,
112112
impl_version: 0,
113113
apis: RUNTIME_API_VERSIONS,
114-
transaction_version: 1,
114+
transaction_version: 2,
115115
};
116116

117117
/// Native version.
@@ -944,7 +944,7 @@ construct_runtime!(
944944
);
945945

946946
/// The address format for describing accounts.
947-
pub type Address = <Indices as StaticLookup>::Source;
947+
pub type Address = sp_runtime::MultiAddress<AccountId, AccountIndex>;
948948
/// Block header type as expected by this runtime.
949949
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
950950
/// Block type as expected by this runtime.

bin/node/testing/src/bench.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,15 @@ impl<'a> Iterator for BlockContentIterator<'a> {
317317
BlockType::RandomTransfersKeepAlive => {
318318
Call::Balances(
319319
BalancesCall::transfer_keep_alive(
320-
pallet_indices::address::Address::Id(receiver),
320+
sp_runtime::MultiAddress::Id(receiver),
321321
node_runtime::ExistentialDeposit::get() + 1,
322322
)
323323
)
324324
},
325325
BlockType::RandomTransfersReaping => {
326326
Call::Balances(
327327
BalancesCall::transfer(
328-
pallet_indices::address::Address::Id(receiver),
328+
sp_runtime::MultiAddress::Id(receiver),
329329
// Transfer so that ending balance would be 1 less than existential deposit
330330
// so that we kill the sender account.
331331
100*DOLLARS - (node_runtime::ExistentialDeposit::get() - 1),
@@ -591,7 +591,7 @@ impl BenchKeyring {
591591
}
592592
}).into();
593593
UncheckedExtrinsic {
594-
signature: Some((pallet_indices::address::Address::Id(signed), signature, extra)),
594+
signature: Some((sp_runtime::MultiAddress::Id(signed), signature, extra)),
595595
function: payload.0,
596596
}
597597
}

bin/node/testing/src/keyring.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn sign(xt: CheckedExtrinsic, spec_version: u32, tx_version: u32, genesis_ha
9494
}
9595
}).into();
9696
UncheckedExtrinsic {
97-
signature: Some((pallet_indices::address::Address::Id(signed), signature, extra)),
97+
signature: Some((sp_runtime::MultiAddress::Id(signed), signature, extra)),
9898
function: payload.0,
9999
}
100100
}

frame/indices/src/address.rs

Lines changed: 0 additions & 159 deletions
This file was deleted.

frame/indices/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,22 @@
2121
#![cfg_attr(not(feature = "std"), no_std)]
2222

2323
mod mock;
24-
pub mod address;
2524
mod tests;
2625
mod benchmarking;
2726
pub mod weights;
2827

2928
use sp_std::prelude::*;
3029
use codec::Codec;
30+
use sp_runtime::MultiAddress;
3131
use sp_runtime::traits::{
3232
StaticLookup, Member, LookupError, Zero, Saturating, AtLeast32Bit
3333
};
3434
use frame_support::{Parameter, decl_module, decl_error, decl_event, decl_storage, ensure};
3535
use frame_support::dispatch::DispatchResult;
3636
use frame_support::traits::{Currency, ReservableCurrency, Get, BalanceStatus::Reserved};
3737
use frame_system::{ensure_signed, ensure_root};
38-
use self::address::Address as RawAddress;
3938
pub use weights::WeightInfo;
4039

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

4442
/// The module's config trait.
@@ -287,24 +285,25 @@ impl<T: Trait> Module<T> {
287285

288286
/// Lookup an address to get an Id, if there's one there.
289287
pub fn lookup_address(
290-
a: address::Address<T::AccountId, T::AccountIndex>
288+
a: MultiAddress<T::AccountId, T::AccountIndex>
291289
) -> Option<T::AccountId> {
292290
match a {
293-
address::Address::Id(i) => Some(i),
294-
address::Address::Index(i) => Self::lookup_index(i),
291+
MultiAddress::Id(i) => Some(i),
292+
MultiAddress::Index(i) => Self::lookup_index(i),
293+
_ => None,
295294
}
296295
}
297296
}
298297

299298
impl<T: Trait> StaticLookup for Module<T> {
300-
type Source = address::Address<T::AccountId, T::AccountIndex>;
299+
type Source = MultiAddress<T::AccountId, T::AccountIndex>;
301300
type Target = T::AccountId;
302301

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

307306
fn unlookup(a: Self::Target) -> Self::Source {
308-
address::Address::Id(a)
307+
MultiAddress::Id(a)
309308
}
310309
}

primitives/runtime/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ pub mod traits;
5656
pub mod transaction_validity;
5757
pub mod random_number_generator;
5858
mod runtime_string;
59+
mod multiaddress;
5960

6061
pub use crate::runtime_string::*;
6162

63+
// Re-export Multiaddress
64+
pub use multiaddress::MultiAddress;
65+
6266
/// Re-export these since they're only "kind of" generic.
6367
pub use generic::{DigestItem, Digest};
6468

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2017-2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! MultiAddress type is a wrapper for multiple downstream account formats.
19+
20+
use codec::{Encode, Decode};
21+
use sp_std::vec::Vec;
22+
23+
/// A multi-format address wrapper for on-chain accounts.
24+
#[derive(Encode, Decode, PartialEq, Eq, Clone, crate::RuntimeDebug)]
25+
#[cfg_attr(feature = "std", derive(Hash))]
26+
pub enum MultiAddress<AccountId, AccountIndex> {
27+
/// It's an account ID (pubkey).
28+
Id(AccountId),
29+
/// It's an account index.
30+
Index(#[codec(compact)] AccountIndex),
31+
/// It's some arbitrary raw bytes.
32+
Raw(Vec<u8>),
33+
/// It's a 32 byte representation.
34+
Address32([u8; 32]),
35+
/// Its a 20 byte representation.
36+
Address20([u8; 20]),
37+
}
38+
39+
#[cfg(feature = "std")]
40+
impl<AccountId, AccountIndex> std::fmt::Display for MultiAddress<AccountId, AccountIndex>
41+
where
42+
AccountId: std::fmt::Debug,
43+
AccountIndex: std::fmt::Debug,
44+
{
45+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46+
use sp_core::hexdisplay::HexDisplay;
47+
match self {
48+
MultiAddress::Raw(inner) => write!(f, "MultiAddress::Raw({})", HexDisplay::from(inner)),
49+
MultiAddress::Address32(inner) => write!(f, "MultiAddress::Address32({})", HexDisplay::from(inner)),
50+
MultiAddress::Address20(inner) => write!(f, "MultiAddress::Address20({})", HexDisplay::from(inner)),
51+
_ => write!(f, "{:?}", self),
52+
}
53+
}
54+
}
55+
56+
impl<AccountId, AccountIndex> From<AccountId> for MultiAddress<AccountId, AccountIndex> {
57+
fn from(a: AccountId) -> Self {
58+
MultiAddress::Id(a)
59+
}
60+
}
61+
62+
impl<AccountId: Default, AccountIndex> Default for MultiAddress<AccountId, AccountIndex> {
63+
fn default() -> Self {
64+
MultiAddress::Id(Default::default())
65+
}
66+
}

0 commit comments

Comments
 (0)