state.rs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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::{collections::HashMap, io::Cursor, time::Duration};
  19. use async_std::sync::{Arc, RwLock};
  20. use chrono::{NaiveDateTime, Utc};
  21. use darkfi_sdk::{
  22. crypto::{
  23. constants::MERKLE_DEPTH,
  24. schnorr::{SchnorrPublic, SchnorrSecret},
  25. ContractId, MerkleNode, PublicKey,
  26. },
  27. db::ZKAS_DB_NAME,
  28. };
  29. use darkfi_serial::{
  30. deserialize, serialize, Decodable, Encodable, SerialDecodable, SerialEncodable, WriteExt,
  31. };
  32. use incrementalmerkletree::{bridgetree::BridgeTree, Tree};
  33. use log::{debug, error, info, warn};
  34. use pasta_curves::{group::ff::PrimeField, pallas};
  35. use rand::{rngs::OsRng, thread_rng, Rng};
  36. use serde_json::json;
  37. use super::{
  38. constants,
  39. leadcoin::{LeadCoin, LeadCoinSecrets},
  40. utils::fbig2base,
  41. Block, BlockInfo, BlockProposal, Float10, Header, LeadInfo, LeadProof, ProposalChain,
  42. };
  43. use crate::{
  44. blockchain::Blockchain,
  45. crypto::proof::{ProvingKey, VerifyingKey},
  46. net,
  47. rpc::jsonrpc::JsonNotification,
  48. runtime::vm_runtime::Runtime,
  49. system::{Subscriber, SubscriberPtr},
  50. tx::Transaction,
  51. util::time::Timestamp,
  52. wallet::WalletPtr,
  53. zk::{vm::ZkCircuit, vm_stack::empty_witnesses},
  54. zkas::ZkBinary,
  55. Error, Result,
  56. };
  57. /// This struct represents the information required by the consensus algorithm
  58. #[derive(Debug)]
  59. pub struct ConsensusState {
  60. /// Genesis block creation timestamp
  61. pub genesis_ts: Timestamp,
  62. /// Genesis block hash
  63. pub genesis_block: blake3::Hash,
  64. /// Participating start slot
  65. pub participating: Option<u64>,
  66. /// Last slot node check for finalization
  67. pub checked_finalization: u64,
  68. /// Slots offset since genesis,
  69. pub offset: Option<u64>,
  70. /// Fork chains containing block proposals
  71. pub proposals: Vec<ProposalChain>,
  72. /// Current epoch
  73. pub epoch: u64,
  74. /// Current epoch eta
  75. pub epoch_eta: pallas::Base,
  76. /// Current epoch competing coins
  77. pub coins: Vec<Vec<LeadCoin>>,
  78. // TODO: Aren't these already in db after finalization?
  79. /// Seen nullifiers from proposals
  80. pub leaders_nullifiers: Vec<pallas::Base>,
  81. /// Seen spent coins from proposals
  82. pub leaders_spent_coins: Vec<(pallas::Base, pallas::Base)>,
  83. /// Leaders count history
  84. pub leaders_history: Vec<u64>,
  85. /// Kp
  86. pub kp: Float10,
  87. /// Previous slot sigma1
  88. pub prev_sigma1: pallas::Base,
  89. /// Previous slot sigma2
  90. pub prev_sigma2: pallas::Base,
  91. }
  92. impl ConsensusState {
  93. pub fn new(genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
  94. let genesis_block = Block::genesis_block(genesis_ts, genesis_data).blockhash();
  95. Ok(Self {
  96. genesis_ts,
  97. genesis_block,
  98. participating: None,
  99. checked_finalization: 0,
  100. offset: None,
  101. proposals: vec![],
  102. epoch: 0,
  103. epoch_eta: pallas::Base::one(),
  104. coins: vec![],
  105. leaders_nullifiers: vec![],
  106. leaders_spent_coins: vec![],
  107. leaders_history: vec![0],
  108. kp: constants::FLOAT10_TWO.clone() / constants::FLOAT10_NINE.clone(),
  109. prev_sigma1: pallas::Base::zero(),
  110. prev_sigma2: pallas::Base::zero(),
  111. })
  112. }
  113. }
  114. /// Auxiliary structure used for consensus syncing.
  115. #[derive(Debug, SerialEncodable, SerialDecodable)]
  116. pub struct ConsensusRequest {}
  117. impl net::Message for ConsensusRequest {
  118. fn name() -> &'static str {
  119. "consensusrequest"
  120. }
  121. }
  122. /// Auxiliary structure used for consensus syncing.
  123. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  124. pub struct ConsensusResponse {
  125. /// Slots offset since genesis,
  126. pub offset: Option<u64>,
  127. /// Hot/live data used by the consensus algorithm
  128. pub proposals: Vec<ProposalChain>,
  129. /// Pending transactions
  130. pub unconfirmed_txs: Vec<Transaction>,
  131. /// Seen nullifiers from proposals
  132. pub leaders_nullifiers: Vec<pallas::Base>,
  133. /// Seen spent coins from proposals
  134. pub leaders_spent_coins: Vec<(pallas::Base, pallas::Base)>,
  135. }
  136. impl net::Message for ConsensusResponse {
  137. fn name() -> &'static str {
  138. "consensusresponse"
  139. }
  140. }
  141. /// Atomic pointer to validator state.
  142. pub type ValidatorStatePtr = Arc<RwLock<ValidatorState>>;
  143. /// This struct represents the state of a validator node.
  144. pub struct ValidatorState {
  145. /// Leader proof proving key
  146. pub lead_proving_key: Option<ProvingKey>,
  147. /// Leader proof verifying key
  148. pub lead_verifying_key: VerifyingKey,
  149. /// Hot/Live data used by the consensus algorithm
  150. pub consensus: ConsensusState,
  151. /// Canonical (finalized) blockchain
  152. pub blockchain: Blockchain,
  153. /// Pending transactions
  154. pub unconfirmed_txs: Vec<Transaction>,
  155. /// A map of various subscribers exporting live info from the blockchain
  156. /// TODO: Instead of JsonNotification, it can be an enum of internal objects,
  157. /// and then we don't have to deal with json in this module but only
  158. // externally.
  159. pub subscribers: HashMap<&'static str, SubscriberPtr<JsonNotification>>,
  160. /// ZK proof verifying keys for smart contract calls
  161. pub verifying_keys: Arc<RwLock<HashMap<[u8; 32], Vec<(String, VerifyingKey)>>>>,
  162. /// Wallet interface
  163. pub wallet: WalletPtr,
  164. }
  165. impl ValidatorState {
  166. pub async fn new(
  167. db: &sled::Db, // <-- TODO: Avoid this with some wrapping, sled should only be in blockchain
  168. genesis_ts: Timestamp,
  169. genesis_data: blake3::Hash,
  170. wallet: WalletPtr,
  171. faucet_pubkeys: Vec<PublicKey>,
  172. enable_participation: bool,
  173. ) -> Result<ValidatorStatePtr> {
  174. info!("Initializing ValidatorState");
  175. info!("Initializing wallet tables for consensus");
  176. // TODO: TESTNET: The stuff is kept entirely in memory for now, what should we write
  177. // to disk/wallet?
  178. //let consensus_tree_init_query = include_str!("../../script/sql/consensus_tree.sql");
  179. //let consensus_keys_init_query = include_str!("../../script/sql/consensus_keys.sql");
  180. //wallet.exec_sql(consensus_tree_init_query).await?;
  181. //wallet.exec_sql(consensus_keys_init_query).await?;
  182. info!("Generating leader proof keys with k: {}", constants::LEADER_PROOF_K);
  183. let bincode = include_bytes!("../../proof/lead.zk.bin");
  184. let zkbin = ZkBinary::decode(bincode)?;
  185. let witnesses = empty_witnesses(&zkbin);
  186. let circuit = ZkCircuit::new(witnesses, zkbin);
  187. let lead_verifying_key = VerifyingKey::build(constants::LEADER_PROOF_K, &circuit);
  188. // We only need this proving key if we're going to participate in the consensus.
  189. let lead_proving_key = if enable_participation {
  190. Some(ProvingKey::build(constants::LEADER_PROOF_K, &circuit))
  191. } else {
  192. None
  193. };
  194. let consensus = ConsensusState::new(genesis_ts, genesis_data)?;
  195. let blockchain = Blockchain::new(db, genesis_ts, genesis_data)?;
  196. let unconfirmed_txs = vec![];
  197. // -----NATIVE WASM CONTRACTS-----
  198. // This is the current place where native contracts are being deployed.
  199. // When the `Blockchain` object is created, it doesn't care whether it
  200. // already has the contract data or not. If there's existing data, it
  201. // will just open the necessary db and trees, and give back what it has.
  202. // This means that on subsequent runs our native contracts will already
  203. // be in a deployed state, so what we actually do here is a redeployment.
  204. // This kind of operation should only modify the contract's state in case
  205. // it wasn't deployed before (meaning the initial run). Otherwise, it
  206. // shouldn't touch anything, or just potentially update the db schemas or
  207. // whatever is necessary. This logic should be handled in the init function
  208. // of the actual contract, so make sure the native contracts handle this well.
  209. // FIXME: This ID should be something that does not solve the pallas curve equation,
  210. // and/or just hardcoded and forbidden in non-native contract deployment.
  211. let money_contract_id = ContractId::from(pallas::Base::from(u64::MAX - 420));
  212. // The faucet pubkeys are pubkeys which are allowed to create clear inputs
  213. // in the money contract.
  214. let money_contract_deploy_payload = serialize(&faucet_pubkeys);
  215. // In this hashmap, we keep references to ZK proof verifying keys needed
  216. // for the circuits our native contracts provide.
  217. let mut verifying_keys = HashMap::new();
  218. let native_contracts = vec![(
  219. "Money Contract",
  220. money_contract_id,
  221. include_bytes!("../contract/money/money_contract.wasm"),
  222. money_contract_deploy_payload,
  223. )];
  224. info!("Deploying native wasm contracts");
  225. for nc in native_contracts {
  226. info!("Deploying {} with ContractID {}", nc.0, nc.1);
  227. let mut runtime = Runtime::new(&nc.2[..], blockchain.clone(), nc.1)?;
  228. runtime.deploy(&nc.3)?;
  229. info!("Successfully deployed {}", nc.0);
  230. // When deployed, we can do a lookup for the zkas circuits and
  231. // initialize verifying keys for them.
  232. info!("Creating ZK verifying keys for {} zkas circuits", nc.0);
  233. debug!("Looking up zkas db for {} (ContractID: {})", nc.0, nc.1);
  234. let zkas_db = blockchain.contracts.lookup(&blockchain.sled_db, &nc.1, ZKAS_DB_NAME)?;
  235. let mut vks = vec![];
  236. for i in zkas_db.iter() {
  237. debug!("Iterating over zkas db");
  238. let (zkas_ns, zkas_bincode) = i?;
  239. debug!("Deserializing namespace");
  240. let zkas_ns: String = deserialize(&zkas_ns)?;
  241. info!("Creating VerifyingKey for zkas circuit with namespace {}", zkas_ns);
  242. let zkbin = ZkBinary::decode(&zkas_bincode)?;
  243. let circuit = ZkCircuit::new(empty_witnesses(&zkbin), zkbin);
  244. // FIXME: This k=13 man...
  245. let vk = VerifyingKey::build(13, &circuit);
  246. vks.push((zkas_ns, vk));
  247. }
  248. info!("Finished creating VerifyingKey objects for {} (ContractID: {})", nc.0, nc.1);
  249. verifying_keys.insert(nc.1.to_bytes(), vks);
  250. }
  251. info!("Finished deployment of native wasm contracts");
  252. // -----NATIVE WASM CONTRACTS-----
  253. // Here we initialize various subscribers that can export live consensus/blockchain data.
  254. let mut subscribers = HashMap::new();
  255. let block_subscriber = Subscriber::new();
  256. subscribers.insert("blocks", block_subscriber);
  257. let state = Arc::new(RwLock::new(ValidatorState {
  258. lead_proving_key,
  259. lead_verifying_key,
  260. consensus,
  261. blockchain,
  262. unconfirmed_txs,
  263. subscribers,
  264. verifying_keys: Arc::new(RwLock::new(verifying_keys)),
  265. wallet,
  266. }));
  267. Ok(state)
  268. }
  269. /// The node retrieves a transaction, validates its state transition,
  270. /// and appends it to the unconfirmed transactions list.
  271. pub async fn append_tx(&mut self, tx: Transaction) -> bool {
  272. let tx_hash = blake3::hash(&serialize(&tx));
  273. let tx_in_txstore = match self.blockchain.transactions.contains(&tx_hash) {
  274. Ok(v) => v,
  275. Err(e) => {
  276. error!("append_tx(): Failed querying txstore: {}", e);
  277. return false
  278. }
  279. };
  280. if self.unconfirmed_txs.contains(&tx) || tx_in_txstore {
  281. debug!("append_tx(): We have already seen this tx.");
  282. return false
  283. }
  284. debug!("append_tx(): Starting state transition validation");
  285. if let Err(e) = self.verify_transactions(&[tx.clone()], false).await {
  286. error!("append_tx(): Failed to verify transaction: {}", e);
  287. return false
  288. };
  289. debug!("append_tx(): Appended tx to mempool");
  290. self.unconfirmed_txs.push(tx);
  291. true
  292. }
  293. /// Calculates current epoch.
  294. pub fn current_epoch(&self) -> u64 {
  295. self.slot_epoch(self.current_slot())
  296. }
  297. /// Calculates the epoch of the provided slot.
  298. /// Epoch duration is configured using the `EPOCH_LENGTH` value.
  299. pub fn slot_epoch(&self, slot: u64) -> u64 {
  300. slot / constants::EPOCH_LENGTH as u64
  301. }
  302. /// Calculates current slot, based on elapsed time from the genesis block.
  303. /// Slot duration is configured using the `SLOT_TIME` constant.
  304. pub fn current_slot(&self) -> u64 {
  305. self.consensus.genesis_ts.elapsed() / constants::SLOT_TIME
  306. }
  307. /// Calculates the relative number of the provided slot.
  308. pub fn relative_slot(&self, slot: u64) -> u64 {
  309. slot % constants::EPOCH_LENGTH as u64
  310. }
  311. /// Finds the last slot a proposal or block was generated.
  312. pub fn last_slot(&self) -> Result<u64> {
  313. let mut slot = 0;
  314. for chain in &self.consensus.proposals {
  315. for proposal in &chain.proposals {
  316. if proposal.block.header.slot > slot {
  317. slot = proposal.block.header.slot;
  318. }
  319. }
  320. }
  321. // We return here in case proposals exist,
  322. // so we don't query the sled database.
  323. if slot > 0 {
  324. return Ok(slot)
  325. }
  326. let (last_slot, _) = self.blockchain.last()?;
  327. Ok(last_slot)
  328. }
  329. /// Calculates seconds until next Nth slot starting time.
  330. /// Slots duration is configured using the SLOT_TIME constant.
  331. pub fn next_n_slot_start(&self, n: u64) -> Duration {
  332. assert!(n > 0);
  333. let start_time = NaiveDateTime::from_timestamp(self.consensus.genesis_ts.0, 0);
  334. let current_slot = self.current_slot() + n;
  335. let next_slot_start =
  336. (current_slot * constants::SLOT_TIME) + (start_time.timestamp() as u64);
  337. let next_slot_start = NaiveDateTime::from_timestamp(next_slot_start as i64, 0);
  338. let current_time = NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0);
  339. let diff = next_slot_start - current_time;
  340. Duration::new(diff.num_seconds().try_into().unwrap(), 0)
  341. }
  342. /// Calculate slots until next Nth epoch.
  343. /// Epoch duration is configured using the EPOCH_LENGTH value.
  344. pub fn slots_to_next_n_epoch(&self, n: u64) -> u64 {
  345. assert!(n > 0);
  346. let slots_till_next_epoch =
  347. constants::EPOCH_LENGTH as u64 - self.relative_slot(self.current_slot());
  348. ((n - 1) * constants::EPOCH_LENGTH as u64) + slots_till_next_epoch
  349. }
  350. /// Calculates seconds until next Nth epoch starting time.
  351. pub fn next_n_epoch_start(&self, n: u64) -> Duration {
  352. self.next_n_slot_start(self.slots_to_next_n_epoch(n))
  353. }
  354. /// Set participating slot to next.
  355. pub fn set_participating(&mut self) -> Result<()> {
  356. self.consensus.participating = Some(self.current_slot() + 1);
  357. Ok(())
  358. }
  359. /// Check if new epoch has started, to create new epoch coins.
  360. /// Returns flag to signify if epoch has changed and vector of
  361. /// new epoch competing coins.
  362. pub async fn epoch_changed(
  363. &mut self,
  364. sigma1: pallas::Base,
  365. sigma2: pallas::Base,
  366. ) -> Result<bool> {
  367. let epoch = self.current_epoch();
  368. self.consensus.prev_sigma1 = sigma1;
  369. self.consensus.prev_sigma2 = sigma2;
  370. if epoch <= self.consensus.epoch {
  371. return Ok(false)
  372. }
  373. let eta = self.get_eta();
  374. // TODO: slot parameter should be absolute slot, not relative.
  375. // At start of epoch, relative slot is 0.
  376. self.consensus.coins = self.create_epoch_coins(eta, epoch).await?;
  377. self.consensus.epoch = epoch;
  378. self.consensus.epoch_eta = eta;
  379. Ok(true)
  380. }
  381. /// return 2-term target approximation sigma coefficients.
  382. pub fn sigmas(&mut self) -> (pallas::Base, pallas::Base) {
  383. let f = self.win_prob_with_full_stake();
  384. // Generate sigmas
  385. let mut total_stake = self.total_stake(); // Only used for fine-tuning
  386. // at genesis epoch first slot, of absolute index 0,
  387. // the total stake would be 0, to avoid division by zero,
  388. // we asume total stake at first division is GENESIS_TOTAL_STAKE.
  389. if total_stake == 0 {
  390. total_stake = constants::GENESIS_TOTAL_STAKE;
  391. }
  392. debug!("consensus::sigmas(): f: {}", f);
  393. debug!("consensus::sigmas(): stake: {}", total_stake);
  394. let one = constants::FLOAT10_ONE.clone();
  395. let two = constants::FLOAT10_TWO.clone();
  396. let field_p = Float10::from_str_native(constants::P)
  397. .unwrap()
  398. .with_precision(constants::RADIX_BITS)
  399. .value();
  400. let total_sigma =
  401. Float10::try_from(total_stake).unwrap().with_precision(constants::RADIX_BITS).value();
  402. let x = one - f;
  403. let c = x.ln();
  404. let sigma1_fbig = c.clone() / total_sigma.clone() * field_p.clone();
  405. let sigma1 = fbig2base(sigma1_fbig);
  406. let sigma2_fbig = (c / total_sigma).powf(two.clone()) * (field_p / two);
  407. let sigma2 = fbig2base(sigma2_fbig);
  408. (sigma1, sigma2)
  409. }
  410. /// Generate epoch-competing coins
  411. async fn create_epoch_coins(
  412. &self,
  413. eta: pallas::Base,
  414. epoch: u64,
  415. ) -> Result<Vec<Vec<LeadCoin>>> {
  416. info!("Consensus: Creating coins for epoch: {}", epoch);
  417. self.create_coins(eta).await
  418. }
  419. /// Generate coins for provided sigmas.
  420. /// NOTE: The strategy here is having a single competing coin per slot.
  421. async fn create_coins(&self, eta: pallas::Base) -> Result<Vec<Vec<LeadCoin>>> {
  422. let slot = self.current_slot();
  423. let mut rng = thread_rng();
  424. let mut seeds: Vec<u64> = Vec::with_capacity(constants::EPOCH_LENGTH);
  425. for _ in 0..constants::EPOCH_LENGTH {
  426. seeds.push(rng.gen());
  427. }
  428. let epoch_secrets = LeadCoinSecrets::generate();
  429. let mut tree_cm = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH);
  430. // LeadCoin matrix where each row represents a slot and contains its competing coins.
  431. let mut coins: Vec<Vec<LeadCoin>> = Vec::with_capacity(constants::EPOCH_LENGTH);
  432. // TODO: TESTNET: Here we would look into the wallet to find coins we're able to use.
  433. // The wallet has specific tables for consensus coins.
  434. // TODO: TESTNET: Token ID still has to be enforced properly in the consensus.
  435. // Temporarily, we compete with zero stake
  436. for i in 0..constants::EPOCH_LENGTH {
  437. let coin = LeadCoin::new(
  438. eta,
  439. constants::LOTTERY_HEAD_START, // TODO: TESTNET: Why is this constant being used?
  440. slot + i as u64,
  441. epoch_secrets.secret_keys[i].inner(),
  442. epoch_secrets.merkle_roots[i],
  443. i,
  444. epoch_secrets.merkle_paths[i],
  445. seeds[i],
  446. epoch_secrets.secret_keys[i],
  447. &mut tree_cm,
  448. );
  449. coins.push(vec![coin]);
  450. }
  451. Ok(coins)
  452. }
  453. /// leadership reward, assuming constant reward
  454. /// TODO (res) implement reward mechanism with accord to DRK,DARK token-economics
  455. fn reward() -> u64 {
  456. constants::REWARD
  457. }
  458. /// Auxillary function to receive current slot offset.
  459. /// If offset is None, its setted up as last block slot offset.
  460. fn get_current_offset(&mut self, current_slot: u64) -> u64 {
  461. // This is the case were we restarted our node, didn't receive offset from other nodes,
  462. // so we need to find offset from last block, exluding network dead period.
  463. if self.consensus.offset.is_none() {
  464. let (last_slot, last_offset) = self.blockchain.get_last_offset().unwrap();
  465. let offset = last_offset + (current_slot - last_slot);
  466. info!("get_current_offset(): Setting slot offset: {}", offset);
  467. self.consensus.offset = Some(offset);
  468. }
  469. self.consensus.offset.unwrap()
  470. }
  471. /// Auxillary function to calculate overall empty slots.
  472. /// We keep an offset from genesis indicating when the first slot actually started.
  473. /// This offset is shared between nodes.
  474. fn overall_empty_slots(&mut self, current_slot: u64) -> u64 {
  475. // Retrieve existing blocks excluding genesis
  476. let blocks = (self.blockchain.len() as u64) - 1;
  477. // Setup offset if only have genesis and havent received offset from other nodes
  478. if blocks == 0 && self.consensus.offset.is_none() {
  479. info!(
  480. "overall_empty_slots(): Blockchain contains only genesis, setting slot offset: {}",
  481. current_slot
  482. );
  483. self.consensus.offset = Some(current_slot);
  484. }
  485. // Retrieve longest fork length, to also those proposals in the calculation
  486. let max_fork_length = self.longest_chain_length() as u64;
  487. current_slot - blocks - self.get_current_offset(current_slot) - max_fork_length
  488. }
  489. /// total stake
  490. /// assuming constant Reward.
  491. fn total_stake(&mut self) -> i64 {
  492. let current_slot = self.current_slot();
  493. ((current_slot - self.overall_empty_slots(current_slot)) * Self::reward()) as i64
  494. }
  495. /// Calculate how many leaders existed in previous slot and appends
  496. /// it to history, to report it if win. On finalization sync period,
  497. /// node replaces its leaders history with the sequence extracted by
  498. /// the longest fork.
  499. fn extend_leaders_history(&mut self) -> Float10 {
  500. let slot = self.current_slot();
  501. let previous_slot = slot - 1;
  502. let mut count = 0;
  503. for chain in &self.consensus.proposals {
  504. // Previous slot proposals exist at end of each fork
  505. if chain.proposals.last().unwrap().block.header.slot == previous_slot {
  506. count += 1;
  507. }
  508. }
  509. self.consensus.leaders_history.push(count);
  510. debug!(
  511. "extend_leaders_history(): Current leaders history: {:?}",
  512. self.consensus.leaders_history
  513. );
  514. Float10::try_from(count as i64).unwrap().with_precision(constants::RADIX_BITS).value()
  515. }
  516. fn f_dif(&mut self) -> Float10 {
  517. let one = constants::FLOAT10_ONE.clone();
  518. one - self.extend_leaders_history()
  519. }
  520. fn f_der(&self) -> Float10 {
  521. let len = self.consensus.leaders_history.len();
  522. let last = Float10::try_from(self.consensus.leaders_history[len - 1] as i64)
  523. .unwrap()
  524. .with_precision(constants::RADIX_BITS)
  525. .value();
  526. let second_to_last = Float10::try_from(self.consensus.leaders_history[len - 2] as i64)
  527. .unwrap()
  528. .with_precision(constants::RADIX_BITS)
  529. .value();
  530. (last - second_to_last) / constants::TD.clone()
  531. }
  532. fn f_int(&self) -> Float10 {
  533. let mut sum = constants::FLOAT10_ZERO.clone();
  534. for f in &self.consensus.leaders_history {
  535. sum += f.clone() * constants::TD.clone();
  536. }
  537. sum
  538. }
  539. /// the probability of winnig lottery having all the stake
  540. /// returns f
  541. fn win_prob_with_full_stake(&mut self) -> Float10 {
  542. let zero = constants::FLOAT10_ZERO.clone();
  543. let one = constants::FLOAT10_ONE.clone();
  544. let p = self.f_dif();
  545. let i = self.f_int();
  546. let d = self.f_der();
  547. let mut f = self.consensus.kp.clone() *
  548. (p.clone() +
  549. one.clone() / constants::TI.clone() * i.clone() +
  550. constants::TD.clone() * d.clone());
  551. while f <= zero.clone() || f >= one.clone() {
  552. info!("Consensus::win_prob_with_full_stake(): f: {}", f);
  553. let mut clipped_f = f;
  554. if clipped_f >= one {
  555. clipped_f = one.clone() - constants::PID_OUT_STEP.clone();
  556. } else if clipped_f <= zero {
  557. clipped_f = zero.clone() + constants::PID_OUT_STEP.clone();
  558. }
  559. let clipped_kp = clipped_f /
  560. (p.clone() +
  561. one.clone() / constants::TI.clone() * i.clone() +
  562. constants::TD.clone() * d.clone());
  563. self.consensus.kp = clipped_kp.clone();
  564. f = clipped_kp *
  565. (p.clone() +
  566. one.clone() / constants::TI.clone() * i.clone() +
  567. constants::TD.clone() * d.clone());
  568. }
  569. info!("Consensus::win_prob_with_full_stake(): last f: {}", f);
  570. f
  571. }
  572. /// Check that the provided participant/stakeholder coins win the slot lottery.
  573. /// If the stakeholder has multiple competing winning coins, only the highest value
  574. /// coin is selected, since the stakeholder can't give more than one proof per block/slot.
  575. /// * 'sigma1', 'sigma2': slot sigmas
  576. /// Returns: (check: bool, idx: usize) where idx is the winning coin's index
  577. pub fn is_slot_leader(&mut self, sigma1: pallas::Base, sigma2: pallas::Base) -> (bool, usize) {
  578. // Slot relative index
  579. let slot = self.relative_slot(self.current_slot());
  580. // Stakeholder's epoch coins
  581. let coins = &self.consensus.coins;
  582. info!("Consensus::is_leader(): slot: {}, coins len: {}", slot, coins.len());
  583. assert!((slot as usize) < coins.len());
  584. let competing_coins = &coins[slot as usize];
  585. let mut won = false;
  586. let mut highest_stake = 0;
  587. let mut highest_stake_idx = 0;
  588. for (winning_idx, coin) in competing_coins.iter().enumerate() {
  589. let first_winning = coin.is_leader(sigma1, sigma2);
  590. if first_winning && !won {
  591. highest_stake_idx = winning_idx;
  592. }
  593. won |= first_winning;
  594. if won && coin.value > highest_stake {
  595. highest_stake = coin.value;
  596. highest_stake_idx = winning_idx;
  597. }
  598. }
  599. (won, highest_stake_idx)
  600. }
  601. /// Generate a block proposal for the current slot, containing all
  602. /// unconfirmed transactions. Proposal extends the longest fork
  603. /// chain the node is holding.
  604. pub fn propose(
  605. &mut self,
  606. idx: usize,
  607. sigma1: pallas::Base,
  608. sigma2: pallas::Base,
  609. ) -> Result<Option<BlockProposal>> {
  610. let slot = self.current_slot();
  611. let (prev_hash, index) = self.longest_chain_last_hash().unwrap();
  612. let unproposed_txs = self.unproposed_txs(index);
  613. // TODO: [PLACEHOLDER] Create and add rewards transaction
  614. let mut tree = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(100);
  615. // The following is pretty weird, so something better should be done.
  616. for tx in &unproposed_txs {
  617. let mut hash = [0_u8; 32];
  618. hash[0..31].copy_from_slice(&blake3::hash(&serialize(tx)).as_bytes()[0..31]);
  619. tree.append(&MerkleNode::from(pallas::Base::from_repr(hash).unwrap()));
  620. }
  621. let root = tree.root(0).unwrap();
  622. let eta = self.consensus.epoch_eta;
  623. // Generating leader proof
  624. let relative_slot = self.relative_slot(slot) as usize;
  625. let coin = self.consensus.coins[relative_slot][idx];
  626. let proof =
  627. coin.create_lead_proof(sigma1, sigma2, self.lead_proving_key.as_ref().unwrap())?;
  628. // Signing using coin
  629. let secret_key = coin.secret_key;
  630. let header =
  631. Header::new(prev_hash, self.slot_epoch(slot), slot, Timestamp::current_time(), root);
  632. let signed_proposal = secret_key.sign(&mut OsRng, &header.headerhash().as_bytes()[..]);
  633. let public_key = PublicKey::from_secret(secret_key);
  634. let lead_info = LeadInfo::new(
  635. signed_proposal,
  636. public_key,
  637. coin.public_inputs(sigma1, sigma2),
  638. eta.to_repr(),
  639. LeadProof::from(proof),
  640. self.get_current_offset(slot),
  641. self.consensus.leaders_history.last().unwrap().clone(),
  642. );
  643. // Replacing old coin with the derived coin
  644. // TODO: do we need that? on next epoch we replace everything
  645. // how is this going to get reused?
  646. self.consensus.coins[relative_slot][idx] = coin.derive_coin();
  647. Ok(Some(BlockProposal::new(header, unproposed_txs, lead_info)))
  648. }
  649. /// Retrieve all unconfirmed transactions not proposed in previous blocks
  650. /// of provided index chain.
  651. pub fn unproposed_txs(&self, index: i64) -> Vec<Transaction> {
  652. let mut unproposed_txs = self.unconfirmed_txs.clone();
  653. // If index is -1 (canonical blockchain) a new fork will be generated,
  654. // therefore all unproposed transactions can be included in the proposal.
  655. if index == -1 {
  656. return unproposed_txs
  657. }
  658. // We iterate over the fork chain proposals to find already proposed
  659. // transactions and remove them from the local unproposed_txs vector.
  660. let chain = &self.consensus.proposals[index as usize];
  661. for proposal in &chain.proposals {
  662. for tx in &proposal.block.txs {
  663. if let Some(pos) = unproposed_txs.iter().position(|txs| *txs == *tx) {
  664. unproposed_txs.remove(pos);
  665. }
  666. }
  667. }
  668. unproposed_txs
  669. }
  670. /// Finds the longest blockchain the node holds and
  671. /// returns the last block hash and the chain index.
  672. pub fn longest_chain_last_hash(&self) -> Result<(blake3::Hash, i64)> {
  673. let mut longest: Option<ProposalChain> = None;
  674. let mut length = 0;
  675. let mut index = -1;
  676. if !self.consensus.proposals.is_empty() {
  677. for (i, chain) in self.consensus.proposals.iter().enumerate() {
  678. if chain.proposals.len() > length {
  679. longest = Some(chain.clone());
  680. length = chain.proposals.len();
  681. index = i as i64;
  682. }
  683. }
  684. }
  685. let hash = match longest {
  686. Some(chain) => chain.proposals.last().unwrap().hash,
  687. None => self.blockchain.last()?.1,
  688. };
  689. Ok((hash, index))
  690. }
  691. /// Finds the length of longest fork chain the node holds.
  692. pub fn longest_chain_length(&self) -> usize {
  693. let mut max = 0;
  694. for proposal in &self.consensus.proposals {
  695. if proposal.proposals.len() > max {
  696. max = proposal.proposals.len();
  697. }
  698. }
  699. max
  700. }
  701. /// Given a proposal, the node verify its sender (slot leader) and finds which blockchain
  702. /// it extends. If the proposal extends the canonical blockchain, a new fork chain is created.
  703. pub async fn receive_proposal(&mut self, proposal: &BlockProposal) -> Result<()> {
  704. let current = self.current_slot();
  705. // Node hasn't started participating
  706. match self.consensus.participating {
  707. Some(start) => {
  708. if current < start {
  709. return Ok(())
  710. }
  711. }
  712. None => return Ok(()),
  713. }
  714. // Node have already checked for finalization in this slot
  715. if current <= self.consensus.checked_finalization {
  716. warn!("receive_proposal(): Proposal received after finalization sync period.");
  717. return Err(Error::ProposalAfterFinalizationError)
  718. }
  719. let lf = &proposal.block.lead_info;
  720. let hdr = &proposal.block.header;
  721. // Verify proposal signature is valid based on producer public key
  722. // TODO: derive public key from proof
  723. if !lf.public_key.verify(proposal.header.as_bytes(), &lf.signature) {
  724. warn!("receive_proposal(): Proposer {} signature could not be verified", lf.public_key);
  725. return Err(Error::InvalidSignature)
  726. }
  727. // Check if proposal hash matches actual one
  728. let proposal_hash = proposal.block.blockhash();
  729. if proposal.hash != proposal_hash {
  730. warn!(
  731. "receive_proposal(): Received proposal contains mismatched hashes: {} - {}",
  732. proposal.hash, proposal_hash
  733. );
  734. return Err(Error::ProposalHashesMissmatchError)
  735. }
  736. // Check if proposal header matches actual one
  737. let proposal_header = hdr.headerhash();
  738. if proposal.header != proposal_header {
  739. warn!(
  740. "receive_proposal(): Received proposal contains mismatched headers: {} - {}",
  741. proposal.header, proposal_header
  742. );
  743. return Err(Error::ProposalHeadersMissmatchError)
  744. }
  745. // Verify proposal offset
  746. let offset = self.get_current_offset(current);
  747. if offset != lf.offset {
  748. warn!(
  749. "receive_proposal(): Received proposal contains different offset: {} - {}",
  750. offset, lf.offset
  751. );
  752. return Err(Error::ProposalDifferentOffsetError)
  753. }
  754. // Verify proposal leader proof
  755. if let Err(e) = lf.proof.verify(&self.lead_verifying_key, &lf.public_inputs) {
  756. error!("receive_proposal(): Error during leader proof verification: {}", e);
  757. return Err(Error::LeaderProofVerification)
  758. };
  759. info!("receive_proposal(): Leader proof verified successfully!");
  760. // Verify proposal public values
  761. let (mu_y, mu_rho) =
  762. LeadCoin::election_seeds_u64(self.consensus.epoch_eta, proposal.block.header.slot);
  763. // y
  764. let prop_mu_y = lf.public_inputs[constants::PI_MU_Y_INDEX];
  765. if mu_y != prop_mu_y {
  766. error!(
  767. "receive_proposal(): Failed to verify mu_y: {:?}, proposed: {:?}",
  768. mu_y, prop_mu_y
  769. );
  770. return Err(Error::ProposalPublicValuesMismatched)
  771. }
  772. // rho
  773. let prop_mu_rho = lf.public_inputs[constants::PI_MU_RHO_INDEX];
  774. if mu_rho != prop_mu_rho {
  775. error!(
  776. "receive_proposal(): Failed to verify mu_rho: {:?}, proposed: {:?}",
  777. mu_rho, prop_mu_rho
  778. );
  779. return Err(Error::ProposalPublicValuesMismatched)
  780. }
  781. // sigma1
  782. let prop_sigma1 = lf.public_inputs[constants::PI_SIGMA1_INDEX];
  783. if self.consensus.prev_sigma1 != prop_sigma1 {
  784. error!(
  785. "receive_proposal(): Failed to verify public value sigma1: {:?}, to proposed: {:?}",
  786. self.consensus.prev_sigma1, prop_sigma1
  787. );
  788. }
  789. // sigma2
  790. let prop_sigma2 = lf.public_inputs[constants::PI_SIGMA2_INDEX];
  791. if self.consensus.prev_sigma2 != prop_sigma2 {
  792. error!(
  793. "receive_proposal(): Failed to verify public value sigma2: {:?}, to proposed: {:?}",
  794. self.consensus.prev_sigma2, prop_sigma2
  795. );
  796. }
  797. // sn
  798. let prop_sn = lf.public_inputs[constants::PI_NULLIFIER_INDEX];
  799. for sn in &self.consensus.leaders_nullifiers {
  800. if *sn == prop_sn {
  801. error!("receive_proposal(): Proposal nullifiers exist.");
  802. return Err(Error::ProposalIsSpent)
  803. }
  804. }
  805. // cm
  806. let prop_cm_x: pallas::Base = lf.public_inputs[constants::PI_COMMITMENT_X_INDEX];
  807. let prop_cm_y: pallas::Base = lf.public_inputs[constants::PI_COMMITMENT_Y_INDEX];
  808. for cm in &self.consensus.leaders_spent_coins {
  809. if *cm == (prop_cm_x, prop_cm_y) {
  810. error!("receive_proposal(): Proposal coin already spent.");
  811. return Err(Error::ProposalIsSpent)
  812. }
  813. }
  814. // Check if proposal extends any existing fork chains
  815. let index = self.find_extended_chain_index(proposal)?;
  816. if index == -2 {
  817. return Err(Error::ExtendedChainIndexNotFound)
  818. }
  819. // Validate state transition against canonical state
  820. // TODO: This should be validated against fork state
  821. debug!("receive_proposal(): Starting state transition validation");
  822. if let Err(e) = self.verify_transactions(&proposal.block.txs, false).await {
  823. error!("receive_proposal(): Transaction verifications failed: {}", e);
  824. return Err(e.into())
  825. };
  826. // TODO: [PLACEHOLDER] Add rewards validation
  827. // Extend corresponding chain
  828. match index {
  829. -1 => {
  830. let pc = ProposalChain::new(self.consensus.genesis_block, proposal.clone());
  831. self.consensus.proposals.push(pc);
  832. }
  833. _ => {
  834. self.consensus.proposals[index as usize].add(proposal);
  835. }
  836. };
  837. // Store proposal coin info
  838. self.consensus.leaders_nullifiers.push(prop_sn);
  839. self.consensus.leaders_spent_coins.push((prop_cm_x, prop_cm_y));
  840. Ok(())
  841. }
  842. /// Given a proposal, find the index of the fork chain it extends.
  843. pub fn find_extended_chain_index(&mut self, proposal: &BlockProposal) -> Result<i64> {
  844. // We iterate through all forks to find which fork to extend
  845. let mut chain_index = -1;
  846. let mut prop_index = 0;
  847. for (c_index, chain) in self.consensus.proposals.iter().enumerate() {
  848. // Traverse proposals in reverse
  849. for (p_index, prop) in chain.proposals.iter().enumerate().rev() {
  850. if proposal.block.header.previous == prop.hash {
  851. chain_index = c_index as i64;
  852. prop_index = p_index;
  853. break
  854. }
  855. }
  856. if chain_index != -1 {
  857. break
  858. }
  859. }
  860. // If no fork was found, we check with canonical
  861. if chain_index == -1 {
  862. let (last_slot, last_block) = self.blockchain.last()?;
  863. if proposal.block.header.previous != last_block ||
  864. proposal.block.header.slot <= last_slot
  865. {
  866. debug!("find_extended_chain_index(): Proposal doesn't extend any known chain");
  867. return Ok(-2)
  868. }
  869. // Proposal extends canonical chain
  870. return Ok(-1)
  871. }
  872. // Found fork chain
  873. let chain = &self.consensus.proposals[chain_index as usize];
  874. // Proposal extends fork at last proposal
  875. if prop_index == (chain.proposals.len() - 1) {
  876. return Ok(chain_index)
  877. }
  878. debug!("find_extended_chain_index(): Proposal to fork a forkchain was received.");
  879. let mut chain = self.consensus.proposals[chain_index as usize].clone();
  880. // We keep all proposals until the one it extends
  881. chain.proposals.drain((prop_index + 1)..);
  882. self.consensus.proposals.push(chain);
  883. Ok(self.consensus.proposals.len() as i64 - 1)
  884. }
  885. /// Search the chains we're holding for the given proposal.
  886. pub fn proposal_exists(&self, input_proposal: &blake3::Hash) -> bool {
  887. for chain in self.consensus.proposals.iter() {
  888. for proposal in chain.proposals.iter() {
  889. if input_proposal == &proposal.hash {
  890. return true
  891. }
  892. }
  893. }
  894. false
  895. }
  896. /// Remove provided transactions vector from unconfirmed_txs if they exist.
  897. pub fn remove_txs(&mut self, transactions: &Vec<Transaction>) -> Result<()> {
  898. for tx in transactions {
  899. if let Some(pos) = self.unconfirmed_txs.iter().position(|txs| txs == tx) {
  900. self.unconfirmed_txs.remove(pos);
  901. }
  902. }
  903. Ok(())
  904. }
  905. /// Auxillary function to set nodes leaders count history to the largest fork sequence
  906. /// of leaders, by using provided index.
  907. fn set_leader_history(&mut self, index: i64) {
  908. // Check if we found longest fork to extract sequence from
  909. match index {
  910. -1 => {
  911. debug!("set_leader_history(): No fork exists.");
  912. }
  913. _ => {
  914. debug!("set_leader_history(): Checking last proposal of fork: {}", index);
  915. let last_proposal =
  916. self.consensus.proposals[index as usize].proposals.last().unwrap();
  917. if last_proposal.block.header.slot == self.current_slot() {
  918. // Replacing our last history element with the leaders one
  919. self.consensus.leaders_history.pop();
  920. self.consensus.leaders_history.push(last_proposal.block.lead_info.leaders);
  921. debug!(
  922. "set_leader_history(): New leaders history: {:?}",
  923. self.consensus.leaders_history
  924. );
  925. return
  926. }
  927. }
  928. }
  929. self.consensus.leaders_history.push(0);
  930. }
  931. /// Node checks if any of the fork chains can be finalized.
  932. /// Consensus finalization logic:
  933. /// - If the node has observed the creation of 3 proposals in a fork chain and no other
  934. /// forks exists at same or greater height, it finalizes (appends to canonical blockchain)
  935. /// all proposals up to the last one.
  936. /// When fork chain proposals are finalized, the rest of fork chains are removed.
  937. pub async fn chain_finalization(&mut self) -> Result<Vec<BlockInfo>> {
  938. let slot = self.current_slot();
  939. debug!("chain_finalization(): Started finalization check for slot: {}", slot);
  940. // Set last slot finalization check occured to current slot
  941. self.consensus.checked_finalization = slot;
  942. // First we find longest chain without any other forks at same height
  943. let mut chain_index = -1;
  944. // Use this index to extract leaders count sequence from longest fork
  945. let mut index_for_history = -1;
  946. let mut max_length = 0;
  947. for (index, chain) in self.consensus.proposals.iter().enumerate() {
  948. let length = chain.proposals.len();
  949. // Check if greater than max to retain index for history
  950. if length > max_length {
  951. index_for_history = index as i64;
  952. }
  953. // Ignore forks with less that 3 blocks
  954. if length < 3 {
  955. continue
  956. }
  957. // Check if less than max
  958. if length < max_length {
  959. continue
  960. }
  961. // Check if same length as max
  962. if length == max_length {
  963. // Setting chain_index so we know we have multiple
  964. // forks at same length.
  965. chain_index = -2;
  966. continue
  967. }
  968. // Set chain as max
  969. chain_index = index as i64;
  970. max_length = length;
  971. }
  972. // Check if we found any fork to finalize
  973. match chain_index {
  974. -2 => {
  975. debug!("chain_finalization(): Eligible forks with same height exist, nothing to finalize.");
  976. self.set_leader_history(index_for_history);
  977. return Ok(vec![])
  978. }
  979. -1 => {
  980. debug!("chain_finalization(): All chains have less than 3 proposals, nothing to finalize.");
  981. self.set_leader_history(index_for_history);
  982. return Ok(vec![])
  983. }
  984. _ => debug!("chain_finalization(): Chain {} can be finalized!", chain_index),
  985. }
  986. // Starting finalization
  987. let mut chain = self.consensus.proposals[chain_index as usize].clone();
  988. // Retrieving proposals to finalize
  989. let bound = max_length - 1;
  990. let mut finalized: Vec<BlockInfo> = vec![];
  991. for proposal in &chain.proposals[..bound] {
  992. finalized.push(proposal.clone().into());
  993. }
  994. // Removing finalized proposals from chain
  995. chain.proposals.drain(..bound);
  996. // Adding finalized proposals to canonical
  997. info!("consensus: Adding {} finalized block to canonical chain.", finalized.len());
  998. match self.blockchain.add(&finalized) {
  999. Ok(v) => v,
  1000. Err(e) => {
  1001. error!("consensus: Failed appending finalized blocks to canonical chain: {}", e);
  1002. return Err(e)
  1003. }
  1004. };
  1005. let blocks_subscriber = self.subscribers.get("blocks").unwrap().clone();
  1006. // Validating state transitions
  1007. for proposal in &finalized {
  1008. // TODO: Is this the right place? We're already doing this in protocol_sync.
  1009. // TODO: These state transitions have already been checked. (I wrote this, but where?)
  1010. // TODO: FIXME: The state transitions have already been written, they have to be in memory
  1011. // until this point.
  1012. debug!(target: "consensus", "Applying state transition for finalized block");
  1013. if let Err(e) = self.verify_transactions(&proposal.txs, true).await {
  1014. error!(target: "consensus", "Finalized block transaction verifications failed: {}", e);
  1015. return Err(e)
  1016. }
  1017. // Remove proposal transactions from memory pool
  1018. if let Err(e) = self.remove_txs(&proposal.txs) {
  1019. error!(target: "consensus", "Removing finalized block transactions failed: {}", e);
  1020. return Err(e)
  1021. }
  1022. // TODO: Don't hardcode this:
  1023. let params = json!([bs58::encode(&serialize(proposal)).into_string()]);
  1024. let notif = JsonNotification::new("blockchain.subscribe_blocks", params);
  1025. info!("consensus: Sending notification about finalized block");
  1026. blocks_subscriber.notify(notif).await;
  1027. }
  1028. // Setting leaders history to last proposal leaders count
  1029. self.consensus.leaders_history =
  1030. vec![chain.proposals.last().unwrap().block.lead_info.leaders];
  1031. // Removing rest forks
  1032. self.consensus.proposals = vec![];
  1033. self.consensus.proposals.push(chain);
  1034. Ok(finalized)
  1035. }
  1036. /// Utility function to extract leader selection lottery randomness(eta),
  1037. /// defined as the hash of the previous lead proof converted to pallas base.
  1038. fn get_eta(&self) -> pallas::Base {
  1039. let proof_tx_hash = self.blockchain.get_last_proof_hash().unwrap();
  1040. let mut bytes: [u8; 32] = *proof_tx_hash.as_bytes();
  1041. // read first 254 bits
  1042. bytes[30] = 0;
  1043. bytes[31] = 0;
  1044. pallas::Base::from_repr(bytes).unwrap()
  1045. }
  1046. // ==========================
  1047. // State transition functions
  1048. // ==========================
  1049. // TODO TESTNET: Write down all cases below
  1050. // State transition checks should be happening in the following cases for a sync node:
  1051. // 1) When a finalized block is received
  1052. // 2) When a transaction is being broadcasted to us
  1053. // State transition checks should be happening in the following cases for a consensus participating node:
  1054. // 1) When a finalized block is received
  1055. // 2) When a transaction is being broadcasted to us
  1056. // ==========================
  1057. /// Validate and append to canonical state received blocks.
  1058. pub async fn receive_blocks(&mut self, blocks: &[BlockInfo]) -> Result<()> {
  1059. // Verify state transitions for all blocks and their respective transactions.
  1060. debug!("receive_blocks(): Starting state transition validations");
  1061. for block in blocks {
  1062. if let Err(e) = self.verify_transactions(&block.txs, false).await {
  1063. error!("receive_blocks(): Transaction verifications failed: {}", e);
  1064. return Err(e)
  1065. }
  1066. }
  1067. debug!("receive_blocks(): All state transitions passed");
  1068. debug!("receive_blocks(): Appending blocks to ledger");
  1069. self.blockchain.add(blocks)?;
  1070. Ok(())
  1071. }
  1072. /// Validate and append to canonical state received finalized block.
  1073. /// Returns boolean flag indicating already existing block.
  1074. pub async fn receive_finalized_block(&mut self, block: BlockInfo) -> Result<bool> {
  1075. match self.blockchain.has_block(&block) {
  1076. Ok(v) => {
  1077. if v {
  1078. debug!("receive_finalized_block(): Existing block received");
  1079. return Ok(false)
  1080. }
  1081. }
  1082. Err(e) => {
  1083. error!("receive_finalized_block(): failed checking for has_block(): {}", e);
  1084. return Ok(false)
  1085. }
  1086. };
  1087. debug!("receive_finalized_block(): Executing state transitions");
  1088. self.receive_blocks(&[block.clone()]).await?;
  1089. // TODO: Don't hardcode this:
  1090. let blocks_subscriber = self.subscribers.get("blocks").unwrap();
  1091. let params = json!([bs58::encode(&serialize(&block)).into_string()]);
  1092. let notif = JsonNotification::new("blockchain.subscribe_blocks", params);
  1093. info!("consensus: Sending notification about finalized block");
  1094. blocks_subscriber.notify(notif).await;
  1095. debug!("receive_finalized_block(): Removing block transactions from unconfirmed_txs");
  1096. self.remove_txs(&block.txs)?;
  1097. Ok(true)
  1098. }
  1099. /// Validate and append to canonical state received finalized blocks from block sync task.
  1100. /// Already existing blocks are ignored.
  1101. pub async fn receive_sync_blocks(&mut self, blocks: &[BlockInfo]) -> Result<()> {
  1102. let mut new_blocks = vec![];
  1103. for block in blocks {
  1104. match self.blockchain.has_block(block) {
  1105. Ok(v) => {
  1106. if v {
  1107. debug!("receive_sync_blocks(): Existing block received");
  1108. continue
  1109. }
  1110. new_blocks.push(block.clone());
  1111. }
  1112. Err(e) => {
  1113. error!("receive_sync_blocks(): failed checking for has_block(): {}", e);
  1114. continue
  1115. }
  1116. };
  1117. }
  1118. if new_blocks.is_empty() {
  1119. debug!("receive_sync_blocks(): no new blocks to append");
  1120. return Ok(())
  1121. }
  1122. debug!("receive_sync_blocks(): Executing state transitions");
  1123. self.receive_blocks(&new_blocks[..]).await?;
  1124. // TODO: Don't hardcode this:
  1125. let blocks_subscriber = self.subscribers.get("blocks").unwrap();
  1126. for block in new_blocks {
  1127. let params = json!([bs58::encode(&serialize(&block)).into_string()]);
  1128. let notif = JsonNotification::new("blockchain.subscribe_blocks", params);
  1129. info!("consensus: Sending notification about finalized block");
  1130. blocks_subscriber.notify(notif).await;
  1131. }
  1132. Ok(())
  1133. }
  1134. /// Validate signatures, wasm execution, and zk proofs for given transactions.
  1135. /// If all of those succeed, try to execute a state update for the contract calls.
  1136. /// Currently the verifications are sequential, and the function will fail if any
  1137. /// of the verifications fail.
  1138. /// The function takes a boolean called `write` which tells it to actually write
  1139. /// the state transitions to the database.
  1140. // TODO: This should be paralellized as if even one tx in the batch fails to verify,
  1141. // we can drop everything.
  1142. pub async fn verify_transactions(&self, txs: &[Transaction], write: bool) -> Result<()> {
  1143. debug!("Verifying {} transaction(s)", txs.len());
  1144. for tx in txs {
  1145. let tx_hash = blake3::hash(&serialize(tx));
  1146. debug!("Verifying transaction {}", tx_hash);
  1147. // Table of public inputs used for ZK proof verification
  1148. let mut zkp_table = vec![];
  1149. // Table of public keys used for signature verification
  1150. let mut sig_table = vec![];
  1151. // State updates produced by contract execcution
  1152. let mut updates = vec![];
  1153. // Iterate over all calls to get the metadata
  1154. for (idx, call) in tx.calls.iter().enumerate() {
  1155. debug!("Executing contract call {}", idx);
  1156. let wasm = match self.blockchain.wasm_bincode.get(call.contract_id) {
  1157. Ok(v) => {
  1158. debug!("Found wasm bincode for {}", call.contract_id);
  1159. v
  1160. }
  1161. Err(e) => {
  1162. error!(
  1163. "Could not find wasm bincode for contract {}: {}",
  1164. call.contract_id, e
  1165. );
  1166. return Err(Error::ContractNotFound(call.contract_id.to_string()))
  1167. }
  1168. };
  1169. // Write the actual payload data
  1170. let mut payload = vec![];
  1171. payload.write_u32(idx as u32)?; // Call index
  1172. tx.calls.encode(&mut payload)?; // Actual call data
  1173. // Instantiate the wasm runtime
  1174. let mut runtime =
  1175. match Runtime::new(&wasm, self.blockchain.clone(), call.contract_id) {
  1176. Ok(v) => v,
  1177. Err(e) => {
  1178. error!(
  1179. "Failed to instantiate WASM runtime for contract {}",
  1180. call.contract_id
  1181. );
  1182. return Err(e.into())
  1183. }
  1184. };
  1185. debug!("Executing \"metadata\" call");
  1186. let metadata = match runtime.metadata(&payload) {
  1187. Ok(v) => v,
  1188. Err(e) => {
  1189. error!("Failed to execute \"metadata\" call: {}", e);
  1190. return Err(e.into())
  1191. }
  1192. };
  1193. // Decode the metadata retrieved from the execution
  1194. let mut decoder = Cursor::new(&metadata);
  1195. let zkp_pub: Vec<(String, Vec<pallas::Base>)> =
  1196. match Decodable::decode(&mut decoder) {
  1197. Ok(v) => v,
  1198. Err(e) => {
  1199. error!("Failed to decode ZK public inputs from metadata: {}", e);
  1200. return Err(e.into())
  1201. }
  1202. };
  1203. let sig_pub: Vec<PublicKey> = match Decodable::decode(&mut decoder) {
  1204. Ok(v) => v,
  1205. Err(e) => {
  1206. error!("Failed to decode signature pubkeys from metadata: {}", e);
  1207. return Err(e.into())
  1208. }
  1209. };
  1210. // TODO: Make sure we've read all the bytes above.
  1211. debug!("Successfully executed \"metadata\" call");
  1212. zkp_table.push(zkp_pub);
  1213. sig_table.push(sig_pub);
  1214. // After getting the metadata, we run the "exec" function with the same
  1215. // runtime and the same payload.
  1216. debug!("Executing \"exec\" call");
  1217. match runtime.exec(&payload) {
  1218. Ok(v) => {
  1219. debug!("Successfully executed \"exec\" call");
  1220. updates.push(v);
  1221. }
  1222. Err(e) => {
  1223. error!(
  1224. "Failed to execute \"exec\" call for contract id {}: {}",
  1225. call.contract_id, e
  1226. );
  1227. return Err(e.into())
  1228. }
  1229. };
  1230. // At this point we're done with the call and move on to the next one.
  1231. }
  1232. // When we're done looping and executing over the tx's contract calls, we
  1233. // move on with verification. First we verify the signatures as that's
  1234. // cheaper, and then finally we verify the ZK proofs.
  1235. debug!("Verifying signatures for transaction {}", tx_hash);
  1236. match tx.verify_sigs(sig_table) {
  1237. Ok(()) => debug!("Signatures verification for tx {} successful", tx_hash),
  1238. Err(e) => {
  1239. error!("Signature verification for tx {} failed: {}", tx_hash, e);
  1240. return Err(e.into())
  1241. }
  1242. };
  1243. // NOTE: When it comes to the ZK proofs, we first do a lookup of the
  1244. // verifying keys, but if we do not find them, we'll generate them
  1245. // inside of this function. This can be kinda expensive, so open to
  1246. // alternatives.
  1247. debug!("Verifying ZK proofs for transaction {}", tx_hash);
  1248. match tx.verify_zkps(self.verifying_keys.clone(), zkp_table).await {
  1249. Ok(()) => debug!("ZK proof verification for tx {} successful", tx_hash),
  1250. Err(e) => {
  1251. error!("ZK proof verrification for tx {} failed: {}", tx_hash, e);
  1252. return Err(e.into())
  1253. }
  1254. };
  1255. // After the verifications stage passes, if we're told to write, we
  1256. // apply the state updates.
  1257. assert!(tx.calls.len() == updates.len());
  1258. if write {
  1259. debug!("Performing state updates");
  1260. for (call, update) in tx.calls.iter().zip(updates.iter()) {
  1261. // For this we instantiate the runtimes again.
  1262. // TODO: Optimize this
  1263. // TODO: Sum up the gas costs of previous calls during execution
  1264. // and verification and these.
  1265. let wasm = match self.blockchain.wasm_bincode.get(call.contract_id) {
  1266. Ok(v) => {
  1267. debug!("Found wasm bincode for {}", call.contract_id);
  1268. v
  1269. }
  1270. Err(e) => {
  1271. error!(
  1272. "Could not find wasm bincode for contract {}: {}",
  1273. call.contract_id, e
  1274. );
  1275. return Err(Error::ContractNotFound(call.contract_id.to_string()))
  1276. }
  1277. };
  1278. let mut runtime =
  1279. match Runtime::new(&wasm, self.blockchain.clone(), call.contract_id) {
  1280. Ok(v) => v,
  1281. Err(e) => {
  1282. error!(
  1283. "Failed to instantiate WASM runtime for contract {}",
  1284. call.contract_id
  1285. );
  1286. return Err(e.into())
  1287. }
  1288. };
  1289. debug!("Executing \"apply\" call");
  1290. match runtime.apply(&update) {
  1291. // TODO: FIXME: This should be done in an atomic tx/batch
  1292. Ok(()) => debug!("State update applied successfully"),
  1293. Err(e) => {
  1294. error!("Failed to apply state update: {}", e);
  1295. return Err(e.into())
  1296. }
  1297. };
  1298. }
  1299. } else {
  1300. debug!("Skipping apply of state updates because write=false");
  1301. }
  1302. debug!("Transaction {} verified successfully", tx_hash);
  1303. }
  1304. Ok(())
  1305. }
  1306. }