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

consensus: replace winning coin with derived one

aggstam 3 лет назад
Родитель
Сommit
f93d87c7d3
3 измененных файлов с 19 добавлено и 23 удалено
  1. 8 7
      src/consensus/leadcoin.rs
  2. 2 6
      src/consensus/metadata.rs
  3. 9 10
      src/consensus/state.rs

+ 8 - 7
src/consensus/leadcoin.rs

@@ -113,8 +113,8 @@ impl LeadCoin {
         sigma2: pallas::Base,
         // Stake value
         value: u64,
-        // Slot index in the epock
-        slot_index: usize,
+        // Slot index in the epoch
+        slot_index: u64,
         // coin1 sk
         coin1_sk: pallas::Base,
         // Merkle root of the `coin_1` secret key in the Merkle tree of secret keys
@@ -133,7 +133,7 @@ impl LeadCoin {
         // Generate random blinding values for commitments:
         let coin1_blind = pallas::Scalar::random(&mut OsRng);
         let coin2_blind = pallas::Scalar::random(&mut OsRng);
-        let tau = pallas::Base::from(slot_index as u64);
+        let tau = pallas::Base::from(slot_index);
         // pk
         let pk_msg =
             [pallas::Base::from(PREFIX_PK), coin1_sk_root.inner(), tau, pallas::Base::from(ZERO)];
@@ -178,7 +178,7 @@ impl LeadCoin {
         // Create commitment to coin2
         let coin2_commitment = pedersen_commitment_base(coin2_commit_v, coin2_blind);
         // Derive election seeds
-        let (y_mu, rho_mu) = Self::election_seeds(eta, pallas::Base::from(slot_index as u64));
+        let (y_mu, rho_mu) = Self::election_seeds(eta, pallas::Base::from(slot_index));
         // Derive a nullifier
         let sn_msg = [
             pallas::Base::from(PREFIX_SN),
@@ -195,7 +195,7 @@ impl LeadCoin {
             coin2_commitment,
             // TODO: Should be abs slot
             idx: u32::try_from(usize::from(leaf_pos)).unwrap(),
-            sl: pallas::Base::from(slot_index as u64),
+            sl: pallas::Base::from(slot_index),
             // Assume tau is sl for simplicity
             tau,
             nonce: pallas::Base::from(seed),
@@ -314,7 +314,8 @@ impl LeadCoin {
     }
     /// the new coin to be minted after the current coin is spent
     /// in lottery.
-    pub fn derive_coin(&self, eta: pallas::Base, slot: pallas::Base) -> LeadCoin {
+    pub fn derive_coin(&self, eta: pallas::Base, slot: u64) -> LeadCoin {
+        let tau = pallas::Base::from(slot);
         let mut derived = self.clone();
         let pk = self.pk();
         let rho = self.derived_rho();
@@ -326,7 +327,7 @@ impl LeadCoin {
         derived.coin1_blind = derived.coin2_blind;
         derived.coin2_blind = blind;
         // update random mau_y, mau_rho in case epoch is changed
-        let (y_mu, rho_mu) = Self::election_seeds(eta, slot);
+        let (y_mu, rho_mu) = Self::election_seeds(eta, tau);
         derived.y_mu = y_mu;
         derived.rho_mu = rho_mu;
         derived

+ 2 - 6
src/consensus/metadata.rs

@@ -38,8 +38,6 @@ pub struct Metadata {
     pub public_key: PublicKey, // TODO: remove this(to be derived by proof)
     /// Block owner slot competing coins public inputs
     pub public_inputs: Vec<pallas::Base>,
-    /// Block owner winning coin index
-    pub winning_index: usize,
     /// Response of global random oracle, or it's emulation.
     pub eta: [u8; 32],
     /// Leader NIZK proof
@@ -52,10 +50,9 @@ impl Default for Metadata {
         let keypair = Keypair::default();
         let signature = Signature::dummy();
         let public_inputs = vec![];
-        let winning_index = 0;
         let eta: [u8; 32] = *blake3::hash(b"let there be dark!").as_bytes();
         let proof = LeadProof::default();
-        Self { signature, public_key: keypair.public, public_inputs, winning_index, eta, proof }
+        Self { signature, public_key: keypair.public, public_inputs, eta, proof }
     }
 }
 
@@ -64,11 +61,10 @@ impl Metadata {
         signature: Signature,
         public_key: PublicKey,
         public_inputs: Vec<pallas::Base>,
-        winning_index: usize,
         eta: [u8; 32],
         proof: LeadProof,
     ) -> Self {
-        Self { signature, public_key, public_inputs, winning_index, eta, proof }
+        Self { signature, public_key, public_inputs, eta, proof }
     }
 }
 

+ 9 - 10
src/consensus/state.rs

@@ -396,7 +396,7 @@ impl ValidatorState {
                 sigma1,
                 sigma2,
                 LOTTERY_HEAD_START, // TODO: TESTNET: Why is this constant being used?
-                i,
+                i as u64,
                 epoch_secrets.secret_keys[i].inner(),
                 epoch_secrets.merkle_roots[i],
                 i, //TODO same as idx now for simplicity.
@@ -486,6 +486,8 @@ impl ValidatorState {
         let (prev_hash, index) = self.longest_chain_last_hash().unwrap();
         let unproposed_txs = self.unproposed_txs(index);
 
+        // TODO: [PLACEHOLDER] Create and add rewards transaction
+
         let mut tree = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(100);
         /* TODO: FIXME: TESTNET:
         for tx in &unproposed_txs {
@@ -497,12 +499,10 @@ impl ValidatorState {
         */
         let root = tree.root(0).unwrap();
 
-        let eta = self.consensus.epoch_eta.to_repr();
+        let eta = self.consensus.epoch_eta;
         // Generating leader proof
         let relative_slot = self.relative_slot(slot) as usize;
         let coin = self.consensus.coins[relative_slot][idx];
-        // TODO: Generate new LeadCoin from newlly minted coin, will reuse original coin for now
-        //let coin2 = something();
         let proof = coin.create_lead_proof(&self.lead_proving_key)?;
 
         // Signing using coin
@@ -516,15 +516,14 @@ impl ValidatorState {
             signed_proposal,
             public_key,
             coin.public_inputs(),
-            idx,
-            eta,
+            eta.to_repr(),
             LeadProof::from(proof),
         );
-        // TODO: replace old coin with new coin
-        self.consensus.coins[relative_slot][idx] = coin;
+        // 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(eta, relative_slot as u64);
 
-        // TODO: [PLACEHOLDER] Add rewards calculation (proof?)
-        // TODO: [PLACEHOLDER] Create and add rewards transaction
         Ok(Some(BlockProposal::new(header, unproposed_txs, metadata)))
     }