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
aaa377e2
Commit
aaa377e2
authored
3 years ago
by
qh11
Browse files
Options
Downloads
Patches
Plain Diff
Update mod.rs
parent
8e2fe86a
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/miner/mod.rs
+46
-5
46 additions, 5 deletions
src/miner/mod.rs
with
46 additions
and
5 deletions
src/miner/mod.rs
+
46
−
5
View file @
aaa377e2
...
...
@@ -7,7 +7,13 @@ use std::time;
use
std
::
thread
;
use
crate
::
types
::
block
::
Block
;
use
crate
::
types
::
block
::{
Block
,
Header
};
use
crate
::
types
::
hash
::{
H256
,
Hashable
};
use
crate
::
types
::
transaction
;
use
crate
::
types
::
transaction
::
SignedTransaction
;
use
std
::
sync
::
{
Arc
,
Mutex
};
use
crate
::
Blockchain
;
use
std
::
time
::{
SystemTime
,
UNIX_EPOCH
};
enum
ControlSignal
{
Start
(
u64
),
// the number controls the lambda of interval between block generation
...
...
@@ -26,6 +32,7 @@ pub struct Context {
control_chan
:
Receiver
<
ControlSignal
>
,
operating_state
:
OperatingState
,
finished_block_chan
:
Sender
<
Block
>
,
blockchain
:
Arc
<
Mutex
<
Blockchain
>>
,
}
#[derive(Clone)]
...
...
@@ -34,7 +41,7 @@ pub struct Handle {
control_chan
:
Sender
<
ControlSignal
>
,
}
pub
fn
new
()
->
(
Context
,
Handle
,
Receiver
<
Block
>
)
{
pub
fn
new
(
blockchain
:
&
Arc
<
Mutex
<
Blockchain
>>
)
->
(
Context
,
Handle
,
Receiver
<
Block
>
)
{
let
(
signal_chan_sender
,
signal_chan_receiver
)
=
unbounded
();
let
(
finished_block_sender
,
finished_block_receiver
)
=
unbounded
();
...
...
@@ -42,6 +49,7 @@ pub fn new() -> (Context, Handle, Receiver<Block>) {
control_chan
:
signal_chan_receiver
,
operating_state
:
OperatingState
::
Paused
,
finished_block_chan
:
finished_block_sender
,
blockchain
:
Arc
::
clone
(
blockchain
),
};
let
handle
=
Handle
{
...
...
@@ -53,7 +61,9 @@ pub fn new() -> (Context, Handle, Receiver<Block>) {
#[cfg(any(test,test_utilities))]
fn
test_new
()
->
(
Context
,
Handle
,
Receiver
<
Block
>
)
{
new
()
let
temp_blockchain
=
Blockchain
::
new
();
let
blockchain
=
Arc
::
new
(
Mutex
::
new
(
temp_blockchain
));
return
new
(
&
blockchain
);
}
impl
Handle
{
...
...
@@ -85,6 +95,9 @@ impl Context {
fn
miner_loop
(
&
mut
self
)
{
// main mining loop
let
mut
locked_blockchain
=
self
.blockchain
.lock
()
.unwrap
();
let
mut
parent
=
locked_blockchain
.tip
();
let
const_difficulty
=
locked_blockchain
.blocks
.get
(
&
parent
)
.unwrap
()
.header.difficulty
;
loop
{
// check and react to control signals
match
self
.operating_state
{
...
...
@@ -120,7 +133,8 @@ impl Context {
self
.operating_state
=
OperatingState
::
Run
(
i
);
}
ControlSignal
::
Update
=>
{
unimplemented!
()
//unimplemented!()
parent
=
locked_blockchain
.tip
();
}
};
}
...
...
@@ -134,6 +148,33 @@ impl Context {
// TODO for student: actual mining, create a block
// TODO for student: if block mining finished, you can have something like: self.finished_block_chan.send(block.clone()).expect("Send finished block error");
let
nonce
:
u32
=
rand
::
random
();
let
timestamp
=
SystemTime
::
now
()
.duration_since
(
UNIX_EPOCH
)
.unwrap
()
.as_millis
();
let
content
:
Vec
<
SignedTransaction
>
=
Vec
::
new
();
let
bytes
=
bincode
::
serialize
(
&
content
)
.unwrap
();
let
merkle_root
=
ring
::
digest
::
digest
(
&
ring
::
digest
::
SHA256
,
&
bytes
)
.into
();
let
header
=
Header
{
parent
,
nonce
,
difficulty
:
const_difficulty
,
timestamp
,
merkle_root
};
let
new_block
=
Block
{
header
,
content
};
if
new_block
.hash
()
<=
const_difficulty
{
match
self
.finished_block_chan
.send
(
new_block
.clone
())
{
Ok
(())
=>
{
parent
=
new_block
.hash
();
}
Err
(
_
)
=>
eprintln!
(
"Send new_block Error!"
)
}
}
if
let
OperatingState
::
Run
(
i
)
=
self
.operating_state
{
if
i
!=
0
{
...
...
@@ -167,4 +208,4 @@ mod test {
}
}
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
\ No newline at end of file
// DO NOT CHANGE THIS COMMENT, IT IS FOR AUTOGRADER. AFTER TEST
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