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

dao: Update wallet schema to link tables together.

parazyd 3 лет назад
Родитель
Сommit
9afb9da4c9
3 измененных файлов с 13 добавлено и 18 удалено
  1. 1 1
      bin/drk/src/wallet_dao.rs
  2. 11 17
      src/contract/dao/wallet.sql
  3. 1 0
      src/wallet/walletdb.rs

+ 1 - 1
bin/drk/src/wallet_dao.rs

@@ -498,7 +498,7 @@ impl Drk {
     /// Fetch known unspent balances from the wallet for the given DAO ID
     pub async fn dao_balance(&self, dao_id: u64) -> Result<HashMap<String, u64>> {
         let daos = self.get_daos().await?;
-        let Some(dao) = daos.get(dao_id as usize -1) else {
+        let Some(dao) = daos.get(dao_id as usize - 1) else {
             return Err(anyhow!("DAO with ID {} not found in wallet", dao_id))
         };
 

+ 11 - 17
src/contract/dao/wallet.sql

@@ -101,8 +101,10 @@
 --              For now I didn't put anything, but we should keep this minor
 --              point in mind and ruminate on it for later.
 
+PRAGMA foreign_keys = ON;
+
 CREATE TABLE IF NOT EXISTS dao_daos (
-	dao_id INTEGER PRIMARY KEY NOT NULL,
+	dao_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
     name BLOB UNIQUE NOT NULL,
     proposer_limit INTEGER NOT NULL,
     -- minimum threshold for total number of votes for proposal to pass.
@@ -127,19 +129,8 @@ CREATE TABLE IF NOT EXISTS dao_trees (
 	proposals_tree BLOB NOT NULL
 );
 
--- NOTE: we should rename coin as coin_id in the money_coins table
---       and also keep track of the tx hash, call index, output of the coin.
---       There should also be a spends table which tracks
---       tx_hash:call_index:input
-
--- This table maps received coins to its DAO owner and vice versa.
-CREATE TABLE IF NOT EXISTS dao_coins (
-    coin_id INTEGER PRIMARY KEY NOT NULL,
-    dao_id INTEGER UNIQUE NOT NULL
-);
-
 CREATE TABLE IF NOT EXISTS dao_proposals (
-    proposal_id INTEGER PRIMARY KEY NOT NULL,
+    proposal_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
     dao_id INTEGER NOT NULL,
     -- Public key of person that would receive the funds
     recv_public BLOB NOT NULL,
@@ -155,18 +146,21 @@ CREATE TABLE IF NOT EXISTS dao_proposals (
     tx_hash BLOB,
     call_index INTEGER,
     -- this is NULL until we have voted on this proposal
-    our_vote_id INTEGER UNIQUE
+    our_vote_id INTEGER UNIQUE,
+
+    FOREIGN KEY(our_vote_id) REFERENCES dao_votes(vote_id) ON DELETE CASCADE ON UPDATE CASCADE,
+    FOREIGN KEY(dao_id) REFERENCES dao_daos(dao_id) ON DELETE CASCADE ON UPDATE CASCADE
 );
 
 CREATE TABLE IF NOT EXISTS dao_votes (
-    vote_id INTEGER PRIMARY KEY NOT NULL,
+    vote_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
     proposal_id INTEGER NOT NULL,
     vote_option INTEGER NOT NULL,
     -- these values are NULL until the vote is minted on chain
     -- and received by the DAO
     tx_hash BLOB,
-    call_index INTEGER
+    call_index INTEGER,
     -- My code has votes merkle tree and position for votes, but
     -- that might be a mistake...
+    FOREIGN KEY(proposal_id) REFERENCES dao_proposals(proposal_id) ON DELETE CASCADE ON UPDATE CASCADE
 );
-

+ 1 - 0
src/wallet/walletdb.rs

@@ -87,6 +87,7 @@ impl WalletDb {
 
         let mut connect_opts = SqliteConnectOptions::from_str(path)?
             //.pragma("key", password.to_string())
+            .pragma("foreign_keys", "ON")
             .create_if_missing(true)
             .journal_mode(SqliteJournalMode::Off);