state.rs 31 KB

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