Skip to content
Snippets Groups Projects
Commit 057d2c93 authored by Cardy Tang's avatar Cardy Tang
Browse files

fix multiple miner

parent 0f0a4f77
No related branches found
No related tags found
No related merge requests found
......@@ -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()]));
}
......
......@@ -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;
......
......@@ -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: {:?}\ncurr 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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment