Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ece598pv-sp2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
ttang13
ece598pv-sp2020
Commits
057d2c93
Commit
057d2c93
authored
4 years ago
by
Cardy Tang
Browse files
Options
Downloads
Patches
Plain Diff
fix multiple miner
parent
0f0a4f77
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/generator.rs
+9
-3
9 additions, 3 deletions
src/generator.rs
src/miner.rs
+25
-0
25 additions, 0 deletions
src/miner.rs
src/network/worker.rs
+25
-0
25 additions, 0 deletions
src/network/worker.rs
with
59 additions
and
3 deletions
src/generator.rs
+
9
−
3
View file @
057d2c93
...
...
@@ -10,7 +10,7 @@ use log::{debug,info};
use
std
::
collections
::
HashMap
;
use
crossbeam
::
channel
::{
unbounded
,
Receiver
,
Sender
,
TryRecvError
};
//
use std::time;
use
std
::
time
;
use
rand
::
random
;
use
std
::
sync
::{
Arc
,
Mutex
};
use
std
::
thread
;
...
...
@@ -116,15 +116,19 @@ impl Context {
ControlSignal
::
Balance
=>
{
let
tips
=
self
.blockchain
.lock
()
.unwrap
()
.tip
();
let
coins
=
self
.glob_state
.lock
()
.unwrap
()[
&
tips
]
.clone
();
let
mut
all_my
=
0
;
info!
(
"Longest Chain tip is {:?} Length: {}"
,
tips
,
self
.blockchain
.lock
()
.unwrap
()
.heights
[
&
tips
]);
info!
(
"tip state {:?}"
,
coins
);
for
(
_k
,(
v_a
,
v_b
))
in
coins
{
if
v_b
==
addr
{
debug!
(
"{} My money"
,
v_a
);
all_my
=
all_my
+
v_a
;
}
else
{
debug!
(
"{} {:?}'s money"
,
v_a
,
v_b
);
}
}
info!
(
"All My money {:?}"
,
all_my
);
}
ControlSignal
::
Print
=>
{
let
m
=
self
.mempool
.lock
()
.unwrap
();
...
...
@@ -219,9 +223,11 @@ impl Context {
}
if
let
OperatingState
::
Run
(
i
)
=
self
.operating_state
{
let
tip
=
self
.blockchain
.lock
()
.unwrap
()
.tip
();
let
interval
=
time
::
Duration
::
from_micros
(
1500000
as
u64
);
thread
::
sleep
(
interval
);
if
most_recent_state
==
tip
{
// println!("stay here");
//
self.operating_state = OperatingState::Paused;
self
.operating_state
=
OperatingState
::
Paused
;
continue
;
}
// println!("new transactions");
...
...
@@ -268,7 +274,7 @@ impl Context {
transaction
:
trans
.clone
(),
sig
:
s
.clone
(),
};
//
debug!("Generated transactions: {:?}",trans);
debug!
(
"Generated transactions: {:?}"
,
trans
);
self
.mempool
.lock
()
.unwrap
()
.insert
(
new_trans
.hash
(),
new_trans
.clone
());
self
.server
.broadcast
(
Message
::
NewTransactionHashes
(
vec!
[
new_trans
.hash
()]));
}
...
...
This diff is collapsed.
Click to expand it.
src/miner.rs
+
25
−
0
View file @
057d2c93
...
...
@@ -170,6 +170,7 @@ impl Context {
let
lim_t
:
usize
=
5
;
let
lim_p
:
usize
=
5
;
let
tip
=
self
.blockchain
.lock
()
.unwrap
()
.tip
();
let
mut
parent_pool
:
HashMap
<
H256
,
bool
>
=
HashMap
::
new
();
let
mut
new_state
:
HashMap
<
(
H256
,
u32
),
(
u32
,
H160
)
>
=
self
.glob_state
.lock
()
.unwrap
()[
&
tip
]
.clone
();
let
mut
to_add_trans
:
Vec
<
SignedTransaction
>
=
Vec
::
new
();
let
mut
to_remove_trans
:
Vec
<
H256
>
=
Vec
::
new
();
...
...
@@ -177,6 +178,21 @@ impl Context {
let
mut
to_remove_trans_b
:
Vec
<
H256
>
=
Vec
::
new
();
let
m
=
self
.mempool
.lock
()
.unwrap
();
let
mut
iter
=
m
.keys
();
let
mut
curr_examine
=
tip
;
//build all parent pointed tx blocks
loop
{
let
curr_block
=
self
.blockchain
.lock
()
.unwrap
()
.blocks
[
&
curr_examine
]
.clone
();
if
curr_block
.header.parent
==
H256
::
from
([
0
;
32
]){
break
;
}
curr_examine
=
curr_block
.header.parent
;
for
item
in
curr_block
.content.data
{
if
!
parent_pool
.contains_key
(
&
item
){
parent_pool
.insert
(
item
.clone
(),
true
);
}
}
}
for
_k
in
0
..
lim_t
{
let
remove_key
:
H256
;
match
iter
.next
(){
...
...
@@ -217,6 +233,7 @@ impl Context {
let
tx_local_lookup
=
self
.txpool
.lock
()
.unwrap
();
let
mut
tx_iter
=
tx_local
.keys
();
let
mut
tx_seen
:
HashMap
<
H256
,
bool
>
=
HashMap
::
new
();
let
mut
to_remove_parent
:
Vec
<
H256
>
=
Vec
::
new
();
for
_k
in
0
..
lim_p
{
let
remove_key
:
H256
;
let
mut
valid
=
false
;
...
...
@@ -228,6 +245,11 @@ impl Context {
break
;
}
}
if
parent_pool
.contains_key
(
&
remove_key
){
println!
(
"block already in parent tree"
);
to_remove_parent
.push
(
remove_key
.clone
());
continue
;
}
let
curr_tx_b
=
tx_local_lookup
[
&
remove_key
]
.clone
();
for
curr_tx_idx
in
0
..
curr_tx_b
.content.data
.len
(){
let
curr_tx
=
curr_tx_b
.content.data
[
curr_tx_idx
]
.clone
();
...
...
@@ -260,6 +282,9 @@ impl Context {
}
std
::
mem
::
drop
(
tx_local
);
std
::
mem
::
drop
(
tx_local_lookup
);
for
item
in
to_remove_parent
{
self
.unseen
.lock
()
.unwrap
()
.remove
(
&
item
);
}
let
new_parent
=
self
.blockchain
.lock
()
.unwrap
()
.tip
();
let
new_difficulty
=
self
.blockchain
.lock
()
.unwrap
()
.blocks
[
&
new_parent
]
.header.difficulty
;
let
new_difficulty_m
=
self
.blockchain
.lock
()
.unwrap
()
.blocks
[
&
new_parent
]
.header.difficulty_m
;
...
...
This diff is collapsed.
Click to expand it.
src/network/worker.rs
+
25
−
0
View file @
057d2c93
...
...
@@ -112,6 +112,7 @@ impl Context {
let
txb
=
self
.txpool
.lock
()
.unwrap
()[
&
txb_hash
]
.clone
();
for
tx
in
txb
.content.data
.clone
(){
println!
(
"Glob state: {:?}
\n
curr tx: {:?}"
,
self
.glob_state
.lock
()
.unwrap
()[
&
tip
],
tx
.transaction
);
//1. Check if the transactions are signed correctly by the public keys
if
verify
(
&
tx
.transaction
,
&
tx
.sig.key
,
&
tx
.sig.signature
)
==
false
{
valid
=
false
;
...
...
@@ -284,6 +285,30 @@ impl Context {
t
.push
(
addr
);
peer
.write
(
Message
::
UpdateAddr
(
t
.clone
()));
self
.server
.broadcast
(
Message
::
BroadcastAddr
(
addr
));
let
tx_local
=
self
.txpool
.lock
()
.unwrap
();
let
mut
tx_iter
=
tx_local
.keys
();
loop
{
match
tx_iter
.next
(){
Some
(
keys
)
=>
{
self
.server
.broadcast
(
Message
::
NewTxBlockHashes
(
vec!
[
keys
.clone
()]));
}
None
=>
{
break
;
}
}
}
let
m
=
self
.mempool
.lock
()
.unwrap
();
let
mut
iter
=
m
.keys
();
loop
{
match
iter
.next
(){
Some
(
keys
)
=>
{
self
.server
.broadcast
(
Message
::
NewTransactionHashes
(
vec!
[
keys
.clone
()]));
}
None
=>
{
break
;
}
}
}
}
Message
::
BroadcastAddr
(
addr
)
=>
{
// debug!("Broadcast Addr {:?}",addr);
...
...
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