Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
8fe5aa4c
Commit
8fe5aa4c
authored
6 years ago
by
Sergey Pepyakin
Committed by
Gav Wood
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Extract SimpleAddressDeterminator (#716)
parent
7520d601
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
substrate/demo/runtime/src/lib.rs
+1
-20
1 addition, 20 deletions
substrate/demo/runtime/src/lib.rs
substrate/substrate/runtime/contract/src/lib.rs
+27
-1
27 additions, 1 deletion
substrate/substrate/runtime/contract/src/lib.rs
with
28 additions
and
21 deletions
substrate/demo/runtime/src/lib.rs
+
1
−
20
View file @
8fe5aa4c
...
...
@@ -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.
...
...
This diff is collapsed.
Click to expand it.
substrate/substrate/runtime/contract/src/lib.rs
+
27
−
1
View file @
8fe5aa4c
...
...
@@ -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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment