|
@@ -1,6 +1,6 @@
|
|
|
use async_executor::Executor;
|
|
use async_executor::Executor;
|
|
|
use async_std::sync::Arc;
|
|
use async_std::sync::Arc;
|
|
|
-//use log::{debug,info};
|
|
|
|
|
|
|
+use log::{debug,info,error};
|
|
|
use std::fmt;
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use rand::rngs::OsRng;
|
|
use rand::rngs::OsRng;
|
|
@@ -39,6 +39,8 @@ use pasta_curves::pallas;
|
|
|
|
|
|
|
|
use group::ff::PrimeField;
|
|
use group::ff::PrimeField;
|
|
|
|
|
|
|
|
|
|
+const LOG_T : &str = "stakeholder";
|
|
|
|
|
+
|
|
|
#[derive(Debug)]
|
|
#[derive(Debug)]
|
|
|
pub struct SlotWorkspace {
|
|
pub struct SlotWorkspace {
|
|
|
pub st: blake3::Hash,
|
|
pub st: blake3::Hash,
|
|
@@ -144,9 +146,7 @@ impl Stakeholder {
|
|
|
k: Option<u32>,
|
|
k: Option<u32>,
|
|
|
) -> Result<Self> {
|
|
) -> Result<Self> {
|
|
|
let path = expand_path(rel_path).unwrap();
|
|
let path = expand_path(rel_path).unwrap();
|
|
|
- println!("opening db");
|
|
|
|
|
let db = sled::open(&path)?;
|
|
let db = sled::open(&path)?;
|
|
|
- println!("opend db");
|
|
|
|
|
let ts = Timestamp::current_time();
|
|
let ts = Timestamp::current_time();
|
|
|
let genesis_hash = blake3::hash(b"");
|
|
let genesis_hash = blake3::hash(b"");
|
|
|
//TODO lisen and add transactions
|
|
//TODO lisen and add transactions
|
|
@@ -172,7 +172,7 @@ impl Stakeholder {
|
|
|
settings.peers,
|
|
settings.peers,
|
|
|
);
|
|
);
|
|
|
let keypair = Keypair::random(&mut OsRng);
|
|
let keypair = Keypair::random(&mut OsRng);
|
|
|
- println!("stakeholder constructed...");
|
|
|
|
|
|
|
+ debug!(target:LOG_T, "stakeholder constructed");
|
|
|
Ok(Self {
|
|
Ok(Self {
|
|
|
blockchain: bc,
|
|
blockchain: bc,
|
|
|
net: p2p,
|
|
net: p2p,
|
|
@@ -225,12 +225,11 @@ impl Stakeholder {
|
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
async fn init_network(&self) -> Result<()> {
|
|
async fn init_network(&self) -> Result<()> {
|
|
|
- println!("runing p2p net");
|
|
|
|
|
let exec = Arc::new(Executor::new());
|
|
let exec = Arc::new(Executor::new());
|
|
|
self.net.clone().start(exec.clone()).await?;
|
|
self.net.clone().start(exec.clone()).await?;
|
|
|
//TODO (fix) await blocks
|
|
//TODO (fix) await blocks
|
|
|
self.net.clone().run(exec);
|
|
self.net.clone().run(exec);
|
|
|
- println!("p2p net running...");
|
|
|
|
|
|
|
+ info!(target:LOG_T, "net initialized");
|
|
|
Ok(())
|
|
Ok(())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -277,19 +276,14 @@ impl Stakeholder {
|
|
|
/// validate the block proof, and the transactions,
|
|
/// validate the block proof, and the transactions,
|
|
|
/// if so add the proof to metadata if stakeholder isn't the lead.
|
|
/// if so add the proof to metadata if stakeholder isn't the lead.
|
|
|
pub async fn sync_block(&self) {
|
|
pub async fn sync_block(&self) {
|
|
|
|
|
+ info!(target:LOG_T, "syncing blocks");
|
|
|
for chanptr in self.net.channels().lock().await.values() {
|
|
for chanptr in self.net.channels().lock().await.values() {
|
|
|
- //
|
|
|
|
|
let message_subsytem = chanptr.get_message_subsystem();
|
|
let message_subsytem = chanptr.get_message_subsystem();
|
|
|
- println!("--> adding dispatcher to msg subsystem");
|
|
|
|
|
message_subsytem.add_dispatch::<BlockInfo>().await;
|
|
message_subsytem.add_dispatch::<BlockInfo>().await;
|
|
|
- println!("--> added");
|
|
|
|
|
//TODO start channel if isn't started yet
|
|
//TODO start channel if isn't started yet
|
|
|
//let info = chanptr.get_info();
|
|
//let info = chanptr.get_info();
|
|
|
- //println!("channel info: {}", info);
|
|
|
|
|
- println!("--> subscribe msg_sub");
|
|
|
|
|
let msg_sub: MessageSubscription<BlockInfo> =
|
|
let msg_sub: MessageSubscription<BlockInfo> =
|
|
|
chanptr.subscribe_msg::<BlockInfo>().await.expect("missing blockinfo");
|
|
chanptr.subscribe_msg::<BlockInfo>().await.expect("missing blockinfo");
|
|
|
- println!("--> subscribed");
|
|
|
|
|
|
|
|
|
|
let res = msg_sub.receive().await.unwrap();
|
|
let res = msg_sub.receive().await.unwrap();
|
|
|
let blk: BlockInfo = (*res).to_owned();
|
|
let blk: BlockInfo = (*res).to_owned();
|
|
@@ -298,7 +292,7 @@ impl Stakeholder {
|
|
|
//TODO if valid only.
|
|
//TODO if valid only.
|
|
|
let _len = self.blockchain.add(&[blk]);
|
|
let _len = self.blockchain.add(&[blk]);
|
|
|
} else {
|
|
} else {
|
|
|
- println!("received block is invalid!");
|
|
|
|
|
|
|
+ error!(target: LOG_T, "received block is invalid!");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -327,12 +321,12 @@ impl Stakeholder {
|
|
|
}
|
|
}
|
|
|
Ticks::NEWSLOT { e, sl } => self.new_slot(e, sl),
|
|
Ticks::NEWSLOT { e, sl } => self.new_slot(e, sl),
|
|
|
Ticks::TOCKS => {
|
|
Ticks::TOCKS => {
|
|
|
- println!("tocks");
|
|
|
|
|
|
|
+ info!(target:LOG_T, "tocks");
|
|
|
// slot is about to end.
|
|
// slot is about to end.
|
|
|
// sync, and validate.
|
|
// sync, and validate.
|
|
|
// no more transactions to be received/send to the end of slot.
|
|
// no more transactions to be received/send to the end of slot.
|
|
|
if self.workspace.is_leader {
|
|
if self.workspace.is_leader {
|
|
|
- println!("<<<--- [[[leadership won]]] --->>>");
|
|
|
|
|
|
|
+ info!(target: LOG_T, "[leadership won]");
|
|
|
//craete block
|
|
//craete block
|
|
|
let (block_info, _block_hash) = self.workspace.new_block();
|
|
let (block_info, _block_hash) = self.workspace.new_block();
|
|
|
//add the block to the blockchain
|
|
//add the block to the blockchain
|
|
@@ -348,7 +342,7 @@ impl Stakeholder {
|
|
|
}
|
|
}
|
|
|
Ticks::IDLE => continue,
|
|
Ticks::IDLE => continue,
|
|
|
Ticks::OUTOFSYNC => {
|
|
Ticks::OUTOFSYNC => {
|
|
|
- println!("out of sync");
|
|
|
|
|
|
|
+ error!(target: LOG_T, "clock/blockchain are out of sync");
|
|
|
// clock, and blockchain are out of sync
|
|
// clock, and blockchain are out of sync
|
|
|
let _ = self.clock.sync().await;
|
|
let _ = self.clock.sync().await;
|
|
|
self.sync_block().await;
|
|
self.sync_block().await;
|
|
@@ -363,7 +357,7 @@ impl Stakeholder {
|
|
|
/// assuming static stake during the epoch, enforced by the commitment to competing coins
|
|
/// assuming static stake during the epoch, enforced by the commitment to competing coins
|
|
|
/// in the epoch's gen2esis data.
|
|
/// in the epoch's gen2esis data.
|
|
|
fn new_epoch(&mut self) {
|
|
fn new_epoch(&mut self) {
|
|
|
- println!("[new epoch] 4 {}", self);
|
|
|
|
|
|
|
+ info!(target:LOG_T, "[new epoch] 4 {}", self);
|
|
|
let eta = self.get_eta();
|
|
let eta = self.get_eta();
|
|
|
let mut epoch = Epoch::new(self.epoch_consensus, eta);
|
|
let mut epoch = Epoch::new(self.epoch_consensus, eta);
|
|
|
//TODO calculate total stake
|
|
//TODO calculate total stake
|
|
@@ -386,7 +380,7 @@ impl Stakeholder {
|
|
|
/// the lottery proof need to be conditioned on the slot itself, and previous proof.
|
|
/// the lottery proof need to be conditioned on the slot itself, and previous proof.
|
|
|
/// this will encourage each potential leader to play with honesty.
|
|
/// this will encourage each potential leader to play with honesty.
|
|
|
fn new_slot(&mut self, e: u64, sl: u64) {
|
|
fn new_slot(&mut self, e: u64, sl: u64) {
|
|
|
- println!("[new slot] 4 {}\ne:{}, sl:{}", self, e, sl);
|
|
|
|
|
|
|
+ info!(target: LOG_T, "[new slot] 4 {}\ne:{}, sl:{}", self, e, sl);
|
|
|
let empty_ptr = blake3::hash(b"");
|
|
let empty_ptr = blake3::hash(b"");
|
|
|
let st: blake3::Hash =
|
|
let st: blake3::Hash =
|
|
|
if e > 0 || (e == 0 && sl > 0) { self.workspace.block.blockhash() } else { empty_ptr };
|
|
if e > 0 || (e == 0 && sl > 0) { self.workspace.block.blockhash() } else { empty_ptr };
|