state.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 darkfi_sdk::crypto::{constants::MERKLE_DEPTH, MerkleNode};
  19. use darkfi_serial::{SerialDecodable, SerialEncodable};
  20. use incrementalmerkletree::bridgetree::BridgeTree;
  21. use pasta_curves::pallas;
  22. use super::{constants, leadcoin::LeadCoin, Block, Float10, ProposalChain};
  23. use crate::{net, tx::Transaction, util::time::Timestamp, Result};
  24. /// This struct represents the information required by the consensus algorithm
  25. #[derive(Debug)]
  26. pub struct ConsensusState {
  27. /// Genesis block creation timestamp
  28. pub genesis_ts: Timestamp,
  29. /// Genesis block hash
  30. pub genesis_block: blake3::Hash,
  31. /// Participating start slot
  32. pub participating: Option<u64>,
  33. /// Last slot node check for finalization
  34. pub checked_finalization: u64,
  35. /// Slots offset since genesis,
  36. pub offset: Option<u64>,
  37. /// Fork chains containing block proposals
  38. pub proposals: Vec<ProposalChain>,
  39. /// Current epoch
  40. pub epoch: u64,
  41. /// Hot/live slot checkpoints
  42. pub slot_checkpoints: Vec<SlotCheckpoint>,
  43. /// previous epoch eta
  44. pub prev_epoch_eta: pallas::Base,
  45. /// Current epoch eta
  46. pub epoch_eta: pallas::Base,
  47. // TODO: Aren't these already in db after finalization?
  48. /// Current epoch competing coins
  49. pub coins: Vec<Vec<LeadCoin>>,
  50. /// Coin commitments tree
  51. pub coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
  52. /// Seen nullifiers from proposals
  53. pub leaders_nullifiers: Vec<pallas::Base>,
  54. /// Seen spent coins from proposals
  55. pub leaders_spent_coins: Vec<(pallas::Base, pallas::Base)>,
  56. /// Leaders count history
  57. pub leaders_history: Vec<u64>,
  58. /// Kp
  59. pub kp: Float10,
  60. /// Previous slot sigma1
  61. pub prev_sigma1: pallas::Base,
  62. /// Previous slot sigma2
  63. pub prev_sigma2: pallas::Base,
  64. }
  65. impl ConsensusState {
  66. pub fn new(genesis_ts: Timestamp, genesis_data: blake3::Hash) -> Result<Self> {
  67. let genesis_block = Block::genesis_block(genesis_ts, genesis_data).blockhash();
  68. Ok(Self {
  69. genesis_ts,
  70. genesis_block,
  71. participating: None,
  72. checked_finalization: 0,
  73. offset: None,
  74. proposals: vec![],
  75. epoch: 0,
  76. slot_checkpoints: vec![],
  77. prev_epoch_eta: pallas::Base::one(),
  78. epoch_eta: pallas::Base::one(),
  79. coins: vec![],
  80. coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
  81. leaders_nullifiers: vec![],
  82. leaders_spent_coins: vec![],
  83. leaders_history: vec![0],
  84. kp: constants::FLOAT10_TWO.clone() / constants::FLOAT10_NINE.clone(),
  85. prev_sigma1: pallas::Base::zero(),
  86. prev_sigma2: pallas::Base::zero(),
  87. })
  88. }
  89. }
  90. /// Auxiliary structure used for consensus syncing.
  91. #[derive(Debug, SerialEncodable, SerialDecodable)]
  92. pub struct ConsensusRequest {}
  93. impl net::Message for ConsensusRequest {
  94. fn name() -> &'static str {
  95. "consensusrequest"
  96. }
  97. }
  98. /// Auxiliary structure used for consensus syncing.
  99. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  100. pub struct ConsensusResponse {
  101. /// Slots offset since genesis,
  102. pub offset: Option<u64>,
  103. /// Hot/live data used by the consensus algorithm
  104. pub proposals: Vec<ProposalChain>,
  105. /// Pending transactions
  106. pub unconfirmed_txs: Vec<Transaction>,
  107. /// Hot/live slot checkpoints
  108. pub slot_checkpoints: Vec<SlotCheckpoint>,
  109. /// Seen nullifiers from proposals
  110. pub leaders_nullifiers: Vec<pallas::Base>,
  111. /// Seen spent coins from proposals
  112. pub leaders_spent_coins: Vec<(pallas::Base, pallas::Base)>,
  113. }
  114. impl net::Message for ConsensusResponse {
  115. fn name() -> &'static str {
  116. "consensusresponse"
  117. }
  118. }
  119. /// Auxiliary structure used to keep track of slot validation parameters.
  120. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  121. pub struct SlotCheckpoint {
  122. /// Slot UID
  123. pub slot: u64,
  124. /// Slot eta
  125. pub eta: pallas::Base,
  126. /// Slot sigma1
  127. pub sigma1: pallas::Base,
  128. /// Slot sigma2
  129. pub sigma2: pallas::Base,
  130. }
  131. impl SlotCheckpoint {
  132. pub fn new(slot: u64, eta: pallas::Base, sigma1: pallas::Base, sigma2: pallas::Base) -> Self {
  133. Self { slot, eta, sigma1, sigma2 }
  134. }
  135. /// Generate the genesis slot checkpoint.
  136. pub fn genesis_slot_checkpoint() -> Self {
  137. let eta = pallas::Base::zero();
  138. let sigma1 = pallas::Base::zero();
  139. let sigma2 = pallas::Base::zero();
  140. Self::new(0, eta, sigma1, sigma2)
  141. }
  142. }
  143. /// Auxiliary structure used for slot checkpoints syncing
  144. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  145. pub struct SlotCheckpointRequest {
  146. /// Slot UID
  147. pub slot: u64,
  148. }
  149. impl net::Message for SlotCheckpointRequest {
  150. fn name() -> &'static str {
  151. "slotcheckpointrequest"
  152. }
  153. }
  154. /// Auxiliary structure used for slot checkpoints syncing
  155. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  156. pub struct SlotCheckpointResponse {
  157. /// Response blocks.
  158. pub slot_checkpoints: Vec<SlotCheckpoint>,
  159. }
  160. impl net::Message for SlotCheckpointResponse {
  161. fn name() -> &'static str {
  162. "slotcheckpointresponse"
  163. }
  164. }