state.rs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 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 std::time::Duration;
  19. use chrono::{NaiveDateTime, Utc};
  20. use darkfi_sdk::crypto::{constants::MERKLE_DEPTH, MerkleNode};
  21. use darkfi_serial::{SerialDecodable, SerialEncodable};
  22. use incrementalmerkletree::bridgetree::BridgeTree;
  23. use log::info;
  24. use pasta_curves::{group::ff::PrimeField, pallas};
  25. use rand::{thread_rng, Rng};
  26. use super::{
  27. constants,
  28. leadcoin::{LeadCoin, LeadCoinSecrets},
  29. utils::fbig2base,
  30. Block, BlockProposal, Float10,
  31. };
  32. use crate::{blockchain::Blockchain, net, tx::Transaction, util::time::Timestamp, Error, Result};
  33. use dashu::base::Abs;
  34. /// This struct represents the information required by the consensus algorithm
  35. pub struct ConsensusState {
  36. /// Canonical (finalized) blockchain
  37. pub blockchain: Blockchain,
  38. /// Genesis block creation timestamp
  39. pub genesis_ts: Timestamp,
  40. /// Genesis block hash
  41. pub genesis_block: blake3::Hash,
  42. /// Participating start slot
  43. pub participating: Option<u64>,
  44. /// Last slot node check for finalization
  45. pub checked_finalization: u64,
  46. /// Slots offset since genesis,
  47. pub offset: Option<u64>,
  48. /// Fork chains containing block proposals
  49. pub forks: Vec<Fork>,
  50. /// Current epoch
  51. pub epoch: u64,
  52. /// Current epoch eta
  53. pub epoch_eta: pallas::Base,
  54. /// Hot/live slot checkpoints
  55. pub slot_checkpoints: Vec<SlotCheckpoint>,
  56. /// Leaders count history
  57. pub leaders_history: Vec<u64>,
  58. // TODO: Aren't these already in db after finalization?
  59. /// Canonical competing coins
  60. pub coins: Vec<LeadCoin>,
  61. /// Canonical coin commitments tree
  62. pub coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  63. /// Canonical seen nullifiers from proposals
  64. pub nullifiers: Vec<pallas::Base>,
  65. }
  66. impl ConsensusState {
  67. pub fn new(
  68. blockchain: Blockchain,
  69. genesis_ts: Timestamp,
  70. genesis_data: blake3::Hash,
  71. ) -> Result<Self> {
  72. let genesis_block = Block::genesis_block(genesis_ts, genesis_data).blockhash();
  73. Ok(Self {
  74. blockchain,
  75. genesis_ts,
  76. genesis_block,
  77. participating: None,
  78. checked_finalization: 0,
  79. offset: None,
  80. forks: vec![],
  81. epoch: 0,
  82. epoch_eta: pallas::Base::one(),
  83. slot_checkpoints: vec![],
  84. leaders_history: vec![0],
  85. coins: vec![],
  86. coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
  87. nullifiers: vec![],
  88. })
  89. }
  90. /// Calculates current epoch.
  91. pub fn current_epoch(&self) -> u64 {
  92. self.slot_epoch(self.current_slot())
  93. }
  94. /// Calculates the epoch of the provided slot.
  95. /// Epoch duration is configured using the `EPOCH_LENGTH` value.
  96. pub fn slot_epoch(&self, slot: u64) -> u64 {
  97. slot / constants::EPOCH_LENGTH as u64
  98. }
  99. /// Calculates current slot, based on elapsed time from the genesis block.
  100. /// Slot duration is configured using the `SLOT_TIME` constant.
  101. pub fn current_slot(&self) -> u64 {
  102. self.genesis_ts.elapsed() / constants::SLOT_TIME
  103. }
  104. /// Calculates the relative number of the provided slot.
  105. pub fn relative_slot(&self, slot: u64) -> u64 {
  106. slot % constants::EPOCH_LENGTH as u64
  107. }
  108. /// Finds the last slot a proposal or block was generated.
  109. pub fn last_slot(&self) -> Result<u64> {
  110. let mut slot = 0;
  111. for chain in &self.forks {
  112. for state_checkpoint in &chain.sequence {
  113. if state_checkpoint.proposal.block.header.slot > slot {
  114. slot = state_checkpoint.proposal.block.header.slot;
  115. }
  116. }
  117. }
  118. // We return here in case proposals exist,
  119. // so we don't query the sled database.
  120. if slot > 0 {
  121. return Ok(slot)
  122. }
  123. let (last_slot, _) = self.blockchain.last()?;
  124. Ok(last_slot)
  125. }
  126. /// Calculates seconds until next Nth slot starting time.
  127. /// Slots duration is configured using the SLOT_TIME constant.
  128. pub fn next_n_slot_start(&self, n: u64) -> Duration {
  129. assert!(n > 0);
  130. let start_time = NaiveDateTime::from_timestamp_opt(self.genesis_ts.0, 0).unwrap();
  131. let current_slot = self.current_slot() + n;
  132. let next_slot_start =
  133. (current_slot * constants::SLOT_TIME) + (start_time.timestamp() as u64);
  134. let next_slot_start = NaiveDateTime::from_timestamp_opt(next_slot_start as i64, 0).unwrap();
  135. let current_time = NaiveDateTime::from_timestamp_opt(Utc::now().timestamp(), 0).unwrap();
  136. let diff = next_slot_start - current_time;
  137. Duration::new(diff.num_seconds().try_into().unwrap(), 0)
  138. }
  139. /// Calculate slots until next Nth epoch.
  140. /// Epoch duration is configured using the EPOCH_LENGTH value.
  141. pub fn slots_to_next_n_epoch(&self, n: u64) -> u64 {
  142. assert!(n > 0);
  143. let slots_till_next_epoch =
  144. constants::EPOCH_LENGTH as u64 - self.relative_slot(self.current_slot());
  145. ((n - 1) * constants::EPOCH_LENGTH as u64) + slots_till_next_epoch
  146. }
  147. /// Calculates seconds until next Nth epoch starting time.
  148. pub fn next_n_epoch_start(&self, n: u64) -> Duration {
  149. self.next_n_slot_start(self.slots_to_next_n_epoch(n))
  150. }
  151. /// Set participating slot to next.
  152. pub fn set_participating(&mut self) -> Result<()> {
  153. self.participating = Some(self.current_slot() + 1);
  154. Ok(())
  155. }
  156. /// Generate current slot checkpoint
  157. fn generate_slot_checkpoint(&mut self, sigma1: pallas::Base, sigma2: pallas::Base) {
  158. let slot = self.current_slot();
  159. let checkpoint = SlotCheckpoint { slot, eta: self.epoch_eta, sigma1, sigma2 };
  160. self.slot_checkpoints.push(checkpoint);
  161. }
  162. /// Check if new epoch has started, to create new epoch coins.
  163. /// Returns flag to signify if epoch has changed and vector of
  164. /// new epoch competing coins.
  165. pub async fn epoch_changed(
  166. &mut self,
  167. sigma1: pallas::Base,
  168. sigma2: pallas::Base,
  169. ) -> Result<bool> {
  170. let epoch = self.current_epoch();
  171. if epoch <= self.epoch {
  172. self.generate_slot_checkpoint(sigma1.clone(), sigma2.clone());
  173. return Ok(false)
  174. }
  175. let eta = self.get_eta();
  176. if self.coins.len() == 0 {
  177. self.coins = self.create_coins(eta).await?;
  178. self.update_forks_checkpoints();
  179. }
  180. self.epoch = epoch;
  181. self.epoch_eta = eta;
  182. self.generate_slot_checkpoint(sigma1.clone(), sigma2.clone());
  183. Ok(true)
  184. }
  185. /// return 2-term target approximation sigma coefficients.
  186. pub fn sigmas(&mut self) -> (pallas::Base, pallas::Base) {
  187. let f = self.win_inv_prob_with_full_stake();
  188. // Generate sigmas
  189. let mut total_stake = self.total_stake(); // Only used for fine-tuning
  190. // at genesis epoch first slot, of absolute index 0,
  191. // the total stake would be 0, to avoid division by zero,
  192. // we asume total stake at first division is GENESIS_TOTAL_STAKE.
  193. if total_stake == 0 {
  194. total_stake = constants::GENESIS_TOTAL_STAKE;
  195. }
  196. info!("sigmas(): f: {}", f);
  197. info!("sigmas(): stake: {}", total_stake);
  198. let one = constants::FLOAT10_ONE.clone();
  199. let two = constants::FLOAT10_TWO.clone();
  200. let field_p = Float10::from_str_native(constants::P)
  201. .unwrap()
  202. .with_precision(constants::RADIX_BITS)
  203. .value();
  204. let total_sigma =
  205. Float10::try_from(total_stake).unwrap().with_precision(constants::RADIX_BITS).value();
  206. let x = one - f;
  207. let c = x.ln();
  208. let sigma1_fbig = c.clone() / total_sigma.clone() * field_p.clone();
  209. let sigma1 = fbig2base(sigma1_fbig);
  210. let sigma2_fbig = (c / total_sigma).powf(two.clone()) * (field_p / two);
  211. let sigma2 = fbig2base(sigma2_fbig);
  212. (sigma1, sigma2)
  213. }
  214. /// Generate coins for provided sigmas.
  215. /// NOTE: The strategy here is having a single competing coin per slot.
  216. // TODO: DRK coin need to be burned, and consensus coin to be minted.
  217. async fn create_coins(&mut self, eta: pallas::Base) -> Result<Vec<LeadCoin>> {
  218. let slot = self.current_slot();
  219. // TODO: cleanup LeadCoinSecrets, no need to keep a vector
  220. let mut rng = thread_rng();
  221. let mut seeds: Vec<u64> = Vec::with_capacity(constants::EPOCH_LENGTH);
  222. for _ in 0..constants::EPOCH_LENGTH {
  223. seeds.push(rng.gen());
  224. }
  225. let epoch_secrets = LeadCoinSecrets::generate();
  226. // LeadCoin matrix containing node competing coins.
  227. let mut coins: Vec<LeadCoin> = Vec::with_capacity(constants::EPOCH_LENGTH);
  228. // TODO: TESTNET: Here we would look into the wallet to find coins we're able to use.
  229. // The wallet has specific tables for consensus coins.
  230. // TODO: TESTNET: Token ID still has to be enforced properly in the consensus.
  231. // Temporarily, we compete with zero stake
  232. let coin = LeadCoin::new(
  233. eta,
  234. rand::thread_rng().gen_range(0..1000),
  235. slot,
  236. epoch_secrets.secret_keys[0].inner(),
  237. epoch_secrets.merkle_roots[0],
  238. 0,
  239. epoch_secrets.merkle_paths[0],
  240. seeds[0],
  241. epoch_secrets.secret_keys[0],
  242. &mut self.coins_tree,
  243. );
  244. coins.push(coin);
  245. Ok(coins)
  246. }
  247. /// leadership reward, assuming constant reward
  248. /// TODO (res) implement reward mechanism with accord to DRK,DARK token-economics
  249. fn reward() -> u64 {
  250. constants::REWARD
  251. }
  252. /// Auxillary function to receive current slot offset.
  253. /// If offset is None, its setted up as last block slot offset.
  254. pub fn get_current_offset(&mut self, current_slot: u64) -> u64 {
  255. // This is the case were we restarted our node, didn't receive offset from other nodes,
  256. // so we need to find offset from last block, exluding network dead period.
  257. if self.offset.is_none() {
  258. let (last_slot, last_offset) = self.blockchain.get_last_offset().unwrap();
  259. let offset = last_offset + (current_slot - last_slot);
  260. info!("get_current_offset(): Setting slot offset: {}", offset);
  261. self.offset = Some(offset);
  262. }
  263. self.offset.unwrap()
  264. }
  265. /// Auxillary function to calculate overall empty slots.
  266. /// We keep an offset from genesis indicating when the first slot actually started.
  267. /// This offset is shared between nodes.
  268. fn overall_empty_slots(&mut self, current_slot: u64) -> u64 {
  269. // Retrieve existing blocks excluding genesis
  270. let blocks = (self.blockchain.len() as u64) - 1;
  271. // Setup offset if only have genesis and havent received offset from other nodes
  272. if blocks == 0 && self.offset.is_none() {
  273. info!(
  274. "overall_empty_slots(): Blockchain contains only genesis, setting slot offset: {}",
  275. current_slot
  276. );
  277. self.offset = Some(current_slot);
  278. }
  279. // Retrieve longest fork length, to also those proposals in the calculation
  280. let max_fork_length = self.longest_chain_length() as u64;
  281. current_slot - blocks - self.get_current_offset(current_slot) - max_fork_length
  282. }
  283. /// total stake
  284. /// assuming constant Reward.
  285. fn total_stake(&mut self) -> i64 {
  286. let current_slot = self.current_slot();
  287. ((current_slot - self.overall_empty_slots(current_slot)) * Self::reward()) as i64
  288. }
  289. /// Calculate how many leaders existed in previous slot and appends
  290. /// it to history, to report it if win. On finalization sync period,
  291. /// node replaces its leaders history with the sequence extracted by
  292. /// the longest fork.
  293. fn extend_leaders_history(&mut self) -> Float10 {
  294. let slot = self.current_slot();
  295. let previous_slot = slot - 1;
  296. let mut count = 0;
  297. for chain in &self.forks {
  298. // Previous slot proposals exist at end of each fork
  299. if chain.sequence.last().unwrap().proposal.block.header.slot == previous_slot {
  300. count += 1;
  301. }
  302. }
  303. self.leaders_history.push(count);
  304. info!("extend_leaders_history(): Current leaders history: {:?}", self.leaders_history);
  305. Float10::try_from(count as i64).unwrap().with_precision(constants::RADIX_BITS).value()
  306. }
  307. fn pid_error(feedback: Float10) -> Float10 {
  308. let target = constants::FLOAT10_ONE.clone();
  309. target - feedback
  310. }
  311. fn f_dif(&mut self) -> Float10 {
  312. Self::pid_error(self.extend_leaders_history())
  313. }
  314. fn weighted_f_dif(&mut self) -> Float10 {
  315. constants::KP.clone() * self.f_dif()
  316. }
  317. fn f_der(&self) -> Float10 {
  318. let len = self.leaders_history.len();
  319. let last = Float10::try_from(self.leaders_history[len - 1] as i64)
  320. .unwrap()
  321. .with_precision(constants::RADIX_BITS)
  322. .value();
  323. let second_to_last = Float10::try_from(self.leaders_history[len - 2] as i64)
  324. .unwrap()
  325. .with_precision(constants::RADIX_BITS)
  326. .value();
  327. let mut der =
  328. (Self::pid_error(second_to_last) - Self::pid_error(last)) / constants::DT.clone();
  329. der = if der > constants::MAX_DER.clone() { constants::MAX_DER.clone() } else { der };
  330. der = if der < constants::MIN_DER.clone() { constants::MIN_DER.clone() } else { der };
  331. der
  332. }
  333. fn weighted_f_der(&self) -> Float10 {
  334. constants::KD.clone() * self.f_der()
  335. }
  336. fn f_int(&self) -> Float10 {
  337. let mut sum = constants::FLOAT10_ZERO.clone();
  338. let lead_history_len = self.leaders_history.len();
  339. let history_begin_index = if lead_history_len > 10 { lead_history_len - 10 } else { 0 };
  340. for lf in &self.leaders_history[history_begin_index..] {
  341. sum += Float10::try_from(lf.clone()).unwrap().abs();
  342. }
  343. sum
  344. }
  345. fn weighted_f_int(&self) -> Float10 {
  346. constants::KI.clone() * self.f_int()
  347. }
  348. fn zero_leads_len(&self) -> Float10 {
  349. let mut count = constants::FLOAT10_ZERO.clone();
  350. let hist_len = self.leaders_history.len();
  351. for i in 1..hist_len {
  352. if self.leaders_history[hist_len - i] == 0 {
  353. count = count + constants::FLOAT10_ONE.clone();
  354. } else {
  355. break
  356. }
  357. }
  358. count
  359. }
  360. /// the probability inverse of winnig lottery having all the stake
  361. /// returns f
  362. fn win_inv_prob_with_full_stake(&mut self) -> Float10 {
  363. let p = self.weighted_f_dif();
  364. let i = self.weighted_f_int();
  365. let d = self.weighted_f_der();
  366. info!("win_inv_prob_with_full_stake(): PID P: {:?}", p);
  367. info!("win_inv_prob_with_full_stake(): PID I: {:?}", i);
  368. info!("win_inv_prob_with_full_stake(): PID D: {:?}", d);
  369. let f = p + i.clone() + d;
  370. info!("win_inv_prob_with_full_stake(): PID f: {}", f);
  371. if f == constants::FLOAT10_ZERO.clone() {
  372. return constants::MIN_F.clone()
  373. } else if f >= constants::FLOAT10_ONE.clone() {
  374. return constants::MAX_F.clone()
  375. }
  376. let hist_len = self.leaders_history.len();
  377. if self.leaders_history[hist_len - 1] == 0 &&
  378. self.leaders_history[hist_len - 2] == 0 &&
  379. self.leaders_history[hist_len - 3] == 0 &&
  380. i.clone() == constants::FLOAT10_ZERO.clone()
  381. {
  382. return f * constants::DEG_RATE.clone().powf(self.zero_leads_len())
  383. }
  384. f
  385. }
  386. /// Check that the participant/stakeholder coins win the slot lottery.
  387. /// If the stakeholder has multiple competing winning coins, only the highest value
  388. /// coin is selected, since the stakeholder can't give more than one proof per block/slot.
  389. /// * 'sigma1', 'sigma2': slot sigmas
  390. /// Returns: (check: bool, idx: usize) where idx is the winning coin's index
  391. pub fn is_slot_leader(&mut self, sigma1: pallas::Base, sigma2: pallas::Base) -> (bool, usize) {
  392. let competing_coins = &self.coins.clone();
  393. let mut won = false;
  394. let mut highest_stake = 0;
  395. let mut highest_stake_idx = 0;
  396. let total_stake = self.total_stake();
  397. for (winning_idx, coin) in competing_coins.iter().enumerate() {
  398. info!("is_slot_leader: coin stake: {:?}", coin.value);
  399. info!("is_slot_leader: total stake: {}", total_stake);
  400. info!("is_slot_leader: relative stake: {}", (coin.value as f64) / total_stake as f64);
  401. let first_winning = coin.is_leader(sigma1, sigma2);
  402. if first_winning && !won {
  403. highest_stake_idx = winning_idx;
  404. }
  405. won |= first_winning;
  406. if won && coin.value > highest_stake {
  407. highest_stake = coin.value;
  408. highest_stake_idx = winning_idx;
  409. }
  410. }
  411. (won, highest_stake_idx)
  412. }
  413. /// Finds the longest blockchain the node holds and
  414. /// returns the last block hash and the chain index.
  415. pub fn longest_chain_last_hash(&self) -> Result<(blake3::Hash, i64)> {
  416. let mut longest: Option<Fork> = None;
  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. longest = Some(chain.clone());
  423. length = chain.sequence.len();
  424. index = i as i64;
  425. }
  426. }
  427. }
  428. let hash = match longest {
  429. Some(chain) => chain.sequence.last().unwrap().proposal.hash,
  430. None => self.blockchain.last()?.1,
  431. };
  432. Ok((hash, index))
  433. }
  434. /// Finds the length of longest fork chain the node holds.
  435. pub fn longest_chain_length(&self) -> usize {
  436. let mut max = 0;
  437. for fork in &self.forks {
  438. if fork.sequence.len() > max {
  439. max = fork.sequence.len();
  440. }
  441. }
  442. max
  443. }
  444. /// Given a proposal, find the index of the fork chain it extends.
  445. pub fn find_extended_chain_index(&mut self, proposal: &BlockProposal) -> Result<i64> {
  446. // We iterate through all forks to find which fork to extend
  447. let mut chain_index = -1;
  448. let mut state_checkpoint_index = 0;
  449. for (c_index, chain) in self.forks.iter().enumerate() {
  450. // Traverse sequence in reverse
  451. for (sc_index, state_checkpoint) in chain.sequence.iter().enumerate().rev() {
  452. if proposal.block.header.previous == state_checkpoint.proposal.hash {
  453. chain_index = c_index as i64;
  454. state_checkpoint_index = sc_index;
  455. break
  456. }
  457. }
  458. if chain_index != -1 {
  459. break
  460. }
  461. }
  462. // If no fork was found, we check with canonical
  463. if chain_index == -1 {
  464. let (last_slot, last_block) = self.blockchain.last()?;
  465. if proposal.block.header.previous != last_block ||
  466. proposal.block.header.slot <= last_slot
  467. {
  468. info!("find_extended_chain_index(): Proposal doesn't extend any known chain");
  469. return Ok(-2)
  470. }
  471. // Proposal extends canonical chain
  472. return Ok(-1)
  473. }
  474. // Found fork chain
  475. let chain = &self.forks[chain_index as usize];
  476. // Proposal extends fork at last proposal
  477. if state_checkpoint_index == (chain.sequence.len() - 1) {
  478. return Ok(chain_index)
  479. }
  480. info!("find_extended_chain_index(): Proposal to fork a forkchain was received.");
  481. let mut chain = self.forks[chain_index as usize].clone();
  482. // We keep all proposals until the one it extends
  483. chain.sequence.drain((state_checkpoint_index + 1)..);
  484. self.forks.push(chain);
  485. Ok(self.forks.len() as i64 - 1)
  486. }
  487. /// Search the chains we're holding for the given proposal.
  488. pub fn proposal_exists(&self, input_proposal: &blake3::Hash) -> bool {
  489. for chain in self.forks.iter() {
  490. for state_checkpoint in chain.sequence.iter().rev() {
  491. if input_proposal == &state_checkpoint.proposal.hash {
  492. return true
  493. }
  494. }
  495. }
  496. false
  497. }
  498. /// Auxillary function to set nodes leaders count history to the largest fork sequence
  499. /// of leaders, by using provided index.
  500. pub fn set_leader_history(&mut self, index: i64) {
  501. // Check if we found longest fork to extract sequence from
  502. match index {
  503. -1 => {
  504. info!("set_leader_history(): No fork exists.");
  505. }
  506. _ => {
  507. info!("set_leader_history(): Checking last proposal of fork: {}", index);
  508. let last_proposal = &self.forks[index as usize].sequence.last().unwrap().proposal;
  509. if last_proposal.block.header.slot == self.current_slot() {
  510. // Replacing our last history element with the leaders one
  511. self.leaders_history.pop();
  512. self.leaders_history.push(last_proposal.block.lead_info.leaders);
  513. info!("set_leader_history(): New leaders history: {:?}", self.leaders_history);
  514. return
  515. }
  516. }
  517. }
  518. self.leaders_history.push(0);
  519. }
  520. /// Utility function to extract leader selection lottery randomness(eta),
  521. /// defined as the hash of the previous lead proof converted to pallas base.
  522. fn get_eta(&self) -> pallas::Base {
  523. let proof_tx_hash = self.blockchain.get_last_proof_hash().unwrap();
  524. let mut bytes: [u8; 32] = *proof_tx_hash.as_bytes();
  525. // read first 254 bits
  526. bytes[30] = 0;
  527. bytes[31] = 0;
  528. pallas::Base::from_repr(bytes).unwrap()
  529. }
  530. /// Auxillary function to retrieve slot checkpoint of provided slot UID.
  531. pub fn get_slot_checkpoint(&self, slot: u64) -> Result<SlotCheckpoint> {
  532. // Check hot/live slot checkpoints
  533. for slot_checkpoint in self.slot_checkpoints.iter().rev() {
  534. if slot_checkpoint.slot == slot {
  535. return Ok(slot_checkpoint.clone())
  536. }
  537. }
  538. // Check if slot is finalized
  539. if let Ok(slot_checkpoints) = self.blockchain.get_slot_checkpoints_by_slot(&[slot]) {
  540. if slot_checkpoints.len() > 0 {
  541. if let Some(slot_checkpoint) = &slot_checkpoints[0] {
  542. return Ok(slot_checkpoint.clone())
  543. }
  544. }
  545. }
  546. Err(Error::SlotCheckpointNotFound(slot))
  547. }
  548. /// Auxillary function to update all fork state checkpoints to nodes coins current canonical states.
  549. /// Note: This function should only be invoked once on nodes' coins creation.
  550. pub fn update_forks_checkpoints(&mut self) {
  551. for fork in &mut self.forks {
  552. for state_checkpoint in &mut fork.sequence {
  553. state_checkpoint.coins = self.coins.clone();
  554. state_checkpoint.coins_tree = self.coins_tree.clone();
  555. }
  556. }
  557. }
  558. }
  559. /// Auxiliary structure used for consensus syncing.
  560. #[derive(Debug, SerialEncodable, SerialDecodable)]
  561. pub struct ConsensusRequest {}
  562. impl net::Message for ConsensusRequest {
  563. fn name() -> &'static str {
  564. "consensusrequest"
  565. }
  566. }
  567. /// Auxiliary structure used for consensus syncing.
  568. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  569. pub struct ConsensusResponse {
  570. /// Slots offset since genesis,
  571. pub offset: Option<u64>,
  572. /// Hot/live data used by the consensus algorithm
  573. pub forks: Vec<ForkInfo>,
  574. /// Pending transactions
  575. pub unconfirmed_txs: Vec<Transaction>,
  576. /// Hot/live slot checkpoints
  577. pub slot_checkpoints: Vec<SlotCheckpoint>,
  578. /// Leaders count history
  579. pub leaders_history: Vec<u64>,
  580. /// Seen nullifiers from proposals
  581. pub nullifiers: Vec<pallas::Base>,
  582. }
  583. impl net::Message for ConsensusResponse {
  584. fn name() -> &'static str {
  585. "consensusresponse"
  586. }
  587. }
  588. /// Auxiliary structure used to keep track of slot validation parameters.
  589. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  590. pub struct SlotCheckpoint {
  591. /// Slot UID
  592. pub slot: u64,
  593. /// Slot eta
  594. pub eta: pallas::Base,
  595. /// Slot sigma1
  596. pub sigma1: pallas::Base,
  597. /// Slot sigma2
  598. pub sigma2: pallas::Base,
  599. }
  600. impl SlotCheckpoint {
  601. pub fn new(slot: u64, eta: pallas::Base, sigma1: pallas::Base, sigma2: pallas::Base) -> Self {
  602. Self { slot, eta, sigma1, sigma2 }
  603. }
  604. /// Generate the genesis slot checkpoint.
  605. pub fn genesis_slot_checkpoint() -> Self {
  606. let eta = pallas::Base::zero();
  607. let sigma1 = pallas::Base::zero();
  608. let sigma2 = pallas::Base::zero();
  609. Self::new(0, eta, sigma1, sigma2)
  610. }
  611. }
  612. impl net::Message for SlotCheckpoint {
  613. fn name() -> &'static str {
  614. "slotcheckpoint"
  615. }
  616. }
  617. /// Auxiliary structure used for slot checkpoints syncing
  618. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  619. pub struct SlotCheckpointRequest {
  620. /// Slot UID
  621. pub slot: u64,
  622. }
  623. impl net::Message for SlotCheckpointRequest {
  624. fn name() -> &'static str {
  625. "slotcheckpointrequest"
  626. }
  627. }
  628. /// Auxiliary structure used for slot checkpoints syncing
  629. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  630. pub struct SlotCheckpointResponse {
  631. /// Response blocks.
  632. pub slot_checkpoints: Vec<SlotCheckpoint>,
  633. }
  634. impl net::Message for SlotCheckpointResponse {
  635. fn name() -> &'static str {
  636. "slotcheckpointresponse"
  637. }
  638. }
  639. /// Auxiliary structure used to keep track of consensus state checkpoints.
  640. #[derive(Debug, Clone)]
  641. pub struct StateCheckpoint {
  642. /// Block proposal
  643. pub proposal: BlockProposal,
  644. /// Node competing coins current state
  645. pub coins: Vec<LeadCoin>,
  646. /// Coin commitments tree current state
  647. pub coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  648. /// Seen nullifiers from proposals current state
  649. pub nullifiers: Vec<pallas::Base>,
  650. }
  651. impl StateCheckpoint {
  652. pub fn new(
  653. proposal: BlockProposal,
  654. coins: Vec<LeadCoin>,
  655. coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  656. nullifiers: Vec<pallas::Base>,
  657. ) -> Self {
  658. Self { proposal, coins, coins_tree, nullifiers }
  659. }
  660. }
  661. /// Auxiliary structure used for forked consensus state checkpoints syncing
  662. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  663. pub struct StateCheckpointInfo {
  664. /// Block proposal
  665. pub proposal: BlockProposal,
  666. /// Seen nullifiers from proposals current state
  667. pub nullifiers: Vec<pallas::Base>,
  668. }
  669. impl From<StateCheckpoint> for StateCheckpointInfo {
  670. fn from(state_checkpoint: StateCheckpoint) -> Self {
  671. Self { proposal: state_checkpoint.proposal, nullifiers: state_checkpoint.nullifiers }
  672. }
  673. }
  674. impl From<StateCheckpointInfo> for StateCheckpoint {
  675. fn from(state_checkpoint_info: StateCheckpointInfo) -> Self {
  676. Self {
  677. proposal: state_checkpoint_info.proposal,
  678. coins: vec![],
  679. coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
  680. nullifiers: state_checkpoint_info.nullifiers,
  681. }
  682. }
  683. }
  684. /// This struct represents a sequence of consensus state checkpoints.
  685. #[derive(Debug, Clone)]
  686. pub struct Fork {
  687. pub genesis_block: blake3::Hash,
  688. pub sequence: Vec<StateCheckpoint>,
  689. }
  690. impl Fork {
  691. pub fn new(genesis_block: blake3::Hash, initial_state_checkpoint: StateCheckpoint) -> Self {
  692. Self { genesis_block, sequence: vec![initial_state_checkpoint] }
  693. }
  694. /// Insertion of a valid state checkpoint.
  695. pub fn add(&mut self, state_checkpoint: &StateCheckpoint) {
  696. if self.check_state_checkpoint(state_checkpoint, self.sequence.last().unwrap()) {
  697. self.sequence.push(state_checkpoint.clone());
  698. }
  699. }
  700. /// A fork chain is considered valid when every state checkpoint is valid,
  701. /// based on the `check_state_checkpoint` function
  702. pub fn check_chain(&self) -> bool {
  703. for (index, state_checkpoint) in self.sequence[1..].iter().enumerate() {
  704. if !self.check_state_checkpoint(state_checkpoint, &self.sequence[index]) {
  705. return false
  706. }
  707. }
  708. true
  709. }
  710. /// A state checkpoint is considered valid when its proposal parent hash is equal to the
  711. /// hash of the previous checkpoint's proposal and their slots are incremental,
  712. /// excluding the genesis block proposal.
  713. pub fn check_state_checkpoint(
  714. &self,
  715. state_checkpoint: &StateCheckpoint,
  716. previous: &StateCheckpoint,
  717. ) -> bool {
  718. if state_checkpoint.proposal.block.header.previous == self.genesis_block {
  719. info!("check_checkpoint(): Genesis block proposal provided.");
  720. return false
  721. }
  722. if state_checkpoint.proposal.block.header.previous != previous.proposal.hash ||
  723. state_checkpoint.proposal.block.header.slot <= previous.proposal.block.header.slot
  724. {
  725. info!("check_checkpoint(): Provided state checkpoint proposal is invalid.");
  726. return false
  727. }
  728. // TODO: validate rest checkpoint info(like nullifiers)
  729. true
  730. }
  731. }
  732. /// Auxiliary structure used for forks syncing
  733. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  734. pub struct ForkInfo {
  735. pub genesis_block: blake3::Hash,
  736. pub sequence: Vec<StateCheckpointInfo>,
  737. }
  738. impl From<Fork> for ForkInfo {
  739. fn from(fork: Fork) -> Self {
  740. let mut sequence = vec![];
  741. for state_checkpoint in fork.sequence {
  742. sequence.push(state_checkpoint.into());
  743. }
  744. Self { genesis_block: fork.genesis_block, sequence }
  745. }
  746. }
  747. impl From<ForkInfo> for Fork {
  748. fn from(fork_info: ForkInfo) -> Self {
  749. let mut sequence = vec![];
  750. for checkpoint in fork_info.sequence {
  751. sequence.push(checkpoint.into());
  752. }
  753. Self { genesis_block: fork_info.genesis_block, sequence }
  754. }
  755. }