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
400a22eb
Commit
400a22eb
authored
6 years ago
by
Wei Tang
Committed by
Gav Wood
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Implement codec for U256 (#761)
* Implement codec for U256 * Use little endian and add tests
parent
319549f5
Branches
gh-readonly-queue/master/pr-7432-934c091421af4be362839996bfaa441ba59bf12b
Branches containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
substrate/core/primitives/src/hasher.rs
+1
-1
1 addition, 1 deletion
substrate/core/primitives/src/hasher.rs
substrate/core/primitives/src/uint.rs
+46
-0
46 additions, 0 deletions
substrate/core/primitives/src/uint.rs
with
47 additions
and
1 deletion
substrate/core/primitives/src/hasher.rs
+
1
−
1
View file @
400a22eb
...
...
@@ -45,7 +45,7 @@ pub mod blake2 {
impl
Hasher
for
Blake2Hasher
{
type
Out
=
H256
;
type
StdHasher
=
PlainHasher
;
const
LENGTH
:
usize
=
32
;
const
LENGTH
:
usize
=
32
;
fn
hash
(
x
:
&
[
u8
])
->
Self
::
Out
{
blake2_256
(
x
)
.into
()
}
...
...
This diff is collapsed.
Click to expand it.
substrate/core/primitives/src/uint.rs
+
46
−
0
View file @
400a22eb
...
...
@@ -43,12 +43,33 @@ macro_rules! impl_serde {
}
}
macro_rules!
impl_codec
{
(
$name
:
ident
,
$len
:
expr
)
=>
{
impl
::
codec
::
Encode
for
$name
{
fn
using_encoded
<
R
,
F
:
FnOnce
(
&
[
u8
])
->
R
>
(
&
self
,
f
:
F
)
->
R
{
let
mut
bytes
=
[
0u8
;
$len
*
8
];
self
.to_little_endian
(
&
mut
bytes
);
bytes
.using_encoded
(
f
)
}
}
impl
::
codec
::
Decode
for
$name
{
fn
decode
<
I
:
::
codec
::
Input
>
(
input
:
&
mut
I
)
->
Option
<
Self
>
{
<
[
u8
;
$len
*
8
]
as
::
codec
::
Decode
>
::
decode
(
input
)
.map
(|
b
|
$name
::
from_little_endian
(
&
b
))
}
}
}
}
construct_uint!
(
U256
,
4
);
impl_serde!
(
U256
,
4
);
impl_codec!
(
U256
,
4
);
#[cfg(test)]
mod
tests
{
use
super
::
*
;
use
codec
::{
Encode
,
Decode
};
use
substrate_serializer
as
ser
;
macro_rules!
test
{
...
...
@@ -86,6 +107,31 @@ mod tests {
test!
(
U256
,
test_u256
);
#[test]
fn
test_u256_codec
()
{
let
res1
=
vec!
[
120
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
];
let
res2
=
vec!
[
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
,
0xff
];
assert_eq!
(
U256
::
from
(
120
)
.encode
(),
res1
);
assert_eq!
(
U256
::
max_value
()
.encode
(),
res2
);
assert_eq!
(
U256
::
decode
(
&
mut
&
res1
[
..
]),
Some
(
U256
::
from
(
120
)));
assert_eq!
(
U256
::
decode
(
&
mut
&
res2
[
..
]),
Some
(
U256
::
max_value
()));
}
#[test]
fn
test_large_values
()
{
assert_eq!
(
...
...
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