Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ece598pv-sp2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
qh11
ece598pv-sp2022
Commits
afc206f4
Commit
afc206f4
authored
3 years ago
by
qh11
Browse files
Options
Downloads
Patches
Plain Diff
Update block.rs
parent
e2c28f45
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/types/block.rs
+69
-5
69 additions, 5 deletions
src/types/block.rs
with
69 additions
and
5 deletions
src/types/block.rs
+
69
−
5
View file @
afc206f4
use
serde
::{
Serialize
,
Deserialize
};
use
crate
::
types
::
hash
::{
H256
,
Hashable
};
use
crate
::
types
::
transaction
::
SignedTransaction
;
use
std
::
time
::{
SystemTime
,
UNIX_EPOCH
};
#[derive(Serialize,
Deserialize,
Debug,
Clone)]
pub
struct
Block
{
pub
header
:
Header
,
pub
content
:
Vec
<
SignedTransaction
>
,
}
#[derive(Serialize,
Deserialize,
Debug,
Clone)]
pub
struct
Header
{
pub
parent
:
H256
,
pub
nonce
:
u32
,
pub
difficulty
:
H256
,
pub
timestamp
:
u128
,
pub
merkle_root
:
H256
,
}
impl
Hashable
for
Block
{
fn
hash
(
&
self
)
->
H256
{
unimplemented!
()
return
self
.header
.hash
();
}
}
impl
Hashable
for
Header
{
fn
hash
(
&
self
)
->
H256
{
let
bytes
=
bincode
::
serialize
(
&
self
)
.unwrap
();
return
ring
::
digest
::
digest
(
&
ring
::
digest
::
SHA256
,
&
bytes
)
.into
();
}
}
impl
Block
{
pub
fn
get_parent
(
&
self
)
->
H256
{
unimplemented!
()
self
.header.parent
}
pub
fn
get_difficulty
(
&
self
)
->
H256
{
unimplemented!
()
self
.header.difficulty
}
}
pub
fn
generate_genesis_block
()
->
Block
{
// let mut rng = rand::r;
let
emptyRoot
=
H256
::
from
([
0
;
32
]);
return
get_random_block
(
&
emptyRoot
);
}
pub
fn
get_random_block
(
parent
:
&
H256
)
->
Block
{
// let mut rng = rand::r;
let
content
:
Vec
<
SignedTransaction
>
=
Vec
::
new
();
let
bytes
=
bincode
::
serialize
(
&
content
)
.unwrap
();
let
merkle_root
=
ring
::
digest
::
digest
(
&
ring
::
digest
::
SHA256
,
&
bytes
)
.into
();
let
difficulty
=
H256
::
from
([
255u8
;
32
]);
let
header
=
Header
{
parent
:
*
parent
,
nonce
:
rand
::
random
::
<
u32
>
(),
timestamp
:
SystemTime
::
now
()
.duration_since
(
UNIX_EPOCH
)
.unwrap
()
.as_millis
(),
merkle_root
:
merkle_root
,
difficulty
:
difficulty
,
};
Block
{
header
:
header
,
content
:
content
,
}
}
#[cfg(any(test,
test_utilities))]
pub
fn
generate_random_block
(
parent
:
&
H256
)
->
Block
{
unimplemented!
()
}
\ No newline at end of file
// let mut rng = rand::r;
let
content
:
Vec
<
SignedTransaction
>
=
Vec
::
new
();
let
bytes
=
bincode
::
serialize
(
&
content
)
.unwrap
();
let
merkle_root
=
ring
::
digest
::
digest
(
&
ring
::
digest
::
SHA256
,
&
bytes
)
.into
();
let
difficulty
=
crate
::
types
::
hash
::
generate_random_hash
();
let
header
=
Header
{
parent
:
*
parent
,
nonce
:
rand
::
random
::
<
u32
>
(),
timestamp
:
SystemTime
::
now
()
.duration_since
(
UNIX_EPOCH
)
.unwrap
()
.as_millis
(),
merkle_root
:
merkle_root
,
difficulty
:
difficulty
,
};
Block
{
header
:
header
,
content
:
content
,
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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