Browse Source

WIP tuning lead election

mohab metwally 4 years ago
parent
commit
bac2d1cd32
3 changed files with 17 additions and 9 deletions
  1. 2 0
      src/crypto/leadcoin.rs
  2. 8 3
      src/stakeholder/stakeholder.rs
  3. 7 6
      src/zk/circuit/lead_contract.rs

+ 2 - 0
src/crypto/leadcoin.rs

@@ -42,6 +42,7 @@ pub struct LeadCoin {
     // election seeds
     pub y_mu: Option<pallas::Base>, // leader election nonce derived from eta at onset of epoch
     pub rho_mu: Option<pallas::Base>, // leader election nonce derived from eta at onset of epoch
+    pub sigma_scalar: Option<pallas::Base>,
 }
 
 impl LeadCoin {
@@ -136,6 +137,7 @@ impl LeadCoin {
             mau_rho: Value::known(mod_r_p(self.rho_mu.unwrap())),
             mau_y: Value::known(mod_r_p(self.y_mu.unwrap())),
             root_cm: Value::known(self.root_cm.unwrap()),
+            sigma_scalar: Value::known(self.sigma_scalar.unwrap()),
         };
         contract
     }

+ 8 - 3
src/stakeholder/stakeholder.rs

@@ -39,9 +39,9 @@ use group::ff::PrimeField;
 pub struct SlotWorkspace
 {
     pub st : blake3::Hash,
-    pub e: u64,
-    pub sl: u64,
-    pub txs: Vec<Transaction>,
+    pub e: u64, // epoch index
+    pub sl: u64, // absolute slot index
+    pub txs: Vec<Transaction>, // unpublished block transactions
     pub metadata: Metadata,
     pub is_leader: bool,
     pub proof: Proof,
@@ -331,6 +331,11 @@ impl Stakeholder
         debug!("[new epoch] 4 {}", self);
         let eta = self.get_eta();
         let mut epoch = Epoch::new(self.epoch_consensus, self.get_eta());
+        //TODO calculate total stake
+        // create coin with absolute slot/epoch.
+        let num_slots = self.workspace.sl;
+        // total stake;
+        let sigma = num_slots*reward;
         epoch.create_coins(); // set epoch interal fields working space with competing coins
         self.epoch = epoch.clone();
     }

+ 7 - 6
src/zk/circuit/lead_contract.rs

@@ -130,6 +130,7 @@ pub struct LeadContract {
     pub mau_rho: Value<pallas::Scalar>,
     pub mau_y: Value<pallas::Scalar>,
     pub root_cm: Value<pallas::Scalar>,
+    pub sigma_scalar: Value<pallas::Base>,
     //pub eta : Option<u32>,
     //pub rho : Option<u32>,
     //pub h : Option<u32>, // hash of this data
@@ -338,11 +339,11 @@ impl Circuit<pallas::Base> for LeadContract {
             self.root_sk
         )?;
 
-        /// scalar used for fine-tuning the leader election frequency
-        let scalar = self.load_private(
+        /// sigma scalar is 2^254/(total network stake + epsilon)
+        let sigma_scalar = self.load_private(
             layouter.namespace(|| "load scalar "),
             config.advices[0],
-            Value::known(pallas::Base::from(1024)),
+            self.sigma_scalar,
         )?;
 
         /// leadership coefficient used for fine-tunning leader election frequency
@@ -563,9 +564,9 @@ impl Circuit<pallas::Base> for LeadContract {
         };
         let rho_commit = com.add(layouter.namespace(|| "nonce commit"), &blind)?;
         let rho_commit_base = rho_commit.inner().x();
-
-        let ord = ar_chip.mul(layouter.namespace(|| ""), &scalar, &c)?;
-        let target = ar_chip.mul(layouter.namespace(|| "calculate target"), &ord, &coin_value.clone())?;
+        // stakeholder absolute stake + 1 (epsilon)
+        let stake_plus = ar_chip.add(layouter.namespace(|| ""), &one, coin_value.clone())?;
+        let target = ar_chip.mul(layouter.namespace(|| "calculate target"), &sigma_scalar, &stake_plus)?;
 
         let y : Value<pallas::Base> = y_commit_base.value().cloned();
         let T : Value<pallas::Base> = target.value().cloned();