From d0ca787bd783e796523bda8fb74494045c670a4a Mon Sep 17 00:00:00 2001 From: colemanirby Date: Mon, 30 Sep 2024 11:46:06 -0500 Subject: [PATCH 01/28] DB connection complete --- Cargo.toml | 6 ++++++ src/main.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a7745f3..4dd008f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,9 @@ license = "MIT-0" rocket = { version = "0.5.1", features = ["tls", "json"] } serde = { version = "1.0", features = ["derive"] } bcrypt = "0.10.1" +mongodb = "3.1.0" +tokio= "1.40.0" + +[dependencies.rocket_db_pools] +version = "0.2.0" +features = ["mongodb"] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c25e495..2650fc9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,37 @@ #[macro_use] extern crate rocket; +// mod mongo_db; + +use mongodb::bson::doc; use bcrypt::hash_with_salt; use rocket::http::Status; use rocket::http::{Cookie, CookieJar}; use rocket::serde::json::Json; +use rocket_db_pools::{Database, mongodb::Client, Connection}; use serde::{Deserialize, Serialize}; const SALT: &str = "your-server-side-secret-salt"; +// static mut DB: Option = None; + +#[derive(Database)] +#[database("murmur")] +struct Db(Client); + #[derive(Serialize, Deserialize)] struct LoginRequest { username: String, password: String, } +#[get("/insert")] +async fn insert(mut db: Connection) { + db.database("admin").run_command(doc! {"ping": 1}, None).await; + println!("Pinged your deployment. You successfully connected to MongoDB!"); + // Db.database("admin") +} + #[post("/login", data = "")] async fn login(login_request: Json, cookies: &CookieJar<'_>) -> &'static str { let username = &login_request.username; @@ -62,6 +79,11 @@ fn derive_seed(password: &str, username: &str) -> String { } #[launch] -fn rocket() -> _ { - rocket::build().mount("/", routes![login, create, execute]) +async fn rocket() -> _ { + // let connection = MongoDbConnection::new().await; + // match connection { + // Ok(c) => unsafe{db = Some(c)}, + // Err(e) => println!("DB Connection Failed: {e:?}") + // } + rocket::build().mount("/", routes![login, create, execute, insert]).attach(Db::init()) } From 3b23a3a1e793b0087a95f411d9d81f01cfa38a8f Mon Sep 17 00:00:00 2001 From: colemanirby Date: Tue, 1 Oct 2024 10:41:28 -0500 Subject: [PATCH 02/28] Verified Creation and Deletion of records --- Cargo.lock | 956 ++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 - Rocket.toml | 2 + src/main.rs | 60 +++- 4 files changed, 986 insertions(+), 34 deletions(-) create mode 100644 Rocket.toml diff --git a/Cargo.lock b/Cargo.lock index 6c4b432..72924ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,19 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "getrandom", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -26,6 +39,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "async-stream" version = "0.3.5" @@ -45,7 +73,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -56,7 +84,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -124,12 +152,39 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "blowfish" version = "0.8.0" @@ -141,6 +196,33 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "bson" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "068208f2b6fcfa27a7f1ee37488d2bb8ba2640f68f5475d08e1d9130696aba59" +dependencies = [ + "ahash", + "base64 0.13.1", + "bitvec", + "hex", + "indexmap", + "js-sys", + "once_cell", + "rand", + "serde", + "serde_bytes", + "serde_json", + "time", + "uuid", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "bytemuck" version = "1.18.0" @@ -174,6 +256,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "windows-targets 0.52.6", +] + [[package]] name = "cipher" version = "0.3.0" @@ -183,6 +277,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "cookie" version = "0.18.1" @@ -194,6 +294,72 @@ dependencies = [ "version_check", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "cpufeatures" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 1.0.109", +] + +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "data-encoding" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" + [[package]] name = "deranged" version = "0.3.11" @@ -203,6 +369,30 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "derive_more" +version = "0.99.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f33878137e4dafd7fa914ad4e259e18a4e8e532b9617a2d0150262bf53abfce" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version 0.4.1", + "syn 2.0.77", +] + [[package]] name = "devise" version = "0.4.2" @@ -229,11 +419,22 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b035a542cf7abf01f2e3c4d5a7acbaebfefe120ae4efc7bde3df98186e4b8af7" dependencies = [ - "bitflags", + "bitflags 2.6.0", "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn", + "syn 2.0.77", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", ] [[package]] @@ -251,6 +452,18 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "enum-as-inner" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21cdad81446a7f7dc43f6a77409efeb9733d2fa65553efef6018ef257c959b73" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -293,6 +506,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "futures" version = "0.3.30" @@ -323,12 +551,34 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -350,6 +600,7 @@ dependencies = [ "futures-channel", "futures-core", "futures-io", + "futures-macro", "futures-sink", "futures-task", "memchr", @@ -429,6 +680,12 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -441,6 +698,32 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "http" version = "0.2.12" @@ -503,13 +786,63 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.5.7", "tokio", "tower-service", "tracing", "want", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "idna" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "indexmap" version = "2.5.0" @@ -527,6 +860,24 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb" +[[package]] +name = "ipconfig" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f" +dependencies = [ + "socket2 0.5.7", + "widestring", + "windows-sys 0.48.0", + "winreg", +] + +[[package]] +name = "ipnet" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" + [[package]] name = "is-terminal" version = "0.4.13" @@ -544,6 +895,15 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -556,6 +916,12 @@ version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -593,6 +959,21 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "lru-cache" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" +dependencies = [ + "linked-hash-map", +] + +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "matchers" version = "0.1.0" @@ -602,6 +983,22 @@ dependencies = [ "regex-automata 0.1.10", ] +[[package]] +name = "matches" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" + +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.7.4" @@ -635,6 +1032,53 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mongodb" +version = "2.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef206acb1b72389b49bc9985efe7eb1f8a9bb18e5680d262fac26c07f44025f1" +dependencies = [ + "async-trait", + "base64 0.13.1", + "bitflags 1.3.2", + "bson", + "chrono", + "derivative", + "derive_more", + "futures-core", + "futures-executor", + "futures-io", + "futures-util", + "hex", + "hmac", + "lazy_static", + "md-5", + "pbkdf2", + "percent-encoding", + "rand", + "rustc_version_runtime", + "rustls", + "rustls-pemfile", + "serde", + "serde_bytes", + "serde_with", + "sha-1", + "sha2", + "socket2 0.4.10", + "stringprep", + "strsim", + "take_mut", + "thiserror", + "tokio", + "tokio-rustls", + "tokio-util", + "trust-dns-proto", + "trust-dns-resolver", + "typed-builder", + "uuid", + "webpki-roots", +] + [[package]] name = "multer" version = "3.1.0" @@ -660,6 +1104,7 @@ version = "0.1.0" dependencies = [ "bcrypt", "rocket", + "rocket_db_pools", "serde", ] @@ -679,6 +1124,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -739,6 +1193,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "pbkdf2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" +dependencies = [ + "digest", +] + [[package]] name = "pear" version = "0.2.9" @@ -759,7 +1222,7 @@ dependencies = [ "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -812,11 +1275,17 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", "version_check", "yansi", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.37" @@ -826,6 +1295,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.8.5" @@ -862,7 +1337,7 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b" dependencies = [ - "bitflags", + "bitflags 2.6.0", ] [[package]] @@ -882,7 +1357,7 @@ checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -929,6 +1404,16 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +[[package]] +name = "resolv-conf" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52e44394d2086d010551b14b53b1f24e31647570cd1deb0379e2c21b329aba00" +dependencies = [ + "hostname", + "quick-error", +] + [[package]] name = "ring" version = "0.17.8" @@ -979,24 +1464,46 @@ dependencies = [ "tokio-util", "ubyte", "version_check", - "yansi", + "yansi", +] + +[[package]] +name = "rocket_codegen" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "575d32d7ec1a9770108c879fc7c47815a80073f96ca07ff9525a94fcede1dd46" +dependencies = [ + "devise", + "glob", + "indexmap", + "proc-macro2", + "quote", + "rocket_http", + "syn 2.0.77", + "unicode-xid", + "version_check", +] + +[[package]] +name = "rocket_db_pools" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6578b2740ceee3e78bff63fe9299d964b7e68318446cdcb9af3b9cab46e1e9d" +dependencies = [ + "mongodb", + "rocket", + "rocket_db_pools_codegen", + "version_check", ] [[package]] -name = "rocket_codegen" -version = "0.5.1" +name = "rocket_db_pools_codegen" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "575d32d7ec1a9770108c879fc7c47815a80073f96ca07ff9525a94fcede1dd46" +checksum = "842e859f2e87a23efc0f81e25756c0fb43f18726e62daf99da7ea19fbc56cebd" dependencies = [ "devise", - "glob", - "indexmap", - "proc-macro2", "quote", - "rocket_http", - "syn", - "unicode-xid", - "version_check", ] [[package]] @@ -1035,13 +1542,41 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver 1.0.23", +] + +[[package]] +name = "rustc_version_runtime" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d31b7153270ebf48bf91c65ae5b0c00e749c4cfad505f66530ac74950249582f" +dependencies = [ + "rustc_version 0.2.3", + "semver 0.9.0", +] + [[package]] name = "rustix" version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ - "bitflags", + "bitflags 2.6.0", "errno", "libc", "linux-raw-sys", @@ -1113,6 +1648,27 @@ dependencies = [ "untrusted", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" version = "1.0.210" @@ -1122,6 +1678,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.210" @@ -1130,7 +1695,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -1139,6 +1704,7 @@ version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ + "indexmap", "itoa", "memchr", "ryu", @@ -1154,6 +1720,50 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678b5a069e50bf00ecd22d0cd8ddf7c236f68581b03db652061ed5eb13a312ff" +dependencies = [ + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "sha-1" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5058ada175748e33390e40e872bd0fe59a19f265d0158daa551c5a88a76009c" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -1193,6 +1803,16 @@ version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.7" @@ -1227,6 +1847,40 @@ dependencies = [ "loom", ] +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.77" @@ -1238,6 +1892,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.12.0" @@ -1251,6 +1917,26 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "thiserror" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -1292,6 +1978,21 @@ dependencies = [ "time-core", ] +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.40.0" @@ -1304,7 +2005,7 @@ dependencies = [ "mio", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.5.7", "tokio-macros", "windows-sys 0.52.0", ] @@ -1317,7 +2018,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -1349,6 +2050,7 @@ checksum = "61e7c3654c13bcd040d4a03abee2c75b1d14a37b423cf5a813ceae1cc903ec6a" dependencies = [ "bytes", "futures-core", + "futures-io", "futures-sink", "pin-project-lite", "tokio", @@ -1413,7 +2115,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -1455,12 +2157,68 @@ dependencies = [ "tracing-log", ] +[[package]] +name = "trust-dns-proto" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c31f240f59877c3d4bb3b3ea0ec5a6a0cff07323580ff8c7a605cd7d08b255d" +dependencies = [ + "async-trait", + "cfg-if", + "data-encoding", + "enum-as-inner", + "futures-channel", + "futures-io", + "futures-util", + "idna 0.2.3", + "ipnet", + "lazy_static", + "log", + "rand", + "smallvec", + "thiserror", + "tinyvec", + "tokio", + "url", +] + +[[package]] +name = "trust-dns-resolver" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4ba72c2ea84515690c9fcef4c6c660bb9df3036ed1051686de84605b74fd558" +dependencies = [ + "cfg-if", + "futures-util", + "ipconfig", + "lazy_static", + "log", + "lru-cache", + "parking_lot", + "resolv-conf", + "smallvec", + "thiserror", + "tokio", + "trust-dns-proto", +] + [[package]] name = "try-lock" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "typed-builder" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89851716b67b937e393b3daa8423e67ddfc4bbbf1654bcf05488e95e0828db0c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "typenum" version = "1.17.0" @@ -1486,12 +2244,33 @@ dependencies = [ "version_check", ] +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + [[package]] name = "unicode-ident" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +[[package]] +name = "unicode-normalization" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "unicode-properties" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ea75f83c0137a9b98608359a5f1af8144876eb67bcb1ce837368e906a9f524" + [[package]] name = "unicode-xid" version = "0.2.6" @@ -1504,6 +2283,27 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna 0.5.0", + "percent-encoding", +] + +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" +dependencies = [ + "getrandom", + "serde", +] + [[package]] name = "valuable" version = "0.1.0" @@ -1531,6 +2331,73 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.77", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + +[[package]] +name = "webpki-roots" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" + +[[package]] +name = "widestring" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311" + [[package]] name = "winapi" version = "0.3.9" @@ -1562,6 +2429,24 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -1710,6 +2595,25 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "yansi" version = "1.0.1" @@ -1737,5 +2641,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] diff --git a/Cargo.toml b/Cargo.toml index 4dd008f..e6814db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,6 @@ license = "MIT-0" rocket = { version = "0.5.1", features = ["tls", "json"] } serde = { version = "1.0", features = ["derive"] } bcrypt = "0.10.1" -mongodb = "3.1.0" -tokio= "1.40.0" [dependencies.rocket_db_pools] version = "0.2.0" diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..dfbc3ac --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,2 @@ +[default.databases.murmur] +url = "mongodb+srv://murmurapi:murmur_api@murmur.shic7.mongodb.net/?retryWrites=true&w=majority&appName=Murmur" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2650fc9..88978c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,17 @@ extern crate rocket; // mod mongo_db; +// #[macro_use] +// extern crate bson; + + +use std::result; + +// use mongodb::{bson::{doc, Document}, results::DeleteResult, error::Error}; +use rocket_db_pools::mongodb::{bson::{self, doc, Bson, Document}, error::Error, results::DeleteResult, Collection}; -use mongodb::bson::doc; use bcrypt::hash_with_salt; -use rocket::http::Status; +use rocket::{futures::FutureExt, http::Status}; use rocket::http::{Cookie, CookieJar}; use rocket::serde::json::Json; use rocket_db_pools::{Database, mongodb::Client, Connection}; @@ -24,14 +31,55 @@ struct LoginRequest { username: String, password: String, } +#[derive(Serialize, Deserialize)] +struct MMR { + test: String, + test2: String +} #[get("/insert")] -async fn insert(mut db: Connection) { - db.database("admin").run_command(doc! {"ping": 1}, None).await; - println!("Pinged your deployment. You successfully connected to MongoDB!"); +async fn insert(db: Connection) { + // db.database("admin").run_command(doc! {"ping": 1}, None).await; + let test = String::from("abc"); + let test2 = String::from("cde"); + let doc = MMR{test, test2}; + let insert_result = db.database("Mmr").collection("mmrs").insert_one(doc, None).await; + + match insert_result { + Err(e) => println!("Error inserting record : {e:?}"), + Ok(insert) => { + println!("succesfully inserted record, {insert:?}"); + } + } + // println!("Pinged your deployment. You successfully connected to MongoDB!"); // Db.database("admin") } +#[get("/delete")] +async fn delete(db: Connection) { + + let test = String::from("abc"); + let test2 = String::from("cde"); + let object = MMR{test, test2}; + + let bson_try = bson::to_bson(&object); + match bson_try { + Err(e) => println!("Error turning object into bson {e:?}"), + Ok(bson_object) => { + + let collection: Collection = db.database("Mmr").collection("mmrs"); + + let query = bson_object.as_document().unwrap(); + + let delete_result = collection.delete_one(query.clone(), None).await; + match delete_result { + Err(e) => println!("Deletion error occurred: {e:?}"), + Ok(success) => println!("Deletion Succeeded {success:?}") + } + } + } +} + #[post("/login", data = "")] async fn login(login_request: Json, cookies: &CookieJar<'_>) -> &'static str { let username = &login_request.username; @@ -85,5 +133,5 @@ async fn rocket() -> _ { // Ok(c) => unsafe{db = Some(c)}, // Err(e) => println!("DB Connection Failed: {e:?}") // } - rocket::build().mount("/", routes![login, create, execute, insert]).attach(Db::init()) + rocket::build().mount("/", routes![login, create, execute, insert, delete]).attach(Db::init()) } From c46e971718cfa45948c09227e5067f0d6e92da5f Mon Sep 17 00:00:00 2001 From: colemanirby Date: Tue, 1 Oct 2024 11:18:29 -0500 Subject: [PATCH 03/28] included extra deletion functionality --- src/main.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 88978c7..4c0fb9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ extern crate rocket; use std::result; // use mongodb::{bson::{doc, Document}, results::DeleteResult, error::Error}; -use rocket_db_pools::mongodb::{bson::{self, doc, Bson, Document}, error::Error, results::DeleteResult, Collection}; +use rocket_db_pools::mongodb::{bson::{self, doc, oid::ObjectId, Bson, Document}, error::Error, results::DeleteResult, Collection}; use bcrypt::hash_with_salt; use rocket::{futures::FutureExt, http::Status}; @@ -69,9 +69,16 @@ async fn delete(db: Connection) { let collection: Collection = db.database("Mmr").collection("mmrs"); + // Can delete by reconstructing the object or by using the object ID that is + // created on record insertion let query = bson_object.as_document().unwrap(); - + // let object_id = ObjectId::parse_str("66fc1dc432627ab776148773").unwrap(); + // let query = doc!{"_id": object_id}; + + let delete_result = collection.delete_one(query.clone(), None).await; + + // let delete_result = collection.delete_one(query.clone(), None).await; match delete_result { Err(e) => println!("Deletion error occurred: {e:?}"), Ok(success) => println!("Deletion Succeeded {success:?}") From 81a604b7616b1b0889f943b2244cd8d239c5574c Mon Sep 17 00:00:00 2001 From: colemanirby Date: Wed, 2 Oct 2024 00:02:27 -0500 Subject: [PATCH 04/28] Added back DB functionality --- Cargo.lock | 595 ++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 4 + Rocket.toml | 4 +- src/main.rs | 106 ++++----- src/store.rs | 41 +++- 5 files changed, 684 insertions(+), 66 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0977a81..8cb85fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,21 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.15" @@ -303,7 +318,7 @@ dependencies = [ "num-bigint", "num-traits", "paste", - "rustc_version", + "rustc_version 0.4.1", "zeroize", ] @@ -909,6 +924,33 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "bson" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "068208f2b6fcfa27a7f1ee37488d2bb8ba2640f68f5475d08e1d9130696aba59" +dependencies = [ + "ahash", + "base64 0.13.1", + "bitvec", + "hex", + "indexmap", + "js-sys", + "once_cell", + "rand", + "serde", + "serde_bytes", + "serde_json", + "time", + "uuid", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "byte-slice-cast" version = "1.2.2" @@ -972,6 +1014,18 @@ dependencies = [ "zeroize", ] +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "windows-targets 0.52.6", +] + [[package]] name = "cipher" version = "0.3.0" @@ -1242,7 +1296,7 @@ dependencies = [ "curve25519-dalek-derive", "digest 0.10.7", "fiat-crypto", - "rustc_version", + "rustc_version 0.4.1", "subtle", "zeroize", ] @@ -1258,6 +1312,16 @@ dependencies = [ "syn 2.0.79", ] +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core 0.13.4", + "darling_macro 0.13.4", +] + [[package]] name = "darling" version = "0.14.4" @@ -1278,6 +1342,20 @@ dependencies = [ "darling_macro 0.20.10", ] +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.10.0", + "syn 1.0.109", +] + [[package]] name = "darling_core" version = "0.14.4" @@ -1306,6 +1384,17 @@ dependencies = [ "syn 2.0.79", ] +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core 0.13.4", + "quote", + "syn 1.0.109", +] + [[package]] name = "darling_macro" version = "0.14.4" @@ -1328,6 +1417,12 @@ dependencies = [ "syn 2.0.79", ] +[[package]] +name = "data-encoding" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8566979429cf69b49a5c740c60791108e86440e8be149bbea4fe54d2c32d6e2" + [[package]] name = "der" version = "0.7.9" @@ -1378,7 +1473,7 @@ dependencies = [ "convert_case", "proc-macro2", "quote", - "rustc_version", + "rustc_version 0.4.1", "syn 2.0.79", ] @@ -1601,6 +1696,18 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "enum-as-inner" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21cdad81446a7f7dc43f6a77409efeb9733d2fa65553efef6018ef257c959b73" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "environmental" version = "1.1.4" @@ -2193,6 +2300,17 @@ dependencies = [ "hmac 0.8.1", ] +[[package]] +name = "hostname" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c731c3e10504cc8ed35cfe2f1db4c9274c3d35fa486e3b31df46f068ef3e867" +dependencies = [ + "libc", + "match_cfg", + "winapi", +] + [[package]] name = "http" version = "0.2.12" @@ -2255,7 +2373,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.5.7", "tokio", "tower-service", "tracing", @@ -2278,12 +2396,46 @@ dependencies = [ "tokio-rustls 0.24.1", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "ident_case" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "idna" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "idna" version = "0.5.0" @@ -2373,6 +2525,24 @@ dependencies = [ "num-traits", ] +[[package]] +name = "ipconfig" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b58db92f96b720de98181bbbe63c831e87005ab460c1bf306eb2622b4707997f" +dependencies = [ + "socket2 0.5.7", + "widestring", + "windows-sys 0.48.0", + "winreg", +] + +[[package]] +name = "ipnet" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" + [[package]] name = "is-terminal" version = "0.4.13" @@ -2423,6 +2593,15 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "jsonrpsee" version = "0.22.5" @@ -2601,6 +2780,12 @@ dependencies = [ "libsecp256k1-core", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -2647,6 +2832,21 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "lru-cache" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" +dependencies = [ + "linked-hash-map", +] + +[[package]] +name = "match_cfg" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" + [[package]] name = "matchers" version = "0.1.0" @@ -2656,6 +2856,22 @@ dependencies = [ "regex-automata 0.1.10", ] +[[package]] +name = "matches" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" + +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest 0.10.7", +] + [[package]] name = "memchr" version = "2.7.4" @@ -2716,6 +2932,53 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mongodb" +version = "2.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef206acb1b72389b49bc9985efe7eb1f8a9bb18e5680d262fac26c07f44025f1" +dependencies = [ + "async-trait", + "base64 0.13.1", + "bitflags 1.3.2", + "bson", + "chrono", + "derivative", + "derive_more", + "futures-core", + "futures-executor", + "futures-io", + "futures-util", + "hex", + "hmac 0.12.1", + "lazy_static", + "md-5", + "pbkdf2 0.11.0", + "percent-encoding", + "rand", + "rustc_version_runtime", + "rustls 0.21.12", + "rustls-pemfile 1.0.4", + "serde", + "serde_bytes", + "serde_with", + "sha-1 0.10.1", + "sha2 0.10.8", + "socket2 0.4.10", + "stringprep", + "strsim 0.10.0", + "take_mut", + "thiserror", + "tokio", + "tokio-rustls 0.24.1", + "tokio-util", + "trust-dns-proto", + "trust-dns-resolver", + "typed-builder", + "uuid", + "webpki-roots", +] + [[package]] name = "multer" version = "3.1.0" @@ -2742,6 +3005,7 @@ dependencies = [ "bcrypt", "murmur-lib", "rocket", + "rocket_db_pools", "serde", "serde_cbor", "serde_json", @@ -3027,6 +3291,15 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "pbkdf2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83a0692ec44e4cf1ef28ca317f14f8f07da2d95ec3fa01f86e4467b725e60917" +dependencies = [ + "digest 0.10.7", +] + [[package]] name = "pbkdf2" version = "0.12.2" @@ -3313,6 +3586,12 @@ dependencies = [ "yansi", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.37" @@ -3451,6 +3730,16 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "resolv-conf" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52e44394d2086d010551b14b53b1f24e31647570cd1deb0379e2c21b329aba00" +dependencies = [ + "hostname", + "quick-error", +] + [[package]] name = "rfc6979" version = "0.4.0" @@ -3548,6 +3837,28 @@ dependencies = [ "version_check", ] +[[package]] +name = "rocket_db_pools" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6578b2740ceee3e78bff63fe9299d964b7e68318446cdcb9af3b9cab46e1e9d" +dependencies = [ + "mongodb", + "rocket", + "rocket_db_pools_codegen", + "version_check", +] + +[[package]] +name = "rocket_db_pools_codegen" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "842e859f2e87a23efc0f81e25756c0fb43f18726e62daf99da7ea19fbc56cebd" +dependencies = [ + "devise", + "quote", +] + [[package]] name = "rocket_http" version = "0.5.1" @@ -3596,13 +3907,32 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e75f6a532d0fd9f7f13144f392b6ad56a32696bfcd9c78f797f16bbb6f072d6" +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + [[package]] name = "rustc_version" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" dependencies = [ - "semver", + "semver 1.0.23", +] + +[[package]] +name = "rustc_version_runtime" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d31b7153270ebf48bf91c65ae5b0c00e749c4cfad505f66530ac74950249582f" +dependencies = [ + "rustc_version 0.2.3", + "semver 0.9.0", ] [[package]] @@ -4009,12 +4339,27 @@ dependencies = [ "libc", ] +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + [[package]] name = "semver" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + [[package]] name = "serde" version = "1.0.210" @@ -4060,6 +4405,7 @@ version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ + "indexmap", "itoa", "memchr", "ryu", @@ -4075,6 +4421,28 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678b5a069e50bf00ecd22d0cd8ddf7c236f68581b03db652061ed5eb13a312ff" +dependencies = [ + "serde", + "serde_with_macros", +] + +[[package]] +name = "serde_with_macros" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082" +dependencies = [ + "darling 0.13.4", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "serdect" version = "0.2.0" @@ -4098,6 +4466,17 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "sha-1" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5058ada175748e33390e40e872bd0fe59a19f265d0158daa551c5a88a76009c" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.7", +] + [[package]] name = "sha1" version = "0.10.6" @@ -4255,7 +4634,7 @@ dependencies = [ "num-bigint", "num-rational", "num-traits", - "pbkdf2", + "pbkdf2 0.12.2", "pin-project", "poly1305", "rand", @@ -4312,6 +4691,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "socket2" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "socket2" version = "0.5.7" @@ -4334,7 +4723,7 @@ dependencies = [ "httparse", "log", "rand", - "sha-1", + "sha-1 0.9.8", ] [[package]] @@ -4972,6 +5361,17 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + [[package]] name = "strsim" version = "0.10.0" @@ -5012,7 +5412,7 @@ version = "0.4.7" source = "git+https://github.com/ideal-lab5/polkadot-sdk.git?branch=testing#0bd1f08ed576063e678539edafdf3a589ff3989e" dependencies = [ "hmac 0.12.1", - "pbkdf2", + "pbkdf2 0.12.2", "schnorrkel", "sha2 0.10.8", "zeroize", @@ -5139,7 +5539,7 @@ dependencies = [ "hex", "hmac 0.12.1", "parity-scale-codec", - "pbkdf2", + "pbkdf2 0.12.2", "regex", "schnorrkel", "secp256k1", @@ -5172,6 +5572,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "take_mut" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f764005d11ee5f36500a149ace24e00e3da98b0158b3e2d53a7495660d3f4d60" + [[package]] name = "tap" version = "1.0.1" @@ -5288,7 +5694,7 @@ dependencies = [ "mio", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.5.7", "tokio-macros", "windows-sys 0.52.0", ] @@ -5519,6 +5925,51 @@ dependencies = [ "hash-db", ] +[[package]] +name = "trust-dns-proto" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c31f240f59877c3d4bb3b3ea0ec5a6a0cff07323580ff8c7a605cd7d08b255d" +dependencies = [ + "async-trait", + "cfg-if", + "data-encoding", + "enum-as-inner", + "futures-channel", + "futures-io", + "futures-util", + "idna 0.2.3", + "ipnet", + "lazy_static", + "log", + "rand", + "smallvec", + "thiserror", + "tinyvec", + "tokio", + "url", +] + +[[package]] +name = "trust-dns-resolver" +version = "0.21.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4ba72c2ea84515690c9fcef4c6c660bb9df3036ed1051686de84605b74fd558" +dependencies = [ + "cfg-if", + "futures-util", + "ipconfig", + "lazy_static", + "log", + "lru-cache", + "parking_lot", + "resolv-conf", + "smallvec", + "thiserror", + "tokio", + "trust-dns-proto", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -5537,6 +5988,17 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "typed-builder" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89851716b67b937e393b3daa8423e67ddfc4bbbf1654bcf05488e95e0828db0c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "typenum" version = "1.17.0" @@ -5595,6 +6057,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-properties" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e70f2a8b45122e719eb623c01822704c4e0907e7e426a05927e1a1cfff5b75d0" + [[package]] name = "unicode-xid" version = "0.2.6" @@ -5624,7 +6092,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" dependencies = [ "form_urlencoded", - "idna", + "idna 0.5.0", "percent-encoding", ] @@ -5634,6 +6102,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" +dependencies = [ + "getrandom", + "serde", +] + [[package]] name = "valuable" version = "0.1.0" @@ -5695,6 +6173,61 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.79", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + [[package]] name = "wasmi" version = "0.31.2" @@ -5735,6 +6268,18 @@ dependencies = [ "indexmap-nostd", ] +[[package]] +name = "webpki-roots" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" + +[[package]] +name = "widestring" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311" + [[package]] name = "winapi" version = "0.3.9" @@ -5775,6 +6320,24 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -5932,6 +6495,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "winreg" +version = "0.50.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +dependencies = [ + "cfg-if", + "windows-sys 0.48.0", +] + [[package]] name = "wyz" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index 71237ed..d907adf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,7 @@ serde_cbor = "0.11.2" sp-core = { git = "https://github.com/ideal-lab5/polkadot-sdk.git", branch = "testing", features = ["bls-experimental"] } subxt-signer = "0.35.2" subxt = "0.35.2" + +[dependencies.rocket_db_pools] +version = "0.2.0" +features = ["mongodb"] \ No newline at end of file diff --git a/Rocket.toml b/Rocket.toml index dfbc3ac..b052397 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,2 +1,2 @@ -[default.databases.murmur] -url = "mongodb+srv://murmurapi:murmur_api@murmur.shic7.mongodb.net/?retryWrites=true&w=majority&appName=Murmur" \ No newline at end of file +[default.databases.Murmur] +url = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f39303b..5b82c9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,9 +24,12 @@ use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, BlockNumber, }; +use rocket_db_pools::{Database, Connection}; +use rocket_db_pools::mongodb::{error::Error, results::DeleteResult, Client, Collection}; +use rocket_db_pools::mongodb::bson::{self, doc, oid::ObjectId, Bson, Document}; use rocket::http::Status; use rocket::http::{Cookie, CookieJar}; -use rocket::serde::{json::Json, Deserialize}; +use rocket::serde::{json::Json, Serialize, Deserialize}; use sp_core::crypto::Ss58Codec; use subxt::utils::{AccountId32, MultiAddress}; use subxt_signer::sr25519::dev; @@ -35,6 +38,10 @@ use utils::{check_cookie, derive_seed}; const SALT: &str = "your-server-side-secret-salt"; const EPHEM_MSK: [u8; 32] = [1; 32]; +#[derive(Database)] +#[database("Murmur")] +struct Db(Client); + #[derive(Deserialize)] struct LoginRequest { username: String, @@ -46,55 +53,52 @@ struct MMR { test2: String } -// #[get("/insert")] -// async fn insert(db: Connection) { -// // db.database("admin").run_command(doc! {"ping": 1}, None).await; -// let test = String::from("abc"); -// let test2 = String::from("cde"); -// let doc = MMR{test, test2}; -// let insert_result = db.database("Mmr").collection("mmrs").insert_one(doc, None).await; - -// match insert_result { -// Err(e) => println!("Error inserting record : {e:?}"), -// Ok(insert) => { -// println!("succesfully inserted record, {insert:?}"); -// } -// } -// // println!("Pinged your deployment. You successfully connected to MongoDB!"); -// // Db.database("admin") -// } - -// #[get("/delete")] -// async fn delete(db: Connection) { - -// let test = String::from("abc"); -// let test2 = String::from("cde"); -// let object = MMR{test, test2}; - -// let bson_try = bson::to_bson(&object); -// match bson_try { -// Err(e) => println!("Error turning object into bson {e:?}"), -// Ok(bson_object) => { - -// let collection: Collection = db.database("Mmr").collection("mmrs"); - -// // Can delete by reconstructing the object or by using the object ID that is -// // created on record insertion -// let query = bson_object.as_document().unwrap(); -// // let object_id = ObjectId::parse_str("66fc1dc432627ab776148773").unwrap(); -// // let query = doc!{"_id": object_id}; +#[get("/insert")] +async fn insert(db: Connection) { + let test = String::from("abc"); + let test2 = String::from("cde"); + let doc = MMR{test, test2}; + let insert_result = db.database("MurmurDB").collection("mmrs").insert_one(doc, None).await; + + match insert_result { + Err(e) => println!("Error inserting record : {e:?}"), + Ok(insert) => { + println!("succesfully inserted record, {insert:?}"); + } + } +} + +#[get("/delete")] +async fn delete(db: Connection) { + + let test = String::from("abc"); + let test2 = String::from("cde"); + let object = MMR{test, test2}; + + let bson_try = bson::to_bson(&object); + match bson_try { + Err(e) => println!("Error turning object into bson {e:?}"), + Ok(bson_object) => { + + let collection: Collection = db.database("MurmurDB").collection("mmrs"); + + // Can delete by reconstructing the object or by using the object ID that is + // created on record insertion + let query = bson_object.as_document().unwrap(); + // let object_id = ObjectId::parse_str("66fc1dc432627ab776148773").unwrap(); + // let query = doc!{"_id": object_id}; -// let delete_result = collection.delete_one(query.clone(), None).await; + let delete_result = collection.delete_one(query.clone(), None).await; -// // let delete_result = collection.delete_one(query.clone(), None).await; -// match delete_result { -// Err(e) => println!("Deletion error occurred: {e:?}"), -// Ok(success) => println!("Deletion Succeeded {success:?}") -// } -// } -// } -// } + // let delete_result = collection.delete_one(query.clone(), None).await; + match delete_result { + Err(e) => println!("Deletion error occurred: {e:?}"), + Ok(success) => println!("Deletion Succeeded {success:?}") + } + } + } +} #[derive(Deserialize)] struct ExecuteRequest { @@ -121,7 +125,7 @@ async fn login(login_request: Json, cookies: &CookieJar<'_>) -> &' #[post("/new", data = "")] /// Generate a wallet valid for the next {validity} blocks -async fn new(cookies: &CookieJar<'_>, request: Json) -> Result { +async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection) -> Result { check_cookie(cookies, |username, seed| async { let (client, current_block_number, round_pubkey_bytes) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; @@ -142,7 +146,7 @@ async fn new(cookies: &CookieJar<'_>, request: Json) -> Result, request: Json) -> Resu value: request.amount, }); - let store = store::load(); + let store = store::load_from_file(); let target_block_number = current_block_number + 1; let tx = murmur::prepare_execute( @@ -194,5 +198,5 @@ async fn execute(cookies: &CookieJar<'_>, request: Json) -> Resu #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![login, new, execute]) + rocket::build().mount("/", routes![login, new, execute, insert, delete]).attach(Db::init()) } diff --git a/src/store.rs b/src/store.rs index d490c0c..63bbf37 100644 --- a/src/store.rs +++ b/src/store.rs @@ -15,9 +15,19 @@ */ use murmur::MurmurStore; +use rocket_db_pools::mongodb::{bson::doc, options::{FindOptions, InsertOneOptions}}; use std::fs::File; -pub(crate) fn load() -> MurmurStore { +use rocket_db_pools::Connection; +use rocket_db_pools::mongodb::Collection; +use rocket_db_pools::mongodb::Cursor; +use rocket_db_pools::mongodb::error::Error; +use rocket_db_pools::mongodb::bson::oid::ObjectId; +use rocket::serde::Serialize; + +use crate::Db; + +pub(crate) fn load_from_file() -> MurmurStore { // TODO: load from DB let mmr_store_file = File::open("mmr_store").expect("Unable to open file"); let data: MurmurStore = serde_cbor::from_reader(mmr_store_file).unwrap(); @@ -25,9 +35,36 @@ pub(crate) fn load() -> MurmurStore { data } +pub(crate) async fn load_from_db(object_id_string: String, db_name: String, collection_name: String, db:Connection, options: Option) -> Result, Error> { + + let object_id = ObjectId::parse_str(object_id_string).unwrap(); + + let filter = doc! {"_id": object_id}; + + let mmr_collection: Collection= db.database(&db_name).collection(&collection_name); + + let mmr_query_result = mmr_collection.find(filter, options).await; + + mmr_query_result + +} + /// Write the MMR data to a file -pub(crate) fn write(mmr_store: MurmurStore) { +pub(crate) fn write_to_file(mmr_store: MurmurStore) { // TODO: write to DB let mmr_store_file = File::create("mmr_store").expect("It should create the file"); serde_cbor::to_writer(mmr_store_file, &mmr_store).unwrap(); } + +pub (crate) async fn write_to_db(db_name: String, collection_name: String, doc: T, db:Connection, options: Option) { + + let insert_result = db.database(&db_name).collection(&collection_name).insert_one(doc, options).await; + + match insert_result { + Err(e) => println!("Error inserting record : {e:?}"), + Ok(insert) => { + println!("succesfully inserted record, {insert:?}"); + } + } + +} From b5f6ad0c039c2a67182d163de86f6bf9df1de506 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Wed, 2 Oct 2024 00:17:44 -0500 Subject: [PATCH 05/28] updated methods for loading and storing in db --- src/main.rs | 27 +++++++++++++++++++++------ src/store.rs | 16 ++++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5b82c9c..bb25ef3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,11 +22,11 @@ mod utils; use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, - BlockNumber, + BlockNumber, MurmurStore, }; use rocket_db_pools::{Database, Connection}; -use rocket_db_pools::mongodb::{error::Error, results::DeleteResult, Client, Collection}; -use rocket_db_pools::mongodb::bson::{self, doc, oid::ObjectId, Bson, Document}; +use rocket_db_pools::mongodb::{Client, Collection}; +use rocket_db_pools::mongodb::bson::{self, doc}; use rocket::http::Status; use rocket::http::{Cookie, CookieJar}; use rocket::serde::{json::Json, Serialize, Deserialize}; @@ -37,6 +37,9 @@ use utils::{check_cookie, derive_seed}; const SALT: &str = "your-server-side-secret-salt"; const EPHEM_MSK: [u8; 32] = [1; 32]; +const use_db: bool = false; +const db_name: &str = "MurmurDB"; +const collection_name: &str = "mmrs"; #[derive(Database)] #[database("Murmur")] @@ -146,7 +149,12 @@ async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection< ) .map_err(|_| Status::InternalServerError)?; // 3. add to storage - store::write_to_file(mmr_store.clone()); + if use_db { + store::write_to_db(db_name, collection_name, mmr_store.clone(), db, None).await; + } else { + store::write_to_file(mmr_store.clone()); + } + // sign and send the call let from = dev::alice(); let _events = client @@ -161,7 +169,7 @@ async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection< #[post("/execute", data = "")] /// Execute a transaction from the wallet -async fn execute(cookies: &CookieJar<'_>, request: Json) -> Result { +async fn execute(cookies: &CookieJar<'_>, request: Json, db: Connection) -> Result { check_cookie(cookies, |username, seed| async { let (client, current_block_number, _) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; @@ -176,7 +184,14 @@ async fn execute(cookies: &CookieJar<'_>, request: Json) -> Resu value: request.amount, }); - let store = store::load_from_file(); + let store: MurmurStore; + if use_db { + let object_id_string = "str".to_string(); + store = store::load_from_db(object_id_string, db_name, collection_name, db, None).await; + } else { + store = store::load_from_file(); + } + let target_block_number = current_block_number + 1; let tx = murmur::prepare_execute( diff --git a/src/store.rs b/src/store.rs index 63bbf37..ab9e93f 100644 --- a/src/store.rs +++ b/src/store.rs @@ -20,10 +20,8 @@ use std::fs::File; use rocket_db_pools::Connection; use rocket_db_pools::mongodb::Collection; -use rocket_db_pools::mongodb::Cursor; -use rocket_db_pools::mongodb::error::Error; use rocket_db_pools::mongodb::bson::oid::ObjectId; -use rocket::serde::Serialize; +use rocket::{futures::TryStreamExt, serde::Serialize}; use crate::Db; @@ -35,7 +33,7 @@ pub(crate) fn load_from_file() -> MurmurStore { data } -pub(crate) async fn load_from_db(object_id_string: String, db_name: String, collection_name: String, db:Connection, options: Option) -> Result, Error> { +pub(crate) async fn load_from_db(object_id_string: String, db_name: &str, collection_name: &str, db:Connection, options: Option) -> MurmurStore { let object_id = ObjectId::parse_str(object_id_string).unwrap(); @@ -43,9 +41,11 @@ pub(crate) async fn load_from_db(object_id_string: String, db_name: String, coll let mmr_collection: Collection= db.database(&db_name).collection(&collection_name); - let mmr_query_result = mmr_collection.find(filter, options).await; + let cursor = mmr_collection.find(filter, options).await?; - mmr_query_result + let mmr = cursor.try_next().await.unwrap().unwrap(); + + mmr } @@ -56,9 +56,9 @@ pub(crate) fn write_to_file(mmr_store: MurmurStore) { serde_cbor::to_writer(mmr_store_file, &mmr_store).unwrap(); } -pub (crate) async fn write_to_db(db_name: String, collection_name: String, doc: T, db:Connection, options: Option) { +pub (crate) async fn write_to_db(db_name: &str, collection_name: &str, doc: T, db:Connection, options: Option) { - let insert_result = db.database(&db_name).collection(&collection_name).insert_one(doc, options).await; + let insert_result = db.database(db_name).collection(collection_name).insert_one(doc, options).await; match insert_result { Err(e) => println!("Error inserting record : {e:?}"), From 36746a29f5c8fc54c37992819ce9b119020567f9 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Wed, 2 Oct 2024 16:46:45 -0500 Subject: [PATCH 06/28] adding object id to cookies for mmr retrieval during later sessions --- src/main.rs | 23 ++++++++++++----------- src/store.rs | 7 +++++-- src/utils.rs | 10 ++++++---- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index bb25ef3..1f82bb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, BlockNumber, MurmurStore, }; -use rocket_db_pools::{Database, Connection}; +use rocket_db_pools::{Connection, Database}; use rocket_db_pools::mongodb::{Client, Collection}; use rocket_db_pools::mongodb::bson::{self, doc}; use rocket::http::Status; @@ -37,9 +37,9 @@ use utils::{check_cookie, derive_seed}; const SALT: &str = "your-server-side-secret-salt"; const EPHEM_MSK: [u8; 32] = [1; 32]; -const use_db: bool = false; -const db_name: &str = "MurmurDB"; -const collection_name: &str = "mmrs"; +const USE_DB: bool = false; +const DB_NAME: &str = "MurmurDB"; +const COLLECTION_NAME: &str = "mmrs"; #[derive(Database)] #[database("Murmur")] @@ -129,7 +129,7 @@ async fn login(login_request: Json, cookies: &CookieJar<'_>) -> &' #[post("/new", data = "")] /// Generate a wallet valid for the next {validity} blocks async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection) -> Result { - check_cookie(cookies, |username, seed| async { + check_cookie(cookies, |username, seed, _object_id| async { let (client, current_block_number, round_pubkey_bytes) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; // 1. prepare block schedule @@ -149,8 +149,9 @@ async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection< ) .map_err(|_| Status::InternalServerError)?; // 3. add to storage - if use_db { - store::write_to_db(db_name, collection_name, mmr_store.clone(), db, None).await; + if USE_DB { + let final_object_id = store::write_to_db(DB_NAME, COLLECTION_NAME, mmr_store.clone(), db, None).await; + cookies.add(Cookie::new("object_id", final_object_id.clone())); } else { store::write_to_file(mmr_store.clone()); } @@ -170,7 +171,7 @@ async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection< #[post("/execute", data = "")] /// Execute a transaction from the wallet async fn execute(cookies: &CookieJar<'_>, request: Json, db: Connection) -> Result { - check_cookie(cookies, |username, seed| async { + check_cookie(cookies, |username, seed, object_id| async { let (client, current_block_number, _) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; @@ -185,9 +186,9 @@ async fn execute(cookies: &CookieJar<'_>, request: Json, db: Con }); let store: MurmurStore; - if use_db { - let object_id_string = "str".to_string(); - store = store::load_from_db(object_id_string, db_name, collection_name, db, None).await; + if USE_DB { + let object_id_string = object_id.to_string(); + store = store::load_from_db(object_id_string, DB_NAME, COLLECTION_NAME, db, None).await; } else { store = store::load_from_file(); } diff --git a/src/store.rs b/src/store.rs index ab9e93f..b58e766 100644 --- a/src/store.rs +++ b/src/store.rs @@ -41,7 +41,7 @@ pub(crate) async fn load_from_db(object_id_string: String, db_name: &str, collec let mmr_collection: Collection= db.database(&db_name).collection(&collection_name); - let cursor = mmr_collection.find(filter, options).await?; + let mut cursor = mmr_collection.find(filter, options).await.unwrap(); let mmr = cursor.try_next().await.unwrap().unwrap(); @@ -56,15 +56,18 @@ pub(crate) fn write_to_file(mmr_store: MurmurStore) { serde_cbor::to_writer(mmr_store_file, &mmr_store).unwrap(); } -pub (crate) async fn write_to_db(db_name: &str, collection_name: &str, doc: T, db:Connection, options: Option) { +pub (crate) async fn write_to_db(db_name: &str, collection_name: &str, doc: T, db:Connection, options: Option) -> String { let insert_result = db.database(db_name).collection(collection_name).insert_one(doc, options).await; + let mut object_id = String::new(); match insert_result { Err(e) => println!("Error inserting record : {e:?}"), Ok(insert) => { println!("succesfully inserted record, {insert:?}"); + object_id = String::from(insert.inserted_id.as_str().unwrap()); } } + object_id } diff --git a/src/utils.rs b/src/utils.rs index 8d897a4..2375516 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -22,16 +22,18 @@ pub(crate) async fn check_cookie<'a, F, Fut, R>( callback: F, ) -> Result where - F: FnOnce(&'a str, &'a str) -> Fut, + F: FnOnce(&'a str, &'a str, &'a str) -> Fut, Fut: std::future::Future>, { let username = cookies.get("username"); let seed = cookies.get("seed"); - match (username, seed) { - (Some(username_cookie), Some(seed_cookie)) => { + let object_id = cookies.get("object_id"); + match (username, seed, object_id) { + (Some(username_cookie), Some(seed_cookie), Some(object_id_cookie) ) => { let username = username_cookie.value(); let seed = seed_cookie.value(); - callback(username, seed).await + let object_id = object_id_cookie.value(); + callback(username, seed, object_id).await }, _ => Err(Status::Forbidden), } From 4b73c7f589ead75c1d5e8e5643e19f5c99d55f60 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Wed, 2 Oct 2024 17:06:25 -0500 Subject: [PATCH 07/28] Flipped flag to use mongodb by default, removed test endpoints, cleaned up dependencies --- src/main.rs | 62 +++++------------------------------------------------ 1 file changed, 5 insertions(+), 57 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1f82bb3..a83218e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,11 +25,11 @@ use murmur::{ BlockNumber, MurmurStore, }; use rocket_db_pools::{Connection, Database}; -use rocket_db_pools::mongodb::{Client, Collection}; -use rocket_db_pools::mongodb::bson::{self, doc}; +use rocket_db_pools::mongodb::Client; +use rocket_db_pools::mongodb::bson::doc; use rocket::http::Status; use rocket::http::{Cookie, CookieJar}; -use rocket::serde::{json::Json, Serialize, Deserialize}; +use rocket::serde::{json::Json, Deserialize}; use sp_core::crypto::Ss58Codec; use subxt::utils::{AccountId32, MultiAddress}; use subxt_signer::sr25519::dev; @@ -37,7 +37,7 @@ use utils::{check_cookie, derive_seed}; const SALT: &str = "your-server-side-secret-salt"; const EPHEM_MSK: [u8; 32] = [1; 32]; -const USE_DB: bool = false; +const USE_DB: bool = true; const DB_NAME: &str = "MurmurDB"; const COLLECTION_NAME: &str = "mmrs"; @@ -50,58 +50,6 @@ struct LoginRequest { username: String, password: String, } -#[derive(Serialize, Deserialize)] -struct MMR { - test: String, - test2: String -} - -#[get("/insert")] -async fn insert(db: Connection) { - let test = String::from("abc"); - let test2 = String::from("cde"); - let doc = MMR{test, test2}; - let insert_result = db.database("MurmurDB").collection("mmrs").insert_one(doc, None).await; - - match insert_result { - Err(e) => println!("Error inserting record : {e:?}"), - Ok(insert) => { - println!("succesfully inserted record, {insert:?}"); - } - } -} - -#[get("/delete")] -async fn delete(db: Connection) { - - let test = String::from("abc"); - let test2 = String::from("cde"); - let object = MMR{test, test2}; - - let bson_try = bson::to_bson(&object); - match bson_try { - Err(e) => println!("Error turning object into bson {e:?}"), - Ok(bson_object) => { - - let collection: Collection = db.database("MurmurDB").collection("mmrs"); - - // Can delete by reconstructing the object or by using the object ID that is - // created on record insertion - let query = bson_object.as_document().unwrap(); - // let object_id = ObjectId::parse_str("66fc1dc432627ab776148773").unwrap(); - // let query = doc!{"_id": object_id}; - - - let delete_result = collection.delete_one(query.clone(), None).await; - - // let delete_result = collection.delete_one(query.clone(), None).await; - match delete_result { - Err(e) => println!("Deletion error occurred: {e:?}"), - Ok(success) => println!("Deletion Succeeded {success:?}") - } - } - } -} #[derive(Deserialize)] struct ExecuteRequest { @@ -214,5 +162,5 @@ async fn execute(cookies: &CookieJar<'_>, request: Json, db: Con #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![login, new, execute, insert, delete]).attach(Db::init()) + rocket::build().mount("/", routes![login, new, execute]).attach(Db::init()) } From 5a6c7ee99bd2855ca9d90e9ce3f4e0ab15f1e05f Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 15:46:25 -0500 Subject: [PATCH 08/28] pushing cleanup --- src/main.rs | 37 ++++++++++++++++++++++++------------- src/store.rs | 39 +++++++++++++++++++++++++++------------ src/utils.rs | 2 +- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index a83218e..85b06e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,12 +24,14 @@ use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, BlockNumber, MurmurStore, }; -use rocket_db_pools::{Connection, Database}; -use rocket_db_pools::mongodb::Client; -use rocket_db_pools::mongodb::bson::doc; -use rocket::http::Status; -use rocket::http::{Cookie, CookieJar}; -use rocket::serde::{json::Json, Deserialize}; +use rocket::{ + http::{Cookie, CookieJar, Status}, + serde::{json::Json, Deserialize}, +}; +use rocket_db_pools::{ + mongodb::{bson::doc, Client}, + Connection, Database, +}; use sp_core::crypto::Ss58Codec; use subxt::utils::{AccountId32, MultiAddress}; use subxt_signer::sr25519::dev; @@ -43,7 +45,7 @@ const COLLECTION_NAME: &str = "mmrs"; #[derive(Database)] #[database("Murmur")] -struct Db(Client); +struct Db(Client); #[derive(Deserialize)] struct LoginRequest { @@ -76,7 +78,11 @@ async fn login(login_request: Json, cookies: &CookieJar<'_>) -> &' #[post("/new", data = "")] /// Generate a wallet valid for the next {validity} blocks -async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection) -> Result { +async fn new( + cookies: &CookieJar<'_>, + request: Json, + db: Connection, +) -> Result { check_cookie(cookies, |username, seed, _object_id| async { let (client, current_block_number, round_pubkey_bytes) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; @@ -91,19 +97,20 @@ async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection< let (call, mmr_store) = murmur::create( username.into(), seed.into(), - EPHEM_MSK, // TODO: replace with an hkdf? https://github.com/ideal-lab5/murmur/issues/13 + EPHEM_MSK, /* TODO: replace with an hkdf? https://github.com/ideal-lab5/murmur/issues/13 */ schedule, round_pubkey_bytes, ) .map_err(|_| Status::InternalServerError)?; // 3. add to storage if USE_DB { - let final_object_id = store::write_to_db(DB_NAME, COLLECTION_NAME, mmr_store.clone(), db, None).await; + let final_object_id = + store::write_to_db(DB_NAME, COLLECTION_NAME, mmr_store.clone(), db, None).await; cookies.add(Cookie::new("object_id", final_object_id.clone())); } else { store::write_to_file(mmr_store.clone()); } - + // sign and send the call let from = dev::alice(); let _events = client @@ -118,7 +125,11 @@ async fn new(cookies: &CookieJar<'_>, request: Json, db: Connection< #[post("/execute", data = "")] /// Execute a transaction from the wallet -async fn execute(cookies: &CookieJar<'_>, request: Json, db: Connection) -> Result { +async fn execute( + cookies: &CookieJar<'_>, + request: Json, + db: Connection, +) -> Result { check_cookie(cookies, |username, seed, object_id| async { let (client, current_block_number, _) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; @@ -140,7 +151,7 @@ async fn execute(cookies: &CookieJar<'_>, request: Json, db: Con } else { store = store::load_from_file(); } - + let target_block_number = current_block_number + 1; let tx = murmur::prepare_execute( diff --git a/src/store.rs b/src/store.rs index b58e766..f75e2c5 100644 --- a/src/store.rs +++ b/src/store.rs @@ -15,13 +15,17 @@ */ use murmur::MurmurStore; -use rocket_db_pools::mongodb::{bson::doc, options::{FindOptions, InsertOneOptions}}; +use rocket_db_pools::mongodb::{ + bson::doc, + options::{FindOptions, InsertOneOptions}, +}; use std::fs::File; -use rocket_db_pools::Connection; -use rocket_db_pools::mongodb::Collection; -use rocket_db_pools::mongodb::bson::oid::ObjectId; use rocket::{futures::TryStreamExt, serde::Serialize}; +use rocket_db_pools::{ + mongodb::{bson::oid::ObjectId, Collection}, + Connection, +}; use crate::Db; @@ -33,20 +37,25 @@ pub(crate) fn load_from_file() -> MurmurStore { data } -pub(crate) async fn load_from_db(object_id_string: String, db_name: &str, collection_name: &str, db:Connection, options: Option) -> MurmurStore { - +pub(crate) async fn load_from_db( + object_id_string: String, + db_name: &str, + collection_name: &str, + db: Connection, + options: Option, +) -> MurmurStore { let object_id = ObjectId::parse_str(object_id_string).unwrap(); let filter = doc! {"_id": object_id}; - let mmr_collection: Collection= db.database(&db_name).collection(&collection_name); + let mmr_collection: Collection = + db.database(&db_name).collection(&collection_name); let mut cursor = mmr_collection.find(filter, options).await.unwrap(); let mmr = cursor.try_next().await.unwrap().unwrap(); mmr - } /// Write the MMR data to a file @@ -56,9 +65,15 @@ pub(crate) fn write_to_file(mmr_store: MurmurStore) { serde_cbor::to_writer(mmr_store_file, &mmr_store).unwrap(); } -pub (crate) async fn write_to_db(db_name: &str, collection_name: &str, doc: T, db:Connection, options: Option) -> String { - - let insert_result = db.database(db_name).collection(collection_name).insert_one(doc, options).await; +pub(crate) async fn write_to_db( + db_name: &str, + collection_name: &str, + doc: T, + db: Connection, + options: Option, +) -> String { + let insert_result = + db.database(db_name).collection(collection_name).insert_one(doc, options).await; let mut object_id = String::new(); match insert_result { @@ -66,7 +81,7 @@ pub (crate) async fn write_to_db(db_name: &str, collection_name: &s Ok(insert) => { println!("succesfully inserted record, {insert:?}"); object_id = String::from(insert.inserted_id.as_str().unwrap()); - } + }, } object_id diff --git a/src/utils.rs b/src/utils.rs index 2375516..bece4ba 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -29,7 +29,7 @@ where let seed = cookies.get("seed"); let object_id = cookies.get("object_id"); match (username, seed, object_id) { - (Some(username_cookie), Some(seed_cookie), Some(object_id_cookie) ) => { + (Some(username_cookie), Some(seed_cookie), Some(object_id_cookie)) => { let username = username_cookie.value(); let seed = seed_cookie.value(); let object_id = object_id_cookie.value(); From d7e41af3952ab7c5b002f1929f17985a6af0d0e7 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 16:29:39 -0500 Subject: [PATCH 09/28] removed file store methods. Added error handling to store.rs --- src/main.rs | 26 +++++----------- src/store.rs | 85 ++++++++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 62 deletions(-) diff --git a/src/main.rs b/src/main.rs index 85b06e0..b4abf40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ mod utils; use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, - BlockNumber, MurmurStore, + BlockNumber }; use rocket::{ http::{Cookie, CookieJar, Status}, @@ -39,9 +39,6 @@ use utils::{check_cookie, derive_seed}; const SALT: &str = "your-server-side-secret-salt"; const EPHEM_MSK: [u8; 32] = [1; 32]; -const USE_DB: bool = true; -const DB_NAME: &str = "MurmurDB"; -const COLLECTION_NAME: &str = "mmrs"; #[derive(Database)] #[database("Murmur")] @@ -103,13 +100,9 @@ async fn new( ) .map_err(|_| Status::InternalServerError)?; // 3. add to storage - if USE_DB { - let final_object_id = - store::write_to_db(DB_NAME, COLLECTION_NAME, mmr_store.clone(), db, None).await; - cookies.add(Cookie::new("object_id", final_object_id.clone())); - } else { - store::write_to_file(mmr_store.clone()); - } + + let username_string: String = username.into(); + store::write(username_string, mmr_store.clone(), db, None).await; // sign and send the call let from = dev::alice(); @@ -144,14 +137,9 @@ async fn execute( value: request.amount, }); - let store: MurmurStore; - if USE_DB { - let object_id_string = object_id.to_string(); - store = store::load_from_db(object_id_string, DB_NAME, COLLECTION_NAME, db, None).await; - } else { - store = store::load_from_file(); - } - + // let store: MurmurStore; + let username_string = username.into(); + let store = store::load(username_string, db, None).await.unwrap().mmr; let target_block_number = current_block_number + 1; let tx = murmur::prepare_execute( diff --git a/src/store.rs b/src/store.rs index f75e2c5..b456862 100644 --- a/src/store.rs +++ b/src/store.rs @@ -14,75 +14,74 @@ * limitations under the License. */ -use murmur::MurmurStore; -use rocket_db_pools::mongodb::{ - bson::doc, - options::{FindOptions, InsertOneOptions}, -}; -use std::fs::File; +use rocket::futures::TryStreamExt; +use rocket_db_pools::mongodb::options::{FindOptions, InsertOneOptions}; +use rocket_db_pools::mongodb::error::Error; +use rocket_db_pools::mongodb::bson::doc; -use rocket::{futures::TryStreamExt, serde::Serialize}; +use rocket::serde::{Deserialize, Serialize}; use rocket_db_pools::{ - mongodb::{bson::oid::ObjectId, Collection}, + mongodb::Collection, Connection, }; + use crate::Db; +use murmur::MurmurStore; + +const DB_NAME: &str = "MurmurDB"; +const COLLECTION_NAME: &str = "mmrs"; -pub(crate) fn load_from_file() -> MurmurStore { - // TODO: load from DB - let mmr_store_file = File::open("mmr_store").expect("Unable to open file"); - let data: MurmurStore = serde_cbor::from_reader(mmr_store_file).unwrap(); - data +#[derive (Serialize, Deserialize)] +pub struct MurmurDbObject { + pub mmr: MurmurStore, + pub username: String } -pub(crate) async fn load_from_db( - object_id_string: String, - db_name: &str, - collection_name: &str, +pub(crate) async fn load( + username: &str, db: Connection, options: Option, -) -> MurmurStore { - let object_id = ObjectId::parse_str(object_id_string).unwrap(); +) -> Result { - let filter = doc! {"_id": object_id}; + let filter = doc! {"username": username}; - let mmr_collection: Collection = - db.database(&db_name).collection(&collection_name); + let mmr_collection: Collection = + db.database(&DB_NAME).collection(&COLLECTION_NAME); - let mut cursor = mmr_collection.find(filter, options).await.unwrap(); + let cursor_result = mmr_collection.find(filter, options).await; - let mmr = cursor.try_next().await.unwrap().unwrap(); + match cursor_result { + Err(e) => Err(e), + Ok(mut mmr_cursor) => { + let mmr_next = mmr_cursor.try_next().await; + match mmr_next { + Err(e) => Err(e), + Ok(mmr_option) => { + let mmr = mmr_option.unwrap(); + Ok(mmr) + } + } - mmr -} - -/// Write the MMR data to a file -pub(crate) fn write_to_file(mmr_store: MurmurStore) { - // TODO: write to DB - let mmr_store_file = File::create("mmr_store").expect("It should create the file"); - serde_cbor::to_writer(mmr_store_file, &mmr_store).unwrap(); + } + } } -pub(crate) async fn write_to_db( - db_name: &str, - collection_name: &str, - doc: T, +pub(crate) async fn write( + username: String, + mmr: MurmurStore, db: Connection, options: Option, -) -> String { - let insert_result = - db.database(db_name).collection(collection_name).insert_one(doc, options).await; - let mut object_id = String::new(); +) { + let murmur_data_object = MurmurDbObject{mmr, username}; + let mmr_collection: Collection = db.database(DB_NAME).collection(COLLECTION_NAME); + let insert_result = mmr_collection.insert_one(murmur_data_object, options).await; match insert_result { Err(e) => println!("Error inserting record : {e:?}"), Ok(insert) => { println!("succesfully inserted record, {insert:?}"); - object_id = String::from(insert.inserted_id.as_str().unwrap()); }, } - - object_id } From 6a7c57b69afb63423635cf06d329512ee5468825 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 16:30:58 -0500 Subject: [PATCH 10/28] removed object_id from cookie retrieval --- src/main.rs | 4 ++-- src/utils.rs | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index b4abf40..93ea531 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ async fn new( request: Json, db: Connection, ) -> Result { - check_cookie(cookies, |username, seed, _object_id| async { + check_cookie(cookies, |username, seed| async { let (client, current_block_number, round_pubkey_bytes) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; // 1. prepare block schedule @@ -123,7 +123,7 @@ async fn execute( request: Json, db: Connection, ) -> Result { - check_cookie(cookies, |username, seed, object_id| async { + check_cookie(cookies, |username, seed| async { let (client, current_block_number, _) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; diff --git a/src/utils.rs b/src/utils.rs index bece4ba..8d897a4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -22,18 +22,16 @@ pub(crate) async fn check_cookie<'a, F, Fut, R>( callback: F, ) -> Result where - F: FnOnce(&'a str, &'a str, &'a str) -> Fut, + F: FnOnce(&'a str, &'a str) -> Fut, Fut: std::future::Future>, { let username = cookies.get("username"); let seed = cookies.get("seed"); - let object_id = cookies.get("object_id"); - match (username, seed, object_id) { - (Some(username_cookie), Some(seed_cookie), Some(object_id_cookie)) => { + match (username, seed) { + (Some(username_cookie), Some(seed_cookie)) => { let username = username_cookie.value(); let seed = seed_cookie.value(); - let object_id = object_id_cookie.value(); - callback(username, seed, object_id).await + callback(username, seed).await }, _ => Err(Status::Forbidden), } From 70d15089aae9fc6e92f4ae3553708176ebca51ec Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 16:37:18 -0500 Subject: [PATCH 11/28] moved error handling up one level for writing to DB --- src/main.rs | 6 +++++- src/store.rs | 13 ++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 93ea531..6c88858 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,7 +102,11 @@ async fn new( // 3. add to storage let username_string: String = username.into(); - store::write(username_string, mmr_store.clone(), db, None).await; + let insert_result = store::write(username_string, mmr_store.clone(), db, None).await; + match insert_result { + Err(e) => println!("an error occurred attempting to insert the record: {e:?}"), + Ok(result) => println!("insertion was successful: {result:?}") + } // sign and send the call let from = dev::alice(); diff --git a/src/store.rs b/src/store.rs index b456862..c3ebc99 100644 --- a/src/store.rs +++ b/src/store.rs @@ -20,6 +20,7 @@ use rocket_db_pools::mongodb::error::Error; use rocket_db_pools::mongodb::bson::doc; use rocket::serde::{Deserialize, Serialize}; +use rocket_db_pools::mongodb::results::InsertOneResult; use rocket_db_pools::{ mongodb::Collection, Connection, @@ -73,15 +74,9 @@ pub(crate) async fn write( mmr: MurmurStore, db: Connection, options: Option, -) { +) -> Result{ let murmur_data_object = MurmurDbObject{mmr, username}; let mmr_collection: Collection = db.database(DB_NAME).collection(COLLECTION_NAME); let insert_result = mmr_collection.insert_one(murmur_data_object, options).await; - - match insert_result { - Err(e) => println!("Error inserting record : {e:?}"), - Ok(insert) => { - println!("succesfully inserted record, {insert:?}"); - }, - } -} + insert_result +} \ No newline at end of file From 10050227e00dac96277c23e07254f1ad7408948f Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 16:38:26 -0500 Subject: [PATCH 12/28] code cleanup --- src/store.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/store.rs b/src/store.rs index c3ebc99..7449d83 100644 --- a/src/store.rs +++ b/src/store.rs @@ -18,7 +18,6 @@ use rocket::futures::TryStreamExt; use rocket_db_pools::mongodb::options::{FindOptions, InsertOneOptions}; use rocket_db_pools::mongodb::error::Error; use rocket_db_pools::mongodb::bson::doc; - use rocket::serde::{Deserialize, Serialize}; use rocket_db_pools::mongodb::results::InsertOneResult; use rocket_db_pools::{ @@ -26,14 +25,11 @@ use rocket_db_pools::{ Connection, }; - use crate::Db; use murmur::MurmurStore; - const DB_NAME: &str = "MurmurDB"; const COLLECTION_NAME: &str = "mmrs"; - #[derive (Serialize, Deserialize)] pub struct MurmurDbObject { pub mmr: MurmurStore, @@ -47,10 +43,7 @@ pub(crate) async fn load( ) -> Result { let filter = doc! {"username": username}; - - let mmr_collection: Collection = - db.database(&DB_NAME).collection(&COLLECTION_NAME); - + let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); let cursor_result = mmr_collection.find(filter, options).await; match cursor_result { @@ -64,7 +57,6 @@ pub(crate) async fn load( Ok(mmr) } } - } } } From 1f46df01d7d1f4bcf2b49857b50787644096a874 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 18:23:00 -0500 Subject: [PATCH 13/28] Full error handling implemented. Need different Error codes for none-severe issues such as having no records --- src/main.rs | 44 ++++++++++++++++++++++++++++---------------- src/store.rs | 10 +++++++--- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6c88858..dbe52df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -143,22 +143,34 @@ async fn execute( // let store: MurmurStore; let username_string = username.into(); - let store = store::load(username_string, db, None).await.unwrap().mmr; - let target_block_number = current_block_number + 1; - - let tx = murmur::prepare_execute( - username.into(), - seed.into(), - target_block_number, - store, - balance_transfer_call, - ) - .map_err(|_| Status::InternalServerError)?; - - // submit the tx using alice to sign it - let _ = client.tx().sign_and_submit_then_watch_default(&tx, &dev::alice()).await; - - Ok("Transaction executed".to_string()) + let query_result = store::load(username_string, db, None).await; + + match query_result { + Err(_e) => Err(Status::InternalServerError), + Ok(mmr_option) => { + match mmr_option { + Some(murmur_store) => { + + let target_block_number = current_block_number + 1; + + let tx = murmur::prepare_execute( + username.into(), + seed.into(), + target_block_number, + murmur_store, + balance_transfer_call, + ) + .map_err(|_| Status::InternalServerError)?; + // submit the tx using alice to sign it + let _ = client.tx().sign_and_submit_then_watch_default(&tx, &dev::alice()).await; + Ok("Transaction executed".to_string()) + }, + None => { + Err(Status::InternalServerError) + } + } + } + } }) .await } diff --git a/src/store.rs b/src/store.rs index 7449d83..ae50a96 100644 --- a/src/store.rs +++ b/src/store.rs @@ -40,7 +40,7 @@ pub(crate) async fn load( username: &str, db: Connection, options: Option, -) -> Result { +) -> Result, Error> { let filter = doc! {"username": username}; let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); @@ -53,8 +53,12 @@ pub(crate) async fn load( match mmr_next { Err(e) => Err(e), Ok(mmr_option) => { - let mmr = mmr_option.unwrap(); - Ok(mmr) + match mmr_option { + Some(mmr_db_object) => { + Ok(Some(mmr_db_object.mmr)) + }, + None => Ok(None) + } } } } From f4d9567e6631eb06fd3c0ac574d93339778f7ea6 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 18:28:36 -0500 Subject: [PATCH 14/28] updated status code for no record in db to 400 --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index dbe52df..30136e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -152,7 +152,7 @@ async fn execute( Some(murmur_store) => { let target_block_number = current_block_number + 1; - + let tx = murmur::prepare_execute( username.into(), seed.into(), @@ -166,7 +166,7 @@ async fn execute( Ok("Transaction executed".to_string()) }, None => { - Err(Status::InternalServerError) + Err(Status::BadRequest) } } } From 525d84a3e6fc56af3ed26be51b3ad653cc2eebe2 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 18:32:00 -0500 Subject: [PATCH 15/28] Added error handling to record insertion --- src/main.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index 30136e6..5522475 100644 --- a/src/main.rs +++ b/src/main.rs @@ -83,6 +83,7 @@ async fn new( check_cookie(cookies, |username, seed| async { let (client, current_block_number, round_pubkey_bytes) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; + // 1. prepare block schedule let mut schedule: Vec = Vec::new(); for i in 2..request.validity + 2 { @@ -90,6 +91,7 @@ async fn new( let next_block_number: BlockNumber = current_block_number + i; schedule.push(next_block_number); } + // 2. create mmr let (call, mmr_store) = murmur::create( username.into(), @@ -99,23 +101,23 @@ async fn new( round_pubkey_bytes, ) .map_err(|_| Status::InternalServerError)?; + // 3. add to storage - let username_string: String = username.into(); let insert_result = store::write(username_string, mmr_store.clone(), db, None).await; match insert_result { - Err(e) => println!("an error occurred attempting to insert the record: {e:?}"), - Ok(result) => println!("insertion was successful: {result:?}") + Err(_e) => Err(Status::InternalServerError), + Ok(_result) => { + // sign and send the call + let from = dev::alice(); + let _events = client + .tx() + .sign_and_submit_then_watch_default(&call, &from) + .await + .map_err(|_| Status::InternalServerError)?; + Ok("MMR proxy account creation successful!".to_string()) + } } - - // sign and send the call - let from = dev::alice(); - let _events = client - .tx() - .sign_and_submit_then_watch_default(&call, &from) - .await - .map_err(|_| Status::InternalServerError)?; - Ok("MMR proxy account creation successful!".to_string()) }) .await } @@ -141,7 +143,6 @@ async fn execute( value: request.amount, }); - // let store: MurmurStore; let username_string = username.into(); let query_result = store::load(username_string, db, None).await; From 775ab21880e6705ac7c2c521aa016c4f5bbb8725 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Fri, 4 Oct 2024 18:33:54 -0500 Subject: [PATCH 16/28] updated formatting --- src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5522475..013083d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -109,13 +109,13 @@ async fn new( Err(_e) => Err(Status::InternalServerError), Ok(_result) => { // sign and send the call - let from = dev::alice(); - let _events = client - .tx() - .sign_and_submit_then_watch_default(&call, &from) - .await - .map_err(|_| Status::InternalServerError)?; - Ok("MMR proxy account creation successful!".to_string()) + let from = dev::alice(); + let _events = client + .tx() + .sign_and_submit_then_watch_default(&call, &from) + .await + .map_err(|_| Status::InternalServerError)?; + Ok("MMR proxy account creation successful!".to_string()) } } }) From cfc27fae145a210854f00e26680cc50b183307e1 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Mon, 7 Oct 2024 12:07:01 -0500 Subject: [PATCH 17/28] using Store struct which is managed by Rocket --- src/main.rs | 19 +++++++----- src/store.rs | 86 +++++++++++++++++++++++++++++----------------------- 2 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/main.rs b/src/main.rs index 013083d..23e30a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,9 @@ extern crate rocket; mod store; mod utils; +use store::Store; +use rocket::State; + use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, BlockNumber @@ -29,8 +32,7 @@ use rocket::{ serde::{json::Json, Deserialize}, }; use rocket_db_pools::{ - mongodb::{bson::doc, Client}, - Connection, Database, + mongodb::{bson::doc, Client}, Database, }; use sp_core::crypto::Ss58Codec; use subxt::utils::{AccountId32, MultiAddress}; @@ -78,7 +80,7 @@ async fn login(login_request: Json, cookies: &CookieJar<'_>) -> &' async fn new( cookies: &CookieJar<'_>, request: Json, - db: Connection, + db: &State, ) -> Result { check_cookie(cookies, |username, seed| async { let (client, current_block_number, round_pubkey_bytes) = @@ -104,7 +106,7 @@ async fn new( // 3. add to storage let username_string: String = username.into(); - let insert_result = store::write(username_string, mmr_store.clone(), db, None).await; + let insert_result = db.write(username_string, mmr_store.clone(), None).await; match insert_result { Err(_e) => Err(Status::InternalServerError), Ok(_result) => { @@ -127,7 +129,7 @@ async fn new( async fn execute( cookies: &CookieJar<'_>, request: Json, - db: Connection, + db: &State, ) -> Result { check_cookie(cookies, |username, seed| async { let (client, current_block_number, _) = @@ -144,7 +146,7 @@ async fn execute( }); let username_string = username.into(); - let query_result = store::load(username_string, db, None).await; + let query_result = db.load(username_string, None).await; match query_result { Err(_e) => Err(Status::InternalServerError), @@ -177,6 +179,7 @@ async fn execute( } #[launch] -fn rocket() -> _ { - rocket::build().mount("/", routes![login, new, execute]).attach(Db::init()) +async fn rocket() -> _ { + let store = Store::init().await; + rocket::build().mount("/", routes![login, new, execute]).manage(store) } diff --git a/src/store.rs b/src/store.rs index ae50a96..3b7e7d4 100644 --- a/src/store.rs +++ b/src/store.rs @@ -16,18 +16,17 @@ use rocket::futures::TryStreamExt; use rocket_db_pools::mongodb::options::{FindOptions, InsertOneOptions}; +use rocket_db_pools::mongodb::Client; use rocket_db_pools::mongodb::error::Error; use rocket_db_pools::mongodb::bson::doc; -use rocket::serde::{Deserialize, Serialize}; use rocket_db_pools::mongodb::results::InsertOneResult; -use rocket_db_pools::{ - mongodb::Collection, - Connection, -}; +use rocket_db_pools::mongodb::Collection; +use rocket::serde::{Deserialize, Serialize}; + -use crate::Db; use murmur::MurmurStore; const DB_NAME: &str = "MurmurDB"; +const DB_URI: &str = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast"; const COLLECTION_NAME: &str = "mmrs"; #[derive (Serialize, Deserialize)] @@ -36,43 +35,54 @@ pub struct MurmurDbObject { pub username: String } -pub(crate) async fn load( - username: &str, - db: Connection, - options: Option, -) -> Result, Error> { +pub(crate) struct Store { + pub(crate) col: Collection, +} - let filter = doc! {"username": username}; - let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); - let cursor_result = mmr_collection.find(filter, options).await; +impl Store { + pub(crate) async fn init() -> Self { + let client = Client::with_uri_str(DB_URI).await.unwrap(); + let col = client.database(&DB_NAME).collection(&COLLECTION_NAME); + Store { col } + } - match cursor_result { - Err(e) => Err(e), - Ok(mut mmr_cursor) => { - let mmr_next = mmr_cursor.try_next().await; - match mmr_next { - Err(e) => Err(e), - Ok(mmr_option) => { - match mmr_option { - Some(mmr_db_object) => { - Ok(Some(mmr_db_object.mmr)) - }, - None => Ok(None) + pub(crate) async fn load( + &self, + username: &str, + options: Option, + ) -> Result, Error> { + + let filter = doc! {"username": username}; + // let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); + let cursor_result = self.col.find(filter, options).await; + + match cursor_result { + Err(e) => Err(e), + Ok(mut mmr_cursor) => { + let mmr_next = mmr_cursor.try_next().await; + match mmr_next { + Err(e) => Err(e), + Ok(mmr_option) => { + match mmr_option { + Some(mmr_db_object) => { + Ok(Some(mmr_db_object.mmr)) + }, + None => Ok(None) + } } } } } } + + pub(crate) async fn write( + &self, + username: String, + mmr: MurmurStore, + options: Option, + ) -> Result{ + let murmur_data_object = MurmurDbObject{mmr, username}; + let insert_result = self.col.insert_one(murmur_data_object, options).await; + insert_result + } } - -pub(crate) async fn write( - username: String, - mmr: MurmurStore, - db: Connection, - options: Option, -) -> Result{ - let murmur_data_object = MurmurDbObject{mmr, username}; - let mmr_collection: Collection = db.database(DB_NAME).collection(COLLECTION_NAME); - let insert_result = mmr_collection.insert_one(murmur_data_object, options).await; - insert_result -} \ No newline at end of file From a92addd32ce73b5f0e1dd028a61d08baa5b73128 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Tue, 8 Oct 2024 16:56:25 +0200 Subject: [PATCH 18/28] Update src/main.rs --- src/main.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index f7bc803..7bdddaf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,20 +123,18 @@ async fn new( // 3. add to storage let username_string: String = username.into(); - let insert_result = db.write(username_string, mmr_store.clone(), None).await; - match insert_result { - Err(_e) => Err(Status::InternalServerError), - Ok(_result) => { - // sign and send the call - let from = dev::alice(); - let _events = client - .tx() - .sign_and_submit_then_watch_default(&call, &from) - .await - .map_err(|_| Status::InternalServerError)?; - Ok("MMR proxy account creation successful!".to_string()) - } - } + db.write(username_string, mmr_store.clone(), None) + .await + .map_err(|_| Status::InternalServerError)?; + + // sign and send the call + let from = dev::alice(); + let _events = client + .tx() + .sign_and_submit_then_watch_default(&call, &from) + .await + .map_err(|_| Status::InternalServerError)?; + Ok("MMR proxy account creation successful!".to_string()) }) .await } From 635dcfe0a595e4db91bd1f3d1157d6d4f1b84630 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Tue, 8 Oct 2024 16:56:35 +0200 Subject: [PATCH 19/28] Update src/main.rs --- src/main.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7bdddaf..ed6837f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,10 +59,6 @@ fn get_ephem_msk() -> [u8; 32] { ephem_msk } -#[derive(Database)] -#[database("Murmur")] -struct Db(Client); - #[derive(Deserialize)] struct LoginRequest { username: String, From 733c3555f39c4ad144af6eb23eccee09e2676c0f Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Tue, 8 Oct 2024 16:56:47 +0200 Subject: [PATCH 20/28] Update src/store.rs --- src/store.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/store.rs b/src/store.rs index 3b7e7d4..b836f26 100644 --- a/src/store.rs +++ b/src/store.rs @@ -53,7 +53,6 @@ impl Store { ) -> Result, Error> { let filter = doc! {"username": username}; - // let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); let cursor_result = self.col.find(filter, options).await; match cursor_result { From 683f338238a6ce87ef8ada7e8c7c67a3bf862aa6 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Tue, 8 Oct 2024 16:56:54 +0200 Subject: [PATCH 21/28] Update src/store.rs --- src/store.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/store.rs b/src/store.rs index b836f26..9590820 100644 --- a/src/store.rs +++ b/src/store.rs @@ -53,24 +53,12 @@ impl Store { ) -> Result, Error> { let filter = doc! {"username": username}; - let cursor_result = self.col.find(filter, options).await; - - match cursor_result { - Err(e) => Err(e), - Ok(mut mmr_cursor) => { - let mmr_next = mmr_cursor.try_next().await; - match mmr_next { - Err(e) => Err(e), - Ok(mmr_option) => { - match mmr_option { - Some(mmr_db_object) => { - Ok(Some(mmr_db_object.mmr)) - }, - None => Ok(None) - } - } - } - } + let mut mmr_cursor = self.col.find(filter, options).await?; + + let mmr_option = mmr_cursor.try_next().await?; + match mmr_option { + Some(mmr_db_object) => Ok(Some(mmr_db_object.mmr)), + None => Ok(None), } } From cc4d987976d93316f7fc76405114a6f6faeed11f Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Tue, 8 Oct 2024 17:06:23 +0200 Subject: [PATCH 22/28] chore: clean up code --- Cargo.lock | 194 ++++++++++++++++++++++++--------------------------- Rocket.toml | 2 - src/main.rs | 65 ++++++++--------- src/store.rs | 23 +++--- 4 files changed, 133 insertions(+), 151 deletions(-) delete mode 100644 Rocket.toml diff --git a/Cargo.lock b/Cargo.lock index 8cb85fb..5465812 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -604,9 +604,9 @@ dependencies = [ [[package]] name = "async-stream" -version = "0.3.6" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476" +checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" dependencies = [ "async-stream-impl", "futures-core", @@ -615,13 +615,13 @@ dependencies = [ [[package]] name = "async-stream-impl" -version = "0.3.6" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" +checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -638,7 +638,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -742,6 +742,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "base64ct" version = "1.6.0" @@ -977,9 +983,9 @@ checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "cc" -version = "1.1.24" +version = "1.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "812acba72f0a070b003d3697490d2b55b837230ae7c6c6497f05cc2ddbb8d938" +checksum = "9540e661f81799159abee814118cc139a2004b3a3aa3ea37724a1b66530b90e0" dependencies = [ "shlex", ] @@ -1086,7 +1092,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1309,7 +1315,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1381,7 +1387,7 @@ dependencies = [ "proc-macro2", "quote", "strsim 0.11.1", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1414,7 +1420,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core 0.20.10", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1461,7 +1467,7 @@ checksum = "d65d7ce8132b7c0e54497a4d9a55a1c2a0912a0d786cf894472ba818fba45762" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1474,7 +1480,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version 0.4.1", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1507,7 +1513,7 @@ dependencies = [ "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -1568,7 +1574,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.79", + "syn 2.0.77", "termcolor", "toml", "walkdir", @@ -1787,7 +1793,7 @@ dependencies = [ [[package]] name = "etf-crypto-primitives" version = "0.2.4" -source = "git+http://github.com/ideal-lab5/etf-sdk?branch=w3fbls-migration#c6e61a89b94b57bcbde2293208a2875469f4fc5c" +source = "git+https://github.com/ideal-lab5/etf-sdk.git?branch=w3fbls-migration#c6e61a89b94b57bcbde2293208a2875469f4fc5c" dependencies = [ "aes-gcm", "ark-bls12-377", @@ -1854,7 +1860,7 @@ dependencies = [ "prettyplease", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -2051,7 +2057,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -2228,12 +2234,6 @@ dependencies = [ "serde", ] -[[package]] -name = "hashbrown" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" - [[package]] name = "heck" version = "0.4.1" @@ -2346,9 +2346,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.9.5" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" +checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" [[package]] name = "httpdate" @@ -2477,12 +2477,12 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.6.0" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" +checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" dependencies = [ "equivalent", - "hashbrown 0.15.0", + "hashbrown 0.14.5", "serde", ] @@ -2539,9 +2539,9 @@ dependencies = [ [[package]] name = "ipnet" -version = "2.10.0" +version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "187674a687eed5fe42285b40c6291f9a01517d415fad1c3cbc6a9f778af7fcd4" +checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" [[package]] name = "is-terminal" @@ -3017,7 +3017,7 @@ dependencies = [ [[package]] name = "murmur-core" version = "0.1.0" -source = "git+https://github.com/ideal-lab5/murmur.git#a277aeb84046764e242a136067b6128140bf29ae" +source = "git+https://github.com/ideal-lab5/murmur.git#54f6b6cee5e518f7774df2e04b47a52cae91e924" dependencies = [ "ark-bls12-377", "ark-serialize", @@ -3036,7 +3036,7 @@ dependencies = [ [[package]] name = "murmur-lib" version = "0.1.0" -source = "git+https://github.com/ideal-lab5/murmur.git#a277aeb84046764e242a136067b6128140bf29ae" +source = "git+https://github.com/ideal-lab5/murmur.git#54f6b6cee5e518f7774df2e04b47a52cae91e924" dependencies = [ "ark-bls12-377", "ark-ff", @@ -3174,12 +3174,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.1" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1" -dependencies = [ - "portable-atomic", -] +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "opaque-debug" @@ -3330,7 +3327,7 @@ dependencies = [ "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -3356,7 +3353,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -3426,7 +3423,7 @@ dependencies = [ "polkavm-common", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -3436,7 +3433,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ba81f7b5faac81e528eb6158a6f3c9e0bb1008e0ffa19653bc8dea925ecb429" dependencies = [ "polkavm-derive-impl", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -3477,12 +3474,6 @@ dependencies = [ "universal-hash", ] -[[package]] -name = "portable-atomic" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" - [[package]] name = "powerfmt" version = "0.2.0" @@ -3505,7 +3496,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "479cf940fbbb3426c32c5d5176f62ad57549a0bb84773423ba8be9d089f5faba" dependencies = [ "proc-macro2", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -3581,7 +3572,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", "version_check", "yansi", ] @@ -3659,9 +3650,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.7" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +checksum = "355ae415ccd3a04315d3f8246e86d67689ea74d88d915576e1589a351062a13b" dependencies = [ "bitflags 2.6.0", ] @@ -3683,19 +3674,19 @@ checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] name = "regex" -version = "1.11.0" +version = "1.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.8", - "regex-syntax 0.8.5", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", ] [[package]] @@ -3709,13 +3700,13 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.8" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax 0.8.5", + "regex-syntax 0.8.4", ] [[package]] @@ -3726,9 +3717,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" [[package]] name = "regex-syntax" -version = "0.8.5" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "resolv-conf" @@ -3832,7 +3823,7 @@ dependencies = [ "proc-macro2", "quote", "rocket_http", - "syn 2.0.79", + "syn 2.0.77", "unicode-xid", "version_check", ] @@ -3993,7 +3984,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5bfb394eeed242e909609f56089eecfe5fda225042e8b171791b9c95f5931e5" dependencies = [ "openssl-probe", - "rustls-pemfile 2.2.0", + "rustls-pemfile 2.1.3", "rustls-pki-types", "schannel", "security-framework", @@ -4010,18 +4001,19 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.2.0" +version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +checksum = "196fe16b00e106300d3e45ecfcb764fa292a535d7326a29a5875c579c7417425" dependencies = [ + "base64 0.22.1", "rustls-pki-types", ] [[package]] name = "rustls-pki-types" -version = "1.9.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e696e35370c65c9c541198af4543ccd580cf17fc25d8e05c5a242b202488c55" +checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" [[package]] name = "rustls-webpki" @@ -4188,7 +4180,7 @@ dependencies = [ "proc-macro2", "quote", "scale-info", - "syn 2.0.79", + "syn 2.0.77", "thiserror", ] @@ -4396,7 +4388,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -4760,7 +4752,7 @@ dependencies = [ "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -4848,7 +4840,7 @@ dependencies = [ "bs58", "dyn-clonable", "ed25519-zebra", - "etf-crypto-primitives 0.2.4 (git+http://github.com/ideal-lab5/etf-sdk?branch=w3fbls-migration)", + "etf-crypto-primitives 0.2.4 (git+https://github.com/ideal-lab5/etf-sdk.git?branch=w3fbls-migration)", "futures", "hash-db", "hash256-std-hasher", @@ -4886,7 +4878,7 @@ dependencies = [ [[package]] name = "sp-crypto-ec-utils" version = "0.10.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "ark-bls12-377", "ark-bls12-377-ext", @@ -4937,7 +4929,7 @@ source = "git+https://github.com/ideal-lab5/polkadot-sdk.git?branch=testing#0bd1 dependencies = [ "quote", "sp-crypto-hashing 0.1.0 (git+https://github.com/ideal-lab5/polkadot-sdk.git?branch=testing)", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -4947,17 +4939,17 @@ source = "git+https://github.com/ideal-lab5/polkadot-sdk.git?branch=testing#0bd1 dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] name = "sp-debug-derive" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -4973,7 +4965,7 @@ dependencies = [ [[package]] name = "sp-externalities" version = "0.25.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "environmental", "parity-scale-codec", @@ -5102,7 +5094,7 @@ dependencies = [ [[package]] name = "sp-runtime-interface" version = "24.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "bytes", "impl-trait-for-tuples", @@ -5128,20 +5120,20 @@ dependencies = [ "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] name = "sp-runtime-interface-proc-macro" version = "17.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "Inflector", "expander", "proc-macro-crate 3.2.0", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -5172,7 +5164,7 @@ source = "git+https://github.com/ideal-lab5/polkadot-sdk.git?branch=testing#0bd1 [[package]] name = "sp-std" version = "14.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" [[package]] name = "sp-storage" @@ -5189,7 +5181,7 @@ dependencies = [ [[package]] name = "sp-storage" version = "19.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "impl-serde", "parity-scale-codec", @@ -5212,7 +5204,7 @@ dependencies = [ [[package]] name = "sp-tracing" version = "16.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "parity-scale-codec", "tracing", @@ -5268,7 +5260,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -5284,7 +5276,7 @@ dependencies = [ [[package]] name = "sp-wasm-interface" version = "20.0.0" -source = "git+https://github.com/paritytech/polkadot-sdk#3de2a92598474828b0c170856d76cfe7b8f7b45d" +source = "git+https://github.com/paritytech/polkadot-sdk#a5e40d0cd0a0d941d6fe58aa278fedfcb9102710" dependencies = [ "anyhow", "impl-trait-for-tuples", @@ -5476,7 +5468,7 @@ dependencies = [ "scale-info", "scale-typegen", "subxt-metadata", - "syn 2.0.79", + "syn 2.0.77", "thiserror", "tokio", ] @@ -5510,7 +5502,7 @@ dependencies = [ "quote", "scale-typegen", "subxt-codegen", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -5563,9 +5555,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.79" +version = "2.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" dependencies = [ "proc-macro2", "quote", @@ -5586,9 +5578,9 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "tempfile" -version = "3.13.0" +version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" +checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" dependencies = [ "cfg-if", "fastrand", @@ -5623,7 +5615,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -5707,7 +5699,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -5861,7 +5853,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -6195,7 +6187,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", "wasm-bindgen-shared", ] @@ -6217,7 +6209,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -6559,7 +6551,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] [[package]] @@ -6579,5 +6571,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.77", ] diff --git a/Rocket.toml b/Rocket.toml deleted file mode 100644 index b052397..0000000 --- a/Rocket.toml +++ /dev/null @@ -1,2 +0,0 @@ -[default.databases.Murmur] -url = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ed6837f..3c5f74d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,20 +20,18 @@ extern crate rocket; mod store; mod utils; -use store::Store; use rocket::State; +use store::Store; use murmur::{ etf::{balances::Call, runtime_types::node_template_runtime::RuntimeCall::Balances}, - BlockNumber + BlockNumber, }; use rocket::{ http::{Cookie, CookieJar, Status}, serde::{json::Json, Deserialize}, }; -use rocket_db_pools::{ - mongodb::{bson::doc, Client}, Database, -}; +use rocket_db_pools::mongodb::bson::doc; use sp_core::crypto::Ss58Codec; use std::env; use subxt::utils::{AccountId32, MultiAddress}; @@ -98,7 +96,7 @@ async fn new( check_cookie(cookies, |username, seed| async { let (client, current_block_number, round_pubkey_bytes) = murmur::idn_connect().await.map_err(|_| Status::InternalServerError)?; - + // 1. prepare block schedule let mut schedule: Vec = Vec::new(); for i in 2..request.validity + 2 { @@ -116,7 +114,7 @@ async fn new( round_pubkey_bytes, ) .map_err(|_| Status::InternalServerError)?; - + // 3. add to storage let username_string: String = username.into(); db.write(username_string, mmr_store.clone(), None) @@ -125,7 +123,7 @@ async fn new( // sign and send the call let from = dev::alice(); - let _events = client + client .tx() .sign_and_submit_then_watch_default(&call, &from) .await @@ -157,34 +155,29 @@ async fn execute( }); let username_string = username.into(); - let query_result = db.load(username_string, None).await; - - match query_result { - Err(_e) => Err(Status::InternalServerError), - Ok(mmr_option) => { - match mmr_option { - Some(murmur_store) => { - - let target_block_number = current_block_number + 1; - - let tx = murmur::prepare_execute( - username.into(), - seed.into(), - target_block_number, - murmur_store, - balance_transfer_call, - ) - .map_err(|_| Status::InternalServerError)?; - // submit the tx using alice to sign it - let _ = client.tx().sign_and_submit_then_watch_default(&tx, &dev::alice()).await; - Ok("Transaction executed".to_string()) - }, - None => { - Err(Status::BadRequest) - } - } - } - } + let mmr_option = + db.load(username_string, None).await.map_err(|_| Status::InternalServerError)?; + + let murmur_store = mmr_option.ok_or(Status::BadRequest)?; + + let target_block_number = current_block_number + 1; + + let tx = murmur::prepare_execute( + username.into(), + seed.into(), + target_block_number, + murmur_store, + balance_transfer_call, + ) + .map_err(|_| Status::InternalServerError)?; + + // submit the tx using alice to sign it + client + .tx() + .sign_and_submit_then_watch_default(&tx, &dev::alice()) + .await + .map_err(|_| Status::InternalServerError)?; + Ok("Transaction executed".to_string()) }) .await } diff --git a/src/store.rs b/src/store.rs index 9590820..422d00d 100644 --- a/src/store.rs +++ b/src/store.rs @@ -14,25 +14,25 @@ * limitations under the License. */ +use murmur::MurmurStore; use rocket::futures::TryStreamExt; -use rocket_db_pools::mongodb::options::{FindOptions, InsertOneOptions}; -use rocket_db_pools::mongodb::Client; -use rocket_db_pools::mongodb::error::Error; +use rocket::serde::{Deserialize, Serialize}; use rocket_db_pools::mongodb::bson::doc; +use rocket_db_pools::mongodb::error::Error; +use rocket_db_pools::mongodb::options::{FindOptions, InsertOneOptions}; use rocket_db_pools::mongodb::results::InsertOneResult; +use rocket_db_pools::mongodb::Client; use rocket_db_pools::mongodb::Collection; -use rocket::serde::{Deserialize, Serialize}; - -use murmur::MurmurStore; +// TODO move to env var https://github.com/ideal-lab5/murmur-api/issues/15 const DB_NAME: &str = "MurmurDB"; const DB_URI: &str = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast"; const COLLECTION_NAME: &str = "mmrs"; -#[derive (Serialize, Deserialize)] +#[derive(Serialize, Deserialize)] pub struct MurmurDbObject { pub mmr: MurmurStore, - pub username: String + pub username: String, } pub(crate) struct Store { @@ -51,7 +51,6 @@ impl Store { username: &str, options: Option, ) -> Result, Error> { - let filter = doc! {"username": username}; let mut mmr_cursor = self.col.find(filter, options).await?; @@ -61,14 +60,14 @@ impl Store { None => Ok(None), } } - + pub(crate) async fn write( &self, username: String, mmr: MurmurStore, options: Option, - ) -> Result{ - let murmur_data_object = MurmurDbObject{mmr, username}; + ) -> Result { + let murmur_data_object = MurmurDbObject { mmr, username }; let insert_result = self.col.insert_one(murmur_data_object, options).await; insert_result } From 1aaa4cb7057b42f9e6028596b2d9c14edf080869 Mon Sep 17 00:00:00 2001 From: colemanirby Date: Tue, 8 Oct 2024 11:59:38 -0500 Subject: [PATCH 23/28] reverted State changes back to attaching DB to rocket --- Rocket.toml | 2 ++ src/main.rs | 26 ++++++++++++----------- src/store.rs | 60 +++++++++++++++++++++++----------------------------- 3 files changed, 42 insertions(+), 46 deletions(-) create mode 100644 Rocket.toml diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..b052397 --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,2 @@ +[default.databases.Murmur] +url = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a7052b9..6cf8860 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,16 +25,20 @@ mod utils; use murmur::{BlockNumber, RuntimeCall}; use parity_scale_codec::Decode; use rocket::{ - http::{Cookie, CookieJar, Method, SameSite, Status}, - serde::json::Json, - State, + http::{Cookie, CookieJar, Method, SameSite, Status}, serde::json::Json }; +use rocket_db_pools::mongodb::Client; +use rocket_db_pools::Connection; +use rocket_db_pools::Database; use rocket_cors::{AllowedHeaders, AllowedOrigins, CorsOptions}; use rocket_db_pools::mongodb::bson::doc; -use store::Store; use types::{AuthRequest, CreateRequest, CreateResponse, ExecuteRequest, ExecuteResponse}; use utils::{check_cookie, derive_seed, get_ephem_msk, get_salt, MurmurError}; +#[derive(Database)] +#[database("Murmur")] +struct Db(Client); + #[post("/authenticate", data = "")] /// Authenticate the user and start a session async fn authenticate(auth_request: Json, cookies: &CookieJar<'_>) -> &'static str { @@ -64,7 +68,7 @@ async fn authenticate(auth_request: Json, cookies: &CookieJar<'_>) async fn create( cookies: &CookieJar<'_>, request: Json, - db: &State, + db: Connection, ) -> Result { check_cookie(cookies, |username, seed| async { let round_pubkey_bytes = translate::pubkey_to_bytes(&request.round_pubkey) @@ -79,7 +83,7 @@ async fn create( } // 2. create mmr - let (payload, store) = murmur::create( + let (payload, mmr_store) = murmur::create( username.into(), seed.into(), get_ephem_msk(), // TODO: replace with an hkdf? https://github.com/ideal-lab5/murmur/issues/13 @@ -89,7 +93,7 @@ async fn create( .map_err(|e| (Status::InternalServerError, MurmurError(e).to_string()))?; // 3. add to storage - db.write(username.into(), store.clone()) + store::write(db, username.into(), mmr_store.clone()) .await .map_err(|e| (Status::InternalServerError, e.to_string()))?; @@ -104,11 +108,10 @@ async fn create( async fn execute( cookies: &CookieJar<'_>, request: Json, - db: &State, + db: Connection, ) -> Result { check_cookie(cookies, |username, seed| async { - let mmr_option = db - .load(username.into()) + let mmr_option = store::load(db, username.into()) .await .map_err(|e| (Status::InternalServerError, e.to_string()))?; @@ -136,7 +139,6 @@ async fn execute( #[launch] async fn rocket() -> _ { - let store = Store::init().await; let cors = CorsOptions::default() .allowed_origins(AllowedOrigins::all()) .allowed_methods( @@ -152,6 +154,6 @@ async fn rocket() -> _ { rocket::build() .mount("/", routes![authenticate, create, execute]) - .manage(store) .attach(cors) + .attach(Db::init()) } diff --git a/src/store.rs b/src/store.rs index 6b4b192..5bc287f 100644 --- a/src/store.rs +++ b/src/store.rs @@ -14,18 +14,21 @@ * limitations under the License. */ + use murmur::MurmurStore; use rocket::{ - futures::TryStreamExt, - serde::{Deserialize, Serialize}, + futures::TryStreamExt, serde::{Deserialize, Serialize} }; use rocket_db_pools::mongodb::{ - bson::doc, error::Error, results::InsertOneResult, Client, Collection, + bson::doc, error::Error, results::InsertOneResult, Cursor }; +use rocket_db_pools::Connection; +use rocket_db_pools::mongodb::Collection; +use crate::Db; // TODO move to env var https://github.com/ideal-lab5/murmur-api/issues/15 const DB_NAME: &str = "MurmurDB"; -const DB_URI: &str = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast"; +// const DB_URI: &str = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast"; const COLLECTION_NAME: &str = "mmrs"; #[derive(Serialize, Deserialize)] @@ -34,35 +37,24 @@ pub struct MurmurDbObject { pub username: String, } -pub(crate) struct Store { - pub(crate) col: Collection, -} - -impl Store { - pub(crate) async fn init() -> Self { - let client = Client::with_uri_str(DB_URI).await.unwrap(); - let col = client.database(&DB_NAME).collection(&COLLECTION_NAME); - Store { col } - } - - pub(crate) async fn load(&self, username: &str) -> Result, Error> { - let filter = doc! {"username": username}; - let mut mmr_cursor = self.col.find(filter, None).await?; - - let mmr_option = mmr_cursor.try_next().await?; - match mmr_option { - Some(mmr_db_object) => Ok(Some(mmr_db_object.mmr)), - None => Ok(None), - } - } - - pub(crate) async fn write( - &self, - username: String, - mmr: MurmurStore, - ) -> Result { - let murmur_data_object = MurmurDbObject { mmr, username }; - let insert_result = self.col.insert_one(murmur_data_object, None).await; - insert_result +pub(crate) async fn load(db: Connection, username: &str) -> Result, Error> { + let filter = doc! {"username": username}; + let mmr_collection:Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); + let mut mmr_cursor:Cursor = mmr_collection.find(filter, None).await?; + let mmr_option:Option = mmr_cursor.try_next().await?; + match mmr_option { + Some(mmr_db_object) => Ok(Some(mmr_db_object.mmr)), + None => Ok(None), } } +pub(crate) async fn write( + db: Connection, + username: String, + mmr: MurmurStore, +) -> Result { + let murmur_data_object:MurmurDbObject = MurmurDbObject { mmr, username }; + let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); + let insert_result: Result = mmr_collection.insert_one(murmur_data_object, None).await; + insert_result +} + From c7e8ee84c2b6448f7ae7b6c9d690dd44ffbf059a Mon Sep 17 00:00:00 2001 From: colemanirby Date: Tue, 8 Oct 2024 12:21:52 -0500 Subject: [PATCH 24/28] remove DB_URI from store.rs --- src/store.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/store.rs b/src/store.rs index 5bc287f..00e03a6 100644 --- a/src/store.rs +++ b/src/store.rs @@ -28,7 +28,6 @@ use crate::Db; // TODO move to env var https://github.com/ideal-lab5/murmur-api/issues/15 const DB_NAME: &str = "MurmurDB"; -// const DB_URI: &str = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast"; const COLLECTION_NAME: &str = "mmrs"; #[derive(Serialize, Deserialize)] From f523412e4c23529327a74e18e30b32fbaf429416 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Wed, 9 Oct 2024 10:33:42 +0200 Subject: [PATCH 25/28] chore: remove database url --- .gitignore | 3 ++- README.md | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fe94142..8e9c62f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -mmr_store \ No newline at end of file +mmr_store +Rocket.toml \ No newline at end of file diff --git a/README.md b/README.md index 94ce3fb..1df335e 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,7 @@ In live environments you should set the following environment variables: export SALT="your_salt_string" # 16 chars length export EPHEM_MSK="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32" # 32 comma separated u8 values ``` + +## Database +**Murmur API** uses MongoDB as its database. You should have a running MongoDB instance and set the variable `default.databases.Murmur.url` in `Rocket.toml`. +Rename `Rocket.toml.example` to `Rocket.toml` and set the value of `url` to your MongoDB instance URL. \ No newline at end of file From 37ee90b00202127b7bd46e180c3802b0874f2050 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Wed, 9 Oct 2024 10:33:56 +0200 Subject: [PATCH 26/28] chore: remove database url --- Rocket.toml.example | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Rocket.toml.example diff --git a/Rocket.toml.example b/Rocket.toml.example new file mode 100644 index 0000000..04ee367 --- /dev/null +++ b/Rocket.toml.example @@ -0,0 +1,2 @@ +[default.databases.Murmur] +url = "mongodb+srv://xxxxxxxxx" \ No newline at end of file From b9329c122ea018a817c3aa762e8c147d91e25962 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Wed, 9 Oct 2024 10:35:05 +0200 Subject: [PATCH 27/28] chore: remove rocket.toml --- Rocket.toml | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 Rocket.toml diff --git a/Rocket.toml b/Rocket.toml deleted file mode 100644 index b052397..0000000 --- a/Rocket.toml +++ /dev/null @@ -1,2 +0,0 @@ -[default.databases.Murmur] -url = "mongodb+srv://murmurapi:GuVsTAEbQtNnnbPj@useast.m8j6h.mongodb.net/?retryWrites=true&w=majority&appName=USEast" \ No newline at end of file From 62647678203b29b18053312516b40f5fd6425a22 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Wed, 9 Oct 2024 10:44:46 +0200 Subject: [PATCH 28/28] chore: reorganize imports --- src/main.rs | 8 +++----- src/store.rs | 28 +++++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6cf8860..5c65e64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,13 +25,11 @@ mod utils; use murmur::{BlockNumber, RuntimeCall}; use parity_scale_codec::Decode; use rocket::{ - http::{Cookie, CookieJar, Method, SameSite, Status}, serde::json::Json + http::{Cookie, CookieJar, Method, SameSite, Status}, + serde::json::Json, }; -use rocket_db_pools::mongodb::Client; -use rocket_db_pools::Connection; -use rocket_db_pools::Database; use rocket_cors::{AllowedHeaders, AllowedOrigins, CorsOptions}; -use rocket_db_pools::mongodb::bson::doc; +use store::{Client, Connection, Database}; use types::{AuthRequest, CreateRequest, CreateResponse, ExecuteRequest, ExecuteResponse}; use utils::{check_cookie, derive_seed, get_ephem_msk, get_salt, MurmurError}; diff --git a/src/store.rs b/src/store.rs index 00e03a6..bb22973 100644 --- a/src/store.rs +++ b/src/store.rs @@ -14,17 +14,17 @@ * limitations under the License. */ - +use crate::Db; use murmur::MurmurStore; use rocket::{ - futures::TryStreamExt, serde::{Deserialize, Serialize} + futures::TryStreamExt, + serde::{Deserialize, Serialize}, }; use rocket_db_pools::mongodb::{ - bson::doc, error::Error, results::InsertOneResult, Cursor + bson::doc, error::Error, results::InsertOneResult, Collection, Cursor, }; -use rocket_db_pools::Connection; -use rocket_db_pools::mongodb::Collection; -use crate::Db; + +pub use rocket_db_pools::{mongodb::Client, Connection, Database}; // TODO move to env var https://github.com/ideal-lab5/murmur-api/issues/15 const DB_NAME: &str = "MurmurDB"; @@ -38,9 +38,10 @@ pub struct MurmurDbObject { pub(crate) async fn load(db: Connection, username: &str) -> Result, Error> { let filter = doc! {"username": username}; - let mmr_collection:Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); - let mut mmr_cursor:Cursor = mmr_collection.find(filter, None).await?; - let mmr_option:Option = mmr_cursor.try_next().await?; + let mmr_collection: Collection = + db.database(&DB_NAME).collection(&COLLECTION_NAME); + let mut mmr_cursor: Cursor = mmr_collection.find(filter, None).await?; + let mmr_option: Option = mmr_cursor.try_next().await?; match mmr_option { Some(mmr_db_object) => Ok(Some(mmr_db_object.mmr)), None => Ok(None), @@ -51,9 +52,10 @@ pub(crate) async fn write( username: String, mmr: MurmurStore, ) -> Result { - let murmur_data_object:MurmurDbObject = MurmurDbObject { mmr, username }; - let mmr_collection: Collection = db.database(&DB_NAME).collection(&COLLECTION_NAME); - let insert_result: Result = mmr_collection.insert_one(murmur_data_object, None).await; + let murmur_data_object: MurmurDbObject = MurmurDbObject { mmr, username }; + let mmr_collection: Collection = + db.database(&DB_NAME).collection(&COLLECTION_NAME); + let insert_result: Result = + mmr_collection.insert_one(murmur_data_object, None).await; insert_result } -