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

fix lead example with new stakeholder api

mohab metwally 4 лет назад
Родитель
Сommit
031ce67a0f
5 измененных файлов с 72 добавлено и 22 удалено
  1. 5 3
      bin/crypsinous/src/main.rs
  2. 15 9
      example/lead.rs
  3. 22 7
      src/blockchain/epoch.rs
  4. 22 3
      src/stakeholder/stakeholder.rs
  5. 8 0
      src/util/clock.rs

+ 5 - 3
bin/crypsinous/src/main.rs

@@ -8,9 +8,11 @@ use std::thread;
 
 fn main()
 {
-    let slots=3;
-    let reward=22;
-    let epoch_consensus = EpochConsensus::new(slots, reward);
+    let slots=22;
+    let epochs=3;
+    let ticks=22;
+    let reward=1;
+    let epoch_consensus = EpochConsensus::new(Some(slots), Some(epochs), Some(ticks), reward);
     /// read n from the cmd
     let n = 3;
     /// initialize n stakeholders

+ 15 - 9
example/lead.rs

@@ -6,6 +6,7 @@ use pasta_curves::{
     pallas,
 };
 
+use futures::executor::block_on;
 
 use darkfi::crypto::proof::VerifyingKey;
 use darkfi::crypto::proof::ProvingKey;
@@ -13,6 +14,7 @@ use darkfi::crypto::proof::ProvingKey;
 use darkfi::{
     blockchain::{
         Blockchain,
+        EpochConsensus,
         epoch::{Epoch,EpochItem},
     },
     stakeholder::stakeholder::{Stakeholder},
@@ -26,37 +28,41 @@ use darkfi::{
     },
     tx::Transaction,
     consensus::{TransactionLeadProof, Metadata, StreamletMetadata, BlockInfo},
+    net::{P2p,Settings, SettingsPtr,},
     zk::circuit::lead_contract::LeadContract,
 };
 
 fn main() {
+    println!("--> starting lead example");
     let k: u32 = 13;
     //
-    let lead_pk = ProvingKey::build(k, &LeadContract::default());
-    let lead_vk = VerifyingKey::build(k, &LeadContract::default());
+
     //
     const LEN: usize = 10;
     let epoch_item = EpochItem {
         value: 0,  //static stake value
     };
     //
-    let genesis_data = blake3::hash(b"");
-    let db = sled::open("/tmp/darkfi.db").unwrap();
-    let oc = Blockchain::new(&db, Timestamp::current_time(), genesis_data).unwrap();
-    let stakeholder = Stakeholder {
-        blockchain: oc,
-    };
+    let settings = Settings::default();
+    let consensus = EpochConsensus::new(Some(22), Some(3), Some(22), Some(1));
+    println!("--> block on stakeholder future");
+    let stakeholder : Stakeholder = block_on(Stakeholder::new(consensus, settings, Some(13))).unwrap();
+    println!("block on stakeholder future <--|");
     let eta : pallas::Base = stakeholder.get_eta();
-    let epoch = Epoch {
+    let mut epoch = Epoch {
         len: Some(LEN),
         item: Some(epoch_item),
         eta: eta,
+        coins: vec![],
     };
     let coins: Vec<LeadCoin> = epoch.create_coins();
     let coin_idx = 0;
     let coin = coins[coin_idx];
     let contract = coin.create_contract();
 
+    let lead_pk = stakeholder.get_provkingkey();
+    let lead_vk = stakeholder.get_verifyingkey();
+
     //
     //let proof = lead_proof::create_lead_proof(lead_pk.clone(), coin.clone());
 

+ 22 - 7
src/blockchain/epoch.rs

@@ -36,22 +36,37 @@ pub struct EpochItem {
 /// should be populated from configuration file.
 #[derive(Copy,Debug,Default,Clone)]
 pub struct EpochConsensus {
-    pub len : u64, /// number of slots per epoch
+    pub sl_len : u64, /// number of slots per epoch
+    pub e_len : u64,
+    pub tick_len: u64,
     pub reward: u64,
 }
 
 impl EpochConsensus{
-    fn new(len: u64, reward: u64) -> Self{
-        Self {len, reward}
+    pub fn new(sl_len: Option<u64>, e_len: Option<u64>, tick_len: Option<u64>, reward: Option<u64>) -> Self {
+        Self {
+            sl_len: sl_len.unwrap_or(22),
+            e_len: e_len.unwrap_or(3),
+            tick_len: tick_len.unwrap_or(22),
+            reward: reward.unwrap_or(1)
+        }
     }
 
     /// TODO how is the reward derived?
-    fn get_reward(&self)  -> u64{
+    pub fn get_reward(&self)  -> u64{
         self.reward
     }
 
-    fn get_sl(&self)  -> u64{
-        self.len
+    pub fn get_slot_len(&self)  -> u64{
+        self.sl_len
+    }
+
+    pub fn get_epoch_len(&self) -> u64 {
+        self.e_len
+    }
+
+    pub fn get_tick_len(&self) -> u64 {
+        self.tick_len
     }
 }
 
@@ -71,7 +86,7 @@ impl Epoch {
 
     pub fn new(consensus: EpochConsensus, true_random:pallas::Base) -> Self
     {
-        Self {len: Some(consensus.len as usize),
+        Self {len: Some(consensus.get_slot_len() as usize),
               item: Some(EpochItem {value: consensus.reward}),
               eta: true_random,
               coins:vec!(),

+ 22 - 3
src/stakeholder/stakeholder.rs

@@ -122,27 +122,39 @@ impl Stakeholder
         let ts = Timestamp::current_time();
         let genesis_hash = blake3::hash(b"");
         //TODO lisen and add transactions
+        println!("--> new blockchain");
         let bc = Blockchain::new(&db, ts, genesis_hash).unwrap();
+        println!("--> bc initialized");
         //TODO replace with const
         let eta = pallas::Base::one();
         let epoch = Epoch::new(consensus, eta);
+        println!("--> building zk proving key");
         let lead_pk = ProvingKey::build(k.unwrap(), &LeadContract::default());
+        println!("--> building zk veryfing key ");
         let lead_vk = VerifyingKey::build(k.unwrap(), &LeadContract::default());
+        println!("-->new network");
         let p2p = P2p::new(settings.clone()).await;
+        println!("--> network initialized");
         //TODO
         let workspace = SlotWorkspace::default();
         let subscription : Subscription<Result<ChannelPtr>> = p2p.subscribe_channel().await;
+        println!("--> channel");
         let chanptr : ChannelPtr =  subscription.receive().await.unwrap();
+        println!("--> received channel");
         //
         let message_subsytem = chanptr.get_message_subsystem();
+        println!("--> adding dispatcher to msg subsystem");
         message_subsytem.add_dispatch::<BlockInfo>().await;
+        println!("--> added");
         //TODO start channel if isn't started yet
         //let info = chanptr.get_info();
         //println!("channel info: {}", info);
+        println!("--> subscribe msg_sub");
         let msg_sub : MessageSubscription::<BlockInfo> =
             chanptr.subscribe_msg::<BlockInfo>().await.expect("missing blockinfo");
+        println!("--> subscribed");
         //
-        let clock = Clock::new(Some(3), Some(22), Some(22), settings.peers);
+        let clock = Clock::new(Some(consensus.get_epoch_len()), Some(consensus.get_slot_len()), Some(consensus.get_tick_len()), settings.peers);
         Ok(Self{blockchain: bc,
                 net: p2p,
                 clock: clock,
@@ -160,6 +172,14 @@ impl Stakeholder
         })
     }
 
+    pub fn get_provkingkey(&self) -> ProvingKey {
+        self.pk.clone()
+    }
+
+    pub fn get_verifyingkey(&self) -> VerifyingKey {
+        self.vk.clone()
+    }
+
     /// get list stakeholder peers on the p2p network for synchronization
     pub fn get_peers(&self) -> Vec<Url> {
         let settings : SettingsPtr = self.net.settings();
@@ -190,8 +210,7 @@ impl Stakeholder
     }
 
     /// add new blockinfo to the blockchain
-    pub fn add_block(&self, block: BlockInfo)
-    {
+    pub fn add_block(&self, block: BlockInfo) {
         let blocks = [block];
         self.blockchain.add(&blocks);
     }

+ 8 - 0
src/util/clock.rs

@@ -42,6 +42,14 @@ impl Clock {
         }
     }
 
+    pub fn get_sl_len(&self) -> u64 {
+        self.sl_len
+    }
+
+    pub fn get_e_len(&self) -> u64 {
+        self.e_len
+    }
+
     async fn time(&self) -> Result<Timestamp> {
         match time::check_clock(self.peers.clone()).await {
             Ok(t) => {