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

drk: Write minted DAO updates to wallet when scanning blocks.

parazyd 3 лет назад
Родитель
Сommit
ee93088dae
3 измененных файлов с 46 добавлено и 2 удалено
  1. 14 1
      bin/drk/src/rpc_blockchain.rs
  2. 31 0
      bin/drk/src/rpc_wallet.rs
  3. 1 1
      src/contract/dao/src/dao_model.rs

+ 14 - 1
bin/drk/src/rpc_blockchain.rs

@@ -154,14 +154,27 @@ impl Drk {
             }
         }
 
+        let mut daos = self.wallet_get_daos().await?;
         let (mut daos_tree, mut proposals_tree) = self.wallet_dao_trees().await?;
         // We assume that the state transitions are correct and won't allow a DAO being
         // minted twice. So here we just blindly put stuff in.
         for bulla in minted_dao_bullas {
             daos_tree.append(&MerkleNode::from(bulla.0.inner()));
-            // TODO: If our wallet has this DAO, add the info in the row, and witness the tree
+            for dao in daos.iter_mut() {
+                if dao.bulla() == bulla.0 {
+                    eprintln!("Found DAO {:?}, noting down for wallet update", bulla.0);
+                    // We have this DAO imported our wallet. Add the metadata:
+                    dao.leaf_position = daos_tree.witness();
+                    dao.tx_hash = Some(bulla.1);
+                    dao.call_index = Some(bulla.2);
+                }
+            }
         }
 
+        eprintln!("Writing DAO updates to wallet");
+        self.put_daos(&daos).await?;
+        self.put_dao_trees(&daos_tree, &proposals_tree).await?;
+
         Ok(())
     }
 

+ 31 - 0
bin/drk/src/rpc_wallet.rs

@@ -651,6 +651,37 @@ impl Drk {
         Ok(())
     }
 
+    /// Write given DAOs into the wallet
+    pub async fn put_daos(&self, daos: &[Dao]) -> Result<()> {
+        for dao in daos {
+            // Note that for now we just write the leaf pos, tx-hash, and call_index.
+            // This is because we don't expect the other stuff to change.
+            let query = format!(
+                "UPDATE {} SET {} = ?1, {} = ?2, {} = ?3 WHERE {} = ?4;",
+                DAO_DAOS_TABLE,
+                DAO_DAOS_COL_LEAF_POSITION,
+                DAO_DAOS_COL_TX_HASH,
+                DAO_DAOS_COL_CALL_INDEX,
+                DAO_DAOS_COL_DAO_ID
+            );
+
+            let params = json!([
+                query,
+                QueryType::Blob as u8,
+                serialize(&dao.leaf_position.unwrap()),
+                QueryType::Blob as u8,
+                serialize(&dao.tx_hash.unwrap()),
+                QueryType::Integer as u8,
+                dao.call_index.unwrap(),
+            ]);
+
+            let req = JsonRequest::new("wallet.exec_sql", params);
+            let _ = self.rpc_client.request(req).await?;
+        }
+
+        Ok(())
+    }
+
     /// Fetch all DAOs from the wallet
     /// We use this a lot because we don't worry too much about performance in this
     /// tool, and also in practice probably not a lot of DAOs will be in a single

+ 1 - 1
src/contract/dao/src/dao_model.rs

@@ -19,7 +19,7 @@
 use darkfi_sdk::crypto::{pallas, pasta_prelude::*, MerkleNode, Nullifier, PublicKey};
 use darkfi_serial::{SerialDecodable, SerialEncodable};
 
-#[derive(SerialEncodable, SerialDecodable)]
+#[derive(Debug, Copy, Clone, Eq, PartialEq, SerialEncodable, SerialDecodable)]
 pub struct DaoBulla(pallas::Base);
 
 impl DaoBulla {