Просмотр исходного кода

consensus: moved coin commitments tree under ConsensusState

aggstam 3 лет назад
Родитель
Сommit
46eee6dfc1
2 измененных файлов с 13 добавлено и 12 удалено
  1. 6 3
      src/consensus/leadcoin.rs
  2. 7 9
      src/consensus/state.rs

+ 6 - 3
src/consensus/leadcoin.rs

@@ -32,7 +32,7 @@ use rand::rngs::OsRng;
 
 use super::constants::EPOCH_LENGTH;
 use crate::{
-    consensus::{TransferStx, TxRcpt, constants},
+    consensus::{constants, TransferStx, TxRcpt},
     crypto::{proof::ProvingKey, Proof},
     zk::{vm::ZkCircuit, vm_stack::Witness},
     zkas::ZkBinary,
@@ -139,7 +139,7 @@ impl LeadCoin {
         // Create commitment to coin2
         let coin2_commitment = Self::commitment(
             pk,
-            pallas::Base::from(value+constants::REWARD),
+            pallas::Base::from(value + constants::REWARD),
             pallas::Base::from(coin2_seed),
             coin2_blind,
         );
@@ -306,7 +306,10 @@ impl LeadCoin {
 
     /// the new coin to be minted after the current coin is spent
     /// in lottery.
-    pub fn derive_coin(&self, coin_commitment_tree:  &mut BridgeTree<MerkleNode, MERKLE_DEPTH>) -> LeadCoin {
+    pub fn derive_coin(
+        &self,
+        coin_commitment_tree: &mut BridgeTree<MerkleNode, MERKLE_DEPTH>,
+    ) -> LeadCoin {
         info!("LeadCoin::derive_coin()");
         let mut derived = self.clone();
         let rho = self.derived_rho();

+ 7 - 9
src/consensus/state.rs

@@ -78,9 +78,11 @@ pub struct ConsensusState {
     pub epoch: u64,
     /// Current epoch eta
     pub epoch_eta: pallas::Base,
+    // TODO: Aren't these already in db after finalization?
     /// Current epoch competing coins
     pub coins: Vec<Vec<LeadCoin>>,
-    // TODO: Aren't these already in db after finalization?
+    /// Coin commitments tree
+    pub coins_tree: BridgeTree<MerkleNode, MERKLE_DEPTH>,
     /// Seen nullifiers from proposals
     pub leaders_nullifiers: Vec<pallas::Base>,
     /// Seen spent coins from proposals
@@ -108,6 +110,7 @@ impl ConsensusState {
             epoch: 0,
             epoch_eta: pallas::Base::one(),
             coins: vec![],
+            coins_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH * 100),
             leaders_nullifiers: vec![],
             leaders_spent_coins: vec![],
             leaders_history: vec![0],
@@ -173,8 +176,6 @@ pub struct ValidatorState {
     pub verifying_keys: Arc<RwLock<HashMap<[u8; 32], Vec<(String, VerifyingKey)>>>>,
     /// Wallet interface
     pub wallet: WalletPtr,
-    /// consensuss coin commitment tree
-    pub coin_tree: BridgeTree::<MerkleNode, MERKLE_DEPTH>,
 }
 
 impl ValidatorState {
@@ -283,7 +284,7 @@ impl ValidatorState {
         let mut subscribers = HashMap::new();
         let block_subscriber = Subscriber::new();
         subscribers.insert("blocks", block_subscriber);
-        let tree_cm = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(constants::EPOCH_LENGTH*100);
+
         let state = Arc::new(RwLock::new(ValidatorState {
             lead_proving_key,
             lead_verifying_key,
@@ -293,7 +294,6 @@ impl ValidatorState {
             subscribers,
             verifying_keys: Arc::new(RwLock::new(verifying_keys)),
             wallet,
-            coin_tree: tree_cm,
         }));
 
         Ok(state)
@@ -508,7 +508,7 @@ impl ValidatorState {
                 epoch_secrets.merkle_paths[i],
                 seeds[i],
                 epoch_secrets.secret_keys[i],
-                &mut self.coin_tree,
+                &mut self.consensus.coins_tree,
             );
 
             coins.push(vec![coin]);
@@ -734,9 +734,7 @@ impl ValidatorState {
             self.consensus.leaders_history.last().unwrap().clone(),
         );
         // Replacing old coin with the derived coin
-        // TODO: do we need that? on next epoch we replace everything
-        // how is this going to get reused?
-        self.consensus.coins[relative_slot][idx] = coin.derive_coin(&mut self.coin_tree);
+        self.consensus.coins[relative_slot][idx] = coin.derive_coin(&mut self.consensus.coins_tree);
 
         Ok(Some(BlockProposal::new(header, unproposed_txs, lead_info)))
     }