state.rs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_sdk::{
  19. blockchain::{PidOutput, PreviousSlot, Slot},
  20. crypto::MerkleTree,
  21. pasta::{group::ff::PrimeField, pallas},
  22. };
  23. use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable};
  24. use log::info;
  25. use rand::{thread_rng, Rng};
  26. use super::{
  27. constants,
  28. lead_coin::{LeadCoin, LeadCoinSecrets},
  29. utils::fbig2base,
  30. Block, BlockProposal, Float10,
  31. };
  32. use crate::{
  33. blockchain::Blockchain,
  34. impl_p2p_message,
  35. net::Message,
  36. tx::Transaction,
  37. util::time::{TimeKeeper, Timestamp},
  38. wallet::WalletPtr,
  39. Error, Result,
  40. };
  41. use std::{
  42. fs::File,
  43. io::{prelude::*, BufWriter},
  44. };
  45. /// This struct represents the information required by the consensus algorithm
  46. pub struct ConsensusState {
  47. /// Wallet interface
  48. pub wallet: WalletPtr,
  49. /// Canonical (finalized) blockchain
  50. pub blockchain: Blockchain,
  51. /// Network bootstrap timestamp
  52. pub bootstrap_ts: Timestamp,
  53. /// Helper structure to calculate time related operations
  54. pub time_keeper: TimeKeeper,
  55. /// Genesis block hash
  56. pub genesis_block: blake3::Hash,
  57. /// Total sum of initial staking coins
  58. pub initial_distribution: u64,
  59. /// Flag to enable single-node mode
  60. pub single_node: bool,
  61. /// Slot the network was bootstrapped
  62. pub bootstrap_slot: u64,
  63. /// Participating start slot
  64. pub participating: Option<u64>,
  65. /// Node is able to propose proposals
  66. pub proposing: bool,
  67. /// Last slot node check for finalization
  68. pub checked_finalization: u64,
  69. /// Fork chains containing block proposals
  70. pub forks: Vec<Fork>,
  71. /// Current epoch
  72. pub epoch: u64,
  73. /// Hot/live slots
  74. pub slots: Vec<Slot>,
  75. /// Last slot leaders count
  76. pub previous_leaders: u64,
  77. /// Controller output history
  78. pub f_history: Vec<Float10>,
  79. /// Controller proportional error history
  80. pub err_history: Vec<Float10>,
  81. // TODO: Aren't these already in db after finalization?
  82. /// Canonical competing coins
  83. pub coins: Vec<LeadCoin>,
  84. /// Canonical coin commitments tree
  85. pub coins_tree: MerkleTree,
  86. /// Canonical seen nullifiers from proposals
  87. pub nullifiers: Vec<pallas::Base>,
  88. }
  89. impl ConsensusState {
  90. pub fn new(
  91. wallet: WalletPtr,
  92. blockchain: Blockchain,
  93. bootstrap_ts: Timestamp,
  94. genesis_ts: Timestamp,
  95. genesis_data: blake3::Hash,
  96. initial_distribution: u64,
  97. single_node: bool,
  98. ) -> Self {
  99. let genesis_block = Block::genesis_block(genesis_ts, genesis_data).blockhash();
  100. let time_keeper =
  101. TimeKeeper::new(genesis_ts, constants::EPOCH_LENGTH as u64, constants::SLOT_TIME, 0);
  102. Self {
  103. wallet,
  104. blockchain,
  105. bootstrap_ts,
  106. time_keeper,
  107. genesis_block,
  108. initial_distribution,
  109. single_node,
  110. bootstrap_slot: 0,
  111. participating: None,
  112. proposing: false,
  113. checked_finalization: 0,
  114. forks: vec![],
  115. epoch: 0,
  116. slots: vec![],
  117. previous_leaders: 0,
  118. f_history: vec![constants::FLOAT10_ZERO.clone()],
  119. err_history: vec![constants::FLOAT10_ZERO.clone(), constants::FLOAT10_ZERO.clone()],
  120. coins: vec![],
  121. coins_tree: MerkleTree::new(constants::EPOCH_LENGTH * 100),
  122. nullifiers: vec![],
  123. }
  124. }
  125. /// Finds the last slot a proposal or block was generated.
  126. pub fn last_slot(&self) -> Result<u64> {
  127. let mut slot = 0;
  128. for chain in &self.forks {
  129. for state_checkpoint in &chain.sequence {
  130. if state_checkpoint.proposal.block.header.slot > slot {
  131. slot = state_checkpoint.proposal.block.header.slot;
  132. }
  133. }
  134. }
  135. // We return here in case proposals exist,
  136. // so we don't query the sled database.
  137. if slot > 0 {
  138. return Ok(slot)
  139. }
  140. let (last_slot, _) = self.blockchain.last()?;
  141. Ok(last_slot)
  142. }
  143. /// Set participating slot to next.
  144. pub fn set_participating(&mut self) -> Result<()> {
  145. self.participating = Some(self.time_keeper.current_slot() + 1);
  146. Ok(())
  147. }
  148. /// Generate current slot
  149. fn generate_slot(
  150. &mut self,
  151. fork_hashes: Vec<blake3::Hash>,
  152. fork_previous_hashes: Vec<blake3::Hash>,
  153. sigma1: pallas::Base,
  154. sigma2: pallas::Base,
  155. ) {
  156. let id = self.time_keeper.current_slot();
  157. let previous =
  158. PreviousSlot::new(0, fork_hashes, fork_previous_hashes, self.get_previous_eta(), 0.0);
  159. let pid = PidOutput::new(0.0, 0.0, sigma1, sigma2);
  160. let slot = Slot::new(id, previous, pid, 0, 0);
  161. info!(target: "consensus::state", "generate_slot: {:?}", slot);
  162. self.slots.push(slot);
  163. }
  164. // Initialize node lead coins and set current epoch and eta.
  165. pub async fn init_coins(&mut self) -> Result<()> {
  166. self.epoch = self.time_keeper.current_epoch();
  167. self.coins = self.create_coins().await?;
  168. self.update_forks_checkpoints();
  169. Ok(())
  170. }
  171. /// Check if new epoch has started and generate slot.
  172. /// Returns flag to signify if epoch has changed.
  173. pub async fn epoch_changed(
  174. &mut self,
  175. fork_hashes: Vec<blake3::Hash>,
  176. fork_previous_hashes: Vec<blake3::Hash>,
  177. sigma1: pallas::Base,
  178. sigma2: pallas::Base,
  179. ) -> Result<bool> {
  180. self.generate_slot(fork_hashes, fork_previous_hashes, sigma1, sigma2);
  181. let epoch = self.time_keeper.current_epoch();
  182. if epoch <= self.epoch {
  183. return Ok(false)
  184. }
  185. self.epoch = epoch;
  186. Ok(true)
  187. }
  188. /// Return 2-term target approximation sigma coefficients.
  189. pub fn sigmas(&mut self) -> (pallas::Base, pallas::Base) {
  190. let f = self.win_inv_prob_with_full_stake();
  191. let total_stake = self.total_stake();
  192. let total_sigma = Float10::try_from(total_stake).unwrap();
  193. self.calc_sigmas(f, total_sigma)
  194. }
  195. fn calc_sigmas(&self, f: Float10, total_sigma: Float10) -> (pallas::Base, pallas::Base) {
  196. info!(target: "consensus::state", "sigmas(): f: {}", f);
  197. info!(target: "consensus::state", "sigmas(): total network stake: {:}", total_sigma);
  198. let one = constants::FLOAT10_ONE.clone();
  199. let neg_one = constants::FLOAT10_NEG_ONE.clone();
  200. let two = constants::FLOAT10_TWO.clone();
  201. let field_p = Float10::try_from(constants::P).unwrap();
  202. let x = one - f;
  203. let c = x.ln();
  204. let neg_c = neg_one * c;
  205. let sigma1_fbig = neg_c.clone() /
  206. (total_sigma.clone() + constants::FLOAT10_EPSILON.clone()) *
  207. field_p.clone();
  208. info!(target: "consensus::state", "sigma1_fbig: {:}", sigma1_fbig);
  209. let sigma1 = fbig2base(sigma1_fbig);
  210. let sigma2_fbig = (neg_c / (total_sigma + constants::FLOAT10_EPSILON.clone()))
  211. .powf(two.clone()) *
  212. (field_p / two);
  213. info!(target: "consensus::state", "sigma2_fbig: {:}", sigma2_fbig);
  214. let sigma2 = fbig2base(sigma2_fbig);
  215. (sigma1, sigma2)
  216. }
  217. /// Generate coins for provided sigmas.
  218. /// NOTE: The strategy here is having a single competing coin per slot.
  219. // TODO: DRK coin need to be burned, and consensus coin to be minted.
  220. async fn create_coins(&mut self) -> Result<Vec<LeadCoin>> {
  221. // TODO: cleanup LeadCoinSecrets, no need to keep a vector
  222. let (seeds, epoch_secrets) = {
  223. let mut rng = thread_rng();
  224. let mut seeds: Vec<u64> = Vec::with_capacity(constants::EPOCH_LENGTH);
  225. for _ in 0..constants::EPOCH_LENGTH {
  226. seeds.push(rng.gen());
  227. }
  228. (seeds, LeadCoinSecrets::generate())
  229. };
  230. // LeadCoin matrix containing node competing coins.
  231. let mut coins: Vec<LeadCoin> = Vec::with_capacity(constants::EPOCH_LENGTH);
  232. // Retrieve coin from wallet
  233. // NOTE: In future this will be retrieved from the money contract.
  234. // Execute the query and see if we find any rows
  235. let query_str = format!("SELECT * FROM {}", constants::CONSENSUS_COIN_TABLE);
  236. let wallet_conn = self.wallet.conn.lock().await;
  237. let mut stmt = wallet_conn.prepare(&query_str)?;
  238. let coin = stmt.query_row((), |row| {
  239. let bytes: Vec<u8> = row.get(constants::CONSENSUS_COIN_COL)?;
  240. let coin = deserialize(&bytes).unwrap();
  241. Ok(coin)
  242. });
  243. stmt.finalize()?;
  244. let coin = match coin {
  245. Ok(c) => c,
  246. Err(_) => {
  247. // If no records are found, we generate a new coin and save it to the database
  248. info!(target: "consensus::state", "create_coins(): No LeadCoin was found in DB, generating new one...");
  249. // Temporarily, we compete with fixed stake.
  250. // This stake should be based on how many nodes we want to run, and they all
  251. // must sum to initial distribution total coins.
  252. //let stake = self.initial_distribution;
  253. let c = LeadCoin::new(
  254. 0,
  255. self.time_keeper.current_slot(),
  256. epoch_secrets.secret_keys[0].inner(),
  257. epoch_secrets.merkle_roots[0],
  258. 0,
  259. epoch_secrets.merkle_paths[0].clone(),
  260. pallas::Base::from(seeds[0]),
  261. &mut self.coins_tree,
  262. );
  263. let query_str = format!(
  264. "INSERT INTO {} ({}) VALUES (?1);",
  265. constants::CONSENSUS_COIN_TABLE,
  266. constants::CONSENSUS_COIN_COL
  267. );
  268. let mut stmt = wallet_conn.prepare(&query_str)?;
  269. stmt.execute([serialize(&c)])?;
  270. c
  271. }
  272. };
  273. info!(target: "consensus::state", "create_coins(): Will use LeadCoin with value: {}", coin.value);
  274. coins.push(coin);
  275. Ok(coins)
  276. }
  277. /// Leadership reward, assuming constant reward
  278. /// TODO (res) implement reward mechanism with accord to DRK,DARK token-economics
  279. fn reward(&self) -> u64 {
  280. constants::REWARD
  281. }
  282. /// Auxillary function to calculate total slot rewards.
  283. fn slot_rewards(&self) -> u64 {
  284. // Retrieve existing blocks excluding genesis
  285. let blocks = (self.blockchain.len() as u64) - 1;
  286. // Retrieve longest fork length, to include those proposals in the calculation
  287. let max_fork_length = self.longest_chain_length() as u64;
  288. // Calculate rewarded slots
  289. let rewarded_slots = blocks + max_fork_length;
  290. rewarded_slots * self.reward()
  291. }
  292. /// Network total stake, assuming constant reward.
  293. /// Only used for fine-tuning. At genesis epoch first slot, of absolute index 0,
  294. /// if no stake was distributed, the total stake would be 0.
  295. /// To avoid division by zero, we asume total stake at first division is GENESIS_TOTAL_STAKE(1).
  296. fn total_stake(&self) -> u64 {
  297. let total_stake = self.slot_rewards() + self.initial_distribution;
  298. if total_stake == 0 {
  299. return constants::GENESIS_TOTAL_STAKE
  300. }
  301. total_stake
  302. }
  303. fn f_err(&mut self) -> Float10 {
  304. info!(target: "consensus::state", "Previous leaders: {}", self.previous_leaders);
  305. // Write counter to file
  306. let mut count_str: String = self.previous_leaders.to_string();
  307. count_str.push(',');
  308. let f =
  309. File::options().append(true).create(true).open(constants::LEADER_HISTORY_LOG).unwrap();
  310. {
  311. let mut writer = BufWriter::new(f);
  312. let _ = writer.write(&count_str.into_bytes()).unwrap();
  313. }
  314. // Calculate feedback
  315. let feedback = Float10::try_from(self.previous_leaders as i64).unwrap();
  316. // Reset previous leaders counter
  317. self.previous_leaders = 0;
  318. let target = constants::FLOAT10_ONE.clone();
  319. target - feedback
  320. }
  321. fn discrete_pid(&mut self) -> Float10 {
  322. let k1 = constants::KP.clone() + constants::KI.clone() + constants::KD.clone();
  323. let k2 = constants::FLOAT10_NEG_ONE.clone() * constants::KP.clone() +
  324. constants::FLOAT10_NEG_TWO.clone() * constants::KD.clone();
  325. let k3 = constants::KD.clone();
  326. let f_len = self.f_history.len();
  327. let err = self.f_err();
  328. let err_len = self.err_history.len();
  329. let ret = self.f_history[f_len - 1].clone() +
  330. k1.clone() * err.clone() +
  331. k2.clone() * self.err_history[err_len - 1].clone() +
  332. k3.clone() * self.err_history[err_len - 2].clone();
  333. info!(target: "consensus::state", "pid::f-1: {:}", self.f_history[f_len - 1].clone());
  334. info!(target: "consensus::state", "pid::err: {:}", err);
  335. info!(target: "consensus::state", "pid::err-1: {}", self.err_history[err_len - 1].clone());
  336. info!(target: "consensus::state", "pid::err-2: {}", self.err_history[err_len - 2].clone());
  337. info!(target: "consensus::state", "pid::k1: {}", k1);
  338. info!(target: "consensus::state", "pid::k2: {}", k2);
  339. info!(target: "consensus::state", "pid::k3: {}", k3);
  340. self.err_history.push(err);
  341. ret
  342. }
  343. /// the probability inverse of winnig lottery having all the stake
  344. /// returns f
  345. fn win_inv_prob_with_full_stake(&mut self) -> Float10 {
  346. let mut f = self.discrete_pid();
  347. if f <= constants::FLOAT10_ZERO.clone() {
  348. f = constants::MIN_F.clone()
  349. } else if f >= constants::FLOAT10_ONE.clone() {
  350. f = constants::MAX_F.clone()
  351. }
  352. // log f history
  353. let file =
  354. File::options().append(true).create(true).open(constants::F_HISTORY_LOG).unwrap();
  355. {
  356. let mut f_history = format!("{:}", f);
  357. f_history.push(',');
  358. let mut writer = BufWriter::new(file);
  359. let _ = writer.write(&f_history.into_bytes()).unwrap();
  360. }
  361. self.f_history.push(f.clone());
  362. f
  363. }
  364. /// Check that the participant/stakeholder coins win the slot lottery.
  365. /// If the stakeholder has multiple competing winning coins, only the highest value
  366. /// coin is selected, since the stakeholder can't give more than one proof per block/slot.
  367. /// * 'sigma1', 'sigma2': slot sigmas
  368. /// Returns: (check: bool, idx: usize) where idx is the winning coin's index
  369. pub fn is_slot_leader(
  370. &mut self,
  371. sigma1: pallas::Base,
  372. sigma2: pallas::Base,
  373. ) -> (bool, i64, usize) {
  374. // Check if node can produce proposals
  375. if !self.proposing {
  376. return (false, 0, 0)
  377. }
  378. let fork_index = self.longest_chain_index();
  379. let competing_coins = if fork_index == -1 {
  380. self.coins.clone()
  381. } else {
  382. self.forks[fork_index as usize].sequence.last().unwrap().coins.clone()
  383. };
  384. // If on single-node mode, node always proposes by extending the
  385. // single fork it holds.
  386. if self.single_node {
  387. return (true, fork_index, 0)
  388. }
  389. let mut won = false;
  390. let mut highest_stake = 0;
  391. let mut highest_stake_idx = 0;
  392. let total_stake = self.total_stake();
  393. for (winning_idx, coin) in competing_coins.iter().enumerate() {
  394. info!(target: "consensus::state", "is_slot_leader: coin stake: {:?}", coin.value);
  395. info!(target: "consensus::state", "is_slot_leader: total stake: {}", total_stake);
  396. info!(target: "consensus::state", "is_slot_leader: relative stake: {}", (coin.value as f64) / total_stake as f64);
  397. let first_winning = coin.is_leader(
  398. sigma1,
  399. sigma2,
  400. self.get_previous_eta(),
  401. pallas::Base::from(self.time_keeper.current_slot()),
  402. );
  403. if first_winning && !won {
  404. highest_stake_idx = winning_idx;
  405. }
  406. won |= first_winning;
  407. if won && coin.value > highest_stake {
  408. highest_stake = coin.value;
  409. highest_stake_idx = winning_idx;
  410. }
  411. }
  412. (won, fork_index, highest_stake_idx)
  413. }
  414. /// Finds the longest forkchain the node holds and
  415. /// returns its index.
  416. pub fn longest_chain_index(&self) -> i64 {
  417. let mut length = 0;
  418. let mut index = -1;
  419. if !self.forks.is_empty() {
  420. for (i, chain) in self.forks.iter().enumerate() {
  421. if chain.sequence.len() > length {
  422. length = chain.sequence.len();
  423. index = i as i64;
  424. }
  425. }
  426. }
  427. index
  428. }
  429. /// Finds the length of longest fork chain the node holds.
  430. pub fn longest_chain_length(&self) -> usize {
  431. let mut max = 0;
  432. for fork in &self.forks {
  433. if fork.sequence.len() > max {
  434. max = fork.sequence.len();
  435. }
  436. }
  437. max
  438. }
  439. /// Given a proposal, find the index of the fork chain it extends.
  440. pub fn find_extended_chain_index(&mut self, proposal: &BlockProposal) -> Result<i64> {
  441. // We iterate through all forks to find which fork to extend
  442. let mut chain_index = -1;
  443. let mut state_checkpoint_index = 0;
  444. for (c_index, chain) in self.forks.iter().enumerate() {
  445. // Traverse sequence in reverse
  446. for (sc_index, state_checkpoint) in chain.sequence.iter().enumerate().rev() {
  447. if proposal.block.header.previous == state_checkpoint.proposal.hash {
  448. chain_index = c_index as i64;
  449. state_checkpoint_index = sc_index;
  450. break
  451. }
  452. }
  453. if chain_index != -1 {
  454. break
  455. }
  456. }
  457. // If no fork was found, we check with canonical
  458. if chain_index == -1 {
  459. let (last_slot, last_block) = self.blockchain.last()?;
  460. if proposal.block.header.previous != last_block ||
  461. proposal.block.header.slot <= last_slot
  462. {
  463. info!(target: "consensus::state", "find_extended_chain_index(): Proposal doesn't extend any known chain");
  464. return Ok(-2)
  465. }
  466. // Proposal extends canonical chain
  467. return Ok(-1)
  468. }
  469. // Found fork chain
  470. let chain = &self.forks[chain_index as usize];
  471. // Proposal extends fork at last proposal
  472. if state_checkpoint_index == (chain.sequence.len() - 1) {
  473. return Ok(chain_index)
  474. }
  475. info!(target: "consensus::state", "find_extended_chain_index(): Proposal to fork a forkchain was received.");
  476. let mut chain = self.forks[chain_index as usize].clone();
  477. // We keep all proposals until the one it extends
  478. chain.sequence.drain((state_checkpoint_index + 1)..);
  479. self.forks.push(chain);
  480. Ok(self.forks.len() as i64 - 1)
  481. }
  482. /// Search the chains we're holding for the given proposal.
  483. pub fn proposal_exists(&self, input_proposal: &blake3::Hash) -> bool {
  484. for chain in self.forks.iter() {
  485. for state_checkpoint in chain.sequence.iter().rev() {
  486. if input_proposal == &state_checkpoint.proposal.hash {
  487. return true
  488. }
  489. }
  490. }
  491. false
  492. }
  493. /// Utility function to extract leader selection lottery randomness(eta),
  494. /// defined as the hash of the last block, converted to pallas base.
  495. pub fn get_previous_eta(&self) -> pallas::Base {
  496. let (_, hash) = self.blockchain.last().unwrap();
  497. let mut bytes: [u8; 32] = *hash.as_bytes();
  498. // Read first 254 bits
  499. bytes[30] = 0;
  500. bytes[31] = 0;
  501. pallas::Base::from_repr(bytes).unwrap()
  502. }
  503. /// Auxillary function to retrieve slot of provided slot UID.
  504. pub fn get_slot(&self, id: u64) -> Result<Slot> {
  505. // Check hot/live slotz
  506. for slot in self.slots.iter().rev() {
  507. if slot.id == id {
  508. return Ok(slot.clone())
  509. }
  510. }
  511. // Check if slot is finalized
  512. if let Ok(slots) = self.blockchain.get_slots_by_id(&[id]) {
  513. if !slots.is_empty() {
  514. if let Some(known_slot) = &slots[0] {
  515. return Ok(known_slot.clone())
  516. }
  517. }
  518. }
  519. Err(Error::SlotNotFound(id))
  520. }
  521. /// Auxillary function to check if node has seen current or previous slots.
  522. /// This check ensures that either the slots exist in memory or node has seen the finalization of these slots.
  523. pub fn slots_is_empty(&self) -> bool {
  524. let current_slot = self.time_keeper.current_slot();
  525. if self.get_slot(current_slot).is_ok() {
  526. return false
  527. }
  528. let previous_slot = current_slot - 1;
  529. self.get_slot(previous_slot).is_err()
  530. }
  531. /// Auxillary function to update all fork state checkpoints to nodes coins current canonical states.
  532. /// Note: This function should only be invoked once on nodes' coins creation.
  533. pub fn update_forks_checkpoints(&mut self) {
  534. for fork in &mut self.forks {
  535. for state_checkpoint in &mut fork.sequence {
  536. state_checkpoint.coins = self.coins.clone();
  537. state_checkpoint.coins_tree = self.coins_tree.clone();
  538. }
  539. }
  540. }
  541. /// Retrieve current forks last proposal hashes and their previous
  542. /// hashes. If node holds no fork, retrieve last canonical hash.
  543. pub fn fork_hashes(&self) -> (Vec<blake3::Hash>, Vec<blake3::Hash>) {
  544. let mut hashes = vec![];
  545. let mut previous_hashes = vec![];
  546. for fork in &self.forks {
  547. let proposal = &fork.sequence.last().unwrap().proposal;
  548. hashes.push(proposal.hash);
  549. previous_hashes.push(proposal.block.header.previous);
  550. }
  551. if hashes.is_empty() {
  552. hashes.push(self.genesis_block);
  553. previous_hashes.push(self.genesis_block);
  554. }
  555. (hashes, previous_hashes)
  556. }
  557. /// Auxiliary structure to reset consensus state for a resync
  558. pub fn reset(&mut self) {
  559. self.participating = None;
  560. self.proposing = false;
  561. self.forks = vec![];
  562. self.slots = vec![];
  563. self.previous_leaders = 0;
  564. self.f_history = vec![constants::FLOAT10_ZERO.clone()];
  565. self.err_history = vec![constants::FLOAT10_ZERO.clone(), constants::FLOAT10_ZERO.clone()];
  566. self.nullifiers = vec![];
  567. }
  568. }
  569. /// Auxiliary structure used for consensus syncing.
  570. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  571. pub struct ConsensusRequest {}
  572. impl_p2p_message!(ConsensusRequest, "consensusrequest");
  573. /// Auxiliary structure used for consensus syncing.
  574. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  575. pub struct ConsensusResponse {
  576. /// Slot the network was bootstrapped
  577. pub bootstrap_slot: u64,
  578. /// Current slot
  579. pub current_slot: u64,
  580. /// Hot/live data used by the consensus algorithm
  581. pub forks: Vec<ForkInfo>,
  582. /// Pending transactions
  583. pub pending_txs: Vec<Transaction>,
  584. /// Hot/live slots
  585. pub slots: Vec<Slot>,
  586. // TODO: When Float10 supports encoding/decoding this should be
  587. // replaced by directly using Vec<Float10>
  588. /// Controller output history
  589. pub f_history: Vec<String>,
  590. /// Controller proportional error history
  591. pub err_history: Vec<String>,
  592. /// Seen nullifiers from proposals
  593. pub nullifiers: Vec<pallas::Base>,
  594. }
  595. impl_p2p_message!(ConsensusResponse, "consensusresponse");
  596. /// Auxiliary structure used for consensus syncing.
  597. #[derive(Debug, SerialEncodable, SerialDecodable)]
  598. pub struct ConsensusSyncRequest {}
  599. impl_p2p_message!(ConsensusSyncRequest, "consensussyncrequest");
  600. /// Auxiliary structure used for consensus syncing.
  601. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  602. pub struct ConsensusSyncResponse {
  603. /// Node known bootstrap slot
  604. pub bootstrap_slot: u64,
  605. /// Node is able to propose proposals
  606. pub proposing: bool,
  607. /// Node has hot/live slots
  608. pub is_empty: bool,
  609. }
  610. impl_p2p_message!(ConsensusSyncResponse, "consensussyncresponse");
  611. impl_p2p_message!(Slot, "slot");
  612. /// Auxiliary structure used for slots syncing
  613. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  614. pub struct SlotRequest {
  615. /// Slot UID
  616. pub slot: u64,
  617. }
  618. impl_p2p_message!(SlotRequest, "slotrequest");
  619. /// Auxiliary structure used for slots syncing
  620. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  621. pub struct SlotResponse {
  622. /// Response blocks.
  623. pub slots: Vec<Slot>,
  624. }
  625. impl_p2p_message!(SlotResponse, "slotresponse");
  626. /// Auxiliary structure used to keep track of consensus state checkpoints.
  627. #[derive(Debug, Clone)]
  628. pub struct StateCheckpoint {
  629. /// Block proposal
  630. pub proposal: BlockProposal,
  631. /// Node competing coins current state
  632. pub coins: Vec<LeadCoin>,
  633. /// Coin commitments tree current state
  634. pub coins_tree: MerkleTree,
  635. /// Seen nullifiers from proposals current state
  636. pub nullifiers: Vec<pallas::Base>,
  637. }
  638. impl StateCheckpoint {
  639. pub fn new(
  640. proposal: BlockProposal,
  641. coins: Vec<LeadCoin>,
  642. coins_tree: MerkleTree,
  643. nullifiers: Vec<pallas::Base>,
  644. ) -> Self {
  645. Self { proposal, coins, coins_tree, nullifiers }
  646. }
  647. }
  648. /// Auxiliary structure used for forked consensus state checkpoints syncing
  649. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  650. pub struct StateCheckpointInfo {
  651. /// Block proposal
  652. pub proposal: BlockProposal,
  653. /// Seen nullifiers from proposals current state
  654. pub nullifiers: Vec<pallas::Base>,
  655. }
  656. impl From<StateCheckpoint> for StateCheckpointInfo {
  657. fn from(state_checkpoint: StateCheckpoint) -> Self {
  658. Self { proposal: state_checkpoint.proposal, nullifiers: state_checkpoint.nullifiers }
  659. }
  660. }
  661. impl From<StateCheckpointInfo> for StateCheckpoint {
  662. fn from(state_checkpoint_info: StateCheckpointInfo) -> Self {
  663. Self {
  664. proposal: state_checkpoint_info.proposal,
  665. coins: vec![],
  666. coins_tree: MerkleTree::new(constants::EPOCH_LENGTH * 100),
  667. nullifiers: state_checkpoint_info.nullifiers,
  668. }
  669. }
  670. }
  671. /// This struct represents a sequence of consensus state checkpoints.
  672. #[derive(Debug, Clone)]
  673. pub struct Fork {
  674. pub genesis_block: blake3::Hash,
  675. pub sequence: Vec<StateCheckpoint>,
  676. }
  677. impl Fork {
  678. pub fn new(genesis_block: blake3::Hash, initial_state_checkpoint: StateCheckpoint) -> Self {
  679. Self { genesis_block, sequence: vec![initial_state_checkpoint] }
  680. }
  681. /// Insertion of a valid state checkpoint.
  682. pub fn add(&mut self, state_checkpoint: &StateCheckpoint) {
  683. if self.check_state_checkpoint(state_checkpoint, self.sequence.last().unwrap()) {
  684. self.sequence.push(state_checkpoint.clone());
  685. }
  686. }
  687. /// A fork chain is considered valid when every state checkpoint is valid,
  688. /// based on the `check_state_checkpoint` function
  689. pub fn check_chain(&self) -> bool {
  690. for (index, state_checkpoint) in self.sequence[1..].iter().enumerate() {
  691. if !self.check_state_checkpoint(state_checkpoint, &self.sequence[index]) {
  692. return false
  693. }
  694. }
  695. true
  696. }
  697. /// A state checkpoint is considered valid when its proposal parent hash is equal to the
  698. /// hash of the previous checkpoint's proposal and their slots are incremental,
  699. /// excluding the genesis block proposal.
  700. pub fn check_state_checkpoint(
  701. &self,
  702. state_checkpoint: &StateCheckpoint,
  703. previous: &StateCheckpoint,
  704. ) -> bool {
  705. if state_checkpoint.proposal.block.header.previous == self.genesis_block {
  706. info!(target: "consensus::state", "check_checkpoint(): Genesis block proposal provided.");
  707. return false
  708. }
  709. if state_checkpoint.proposal.block.header.previous != previous.proposal.hash ||
  710. state_checkpoint.proposal.block.header.slot <= previous.proposal.block.header.slot
  711. {
  712. info!(target: "consensus::state", "check_checkpoint(): Provided state checkpoint proposal is invalid.");
  713. return false
  714. }
  715. // TODO: validate rest checkpoint info(like nullifiers)
  716. true
  717. }
  718. }
  719. /// Auxiliary structure used for forks syncing
  720. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  721. pub struct ForkInfo {
  722. pub genesis_block: blake3::Hash,
  723. pub sequence: Vec<StateCheckpointInfo>,
  724. }
  725. impl From<Fork> for ForkInfo {
  726. fn from(fork: Fork) -> Self {
  727. let mut sequence = vec![];
  728. for state_checkpoint in fork.sequence {
  729. sequence.push(state_checkpoint.into());
  730. }
  731. Self { genesis_block: fork.genesis_block, sequence }
  732. }
  733. }
  734. impl From<ForkInfo> for Fork {
  735. fn from(fork_info: ForkInfo) -> Self {
  736. let mut sequence = vec![];
  737. for checkpoint in fork_info.sequence {
  738. sequence.push(checkpoint.into());
  739. }
  740. Self { genesis_block: fork_info.genesis_block, sequence }
  741. }
  742. }
  743. #[cfg(test)]
  744. mod tests {
  745. use crate::{
  746. consensus::{
  747. state::{Blockchain, ConsensusState},
  748. utils::fbig2base,
  749. Float10, TESTNET_BOOTSTRAP_TIMESTAMP, TESTNET_GENESIS_HASH_BYTES,
  750. TESTNET_GENESIS_TIMESTAMP, TESTNET_INITIAL_DISTRIBUTION,
  751. },
  752. wallet::WalletDb,
  753. Result,
  754. };
  755. #[async_std::test]
  756. async fn calc_sigmas_test() -> Result<()> {
  757. // Generate dummy state
  758. let wallet = WalletDb::new(None, None)?;
  759. let sled_db = sled::Config::new().temporary(true).open()?;
  760. let blockchain = Blockchain::new(&sled_db)?;
  761. let state = ConsensusState::new(
  762. wallet,
  763. blockchain,
  764. *TESTNET_BOOTSTRAP_TIMESTAMP,
  765. *TESTNET_GENESIS_TIMESTAMP,
  766. *TESTNET_GENESIS_HASH_BYTES,
  767. *TESTNET_INITIAL_DISTRIBUTION,
  768. true,
  769. );
  770. let precision_diff = Float10::try_from(
  771. "10000000000000000000000000000000000000000000000000000000000000000000000000",
  772. )
  773. .unwrap();
  774. let precision_diff_base = fbig2base(precision_diff);
  775. let f = Float10::try_from("0.01").unwrap();
  776. let total_stake = Float10::try_from("100").unwrap();
  777. let (sigma1, sigma2) = state.calc_sigmas(f, total_stake);
  778. let sigma1_rhs = Float10::try_from(
  779. "2909373465034095801035568917399197865646520818579502832252119592405565440",
  780. )
  781. .unwrap();
  782. let sigma1_rhs_base = fbig2base(sigma1_rhs);
  783. let sigma2_rhs = Float10::try_from(
  784. "9137556389643100714432609642916129738741963230846798778430644027392",
  785. )
  786. .unwrap();
  787. let sigma2_rhs_base = fbig2base(sigma2_rhs);
  788. let sigma1_delta = if sigma1_rhs_base > sigma1 {
  789. sigma1_rhs_base - sigma1
  790. } else {
  791. sigma1 - sigma1_rhs_base
  792. };
  793. let sigma2_delta = if sigma2_rhs_base > sigma2 {
  794. sigma2_rhs_base - sigma2
  795. } else {
  796. sigma2 - sigma2_rhs_base
  797. };
  798. //note! test cases were generated by low precision python scripts.
  799. //https://github.com/ertosns/lotterysim/blob/master/pallas_unittests.csv
  800. assert!(sigma1_delta < precision_diff_base);
  801. assert!(sigma2_delta < precision_diff_base);
  802. Ok(())
  803. }
  804. }