Browse Source

Fix shared memory issue

Cesar Rodas 1 year ago
parent
commit
929f831c33
6 changed files with 30 additions and 11 deletions
  1. 4 4
      client.js
  2. 5 3
      src/main.rs
  3. 1 1
      utxo/src/id/mod.rs
  4. 12 1
      utxo/src/ledger.rs
  5. 4 2
      utxo/src/storage/sqlite/mod.rs
  6. 4 0
      utxo/src/transaction/inner.rs

+ 4 - 4
client.js

@@ -92,14 +92,14 @@ async function change_status(id, s_status) {
 async function test() {
   let d = (await deposit(addr1, 1.5, "BTC/8"));
   dbg(d);
-  dbg(await change_status(d.id, 'settled'));
+  dbg(await change_status(d._id, 'settled'));
   d = (await deposit(addr2, 1000000, "USD/4"));
-  dbg(await change_status(d.id, 'settled'));
+  dbg(await change_status(d._id, 'settled'));
 
   const t = await trade(1, "BTC/8", addr1, 26751.11, "USD/4", addr2);
   dbg(t);
-  dbg(await change_status(t.id, 'processing',));
-  dbg(await change_status(t.id, 'settled'));
+  dbg(await change_status(t._id, 'processing',));
+  dbg(await change_status(t._id, 'settled'));
   dbg(await get_balance(addr1));
   dbg(await get_balance(addr2));
   dbg(await get_balance(fee));

+ 5 - 3
src/main.rs

@@ -232,10 +232,12 @@ async fn main() -> std::io::Result<()> {
     let storage = verax::storage::SQLite::new(pool.clone());
     storage.setup().await.expect("setup");
 
+    let inner_storage = verax::storage::SQLite::new(pool.clone());
+    let storage = verax::storage::Cache::new(inner_storage);
+    let ledger = verax::Ledger::new(storage.into());
+
     HttpServer::new(move || {
-        let inner_storage = verax::storage::SQLite::new(pool.clone());
-        let storage = verax::storage::Cache::new(inner_storage);
-        let ledger = verax::Ledger::new(storage.into());
+        let ledger = ledger.clone();
 
         App::new()
             .wrap(Logger::default())

+ 1 - 1
utxo/src/id/mod.rs

@@ -8,7 +8,7 @@ pub struct MaxLengthString<const MAX_LENGTH: usize>(String);
 
 impl<const MAX_LENGTH: usize> PartialEq<str> for MaxLengthString<MAX_LENGTH> {
     fn eq(&self, other: &str) -> bool {
-        self.0 == *other
+        self.0.eq(other)
     }
 }
 

+ 12 - 1
utxo/src/ledger.rs

@@ -6,7 +6,7 @@ use crate::{
 use std::{cmp::Ordering, collections::HashMap, sync::Arc};
 
 /// The Verax ledger
-#[derive(Clone, Debug)]
+#[derive(Debug)]
 pub struct Ledger<S>
 where
     S: Storage + Sync + Send,
@@ -14,6 +14,17 @@ where
     config: Arc<Config<S>>,
 }
 
+impl<S> Clone for Ledger<S>
+where
+    S: Storage + Sync + Send,
+{
+    fn clone(&self) -> Self {
+        Self {
+            config: self.config.clone(),
+        }
+    }
+}
+
 impl<S> Ledger<S>
 where
     S: Storage + Sync + Send,

+ 4 - 2
utxo/src/storage/sqlite/mod.rs

@@ -60,7 +60,7 @@ impl SQLite {
             "created_at" DATETIME DEFAULT CURRENT_TIMESTAMP,
             "updated_at" DATETIME DEFAULT CURRENT_TIMESTAMP
         );
-        CREATE INDEX IF NOT EXISTS "spent_by" ON "payments" ("to", "spent_by");
+        CREATE INDEX IF NOT EXISTS "spent_by" ON "payments" ("to", "status", "spent_by");
         CREATE TABLE IF NOT EXISTS "transaction_accounts" (
             "id" INTEGER PRIMARY KEY AUTOINCREMENT,
             "account_id" VARCHAR(64) NOT NULL,
@@ -132,7 +132,9 @@ impl Storage for SQLite {
             FROM
                 "payments"
             WHERE
-                "to" = ? AND "spent_by" IS NULL AND status = ?
+                "to" = ?
+                AND status = ?
+                AND "spent_by" IS NULL
             GROUP BY "asset"
             "#,
         )

+ 4 - 0
utxo/src/transaction/inner.rs

@@ -23,6 +23,7 @@ use std::collections::HashMap;
 /// security that its content was not altered.
 #[derive(Debug, Clone, Deserialize, Serialize)]
 pub struct Revision {
+    #[serde(rename = "_prev_rev")]
     /// Any previous transaction that this transaction is replacing.
     previous: Option<RevisionId>,
     /// A human-readable description of the transaction changes.
@@ -157,12 +158,15 @@ impl Revision {
 /// The transaction ID, and the revision ID, are the cryptographic hash of the transactions
 #[derive(Debug, Clone, Serialize)]
 pub struct Transaction {
+    #[serde(rename = "_id")]
     /// The RevisionId is the RevisionID of the first revision of the transaction.
     pub id: RevisionId,
 
+    #[serde(rename = "_rev")]
     /// Current Revision ID.
     pub revision_id: RevisionId,
 
+    #[serde(rename = "_latest_rev")]
     /// Latest revision of this transaction
     pub lastest_revision: RevisionId,