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
adbeecbd
Commit
adbeecbd
authored
7 years ago
by
Gav
Browse files
Options
Downloads
Patches
Plain Diff
Rejig tests and ensure authorities are addressed consistently.
parent
bd8f67a2
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
substrate/state_machine/src/lib.rs
+19
-23
19 additions, 23 deletions
substrate/state_machine/src/lib.rs
substrate/wasm-runtime/polkadot/src/lib.rs
+24
-14
24 additions, 14 deletions
substrate/wasm-runtime/polkadot/src/lib.rs
with
43 additions
and
37 deletions
substrate/state_machine/src/lib.rs
+
19
−
23
View file @
adbeecbd
...
@@ -114,13 +114,9 @@ impl OverlayedChanges {
...
@@ -114,13 +114,9 @@ impl OverlayedChanges {
pub
trait
Error
:
'static
+
fmt
::
Debug
+
fmt
::
Display
+
Send
{}
pub
trait
Error
:
'static
+
fmt
::
Debug
+
fmt
::
Display
+
Send
{}
impl
<
E
>
Error
for
E
where
E
:
'static
+
fmt
::
Debug
+
fmt
::
Display
+
Send
{}
impl
<
E
>
Error
for
E
where
E
:
'static
+
fmt
::
Debug
+
fmt
::
Display
+
Send
{}
fn
value_vec
(
mut
value
:
usize
,
initial
:
Vec
<
u8
>
)
->
Vec
<
u8
>
{
fn
to_keyed_vec
(
value
:
u32
,
mut
prepend
:
Vec
<
u8
>
)
->
Vec
<
u8
>
{
let
mut
acc
=
initial
;
prepend
.extend
((
0
..
::
std
::
mem
::
size_of
::
<
u32
>
())
.into_iter
()
.map
(|
i
|
(
value
>>
(
i
*
8
))
as
u8
));
while
value
>
0
{
prepend
acc
.push
(
value
as
u8
);
value
/=
256
;
}
acc
}
}
/// Externalities: pinned to specific active address.
/// Externalities: pinned to specific active address.
...
@@ -134,12 +130,12 @@ pub trait Externalities {
...
@@ -134,12 +130,12 @@ pub trait Externalities {
/// Set storage of current contract being called (effective immediately).
/// Set storage of current contract being called (effective immediately).
fn
set_storage
(
&
mut
self
,
key
:
Vec
<
u8
>
,
value
:
Vec
<
u8
>
);
fn
set_storage
(
&
mut
self
,
key
:
Vec
<
u8
>
,
value
:
Vec
<
u8
>
);
/// Get the current set of
validator
s.
/// Get the current set of
authoritie
s.
fn
validator
s
(
&
self
)
->
Result
<
Vec
<&
[
u8
]
>
,
Self
::
Error
>
{
fn
authoritie
s
(
&
self
)
->
Result
<
Vec
<&
[
u8
]
>
,
Self
::
Error
>
{
(
0
..
self
.storage
(
b"
\0
validator_count
"
)
?
.into_iter
()
(
0
..
self
.storage
(
b"
con
\0
aut
\0
len
"
)
?
.into_iter
()
.rev
()
.rev
()
.fold
(
0
,
|
acc
,
&
i
|
(
acc
<<
8
)
+
(
i
as
u
size
)))
.fold
(
0
,
|
acc
,
&
i
|
(
acc
<<
8
)
+
(
i
as
u
32
)))
.map
(|
i
|
self
.storage
(
&
value
_vec
(
i
,
b"
\0
validator
"
.to_vec
())))
.map
(|
i
|
self
.storage
(
&
to_keyed
_vec
(
i
,
b"
con
\0
aut
\0
"
.to_vec
())))
.collect
()
.collect
()
}
}
}
}
...
@@ -248,22 +244,22 @@ mod tests {
...
@@ -248,22 +244,22 @@ mod tests {
}
}
#[test]
#[test]
fn
validator
s_call_works
()
{
fn
authoritie
s_call_works
()
{
let
mut
ext
=
TestExternalities
::
default
();
let
mut
ext
=
TestExternalities
::
default
();
assert_eq!
(
ext
.
validator
s
(),
Ok
(
vec!
[]));
assert_eq!
(
ext
.
authoritie
s
(),
Ok
(
vec!
[]));
ext
.set_storage
(
b"
\0
validator_count
"
.to_vec
(),
vec!
[]);
ext
.set_storage
(
b"
con
\0
aut
\0
len
"
.to_vec
(),
vec!
[
0u8
;
4
]);
assert_eq!
(
ext
.
validator
s
(),
Ok
(
vec!
[]));
assert_eq!
(
ext
.
authoritie
s
(),
Ok
(
vec!
[]));
ext
.set_storage
(
b"
\0
validator_count
"
.to_vec
(),
vec!
[
1
]);
ext
.set_storage
(
b"
con
\0
aut
\0
len
"
.to_vec
(),
vec!
[
1
u8
,
0
,
0
,
0
]);
assert_eq!
(
ext
.
validator
s
(),
Ok
(
vec!
[
&
[][
..
]]));
assert_eq!
(
ext
.
authoritie
s
(),
Ok
(
vec!
[
&
[][
..
]]));
ext
.set_storage
(
b"
\0
validator
"
.to_vec
(),
b"first"
.to_vec
());
ext
.set_storage
(
b"
con
\0
aut
\0\0\0\0\0
"
.to_vec
(),
b"first"
.to_vec
());
assert_eq!
(
ext
.
validator
s
(),
Ok
(
vec!
[
&
b"first"
[
..
]]));
assert_eq!
(
ext
.
authoritie
s
(),
Ok
(
vec!
[
&
b"first"
[
..
]]));
ext
.set_storage
(
b"
\0
validator_count
"
.to_vec
(),
vec!
[
2
]);
ext
.set_storage
(
b"
con
\0
aut
\0
len
"
.to_vec
(),
vec!
[
2
u8
,
0
,
0
,
0
]);
ext
.set_storage
(
b"
\0
validator
\x01
"
.to_vec
(),
b"second"
.to_vec
());
ext
.set_storage
(
b"
con
\0
aut
\0\x01\0\0\0
"
.to_vec
(),
b"second"
.to_vec
());
assert_eq!
(
ext
.
validator
s
(),
Ok
(
vec!
[
&
b"first"
[
..
],
&
b"second"
[
..
]]));
assert_eq!
(
ext
.
authoritie
s
(),
Ok
(
vec!
[
&
b"first"
[
..
],
&
b"second"
[
..
]]));
}
}
}
}
This diff is collapsed.
Click to expand it.
substrate/wasm-runtime/polkadot/src/lib.rs
+
24
−
14
View file @
adbeecbd
...
@@ -117,15 +117,6 @@ fn env() -> Rc<RefCell<Environment>> {
...
@@ -117,15 +117,6 @@ fn env() -> Rc<RefCell<Environment>> {
}
}
}
}
fn
value_vec
(
mut
value
:
u32
,
initial
:
Vec
<
u8
>
)
->
Vec
<
u8
>
{
let
mut
acc
=
initial
;
while
value
>
0
{
acc
.push
(
value
as
u8
);
value
/=
256
;
}
acc
}
trait
EndianSensitive
:
Sized
{
trait
EndianSensitive
:
Sized
{
fn
to_le
(
self
)
->
Self
{
self
}
fn
to_le
(
self
)
->
Self
{
self
}
fn
to_be
(
self
)
->
Self
{
self
}
fn
to_be
(
self
)
->
Self
{
self
}
...
@@ -176,10 +167,10 @@ fn storage_into<T: Storage>(key: &[u8]) -> T {
...
@@ -176,10 +167,10 @@ fn storage_into<T: Storage>(key: &[u8]) -> T {
T
::
storage_into
(
key
)
T
::
storage_into
(
key
)
}
}
trait
KeyedVec
{
trait
KeyedVec
{
fn
to_keyed_vec
(
&
self
,
prepend_key
:
&
[
u8
])
->
Vec
<
u8
>
;
fn
to_keyed_vec
(
&
self
,
prepend_key
:
&
[
u8
])
->
Vec
<
u8
>
;
}
}
impl
KeyedVec
for
AccountID
{
impl
KeyedVec
for
AccountID
{
fn
to_keyed_vec
(
&
self
,
prepend_key
:
&
[
u8
])
->
Vec
<
u8
>
{
fn
to_keyed_vec
(
&
self
,
prepend_key
:
&
[
u8
])
->
Vec
<
u8
>
{
let
mut
r
=
prepend_key
.to_vec
();
let
mut
r
=
prepend_key
.to_vec
();
...
@@ -188,6 +179,25 @@ impl KeyedVec for AccountID {
...
@@ -188,6 +179,25 @@ impl KeyedVec for AccountID {
}
}
}
}
macro_rules!
impl_endians
{
(
$
(
$t:ty
),
*
)
=>
{
$
(
impl
KeyedVec
for
$t
{
fn
to_keyed_vec
(
&
self
,
prepend_key
:
&
[
u8
])
->
Vec
<
u8
>
{
let
size
=
size_of
::
<
Self
>
();
let
value_bytes
=
self
.to_le
();
let
value_slice
=
unsafe
{
std
::
slice
::
from_raw_parts
(
transmute
::
<*
const
Self
,
*
const
u8
>
(
&
value_bytes
),
size
)
};
let
mut
r
=
prepend_key
.to_vec
();
r
.extend_from_slice
(
value_slice
);
r
}
}
)
*
}
}
impl_endians!
(
u8
,
u16
,
u32
,
u64
,
usize
,
i8
,
i16
,
i32
,
i64
,
isize
);
// TODO: include RLP implementation
// TODO: include RLP implementation
// TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme)
// TODO: add keccak256 (or some better hashing scheme) & ECDSA-recover (or some better sig scheme)
...
@@ -266,20 +276,20 @@ pub mod consensus {
...
@@ -266,20 +276,20 @@ pub mod consensus {
use
super
::
*
;
use
super
::
*
;
pub
fn
set_authority
(
index
:
u32
,
authority
:
AccountID
)
{
pub
fn
set_authority
(
index
:
u32
,
authority
:
AccountID
)
{
runtime_support
::
set_
stor
ag
e
(
&
value_vec
(
index
,
b"
\0
authority"
.to_vec
()),
&
authority
[
..
]
);
authority
.
store
(
&
index
.to_keyed_vec
(
b"con
\0
aut
\0
"
)
);
}
}
fn
authority
(
index
:
u32
)
->
AccountID
{
fn
authority
(
index
:
u32
)
->
AccountID
{
runtime_support
::
storage_into
(
&
value_vec
(
index
,
b"
\0
authority"
.to_vec
()))
.unwrap
(
)
storage_into
(
&
index
.to_keyed_vec
(
b"con
\0
aut
\0
"
)
)
}
}
pub
fn
set_authority_count
(
count
:
u32
)
{
pub
fn
set_authority_count
(
count
:
u32
)
{
(
count
..
authority_count
())
.for_each
(|
i
|
set_authority
(
i
,
SessionKey
::
default
()));
(
count
..
authority_count
())
.for_each
(|
i
|
set_authority
(
i
,
SessionKey
::
default
()));
r
unt
ime_support
::
set_
stor
ag
e
(
b"
\0
authority_count"
,
&
value_vec
(
count
,
Vec
::
new
())
);
co
unt
.
store
(
b"
con
\0
aut
\0
len"
);
}
}
fn
authority_count
()
->
u32
{
fn
authority_count
()
->
u32
{
runtime_support
::
storage
(
b"
\0
authority_count"
)
.into_iter
()
.rev
()
.fold
(
0
,
|
acc
,
i
|
(
acc
<<
8
)
+
(
i
as
u32
)
)
storage_into
(
b"con
\0
aut
\0
len"
)
}
}
/// Get the current set of authorities. These are the session keys.
/// Get the current set of authorities. These are the session keys.
...
...
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