teaclave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From din...@apache.org
Subject [mesatee-sgx] branch master updated: rust-poc complete
Date Mon, 04 Nov 2019 21:25:18 GMT
This is an automated email from the ASF dual-hosted git repository.

dingyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesatee-sgx.git


The following commit(s) were added to refs/heads/master by this push:
     new 931a1c7  rust-poc complete
     new 1d8a9f8  Merge pull request #175 from bradyjoestar/baidu_master
931a1c7 is described below

commit 931a1c70a26a3d0679e329aef906a3a03bbc4907
Author: webb.shi <bradyjoestar@gmail.com>
AuthorDate: Fri Nov 1 02:51:49 2019 -0400

    rust-poc complete
---
 samplecode/db-proxy/db-server/Cargo.toml           |   2 +-
 samplecode/db-proxy/db-server/src/main.rs          |  14 +-
 samplecode/db-proxy/db-server/src/server/mod.rs    | 175 ++++++++++++++++++---
 .../db-proxy/db-server/src/verifytree/mbtree.rs    |  43 +++--
 4 files changed, 189 insertions(+), 45 deletions(-)

diff --git a/samplecode/db-proxy/db-server/Cargo.toml b/samplecode/db-proxy/db-server/Cargo.toml
index 016505e..2009302 100644
--- a/samplecode/db-proxy/db-server/Cargo.toml
+++ b/samplecode/db-proxy/db-server/Cargo.toml
@@ -7,7 +7,7 @@ edition = "2018"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-merklebtree = "0.1.1"
+merklebtree = "0.1.5"
 ring = "0.16.9"
 hex = "0.4.0"
 serde = { version = "1.0.60", features = ["derive"] }
diff --git a/samplecode/db-proxy/db-server/src/main.rs b/samplecode/db-proxy/db-server/src/main.rs
index 892485f..06a9e56 100644
--- a/samplecode/db-proxy/db-server/src/main.rs
+++ b/samplecode/db-proxy/db-server/src/main.rs
@@ -1,29 +1,26 @@
 extern crate hex;
 extern crate merklebtree;
+extern crate parking_lot;
 extern crate ring;
+extern crate rocksdb;
 extern crate serde;
 extern crate serde_json;
-extern crate rocksdb;
-extern crate parking_lot;
-
 
 mod client;
 mod server;
 mod verifytree;
 
-
-use std::fs;
-use std::io::{Read, Write};
-use std::path;
 use parking_lot::RwLock;
 use rocksdb::{DBVector, WriteBatch, DB};
 use server::new_server;
+use std::fs;
+use std::io::{Read, Write};
+use std::path;
 
 fn main() {
     vertias_db();
 }
 
-
 pub fn vertias_db() {
     // generate the key_value of hmac key
     let mut key_value = Vec::new();
@@ -34,4 +31,3 @@ pub fn vertias_db() {
     let mut server = new_server(key_value);
     client::client_test(&mut server);
 }
-
diff --git a/samplecode/db-proxy/db-server/src/server/mod.rs b/samplecode/db-proxy/db-server/src/server/mod.rs
index 52f2503..1b7a499 100644
--- a/samplecode/db-proxy/db-server/src/server/mod.rs
+++ b/samplecode/db-proxy/db-server/src/server/mod.rs
@@ -1,10 +1,13 @@
 use crate::client::*;
 use crate::verifytree::mbtree::*;
 use hex;
-use ring::hmac::Key;
+use merklebtree::iterator::prev;
+use merklebtree::merklebtree::{MerkleBTree, Nodes};
 use merklebtree::node::Node;
+use merklebtree::sgxdb;
 use merklebtree::traits::CalculateHash;
 use parking_lot::RwLock;
+use ring::hmac::Key;
 use ring::{digest, hmac, rand};
 use rocksdb::{DBVector, DB};
 use serde::{Deserialize, Serialize};
@@ -17,6 +20,8 @@ pub struct server {
     hmac_key: Key,
     present_mbtree: Merklebtree,
     deleted_mbtree: Merklebtree,
+    sgx_present_root_node: Node<key_version>,
+    sgx_delete_root_node: Node<key_version>,
 }
 
 //hmacPayload is used to compute hmac
@@ -52,12 +57,12 @@ pub struct mbtree_payload {
 }
 
 impl server {
-    fn put(&mut self, key: String, value: String) {
+    fn db_put(&mut self, key: String, value: String) {
         let db = self.db_handler.clone();
         db.write().put(key.as_bytes(), value.as_bytes()).unwrap();
     }
 
-    fn get(&mut self, key: String) -> String {
+    fn db_get(&mut self, key: String) -> String {
         let db = self.db_handler.clone();
         let db_read = db.read();
         let r = db.read().get(key.as_str());
@@ -70,10 +75,140 @@ impl server {
         }
     }
 
-    fn delete(&mut self, key: String) {
+    fn db_delete(&mut self, key: String) {
         let db = self.db_handler.clone();
         db.write().delete(key.as_bytes()).unwrap();
     }
+
+    fn present_search(&mut self, key: String) -> search_result {
+        let (get_result, mut present_subnodes) = self.present_mbtree.search(key);
+        if get_result.existed {
+            present_subnodes.nodes_map.remove(&0).unwrap();
+            present_subnodes
+                .nodes_map
+                .insert(0, self.sgx_present_root_node.clone());
+            sgxdb::verify_subnodes_hash(&present_subnodes);
+        }
+        get_result
+    }
+
+    fn delete_search(&mut self, key: String) -> search_result {
+        let (get_result, mut delete_subnodes) = self.deleted_mbtree.search(key);
+        if get_result.existed {
+            delete_subnodes.nodes_map.remove(&0).unwrap();
+            delete_subnodes
+                .nodes_map
+                .insert(0, self.sgx_delete_root_node.clone());
+            sgxdb::verify_subnodes_hash(&delete_subnodes);
+        }
+        get_result
+    }
+
+    fn present_remove(&mut self, key: String) {
+        let mut present_subnodes = self.present_mbtree.delete(key.clone());
+        if present_subnodes.nodes_map.len() == 0 {
+            return;
+        }
+
+        present_subnodes.nodes_map.remove(&0).unwrap();
+        present_subnodes
+            .nodes_map
+            .insert(0, self.sgx_present_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&present_subnodes);
+
+        let kv = key_version {
+            key: key.clone(),
+            version: 0,
+        };
+
+        self.present_mbtree
+            .mbtree
+            .remove(kv.clone(), &mut present_subnodes);
+
+        let node_hash = self.present_mbtree.nodes.merkleroot();
+        let subnode_hash = present_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_present_root_node = present_subnodes.nodes_map.remove(&0).unwrap();
+    }
+
+    fn delete_remove(&mut self, key: String) {
+        let mut delete_subnodes = self.deleted_mbtree.delete(key.clone());
+        if delete_subnodes.nodes_map.len() == 0 {
+            return;
+        }
+
+        delete_subnodes.nodes_map.remove(&0).unwrap();
+        delete_subnodes
+            .nodes_map
+            .insert(0, self.sgx_delete_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&delete_subnodes);
+        let kv = key_version {
+            key: key.clone(),
+            version: 0,
+        };
+        self.deleted_mbtree
+            .mbtree
+            .remove(kv.clone(), &mut delete_subnodes);
+
+        let node_hash = self.deleted_mbtree.nodes.merkleroot();
+        let subnode_hash = delete_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_delete_root_node = delete_subnodes.nodes_map.remove(&0).unwrap();
+    }
+
+    fn present_build_with_kv(&mut self, kv: key_version) {
+        if self.sgx_present_root_node.content.len() == 0 {
+            self.sgx_present_root_node = Node::new_node(kv.clone(), 0);
+            self.sgx_present_root_node.root_flag = true;
+            self.present_mbtree.build_with_key_value(kv.clone());
+            return;
+        }
+        let mut present_subnodes = self.present_mbtree.build_with_key_value(kv.clone());
+        present_subnodes.nodes_map.remove(&0).unwrap();
+        present_subnodes
+            .nodes_map
+            .insert(0, self.sgx_present_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&present_subnodes);
+        self.present_mbtree
+            .mbtree
+            .put(kv.clone(), &mut present_subnodes);
+
+        let node_hash = self.present_mbtree.nodes.merkleroot();
+        let subnode_hash = present_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_present_root_node = present_subnodes.nodes_map.remove(&0).unwrap();
+    }
+
+    fn delete_build_with_kv(&mut self, kv: key_version) {
+        if self.sgx_delete_root_node.content.len() == 0 {
+            self.sgx_delete_root_node = Node::new_node(kv.clone(), 0);
+            self.sgx_delete_root_node.root_flag = true;
+            self.deleted_mbtree.build_with_key_value(kv.clone());
+            return;
+        }
+        let mut delete_subnodes = self.deleted_mbtree.build_with_key_value(kv.clone());
+        delete_subnodes.nodes_map.remove(&0).unwrap();
+        delete_subnodes
+            .nodes_map
+            .insert(0, self.sgx_present_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&delete_subnodes);
+        self.deleted_mbtree
+            .mbtree
+            .put(kv.clone(), &mut delete_subnodes);
+
+        let node_hash = self.deleted_mbtree.nodes.merkleroot();
+        let subnode_hash = delete_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_delete_root_node = delete_subnodes.nodes_map.remove(&0).unwrap();
+    }
 }
 
 pub fn new_server(key_value: Vec<u8>) -> server {
@@ -90,6 +225,8 @@ pub fn new_server(key_value: Vec<u8>) -> server {
         hmac_key: s_key,
         present_mbtree: new_mbtree(),
         deleted_mbtree: new_mbtree(),
+        sgx_present_root_node: Node::new_empty(0),
+        sgx_delete_root_node: Node::new_empty(0),
     }
 }
 
@@ -117,7 +254,7 @@ impl server {
     }
 
     pub fn veritasdb_get(&mut self, req: request) -> String {
-        let data = self.get(req.key.clone());
+        let data = self.db_get(req.key.clone());
         if data == "".to_string() {
             return String::new();
         }
@@ -131,7 +268,7 @@ impl server {
         let hmac_string = serde_json::to_string(&hmac_data).unwrap();
         let verify_result = self.verify_hmac(hmac_string, sp.tag.clone());
 
-        let sr = self.present_mbtree.search(req.key.clone());
+        let sr = self.present_search(req.key.clone());
         if verify_result && sp.ctr == sr.version {
             return sp.value;
         } else {
@@ -141,7 +278,7 @@ impl server {
     }
 
     pub fn veritasdb_put(&mut self, req: request) {
-        let get_result = self.present_mbtree.search(req.key.clone());
+        let get_result = self.present_search(req.key.clone());
         if get_result.existed {
             let hmac_data = hmac_payload {
                 key: req.key.clone(),
@@ -158,10 +295,10 @@ impl server {
                 ctr: get_result.version + 1,
             };
             let store_string = serde_json::to_string(&store_data).unwrap();
-            self.put(req.key.clone(), store_string.clone());
+            self.db_put(req.key.clone(), store_string.clone());
 
             //update present if there is no error
-            self.present_mbtree.build_with_key_value(key_version {
+            self.present_build_with_kv(key_version {
                 key: req.key,
                 version: get_result.version + 1,
             });
@@ -172,13 +309,13 @@ impl server {
     }
 
     pub fn veritasdb_insert(&mut self, req: request) {
-        let sr = self.present_mbtree.search(req.key.clone());
+        let sr = self.present_search(req.key.clone());
         if sr.existed {
             println!("key existed in present when called insert");
             return;
         } else {
             let mut ctr = 0;
-            let delete_sr = self.deleted_mbtree.search(req.key.clone());
+            let delete_sr = self.delete_search(req.key.clone());
             if delete_sr.existed {
                 ctr = delete_sr.version + 1;
             } else {
@@ -198,26 +335,26 @@ impl server {
                 tag: tag_string,
             };
             let store_string = serde_json::to_string(&store_data).unwrap();
-            self.put(req.key.clone(), store_string);
-            self.present_mbtree.build_with_key_value(key_version {
+            self.db_put(req.key.clone(), store_string);
+            self.present_build_with_kv(key_version {
                 key: req.key.clone(),
                 version: ctr,
             });
-            self.deleted_mbtree.delete(req.key.clone());
+            self.delete_remove(req.key.clone());
         }
     }
 
     pub fn veritasdb_delete(&mut self, req: request) {
-        let get_result = self.get(req.key.clone());
+        let get_result = self.db_get(req.key.clone());
         if get_result == "".to_string() {
         } else {
-            self.delete(req.key.clone());
-            let sr = self.present_mbtree.search(req.key.clone());
-            self.deleted_mbtree.build_with_key_value(key_version {
+            self.db_delete(req.key.clone());
+            let sr = self.present_search(req.key.clone());
+            self.delete_build_with_kv(key_version {
                 key: req.key.clone(),
                 version: sr.version,
             });
-            self.present_mbtree.delete(req.key);
+            self.present_remove(req.key.clone());
         }
     }
 
diff --git a/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs b/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs
index b9f7965..7dbf2d1 100644
--- a/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs
+++ b/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs
@@ -1,4 +1,5 @@
 use merklebtree::merklebtree::{MerkleBTree, Nodes};
+use merklebtree::node::Node;
 use merklebtree::traits::CalculateHash;
 use ring::digest;
 use std::cmp::Ordering;
@@ -52,6 +53,7 @@ impl CalculateHash for key_version {
 pub struct Merklebtree {
     pub mbtree: MerkleBTree,
     pub nodes: Nodes<key_version>,
+    pub root_node: Node<key_version>,
 }
 
 pub fn new_mbtree() -> Merklebtree {
@@ -71,12 +73,13 @@ pub fn new_mbtree() -> Merklebtree {
     Merklebtree {
         mbtree: tree,
         nodes,
+        root_node: Node::new_empty(0),
     }
 }
 
 impl Merklebtree {
-    pub fn search(&mut self, key: String) -> search_result {
-        let (value, found) = self.mbtree.get(
+    pub fn search(&mut self, key: String) -> (search_result, Nodes<key_version>)
{
+        let (value, found, subnodes) = self.mbtree.get_clone(
             key_version {
                 key: key.clone(),
                 version: 0,
@@ -84,38 +87,46 @@ impl Merklebtree {
             &mut self.nodes,
         );
         if found {
-            return search_result {
-                existed: true,
-                key: key,
-                version: value.version,
-            };
+            return (
+                search_result {
+                    existed: true,
+                    key: key,
+                    version: value.version,
+                },
+                subnodes,
+            );
         } else {
-            return search_result {
-                existed: false,
-                key: key,
-                version: -1,
-            };
+            return (
+                search_result {
+                    existed: false,
+                    key: key,
+                    version: -1,
+                },
+                subnodes,
+            );
         }
     }
 
-    pub fn delete(&mut self, key: String) {
-        self.mbtree.remove(
+    pub fn delete(&mut self, key: String) -> Nodes<key_version> {
+        let subnodes = self.mbtree.remove_clone(
             key_version {
                 key: key,
                 version: 0,
             },
             &mut self.nodes,
         );
+        subnodes
     }
 
-    pub fn build_with_key_value(&mut self, kv: key_version) {
+    pub fn build_with_key_value(&mut self, kv: key_version) -> Nodes<key_version>
{
         println!("kv:{:?}", kv);
-        self.mbtree.put(
+        let subnodes = self.mbtree.put_clone(
             key_version {
                 key: kv.key,
                 version: kv.version,
             },
             &mut self.nodes,
         );
+        subnodes
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@mesatee.apache.org
For additional commands, e-mail: commits-help@mesatee.apache.org


Mime
View raw message