Bladeren bron

node: Return Transaction on send(), and lazy-init VerifyingKeys.

parazyd 4 jaren geleden
bovenliggende
commit
df4acc2814
3 gewijzigde bestanden met toevoegingen van 24 en 10 verwijderingen
  1. 7 6
      src/node/client.rs
  2. 3 0
      src/node/mod.rs
  3. 14 4
      src/node/state.rs

+ 7 - 6
src/node/client.rs

@@ -109,7 +109,7 @@ impl Client {
         token_id: DrkTokenId,
         clear_input: bool,
         state: Arc<Mutex<State>>,
-    ) -> ClientResult<Vec<Coin>> {
+    ) -> ClientResult<(Transaction, Vec<Coin>)> {
         debug!("build_slab_from_tx(): Begin building slab from tx");
         let mut clear_inputs = vec![];
         let mut inputs = vec![];
@@ -180,7 +180,7 @@ impl Client {
         debug!("build_slab_from_tx(): Checking if state transition is valid");
         let state = &*state.lock().await;
         debug!("build_slab_from_tx(): Got state lock");
-        state_transition(state, tx)?;
+        state_transition(state, tx.clone())?;
         debug!("build_slab_from_tx(): Successful state transition");
 
         debug!("build_slab_from_tx(): Broadcasting transaction");
@@ -188,7 +188,7 @@ impl Client {
         //self.p2p.broadcast(Tx(Transaction)).await?;
         debug!("build_slab_from_tx(): Broadcasted successfully");
 
-        Ok(coins)
+        Ok((tx, coins))
     }
 
     pub async fn send(
@@ -198,7 +198,7 @@ impl Client {
         token_id: DrkTokenId,
         clear_input: bool,
         state: Arc<Mutex<State>>,
-    ) -> ClientResult<()> {
+    ) -> ClientResult<Transaction> {
         // TODO: Token id debug
         debug!("send(): Sending {}", amount);
 
@@ -206,7 +206,8 @@ impl Client {
             return Err(ClientFailed::InvalidAmount(0))
         }
 
-        let coins = self.build_slab_from_tx(pubkey, amount, token_id, clear_input, state).await?;
+        let (tx, coins) =
+            self.build_slab_from_tx(pubkey, amount, token_id, clear_input, state).await?;
         for coin in coins.iter() {
             // TODO: This should be more robust. In case our transaction is denied,
             // we want to revert to be able to send again.
@@ -214,7 +215,7 @@ impl Client {
         }
 
         debug!("send(): Sent {}", amount);
-        Ok(())
+        Ok(tx)
     }
 
     pub async fn transfer(

+ 3 - 0
src/node/mod.rs

@@ -1,2 +1,5 @@
 pub mod client;
+pub use client::Client;
+
 pub mod state;
+pub use state::State;

+ 14 - 4
src/node/state.rs

@@ -1,4 +1,5 @@
 use incrementalmerkletree::{bridgetree::BridgeTree, Frontier, Tree};
+use lazy_init::Lazy;
 use log::{debug, error};
 
 use crate::{
@@ -14,6 +15,7 @@ use crate::{
     },
     tx::Transaction,
     wallet::walletdb::WalletPtr,
+    zk::circuit::{BurnContract, MintContract},
     Result,
 };
 
@@ -154,9 +156,9 @@ pub struct State {
     /// List of Faucet public keys
     pub faucet_pubkeys: Vec<PublicKey>,
     /// Verifying key for the Mint ZK proof
-    pub mint_vk: VerifyingKey,
+    pub mint_vk: Lazy<VerifyingKey>,
     /// Verifying key for the Burn ZK proof
-    pub burn_vk: VerifyingKey,
+    pub burn_vk: Lazy<VerifyingKey>,
 }
 
 impl State {
@@ -244,10 +246,18 @@ impl ProgramState for State {
     }
 
     fn mint_vk(&self) -> &VerifyingKey {
-        &self.mint_vk
+        self.mint_vk.get_or_create(build_mint_vk)
     }
 
     fn burn_vk(&self) -> &VerifyingKey {
-        &self.burn_vk
+        self.burn_vk.get_or_create(build_burn_vk)
     }
 }
+
+fn build_mint_vk() -> VerifyingKey {
+    VerifyingKey::build(11, &MintContract::default())
+}
+
+fn build_burn_vk() -> VerifyingKey {
+    VerifyingKey::build(11, &BurnContract::default())
+}