Skip to content
Snippets Groups Projects
Commit 8fe5aa4c authored by Sergey Pepyakin's avatar Sergey Pepyakin Committed by Gav Wood
Browse files

Extract SimpleAddressDeterminator (#716)

parent 7520d601
No related merge requests found
......@@ -204,28 +204,9 @@ impl treasury::Trait for Runtime {
/// Treasury module for this concrete runtime.
pub type Treasury = treasury::Module<Runtime>;
/// Address calculated from the code (of the constructor), input data to the constructor
/// and account id which requested the account creation.
///
/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)`
pub struct DetermineContractAddress;
impl contract::ContractAddressFor<AccountId> for DetermineContractAddress {
fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId {
use runtime_primitives::traits::Hash;
let code_hash = BlakeTwo256::hash(code);
let data_hash = BlakeTwo256::hash(data);
let mut buf = [0u8, 32 + 32 + 32];
&mut buf[0..32].copy_from_slice(&code_hash);
&mut buf[32..64].copy_from_slice(&data_hash);
&mut buf[64..96].copy_from_slice(origin);
AccountId::from(BlakeTwo256::hash(&buf[..]))
}
}
impl contract::Trait for Runtime {
type Gas = u64;
type DetermineContractAddress = DetermineContractAddress;
type DetermineContractAddress = contract::SimpleAddressDeterminator<Runtime>;
}
/// Contract module for this concrete runtime.
......
......@@ -103,8 +103,9 @@ use account_db::{AccountDb, OverlayAccountDb};
use double_map::StorageDoubleMap;
use rstd::prelude::*;
use rstd::marker::PhantomData;
use codec::Codec;
use runtime_primitives::traits::{As, SimpleArithmetic, OnFinalise};
use runtime_primitives::traits::{Hash, As, SimpleArithmetic, OnFinalise};
use runtime_support::dispatch::Result;
use runtime_support::{Parameter, StorageMap, StorageValue};
use system::ensure_signed;
......@@ -121,6 +122,31 @@ pub trait ContractAddressFor<AccountId: Sized> {
fn contract_address_for(code: &[u8], data: &[u8], origin: &AccountId) -> AccountId;
}
/// Simple contract address determintator.
///
/// Address calculated from the code (of the constructor), input data to the constructor
/// and account id which requested the account creation.
///
/// Formula: `blake2_256(blake2_256(code) + blake2_256(data) + origin)`
pub struct SimpleAddressDeterminator<T: Trait>(PhantomData<T>);
impl<T: Trait> ContractAddressFor<T::AccountId> for SimpleAddressDeterminator<T>
where
T::AccountId: From<T::Hash> + AsRef<[u8]>
{
fn contract_address_for(code: &[u8], data: &[u8], origin: &T::AccountId) -> T::AccountId {
let code_hash = T::Hashing::hash(code);
let data_hash = T::Hashing::hash(data);
let mut buf = Vec::new();
buf.extend_from_slice(code_hash.as_ref());
buf.extend_from_slice(data_hash.as_ref());
buf.extend_from_slice(origin.as_ref());
T::Hashing::hash(&buf[..]).into()
}
}
decl_module! {
/// Contracts module.
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment