state.rs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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 max_windowed_forks(&self) -> Float10 {
  315. let mut max : u64= 5;
  316. let window_size = 10;
  317. let len = self.leaders_history.len();
  318. let window_begining = if len <= (window_size+1) {
  319. 0
  320. } else {
  321. len - (window_size +1)
  322. };
  323. for item in &self.leaders_history[window_begining..] {
  324. if *item>max {
  325. max = *item;
  326. }
  327. }
  328. Float10::try_from(max as i64).unwrap().with_precision(constants::RADIX_BITS).value()
  329. }
  330. fn tuned_kp(&self) -> Float10 {
  331. (constants::KP.clone() * constants::FLOAT10_FIVE.clone())/self.max_windowed_forks()
  332. }
  333. fn weighted_f_dif(&mut self) -> Float10 {
  334. self.tuned_kp() * self.f_dif()
  335. }
  336. fn f_der(&self) -> Float10 {
  337. let len = self.leaders_history.len();
  338. let last = Float10::try_from(self.leaders_history[len - 1] as i64)
  339. .unwrap()
  340. .with_precision(constants::RADIX_BITS)
  341. .value();
  342. let second_to_last = Float10::try_from(self.leaders_history[len - 2] as i64)
  343. .unwrap()
  344. .with_precision(constants::RADIX_BITS)
  345. .value();
  346. let mut der =
  347. (Self::pid_error(second_to_last) - Self::pid_error(last)) / constants::DT.clone();
  348. der = if der > constants::MAX_DER.clone() { constants::MAX_DER.clone() } else { der };
  349. der = if der < constants::MIN_DER.clone() { constants::MIN_DER.clone() } else { der };
  350. der
  351. }
  352. fn weighted_f_der(&self) -> Float10 {
  353. constants::KD.clone() * self.f_der()
  354. }
  355. fn f_int(&self) -> Float10 {
  356. let mut sum = constants::FLOAT10_ZERO.clone();
  357. let lead_history_len = self.leaders_history.len();
  358. let history_begin_index = if lead_history_len > 10 { lead_history_len - 10 } else { 0 };
  359. for lf in &self.leaders_history[history_begin_index..] {
  360. sum += Float10::try_from(lf.clone()).unwrap().abs();
  361. }
  362. sum
  363. }
  364. fn tuned_ki(&self) -> Float10 {
  365. (constants::KI.clone() * constants::FLOAT10_FIVE.clone())/self.max_windowed_forks()
  366. }
  367. fn weighted_f_int(&self) -> Float10 {
  368. constants::KI.clone() * self.f_int()
  369. }
  370. fn zero_leads_len(&self) -> Float10 {
  371. let mut count = constants::FLOAT10_ZERO.clone();
  372. let hist_len = self.leaders_history.len();
  373. for i in 1..hist_len {
  374. if self.leaders_history[hist_len - i] == 0 {
  375. count = count + constants::FLOAT10_ONE.clone();
  376. } else {
  377. break
  378. }
  379. }
  380. count
  381. }
  382. /// the probability inverse of winnig lottery having all the stake
  383. /// returns f
  384. fn win_inv_prob_with_full_stake(&mut self) -> Float10 {
  385. let p = self.weighted_f_dif();
  386. let i = self.weighted_f_int();
  387. let d = self.weighted_f_der();
  388. info!("win_inv_prob_with_full_stake(): PID P: {:?}", p);
  389. info!("win_inv_prob_with_full_stake(): PID I: {:?}", i);
  390. info!("win_inv_prob_with_full_stake(): PID D: {:?}", d);
  391. let f = p + i.clone() + d;
  392. info!("win_inv_prob_with_full_stake(): PID f: {}", f);
  393. if f == constants::FLOAT10_ZERO.clone() {
  394. return constants::MIN_F.clone()
  395. } else if f >= constants::FLOAT10_ONE.clone() {
  396. return constants::MAX_F.clone()
  397. }
  398. let hist_len = self.leaders_history.len();
  399. if self.leaders_history[hist_len - 1] == 0 &&
  400. self.leaders_history[hist_len - 2] == 0 &&
  401. self.leaders_history[hist_len - 3] == 0 &&
  402. i.clone() == constants::FLOAT10_ZERO.clone()
  403. {
  404. return f * constants::DEG_RATE.clone().powf(self.zero_leads_len())
  405. }
  406. f
  407. }
  408. /// Check that the participant/stakeholder coins win the slot lottery.
  409. /// If the stakeholder has multiple competing winning coins, only the highest value
  410. /// coin is selected, since the stakeholder can't give more than one proof per block/slot.
  411. /// * 'sigma1', 'sigma2': slot sigmas
  412. /// Returns: (check: bool, idx: usize) where idx is the winning coin's index
  413. pub fn is_slot_leader(&mut self, sigma1: pallas::Base, sigma2: pallas::Base) -> (bool, usize) {
  414. let competing_coins = &self.coins.clone();
  415. let mut won = false;
  416. let mut highest_stake = 0;
  417. let mut highest_stake_idx = 0;
  418. let total_stake = self.total_stake();
  419. for (winning_idx, coin) in competing_coins.iter().enumerate() {
  420. info!("is_slot_leader: coin stake: {:?}", coin.value);
  421. info!("is_slot_leader: total stake: {}", total_stake);
  422. info!("is_slot_leader: relative stake: {}", (coin.value as f64) / total_stake as f64);
  423. let first_winning = coin.is_leader(sigma1, sigma2);
  424. if first_winning && !won {
  425. highest_stake_idx = winning_idx;
  426. }
  427. won |= first_winning;
  428. if won && coin.value > highest_stake {
  429. highest_stake = coin.value;
  430. highest_stake_idx = winning_idx;
  431. }
  432. }
  433. (won, highest_stake_idx)
  434. }
  435. /// Finds the longest blockchain the node holds and
  436. /// returns the last block hash and the chain index.
  437. pub fn longest_chain_last_hash(&self) -> Result<(blake3::Hash, i64)> {
  438. let mut longest: Option<Fork> = None;
  439. let mut length = 0;
  440. let mut index = -1;
  441. if !self.forks.is_empty() {
  442. for (i, chain) in self.forks.iter().enumerate() {
  443. if chain.sequence.len() > length {
  444. longest = Some(chain.clone());
  445. length = chain.sequence.len();
  446. index = i as i64;
  447. }
  448. }
  449. }
  450. let hash = match longest {
  451. Some(chain) => chain.sequence.last().unwrap().proposal.hash,
  452. None => self.blockchain.last()?.1,
  453. };
  454. Ok((hash, index))
  455. }
  456. /// Finds the length of longest fork chain the node holds.
  457. pub fn longest_chain_length(&self) -> usize {
  458. let mut max = 0;
  459. for fork in &self.forks {
  460. if fork.sequence.len() > max {
  461. max = fork.sequence.len();
  462. }
  463. }
  464. max
  465. }
  466. /// Given a proposal, find the index of the fork chain it extends.
  467. pub fn find_extended_chain_index(&mut self, proposal: &BlockProposal) -> Result<i64> {
  468. // We iterate through all forks to find which fork to extend
  469. let mut chain_index = -1;
  470. let mut state_checkpoint_index = 0;
  471. for (c_index, chain) in self.forks.iter().enumerate() {
  472. // Traverse sequence in reverse
  473. for (sc_index, state_checkpoint) in chain.sequence.iter().enumerate().rev() {
  474. if proposal.block.header.previous == state_checkpoint.proposal.hash {
  475. chain_index = c_index as i64;
  476. state_checkpoint_index = sc_index;
  477. break
  478. }
  479. }
  480. if chain_index != -1 {
  481. break
  482. }
  483. }
  484. // If no fork was found, we check with canonical
  485. if chain_index == -1 {
  486. let (last_slot, last_block) = self.blockchain.last()?;
  487. if proposal.block.header.previous != last_block ||
  488. proposal.block.header.slot <= last_slot
  489. {
  490. info!("find_extended_chain_index(): Proposal doesn't extend any known chain");
  491. return Ok(-2)
  492. }
  493. // Proposal extends canonical chain
  494. return Ok(-1)
  495. }
  496. // Found fork chain
  497. let chain = &self.forks[chain_index as usize];
  498. // Proposal extends fork at last proposal
  499. if state_checkpoint_index == (chain.sequence.len() - 1) {
  500. return Ok(chain_index)
  501. }
  502. info!("find_extended_chain_index(): Proposal to fork a forkchain was received.");
  503. let mut chain = self.forks[chain_index as usize].clone();
  504. // We keep all proposals until the one it extends
  505. chain.sequence.drain((state_checkpoint_index + 1)..);
  506. self.forks.push(chain);
  507. Ok(self.forks.len() as i64 - 1)
  508. }
  509. /// Search the chains we're holding for the given proposal.
  510. pub fn proposal_exists(&self, input_proposal: &blake3::Hash) -> bool {
  511. for chain in self.forks.iter() {
  512. for state_checkpoint in chain.sequence.iter().rev() {
  513. if input_proposal == &state_checkpoint.proposal.hash {
  514. return true
  515. }
  516. }
  517. }
  518. false
  519. }
  520. /// Auxillary function to set nodes leaders count history to the largest fork sequence
  521. /// of leaders, by using provided index.
  522. pub fn set_leader_history(&mut self, index: i64) {
  523. // Check if we found longest fork to extract sequence from
  524. match index {
  525. -1 => {
  526. info!("set_leader_history(): No fork exists.");
  527. }
  528. _ => {
  529. info!("set_leader_history(): Checking last proposal of fork: {}", index);
  530. let last_proposal = &self.forks[index as usize].sequence.last().unwrap().proposal;
  531. if last_proposal.block.header.slot == self.current_slot() {
  532. // Replacing our last history element with the leaders one
  533. self.leaders_history.pop();
  534. self.leaders_history.push(last_proposal.block.lead_info.leaders);
  535. info!("set_leader_history(): New leaders history: {:?}", self.leaders_history);
  536. return
  537. }
  538. }
  539. }
  540. self.leaders_history.push(0);
  541. }
  542. /// Utility function to extract leader selection lottery randomness(eta),
  543. /// defined as the hash of the previous lead proof converted to pallas base.
  544. fn get_eta(&self) -> pallas::Base {
  545. let proof_tx_hash = self.blockchain.get_last_proof_hash().unwrap();
  546. let mut bytes: [u8; 32] = *proof_tx_hash.as_bytes();
  547. // read first 254 bits
  548. bytes[30] = 0;
  549. bytes[31] = 0;
  550. pallas::Base::from_repr(bytes).unwrap()
  551. }
  552. /// Auxillary function to retrieve slot checkpoint of provided slot UID.
  553. pub fn get_slot_checkpoint(&self, slot: u64) -> Result<SlotCheckpoint> {
  554. // Check hot/live slot checkpoints
  555. for slot_checkpoint in self.slot_checkpoints.iter().rev() {
  556. if slot_checkpoint.slot == slot {
  557. return Ok(slot_checkpoint.clone())
  558. }
  559. }
  560. // Check if slot is finalized
  561. if let Ok(slot_checkpoints) = self.blockchain.get_slot_checkpoints_by_slot(&[slot]) {
  562. if slot_checkpoints.len() > 0 {
  563. if let Some(slot_checkpoint) = &slot_checkpoints[0] {
  564. return Ok(slot_checkpoint.clone())
  565. }
  566. }
  567. }
  568. Err(Error::SlotCheckpointNotFound(slot))
  569. }
  570. /// Auxillary function to update all fork state checkpoints to nodes coins current canonical states.
  571. /// Note: This function should only be invoked once on nodes' coins creation.
  572. pub fn update_forks_checkpoints(&mut self) {
  573. for fork in &mut self.forks {
  574. for state_checkpoint in &mut fork.sequence {
  575. state_checkpoint.coins = self.coins.clone();
  576. state_checkpoint.coins_tree = self.coins_tree.clone();
  577. }
  578. }
  579. }
  580. }
  581. /// Auxiliary structure used for consensus syncing.
  582. #[derive(Debug, SerialEncodable, SerialDecodable)]
  583. pub struct ConsensusRequest {}
  584. impl net::Message for ConsensusRequest {
  585. fn name() -> &'static str {
  586. "consensusrequest"
  587. }
  588. }
  589. /// Auxiliary structure used for consensus syncing.
  590. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  591. pub struct ConsensusResponse {
  592. /// Slots offset since genesis,
  593. pub offset: Option<u64>,
  594. /// Hot/live data used by the consensus algorithm
  595. pub forks: Vec<ForkInfo>,
  596. /// Pending transactions
  597. pub unconfirmed_txs: Vec<Transaction>,
  598. /// Hot/live slot checkpoints
  599. pub slot_checkpoints: Vec<SlotCheckpoint>,
  600. /// Leaders count history
  601. pub leaders_history: Vec<u64>,
  602. /// Seen nullifiers from proposals
  603. pub nullifiers: Vec<pallas::Base>,
  604. }
  605. impl net::Message for ConsensusResponse {
  606. fn name() -> &'static str {
  607. "consensusresponse"
  608. }
  609. }
  610. /// Auxiliary structure used to keep track of slot validation parameters.
  611. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  612. pub struct SlotCheckpoint {
  613. /// Slot UID
  614. pub slot: u64,
  615. /// Slot eta
  616. pub eta: pallas::Base,
  617. /// Slot sigma1
  618. pub sigma1: pallas::Base,
  619. /// Slot sigma2
  620. pub sigma2: pallas::Base,
  621. }
  622. impl SlotCheckpoint {
  623. pub fn new(slot: u64, eta: pallas::Base, sigma1: pallas::Base, sigma2: pallas::Base) -> Self {
  624. Self { slot, eta, sigma1, sigma2 }
  625. }
  626. /// Generate the genesis slot checkpoint.
  627. pub fn genesis_slot_checkpoint() -> Self {
  628. let eta = pallas::Base::zero();
  629. let sigma1 = pallas::Base::zero();
  630. let sigma2 = pallas::Base::zero();
  631. Self::new(0, eta, sigma1, sigma2)
  632. }
  633. }
  634. impl net::Message for SlotCheckpoint {
  635. fn name() -> &'static str {
  636. "slotcheckpoint"
  637. }
  638. }
  639. /// Auxiliary structure used for slot checkpoints syncing
  640. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  641. pub struct SlotCheckpointRequest {
  642. /// Slot UID
  643. pub slot: u64,
  644. }
  645. impl net::Message for SlotCheckpointRequest {
  646. fn name() -> &'static str {
  647. "slotcheckpointrequest"
  648. }
  649. }
  650. /// Auxiliary structure used for slot checkpoints syncing
  651. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  652. pub struct SlotCheckpointResponse {
  653. /// Response blocks.
  654. pub slot_checkpoints: Vec<SlotCheckpoint>,
  655. }
  656. impl net::Message for SlotCheckpointResponse {
  657. fn name() -> &'static str {
  658. "slotcheckpointresponse"
  659. }
  660. }
  661. /// Auxiliary structure used to keep track of consensus state checkpoints.
  662. #[derive(Debug, Clone)]
  663. pub struct StateCheckpoint {
  664. /// Block proposal
  665. pub proposal: BlockProposal,
  666. /// Node competing coins current state
  667. pub coins: Vec<LeadCoin>,
  668. /// Coin commitments tree current state
  669. pub coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  670. /// Seen nullifiers from proposals current state
  671. pub nullifiers: Vec<pallas::Base>,
  672. }
  673. impl StateCheckpoint {
  674. pub fn new(
  675. proposal: BlockProposal,
  676. coins: Vec<LeadCoin>,
  677. coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  678. nullifiers: Vec<pallas::Base>,
  679. ) -> Self {
  680. Self { proposal, coins, coins_tree, nullifiers }
  681. }
  682. }
  683. /// Auxiliary structure used for forked consensus state checkpoints syncing
  684. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  685. pub struct StateCheckpointInfo {
  686. /// Block proposal
  687. pub proposal: BlockProposal,
  688. /// Seen nullifiers from proposals current state
  689. pub nullifiers: Vec<pallas::Base>,
  690. }
  691. impl From<StateCheckpoint> for StateCheckpointInfo {
  692. fn from(state_checkpoint: StateCheckpoint) -> Self {
  693. Self { proposal: state_checkpoint.proposal, nullifiers: state_checkpoint.nullifiers }
  694. }
  695. }
  696. impl From<StateCheckpointInfo> for StateCheckpoint {
  697. fn from(state_checkpoint_info: StateCheckpointInfo) -> Self {
  698. Self {
  699. proposal: state_checkpoint_info.proposal,
  700. coins: vec![],
  701. coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
  702. nullifiers: state_checkpoint_info.nullifiers,
  703. }
  704. }
  705. }
  706. /// This struct represents a sequence of consensus state checkpoints.
  707. #[derive(Debug, Clone)]
  708. pub struct Fork {
  709. pub genesis_block: blake3::Hash,
  710. pub sequence: Vec<StateCheckpoint>,
  711. }
  712. impl Fork {
  713. pub fn new(genesis_block: blake3::Hash, initial_state_checkpoint: StateCheckpoint) -> Self {
  714. Self { genesis_block, sequence: vec![initial_state_checkpoint] }
  715. }
  716. /// Insertion of a valid state checkpoint.
  717. pub fn add(&mut self, state_checkpoint: &StateCheckpoint) {
  718. if self.check_state_checkpoint(state_checkpoint, self.sequence.last().unwrap()) {
  719. self.sequence.push(state_checkpoint.clone());
  720. }
  721. }
  722. /// A fork chain is considered valid when every state checkpoint is valid,
  723. /// based on the `check_state_checkpoint` function
  724. pub fn check_chain(&self) -> bool {
  725. for (index, state_checkpoint) in self.sequence[1..].iter().enumerate() {
  726. if !self.check_state_checkpoint(state_checkpoint, &self.sequence[index]) {
  727. return false
  728. }
  729. }
  730. true
  731. }
  732. /// A state checkpoint is considered valid when its proposal parent hash is equal to the
  733. /// hash of the previous checkpoint's proposal and their slots are incremental,
  734. /// excluding the genesis block proposal.
  735. pub fn check_state_checkpoint(
  736. &self,
  737. state_checkpoint: &StateCheckpoint,
  738. previous: &StateCheckpoint,
  739. ) -> bool {
  740. if state_checkpoint.proposal.block.header.previous == self.genesis_block {
  741. info!("check_checkpoint(): Genesis block proposal provided.");
  742. return false
  743. }
  744. if state_checkpoint.proposal.block.header.previous != previous.proposal.hash ||
  745. state_checkpoint.proposal.block.header.slot <= previous.proposal.block.header.slot
  746. {
  747. info!("check_checkpoint(): Provided state checkpoint proposal is invalid.");
  748. return false
  749. }
  750. // TODO: validate rest checkpoint info(like nullifiers)
  751. true
  752. }
  753. }
  754. /// Auxiliary structure used for forks syncing
  755. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  756. pub struct ForkInfo {
  757. pub genesis_block: blake3::Hash,
  758. pub sequence: Vec<StateCheckpointInfo>,
  759. }
  760. impl From<Fork> for ForkInfo {
  761. fn from(fork: Fork) -> Self {
  762. let mut sequence = vec![];
  763. for state_checkpoint in fork.sequence {
  764. sequence.push(state_checkpoint.into());
  765. }
  766. Self { genesis_block: fork.genesis_block, sequence }
  767. }
  768. }
  769. impl From<ForkInfo> for Fork {
  770. fn from(fork_info: ForkInfo) -> Self {
  771. let mut sequence = vec![];
  772. for checkpoint in fork_info.sequence {
  773. sequence.push(checkpoint.into());
  774. }
  775. Self { genesis_block: fork_info.genesis_block, sequence }
  776. }
  777. }