state.rs 56 KB

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