From c58f50b2224af507cae76b8e7f53726d1ddd50e8 Mon Sep 17 00:00:00 2001 From: josojo Date: Mon, 14 Jan 2019 07:10:14 +0100 Subject: [PATCH 01/11] initial commit --- Cargo.toml | 12 ++++++ README.md | 40 ++++++++++++++++++++ src/bin/database_setup.rs | 78 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 67 +++++++++++++++++++++++++++++++++ 4 files changed, 197 insertions(+) create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/bin/database_setup.rs create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000..9385e5c40 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "backend_services" +version = "0.1.0" +authors = ["josojo "] +edition = "2018" + +[dependencies] +mongodb = "0.3.2" +serde_json = "1.0" +serde_derive = "1.0" +serde = "1.0" +bson = "0.9.1" diff --git a/README.md b/README.md new file mode 100644 index 000000000..cd259b86c --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# Running pepper with snarks from this repo + + +## Prerequisits: + +(These prerequisits will soon be dockerized) + +Install mongodb("0.3.2") and run it using: + +```sh +sudo systemctl start mongodb + +``` +- do not use any authentification for the database. + + +Install rust & cargo ("1.31") - older versions would not be compatible with newest mongodb drivers. + + + +## Setup: + +In order to load the first data into the data base, run: + + +```sh +cargo run --bin database_setup + +``` + +## Running the code: + +Running + +```sh +cargo run --bin backend_services + +``` + +will start a listener for the data base and run some c++ executions based on the data. diff --git a/src/bin/database_setup.rs b/src/bin/database_setup.rs new file mode 100644 index 000000000..290f92e29 --- /dev/null +++ b/src/bin/database_setup.rs @@ -0,0 +1,78 @@ +const ACCOUNTS: i32 = 8; +const TOKENS: i32 = 4; +const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; + +extern crate serde_json; +extern crate serde; + +#[macro_use] +extern crate serde_derive; + +#[derive(Serialize, Deserialize)] +struct State { + curState: String, + prevState: String, + nextStates: String, + slot: i32, + balances: [i64; SIZE_BALANCE] +} + +extern crate mongodb; +use mongodb::{bson, doc}; +use mongodb::{Client, ThreadedClient}; +use mongodb::db::ThreadedDatabase; + + +fn main() { + + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let coll = client.db("dfusion").collection("CurrentState"); + + let doc = doc! { + "CurrentState": "0000000000000000000000000000000000000000", + }; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(doc.clone(), None) + .ok().expect("Failed to insert CurrentState."); + + let coll = client.db("dfusion").collection("State"); + + + let state = State { + curState: "0000000000000000000000000000000000000000".to_owned(), + prevState: "0000000000000000000000000000000000000000".to_owned(), + nextStates: "0000000000000000000000000000000000000000".to_owned(), + slot: 0, + balances: [0; SIZE_BALANCE] + }; + + let document = serde_json::to_string(&state).ok().expect("Failed to convert first State"); + + println!("{}", document); + + // let document: String = String::from(r#"{"curState":"0000000000000000000000000000000000000000","prevState":"0000000000000000000000000000000000000000","nextStates":"0000000000000000000000000000000000000000","slot":0,"balances":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}"#); + // let temp = doc!( r#document#); + let temp = doc! {"curState":"0000000000000000000000000000000000000000","prevState":"0000000000000000000000000000000000000000","nextStates":"0000000000000000000000000000000000000000","slot":0,"balances":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(temp.clone(), None) + .ok().expect("Failed to insert CurrentState."); + + let coll = client.db("dfusion").collection("Deposits"); + + let doc2 = doc! { + "depositHash": "0000000000000000000000000000000000000000", + "depositIndex": "0000000000000000000000000000000000000000", + "slot": 1, + "addressIndex": 0, + "tokenIndex": 1, + "amount": 55465465, + }; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(doc2.clone(), None) + .ok().expect("Failed to insert Deposit"); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 000000000..c6b162f77 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,67 @@ +extern crate serde_json; +extern crate serde; + + +const ACCOUNTS: i32 = 8; +const TOKENS: i32 = 4; +const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; + +#[macro_use] +extern crate serde_derive; + +#[derive(Serialize, Deserialize)] +struct State { + curState: String, + prevState: String, + nextStates: String, + slot: i32, + balances: [i64; SIZE_BALANCE] +} +/* +impl State { + fn getNextState(&self) -> std::string::String { + self.nextStates + } +}*/ +extern crate mongodb; +use mongodb::{Bson, bson, doc}; +use mongodb::{Client, ThreadedClient}; +use mongodb::db::ThreadedDatabase; + + +fn main() { + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let coll = client.db("dfusion").collection("CurrentState"); + + + // Find the document and receive a cursor + let mut cursor = coll.find(None, None) + .ok().expect("Failed to execute find."); + + let item = cursor.next(); + + let cur_state; + // cursor.next() returns an Option> + match item { + Some(Ok(doc)) => match doc.get("CurrentState") { + Some(&Bson::String(ref CurrentState)) => {cur_state = &CurrentState; println!("{}", cur_state)}, + _ => panic!("Expected title to be a string!"), + }, + Some(Err(_)) => panic!("Failed to get next from server!"), + None => panic!("Server returned no results!"), + } + + let coll = client.db("dfusion").collection("State"); + + + let cursor = match coll.find(Some(doc! { "curState" : "0x0000000000000000000000000000000000000000", },) , None) { + Ok(cursor) => cursor, + Err(error) => panic!("The following error occured: {}", error) + }; + + for doc in cursor { + println!("{}", doc.unwrap()); + } +} From 7413042db563cb56c26e57be1076521ed1e1550b Mon Sep 17 00:00:00 2001 From: josojo Date: Tue, 15 Jan 2019 12:59:32 +0100 Subject: [PATCH 02/11] updates as suggested by comments --- driver/Cargo.lock | 700 ++++++++++++++++++++++ Cargo.toml => driver/Cargo.toml | 6 +- README.md => driver/README.md | 0 {src => driver/src}/bin/database_setup.rs | 30 +- driver/src/bin/global_helpers.rs | 14 + {src => driver/src}/main.rs | 29 +- 6 files changed, 733 insertions(+), 46 deletions(-) create mode 100644 driver/Cargo.lock rename Cargo.toml => driver/Cargo.toml (77%) rename README.md => driver/README.md (100%) rename {src => driver/src}/bin/database_setup.rs (83%) create mode 100644 driver/src/bin/global_helpers.rs rename {src => driver/src}/main.rs (71%) diff --git a/driver/Cargo.lock b/driver/Cargo.lock new file mode 100644 index 000000000..e404fd98f --- /dev/null +++ b/driver/Cargo.lock @@ -0,0 +1,700 @@ +[[package]] +name = "aho-corasick" +version = "0.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "arrayref" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "base64" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "block-buffer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bson" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hostname 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bson" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hostname 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "md5 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "try_from 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bufstream" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byte-tools" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byteorder" +version = "1.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "constant_time_eq" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "crypto-mac" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "data-encoding" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "digest" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "driver" +version = "0.1.0" +dependencies = [ + "bson 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "mongodb 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", + "pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "gcc" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "generic-array" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hex" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "hmac" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crypto-mac 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hostname" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "winutil 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "indexmap" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "itoa" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "lazy_static" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "linked-hash-map" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "linked-hash-map" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "md-5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "md5" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "mongodb" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bson 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bufstream 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hmac 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "md-5 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pbkdf2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "semver 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "separator 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textnonce 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-integer" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pairing" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pbkdf2" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "crypto-mac 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", + "hmac 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro2" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "redox_syscall" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "regex" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ryu" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "safemem" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "scan_fmt" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "separator" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "1.0.84" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.84" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_json" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sha-1" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sha2" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "0.15.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "textnonce" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.50 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "try_from" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "typenum" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ucd-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "utf8-ranges" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "version_check" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winutil" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" +"checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" +"checksum base64 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "489d6c0ed21b11d038c31b6ceccca973e65d73ba3bd8ecb9a2babf5546164643" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" +"checksum bson 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8984b7b33b1f8ac97468df3cefa76c7035abb0786473aa2a437dea0c72855702" +"checksum bson 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f06c2aee15fed63d9b10f11faef009646388d6d034751c889b9a73246751328c" +"checksum bufstream 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "40e38929add23cdf8a366df9b0e088953150724bcbe5fc330b0d8eb3b328eec8" +"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" +"checksum byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "94f88df23a25417badc922ab0f5716cc1330e87f71ddd9203b3a3ccd9cedf75d" +"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" +"checksum crypto-mac 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7afa06d05a046c7a47c3a849907ec303504608c927f4e85f7bfff22b7180d971" +"checksum data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" +"checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" +"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" +"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" +"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +"checksum hex 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d6a22814455d41612f41161581c2883c0c6a1c41852729b17d5ed88f01e153aa" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" +"checksum hmac 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "733e1b3ac906631ca01ebb577e9bb0f5e37a454032b9036b5eaea4013ed6f99a" +"checksum hostname 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "21ceb46a83a85e824ef93669c8b390009623863b5c195d1ba747292c0c72f94e" +"checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" +"checksum itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" +"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" +"checksum libc 0.2.46 (registry+https://github.com/rust-lang/crates.io-index)" = "023a4cd09b2ff695f9734c1934145a315594b7986398496841c7031a5a1bbdbd" +"checksum linked-hash-map 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd" +"checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e" +"checksum md-5 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9402eaae33a9e144ce18ef488a0e4ca19869673c7bcdbbfe2030fdc3f84211cd" +"checksum md5 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "79c56d6a0b07f9e19282511c83fc5b086364cbae4ba8c7d5f190c3d9b0425a48" +"checksum memchr 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db4c41318937f6e76648f42826b1d9ade5c09cafb5aef7e351240a70f39206e9" +"checksum mongodb 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)" = "d558c8c1ee6140954f82b53558135bb9ecb7b258e10dbd7206d985d134d388e0" +"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" +"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" +"checksum pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ceda21136251c6d5a422d3d798d8ac22515a6e8d3521bb60c59a8349d36d0d57" +"checksum pbkdf2 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0c09cddfbfc98de7f76931acf44460972edb4023eb14d0c6d4018800e552d8e0" +"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" +"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" +"checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1" +"checksum rand 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "dee497e66d8d76bf08ce20c8d36e16f93749ab0bf89975b4f8ae5cee660c2da2" +"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" +"checksum rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1961a422c4d189dfb50ffa9320bf1f2a9bd54ecb92792fb9477f99a1045f3372" +"checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +"checksum redox_syscall 0.1.50 (registry+https://github.com/rust-lang/crates.io-index)" = "52ee9a534dc1301776eff45b4fa92d2c39b1d8c3d3357e6eb593e0d795506fc2" +"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" +"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" +"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum ryu 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9e9b8cde282a9fe6a42dd4681319bfb63f121b8a8ee9439c6f4107e58a46f7" +"checksum safemem 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8dca453248a96cb0749e36ccdfe2b0b4e54a61bfef89fb97ec621eb8e0a93dd9" +"checksum scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b87497427f9fbe539ee6b9626f5a5e899331fdf1c1d62f14c637a462969db30" +"checksum semver 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bee2bc909ab2d8d60dab26e8cad85b25d795b14603a0dcb627b78b9d30b6454b" +"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum separator 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7acc4d818f952ed02e7911df5da8098c8b00a3c5ba2832e035a750b56e8fc32b" +"checksum serde 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)" = "0e732ed5a5592c17d961555e3b552985baf98d50ce418b7b655f31f6ba7eb1b7" +"checksum serde_derive 1.0.84 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d6115a3ca25c224e409185325afc16a0d5aaaabc15c42b09587d6f1ba39a5b" +"checksum serde_json 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)" = "dfb1277d4d0563e4593e0b8b5d23d744d277b55d2bc0bf1c38d0d8a6589d38aa" +"checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded" +"checksum sha2 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9eb6be24e4c23a84d7184280d2722f7f2731fcdd4a9d886efbfe4413e4847ea0" +"checksum syn 0.15.24 (registry+https://github.com/rust-lang/crates.io-index)" = "734ecc29cd36e8123850d9bf21dfd62ef8300aaa8f879aabaa899721808be37c" +"checksum textnonce 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "df4033262e39249fc34cad20ce07fa03b8181e33e65d854ee1afb229b28974c6" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum try_from 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "923a7ee3e97dbfe8685261beb4511cc9620a1252405d02693d43169729570111" +"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" +"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" +"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum winutil 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7daf138b6b14196e3830a588acf1e86966c694d3e8fb026fb105b8b5dca07e6e" diff --git a/Cargo.toml b/driver/Cargo.toml similarity index 77% rename from Cargo.toml rename to driver/Cargo.toml index 9385e5c40..ac669f9a4 100644 --- a/Cargo.toml +++ b/driver/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "backend_services" +name = "driver" version = "0.1.0" authors = ["josojo "] edition = "2018" @@ -9,4 +9,6 @@ mongodb = "0.3.2" serde_json = "1.0" serde_derive = "1.0" serde = "1.0" -bson = "0.9.1" +bson = "0.9.1" +pairing = "0.14" + diff --git a/README.md b/driver/README.md similarity index 100% rename from README.md rename to driver/README.md diff --git a/src/bin/database_setup.rs b/driver/src/bin/database_setup.rs similarity index 83% rename from src/bin/database_setup.rs rename to driver/src/bin/database_setup.rs index 290f92e29..eb4932717 100644 --- a/src/bin/database_setup.rs +++ b/driver/src/bin/database_setup.rs @@ -1,26 +1,16 @@ -const ACCOUNTS: i32 = 8; -const TOKENS: i32 = 4; -const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; - -extern crate serde_json; -extern crate serde; - #[macro_use] extern crate serde_derive; -#[derive(Serialize, Deserialize)] -struct State { - curState: String, - prevState: String, - nextStates: String, - slot: i32, - balances: [i64; SIZE_BALANCE] -} +mod global_helpers; +extern crate serde_json; +extern crate serde; extern crate mongodb; + use mongodb::{bson, doc}; use mongodb::{Client, ThreadedClient}; use mongodb::db::ThreadedDatabase; +use pairing::{ PrimeField }; fn main() { @@ -41,12 +31,12 @@ fn main() { let coll = client.db("dfusion").collection("State"); - let state = State { + let state = global_helpers::State { curState: "0000000000000000000000000000000000000000".to_owned(), prevState: "0000000000000000000000000000000000000000".to_owned(), - nextStates: "0000000000000000000000000000000000000000".to_owned(), + nextState: "0000000000000000000000000000000000000000".to_owned(), slot: 0, - balances: [0; SIZE_BALANCE] + balances: [0; global_helpers::SIZE_BALANCE] }; let document = serde_json::to_string(&state).ok().expect("Failed to convert first State"); @@ -67,8 +57,8 @@ fn main() { "depositHash": "0000000000000000000000000000000000000000", "depositIndex": "0000000000000000000000000000000000000000", "slot": 1, - "addressIndex": 0, - "tokenIndex": 1, + "addressId": 1, + "tokenId": 1, "amount": 55465465, }; diff --git a/driver/src/bin/global_helpers.rs b/driver/src/bin/global_helpers.rs new file mode 100644 index 000000000..f43c9f0a6 --- /dev/null +++ b/driver/src/bin/global_helpers.rs @@ -0,0 +1,14 @@ +pub const ACCOUNTS: i32 = 8; +pub const TOKENS: i32 = 8; +pub const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; + +use pairing::{ PrimeField }; + +#[derive(Serialize, Deserialize)] +pub struct State { + pub curState: String, + pub prevState: String, + pub nextState: String, + pub slot: i32, + pub balances: [ i32; SIZE_BALANCE], +} \ No newline at end of file diff --git a/src/main.rs b/driver/src/main.rs similarity index 71% rename from src/main.rs rename to driver/src/main.rs index c6b162f77..53dd9e319 100644 --- a/src/main.rs +++ b/driver/src/main.rs @@ -1,28 +1,8 @@ -extern crate serde_json; -extern crate serde; - - -const ACCOUNTS: i32 = 8; -const TOKENS: i32 = 4; -const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; - #[macro_use] extern crate serde_derive; -#[derive(Serialize, Deserialize)] -struct State { - curState: String, - prevState: String, - nextStates: String, - slot: i32, - balances: [i64; SIZE_BALANCE] -} -/* -impl State { - fn getNextState(&self) -> std::string::String { - self.nextStates - } -}*/ +extern crate serde_json; +extern crate serde; extern crate mongodb; use mongodb::{Bson, bson, doc}; use mongodb::{Client, ThreadedClient}; @@ -43,6 +23,7 @@ fn main() { let item = cursor.next(); let cur_state; + // cursor.next() returns an Option> match item { Some(Ok(doc)) => match doc.get("CurrentState") { @@ -55,8 +36,8 @@ fn main() { let coll = client.db("dfusion").collection("State"); - - let cursor = match coll.find(Some(doc! { "curState" : "0x0000000000000000000000000000000000000000", },) , None) { + // let cursor = match coll.find(Some(doc!(cur_state) ) , None) { + let cursor = match coll.find(Some(doc! { "curState": "0000000000000000000000000000000000000000", },) , None) { Ok(cursor) => cursor, Err(error) => panic!("The following error occured: {}", error) }; From 80633219f6664dc875eeb60dedc36efc7bf1076b Mon Sep 17 00:00:00 2001 From: josojo Date: Wed, 16 Jan 2019 10:07:11 +0100 Subject: [PATCH 03/11] Felix showed me how to create mongodb::bson::Documents --- driver/Cargo.toml | 4 ++-- driver/src/bin/database_setup.rs | 15 +++++++-------- driver/src/bin/global_helpers.rs | 14 -------------- driver/src/main.rs | 25 ++++++++++++++----------- 4 files changed, 23 insertions(+), 35 deletions(-) delete mode 100644 driver/src/bin/global_helpers.rs diff --git a/driver/Cargo.toml b/driver/Cargo.toml index ac669f9a4..23b871694 100644 --- a/driver/Cargo.toml +++ b/driver/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "driver" version = "0.1.0" -authors = ["josojo "] +authors = ["josojo "] edition = "2018" [dependencies] @@ -11,4 +11,4 @@ serde_derive = "1.0" serde = "1.0" bson = "0.9.1" pairing = "0.14" - + diff --git a/driver/src/bin/database_setup.rs b/driver/src/bin/database_setup.rs index eb4932717..c711899c9 100644 --- a/driver/src/bin/database_setup.rs +++ b/driver/src/bin/database_setup.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate serde_derive; -mod global_helpers; +mod models; extern crate serde_json; extern crate serde; @@ -31,22 +31,21 @@ fn main() { let coll = client.db("dfusion").collection("State"); - let state = global_helpers::State { + let state = models::State { curState: "0000000000000000000000000000000000000000".to_owned(), prevState: "0000000000000000000000000000000000000000".to_owned(), nextState: "0000000000000000000000000000000000000000".to_owned(), slot: 0, - balances: [0; global_helpers::SIZE_BALANCE] + balances: [0; models::SIZE_BALANCE] }; let document = serde_json::to_string(&state).ok().expect("Failed to convert first State"); println!("{}", document); - - // let document: String = String::from(r#"{"curState":"0000000000000000000000000000000000000000","prevState":"0000000000000000000000000000000000000000","nextStates":"0000000000000000000000000000000000000000","slot":0,"balances":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}"#); - // let temp = doc!( r#document#); - let temp = doc! {"curState":"0000000000000000000000000000000000000000","prevState":"0000000000000000000000000000000000000000","nextStates":"0000000000000000000000000000000000000000","slot":0,"balances":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}; - + let json: serde_json::Value = serde_json::to_value(&state).expect("Failed to parse json"); + let bson = json.into(); + let temp: bson::Document = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + // Insert document into 'dfusion.CurrentState' collection coll.insert_one(temp.clone(), None) .ok().expect("Failed to insert CurrentState."); diff --git a/driver/src/bin/global_helpers.rs b/driver/src/bin/global_helpers.rs deleted file mode 100644 index f43c9f0a6..000000000 --- a/driver/src/bin/global_helpers.rs +++ /dev/null @@ -1,14 +0,0 @@ -pub const ACCOUNTS: i32 = 8; -pub const TOKENS: i32 = 8; -pub const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; - -use pairing::{ PrimeField }; - -#[derive(Serialize, Deserialize)] -pub struct State { - pub curState: String, - pub prevState: String, - pub nextState: String, - pub slot: i32, - pub balances: [ i32; SIZE_BALANCE], -} \ No newline at end of file diff --git a/driver/src/main.rs b/driver/src/main.rs index 53dd9e319..8b1ca1ee6 100644 --- a/driver/src/main.rs +++ b/driver/src/main.rs @@ -15,29 +15,32 @@ fn main() { let coll = client.db("dfusion").collection("CurrentState"); - // Find the document and receive a cursor let mut cursor = coll.find(None, None) .ok().expect("Failed to execute find."); - - let item = cursor.next(); - - let cur_state; - + + let mut cur_state; // cursor.next() returns an Option> - match item { + let item = cursor.next(); + + let cur_state = match item { Some(Ok(doc)) => match doc.get("CurrentState") { - Some(&Bson::String(ref CurrentState)) => {cur_state = &CurrentState; println!("{}", cur_state)}, - _ => panic!("Expected title to be a string!"), + Some(&Bson::String(ref CurrentState)) => {&CurrentState;}, + _ => panic!("Expected item to be a string!"), }, Some(Err(_)) => panic!("Failed to get next from server!"), None => panic!("Server returned no results!"), - } + }; + // convert value to mongodb::bson::Document + let json: serde_json::Value = serde_json::to_value(&cur_state).expect("Failed to parse json"); + let bson = json.into(); + let temp: bson::Document = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + let coll = client.db("dfusion").collection("State"); // let cursor = match coll.find(Some(doc!(cur_state) ) , None) { - let cursor = match coll.find(Some(doc! { "curState": "0000000000000000000000000000000000000000", },) , None) { + let cursor = match coll.find(Some(temp) , None) { Ok(cursor) => cursor, Err(error) => panic!("The following error occured: {}", error) }; From 4c4ddf8507ed9e3f83585a8616a46bad16fa8b90 Mon Sep 17 00:00:00 2001 From: josojo Date: Thu, 17 Jan 2019 14:27:09 +0100 Subject: [PATCH 04/11] able to get the current state and current deposits into rust structs --- driver/README.md | 2 +- driver/src/bin/database_setup.rs | 6 ++-- driver/src/main.rs | 47 -------------------------------- 3 files changed, 3 insertions(+), 52 deletions(-) diff --git a/driver/README.md b/driver/README.md index cd259b86c..825971130 100644 --- a/driver/README.md +++ b/driver/README.md @@ -33,7 +33,7 @@ cargo run --bin database_setup Running ```sh -cargo run --bin backend_services +cargo run --bin driver ``` diff --git a/driver/src/bin/database_setup.rs b/driver/src/bin/database_setup.rs index c711899c9..cebc07b67 100644 --- a/driver/src/bin/database_setup.rs +++ b/driver/src/bin/database_setup.rs @@ -36,16 +36,14 @@ fn main() { prevState: "0000000000000000000000000000000000000000".to_owned(), nextState: "0000000000000000000000000000000000000000".to_owned(), slot: 0, - balances: [0; models::SIZE_BALANCE] + balances: vec![0, 0, 0, 0] }; - let document = serde_json::to_string(&state).ok().expect("Failed to convert first State"); - - println!("{}", document); let json: serde_json::Value = serde_json::to_value(&state).expect("Failed to parse json"); let bson = json.into(); let temp: bson::Document = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + println!("Data to be inserted{:?}", temp ); // Insert document into 'dfusion.CurrentState' collection coll.insert_one(temp.clone(), None) .ok().expect("Failed to insert CurrentState."); diff --git a/driver/src/main.rs b/driver/src/main.rs index 8b1ca1ee6..3783460ad 100644 --- a/driver/src/main.rs +++ b/driver/src/main.rs @@ -1,51 +1,4 @@ -#[macro_use] -extern crate serde_derive; - -extern crate serde_json; -extern crate serde; -extern crate mongodb; -use mongodb::{Bson, bson, doc}; -use mongodb::{Client, ThreadedClient}; -use mongodb::db::ThreadedDatabase; - fn main() { - let client = Client::connect("localhost", 27017) - .expect("Failed to initialize standalone client."); - - let coll = client.db("dfusion").collection("CurrentState"); - - // Find the document and receive a cursor - let mut cursor = coll.find(None, None) - .ok().expect("Failed to execute find."); - - let mut cur_state; - // cursor.next() returns an Option> - let item = cursor.next(); - let cur_state = match item { - Some(Ok(doc)) => match doc.get("CurrentState") { - Some(&Bson::String(ref CurrentState)) => {&CurrentState;}, - _ => panic!("Expected item to be a string!"), - }, - Some(Err(_)) => panic!("Failed to get next from server!"), - None => panic!("Server returned no results!"), - }; - - // convert value to mongodb::bson::Document - let json: serde_json::Value = serde_json::to_value(&cur_state).expect("Failed to parse json"); - let bson = json.into(); - let temp: bson::Document = mongodb::from_bson(bson).expect("Failed to convert bson to document"); - - let coll = client.db("dfusion").collection("State"); - - // let cursor = match coll.find(Some(doc!(cur_state) ) , None) { - let cursor = match coll.find(Some(temp) , None) { - Ok(cursor) => cursor, - Err(error) => panic!("The following error occured: {}", error) - }; - - for doc in cursor { - println!("{}", doc.unwrap()); - } } From ca5eaf3fd647056bcad4db6968d7b13a42237f7e Mon Sep 17 00:00:00 2001 From: josojo Date: Thu, 17 Jan 2019 14:43:15 +0100 Subject: [PATCH 05/11] adding missing files --- driver/src/bin/fetch_db.rs | 89 ++++++++++++++++++++++++++++++++++++++ driver/src/bin/models.rs | 22 ++++++++++ 2 files changed, 111 insertions(+) create mode 100644 driver/src/bin/fetch_db.rs create mode 100644 driver/src/bin/models.rs diff --git a/driver/src/bin/fetch_db.rs b/driver/src/bin/fetch_db.rs new file mode 100644 index 000000000..592610c95 --- /dev/null +++ b/driver/src/bin/fetch_db.rs @@ -0,0 +1,89 @@ +#[macro_use] +extern crate serde_derive; + +mod models; + +extern crate serde_json; +extern crate serde; +extern crate mongodb; +use std::io; +use mongodb::{Bson, bson, doc}; +use mongodb::{Client, ThreadedClient}; +use mongodb::db::ThreadedDatabase; +use pairing::{ PrimeField }; + + +fn getCurrentBalances() -> Result{ + + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let coll = client.db("dfusion").collection("CurrentState"); + + // Find the document and receive a cursor + let mut cursor = coll.find(None, None) + .ok().expect("Failed to execute find."); + + let mut docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); + + if docs.len() !=1 { + println!("Error: There should be only one CurrentState"); + } + + let json: serde_json::Value = serde_json::to_value(&docs[0]).expect("Failed to parse json");; + let mut query=String::from(r#" { "curState": "#); + let t=json["CurrentState"].to_string(); + query.push_str( &t ); + query.push_str(" }"); + let v: serde_json::Value = serde_json::from_str(&query).expect("Failed to parse query to serde_json::value"); + let bson = v.into(); + let mut _temp: bson::ordered::OrderedDocument = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + + let coll = client.db("dfusion").collection("State"); + + let mut cursor = coll.find(Some(_temp) , None) + .ok().expect("Failed to execute find."); + + let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); + + let json: String = serde_json::to_string(&docs[0]).expect("Failed to parse json"); + + let deserialized: models::State = serde_json::from_str(&json).unwrap(); + Ok(deserialized) +} + + +fn getDepositsOfSlot(slot: i32) -> Result, io::Error>{ + + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let mut query=String::from(r#" { "slot": "#); + let t=slot.to_string(); + query.push_str( &t ); + query.push_str(" }"); + let v: serde_json::Value = serde_json::from_str(&query).expect("Failed to parse query to serde_json::value"); + let bson = v.into(); + let mut _temp: bson::ordered::OrderedDocument = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + + let coll = client.db("dfusion").collection("Deposits"); + + let mut cursor = coll.find(Some(_temp) , None) + .ok().expect("Failed to execute find."); + + let docs: Vec<_> = cursor.map(|doc| doc.unwrap()) + .map(|doc| serde_json::to_string(&doc) + .map(|json| serde_json::from_str(&json).unwrap()) + .expect("Failed to parse json")).collect(); + Ok(docs) +} + + +fn main() { + + let state = getCurrentBalances().expect("Could not get the current state of the chain"); + println!("Current balances are: {:?}", state.balances); + let deposits = getDepositsOfSlot(state.slot + 1); + println!("Current deposit hash: {:?}", deposits.unwrap()[0].depositHash); + +} diff --git a/driver/src/bin/models.rs b/driver/src/bin/models.rs new file mode 100644 index 000000000..b554c00c1 --- /dev/null +++ b/driver/src/bin/models.rs @@ -0,0 +1,22 @@ +pub const ACCOUNTS: i32 = 2; +pub const TOKENS: i32 = 2; +pub const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; + +#[derive(Serialize, Deserialize)] +pub struct State { + pub curState: String, + pub prevState: String, + pub nextState: String, + pub slot: i32, + pub balances: Vec, +} + +#[derive(Serialize, Deserialize)] +pub struct Deposits { + pub depositHash: String, + depositIndex: String, + slot: i32, + addressId: i32, + tokenId: i32, + amount: i32, +} \ No newline at end of file From b6e8a6b7a59c5ccd218faeff390414c722f6e908 Mon Sep 17 00:00:00 2001 From: josojo Date: Thu, 17 Jan 2019 16:36:21 +0100 Subject: [PATCH 06/11] apply deposits is working --- driver/src/bin/database_setup.rs | 12 ++++++++++++ driver/src/bin/fetch_db.rs | 31 +++++++++++++++++++------------ driver/src/bin/models.rs | 12 ++++++------ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/driver/src/bin/database_setup.rs b/driver/src/bin/database_setup.rs index cebc07b67..adf05c56e 100644 --- a/driver/src/bin/database_setup.rs +++ b/driver/src/bin/database_setup.rs @@ -62,4 +62,16 @@ fn main() { // Insert document into 'dfusion.CurrentState' collection coll.insert_one(doc2.clone(), None) .ok().expect("Failed to insert Deposit"); + let doc2 = doc! { + "depositHash": "0000000000000000000000000000000000000000", + "depositIndex": "0000000000000000000000000000000000000000", + "slot": 1, + "addressId": 1, + "tokenId": 0, + "amount": 65465, + }; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(doc2.clone(), None) + .ok().expect("Failed to insert Deposit"); } diff --git a/driver/src/bin/fetch_db.rs b/driver/src/bin/fetch_db.rs index 592610c95..08a73bd67 100644 --- a/driver/src/bin/fetch_db.rs +++ b/driver/src/bin/fetch_db.rs @@ -7,13 +7,11 @@ extern crate serde_json; extern crate serde; extern crate mongodb; use std::io; -use mongodb::{Bson, bson, doc}; +use mongodb::bson; use mongodb::{Client, ThreadedClient}; use mongodb::db::ThreadedDatabase; -use pairing::{ PrimeField }; - -fn getCurrentBalances() -> Result{ +fn get_current_balances() -> Result{ let client = Client::connect("localhost", 27017) .expect("Failed to initialize standalone client."); @@ -21,10 +19,10 @@ fn getCurrentBalances() -> Result{ let coll = client.db("dfusion").collection("CurrentState"); // Find the document and receive a cursor - let mut cursor = coll.find(None, None) + let cursor = coll.find(None, None) .ok().expect("Failed to execute find."); - let mut docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); + let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); if docs.len() !=1 { println!("Error: There should be only one CurrentState"); @@ -41,7 +39,7 @@ fn getCurrentBalances() -> Result{ let coll = client.db("dfusion").collection("State"); - let mut cursor = coll.find(Some(_temp) , None) + let cursor = coll.find(Some(_temp) , None) .ok().expect("Failed to execute find."); let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); @@ -53,7 +51,7 @@ fn getCurrentBalances() -> Result{ } -fn getDepositsOfSlot(slot: i32) -> Result, io::Error>{ +fn get_deposits_of_slot(slot: i32) -> Result, io::Error>{ let client = Client::connect("localhost", 27017) .expect("Failed to initialize standalone client."); @@ -68,7 +66,7 @@ fn getDepositsOfSlot(slot: i32) -> Result, io::Error>{ let coll = client.db("dfusion").collection("Deposits"); - let mut cursor = coll.find(Some(_temp) , None) + let cursor = coll.find(Some(_temp) , None) .ok().expect("Failed to execute find."); let docs: Vec<_> = cursor.map(|doc| doc.unwrap()) @@ -78,12 +76,21 @@ fn getDepositsOfSlot(slot: i32) -> Result, io::Error>{ Ok(docs) } +fn apply_deposits(state: &mut models::State, deposits: &Vec) -> Result { + for i in deposits { + state.balances[ (i.addressId * models::ACCOUNTS + i.tokenId) as usize] += i.amount; + } + Ok(state.clone()) +} fn main() { - let state = getCurrentBalances().expect("Could not get the current state of the chain"); + let mut state = get_current_balances().expect("Could not get the current state of the chain"); println!("Current balances are: {:?}", state.balances); - let deposits = getDepositsOfSlot(state.slot + 1); - println!("Current deposit hash: {:?}", deposits.unwrap()[0].depositHash); + let deposits = get_deposits_of_slot(state.slot + 1).unwrap(); + println!("Current deposit hash: {:?}", deposits[0].depositHash); + + state = apply_deposits(&mut state, &deposits).ok().expect("Deposits could not be applied"); + println!("After the deposit the new balances are: {:?}", state.balances); } diff --git a/driver/src/bin/models.rs b/driver/src/bin/models.rs index b554c00c1..987bf3098 100644 --- a/driver/src/bin/models.rs +++ b/driver/src/bin/models.rs @@ -2,7 +2,7 @@ pub const ACCOUNTS: i32 = 2; pub const TOKENS: i32 = 2; pub const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] pub struct State { pub curState: String, pub prevState: String, @@ -14,9 +14,9 @@ pub struct State { #[derive(Serialize, Deserialize)] pub struct Deposits { pub depositHash: String, - depositIndex: String, - slot: i32, - addressId: i32, - tokenId: i32, - amount: i32, + pub depositIndex: String, + pub slot: i32, + pub addressId: i32, + pub tokenId: i32, + pub amount: i64, } \ No newline at end of file From b5d5a8fe0141874fcb13e28a47cd6e92cb95092a Mon Sep 17 00:00:00 2001 From: josojo Date: Thu, 17 Jan 2019 16:45:21 +0100 Subject: [PATCH 07/11] clearing some warning --- driver/src/bin/fetch_db.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/driver/src/bin/fetch_db.rs b/driver/src/bin/fetch_db.rs index 08a73bd67..c63d237c9 100644 --- a/driver/src/bin/fetch_db.rs +++ b/driver/src/bin/fetch_db.rs @@ -76,12 +76,6 @@ fn get_deposits_of_slot(slot: i32) -> Result, io::Error> Ok(docs) } -fn apply_deposits(state: &mut models::State, deposits: &Vec) -> Result { - for i in deposits { - state.balances[ (i.addressId * models::ACCOUNTS + i.tokenId) as usize] += i.amount; - } - Ok(state.clone()) -} fn main() { @@ -90,7 +84,4 @@ fn main() { let deposits = get_deposits_of_slot(state.slot + 1).unwrap(); println!("Current deposit hash: {:?}", deposits[0].depositHash); - state = apply_deposits(&mut state, &deposits).ok().expect("Deposits could not be applied"); - println!("After the deposit the new balances are: {:?}", state.balances); - } From 9c7f235f4d3c9e389e4c3d3a08b0b68eda73171f Mon Sep 17 00:00:00 2001 From: josojo Date: Thu, 17 Jan 2019 17:32:10 +0100 Subject: [PATCH 08/11] making an accountID ipo addressID --- driver/src/bin/models.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver/src/bin/models.rs b/driver/src/bin/models.rs index 987bf3098..b56649e96 100644 --- a/driver/src/bin/models.rs +++ b/driver/src/bin/models.rs @@ -16,7 +16,7 @@ pub struct Deposits { pub depositHash: String, pub depositIndex: String, pub slot: i32, - pub addressId: i32, + pub accountId: i32, pub tokenId: i32, pub amount: i64, } \ No newline at end of file From 86a75541310b9aa9e6218195890275bb5971cc62 Mon Sep 17 00:00:00 2001 From: josojo Date: Fri, 18 Jan 2019 18:14:17 +0100 Subject: [PATCH 09/11] restructed models into own lib --- driver/Cargo.toml | 9 ++++ driver/src/bin/database_setup.rs | 77 ---------------------------- driver/src/bin/fetch_db.rs | 87 -------------------------------- driver/src/bin/models.rs | 22 -------- driver/src/main.rs | 79 +++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 186 deletions(-) delete mode 100644 driver/src/bin/database_setup.rs delete mode 100644 driver/src/bin/fetch_db.rs delete mode 100644 driver/src/bin/models.rs diff --git a/driver/Cargo.toml b/driver/Cargo.toml index 23b871694..1b76ffe03 100644 --- a/driver/Cargo.toml +++ b/driver/Cargo.toml @@ -4,6 +4,15 @@ version = "0.1.0" authors = ["josojo "] edition = "2018" +[lib] +name = "models" +path = "src/models.rs" +bench = true + +[[bin]] +name = "driver" +path = "src/main.rs" + [dependencies] mongodb = "0.3.2" serde_json = "1.0" diff --git a/driver/src/bin/database_setup.rs b/driver/src/bin/database_setup.rs deleted file mode 100644 index adf05c56e..000000000 --- a/driver/src/bin/database_setup.rs +++ /dev/null @@ -1,77 +0,0 @@ -#[macro_use] -extern crate serde_derive; - -mod models; - -extern crate serde_json; -extern crate serde; -extern crate mongodb; - -use mongodb::{bson, doc}; -use mongodb::{Client, ThreadedClient}; -use mongodb::db::ThreadedDatabase; -use pairing::{ PrimeField }; - - -fn main() { - - let client = Client::connect("localhost", 27017) - .expect("Failed to initialize standalone client."); - - let coll = client.db("dfusion").collection("CurrentState"); - - let doc = doc! { - "CurrentState": "0000000000000000000000000000000000000000", - }; - - // Insert document into 'dfusion.CurrentState' collection - coll.insert_one(doc.clone(), None) - .ok().expect("Failed to insert CurrentState."); - - let coll = client.db("dfusion").collection("State"); - - - let state = models::State { - curState: "0000000000000000000000000000000000000000".to_owned(), - prevState: "0000000000000000000000000000000000000000".to_owned(), - nextState: "0000000000000000000000000000000000000000".to_owned(), - slot: 0, - balances: vec![0, 0, 0, 0] - }; - - let json: serde_json::Value = serde_json::to_value(&state).expect("Failed to parse json"); - let bson = json.into(); - let temp: bson::Document = mongodb::from_bson(bson).expect("Failed to convert bson to document"); - - println!("Data to be inserted{:?}", temp ); - // Insert document into 'dfusion.CurrentState' collection - coll.insert_one(temp.clone(), None) - .ok().expect("Failed to insert CurrentState."); - - let coll = client.db("dfusion").collection("Deposits"); - - let doc2 = doc! { - "depositHash": "0000000000000000000000000000000000000000", - "depositIndex": "0000000000000000000000000000000000000000", - "slot": 1, - "addressId": 1, - "tokenId": 1, - "amount": 55465465, - }; - - // Insert document into 'dfusion.CurrentState' collection - coll.insert_one(doc2.clone(), None) - .ok().expect("Failed to insert Deposit"); - let doc2 = doc! { - "depositHash": "0000000000000000000000000000000000000000", - "depositIndex": "0000000000000000000000000000000000000000", - "slot": 1, - "addressId": 1, - "tokenId": 0, - "amount": 65465, - }; - - // Insert document into 'dfusion.CurrentState' collection - coll.insert_one(doc2.clone(), None) - .ok().expect("Failed to insert Deposit"); -} diff --git a/driver/src/bin/fetch_db.rs b/driver/src/bin/fetch_db.rs deleted file mode 100644 index c63d237c9..000000000 --- a/driver/src/bin/fetch_db.rs +++ /dev/null @@ -1,87 +0,0 @@ -#[macro_use] -extern crate serde_derive; - -mod models; - -extern crate serde_json; -extern crate serde; -extern crate mongodb; -use std::io; -use mongodb::bson; -use mongodb::{Client, ThreadedClient}; -use mongodb::db::ThreadedDatabase; - -fn get_current_balances() -> Result{ - - let client = Client::connect("localhost", 27017) - .expect("Failed to initialize standalone client."); - - let coll = client.db("dfusion").collection("CurrentState"); - - // Find the document and receive a cursor - let cursor = coll.find(None, None) - .ok().expect("Failed to execute find."); - - let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); - - if docs.len() !=1 { - println!("Error: There should be only one CurrentState"); - } - - let json: serde_json::Value = serde_json::to_value(&docs[0]).expect("Failed to parse json");; - let mut query=String::from(r#" { "curState": "#); - let t=json["CurrentState"].to_string(); - query.push_str( &t ); - query.push_str(" }"); - let v: serde_json::Value = serde_json::from_str(&query).expect("Failed to parse query to serde_json::value"); - let bson = v.into(); - let mut _temp: bson::ordered::OrderedDocument = mongodb::from_bson(bson).expect("Failed to convert bson to document"); - - let coll = client.db("dfusion").collection("State"); - - let cursor = coll.find(Some(_temp) , None) - .ok().expect("Failed to execute find."); - - let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); - - let json: String = serde_json::to_string(&docs[0]).expect("Failed to parse json"); - - let deserialized: models::State = serde_json::from_str(&json).unwrap(); - Ok(deserialized) -} - - -fn get_deposits_of_slot(slot: i32) -> Result, io::Error>{ - - let client = Client::connect("localhost", 27017) - .expect("Failed to initialize standalone client."); - - let mut query=String::from(r#" { "slot": "#); - let t=slot.to_string(); - query.push_str( &t ); - query.push_str(" }"); - let v: serde_json::Value = serde_json::from_str(&query).expect("Failed to parse query to serde_json::value"); - let bson = v.into(); - let mut _temp: bson::ordered::OrderedDocument = mongodb::from_bson(bson).expect("Failed to convert bson to document"); - - let coll = client.db("dfusion").collection("Deposits"); - - let cursor = coll.find(Some(_temp) , None) - .ok().expect("Failed to execute find."); - - let docs: Vec<_> = cursor.map(|doc| doc.unwrap()) - .map(|doc| serde_json::to_string(&doc) - .map(|json| serde_json::from_str(&json).unwrap()) - .expect("Failed to parse json")).collect(); - Ok(docs) -} - - -fn main() { - - let mut state = get_current_balances().expect("Could not get the current state of the chain"); - println!("Current balances are: {:?}", state.balances); - let deposits = get_deposits_of_slot(state.slot + 1).unwrap(); - println!("Current deposit hash: {:?}", deposits[0].depositHash); - -} diff --git a/driver/src/bin/models.rs b/driver/src/bin/models.rs deleted file mode 100644 index b56649e96..000000000 --- a/driver/src/bin/models.rs +++ /dev/null @@ -1,22 +0,0 @@ -pub const ACCOUNTS: i32 = 2; -pub const TOKENS: i32 = 2; -pub const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; - -#[derive(Serialize, Deserialize, Clone)] -pub struct State { - pub curState: String, - pub prevState: String, - pub nextState: String, - pub slot: i32, - pub balances: Vec, -} - -#[derive(Serialize, Deserialize)] -pub struct Deposits { - pub depositHash: String, - pub depositIndex: String, - pub slot: i32, - pub accountId: i32, - pub tokenId: i32, - pub amount: i64, -} \ No newline at end of file diff --git a/driver/src/main.rs b/driver/src/main.rs index 3783460ad..c5ffd1dae 100644 --- a/driver/src/main.rs +++ b/driver/src/main.rs @@ -1,4 +1,83 @@ +extern crate models; + +extern crate mongodb; + +use std::io; +use mongodb::bson; +use mongodb::{Client, ThreadedClient}; +use mongodb::db::ThreadedDatabase; + +fn get_current_balances() -> Result{ + + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let coll = client.db("dfusion").collection("CurrentState"); + + // Find the document and receive a cursor + let cursor = coll.find(None, None) + .ok().expect("Failed to execute find."); + + let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); + + if docs.len() !=1 { + println!("Error: There should be only one CurrentState"); + } + + let json: serde_json::Value = serde_json::to_value(&docs[0]).expect("Failed to parse json");; + let mut query=String::from(r#" { "curState": "#); + let t=json["CurrentState"].to_string(); + query.push_str( &t ); + query.push_str(" }"); + let v: serde_json::Value = serde_json::from_str(&query).expect("Failed to parse query to serde_json::value"); + let bson = v.into(); + let mut _temp: bson::ordered::OrderedDocument = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + + let coll = client.db("dfusion").collection("State"); + + let cursor = coll.find(Some(_temp) , None) + .ok().expect("Failed to execute find."); + + let docs: Vec<_> = cursor.map(|doc| doc.unwrap()).collect(); + + let json: String = serde_json::to_string(&docs[0]).expect("Failed to parse json"); + + let deserialized: models::State = serde_json::from_str(&json).unwrap(); + Ok(deserialized) +} + + +fn get_deposits_of_slot(slot: i32) -> Result, io::Error>{ + + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let mut query=String::from(r#" { "slot": "#); + let t=slot.to_string(); + query.push_str( &t ); + query.push_str(" }"); + let v: serde_json::Value = serde_json::from_str(&query).expect("Failed to parse query to serde_json::value"); + let bson = v.into(); + let mut _temp: bson::ordered::OrderedDocument = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + + let coll = client.db("dfusion").collection("Deposits"); + + let cursor = coll.find(Some(_temp) , None) + .ok().expect("Failed to execute find."); + + let docs: Vec<_> = cursor.map(|doc| doc.unwrap()) + .map(|doc| serde_json::to_string(&doc) + .map(|json| serde_json::from_str(&json).unwrap()) + .expect("Failed to parse json")).collect(); + Ok(docs) +} + fn main() { + let mut state = get_current_balances().expect("Could not get the current state of the chain"); + println!("Current balances are: {:?}", state.balances); + let deposits = get_deposits_of_slot(state.slot + 1).unwrap(); + println!("Current deposit hash: {:?}", deposits[0].depositHash); + } From 8fb3bfa29fda09edf418fbc86456fb5acb22e89d Mon Sep 17 00:00:00 2001 From: josojo Date: Fri, 18 Jan 2019 18:19:28 +0100 Subject: [PATCH 10/11] udpateing readme --- driver/README.md | 6 +++--- driver/src/main.rs | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/driver/README.md b/driver/README.md index 825971130..7ff227271 100644 --- a/driver/README.md +++ b/driver/README.md @@ -20,11 +20,11 @@ Install rust & cargo ("1.31") - older versions would not be compatible with newe ## Setup: -In order to load the first data into the data base, run: +In order to load the first data into the data base, we have a test prepared. Just run: ```sh -cargo run --bin database_setup +cargo test -- --nocapture ``` @@ -33,7 +33,7 @@ cargo run --bin database_setup Running ```sh -cargo run --bin driver +cargo run ``` diff --git a/driver/src/main.rs b/driver/src/main.rs index c5ffd1dae..ade4df08b 100644 --- a/driver/src/main.rs +++ b/driver/src/main.rs @@ -1,5 +1,4 @@ extern crate models; - extern crate mongodb; use std::io; From 2f42f462edd630dfcf85851caea9bb2d0b43d3c1 Mon Sep 17 00:00:00 2001 From: josojo Date: Fri, 18 Jan 2019 18:22:00 +0100 Subject: [PATCH 11/11] added missing files --- driver/src/models.rs | 28 ++++++++++++ driver/tests/create_fake_data.rs | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 driver/src/models.rs create mode 100644 driver/tests/create_fake_data.rs diff --git a/driver/src/models.rs b/driver/src/models.rs new file mode 100644 index 000000000..377675c08 --- /dev/null +++ b/driver/src/models.rs @@ -0,0 +1,28 @@ +#[macro_use] +extern crate serde_derive; + +extern crate serde_json; +extern crate serde; + +pub const ACCOUNTS: i32 = 2; +pub const TOKENS: i32 = 2; +pub const SIZE_BALANCE: usize = (ACCOUNTS * TOKENS) as usize; + +#[derive(Serialize, Deserialize, Clone)] +pub struct State { + pub curState: String, + pub prevState: String, + pub nextState: String, + pub slot: i32, + pub balances: Vec, +} + +#[derive(Serialize, Deserialize)] +pub struct Deposits { + pub depositHash: String, + pub slotIndex: i32, + pub slot: i32, + pub accountId: i32, + pub tokenId: i32, + pub amount: i64, +} \ No newline at end of file diff --git a/driver/tests/create_fake_data.rs b/driver/tests/create_fake_data.rs new file mode 100644 index 000000000..94b269b3c --- /dev/null +++ b/driver/tests/create_fake_data.rs @@ -0,0 +1,73 @@ +extern crate models; + +extern crate serde_json; +extern crate serde; +extern crate mongodb; + +use mongodb::{bson, doc}; +use mongodb::{Client, ThreadedClient}; +use mongodb::db::ThreadedDatabase; + +#[test] +fn create_fake_data() { + + let client = Client::connect("localhost", 27017) + .expect("Failed to initialize standalone client."); + + let coll = client.db("dfusion").collection("CurrentState"); + + let doc = doc! { + "CurrentState": "0000000000000000000000000000000000000000", + }; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(doc.clone(), None) + .ok().expect("Failed to insert CurrentState."); + + let coll = client.db("dfusion").collection("State"); + + + let state = models::State { + curState: "0000000000000000000000000000000000000000".to_owned(), + prevState: "0000000000000000000000000000000000000000".to_owned(), + nextState: "0000000000000000000000000000000000000000".to_owned(), + slot: 0, + balances: vec![0; models::SIZE_BALANCE], + }; + + let json: serde_json::Value = serde_json::to_value(&state).expect("Failed to parse json"); + let bson = json.into(); + let temp: bson::Document = mongodb::from_bson(bson).expect("Failed to convert bson to document"); + + println!("Data to be inserted{:?}", temp ); + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(temp.clone(), None) + .ok().expect("Failed to insert CurrentState."); + + let coll = client.db("dfusion").collection("Deposits"); + + let doc2 = doc! { + "depositHash": "0000000000000000000000000000000000000000", + "slotIndex": 0, + "slot": 1, + "accountId": 1, + "tokenId": 1, + "amount": 55465465, + }; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(doc2.clone(), None) + .ok().expect("Failed to insert Deposit"); + let doc2 = doc! { + "depositHash": "0000000000000000000000000000000000000000", + "slotIndex": 1, + "slot": 1, + "accountId": 1, + "tokenId": 0, + "amount": 65465, + }; + + // Insert document into 'dfusion.CurrentState' collection + coll.insert_one(doc2.clone(), None) + .ok().expect("Failed to insert Deposit"); +}