state.rs 33 KB

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