[consensus] pid k2 bug in sign fixed; miscel changes

This commit is contained in:
mohab metwally
2023-01-07 20:30:13 +02:00
parent 9a376a84d0
commit 65648e55cb
6 changed files with 50 additions and 25 deletions

View File

@@ -30,8 +30,9 @@ use darkfi::{
consensus::{
constants::{
MAINNET_BOOTSTRAP_TIMESTAMP, MAINNET_GENESIS_HASH_BYTES, MAINNET_GENESIS_TIMESTAMP,
MAINNET_INITIAL_DISTRIBUTION, TESTNET_BOOTSTRAP_TIMESTAMP, TESTNET_GENESIS_HASH_BYTES,
TESTNET_GENESIS_TIMESTAMP, TESTNET_INITIAL_DISTRIBUTION,
MAINNET_INITIAL_DISTRIBUTION, TESTNET_GENESIS_HASH_BYTES,
TESTNET_INITIAL_DISTRIBUTION, TESTNET_GENESIS_TIMESTAMP,
TESTNET_BOOTSTRAP_TIMESTAMP,
},
proto::{ProtocolProposal, ProtocolSync, ProtocolSyncConsensus, ProtocolTx},
task::{block_sync_task, proposal_task},
@@ -48,7 +49,7 @@ use darkfi::{
},
server::{listen_and_serve, RequestHandler},
},
util::path::expand_path,
util::{path::expand_path,time::Timestamp},
wallet::{walletdb::init_wallet, WalletPtr},
Error, Result,
};
@@ -299,7 +300,7 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'_>>) -> Result<()> {
let sled_db = sled::open(&db_path)?;
// Initialize validator state
let (bootstrap_ts, genesis_ts, genesis_data, initial_distribution) = match args.chain.as_str() {
let (mut bootstrap_ts, mut genesis_ts, genesis_data, initial_distribution) = match args.chain.as_str() {
"mainnet" => (
*MAINNET_BOOTSTRAP_TIMESTAMP,
*MAINNET_GENESIS_TIMESTAMP,
@@ -309,6 +310,8 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'_>>) -> Result<()> {
"testnet" => (
*TESTNET_BOOTSTRAP_TIMESTAMP,
*TESTNET_GENESIS_TIMESTAMP,
//Timestamp::current_time(),
//Timestamp::current_time(),
*TESTNET_GENESIS_HASH_BYTES,
*TESTNET_INITIAL_DISTRIBUTION,
),
@@ -317,7 +320,8 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'_>>) -> Result<()> {
return Err(Error::UnsupportedChain)
}
};
//genesis_ts.add(-9);
//bootstrap_ts.add(-9);
// Parse faucet addresses
let mut faucet_pubkeys = vec![];

View File

@@ -1,8 +1,8 @@
#!/bin/sh
rm -rf darkfid0 darkfid1 darkfid2 darkfid3 darkfid4
# clean log files
rm /tmp/f_history.log /tmp/lead_history.log &>/dev/null &
rm /tmp/f_history.log /tmp/lead_history.log /tmp/lottery_history.log &>/dev/null &
touch /tmp/f_history.log
touch /tmp/lead_history.log
touch /tmp/lottery_history.log

View File

@@ -9,6 +9,10 @@ else
verbose=""
fi
touch /tmp/f_history.log
touch /tmp/lead_history.log
touch /tmp/lottery_history.log
tmux new-session -d
tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid0.toml" Enter
sleep 2

View File

@@ -40,10 +40,10 @@ lazy_static! {
pub static ref TESTNET_GENESIS_HASH_BYTES: blake3::Hash = blake3::hash(b"darkfi_testnet");
/// Genesis timestamp for the testnet chain
pub static ref TESTNET_GENESIS_TIMESTAMP: Timestamp = Timestamp(1673039000);
pub static ref TESTNET_GENESIS_TIMESTAMP: Timestamp = Timestamp(1673115200);
/// Bootstrap timestamp for the testnet chain
pub static ref TESTNET_BOOTSTRAP_TIMESTAMP: Timestamp = Timestamp(1673039000);
pub static ref TESTNET_BOOTSTRAP_TIMESTAMP: Timestamp = Timestamp(1673115200);
/// Total sum of initial staking coins for the testnet chain
pub static ref TESTNET_INITIAL_DISTRIBUTION: u64 = 1000;
@@ -134,3 +134,4 @@ pub const GENESIS_TOTAL_STAKE: u64 = 1;
pub const LEADER_HISTORY_LOG : &str = "/tmp/lead_history.log";
pub const F_HISTORY_LOG : &str = "/tmp/f_history.log";
pub const LOTTERY_HISTORY_LOG : &str = "/tmp/lottery_history.log";

View File

@@ -42,6 +42,9 @@ use crate::{
Result,
};
use std::io::{prelude::*, BufWriter};
use std::fs::File;
pub const MERKLE_DEPTH_LEADCOIN: usize = 32;
pub const MERKLE_DEPTH: u8 = 32;
pub const ZERO: pallas::Base = pallas::Base::zero();
@@ -311,8 +314,15 @@ impl LeadCoin {
let value = pallas::Base::from(self.value);
let target = pallas::Base::one().neg() * (sigma1 * value + sigma2 * value * value);
info!(target: "consensus::leadcoin", "is_leader(): y = {:?}", y);
info!(target: "consensus::leadcoin", "is_leader(): T = {:?}", target);
let mut y_t_str = format!("{:?},{:?}\n", y,target);
let mut f = File::options().append(true).open(constants::LOTTERY_HISTORY_LOG).unwrap();
{
let mut writer = BufWriter::new(f);
writer.write(&y_t_str.into_bytes()).unwrap();
}
info!("is_leader(): y = {:?}", y);
info!("is_leader(): T = {:?}", target);
y < target
}

View File

@@ -495,17 +495,23 @@ impl ConsensusState {
let k1 = constants::KP.clone() +
constants::KI.clone() +
constants::KD.clone();
let k2 = constants::FLOAT10_NEG_ONE.clone() * constants::KP.clone() -
constants::FLOAT10_NEG_TWO.clone() * constants::KD.clone();
let k2 = constants::FLOAT10_NEG_ONE.clone() * constants::KP.clone() + constants::FLOAT10_NEG_TWO.clone() * constants::KD.clone();
let k3 = constants::KD.clone();
let f_len = self.f_history.len();
let err = self.f_dif();
let err_len = self.err_history.len();
let ret = self.f_history[f_len-1].clone() +
k1 * err.clone() +
k2 * self.err_history[err_len-1].clone() +
k3 * self.err_history[err_len-2].clone();
self.err_history.push(err);
k1.clone() * err.clone() +
k2.clone() * self.err_history[err_len-1].clone() +
k3.clone() * self.err_history[err_len-2].clone();
info!("pid::f-1: {:}", self.f_history[f_len-1].clone());
info!("pid::err: {:}", err);
info!("pid::err-1: {}", self.err_history[err_len-1].clone());
info!("pid::err-2: {}", self.err_history[err_len-2].clone());
info!("pid::k1: {}", k1.clone());
info!("pid::k2: {}", k2.clone());
info!("pid::k3: {}", k3.clone());
self.err_history.push(err.clone());
ret
}
/// the probability inverse of winnig lottery having all the stake
@@ -514,14 +520,6 @@ impl ConsensusState {
self.extend_leaders_history();
//
let mut f = self.discrete_pid();
// log f history
let file = File::options().append(true).open(constants::F_HISTORY_LOG).unwrap();
{
let mut f_history = format!("{:}", f);
f_history.push_str(",");
let mut writer = BufWriter::new(file);
writer.write(&f_history.into_bytes()).unwrap();
}
if f <= constants::FLOAT10_ZERO.clone() {
f = constants::MIN_F.clone()
} else if f >= constants::FLOAT10_ONE.clone() {
@@ -536,6 +534,14 @@ impl ConsensusState {
{
f = f.clone() * constants::DEG_RATE.clone().powf(self.zero_leads_len());
}
// log f history
let file = File::options().append(true).open(constants::F_HISTORY_LOG).unwrap();
{
let mut f_history = format!("{:}", f);
f_history.push_str(",");
let mut writer = BufWriter::new(file);
writer.write(&f_history.into_bytes()).unwrap();
}
self.f_history.push(f.clone());
f
}