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
8c0c5049
Commit
8c0c5049
authored
7 years ago
by
Gav
Browse files
Options
Downloads
Patches
Plain Diff
Use a struct!
parent
3ad32885
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
substrate/demo/runtime/src/runtime/staking.rs
+35
-22
35 additions, 22 deletions
substrate/demo/runtime/src/runtime/staking.rs
with
35 additions
and
22 deletions
substrate/demo/runtime/src/runtime/staking.rs
+
35
−
22
View file @
8c0c5049
...
...
@@ -141,7 +141,20 @@ pub fn total_stake() -> Balance {
pub
mod
public
{
use
super
::
*
;
type
State
=
BTreeMap
<
AccountId
,
(
Option
<
Balance
>
,
Option
<
Vec
<
u8
>>
,
BTreeMap
<
Vec
<
u8
>
,
Option
<
Vec
<
u8
>>>
)
>
;
#[derive(Default)]
struct
ChangeEntry
{
balance
:
Option
<
Balance
>
,
code
:
Option
<
Vec
<
u8
>>
,
storage
:
BTreeMap
<
Vec
<
u8
>
,
Option
<
Vec
<
u8
>>>
,
}
impl
ChangeEntry
{
pub
fn
balance_changed
(
b
:
Balance
)
->
Self
{
ChangeEntry
{
balance
:
Some
(
b
),
code
:
None
,
storage
:
Default
::
default
()
}
}
}
type
State
=
BTreeMap
<
AccountId
,
ChangeEntry
>
;
trait
Externalities
{
fn
get_storage
(
&
self
,
account
:
&
AccountId
,
location
:
&
[
u8
])
->
Option
<
Vec
<
u8
>>
;
...
...
@@ -191,15 +204,15 @@ pub mod public {
}
fn
commit_state
(
s
:
State
)
{
for
(
address
,
(
maybe_balance
,
maybe_code
,
storage
)
)
in
s
.into_iter
()
{
if
let
Some
(
balance
)
=
maybe_
balance
{
for
(
address
,
changed
)
in
s
.into_iter
()
{
if
let
Some
(
balance
)
=
changed
.
balance
{
storage
::
put
(
&
address
.to_keyed_vec
(
BALANCE_OF
),
&
balance
);
}
if
let
Some
(
code
)
=
maybe_
code
{
if
let
Some
(
code
)
=
changed
.
code
{
storage
::
put
(
&
address
.to_keyed_vec
(
CODE_OF
),
&
code
);
}
let
storage_key
=
address
.to_keyed_vec
(
STORAGE_OF
);
for
(
k
,
v
)
in
storage
.into_iter
()
{
for
(
k
,
v
)
in
changed
.
storage
.into_iter
()
{
let
mut
key
=
storage_key
.clone
();
key
.extend
(
k
);
if
let
Some
(
value
)
=
v
{
...
...
@@ -212,20 +225,20 @@ pub mod public {
}
fn
merge_state
(
commit_state
:
State
,
local
:
&
mut
State
)
{
for
(
address
,
(
maybe_balance
,
maybe_code
,
storage
)
)
in
commit_state
.into_iter
()
{
for
(
address
,
changed
)
in
commit_state
.into_iter
()
{
match
local
.entry
(
address
)
{
Entry
::
Occupied
(
e
)
=>
{
let
mut
value
=
e
.into_mut
();
if
maybe_
balance
.is_some
()
{
value
.
0
=
maybe_
balance
;
if
changed
.
balance
.is_some
()
{
value
.
balance
=
changed
.
balance
;
}
if
maybe_
code
.is_some
()
{
value
.
1
=
maybe_
code
;
if
changed
.
code
.is_some
()
{
value
.
code
=
changed
.
code
;
}
value
.
2
.extend
(
storage
.into_iter
());
value
.
storage
.extend
(
changed
.
storage
.into_iter
());
}
Entry
::
Vacant
(
e
)
=>
{
e
.insert
(
(
maybe_balance
,
maybe_code
,
storage
)
);
e
.insert
(
changed
);
}
}
}
...
...
@@ -262,8 +275,8 @@ pub mod public {
// two inserts are safe
assert!
(
&
dest
!=
transactor
);
local
.insert
(
dest
,
(
Some
(
value
),
Some
(
code
.to_vec
()),
Default
::
default
()
)
);
local
.insert
(
transactor
.clone
(),
(
Some
(
from_balance
-
value
),
None
,
Default
::
default
()
));
local
.insert
(
dest
,
ChangeEntry
{
balance
:
Some
(
value
),
code
:
Some
(
code
.to_vec
()),
storage
:
Default
::
default
()
}
);
local
.insert
(
transactor
.clone
(),
ChangeEntry
::
balance_changed
(
from_balance
-
value
));
Some
(
local
)
}
...
...
@@ -298,8 +311,8 @@ pub mod public {
if
transactor
!=
dest
{
let
mut
local
=
local
.borrow_mut
();
local
.insert
(
transactor
.clone
(),
(
Some
(
from_balance
-
value
),
None
,
Default
::
default
()
));
local
.insert
(
dest
.clone
(),
(
Some
(
to_balance
+
value
),
None
,
Default
::
default
()
));
local
.insert
(
transactor
.clone
(),
ChangeEntry
::
balance_changed
(
from_balance
-
value
));
local
.insert
(
dest
.clone
(),
ChangeEntry
::
balance_changed
(
to_balance
+
value
));
}
let
should_commit
=
{
...
...
@@ -307,16 +320,16 @@ pub mod public {
let
ext
=
||
Ext
{
do_get_storage
:
|
account
:
&
AccountId
,
location
:
&
[
u8
]|
local
.borrow
()
.get
(
account
)
.and_then
(|
a
|
a
.
2
.get
(
location
))
.and_then
(|
a
|
a
.
storage
.get
(
location
))
.cloned
()
.unwrap_or_else
(||
ext
.get_storage
(
account
,
location
)),
do_get_code
:
|
account
:
&
AccountId
|
local
.borrow
()
.get
(
account
)
.and_then
(|
a
|
a
.
1
.clone
())
.and_then
(|
a
|
a
.
code
.clone
())
.unwrap_or_else
(||
ext
.get_code
(
account
)),
do_get_balance
:
|
account
:
&
AccountId
|
local
.borrow
()
.get
(
account
)
.and_then
(|
a
|
a
.
0
)
.and_then
(|
a
|
a
.
balance
)
.unwrap_or_else
(||
ext
.get_balance
(
account
)),
};
let
mut
transfer
=
|
inner_dest
:
&
AccountId
,
value
:
Balance
|
{
...
...
@@ -332,8 +345,8 @@ pub mod public {
let
mut
put_storage
=
|
location
:
Vec
<
u8
>
,
value
:
Option
<
Vec
<
u8
>>
|
{
local
.borrow_mut
()
.entry
(
dest
.clone
())
.or_insert
(
(
None
,
None
,
Default
::
default
())
)
.
2
.insert
(
location
,
value
);
.or_insert
(
Default
::
default
())
.
storage
.insert
(
location
,
value
);
};
// TODO: logging (logs are just appended into a notable storage-based vector and cleared every
...
...
@@ -518,7 +531,7 @@ fn new_era() {
);
}
#[cfg(any(feature
=
"std"
,
test))]
#[cfg(any(feature
=
"std"
,
test))]
pub
mod
testing
{
use
super
::
*
;
use
runtime_io
::{
twox_128
,
TestExternalities
};
...
...
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