Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
substrate
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
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
Radu Popa
substrate
Commits
93beee9a
Unverified
Commit
93beee9a
authored
5 years ago
by
David Craven
Browse files
Options
Downloads
Patches
Plain Diff
Move OpaqueKeys to primitives.
parent
6f939a36
Branches
Branches containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
core/sr-primitives/src/testing.rs
+8
-7
8 additions, 7 deletions
core/sr-primitives/src/testing.rs
core/sr-primitives/src/traits.rs
+72
-0
72 additions, 0 deletions
core/sr-primitives/src/traits.rs
srml/session/src/lib.rs
+1
-77
1 addition, 77 deletions
srml/session/src/lib.rs
with
81 additions
and
84 deletions
core/sr-primitives/src/testing.rs
+
8
−
7
View file @
93beee9a
...
...
@@ -19,7 +19,7 @@
use
serde
::{
Serialize
,
Serializer
,
Deserialize
,
de
::
Error
as
DeError
,
Deserializer
};
use
std
::{
fmt
::
Debug
,
ops
::
Deref
,
fmt
};
use
crate
::
codec
::{
Codec
,
Encode
,
Decode
};
use
crate
::
traits
::{
self
,
Checkable
,
Applyable
,
BlakeTwo256
,
Convert
};
use
crate
::
traits
::{
self
,
Checkable
,
Applyable
,
BlakeTwo256
,
OpaqueKeys
};
use
crate
::
generic
::
DigestItem
as
GenDigestItem
;
pub
use
substrate_primitives
::
H256
;
use
substrate_primitives
::
U256
;
...
...
@@ -36,13 +36,14 @@ impl Into<AuthorityId> for UintAuthorityId {
}
}
/// Converter between u64 and the AuthorityId wrapper type.
pub
struct
ConvertUintAuthorityId
;
impl
Convert
<
u64
,
Option
<
UintAuthorityId
>>
for
ConvertUintAuthorityId
{
fn
convert
(
a
:
u64
)
->
Option
<
UintAuthorityId
>
{
Some
(
UintAuthorityId
(
a
))
}
impl
OpaqueKeys
for
UintAuthorityId
{
fn
count
()
->
usize
{
1
}
// Unsafe, i know, but it's test code and it's just there because it's really convenient to
// keep `UintAuthorityId` as a u64 under the hood.
fn
get_raw
(
&
self
,
_
:
usize
)
->
&
[
u8
]
{
unsafe
{
&
std
::
mem
::
transmute
::
<
_
,
&
[
u8
;
8
]
>
(
&
self
.0
)[
..
]
}
}
fn
get
<
T
:
Decode
>
(
&
self
,
_
:
usize
)
->
Option
<
T
>
{
self
.0
.using_encoded
(|
mut
x
|
T
::
decode
(
&
mut
x
))
}
}
/// Digest item
pub
type
DigestItem
=
GenDigestItem
<
H256
>
;
...
...
This diff is collapsed.
Click to expand it.
core/sr-primitives/src/traits.rs
+
72
−
0
View file @
93beee9a
...
...
@@ -878,3 +878,75 @@ pub trait ValidateUnsigned {
/// Changes made to storage should be discarded by caller.
fn
validate_unsigned
(
call
:
&
Self
::
Call
)
->
TransactionValidity
;
}
/// Opaque datatype that may be destructured into a series of raw byte slices (which represent
/// individual keys).
pub
trait
OpaqueKeys
{
/// Return the number of encoded keys.
fn
count
()
->
usize
{
0
}
/// Get the raw bytes of key with index `i`.
fn
get_raw
(
&
self
,
i
:
usize
)
->
&
[
u8
];
/// Get the decoded key with index `i`.
fn
get
<
T
:
Decode
>
(
&
self
,
i
:
usize
)
->
Option
<
T
>
{
T
::
decode
(
&
mut
self
.get_raw
(
i
))
}
/// Verify a proof of ownership for the keys.
fn
ownership_proof_is_valid
(
&
self
,
_proof
:
&
[
u8
])
->
bool
{
true
}
}
/// Calls a given macro a number of times with a set of fixed params and an incrementing numeral.
/// e.g.
/// ```
/// count!(println ("{}",) foo, bar, baz);
/// // Will result in three `println!`s: "0", "1" and "2".
/// ```
#[macro_export]
macro_rules!
count
{
(
$f:ident
(
$
(
$x:tt
)
*
)
)
=>
();
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
,
$x3:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
$f
!
(
$
(
$x
)
*
2
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
,
$x3:tt
,
$x4:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
$f
!
(
$
(
$x
)
*
2
);
$f
!
(
$
(
$x
)
*
3
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
,
$x3:tt
,
$x4:tt
,
$x5:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
$f
!
(
$
(
$x
)
*
2
);
$f
!
(
$
(
$x
)
*
3
);
$f
!
(
$
(
$x
)
*
4
);
};
}
#[macro_export]
/// Just implement `OpaqueKeys` for a given tuple-struct.
/// Would be much nicer for this to be converted to `derive` code.
macro_rules!
impl_opaque_keys
{
(
pub
struct
$name:ident
(
$
(
$t:ty
),
*
$
(,)
*
);
)
=>
{
impl_opaque_keys!
{
pub
struct
$name
(
$
(
$t
,)
*
);
impl
OpaqueKeys
for
_
{}
}
};
(
pub
struct
$name:ident
(
$
(
$t:ty
),
*
$
(,)
*
);
impl
OpaqueKeys
for
_
{
$
(
$rest:tt
)
*
}
)
=>
{
#[derive(Default,
Clone,
PartialEq,
Eq,
Encode,
Decode)]
#[cfg_attr(feature
=
"std"
,
derive(Debug,
Serialize,
Deserialize))]
pub
struct
$name
(
$
(
pub
$t
,)
*
);
impl
$crate
::
OpaqueKeys
for
$name
{
fn
count
()
->
usize
{
let
mut
c
=
0
;
$
(
let
_
:
$t
;
c
+=
1
;
)
*
c
}
fn
get_raw
(
&
self
,
i
:
usize
)
->
&
[
u8
]
{
$crate
::
count!
(
impl_opaque_keys
(
!!
self
i
)
$
(
$t
),
*
);
&
[]
}
$
(
$rest
)
*
}
};
(
!!
$self:ident
$param_i:ident
$i:tt
)
=>
{
if
$param_i
==
$i
{
return
$self
.
$i
.as_ref
()
}
}
}
This diff is collapsed.
Click to expand it.
srml/session/src/lib.rs
+
1
−
77
View file @
93beee9a
...
...
@@ -117,7 +117,7 @@
use
rstd
::{
prelude
::
*
,
marker
::
PhantomData
,
ops
::
Rem
};
use
parity_codec
::
Decode
;
use
primitives
::
traits
::{
Zero
,
Saturating
,
Member
};
use
primitives
::
traits
::{
Zero
,
Saturating
,
Member
,
OpaqueKeys
};
use
srml_support
::{
StorageValue
,
StorageMap
,
for_each_tuple
,
decl_module
,
decl_event
,
decl_storage
};
use
srml_support
::{
ensure
,
traits
::{
OnFreeBalanceZero
,
Get
},
Parameter
,
print
};
use
system
::
ensure_signed
;
...
...
@@ -125,74 +125,6 @@ use system::ensure_signed;
/// Simple index type with which we can count sessions.
pub
type
SessionIndex
=
u32
;
/// Opaque datatype that may be destructured into a series of raw byte slices (which represent
/// individual keys).
pub
trait
OpaqueKeys
{
fn
count
()
->
usize
{
0
}
fn
get_raw
(
&
self
,
i
:
usize
)
->
&
[
u8
];
fn
get
<
T
:
Decode
>
(
&
self
,
i
:
usize
)
->
Option
<
T
>
{
T
::
decode
(
&
mut
self
.get_raw
(
i
))
}
fn
ownership_proof_is_valid
(
&
self
,
_proof
:
&
[
u8
])
->
bool
{
true
}
}
/// Calls a given macro a number of times with a set of fixed params and an incrementing numeral.
/// e.g.
/// ```
/// count!(println ("{}",) foo, bar, baz);
/// // Will result in three `println!`s: "0", "1" and "2".
/// ```
#[macro_export]
macro_rules!
count
{
(
$f:ident
(
$
(
$x:tt
)
*
)
)
=>
();
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
,
$x3:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
$f
!
(
$
(
$x
)
*
2
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
,
$x3:tt
,
$x4:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
$f
!
(
$
(
$x
)
*
2
);
$f
!
(
$
(
$x
)
*
3
);
};
(
$f:ident
(
$
(
$x:tt
)
*
)
$x1:tt
,
$x2:tt
,
$x3:tt
,
$x4:tt
,
$x5:tt
)
=>
{
$f
!
(
$
(
$x
)
*
0
);
$f
!
(
$
(
$x
)
*
1
);
$f
!
(
$
(
$x
)
*
2
);
$f
!
(
$
(
$x
)
*
3
);
$f
!
(
$
(
$x
)
*
4
);
};
}
#[macro_export]
/// Just implement `OpaqueKeys` for a given tuple-struct.
/// Would be much nicer for this to be converted to `derive` code.
macro_rules!
impl_opaque_keys
{
(
pub
struct
$name:ident
(
$
(
$t:ty
),
*
$
(,)
*
);
)
=>
{
impl_opaque_keys!
{
pub
struct
$name
(
$
(
$t
,)
*
);
impl
OpaqueKeys
for
_
{}
}
};
(
pub
struct
$name:ident
(
$
(
$t:ty
),
*
$
(,)
*
);
impl
OpaqueKeys
for
_
{
$
(
$rest:tt
)
*
}
)
=>
{
#[derive(Default,
Clone,
PartialEq,
Eq,
Encode,
Decode)]
#[cfg_attr(feature
=
"std"
,
derive(Debug,
Serialize,
Deserialize))]
pub
struct
$name
(
$
(
pub
$t
,)
*
);
impl
$crate
::
OpaqueKeys
for
$name
{
fn
count
()
->
usize
{
let
mut
c
=
0
;
$
(
let
_
:
$t
;
c
+=
1
;
)
*
c
}
fn
get_raw
(
&
self
,
i
:
usize
)
->
&
[
u8
]
{
$crate
::
count!
(
impl_opaque_keys
(
!!
self
i
)
$
(
$t
),
*
);
&
[]
}
$
(
$rest
)
*
}
};
(
!!
$self:ident
$param_i:ident
$i:tt
)
=>
{
if
$param_i
==
$i
{
return
$self
.
$i
.as_ref
()
}
}
}
pub
trait
ShouldEndSession
<
BlockNumber
>
{
fn
should_end_session
(
now
:
BlockNumber
)
->
bool
;
}
...
...
@@ -460,14 +392,6 @@ mod tests {
}
}
impl
OpaqueKeys
for
UintAuthorityId
{
fn
count
()
->
usize
{
1
}
// Unsafe, i know, but it's test code and it's just there because it's really convenient to
// keep `UintAuthorityId` as a u64 under the hood.
fn
get_raw
(
&
self
,
_
:
usize
)
->
&
[
u8
]
{
unsafe
{
&
std
::
mem
::
transmute
::
<
_
,
&
[
u8
;
8
]
>
(
&
self
.0
)[
..
]
}
}
fn
get
<
T
:
Decode
>
(
&
self
,
_
:
usize
)
->
Option
<
T
>
{
self
.0
.using_encoded
(|
mut
x
|
T
::
decode
(
&
mut
x
))
}
}
pub
struct
TestSessionHandler
;
impl
SessionHandler
<
u64
>
for
TestSessionHandler
{
fn
on_new_session
<
T
:
OpaqueKeys
>
(
changed
:
bool
,
validators
:
&
[(
u64
,
T
)])
{
...
...
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