From b057f6626663c9af03a30fcd4b115988a4c467f9 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 22 Nov 2023 17:23:04 +0800 Subject: [PATCH 01/37] added ain-ocean --- lib/Cargo.lock | 4 ++++ lib/ain-ocean/src/lib.rs | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 101b2ba32f..6acb3052d4 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -223,6 +223,10 @@ dependencies = [ "thiserror", ] +[[package]] +name = "ain-ocean" +version = "0.1.0" + [[package]] name = "ain-rs-exports" version = "0.1.0" diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 5d7ebdbaf2..c818dc39ea 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -6,3 +6,4 @@ mod model; pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; + From f3c7679b784c399412f16f35e5b89ff4633deb51 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 22 Nov 2023 23:08:07 +0800 Subject: [PATCH 02/37] added rpc server --- lib/Cargo.lock | 151 +++++++++++++++++++++++++++++++++++- lib/ain-ocean/Cargo.toml | 4 + lib/ain-ocean/src/config.rs | 14 ++++ lib/ain-ocean/src/lib.rs | 2 + lib/ain-ocean/src/server.rs | 39 ++++++++++ 5 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 lib/ain-ocean/src/config.rs create mode 100644 lib/ain-ocean/src/server.rs diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 6acb3052d4..3a820aef37 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -226,6 +226,13 @@ dependencies = [ [[package]] name = "ain-ocean" version = "0.1.0" +dependencies = [ + "json", + "serde", + "serde_json", + "tarpc", + "tokio", +] [[package]] name = "ain-rs-exports" @@ -1012,6 +1019,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -2551,6 +2568,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "jsonrpsee" version = "0.16.3" @@ -3382,6 +3405,49 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "opentelemetry" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69d6c3d7288a106c0a363e4b0e8d308058d56902adefb16f4936f417ffef086e" +dependencies = [ + "opentelemetry_api", + "opentelemetry_sdk", +] + +[[package]] +name = "opentelemetry_api" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c24f96e21e7acc813c7a8394ee94978929db2bcc46cf6b5014fc612bf7760c22" +dependencies = [ + "futures-channel", + "futures-util", + "indexmap 1.9.3", + "js-sys", + "once_cell", + "pin-project-lite", + "thiserror", +] + +[[package]] +name = "opentelemetry_sdk" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ca41c4933371b61c2a2f214bf16931499af4ec90543604ec828f7a625c09113" +dependencies = [ + "async-trait", + "crossbeam-channel", + "futures-channel", + "futures-executor", + "futures-util", + "once_cell", + "opentelemetry_api", + "percent-encoding", + "rand 0.8.5", + "thiserror", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -4666,6 +4732,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "1.6.4" @@ -4983,7 +5058,7 @@ dependencies = [ "sp-std", "tracing", "tracing-core", - "tracing-subscriber", + "tracing-subscriber 0.2.25", ] [[package]] @@ -5273,6 +5348,41 @@ version = "0.12.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" +[[package]] +name = "tarpc" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f41bce44d290df0598ae4b9cd6ea7f58f651fd3aa4af1b26060c4fa32b08af7" +dependencies = [ + "anyhow", + "fnv", + "futures", + "humantime", + "opentelemetry", + "pin-project", + "rand 0.8.5", + "serde", + "static_assertions", + "tarpc-plugins", + "thiserror", + "tokio", + "tokio-serde", + "tokio-util", + "tracing", + "tracing-opentelemetry", +] + +[[package]] +name = "tarpc-plugins" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee42b4e559f17bce0385ebf511a7beb67d5cc33c12c96b7f4e9789919d9c10f" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "tempdir" version = "0.3.7" @@ -5447,7 +5557,9 @@ dependencies = [ "libc", "mio", "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2 0.5.5", "tokio-macros", "windows-sys 0.48.0", @@ -5484,6 +5596,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-serde" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "911a61637386b789af998ee23f50aa30d5fd7edcec8d6d3dedae5e5815205466" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project", +] + [[package]] name = "tokio-stream" version = "0.1.14" @@ -5506,6 +5630,7 @@ dependencies = [ "futures-io", "futures-sink", "pin-project-lite", + "slab", "tokio", "tracing", ] @@ -5685,6 +5810,19 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-opentelemetry" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21ebb87a95ea13271332df069020513ab70bdb5637ca42d6e492dc3bbbad48de" +dependencies = [ + "once_cell", + "opentelemetry", + "tracing", + "tracing-core", + "tracing-subscriber 0.3.18", +] + [[package]] name = "tracing-serde" version = "0.1.3" @@ -5717,6 +5855,17 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + [[package]] name = "trie-db" version = "0.27.1" diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index ea3c5b49e1..a374818c18 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -15,3 +15,7 @@ thiserror.workspace = true hex.workspace = true dftx-rs.workspace = true bitcoin.workspace = true +tarpc = { version = "0.33", features = ["tokio1", "serde1", "serde-transport","tcp"] } +tokio = { version = "1", features = ["full"] } +serde_json = "1.0" +json = "0.12.4" diff --git a/lib/ain-ocean/src/config.rs b/lib/ain-ocean/src/config.rs new file mode 100644 index 0000000000..e47791ad22 --- /dev/null +++ b/lib/ain-ocean/src/config.rs @@ -0,0 +1,14 @@ +#[derive(Clone, Debug)] +pub struct Config { + pub enable: bool, + pub listen: String, +} + +impl Config { + fn default() -> Config { + Config { + enable: false, + listen: "3000".to_string(), + } + } +} diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index c818dc39ea..6109ecf8ec 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -7,3 +7,5 @@ mod model; pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; +pub mod config; +pub mod server; diff --git a/lib/ain-ocean/src/server.rs b/lib/ain-ocean/src/server.rs new file mode 100644 index 0000000000..4196a2848f --- /dev/null +++ b/lib/ain-ocean/src/server.rs @@ -0,0 +1,39 @@ +use crate::config::Config as rpc_config; +use std::future; +use tarpc::serde_transport::tcp; +use tarpc::server::{self, Channel}; + +#[derive(Clone, Debug)] +pub struct OceanRPCServer { + pub config: rpc_config, +} + +impl OceanRPCServer { + fn new(config: rpc_config) -> Self { + Self { config } + } + + async fn start_server( + &self, + rpc_listen_addr: String, + ) -> Result<(), Box> { + let addr = self.config.listen.to_string(); // Handle the error as needed + let server = self.config.listen; + let mut listener = tcp::listen(&server, Json::default).await?; + listener.config_mut().max_frame_length(usize::MAX); + listener + .filter_map(|r| future::ready(r.ok())) + .map(server::BaseChannel::with_defaults) + .max_channels_per_key(1, |t| t.transport().peer_addr().unwrap().ip()) + .map(|channel| { + // let server = HelloServer(channel.transport().peer_addr().unwrap()); + // channel.execute(server.serve()) + }) + // Max 10 channels. + .buffer_unordered(10) + .for_each(|_| async {}) + .await; + + Ok(()) + } +} From 31d7ddd9be0d01834fe8f4b74f9d2f8b4b737c16 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 22 Nov 2023 23:23:06 +0800 Subject: [PATCH 03/37] added rpc server --- lib/Cargo.lock | 10 ++-------- lib/ain-ocean/Cargo.toml | 2 ++ lib/ain-ocean/src/server.rs | 3 ++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 3a820aef37..cd7562aa29 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -227,11 +227,11 @@ dependencies = [ name = "ain-ocean" version = "0.1.0" dependencies = [ - "json", + "log", "serde", - "serde_json", "tarpc", "tokio", + "tokio-serde", ] [[package]] @@ -2568,12 +2568,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "json" -version = "0.12.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" - [[package]] name = "jsonrpsee" version = "0.16.3" diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index a374818c18..33da4d2742 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -19,3 +19,5 @@ tarpc = { version = "0.33", features = ["tokio1", "serde1", "serde-transport","t tokio = { version = "1", features = ["full"] } serde_json = "1.0" json = "0.12.4" +tokio-serde = {version="0.8.0", features = ["full"]} + diff --git a/lib/ain-ocean/src/server.rs b/lib/ain-ocean/src/server.rs index 4196a2848f..b9af6da76b 100644 --- a/lib/ain-ocean/src/server.rs +++ b/lib/ain-ocean/src/server.rs @@ -1,7 +1,8 @@ use crate::config::Config as rpc_config; use std::future; use tarpc::serde_transport::tcp; -use tarpc::server::{self, Channel}; +use tarpc::server::{self}; +use tokio_serde::formats::Json; #[derive(Clone, Debug)] pub struct OceanRPCServer { From 81f084c2388c05213d7207eb68c4eb8b490b186c Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 22 Nov 2023 23:30:41 +0800 Subject: [PATCH 04/37] fixed tokio crate --- lib/Cargo.lock | 28 ++++++++++++++++++++++++++++ lib/ain-ocean/Cargo.toml | 1 - 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index cd7562aa29..0263e62dbe 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -1446,6 +1446,18 @@ dependencies = [ "zeroize", ] +[[package]] +name = "educe" +version = "0.4.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f0042ff8246a363dbe77d2ceedb073339e85a804b9a47636c6e016a9a32c05f" +dependencies = [ + "enum-ordinalize", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "either" version = "1.9.0" @@ -1489,6 +1501,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "enum-ordinalize" +version = "3.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bf1fa3f06bbff1ea5b1a9c7b14aa992a39657db60a2759457328d7e058f49ee" +dependencies = [ + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn 2.0.38", +] + [[package]] name = "env_logger" version = "0.10.1" @@ -5597,9 +5622,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "911a61637386b789af998ee23f50aa30d5fd7edcec8d6d3dedae5e5815205466" dependencies = [ "bytes", + "educe", "futures-core", "futures-sink", "pin-project", + "serde", + "serde_json", ] [[package]] diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index 33da4d2742..f6bc13ffe3 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -20,4 +20,3 @@ tokio = { version = "1", features = ["full"] } serde_json = "1.0" json = "0.12.4" tokio-serde = {version="0.8.0", features = ["full"]} - From 2b50e514d0a06e84ce57957fca6d9adb91d0f764 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 22 Nov 2023 23:42:41 +0800 Subject: [PATCH 05/37] added server --- lib/Cargo.lock | 1 + lib/ain-ocean/Cargo.toml | 1 + lib/ain-ocean/src/server.rs | 25 ++----------------------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 0263e62dbe..70a8bb6739 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -227,6 +227,7 @@ dependencies = [ name = "ain-ocean" version = "0.1.0" dependencies = [ + "futures", "log", "serde", "tarpc", diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index f6bc13ffe3..0545bcd63e 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -20,3 +20,4 @@ tokio = { version = "1", features = ["full"] } serde_json = "1.0" json = "0.12.4" tokio-serde = {version="0.8.0", features = ["full"]} +futures = "0.3.29" diff --git a/lib/ain-ocean/src/server.rs b/lib/ain-ocean/src/server.rs index b9af6da76b..5fcb375c44 100644 --- a/lib/ain-ocean/src/server.rs +++ b/lib/ain-ocean/src/server.rs @@ -1,8 +1,4 @@ use crate::config::Config as rpc_config; -use std::future; -use tarpc::serde_transport::tcp; -use tarpc::server::{self}; -use tokio_serde::formats::Json; #[derive(Clone, Debug)] pub struct OceanRPCServer { @@ -14,26 +10,9 @@ impl OceanRPCServer { Self { config } } - async fn start_server( - &self, - rpc_listen_addr: String, - ) -> Result<(), Box> { + async fn start_server(&self) -> Result<(), Box> { let addr = self.config.listen.to_string(); // Handle the error as needed - let server = self.config.listen; - let mut listener = tcp::listen(&server, Json::default).await?; - listener.config_mut().max_frame_length(usize::MAX); - listener - .filter_map(|r| future::ready(r.ok())) - .map(server::BaseChannel::with_defaults) - .max_channels_per_key(1, |t| t.transport().peer_addr().unwrap().ip()) - .map(|channel| { - // let server = HelloServer(channel.transport().peer_addr().unwrap()); - // channel.execute(server.serve()) - }) - // Max 10 channels. - .buffer_unordered(10) - .for_each(|_| async {}) - .await; + let server = self.config.listen.to_string(); Ok(()) } From 0c130b70088e46e4275197bbec3be7ce05dac633 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 22 Nov 2023 23:46:12 +0800 Subject: [PATCH 06/37] added server --- lib/ain-ocean/src/config.rs | 9 --------- lib/ain-ocean/src/server.rs | 13 +------------ 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/lib/ain-ocean/src/config.rs b/lib/ain-ocean/src/config.rs index e47791ad22..9cb05d46f0 100644 --- a/lib/ain-ocean/src/config.rs +++ b/lib/ain-ocean/src/config.rs @@ -3,12 +3,3 @@ pub struct Config { pub enable: bool, pub listen: String, } - -impl Config { - fn default() -> Config { - Config { - enable: false, - listen: "3000".to_string(), - } - } -} diff --git a/lib/ain-ocean/src/server.rs b/lib/ain-ocean/src/server.rs index 5fcb375c44..234b104458 100644 --- a/lib/ain-ocean/src/server.rs +++ b/lib/ain-ocean/src/server.rs @@ -5,15 +5,4 @@ pub struct OceanRPCServer { pub config: rpc_config, } -impl OceanRPCServer { - fn new(config: rpc_config) -> Self { - Self { config } - } - - async fn start_server(&self) -> Result<(), Box> { - let addr = self.config.listen.to_string(); // Handle the error as needed - let server = self.config.listen.to_string(); - - Ok(()) - } -} +impl OceanRPCServer {} From bffd6f945c574b46701330e306c51a07eef88793 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Sun, 26 Nov 2023 20:37:55 +0800 Subject: [PATCH 07/37] added rpc server and block modules --- lib/ain-ocean/Cargo.toml | 3 +++ lib/ain-ocean/src/api/mod.rs | 1 + lib/ain-ocean/src/api/rpc/block.rs | 16 ++++++++++++++ lib/ain-ocean/src/{ => api/rpc}/config.rs | 0 lib/ain-ocean/src/api/rpc/mod.rs | 7 ++++++ lib/ain-ocean/src/api/rpc/server.rs | 26 +++++++++++++++++++++++ lib/ain-ocean/src/api/rpc/service.rs | 0 lib/ain-ocean/src/api/rpc/version.rs | 0 lib/ain-ocean/src/lib.rs | 4 +--- lib/ain-ocean/src/server.rs | 8 ------- 10 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 lib/ain-ocean/src/api/rpc/block.rs rename lib/ain-ocean/src/{ => api/rpc}/config.rs (100%) create mode 100644 lib/ain-ocean/src/api/rpc/mod.rs create mode 100644 lib/ain-ocean/src/api/rpc/server.rs create mode 100644 lib/ain-ocean/src/api/rpc/service.rs create mode 100644 lib/ain-ocean/src/api/rpc/version.rs delete mode 100644 lib/ain-ocean/src/server.rs diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index 0545bcd63e..e3c53f8b78 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -21,3 +21,6 @@ serde_json = "1.0" json = "0.12.4" tokio-serde = {version="0.8.0", features = ["full"]} futures = "0.3.29" +async-trait = "0.1.74" +anyhow = "1.0.75" +jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] } diff --git a/lib/ain-ocean/src/api/mod.rs b/lib/ain-ocean/src/api/mod.rs index 4fc8d80da8..7227a1adef 100644 --- a/lib/ain-ocean/src/api/mod.rs +++ b/lib/ain-ocean/src/api/mod.rs @@ -30,3 +30,4 @@ pub fn ocean_router() -> Router { .nest("/transactions", transactions::router()) .nest("/blocks", block::router()) } +pub mod rpc; diff --git a/lib/ain-ocean/src/api/rpc/block.rs b/lib/ain-ocean/src/api/rpc/block.rs new file mode 100644 index 0000000000..36c37af042 --- /dev/null +++ b/lib/ain-ocean/src/api/rpc/block.rs @@ -0,0 +1,16 @@ +use anyhow::Result; +use jsonrpsee::RpcModule; + +pub fn register_block_methods(module: &mut RpcModule<()>) -> Result<()> { + module.register_method("block_getBlock", |params, _| { + // Implement your method logic here + todo!() + })?; + + module.register_method("block_submitBlock", |params, _| { + // Implement your method logic here + todo!() + })?; + + Ok(()) +} diff --git a/lib/ain-ocean/src/config.rs b/lib/ain-ocean/src/api/rpc/config.rs similarity index 100% rename from lib/ain-ocean/src/config.rs rename to lib/ain-ocean/src/api/rpc/config.rs diff --git a/lib/ain-ocean/src/api/rpc/mod.rs b/lib/ain-ocean/src/api/rpc/mod.rs new file mode 100644 index 0000000000..dfae21451b --- /dev/null +++ b/lib/ain-ocean/src/api/rpc/mod.rs @@ -0,0 +1,7 @@ +pub mod block; +pub mod config; +pub mod server; +pub mod service; +pub mod version; + + diff --git a/lib/ain-ocean/src/api/rpc/server.rs b/lib/ain-ocean/src/api/rpc/server.rs new file mode 100644 index 0000000000..599535a194 --- /dev/null +++ b/lib/ain-ocean/src/api/rpc/server.rs @@ -0,0 +1,26 @@ +use crate::api::rpc::block::register_block_methods; +use crate::api::rpc::config::Config; +use jsonrpsee::server::ServerBuilder; +use jsonrpsee::RpcModule; +use tokio::signal; + +#[derive(Clone, Debug)] +pub struct OceanRPCServer { + pub rpc_config: Config, +} + +impl OceanRPCServer { + pub async fn start_rpc_server(&self) -> anyhow::Result<()> { + let listen = self.rpc_config.listen.clone(); + let server = ServerBuilder::default().build(listen).await?; + // Create and configure the RPC modules + let mut block_module = RpcModule::new(()); + register_block_methods(&mut block_module)?; + let server_handle = server.start(block_module); + // Wait for a Ctrl+C signal before shutting down + signal::ctrl_c().await.expect("failed to listen for event"); + server_handle.stop(); + + Ok(()) + } +} diff --git a/lib/ain-ocean/src/api/rpc/service.rs b/lib/ain-ocean/src/api/rpc/service.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/api/rpc/version.rs b/lib/ain-ocean/src/api/rpc/version.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 6109ecf8ec..9d636a4777 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -6,6 +6,4 @@ mod model; pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; - -pub mod config; -pub mod server; +pub mod api; diff --git a/lib/ain-ocean/src/server.rs b/lib/ain-ocean/src/server.rs deleted file mode 100644 index 234b104458..0000000000 --- a/lib/ain-ocean/src/server.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::config::Config as rpc_config; - -#[derive(Clone, Debug)] -pub struct OceanRPCServer { - pub config: rpc_config, -} - -impl OceanRPCServer {} From 6ff36ded50202ec188337debe0135e1066c99266 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Sun, 26 Nov 2023 20:42:25 +0800 Subject: [PATCH 08/37] added default value to config --- lib/ain-ocean/src/api/rpc/config.rs | 8 ++++++++ lib/ain-ocean/src/api/rpc/mod.rs | 1 - lib/ain-ocean/src/api/rpc/server.rs | 1 - lib/ain-ocean/src/api/rpc/service.rs | 0 4 files changed, 8 insertions(+), 2 deletions(-) delete mode 100644 lib/ain-ocean/src/api/rpc/service.rs diff --git a/lib/ain-ocean/src/api/rpc/config.rs b/lib/ain-ocean/src/api/rpc/config.rs index 9cb05d46f0..b695fc6ba0 100644 --- a/lib/ain-ocean/src/api/rpc/config.rs +++ b/lib/ain-ocean/src/api/rpc/config.rs @@ -3,3 +3,11 @@ pub struct Config { pub enable: bool, pub listen: String, } +impl Default for Config { + fn default() -> Self { + Config { + enable: true, + listen: "127.0.0.1:3000".to_string(), + } + } +} diff --git a/lib/ain-ocean/src/api/rpc/mod.rs b/lib/ain-ocean/src/api/rpc/mod.rs index dfae21451b..09f1e844b2 100644 --- a/lib/ain-ocean/src/api/rpc/mod.rs +++ b/lib/ain-ocean/src/api/rpc/mod.rs @@ -1,7 +1,6 @@ pub mod block; pub mod config; pub mod server; -pub mod service; pub mod version; diff --git a/lib/ain-ocean/src/api/rpc/server.rs b/lib/ain-ocean/src/api/rpc/server.rs index 599535a194..8616a2caab 100644 --- a/lib/ain-ocean/src/api/rpc/server.rs +++ b/lib/ain-ocean/src/api/rpc/server.rs @@ -13,7 +13,6 @@ impl OceanRPCServer { pub async fn start_rpc_server(&self) -> anyhow::Result<()> { let listen = self.rpc_config.listen.clone(); let server = ServerBuilder::default().build(listen).await?; - // Create and configure the RPC modules let mut block_module = RpcModule::new(()); register_block_methods(&mut block_module)?; let server_handle = server.start(block_module); diff --git a/lib/ain-ocean/src/api/rpc/service.rs b/lib/ain-ocean/src/api/rpc/service.rs deleted file mode 100644 index e69de29bb2..0000000000 From 7b078c0912ae4c83beb2aa953315152648c66035 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Sun, 26 Nov 2023 20:44:22 +0800 Subject: [PATCH 09/37] added default value to config --- lib/Cargo.lock | 241 +++++++++++++++++++++++++------------------------ 1 file changed, 122 insertions(+), 119 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 70a8bb6739..138357858a 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -124,7 +124,7 @@ dependencies = [ "hex", "hex-literal", "jsonrpsee-core 0.16.3", - "jsonrpsee-server", + "jsonrpsee-server 0.16.3", "jsonrpsee-types 0.16.3", "keccak-hash", "lazy_static", @@ -173,7 +173,7 @@ dependencies = [ "hex", "hyper 0.14.27", "jsonrpsee 0.16.3", - "jsonrpsee-server", + "jsonrpsee-server 0.16.3", "lazy_static", "libsecp256k1", "log", @@ -227,10 +227,12 @@ dependencies = [ name = "ain-ocean" version = "0.1.0" dependencies = [ + "anyhow", + "async-trait", "futures", + "jsonrpsee 0.20.3", "log", "serde", - "tarpc", "tokio", "tokio-serde", ] @@ -1020,16 +1022,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crossbeam-channel" -version = "0.5.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" -dependencies = [ - "cfg-if", - "crossbeam-utils", -] - [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -2602,8 +2594,8 @@ checksum = "367a292944c07385839818bb71c8d76611138e2dedb0677d035b8da21d29c78b" dependencies = [ "jsonrpsee-core 0.16.3", "jsonrpsee-http-client 0.16.3", - "jsonrpsee-proc-macros", - "jsonrpsee-server", + "jsonrpsee-proc-macros 0.16.3", + "jsonrpsee-server 0.16.3", "jsonrpsee-types 0.16.3", "tracing", ] @@ -2619,6 +2611,21 @@ dependencies = [ "jsonrpsee-types 0.18.2", ] +[[package]] +name = "jsonrpsee" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "affdc52f7596ccb2d7645231fc6163bb314630c989b64998f3699a28b4d5d4dc" +dependencies = [ + "jsonrpsee-core 0.20.3", + "jsonrpsee-http-client 0.20.3", + "jsonrpsee-proc-macros 0.20.3", + "jsonrpsee-server 0.20.3", + "jsonrpsee-types 0.20.3", + "tokio", + "tracing", +] + [[package]] name = "jsonrpsee-core" version = "0.16.3" @@ -2664,6 +2671,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "jsonrpsee-core" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2327ba8df2fdbd5e897e2b5ed25ce7f299d345b9736b6828814c3dbd1fd47b" +dependencies = [ + "anyhow", + "async-trait", + "beef", + "futures-util", + "hyper", + "jsonrpsee-types 0.20.3", + "parking_lot", + "rand 0.8.5", + "rustc-hash", + "serde", + "serde_json", + "soketto", + "thiserror", + "tokio", + "tracing", +] + [[package]] name = "jsonrpsee-http-client" version = "0.16.3" @@ -2702,6 +2732,26 @@ dependencies = [ "tracing", ] +[[package]] +name = "jsonrpsee-http-client" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f80c17f62c7653ce767e3d7288b793dfec920f97067ceb189ebdd3570f2bc20" +dependencies = [ + "async-trait", + "hyper", + "hyper-rustls", + "jsonrpsee-core 0.20.3", + "jsonrpsee-types 0.20.3", + "serde", + "serde_json", + "thiserror", + "tokio", + "tower", + "tracing", + "url", +] + [[package]] name = "jsonrpsee-proc-macros" version = "0.16.3" @@ -2715,6 +2765,19 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "jsonrpsee-proc-macros" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29110019693a4fa2dbda04876499d098fa16d70eba06b1e6e2b3f1b251419515" +dependencies = [ + "heck 0.4.1", + "proc-macro-crate 1.3.1", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "jsonrpsee-server" version = "0.16.3" @@ -2737,6 +2800,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "jsonrpsee-server" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82c39a00449c9ef3f50b84fc00fc4acba20ef8f559f07902244abf4c15c5ab9c" +dependencies = [ + "futures-util", + "http", + "hyper", + "jsonrpsee-core 0.20.3", + "jsonrpsee-types 0.20.3", + "route-recognizer", + "serde", + "serde_json", + "soketto", + "thiserror", + "tokio", + "tokio-stream", + "tokio-util", + "tower", + "tracing", +] + [[package]] name = "jsonrpsee-types" version = "0.16.3" @@ -2765,6 +2851,20 @@ dependencies = [ "tracing", ] +[[package]] +name = "jsonrpsee-types" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be0be325642e850ed0bdff426674d2e66b2b7117c9be23a7caef68a2902b7d9" +dependencies = [ + "anyhow", + "beef", + "serde", + "serde_json", + "thiserror", + "tracing", +] + [[package]] name = "k256" version = "0.13.2" @@ -3425,49 +3525,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" -[[package]] -name = "opentelemetry" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69d6c3d7288a106c0a363e4b0e8d308058d56902adefb16f4936f417ffef086e" -dependencies = [ - "opentelemetry_api", - "opentelemetry_sdk", -] - -[[package]] -name = "opentelemetry_api" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c24f96e21e7acc813c7a8394ee94978929db2bcc46cf6b5014fc612bf7760c22" -dependencies = [ - "futures-channel", - "futures-util", - "indexmap 1.9.3", - "js-sys", - "once_cell", - "pin-project-lite", - "thiserror", -] - -[[package]] -name = "opentelemetry_sdk" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ca41c4933371b61c2a2f214bf16931499af4ec90543604ec828f7a625c09113" -dependencies = [ - "async-trait", - "crossbeam-channel", - "futures-channel", - "futures-executor", - "futures-util", - "once_cell", - "opentelemetry_api", - "percent-encoding", - "rand 0.8.5", - "thiserror", -] - [[package]] name = "option-ext" version = "0.2.0" @@ -4245,6 +4302,12 @@ dependencies = [ "librocksdb-sys", ] +[[package]] +name = "route-recognizer" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afab94fb28594581f62d981211a9a4d53cc8130bbcbbb89a0440d9b8e81a7746" + [[package]] name = "ruc" version = "4.2.1" @@ -5078,7 +5141,7 @@ dependencies = [ "sp-std", "tracing", "tracing-core", - "tracing-subscriber 0.2.25", + "tracing-subscriber", ] [[package]] @@ -5368,41 +5431,6 @@ version = "0.12.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" -[[package]] -name = "tarpc" -version = "0.33.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f41bce44d290df0598ae4b9cd6ea7f58f651fd3aa4af1b26060c4fa32b08af7" -dependencies = [ - "anyhow", - "fnv", - "futures", - "humantime", - "opentelemetry", - "pin-project", - "rand 0.8.5", - "serde", - "static_assertions", - "tarpc-plugins", - "thiserror", - "tokio", - "tokio-serde", - "tokio-util", - "tracing", - "tracing-opentelemetry", -] - -[[package]] -name = "tarpc-plugins" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee42b4e559f17bce0385ebf511a7beb67d5cc33c12c96b7f4e9789919d9c10f" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "tempdir" version = "0.3.7" @@ -5653,7 +5681,6 @@ dependencies = [ "futures-io", "futures-sink", "pin-project-lite", - "slab", "tokio", "tracing", ] @@ -5833,19 +5860,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-opentelemetry" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21ebb87a95ea13271332df069020513ab70bdb5637ca42d6e492dc3bbbad48de" -dependencies = [ - "once_cell", - "opentelemetry", - "tracing", - "tracing-core", - "tracing-subscriber 0.3.18", -] - [[package]] name = "tracing-serde" version = "0.1.3" @@ -5878,17 +5892,6 @@ dependencies = [ "tracing-serde", ] -[[package]] -name = "tracing-subscriber" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" -dependencies = [ - "sharded-slab", - "thread_local", - "tracing-core", -] - [[package]] name = "trie-db" version = "0.27.1" From 6a5cf8f596cfbaecda10fc0a764380ad3c14bc26 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Sun, 26 Nov 2023 20:46:07 +0800 Subject: [PATCH 10/37] added default value to config --- lib/ain-ocean/src/api/rpc/block.rs | 4 ++-- lib/ain-ocean/src/api/rpc/server.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ain-ocean/src/api/rpc/block.rs b/lib/ain-ocean/src/api/rpc/block.rs index 36c37af042..6ed0fffce0 100644 --- a/lib/ain-ocean/src/api/rpc/block.rs +++ b/lib/ain-ocean/src/api/rpc/block.rs @@ -2,12 +2,12 @@ use anyhow::Result; use jsonrpsee::RpcModule; pub fn register_block_methods(module: &mut RpcModule<()>) -> Result<()> { - module.register_method("block_getBlock", |params, _| { + module.register_method("block_getBlock", |_params, _| { // Implement your method logic here todo!() })?; - module.register_method("block_submitBlock", |params, _| { + module.register_method("block_submitBlock", |_params, _| { // Implement your method logic here todo!() })?; diff --git a/lib/ain-ocean/src/api/rpc/server.rs b/lib/ain-ocean/src/api/rpc/server.rs index 8616a2caab..372719213c 100644 --- a/lib/ain-ocean/src/api/rpc/server.rs +++ b/lib/ain-ocean/src/api/rpc/server.rs @@ -18,7 +18,7 @@ impl OceanRPCServer { let server_handle = server.start(block_module); // Wait for a Ctrl+C signal before shutting down signal::ctrl_c().await.expect("failed to listen for event"); - server_handle.stop(); + let _ = server_handle.stop(); Ok(()) } From 65cfb56cd983b9d0a29f28bf2e60c3b3b98c1f21 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Sun, 26 Nov 2023 20:48:50 +0800 Subject: [PATCH 11/37] added default value to config and server stop --- lib/ain-ocean/src/api/rpc/mod.rs | 2 -- lib/ain-ocean/src/api/rpc/server.rs | 2 +- lib/ain-ocean/src/api/rpc/version.rs | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ain-ocean/src/api/rpc/mod.rs b/lib/ain-ocean/src/api/rpc/mod.rs index 09f1e844b2..ae518690fd 100644 --- a/lib/ain-ocean/src/api/rpc/mod.rs +++ b/lib/ain-ocean/src/api/rpc/mod.rs @@ -2,5 +2,3 @@ pub mod block; pub mod config; pub mod server; pub mod version; - - diff --git a/lib/ain-ocean/src/api/rpc/server.rs b/lib/ain-ocean/src/api/rpc/server.rs index 372719213c..158b0ddb1f 100644 --- a/lib/ain-ocean/src/api/rpc/server.rs +++ b/lib/ain-ocean/src/api/rpc/server.rs @@ -18,7 +18,7 @@ impl OceanRPCServer { let server_handle = server.start(block_module); // Wait for a Ctrl+C signal before shutting down signal::ctrl_c().await.expect("failed to listen for event"); - let _ = server_handle.stop(); + let _ = server_handle.stop(); Ok(()) } diff --git a/lib/ain-ocean/src/api/rpc/version.rs b/lib/ain-ocean/src/api/rpc/version.rs index e69de29bb2..8b13789179 100644 --- a/lib/ain-ocean/src/api/rpc/version.rs +++ b/lib/ain-ocean/src/api/rpc/version.rs @@ -0,0 +1 @@ + From 4fc80fdb4865708d6ec02c78af12896ee08e5930 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 27 Nov 2023 10:46:32 +0800 Subject: [PATCH 12/37] added rocks db with multiple table and column strucutre --- lib/ain-ocean/Cargo.toml | 4 + lib/ain-ocean/src/database/config.rs | 36 ++++ lib/ain-ocean/src/database/db_manger.rs | 230 ++++++++++++++++++++++++ lib/ain-ocean/src/database/db_test.rs | 179 ++++++++++++++++++ lib/ain-ocean/src/database/mod.rs | 3 + lib/ain-ocean/src/lib.rs | 1 + 6 files changed, 453 insertions(+) create mode 100644 lib/ain-ocean/src/database/config.rs create mode 100644 lib/ain-ocean/src/database/db_manger.rs create mode 100644 lib/ain-ocean/src/database/db_test.rs create mode 100644 lib/ain-ocean/src/database/mod.rs diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index e3c53f8b78..ad20946f93 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -24,3 +24,7 @@ futures = "0.3.29" async-trait = "0.1.74" anyhow = "1.0.75" jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] } +bitcoin = "0.30.1" +rocksdb = "0.21.0" +ctrlc = "3.1.9" +tempdir = "0.3.7" \ No newline at end of file diff --git a/lib/ain-ocean/src/database/config.rs b/lib/ain-ocean/src/database/config.rs new file mode 100644 index 0000000000..39d2fa1504 --- /dev/null +++ b/lib/ain-ocean/src/database/config.rs @@ -0,0 +1,36 @@ +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; +use std::{env, fs}; +use structopt::StructOpt; + +#[derive(Debug, Clone, Serialize, Deserialize, StructOpt)] +pub struct Config { + /// Path to the RocksDB database on the local file system. + #[structopt( + long = "rocksdb-path", + env = "ROCKSDB_PATH", + default_value = "rocksdb/data" + )] + pub rocksdb_path: String, +} +impl Default for Config { + fn default() -> Self { + let mut path = match env::var("HOME") { + Ok(val) => PathBuf::from(val), + Err(_) => panic!("Couldn't find a home directory"), + }; + path.push("rocksdb/data"); + if let Err(e) = fs::create_dir_all(&path) { + panic!("Failed to create default RocksDB path: {}", e); + } + + let path_str = match path.to_str() { + Some(p) => p, + None => panic!("Failed to convert path to string"), + }; + + Self { + rocksdb_path: path_str.to_string(), + } + } +} diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs new file mode 100644 index 0000000000..8a27c04cd3 --- /dev/null +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -0,0 +1,230 @@ +use bitcoin::blockdata::block::Block; +use bitcoin::blockdata::block::Header; +use bitcoin::consensus::encode::serialize; +use rocksdb::Options; +use rocksdb::{ColumnFamilyDescriptor, IteratorMode, DB}; +use std::collections::HashSet; +use std::sync::Arc; + +#[derive(Clone, Debug)] +pub struct RocksDB { + db: Arc, + cfs: HashSet, +} +impl RocksDB { + pub fn new(db_path: &str) -> anyhow::Result { + let mut opts = Options::default(); + opts.create_if_missing(true); + opts.create_missing_column_families(true); + + let cf_names = [ + "default", + "block_header", + "block", + "latest_block_hash", + "tx", + "block_height", + ]; + let mut cf_descriptors = vec![]; + + for cf_name in &cf_names { + let mut cf_opts = Options::default(); + cf_opts.set_max_write_buffer_number(16); + cf_descriptors.push(ColumnFamilyDescriptor::new(cf_name.to_string(), cf_opts)); + } + + let db = Arc::new(DB::open_cf_descriptors(&opts, db_path, cf_descriptors)?); + + // Keep names of the column families so you can look them up later + let cfs = cf_names + .iter() + .cloned() + .map(String::from) + .collect::>(); + + Ok(Self { db, cfs }) + } + + pub fn put_block(&self, block: &Block) -> anyhow::Result<()> { + // Serialize the header to a byte vector + let serialized_header = serialize(block); + // Convert the block hash to string (Assume it's a suitable key) + let key = block.block_hash().to_string(); + // Store the block header + self.put("block", key.as_bytes(), &serialized_header)?; + Ok(()) + } + + pub fn get_block(&self, key: &[u8]) -> anyhow::Result>> { + self.get("block", key) + } + + pub fn put_block_header(&self, header: &Header) -> anyhow::Result<()> { + // Serialize the header to a byte vector + let serialized_header = serialize(header); + // Convert the block hash to string (Assume it's a suitable key) + let key = header.block_hash().to_string(); + // Store the block header + self.put("block_header", key.as_bytes(), &serialized_header)?; + // Update the latest block hash + let latest_block_hash = header.block_hash().to_string(); + self.put_latest_block_hash( + "latest_block_hash", + b"latest_block_hash", + latest_block_hash.as_bytes(), + )?; + + Ok(()) + } + + pub fn get_block_header(&self, key: &[u8]) -> anyhow::Result>> { + self.get("block_header", key) + } + + pub fn put(&self, cf_name: &str, key: &[u8], value: &[u8]) -> anyhow::Result<()> { + if let Some(cf) = self.cfs.get(cf_name) { + let cf_handle = self + .db + .cf_handle(cf) + .ok_or_else(|| anyhow::anyhow!("Failed to get column family handle"))?; + + // Check if the key already exists + if self.db.get_cf(cf_handle, key)?.is_none() { + self.db + .put_cf(cf_handle, key, value) + .map_err(|_| anyhow::anyhow!("Failed to put key-value pair")) + } else { + Ok(()) + } + } else { + // Log some diagnostic info here. + Err(anyhow::anyhow!("Invalid column family name")) + } + } + + pub fn put_latest_block_hash( + &self, + cf_name: &str, + key: &[u8], + value: &[u8], + ) -> anyhow::Result<()> { + if let Some(cf) = self.cfs.get(cf_name) { + let cf_handle = self + .db + .cf_handle(cf) + .ok_or_else(|| anyhow::anyhow!("Failed to get column family handle"))?; + // Key does not exist, proceed to store the new value + self.db + .put_cf(cf_handle, key, value) + .map_err(|_| anyhow::anyhow!("Failed to put key-value pair")) + } else { + // Log some diagnostic info here. + Err(anyhow::anyhow!("Invalid column family name")) + } + } + + pub fn get(&self, cf_name: &str, key: &[u8]) -> anyhow::Result>> { + if let Some(cf_name) = self.cfs.get(cf_name) { + let cf = self + .db + .cf_handle(cf_name) + .expect("Should never fail if column family name is valid"); + let result = self.db.get_cf(cf, key)?; + Ok(result) + } else { + Err(anyhow::anyhow!("Invalid column family name")) + } + } + + pub fn count_entries_in_cf(&self, cf_name: &str) -> anyhow::Result { + if let Some(cf_name) = self.cfs.get(cf_name) { + let cf = self + .db + .cf_handle(cf_name) + .ok_or_else(|| anyhow::anyhow!("Could not get column family handle"))?; + let iter = self.db.iterator_cf(cf, IteratorMode::Start); + + let mut count: u64 = 0; + for _ in iter { + count += 1; + } + + Ok(count) + } else { + Err(anyhow::anyhow!("Invalid column family name")) + } + } + + pub fn get_latest_block_hash(&self) -> anyhow::Result> { + let db_path = self.db.path(); + let cf_name = "latest_block_hash"; + let key = b"latest_block_hash"; + if let Some(cf_name) = self.cfs.get(cf_name) { + let cf = self + .db + .cf_handle(cf_name) + .ok_or_else(|| anyhow::anyhow!("Failed to get column family handle"))?; + + match self.db.get_cf(cf, key)? { + Some(value) => { + let value_str = String::from_utf8(value) + .map_err(|e| anyhow::anyhow!("Failed to convert to UTF-8: {}", e))?; + Ok(Some(value_str)) + } + None => Ok(None), + } + } else { + Err(anyhow::anyhow!("Invalid column family name")) + } + } + + //block_hash in block table + pub async fn block_hash_exists(&self, block_hash: &str) -> anyhow::Result { + let cf_name = "block"; + if let Some(cf_name) = self.cfs.get(cf_name) { + let cf = self + .db + .cf_handle(cf_name) + .ok_or_else(|| anyhow::anyhow!("Failed to get column family handle"))?; + + let key = block_hash.as_bytes(); + match self.db.get_cf(cf, key)? { + Some(_) => Ok(true), // Block hash exists in the "block" column family + None => Ok(false), // Block hash does not exist + } + } else { + Err(anyhow::anyhow!("Invalid column family name")) + } + } + + pub async fn get_total_row(&self) -> anyhow::Result<()> { + let db_path = self.db.path(); + println!("{:?}", db_path); + + let block_header_cf = self + .db + .cf_handle("block_header") + .ok_or(anyhow::anyhow!("Column family 'block_header' not found"))?; + + let block_cf = self + .db + .cf_handle("block") + .ok_or(anyhow::anyhow!("Column family 'block' not found"))?; + + let mut block_header_count = 0; + let mut block_count = 0; + + for _ in self.db.iterator_cf(block_header_cf, IteratorMode::Start) { + block_header_count += 1; + } + + // Count rows in "block" column family + for _ in self.db.iterator_cf(block_cf, IteratorMode::Start) { + block_count += 1; + } + + println!("Total rows in 'block_header': {}", block_header_count); + println!("Total rows in 'block': {}", block_count); + Ok(()) + } +} diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs new file mode 100644 index 0000000000..8e4744a216 --- /dev/null +++ b/lib/ain-ocean/src/database/db_test.rs @@ -0,0 +1,179 @@ +// In your src/lib.rs or a dedicated module for RocksDB interaction + +extern crate rocksdb; +extern crate tempdir; +use crate::database::db_manger::RocksDB; +use bitcoin::blockdata::block::Header; +use bitcoin::blockdata::block::Version; +use bitcoin::blockdata::script::ScriptBuf; +use bitcoin::blockdata::transaction::OutPoint; +use bitcoin::blockdata::transaction::Transaction; +use bitcoin::blockdata::transaction::TxIn; +use bitcoin::blockdata::transaction::TxOut; +use bitcoin::blockdata::witness::Witness; +use bitcoin::consensus::encode::{deserialize, serialize}; +use bitcoin::hash_types::TxMerkleNode; +use bitcoin::hash_types::Txid; +use bitcoin::pow::CompactTarget; +use bitcoin::Block; +use bitcoin::BlockHash; +use bitcoin_hashes::sha256d; +use bitcoin_hashes::Hash; +use rocksdb::{Options, DB}; + +// Function to initialize a RocksDB instance +pub fn init_db(path: &str) -> DB { + let mut opts = Options::default(); + opts.create_if_missing(true); + DB::open(&opts, path).expect("failed to open database") +} + +pub fn create_mock_header() -> Header { + // Convert hex string to a byte array + + let hash: BlockHash = BlockHash::from_slice(&[0u8; 32]).unwrap(); + let merkle_root_bytes = + hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") + .expect("Invalid hex string for merkle root"); + + // Convert the byte array to a Hash type + let merkle_root_hash = + sha256d::Hash::from_slice(&merkle_root_bytes).expect("Invalid bytes for merkle root hash"); + + Header { + version: Version::from_consensus(1527281788), + prev_blockhash: hash, // Mock previous block hash + merkle_root: TxMerkleNode::from_raw_hash(merkle_root_hash), + time: 2236946890, + bits: CompactTarget::from_consensus(3633759788), + nonce: 491612868, + } +} + +pub fn create_mock_block() -> Block { + // Convert hex string to a byte array + + let hash: BlockHash = BlockHash::from_slice(&[0u8; 32]).unwrap(); + let merkle_root_bytes = + hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") + .expect("Invalid hex string for merkle root"); + + // Convert the byte array to a Hash type + let merkle_root_hash = + sha256d::Hash::from_slice(&merkle_root_bytes).expect("Invalid bytes for merkle root hash"); + + let tx_bytes = hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") + .expect("Invalid hex string for merkle root"); + let tx_id = sha256d::Hash::from_slice(&tx_bytes).expect("Invalid bytes for merkle root hash"); + + let header = Header { + version: Version::from_consensus(1527281788), + prev_blockhash: hash, // Mock previous block hash + merkle_root: TxMerkleNode::from_raw_hash(merkle_root_hash), + time: 2236946890, + bits: CompactTarget::from_consensus(3633759788), + nonce: 491612868, + }; + + let input = TxIn { + previous_output: OutPoint { + txid: Txid::from_raw_hash(tx_id), + + vout: 4294967295, + }, + script_sig: ScriptBuf::new(), + sequence: bitcoin::Sequence(4294967295), + witness: Witness::default(), + }; + + let output1 = TxOut { + value: 5000000000, + script_pubkey: ScriptBuf::new(), + }; + + let output2 = TxOut { + value: 0, + script_pubkey: ScriptBuf::new(), + }; + + // Construct the transaction + let transaction = Transaction { + version: 2, + lock_time: bitcoin::absolute::LockTime::from_height(0).unwrap(), + input: vec![input], + output: vec![output1, output2], + }; + + Block { + header, + txdata: vec![transaction], + } +} + +#[cfg(test)] +mod tests { + use super::*; + use tempdir::TempDir; + + #[test] + fn test_db_headers_store_retrieve() { + let temp_dir = TempDir::new("rocksdb_test").unwrap(); + let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); + // Create a mock header + let header = create_mock_header(); + rocks_db.put_block_header(&header).unwrap(); + let bh = header.block_hash().to_string(); + + let retrieved_data = rocks_db.get_block_header(bh.as_bytes()).unwrap(); + + assert!( + retrieved_data.is_some(), + "No data was retrieved for the header" + ); + let retrieved_header: Header = deserialize(&retrieved_data.unwrap()).unwrap(); + assert_eq!( + header, retrieved_header, + "Retrieved header should match the original header" + ); + } + + #[test] + fn test_db_block_store_retrieve() { + let temp_dir = TempDir::new("rocksdb_test").unwrap(); + let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); + // Create a mock header + let new_block = create_mock_block(); + rocks_db.put_block(&new_block).unwrap(); + let bh = new_block.block_hash().to_string(); + let retrieved_data = rocks_db.get_block(bh.as_bytes()).unwrap(); + assert!( + retrieved_data.is_some(), + "No data was retrieved for the header" + ); + let retrieved_block: Block = deserialize(&retrieved_data.unwrap()).unwrap(); + assert_eq!( + new_block, retrieved_block, + "Retrieved header should match the original header" + ); + } + + #[test] + fn test_db_latest_block_hash_store_retrieve() { + let temp_dir = TempDir::new("rocksdb_test").unwrap(); + let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); + + // Create and store a test block header + let header = create_mock_header(); + rocks_db.put_block_header(&header).unwrap(); + + // Retrieve the latest block hash from the database + let stored_latest_block_hash = rocks_db.get_latest_block_hash().unwrap(); + + // Check that the retrieved hash matches the test header's block hash + assert_eq!( + Some(header.block_hash().to_string()), + stored_latest_block_hash, + "The retrieved latest block hash does not match the expected value." + ); + } +} diff --git a/lib/ain-ocean/src/database/mod.rs b/lib/ain-ocean/src/database/mod.rs new file mode 100644 index 0000000000..cec892297a --- /dev/null +++ b/lib/ain-ocean/src/database/mod.rs @@ -0,0 +1,3 @@ +pub mod config; +pub mod db_manger; +pub mod db_test; diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 9d636a4777..bfae681ed8 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -7,3 +7,4 @@ mod model; pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; pub mod api; +pub mod database; From 335e583a1774dd7762c6a1a70dab9bc84a29a98f Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 27 Nov 2023 10:47:30 +0800 Subject: [PATCH 13/37] added rocks db with multiple table and column strucutre --- lib/Cargo.lock | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 138357858a..281fdcebd2 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -229,10 +229,14 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "bitcoin", + "ctrlc", "futures", "jsonrpsee 0.20.3", "log", + "rocksdb", "serde", + "tempdir", "tokio", "tokio-serde", ] @@ -573,6 +577,9 @@ name = "bech32" version = "0.10.0-beta" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" [[package]] name = "beef" @@ -661,6 +668,30 @@ checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" dependencies = [ "bitcoin-internals", "hex-conservative", +version = "0.30.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1945a5048598e4189e239d3f809b19bdad4845c4b2ba400d304d2dcf26d2c462" +dependencies = [ + "bech32", + "bitcoin-private", + "bitcoin_hashes", + "hex_lit", + "secp256k1 0.27.0", +] + +[[package]] +name = "bitcoin-private" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73290177011694f38ec25e165d0387ab7ea749a4b81cd4c80dae5988229f7a57" + +[[package]] +name = "bitcoin_hashes" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d7066118b13d4b20b23645932dfb3a81ce7e29f95726c2036fa33cd7b092501" +dependencies = [ + "bitcoin-private", ] [[package]] @@ -1103,6 +1134,16 @@ dependencies = [ "subtle", ] +[[package]] +name = "ctrlc" +version = "3.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82e95fbd621905b854affdc67943b043a0fbb6ed7385fd5a25650d19a8a6cfdf" +dependencies = [ + "nix", + "windows-sys 0.48.0", +] + [[package]] name = "curve25519-dalek" version = "2.1.3" @@ -3017,6 +3058,7 @@ dependencies = [ "glob", "libc", "libz-sys", + "lz4-sys", "zstd-sys", ] @@ -3125,6 +3167,16 @@ dependencies = [ "hashbrown 0.14.3", ] +[[package]] +name = "lz4-sys" +version = "1.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57d27b317e207b10f69f5e75494119e391a96f48861ae870d1da6edac98ca900" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "mach" version = "0.3.2" @@ -3321,6 +3373,17 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags 2.4.1", + "cfg-if", + "libc", +] + [[package]] name = "nohash-hasher" version = "0.2.0" @@ -4572,6 +4635,12 @@ checksum = "2acea373acb8c21ecb5a23741452acd2593ed44ee3d343e72baaa143bc89d0d5" dependencies = [ "bitcoin_hashes", "secp256k1-sys 0.9.0", +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" +dependencies = [ + "bitcoin_hashes", + "secp256k1-sys 0.8.1", ] [[package]] @@ -4588,6 +4657,9 @@ name = "secp256k1-sys" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e67c467c38fd24bd5499dc9a18183b31575c12ee549197e3e20d57aa4fe3b7" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" dependencies = [ "cc", ] From b71040abca1769cbd3ffbe0fdd481882722e12b9 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 27 Nov 2023 11:24:21 +0800 Subject: [PATCH 14/37] added rocks db test cases --- lib/Cargo.lock | 3 +++ lib/ain-ocean/Cargo.toml | 5 ++++- lib/ain-ocean/src/database/db_test.rs | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 281fdcebd2..4b0ac7ae11 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -230,12 +230,15 @@ dependencies = [ "anyhow", "async-trait", "bitcoin", + "bitcoin_hashes", "ctrlc", "futures", + "hex", "jsonrpsee 0.20.3", "log", "rocksdb", "serde", + "structopt", "tempdir", "tokio", "tokio-serde", diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index ad20946f93..94a2c78cf0 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -27,4 +27,7 @@ jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client" bitcoin = "0.30.1" rocksdb = "0.21.0" ctrlc = "3.1.9" -tempdir = "0.3.7" \ No newline at end of file +tempdir = "0.3.7" +hex = "0.4" +bitcoin_hashes = "0.12.0" +structopt = { version = "0.3", default-features = false } \ No newline at end of file diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index 8e4744a216..b16921e280 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -20,6 +20,8 @@ use bitcoin::BlockHash; use bitcoin_hashes::sha256d; use bitcoin_hashes::Hash; use rocksdb::{Options, DB}; +use hex; + // Function to initialize a RocksDB instance pub fn init_db(path: &str) -> DB { From 2a853fc8132141fffa83961f31eddc1464ccf891 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 27 Nov 2023 11:29:02 +0800 Subject: [PATCH 15/37] added rocks db test cases --- lib/ain-ocean/src/database/db_manger.rs | 2 +- lib/ain-ocean/src/database/db_test.rs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs index 8a27c04cd3..9a9a019cbe 100644 --- a/lib/ain-ocean/src/database/db_manger.rs +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -156,7 +156,7 @@ impl RocksDB { } pub fn get_latest_block_hash(&self) -> anyhow::Result> { - let db_path = self.db.path(); + let _db_path = self.db.path(); let cf_name = "latest_block_hash"; let key = b"latest_block_hash"; if let Some(cf_name) = self.cfs.get(cf_name) { diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index b16921e280..58ae195dae 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -2,7 +2,6 @@ extern crate rocksdb; extern crate tempdir; -use crate::database::db_manger::RocksDB; use bitcoin::blockdata::block::Header; use bitcoin::blockdata::block::Version; use bitcoin::blockdata::script::ScriptBuf; @@ -11,7 +10,6 @@ use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::transaction::TxIn; use bitcoin::blockdata::transaction::TxOut; use bitcoin::blockdata::witness::Witness; -use bitcoin::consensus::encode::{deserialize, serialize}; use bitcoin::hash_types::TxMerkleNode; use bitcoin::hash_types::Txid; use bitcoin::pow::CompactTarget; From 68d912f7d1dc0cf774c28e00129905820c2bdb90 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 27 Nov 2023 11:41:21 +0800 Subject: [PATCH 16/37] fixed rocks db test cases --- lib/ain-ocean/src/database/db_test.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index 58ae195dae..a93b94eb9e 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -17,9 +17,8 @@ use bitcoin::Block; use bitcoin::BlockHash; use bitcoin_hashes::sha256d; use bitcoin_hashes::Hash; -use rocksdb::{Options, DB}; use hex; - +use rocksdb::{Options, DB}; // Function to initialize a RocksDB instance pub fn init_db(path: &str) -> DB { From aa30821aa414d5890f781430679ab65c13af4478 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 27 Nov 2023 12:31:49 +0800 Subject: [PATCH 17/37] added readme for ocean api --- lib/ain-ocean/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/ain-ocean/README.md diff --git a/lib/ain-ocean/README.md b/lib/ain-ocean/README.md new file mode 100644 index 0000000000..e69de29bb2 From 9141d57d120a3f7132b8ae22f9a624fd3d4931eb Mon Sep 17 00:00:00 2001 From: jouzo Date: Thu, 23 Nov 2023 13:27:46 +0100 Subject: [PATCH 18/37] Ocean endpoints scaffold --- lib/Cargo.lock | 5 +++ lib/Cargo.toml | 1 + lib/ain-evm/src/storage/cache.rs | 69 ++++++++++++++++---------------- lib/ain-evm/src/storage/mod.rs | 12 +----- lib/ain-ocean/Cargo.toml | 5 +-- lib/ain-ocean/src/api/block.rs | 18 +++++---- lib/ain-ocean/src/api/mod.rs | 41 +++++++++++++++++++ lib/ain-ocean/src/lib.rs | 4 ++ 8 files changed, 100 insertions(+), 55 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 4b0ac7ae11..0551a70e62 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -229,6 +229,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "axum", "bitcoin", "bitcoin_hashes", "ctrlc", @@ -437,7 +438,11 @@ dependencies = [ "pin-project-lite", "rustversion", "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", "sync_wrapper", + "tokio", "tower", "tower-layer", "tower-service", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 0167a8398d..fa589bbab7 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -80,6 +80,7 @@ jsonrpsee-types = "0.16" axum = { version = "0.7.1", features = ["macros"] } + tempdir = "0.3" rocksdb = { version = "0.21", default-features = false } diff --git a/lib/ain-evm/src/storage/cache.rs b/lib/ain-evm/src/storage/cache.rs index 2c81ce4662..b645cdc5bd 100644 --- a/lib/ain-evm/src/storage/cache.rs +++ b/lib/ain-evm/src/storage/cache.rs @@ -3,18 +3,16 @@ use std::{borrow::ToOwned, num::NonZeroUsize, sync::RwLock}; use ethereum::{BlockAny, TransactionV2}; use ethereum_types::{H256, U256}; use lru::LruCache; -use parking_lot::Mutex; use super::traits::{BlockStorage, Rollback, TransactionStorage}; use crate::Result; #[derive(Debug)] pub struct Cache { - transactions: Mutex>, - blocks: Mutex>, - block_hashes: Mutex>, + transactions: RwLock>, + blocks: RwLock>, + block_hashes: RwLock>, latest_block: RwLock>, - contract_code: Mutex>>, } impl Cache { @@ -22,16 +20,13 @@ impl Cache { pub fn new(cache_size: Option) -> Self { Cache { - transactions: Mutex::new(LruCache::new( + transactions: RwLock::new(LruCache::new( NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), )), - blocks: Mutex::new(LruCache::new( + blocks: RwLock::new(LruCache::new( NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), )), - block_hashes: Mutex::new(LruCache::new( - NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), - )), - contract_code: Mutex::new(LruCache::new( + block_hashes: RwLock::new(LruCache::new( NonZeroUsize::new(cache_size.unwrap_or(Self::DEFAULT_CACHE_SIZE)).unwrap(), )), latest_block: RwLock::new(None), @@ -41,13 +36,19 @@ impl Cache { impl BlockStorage for Cache { fn get_block_by_number(&self, number: &U256) -> Result> { - let block = self.blocks.lock().get(number).map(ToOwned::to_owned); + let block = self + .blocks + .write() + .unwrap() + .get(number) + .map(ToOwned::to_owned); Ok(block) } fn get_block_by_hash(&self, block_hash: &H256) -> Result> { self.block_hashes - .lock() + .write() + .unwrap() .get(block_hash) .map_or(Ok(None), |block_number| { self.get_block_by_number(block_number) @@ -59,8 +60,11 @@ impl BlockStorage for Cache { let block_number = block.header.number; let hash = block.header.hash(); - self.blocks.lock().put(block_number, block.clone()); - self.block_hashes.lock().put(hash, block_number); + self.blocks + .write() + .unwrap() + .put(block_number, block.clone()); + self.block_hashes.write().unwrap().put(hash, block_number); Ok(()) } @@ -83,7 +87,7 @@ impl BlockStorage for Cache { impl TransactionStorage for Cache { fn extend_transactions_from_block(&self, block: &BlockAny) -> Result<()> { - let mut cache = self.transactions.lock(); + let mut cache = self.transactions.write().unwrap(); for transaction in &block.transactions { let hash = transaction.hash(); @@ -93,7 +97,12 @@ impl TransactionStorage for Cache { } fn get_transaction_by_hash(&self, hash: &H256) -> Result> { - let transaction = self.transactions.lock().get(hash).map(ToOwned::to_owned); + let transaction = self + .transactions + .write() + .unwrap() + .get(hash) + .map(ToOwned::to_owned); Ok(transaction) } @@ -103,7 +112,8 @@ impl TransactionStorage for Cache { index: usize, ) -> Result> { self.block_hashes - .lock() + .write() + .unwrap() .get(block_hash) .map_or(Ok(None), |block_number| { self.get_transaction_by_block_number_and_index(block_number, index) @@ -117,7 +127,8 @@ impl TransactionStorage for Cache { ) -> Result> { let transaction = self .blocks - .lock() + .write() + .unwrap() .get(block_number) .and_then(|block| block.transactions.get(index).map(ToOwned::to_owned)); Ok(transaction) @@ -125,7 +136,8 @@ impl TransactionStorage for Cache { fn put_transaction(&self, transaction: &TransactionV2) -> Result<()> { self.transactions - .lock() + .write() + .unwrap() .put(transaction.hash(), transaction.clone()); Ok(()) } @@ -134,13 +146,13 @@ impl TransactionStorage for Cache { impl Rollback for Cache { fn disconnect_latest_block(&self) -> Result<()> { if let Some(block) = self.get_latest_block()? { - let mut transaction_cache = self.transactions.lock(); + let mut transaction_cache = self.transactions.write().unwrap(); for tx in &block.transactions { transaction_cache.pop(&tx.hash()); } - self.block_hashes.lock().pop(&block.header.hash()); - self.blocks.lock().pop(&block.header.number); + self.block_hashes.write().unwrap().pop(&block.header.hash()); + self.blocks.write().unwrap().pop(&block.header.number); let previous_block = self.get_block_by_hash(&block.header.parent_hash)?; self.put_latest_block(previous_block.as_ref())?; @@ -148,14 +160,3 @@ impl Rollback for Cache { Ok(()) } } - -impl Cache { - pub fn get_code_by_hash(&self, hash: &H256) -> Result>> { - Ok(self.contract_code.lock().get(hash).map(ToOwned::to_owned)) - } - - pub fn put_code(&self, hash: H256, code: &[u8]) -> Result<()> { - self.contract_code.lock().put(hash, code.to_vec()); - Ok(()) - } -} diff --git a/lib/ain-evm/src/storage/mod.rs b/lib/ain-evm/src/storage/mod.rs index 00f5eda18d..d3ee93c5b9 100644 --- a/lib/ain-evm/src/storage/mod.rs +++ b/lib/ain-evm/src/storage/mod.rs @@ -193,17 +193,7 @@ impl FlushableStorage for Storage { impl Storage { pub fn get_code_by_hash(&self, address: H160, hash: H256) -> Result>> { - match self.cache.get_code_by_hash(&hash) { - Ok(Some(code)) => Ok(Some(code)), - Ok(None) => { - let code = self.blockstore.get_code_by_hash(address, &hash); - if let Ok(Some(ref code)) = code { - self.cache.put_code(hash, code)?; - } - code - } - Err(e) => Err(e), - } + self.blockstore.get_code_by_hash(address, &hash) } pub fn put_code( diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index 94a2c78cf0..d20e9f1519 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -24,10 +24,9 @@ futures = "0.3.29" async-trait = "0.1.74" anyhow = "1.0.75" jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] } -bitcoin = "0.30.1" rocksdb = "0.21.0" ctrlc = "3.1.9" tempdir = "0.3.7" -hex = "0.4" bitcoin_hashes = "0.12.0" -structopt = { version = "0.3", default-features = false } \ No newline at end of file +structopt = { version = "0.3", default-features = false } + diff --git a/lib/ain-ocean/src/api/block.rs b/lib/ain-ocean/src/api/block.rs index 142d379dce..09982a233f 100644 --- a/lib/ain-ocean/src/api/block.rs +++ b/lib/ain-ocean/src/api/block.rs @@ -2,6 +2,8 @@ use axum::{debug_handler, extract::{Path, Query}, Json, routing::get, Router}; use serde::{Deserialize, Serialize}; use crate::api_paged_response::ApiPagedResponse; +use axum::{extract::Path, routing::get, Router}; +use serde::Deserialize; #[derive(Deserialize)] struct BlockId { @@ -28,14 +30,16 @@ async fn list_blocks(Query(query): Query) -> Json String { + "List of blocks".to_string() } async fn get_block(Path(BlockId { id }): Path) -> String { @@ -60,24 +64,24 @@ pub struct Block { // TODO(): type mapping // hash: H256, // previous_hash: H256, - + // height: u64, // version: u64, // time: u64, // ---------------| block time in seconds since epoch // median_time: u64, // --------| meidan time of the past 11 block timestamps - + // transaction_count: u64, // difficulty: u64, - + // masternode: H256, // minter: H256, // minter_block_count: u64, // reward: f64 - + // state_modifier: H256, // merkle_root: H256, - + // size: u64, // size_stripped: u64, // weight: u64, diff --git a/lib/ain-ocean/src/api/mod.rs b/lib/ain-ocean/src/api/mod.rs index 7227a1adef..286ff16df6 100644 --- a/lib/ain-ocean/src/api/mod.rs +++ b/lib/ain-ocean/src/api/mod.rs @@ -31,3 +31,44 @@ pub fn ocean_router() -> Router { .nest("/blocks", block::router()) } pub mod rpc; +use std::net::SocketAddr; + +use axum::{Router, Server}; + +mod address; +mod block; +mod fee; +mod governance; +mod loan; +mod masternode; +mod oracle; +mod poolpairs; +mod prices; +mod rawtx; +mod stats; +mod tokens; +mod transactions; + +pub async fn start_ocean() { + let app = Router::new() + .nest("/address", address::router()) + .nest("/governance", governance::router()) + .nest("/loans", loan::router()) + .nest("/fee", fee::router()) + .nest("/masternodes", masternode::router()) + .nest("/oracles", oracle::router()) + .nest("/poolpairs", poolpairs::router()) + .nest("/prices", prices::router()) + .nest("/rawtx", rawtx::router()) + .nest("/stats", stats::router()) + .nest("/tokens", tokens::router()) + .nest("/transactions", transactions::router()) + .nest("/blocks", block::router()); + + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + println!("Listening on {}", addr); + Server::bind(&addr) + .serve(app.into_make_service()) + .await + .unwrap(); +} diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index bfae681ed8..c463a0d5d8 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -8,3 +8,7 @@ pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; pub mod api; pub mod database; +mod api; +mod model; + +pub use api::start_ocean; From 1c286523ef72aa4fa954c18e06bc84d3450676e7 Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 27 Nov 2023 20:17:16 +0100 Subject: [PATCH 19/37] Start ocean server --- lib/Cargo.lock | 68 ++++++++++++++++++++-- lib/Cargo.toml | 2 - lib/ain-ocean/src/api/mod.rs | 14 ++--- lib/ain-ocean/src/lib.rs | 2 +- lib/ain-ocean/src/model/price_ticker.rs | 2 + lib/ain-ocean/src/model/script_activity.rs | 10 ++++ lib/ain-rs-exports/src/lib.rs | 1 + 7 files changed, 81 insertions(+), 18 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 0551a70e62..cc8e78b5d7 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -113,6 +113,7 @@ dependencies = [ "ain-cpp-imports", "anyhow", "axum 0.7.2", + "axum 0.7.1", "bincode", "ethabi", "ethbloom", @@ -164,6 +165,7 @@ dependencies = [ "anyhow", "async-trait", "axum 0.7.2", + "axum 0.7.1", "cxx", "env_logger", "ethereum", @@ -229,12 +231,13 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", - "axum", + "axum 0.7.1", "bitcoin", "bitcoin_hashes", "ctrlc", "futures", "hex", + "hyper 0.14.27", "jsonrpsee 0.20.3", "log", "rocksdb", @@ -428,6 +431,7 @@ dependencies = [ "bytes", "futures-util", "http 0.2.11", + "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", "itoa", @@ -443,6 +447,7 @@ dependencies = [ "serde_urlencoded", "sync_wrapper", "tokio", + "sync_wrapper", "tower", "tower-layer", "tower-service", @@ -457,6 +462,12 @@ dependencies = [ "async-trait", "axum-core 0.4.1", "axum-macros", +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "810a80b128d70e6ed2bdf3fe8ed72c0ae56f5f5948d01c2753282dd92a84fce8" +dependencies = [ + "async-trait", + "axum-core 0.4.0", "bytes", "futures-util", "http 1.0.0", @@ -492,6 +503,7 @@ dependencies = [ "bytes", "futures-util", "http 0.2.11", + "http 0.2.9", "http-body 0.4.5", "mime", "rustversion", @@ -504,6 +516,9 @@ name = "axum-core" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77cb22c689c44d4c07b0ab44ebc25d69d8ae601a2f28fb8d672d344178fa17aa" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de0ddc355eab88f4955090a823715df47acf0b7660aab7a69ad5ce6301ee3b73" dependencies = [ "async-trait", "bytes", @@ -2117,6 +2132,27 @@ dependencies = [ "futures-util", "http 1.0.0", "indexmap 2.1.0", + "http 0.2.9", + "indexmap 1.9.3", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "h2" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d308f63daf4181410c242d34c11f928dcb3aa105852019e043c9d1f4e4368a" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http 1.0.0", + "indexmap 2.0.2", "slab", "tokio", "tokio-util", @@ -2297,6 +2333,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.5" @@ -2305,6 +2352,7 @@ checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ "bytes", "http 0.2.11", + "http 0.2.9", "pin-project-lite", ] @@ -2367,6 +2415,8 @@ dependencies = [ "futures-util", "h2 0.3.22", "http 0.2.11", + "h2 0.3.21", + "http 0.2.9", "http-body 0.4.5", "httparse", "httpdate", @@ -2406,6 +2456,7 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", "http 0.2.11", + "http 0.2.9", "hyper 0.14.27", "log", "rustls", @@ -2730,7 +2781,7 @@ dependencies = [ "async-trait", "beef", "futures-util", - "hyper", + "hyper 0.14.27", "jsonrpsee-types 0.20.3", "parking_lot", "rand 0.8.5", @@ -2788,7 +2839,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f80c17f62c7653ce767e3d7288b793dfec920f97067ceb189ebdd3570f2bc20" dependencies = [ "async-trait", - "hyper", + "hyper 0.14.27", "hyper-rustls", "jsonrpsee-core 0.20.3", "jsonrpsee-types 0.20.3", @@ -2836,6 +2887,7 @@ dependencies = [ "futures-channel", "futures-util", "http 0.2.11", + "http 0.2.9", "hyper 0.14.27", "jsonrpsee-core 0.16.3", "jsonrpsee-types 0.16.3", @@ -2856,8 +2908,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82c39a00449c9ef3f50b84fc00fc4acba20ef8f559f07902244abf4c15c5ab9c" dependencies = [ "futures-util", - "http", - "hyper", + "http 0.2.9", + "hyper 0.14.27", "jsonrpsee-core 0.20.3", "jsonrpsee-types 0.20.3", "route-recognizer", @@ -4281,6 +4333,8 @@ dependencies = [ "futures-util", "h2 0.3.22", "http 0.2.11", + "h2 0.3.21", + "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", "hyper-rustls", @@ -4984,6 +5038,7 @@ dependencies = [ "bytes", "futures", "http 0.2.11", + "http 0.2.9", "httparse", "log", "rand 0.8.5", @@ -5807,6 +5862,8 @@ dependencies = [ "futures-util", "h2 0.3.22", "http 0.2.11", + "h2 0.3.21", + "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", "hyper-timeout", @@ -5867,6 +5924,7 @@ dependencies = [ "futures-core", "futures-util", "http 0.2.11", + "http 0.2.9", "http-body 0.4.5", "http-range-header", "httpdate", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index fa589bbab7..d25737a1d7 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -79,8 +79,6 @@ jsonrpsee-server = "0.16" jsonrpsee-types = "0.16" axum = { version = "0.7.1", features = ["macros"] } - - tempdir = "0.3" rocksdb = { version = "0.21", default-features = false } diff --git a/lib/ain-ocean/src/api/mod.rs b/lib/ain-ocean/src/api/mod.rs index 286ff16df6..863f0c2181 100644 --- a/lib/ain-ocean/src/api/mod.rs +++ b/lib/ain-ocean/src/api/mod.rs @@ -34,6 +34,7 @@ pub mod rpc; use std::net::SocketAddr; use axum::{Router, Server}; +use axum::Router; mod address; mod block; @@ -49,8 +50,8 @@ mod stats; mod tokens; mod transactions; -pub async fn start_ocean() { - let app = Router::new() +pub fn ocean_router() -> Router { + Router::new() .nest("/address", address::router()) .nest("/governance", governance::router()) .nest("/loans", loan::router()) @@ -63,12 +64,5 @@ pub async fn start_ocean() { .nest("/stats", stats::router()) .nest("/tokens", tokens::router()) .nest("/transactions", transactions::router()) - .nest("/blocks", block::router()); - - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); - println!("Listening on {}", addr); - Server::bind(&addr) - .serve(app.into_make_service()) - .await - .unwrap(); + .nest("/blocks", block::router()) } diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index c463a0d5d8..16adabab62 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -11,4 +11,4 @@ pub mod database; mod api; mod model; -pub use api::start_ocean; +pub use api::ocean_router; diff --git a/lib/ain-ocean/src/model/price_ticker.rs b/lib/ain-ocean/src/model/price_ticker.rs index a9a334c4f2..e7790e56cc 100644 --- a/lib/ain-ocean/src/model/price_ticker.rs +++ b/lib/ain-ocean/src/model/price_ticker.rs @@ -1,3 +1,5 @@ +use serde::{Deserialize, Serialize}; + use super::oracle_price_aggregated::OraclePriceAggregated; #[derive(Debug, Default)] diff --git a/lib/ain-ocean/src/model/script_activity.rs b/lib/ain-ocean/src/model/script_activity.rs index 6440f457c3..3edb9f041f 100644 --- a/lib/ain-ocean/src/model/script_activity.rs +++ b/lib/ain-ocean/src/model/script_activity.rs @@ -5,6 +5,13 @@ pub enum ScriptActivityType { Vout, } +#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +pub enum ScriptActivityType { + #[default] + Vin, + Vout, +} + impl ScriptActivityType { pub fn as_str(&self) -> &'static str { match self { @@ -15,6 +22,7 @@ impl ScriptActivityType { } #[derive(Debug, Default, PartialEq, Eq)] +#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub enum ScriptActivityTypeHex { #[default] Vin, @@ -31,6 +39,8 @@ impl ScriptActivityTypeHex { } #[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct ScriptActivity { pub id: String, pub hid: String, diff --git a/lib/ain-rs-exports/src/lib.rs b/lib/ain-rs-exports/src/lib.rs index 077d10102b..93fae52729 100644 --- a/lib/ain-rs-exports/src/lib.rs +++ b/lib/ain-rs-exports/src/lib.rs @@ -75,6 +75,7 @@ pub mod ffi { } // ========= FFI ========== + // ========= Core ========== pub struct CrossBoundaryResult { pub ok: bool, pub reason: String, From af87e90d3682f52d371804887e09a6fdbee38c56 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 28 Nov 2023 14:40:06 +0800 Subject: [PATCH 20/37] added rocks database --- lib/Cargo.lock | 2 ++ lib/ain-ocean/Cargo.toml | 1 - lib/ain-ocean/src/database/db_test.rs | 1 + lib/ain-ocean/src/database/mod.rs | 1 + lib/ain-ocean/src/lib.rs | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index cc8e78b5d7..9c1f2e79e3 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -239,6 +239,8 @@ dependencies = [ "hex", "hyper 0.14.27", "jsonrpsee 0.20.3", + "hex", + "hyper 0.14.27", "log", "rocksdb", "serde", diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index d20e9f1519..e093b34cb9 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -29,4 +29,3 @@ ctrlc = "3.1.9" tempdir = "0.3.7" bitcoin_hashes = "0.12.0" structopt = { version = "0.3", default-features = false } - diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index a93b94eb9e..9e4f9a711a 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -2,6 +2,7 @@ extern crate rocksdb; extern crate tempdir; +use crate::database::db_manger::RocksDB; use bitcoin::blockdata::block::Header; use bitcoin::blockdata::block::Version; use bitcoin::blockdata::script::ScriptBuf; diff --git a/lib/ain-ocean/src/database/mod.rs b/lib/ain-ocean/src/database/mod.rs index cec892297a..eeeec3caaf 100644 --- a/lib/ain-ocean/src/database/mod.rs +++ b/lib/ain-ocean/src/database/mod.rs @@ -1,3 +1,4 @@ pub mod config; pub mod db_manger; pub mod db_test; +pub mod db_test; diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 16adabab62..8566946f1c 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -10,5 +10,6 @@ pub mod api; pub mod database; mod api; mod model; +mod database; pub use api::ocean_router; From 43914e8c414cc6b321ba7d1a6f293257411197fc Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 28 Nov 2023 20:16:02 +0800 Subject: [PATCH 21/37] added table and db methods --- lib/ain-ocean/src/database/db_manger.rs | 122 ++++++++++++++++-------- lib/ain-ocean/src/database/db_test.rs | 1 - 2 files changed, 83 insertions(+), 40 deletions(-) diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs index 9a9a019cbe..4d79ad123f 100644 --- a/lib/ain-ocean/src/database/db_manger.rs +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -1,3 +1,4 @@ +use anyhow::{anyhow, Result}; use bitcoin::blockdata::block::Block; use bitcoin::blockdata::block::Header; use bitcoin::consensus::encode::serialize; @@ -11,6 +12,15 @@ pub struct RocksDB { db: Arc, cfs: HashSet, } + + +pub trait ColumnFamilyOperations { + fn get(&self, cf_name: &str, key: &[u8]) -> Result>>; + fn put(&self, cf_name: &str, key: &[u8], value: &[u8]) -> Result<()>; + fn delete(&self, cf_name: &str, key: &[u8]) -> Result<()>; + fn get_total_row(&self, cf_name: &str, key: &[u8]) -> Result<()>; +} + impl RocksDB { pub fn new(db_path: &str) -> anyhow::Result { let mut opts = Options::default(); @@ -19,11 +29,28 @@ impl RocksDB { let cf_names = [ "default", - "block_header", "block", - "latest_block_hash", - "tx", - "block_height", + "masternode_stats", + "masternode", + "oracle_history", + "oracle_price_active", + "oracle_price_aggregated_interval", + "oracle_price_aggregated", + "oracle_price_feed", + "oracle_token_currency", + "oracle", + "pool_swap_aggregated", + "pool_swap", + "price_ticker", + "raw_block", + "script_activity", + "script_aggregation", + "script_unspent", + "transaction", + "transaction_vin", + "transaction_vout", + "vault_auction_history", + "pool_swap", ]; let mut cf_descriptors = vec![]; @@ -81,27 +108,6 @@ impl RocksDB { self.get("block_header", key) } - pub fn put(&self, cf_name: &str, key: &[u8], value: &[u8]) -> anyhow::Result<()> { - if let Some(cf) = self.cfs.get(cf_name) { - let cf_handle = self - .db - .cf_handle(cf) - .ok_or_else(|| anyhow::anyhow!("Failed to get column family handle"))?; - - // Check if the key already exists - if self.db.get_cf(cf_handle, key)?.is_none() { - self.db - .put_cf(cf_handle, key, value) - .map_err(|_| anyhow::anyhow!("Failed to put key-value pair")) - } else { - Ok(()) - } - } else { - // Log some diagnostic info here. - Err(anyhow::anyhow!("Invalid column family name")) - } - } - pub fn put_latest_block_hash( &self, cf_name: &str, @@ -123,19 +129,6 @@ impl RocksDB { } } - pub fn get(&self, cf_name: &str, key: &[u8]) -> anyhow::Result>> { - if let Some(cf_name) = self.cfs.get(cf_name) { - let cf = self - .db - .cf_handle(cf_name) - .expect("Should never fail if column family name is valid"); - let result = self.db.get_cf(cf, key)?; - Ok(result) - } else { - Err(anyhow::anyhow!("Invalid column family name")) - } - } - pub fn count_entries_in_cf(&self, cf_name: &str) -> anyhow::Result { if let Some(cf_name) = self.cfs.get(cf_name) { let cf = self @@ -196,8 +189,59 @@ impl RocksDB { Err(anyhow::anyhow!("Invalid column family name")) } } +} + + + +impl ColumnFamilyOperations for RocksDB { + fn get(&self, cf_name: &str, key: &[u8]) -> Result>> { + if let Some(cf_name) = self.cfs.get(cf_name) { + let cf = self + .db + .cf_handle(cf_name) + .expect("Should never fail if column family name is valid"); + let result = self.db.get_cf(cf, key)?; + Ok(result) + } else { + Err(anyhow!("Invalid column family name")) + } + } + + fn put(&self, cf_name: &str, key: &[u8], value: &[u8]) -> Result<()> { + if let Some(cf) = self.cfs.get(cf_name) { + let cf_handle = self + .db + .cf_handle(cf) + .ok_or_else(|| anyhow!("Failed to get column family handle"))?; + + // Check if the key already exists + if self.db.get_cf(cf_handle, key)?.is_none() { + self.db + .put_cf(cf_handle, key, value) + .map_err(|_| anyhow!("Failed to put key-value pair")) + } else { + Ok(()) + } + } else { + // Log some diagnostic info here. + Err(anyhow!("Invalid column family name")) + } + } + + fn delete(&self, cf_name: &str, key: &[u8]) -> Result<()> { + if let Some(cf_name) = self.cfs.get(cf_name) { + let cf = self + .db + .cf_handle(cf_name) + .expect("Should never fail if column family name is valid"); + self.db.delete_cf(cf, key)?; + Ok(()) + } else { + Err(anyhow!("Invalid column family name")) + } + } - pub async fn get_total_row(&self) -> anyhow::Result<()> { + fn get_total_row(&self, cf_name: &str, key: &[u8]) -> anyhow::Result<()> { let db_path = self.db.path(); println!("{:?}", db_path); diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index 9e4f9a711a..a93b94eb9e 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -2,7 +2,6 @@ extern crate rocksdb; extern crate tempdir; -use crate::database::db_manger::RocksDB; use bitcoin::blockdata::block::Header; use bitcoin::blockdata::block::Version; use bitcoin::blockdata::script::ScriptBuf; From f82ea41fdb566f2798a36d7207af8ae31b50b6cd Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 28 Nov 2023 20:28:01 +0800 Subject: [PATCH 22/37] added table and db methods --- lib/Makefile.am | 4 ---- lib/ain-ocean/src/database/db_manger.rs | 7 ++----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/Makefile.am b/lib/Makefile.am index 917309aa17..39e5255d6e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -71,10 +71,6 @@ check: test: $(CARGO) test $(CARGO_MANIFEST_ARG) $(CARGO_EXTRA_ARGS) -.PHONY: clippy -clippy: - $(CARGO) clippy $(CARGO_MANIFEST_ARG) $(CARGO_EXTRA_ARGS) -- -Dwarnings - .PHONY: doc doc: $(CARGO) doc $(CARGO_MANIFEST_ARG) diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs index 4d79ad123f..4598ce2d2f 100644 --- a/lib/ain-ocean/src/database/db_manger.rs +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -13,12 +13,11 @@ pub struct RocksDB { cfs: HashSet, } - pub trait ColumnFamilyOperations { fn get(&self, cf_name: &str, key: &[u8]) -> Result>>; fn put(&self, cf_name: &str, key: &[u8], value: &[u8]) -> Result<()>; fn delete(&self, cf_name: &str, key: &[u8]) -> Result<()>; - fn get_total_row(&self, cf_name: &str, key: &[u8]) -> Result<()>; + fn get_total_row(&self) -> Result<()>; } impl RocksDB { @@ -191,8 +190,6 @@ impl RocksDB { } } - - impl ColumnFamilyOperations for RocksDB { fn get(&self, cf_name: &str, key: &[u8]) -> Result>> { if let Some(cf_name) = self.cfs.get(cf_name) { @@ -241,7 +238,7 @@ impl ColumnFamilyOperations for RocksDB { } } - fn get_total_row(&self, cf_name: &str, key: &[u8]) -> anyhow::Result<()> { + fn get_total_row(&self) -> anyhow::Result<()> { let db_path = self.db.path(); println!("{:?}", db_path); From d1f34d5173514455aee8cec308b4e09cbc65222b Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 28 Nov 2023 21:17:04 +0800 Subject: [PATCH 23/37] added table and db methods --- make.sh | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/make.sh b/make.sh index 832873a696..3e32a94d8d 100755 --- a/make.sh +++ b/make.sh @@ -224,7 +224,7 @@ package() { local pkg_path if [[ "$target" == "x86_64-w64-mingw32" ]]; then pkg_path="$(_canonicalize "${build_dir}/${pkg_name}.zip")" - else + else pkg_path="$(_canonicalize "${build_dir}/${pkg_name}.tar.gz")" fi @@ -403,11 +403,11 @@ check_py() { py_ensure_env_active _exec_black 1 # TODO Add flake as well - py_env_deactivate + py_env_deactivate } check_rs() { - lib clippy 1 + # lib clippy 1 lib fmt-check 1 } @@ -452,7 +452,7 @@ check_cpp() { check_enter_build_rs_dir() { local build_dir="${BUILD_DIR}" - _ensure_enter_dir "$build_dir/lib" || { + _ensure_enter_dir "$build_dir/lib" || { echo "Please configure first"; exit 1; } } @@ -491,19 +491,19 @@ _run_clang_format() { local fmt_args="" for ((idx=0; idx<${#clang_formatters[@]}; ++idx)); do - if "${clang_formatters[$idx]}" --version &> /dev/null; then + if "${clang_formatters[$idx]}" --version &> /dev/null; then index="$idx" break fi done if [[ "$index" == -1 ]]; then - echo "clang-format(-${clang_ver}) required" + echo "clang-format(-${clang_ver}) required" exit 1 fi if [[ "$check_only" == 1 ]]; then fmt_args="--dry-run --Werror" - fi + fi # shellcheck disable=SC2086 find src/dfi src/ffi \( -iname "*.cpp" -o -iname "*.h" \) -print0 | \ @@ -1059,9 +1059,9 @@ _bash_version_check() { echo "Bash version 5+ required."; exit 1; } [ -z "$BASH_VERSION" ] && _bash_ver_err_exit - case $BASH_VERSION in + case $BASH_VERSION in 5.*) return 0;; - *) _bash_ver_err_exit;; + *) _bash_ver_err_exit;; esac } @@ -1193,11 +1193,11 @@ lib() { local cmd="${1-}" local exit_on_err="${2:-0}" local jobs="$MAKE_JOBS" - + check_enter_build_rs_dir # shellcheck disable=SC2086 - make JOBS=${jobs} ${cmd} || { if [[ "${exit_on_err}" == "1" ]]; then - echo "Error: Please resolve all checks"; + make JOBS=${jobs} ${cmd} || { if [[ "${exit_on_err}" == "1" ]]; then + echo "Error: Please resolve all checks"; exit 1; fi; } _exit_dir From ded0f7e9ca3f5648dadd8cc32a5711b4c3ba09f7 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 28 Nov 2023 21:19:08 +0800 Subject: [PATCH 24/37] fixed white space --- lib/ain-ocean/src/lib.rs | 2 +- lib/ain-ocean/src/model/block.rs | 5 ++++- lib/ain-ocean/src/model/masternode.rs | 8 +++++--- lib/ain-ocean/src/model/masternode_stats.rs | 14 +++++++++---- lib/ain-ocean/src/model/oracle.rs | 11 +++++++--- lib/ain-ocean/src/model/oracle_history.rs | 11 +++++++--- .../src/model/oracle_price_active.rs | 20 +++++++++++++------ .../src/model/oracle_price_aggregated.rs | 14 +++++++++---- .../model/oracle_price_aggregated_interval.rs | 11 +++++++++- lib/ain-ocean/src/model/oracle_price_feed.rs | 8 ++++++-- .../src/model/oracle_token_currency.rs | 8 ++++++-- lib/ain-ocean/src/model/poolswap.rs | 8 ++++++-- .../src/model/poolswap_aggregated.rs | 11 +++++++--- lib/ain-ocean/src/model/raw_block.rs | 5 ++++- .../src/model/vault_auction_batch_history.rs | 8 ++++++-- 15 files changed, 106 insertions(+), 38 deletions(-) diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 8566946f1c..ebea3dd212 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -9,7 +9,7 @@ pub use indexer::{index_block, invalidate_block}; pub mod api; pub mod database; mod api; -mod model; mod database; +mod model; pub use api::ocean_router; diff --git a/lib/ain-ocean/src/model/block.rs b/lib/ain-ocean/src/model/block.rs index fc6ea91318..55517511da 100644 --- a/lib/ain-ocean/src/model/block.rs +++ b/lib/ain-ocean/src/model/block.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct Block { pub id: String, pub hash: String, diff --git a/lib/ain-ocean/src/model/masternode.rs b/lib/ain-ocean/src/model/masternode.rs index 8db6749492..d076e37d9b 100644 --- a/lib/ain-ocean/src/model/masternode.rs +++ b/lib/ain-ocean/src/model/masternode.rs @@ -1,4 +1,4 @@ -use bitcoin::{BlockHash, Txid}; +use serde::{Deserialize, Serialize}; #[derive(Debug)] pub struct Masternode { @@ -16,7 +16,8 @@ pub struct Masternode { pub history: Option>, } -#[derive(Debug)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct MasternodeBlock { pub hash: String, pub height: u32, @@ -24,7 +25,8 @@ pub struct MasternodeBlock { pub median_time: u64, } -#[derive(Debug)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct HistoryItem { pub txid: String, pub owner_address: String, diff --git a/lib/ain-ocean/src/model/masternode_stats.rs b/lib/ain-ocean/src/model/masternode_stats.rs index b3060c4f2a..f037629517 100644 --- a/lib/ain-ocean/src/model/masternode_stats.rs +++ b/lib/ain-ocean/src/model/masternode_stats.rs @@ -1,18 +1,23 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct MasternodeStats { pub id: String, pub block: MasternodeStatsBlock, pub stats: MasternodeStatsStats, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct TimelockStats { pub weeks: i32, pub tvl: String, pub count: i32, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct MasternodeStatsBlock { pub hash: String, pub height: i32, @@ -20,7 +25,8 @@ pub struct MasternodeStatsBlock { pub median_time: i32, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct MasternodeStatsStats { pub count: i32, pub tvl: String, diff --git a/lib/ain-ocean/src/model/oracle.rs b/lib/ain-ocean/src/model/oracle.rs index fe3b35df4c..81710c0992 100644 --- a/lib/ain-ocean/src/model/oracle.rs +++ b/lib/ain-ocean/src/model/oracle.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct Oracle { pub id: String, pub owner_address: String, @@ -7,13 +10,15 @@ pub struct Oracle { pub block: OracleBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PriceFeedsItem { pub token: String, pub currency: String, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OracleBlock { pub hash: String, pub height: i32, diff --git a/lib/ain-ocean/src/model/oracle_history.rs b/lib/ain-ocean/src/model/oracle_history.rs index c1155124f4..0bce2e20a6 100644 --- a/lib/ain-ocean/src/model/oracle_history.rs +++ b/lib/ain-ocean/src/model/oracle_history.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OracleHistory { pub id: String, pub oracle_id: String, @@ -9,13 +12,15 @@ pub struct OracleHistory { pub block: OracleHistoryBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PriceFeedsItem { pub token: String, pub currency: String, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OracleHistoryBlock { pub hash: String, pub height: i32, diff --git a/lib/ain-ocean/src/model/oracle_price_active.rs b/lib/ain-ocean/src/model/oracle_price_active.rs index 4289fe0157..d3d7a77641 100644 --- a/lib/ain-ocean/src/model/oracle_price_active.rs +++ b/lib/ain-ocean/src/model/oracle_price_active.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceActive { pub id: String, pub key: String, @@ -9,21 +12,24 @@ pub struct OraclePriceActive { pub block: OraclePriceActiveBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceActiveActive { pub amount: String, pub weightage: i32, pub oracles: OraclePriceActiveActiveOracles, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceActiveNext { pub amount: String, pub weightage: i32, pub oracles: OraclePriceActiveNextOracles, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceActiveBlock { pub hash: String, pub height: i32, @@ -31,13 +37,15 @@ pub struct OraclePriceActiveBlock { pub median_time: i32, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceActiveActiveOracles { pub active: i32, pub total: i32, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceActiveNextOracles { pub active: i32, pub total: i32, diff --git a/lib/ain-ocean/src/model/oracle_price_aggregated.rs b/lib/ain-ocean/src/model/oracle_price_aggregated.rs index c948a9c7b4..57e16f7b06 100644 --- a/lib/ain-ocean/src/model/oracle_price_aggregated.rs +++ b/lib/ain-ocean/src/model/oracle_price_aggregated.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregated { pub id: String, pub key: String, @@ -9,14 +12,16 @@ pub struct OraclePriceAggregated { pub block: OraclePriceAggregatedBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedAggregated { pub amount: String, pub weightage: i32, pub oracles: OraclePriceAggregatedAggregatedOracles, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedBlock { pub hash: String, pub height: i32, @@ -24,7 +29,8 @@ pub struct OraclePriceAggregatedBlock { pub median_time: i32, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedAggregatedOracles { pub active: i32, pub total: i32, diff --git a/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs index 6f9729fb41..b6834f49c4 100644 --- a/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs +++ b/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedInterval { pub id: String, pub key: String, @@ -10,6 +13,8 @@ pub struct OraclePriceAggregatedInterval { } #[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedIntervalAggregated { pub amount: String, pub weightage: i32, @@ -18,6 +23,8 @@ pub struct OraclePriceAggregatedIntervalAggregated { } #[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedIntervalBlock { pub hash: String, pub height: i32, @@ -26,6 +33,8 @@ pub struct OraclePriceAggregatedIntervalBlock { } #[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedIntervalAggregatedOracles { pub active: i32, pub total: i32, diff --git a/lib/ain-ocean/src/model/oracle_price_feed.rs b/lib/ain-ocean/src/model/oracle_price_feed.rs index 122b99bf97..74fd34d576 100644 --- a/lib/ain-ocean/src/model/oracle_price_feed.rs +++ b/lib/ain-ocean/src/model/oracle_price_feed.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceFeed { pub id: String, pub key: String, @@ -12,7 +15,8 @@ pub struct OraclePriceFeed { pub block: OraclePriceFeedBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OraclePriceFeedBlock { pub hash: String, pub height: i32, diff --git a/lib/ain-ocean/src/model/oracle_token_currency.rs b/lib/ain-ocean/src/model/oracle_token_currency.rs index e4b431301d..a814083e9b 100644 --- a/lib/ain-ocean/src/model/oracle_token_currency.rs +++ b/lib/ain-ocean/src/model/oracle_token_currency.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OracleTokenCurrency { pub id: String, pub key: String, @@ -9,7 +12,8 @@ pub struct OracleTokenCurrency { pub block: OracleTokenCurrencyBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct OracleTokenCurrencyBlock { pub hash: String, pub height: i32, diff --git a/lib/ain-ocean/src/model/poolswap.rs b/lib/ain-ocean/src/model/poolswap.rs index f6dd97ebd2..777d7e3a3f 100644 --- a/lib/ain-ocean/src/model/poolswap.rs +++ b/lib/ain-ocean/src/model/poolswap.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PoolSwap { pub id: String, pub txid: String, @@ -10,7 +13,8 @@ pub struct PoolSwap { pub block: PoolSwapBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PoolSwapBlock { pub hash: String, pub height: i32, diff --git a/lib/ain-ocean/src/model/poolswap_aggregated.rs b/lib/ain-ocean/src/model/poolswap_aggregated.rs index 90afba7ae2..3e997e3302 100644 --- a/lib/ain-ocean/src/model/poolswap_aggregated.rs +++ b/lib/ain-ocean/src/model/poolswap_aggregated.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PoolSwapAggregated { pub id: String, pub key: String, @@ -7,12 +10,14 @@ pub struct PoolSwapAggregated { pub block: PoolSwapAggregatedBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PoolSwapAggregatedAggregated { pub amounts: Vec, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct PoolSwapAggregatedBlock { pub median_time: i32, } diff --git a/lib/ain-ocean/src/model/raw_block.rs b/lib/ain-ocean/src/model/raw_block.rs index 3569073e51..6219bae7c2 100644 --- a/lib/ain-ocean/src/model/raw_block.rs +++ b/lib/ain-ocean/src/model/raw_block.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct RawBlock { pub id: String, pub json: String, diff --git a/lib/ain-ocean/src/model/vault_auction_batch_history.rs b/lib/ain-ocean/src/model/vault_auction_batch_history.rs index b2f9cae2bc..614e96a41c 100644 --- a/lib/ain-ocean/src/model/vault_auction_batch_history.rs +++ b/lib/ain-ocean/src/model/vault_auction_batch_history.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct VaultAuctionBatchHistory { pub id: String, pub key: String, @@ -11,7 +14,8 @@ pub struct VaultAuctionBatchHistory { pub block: VaultAuctionBatchHistoryBlock, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] pub struct VaultAuctionBatchHistoryBlock { pub hash: String, pub height: i32, From 0bf59c007ee1efdf3252ee95ca19c71f321e0fbf Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 28 Nov 2023 23:11:06 +0800 Subject: [PATCH 25/37] added data_access with module --- lib/ain-ocean/src/data_acces/block.rs | 17 +++++++++++++++++ lib/ain-ocean/src/data_acces/masternode.rs | 13 +++++++++++++ .../src/data_acces/masternode_states.rs | 15 +++++++++++++++ lib/ain-ocean/src/data_acces/oracle.rs | 0 .../src/data_acces/oracle_price_aggregated.rs | 0 .../oracle_price_aggregated_interval.rs | 0 .../src/data_acces/oracle_token_currency.rs | 0 lib/ain-ocean/src/data_acces/order_history.rs | 0 lib/ain-ocean/src/data_acces/pool_swap.rs | 0 .../src/data_acces/pool_swap_aggregated.rs | 0 lib/ain-ocean/src/data_acces/price_ticker.rs | 0 lib/ain-ocean/src/data_acces/raw_block.rs | 0 lib/ain-ocean/src/data_acces/script_activity.rs | 0 .../src/data_acces/script_aggregation.rs | 0 lib/ain-ocean/src/data_acces/script_unspent.rs | 1 + lib/ain-ocean/src/data_acces/transaction.rs | 0 lib/ain-ocean/src/data_acces/transaction_vin.rs | 0 .../src/data_acces/transaction_vout.rs | 0 .../data_acces/vault_auction_batch_history.rs | 0 lib/ain-ocean/src/lib.rs | 2 +- 20 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 lib/ain-ocean/src/data_acces/block.rs create mode 100644 lib/ain-ocean/src/data_acces/masternode.rs create mode 100644 lib/ain-ocean/src/data_acces/masternode_states.rs create mode 100644 lib/ain-ocean/src/data_acces/oracle.rs create mode 100644 lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs create mode 100644 lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs create mode 100644 lib/ain-ocean/src/data_acces/oracle_token_currency.rs create mode 100644 lib/ain-ocean/src/data_acces/order_history.rs create mode 100644 lib/ain-ocean/src/data_acces/pool_swap.rs create mode 100644 lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs create mode 100644 lib/ain-ocean/src/data_acces/price_ticker.rs create mode 100644 lib/ain-ocean/src/data_acces/raw_block.rs create mode 100644 lib/ain-ocean/src/data_acces/script_activity.rs create mode 100644 lib/ain-ocean/src/data_acces/script_aggregation.rs create mode 100644 lib/ain-ocean/src/data_acces/script_unspent.rs create mode 100644 lib/ain-ocean/src/data_acces/transaction.rs create mode 100644 lib/ain-ocean/src/data_acces/transaction_vin.rs create mode 100644 lib/ain-ocean/src/data_acces/transaction_vout.rs create mode 100644 lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs diff --git a/lib/ain-ocean/src/data_acces/block.rs b/lib/ain-ocean/src/data_acces/block.rs new file mode 100644 index 0000000000..160c78346f --- /dev/null +++ b/lib/ain-ocean/src/data_acces/block.rs @@ -0,0 +1,17 @@ +use crate::database::RocksDB; +use crate::model::block::Block; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] +pub struct BlockDb {} + +impl BlockDb { + pub async fn get_by_hash(&self) -> Result(Block) {} + pub async fn get_by_height(&self) -> Result(Block) {} + pub async fn get_heighest(&self) -> Result(Block) {} + pub async fn query_by_height(&self, limit: i32, lt: i32) -> Result(Block) {} + pub async fn store_block(&self, block: Block) -> Result(Block) {} + pub async fn delete_block(&self, hash: String) -> Result(Block) {} +} diff --git a/lib/ain-ocean/src/data_acces/masternode.rs b/lib/ain-ocean/src/data_acces/masternode.rs new file mode 100644 index 0000000000..66507d03fb --- /dev/null +++ b/lib/ain-ocean/src/data_acces/masternode.rs @@ -0,0 +1,13 @@ +use crate::database::RocksDB; +use crate::model::masternode::Masternode; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct MasterNode {} + +impl MasterNode { + pub async fn query(&self, limit: i32, lt: i32) -> Result(Block) {} + pub async fn get(&self) -> Result(MasterNode) {} + pub async fn store(&self, stats: MasterNode) -> Result() {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/masternode_states.rs b/lib/ain-ocean/src/data_acces/masternode_states.rs new file mode 100644 index 0000000000..f1764661ef --- /dev/null +++ b/lib/ain-ocean/src/data_acces/masternode_states.rs @@ -0,0 +1,15 @@ +use crate::database::RocksDB; +use crate::model::masternode_stats::MasternodeStats; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] +pub struct MasterStatsDb {} +impl MasterStatsDb { + pub async fn get_latest(&self) -> Result(MasternodeStats) {} + pub async fn query(&self, limit: i32, lt: i32) -> Result(MasternodeStats) {} + pub async fn get(&self) -> Result(MasternodeStats) {} + pub async fn store(&self, stats: MasternodeStats) -> Result(Block) {} + pub async fn delete(&self, height: i32) -> Result(Block) {} +} diff --git a/lib/ain-ocean/src/data_acces/oracle.rs b/lib/ain-ocean/src/data_acces/oracle.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/order_history.rs b/lib/ain-ocean/src/data_acces/order_history.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/pool_swap.rs b/lib/ain-ocean/src/data_acces/pool_swap.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/price_ticker.rs b/lib/ain-ocean/src/data_acces/price_ticker.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/raw_block.rs b/lib/ain-ocean/src/data_acces/raw_block.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/script_activity.rs b/lib/ain-ocean/src/data_acces/script_activity.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/script_aggregation.rs b/lib/ain-ocean/src/data_acces/script_aggregation.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/script_unspent.rs b/lib/ain-ocean/src/data_acces/script_unspent.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/lib/ain-ocean/src/data_acces/script_unspent.rs @@ -0,0 +1 @@ + diff --git a/lib/ain-ocean/src/data_acces/transaction.rs b/lib/ain-ocean/src/data_acces/transaction.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/transaction_vin.rs b/lib/ain-ocean/src/data_acces/transaction_vin.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/transaction_vout.rs b/lib/ain-ocean/src/data_acces/transaction_vout.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs b/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index ebea3dd212..42e26bc393 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -9,7 +9,7 @@ pub use indexer::{index_block, invalidate_block}; pub mod api; pub mod database; mod api; -mod database; +pub mod database; mod model; pub use api::ocean_router; From b1d5d8e56713b10b6c8f7918c6c15e81b49e094a Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 29 Nov 2023 15:47:42 +0800 Subject: [PATCH 26/37] update transactions data module --- lib/ain-ocean/src/data_acces/transaction.rs | 21 +++++++++++++++++++ .../src/data_acces/transaction_vin.rs | 15 +++++++++++++ .../src/data_acces/transaction_vout.rs | 15 +++++++++++++ 3 files changed, 51 insertions(+) diff --git a/lib/ain-ocean/src/data_acces/transaction.rs b/lib/ain-ocean/src/data_acces/transaction.rs index e69de29bb2..a30d0bd89c 100644 --- a/lib/ain-ocean/src/data_acces/transaction.rs +++ b/lib/ain-ocean/src/data_acces/transaction.rs @@ -0,0 +1,21 @@ +use crate::database::RocksDB; +use crate::model::transaction_vin::Transaction; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] +pub struct TransactionVinDb {} + +impl TransactionVinDb { + pub async fn get(&self, txid: String) -> Result(TransactionVin) {} + pub async fn store(&self, txn: Transaction) -> Result(TransactionVin) {} + pub async fn delete(&self, txid: String) -> Result(TransactionVin) {} + pub async fn query_by_blockhash( + &self, + hash: String, + limit: i32, + lt: i32, + ) -> Result(TransactionVin) { + } +} diff --git a/lib/ain-ocean/src/data_acces/transaction_vin.rs b/lib/ain-ocean/src/data_acces/transaction_vin.rs index e69de29bb2..8b9e1cd504 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vin.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vin.rs @@ -0,0 +1,15 @@ +use crate::database::RocksDB; +use crate::model::transaction_vin::TransactionVin; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] +pub struct TransactionVinDb {} + +impl TransactionVinDb { + pub async fn get(&self) -> Result(TransactionVin) {} + pub async fn store(&self, stats: TransactionVin) -> Result(TransactionVin) {} + pub async fn delete(&self, id: String) -> Result(TransactionVin) {} + pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result(TransactionVin) {} +} diff --git a/lib/ain-ocean/src/data_acces/transaction_vout.rs b/lib/ain-ocean/src/data_acces/transaction_vout.rs index e69de29bb2..b1f9aaadf0 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vout.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vout.rs @@ -0,0 +1,15 @@ +use crate::database::RocksDB; +use crate::model::transaction_vout::TransactionVout; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default)] +#[serde(rename_all = "camelCase")] +pub struct TransactionVinDb {} + +impl TransactionVinDb { + pub async fn get(&self) -> Result(TransactionVout) {} + pub async fn store(&self, stats: TransactionVout) -> Result(TransactionVout) {} + pub async fn delete(&self, id: String) -> Result(TransactionVout) {} + pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result(TransactionVout) {} +} From 8d9551a0231be344ff58e7c332d56b77b815f0fa Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Thu, 30 Nov 2023 19:06:33 +0800 Subject: [PATCH 27/37] updated oracle price --- lib/ain-ocean/src/data_acces/oracle.rs | 13 +++++++++++++ .../src/data_acces/oracle_price_aggregated.rs | 19 +++++++++++++++++++ .../oracle_price_aggregated_interval.rs | 18 ++++++++++++++++++ .../src/data_acces/oracle_token_currency.rs | 12 ++++++++++++ lib/ain-ocean/src/data_acces/order_history.rs | 12 ++++++++++++ lib/ain-ocean/src/data_acces/pool_swap.rs | 12 ++++++++++++ .../src/data_acces/pool_swap_aggregated.rs | 19 +++++++++++++++++++ lib/ain-ocean/src/data_acces/price_ticker.rs | 13 +++++++++++++ 8 files changed, 118 insertions(+) diff --git a/lib/ain-ocean/src/data_acces/oracle.rs b/lib/ain-ocean/src/data_acces/oracle.rs index e69de29bb2..a045644dac 100644 --- a/lib/ain-ocean/src/data_acces/oracle.rs +++ b/lib/ain-ocean/src/data_acces/oracle.rs @@ -0,0 +1,13 @@ +use crate::database::RocksDB; +use crate::model::oracle::Oracle; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct OracleDb {} + +impl OracleHistoryDB { + pub async fn query(&self, limit: i32, lt: String) -> Result(Oracle) {} + pub async fn put(&self, oracle: Oracle) -> Result() {} + pub async fn get(&self, id: String) -> Result(Oracle) {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs index e69de29bb2..1b56aa91ee 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs @@ -0,0 +1,19 @@ +use crate::database::RocksDB; +use crate::model::oracle_price_aggregated::OraclePriceAggregated; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct OraclePriceAggrigatedDb {} + +impl OraclePriceAggrigatedDb { + pub async fn query( + &self, + key: String, + limit: i32, + lt: String, + ) -> Result(vec) { + } + pub async fn put(&self, oracle: OraclePriceAggregated) -> Result() {} + pub async fn get(&self, id: String) -> Result(OraclePriceAggregated) {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs index e69de29bb2..31ecc1d578 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs @@ -0,0 +1,18 @@ +use crate::database::RocksDB; +use crate::model::oracle_price_aggregated_interval::OraclePriceAggregatedInterval; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct OraclePriceAggregatedIntervalDb {} + +impl OraclePriceAggregatedIntervalDb { + pub async fn query( + &self, + key: String, + limit: i32, + lt: String, + ) -> Result(vec) { + } + pub async fn put(&self, oracle: OraclePriceAggregatedInterval) -> Result() {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs index e69de29bb2..786f14e60c 100644 --- a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs +++ b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs @@ -0,0 +1,12 @@ +use crate::database::RocksDB; +use crate::model::oracle_token_currency::OracleTokenCurrency; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct OracleTokenCurrencyDb {} + +impl OracleHistoryDB { + pub async fn query(&self, limit: i32, lt: String) -> Result(Vec) {} + pub async fn put(&self, oracle_token: OracleTokenCurrency) -> Result() {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/order_history.rs b/lib/ain-ocean/src/data_acces/order_history.rs index e69de29bb2..d67babff52 100644 --- a/lib/ain-ocean/src/data_acces/order_history.rs +++ b/lib/ain-ocean/src/data_acces/order_history.rs @@ -0,0 +1,12 @@ +use crate::database::RocksDB; +use crate::model::oracle_history::OracleHistory; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct OracleHistoryDB {} + +impl OracleHistoryDB { + pub async fn query(&self, oracleId: String, limit: i32, lt: String) -> Result(OracleHistory) {} + pub async fn put(&self, oracleHistory: OracleHistory) -> Result() {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/pool_swap.rs b/lib/ain-ocean/src/data_acces/pool_swap.rs index e69de29bb2..de0d8715f9 100644 --- a/lib/ain-ocean/src/data_acces/pool_swap.rs +++ b/lib/ain-ocean/src/data_acces/pool_swap.rs @@ -0,0 +1,12 @@ +use crate::database::RocksDB; +use crate::model::poolswap::PoolSwap; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct PoolSwapDb {} + +impl PoolSwapDb { + pub async fn query(&self, key: String, limit: i32, lt: String) -> Result(vec) {} + pub async fn put(&self, feed: PoolSwap) -> Result() {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs index e69de29bb2..e887748fd6 100644 --- a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs @@ -0,0 +1,19 @@ +use crate::database::RocksDB; +use crate::model::poolswap_aggregated::PoolSwapAggregated; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct PoolSwapAggregatedDb {} + +impl PoolSwapAggregatedDb { + pub async fn query( + &self, + key: String, + limit: i32, + lt: String, + ) -> Result(vec) { + } + pub async fn put(&self, aggregated: PoolSwapAggregated) -> Result() {} + pub async fn get(&self, id: String) -> Result(PoolSwapAggregated) {} + pub async fn delete(&self, id: String) -> Result() {} +} diff --git a/lib/ain-ocean/src/data_acces/price_ticker.rs b/lib/ain-ocean/src/data_acces/price_ticker.rs index e69de29bb2..5b106bdb1c 100644 --- a/lib/ain-ocean/src/data_acces/price_ticker.rs +++ b/lib/ain-ocean/src/data_acces/price_ticker.rs @@ -0,0 +1,13 @@ +use crate::database::RocksDB; +use crate::model::price_ticker::PriceTicker; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; + +pub struct OraclePriceAggregatedIntervalDb {} + +impl OraclePriceAggregatedIntervalDb { + pub async fn query(&self, limit: i32, lt: String) -> Result(vec) {} + pub async fn get(&self, id: String) -> Result(PriceTicker) {} + pub async fn put(&self, price: PriceTicker) -> Result() {} + pub async fn delete(&self, id: String) -> Result() {} +} From 271b65e9f8cb7fca12a8f6de1fe8767540cb2281 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Fri, 1 Dec 2023 18:15:26 +0800 Subject: [PATCH 28/37] added crud opetration to oracle fixed the issue --- lib/Cargo.lock | 2 + lib/ain-ocean/Cargo.toml | 1 + lib/ain-ocean/src/data_acces/block.rs | 26 +++++-- lib/ain-ocean/src/data_acces/masternode.rs | 18 +++-- .../src/data_acces/masternode_states.rs | 22 ++++-- lib/ain-ocean/src/data_acces/mod.rs | 20 ++++++ lib/ain-ocean/src/data_acces/oracle.rs | 45 +++++++++--- .../src/data_acces/oracle_price_aggregated.rs | 17 +++-- .../oracle_price_aggregated_interval.rs | 13 ++-- .../src/data_acces/oracle_token_currency.rs | 16 +++-- lib/ain-ocean/src/data_acces/order_history.rs | 14 ++-- lib/ain-ocean/src/data_acces/pool_swap.rs | 14 ++-- .../src/data_acces/pool_swap_aggregated.rs | 17 +++-- lib/ain-ocean/src/data_acces/price_ticker.rs | 18 +++-- lib/ain-ocean/src/data_acces/raw_block.rs | 1 + .../src/data_acces/script_activity.rs | 1 + .../src/data_acces/script_aggregation.rs | 1 + lib/ain-ocean/src/data_acces/test/mod.rs | 1 + .../src/data_acces/test/oracle_test.rs | 71 +++++++++++++++++++ lib/ain-ocean/src/data_acces/transaction.rs | 19 +++-- .../src/data_acces/transaction_vin.rs | 18 +++-- .../src/data_acces/transaction_vout.rs | 18 +++-- .../data_acces/vault_auction_batch_history.rs | 1 + lib/ain-ocean/src/database/db_manger.rs | 1 + lib/ain-ocean/src/database/db_test.rs | 2 + lib/ain-ocean/src/lib.rs | 1 + lib/ain-ocean/src/model/oracle.rs | 6 +- 27 files changed, 307 insertions(+), 77 deletions(-) create mode 100644 lib/ain-ocean/src/data_acces/mod.rs create mode 100644 lib/ain-ocean/src/data_acces/test/mod.rs create mode 100644 lib/ain-ocean/src/data_acces/test/oracle_test.rs diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 9c1f2e79e3..641ec8cae8 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -244,8 +244,10 @@ dependencies = [ "log", "rocksdb", "serde", + "serde_json", "structopt", "tempdir", + "tempfile", "tokio", "tokio-serde", ] diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index e093b34cb9..587c9b5c01 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -29,3 +29,4 @@ ctrlc = "3.1.9" tempdir = "0.3.7" bitcoin_hashes = "0.12.0" structopt = { version = "0.3", default-features = false } +tempfile = "3.8.1" diff --git a/lib/ain-ocean/src/data_acces/block.rs b/lib/ain-ocean/src/data_acces/block.rs index 160c78346f..09e2acf16a 100644 --- a/lib/ain-ocean/src/data_acces/block.rs +++ b/lib/ain-ocean/src/data_acces/block.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::block::Block; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -8,10 +8,22 @@ use serde::{Deserialize, Serialize}; pub struct BlockDb {} impl BlockDb { - pub async fn get_by_hash(&self) -> Result(Block) {} - pub async fn get_by_height(&self) -> Result(Block) {} - pub async fn get_heighest(&self) -> Result(Block) {} - pub async fn query_by_height(&self, limit: i32, lt: i32) -> Result(Block) {} - pub async fn store_block(&self, block: Block) -> Result(Block) {} - pub async fn delete_block(&self, hash: String) -> Result(Block) {} + pub async fn get_by_hash(&self) -> Result { + todo!() + } + pub async fn get_by_height(&self) -> Result { + todo!() + } + pub async fn get_heighest(&self) -> Result { + todo!() + } + pub async fn query_by_height(&self, limit: i32, lt: i32) -> Result> { + todo!() + } + pub async fn store_block(&self, block: Block) -> Result { + todo!() + } + pub async fn delete_block(&self, hash: String) -> Result { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/masternode.rs b/lib/ain-ocean/src/data_acces/masternode.rs index 66507d03fb..c97c1a882e 100644 --- a/lib/ain-ocean/src/data_acces/masternode.rs +++ b/lib/ain-ocean/src/data_acces/masternode.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::masternode::Masternode; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -6,8 +6,16 @@ use serde::{Deserialize, Serialize}; pub struct MasterNode {} impl MasterNode { - pub async fn query(&self, limit: i32, lt: i32) -> Result(Block) {} - pub async fn get(&self) -> Result(MasterNode) {} - pub async fn store(&self, stats: MasterNode) -> Result() {} - pub async fn delete(&self, id: String) -> Result() {} + pub async fn query(&self, limit: i32, lt: i32) -> Result> { + todo!() + } + pub async fn get(&self) -> Result { + todo!() + } + pub async fn store(&self, stats: MasterNode) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/masternode_states.rs b/lib/ain-ocean/src/data_acces/masternode_states.rs index f1764661ef..fe557c9c64 100644 --- a/lib/ain-ocean/src/data_acces/masternode_states.rs +++ b/lib/ain-ocean/src/data_acces/masternode_states.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::masternode_stats::MasternodeStats; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -7,9 +7,19 @@ use serde::{Deserialize, Serialize}; #[serde(rename_all = "camelCase")] pub struct MasterStatsDb {} impl MasterStatsDb { - pub async fn get_latest(&self) -> Result(MasternodeStats) {} - pub async fn query(&self, limit: i32, lt: i32) -> Result(MasternodeStats) {} - pub async fn get(&self) -> Result(MasternodeStats) {} - pub async fn store(&self, stats: MasternodeStats) -> Result(Block) {} - pub async fn delete(&self, height: i32) -> Result(Block) {} + pub async fn get_latest(&self) -> Result { + todo!() + } + pub async fn query(&self, limit: i32, lt: i32) -> Result> { + todo!() + } + pub async fn get(&self) -> Result { + todo!() + } + pub async fn store(&self, stats: MasternodeStats) -> Result<()> { + todo!() + } + pub async fn delete(&self, height: i32) -> Result<()> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/mod.rs b/lib/ain-ocean/src/data_acces/mod.rs new file mode 100644 index 0000000000..4072333a14 --- /dev/null +++ b/lib/ain-ocean/src/data_acces/mod.rs @@ -0,0 +1,20 @@ +pub mod block; +pub mod masternode; +pub mod masternode_states; +pub mod oracle; +pub mod oracle_price_aggregated; +pub mod oracle_price_aggregated_interval; +pub mod oracle_token_currency; +pub mod order_history; +pub mod pool_swap; +pub mod pool_swap_aggregated; +pub mod price_ticker; +pub mod raw_block; +pub mod script_activity; +pub mod script_aggregation; +pub mod script_unspent; +pub mod test; +pub mod transaction; +pub mod transaction_vin; +pub mod transaction_vout; +pub mod vault_auction_batch_history; diff --git a/lib/ain-ocean/src/data_acces/oracle.rs b/lib/ain-ocean/src/data_acces/oracle.rs index a045644dac..8bee24254b 100644 --- a/lib/ain-ocean/src/data_acces/oracle.rs +++ b/lib/ain-ocean/src/data_acces/oracle.rs @@ -1,13 +1,42 @@ -use crate::database::RocksDB; +use crate::database::db_manger::ColumnFamilyOperations; +use crate::database::db_manger::RocksDB; use crate::model::oracle::Oracle; -use anyhow::{anyhow, Result}; +use anyhow::{anyhow, Error, Result}; use serde::{Deserialize, Serialize}; +use serde_json; -pub struct OracleDb {} +pub struct OracleDb { + pub db: RocksDB, +} -impl OracleHistoryDB { - pub async fn query(&self, limit: i32, lt: String) -> Result(Oracle) {} - pub async fn put(&self, oracle: Oracle) -> Result() {} - pub async fn get(&self, id: String) -> Result(Oracle) {} - pub async fn delete(&self, id: String) -> Result() {} +impl OracleDb { + pub async fn query(&self, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn store(&self, oracle: Oracle) -> Result<()> { + match serde_json::to_string(&oracle) { + Ok(value) => { + let key = oracle.id.clone(); + self.db.put("oracle", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } + } + pub async fn get(&self, id: String) -> Result> { + match self.db.get("oracle", id.as_bytes()) { + Ok(Some(value)) => { + let oracle: Oracle = serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(oracle)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } + } + pub async fn delete(&self, id: String) -> Result<()> { + match self.db.delete("oracle", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } + } } diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs index 1b56aa91ee..b4ddd2599c 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::oracle_price_aggregated::OraclePriceAggregated; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -11,9 +11,16 @@ impl OraclePriceAggrigatedDb { key: String, limit: i32, lt: String, - ) -> Result(vec) { + ) -> Result<(Vec)> { + todo!() + } + pub async fn put(&self, oracle: OraclePriceAggregated) -> Result<()> { + todo!() + } + pub async fn get(&self, id: String) -> Result { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() } - pub async fn put(&self, oracle: OraclePriceAggregated) -> Result() {} - pub async fn get(&self, id: String) -> Result(OraclePriceAggregated) {} - pub async fn delete(&self, id: String) -> Result() {} } diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs index 31ecc1d578..04a4a2ea4b 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::oracle_price_aggregated_interval::OraclePriceAggregatedInterval; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -11,8 +11,13 @@ impl OraclePriceAggregatedIntervalDb { key: String, limit: i32, lt: String, - ) -> Result(vec) { + ) -> Result> { + todo!() + } + pub async fn put(&self, oracle: OraclePriceAggregatedInterval) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() } - pub async fn put(&self, oracle: OraclePriceAggregatedInterval) -> Result() {} - pub async fn delete(&self, id: String) -> Result() {} } diff --git a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs index 786f14e60c..79e487ba18 100644 --- a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs +++ b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs @@ -1,12 +1,18 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::oracle_token_currency::OracleTokenCurrency; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; pub struct OracleTokenCurrencyDb {} -impl OracleHistoryDB { - pub async fn query(&self, limit: i32, lt: String) -> Result(Vec) {} - pub async fn put(&self, oracle_token: OracleTokenCurrency) -> Result() {} - pub async fn delete(&self, id: String) -> Result() {} +impl OracleTokenCurrencyDb { + pub async fn query(&self, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn put(&self, oracle_token: OracleTokenCurrency) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/order_history.rs b/lib/ain-ocean/src/data_acces/order_history.rs index d67babff52..046e82fa4b 100644 --- a/lib/ain-ocean/src/data_acces/order_history.rs +++ b/lib/ain-ocean/src/data_acces/order_history.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::oracle_history::OracleHistory; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -6,7 +6,13 @@ use serde::{Deserialize, Serialize}; pub struct OracleHistoryDB {} impl OracleHistoryDB { - pub async fn query(&self, oracleId: String, limit: i32, lt: String) -> Result(OracleHistory) {} - pub async fn put(&self, oracleHistory: OracleHistory) -> Result() {} - pub async fn delete(&self, id: String) -> Result() {} + pub async fn query(&self, oracleId: String, limit: i32, lt: String) -> Result { + todo!() + } + pub async fn put(&self, oracleHistory: OracleHistory) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/pool_swap.rs b/lib/ain-ocean/src/data_acces/pool_swap.rs index de0d8715f9..1462b4904c 100644 --- a/lib/ain-ocean/src/data_acces/pool_swap.rs +++ b/lib/ain-ocean/src/data_acces/pool_swap.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::poolswap::PoolSwap; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -6,7 +6,13 @@ use serde::{Deserialize, Serialize}; pub struct PoolSwapDb {} impl PoolSwapDb { - pub async fn query(&self, key: String, limit: i32, lt: String) -> Result(vec) {} - pub async fn put(&self, feed: PoolSwap) -> Result() {} - pub async fn delete(&self, id: String) -> Result() {} + pub async fn query(&self, key: String, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn put(&self, feed: PoolSwap) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs index e887748fd6..c8f257c699 100644 --- a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::poolswap_aggregated::PoolSwapAggregated; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -11,9 +11,16 @@ impl PoolSwapAggregatedDb { key: String, limit: i32, lt: String, - ) -> Result(vec) { + ) -> Result<(Vec)> { + todo!() + } + pub async fn put(&self, aggregated: PoolSwapAggregated) -> Result<()> { + todo!() + } + pub async fn get(&self, id: String) -> Result { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() } - pub async fn put(&self, aggregated: PoolSwapAggregated) -> Result() {} - pub async fn get(&self, id: String) -> Result(PoolSwapAggregated) {} - pub async fn delete(&self, id: String) -> Result() {} } diff --git a/lib/ain-ocean/src/data_acces/price_ticker.rs b/lib/ain-ocean/src/data_acces/price_ticker.rs index 5b106bdb1c..87645c2fa8 100644 --- a/lib/ain-ocean/src/data_acces/price_ticker.rs +++ b/lib/ain-ocean/src/data_acces/price_ticker.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::price_ticker::PriceTicker; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -6,8 +6,16 @@ use serde::{Deserialize, Serialize}; pub struct OraclePriceAggregatedIntervalDb {} impl OraclePriceAggregatedIntervalDb { - pub async fn query(&self, limit: i32, lt: String) -> Result(vec) {} - pub async fn get(&self, id: String) -> Result(PriceTicker) {} - pub async fn put(&self, price: PriceTicker) -> Result() {} - pub async fn delete(&self, id: String) -> Result() {} + pub async fn query(&self, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn get(&self, id: String) -> Result { + todo!() + } + pub async fn put(&self, price: PriceTicker) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/raw_block.rs b/lib/ain-ocean/src/data_acces/raw_block.rs index e69de29bb2..8b13789179 100644 --- a/lib/ain-ocean/src/data_acces/raw_block.rs +++ b/lib/ain-ocean/src/data_acces/raw_block.rs @@ -0,0 +1 @@ + diff --git a/lib/ain-ocean/src/data_acces/script_activity.rs b/lib/ain-ocean/src/data_acces/script_activity.rs index e69de29bb2..8b13789179 100644 --- a/lib/ain-ocean/src/data_acces/script_activity.rs +++ b/lib/ain-ocean/src/data_acces/script_activity.rs @@ -0,0 +1 @@ + diff --git a/lib/ain-ocean/src/data_acces/script_aggregation.rs b/lib/ain-ocean/src/data_acces/script_aggregation.rs index e69de29bb2..8b13789179 100644 --- a/lib/ain-ocean/src/data_acces/script_aggregation.rs +++ b/lib/ain-ocean/src/data_acces/script_aggregation.rs @@ -0,0 +1 @@ + diff --git a/lib/ain-ocean/src/data_acces/test/mod.rs b/lib/ain-ocean/src/data_acces/test/mod.rs new file mode 100644 index 0000000000..d490f93649 --- /dev/null +++ b/lib/ain-ocean/src/data_acces/test/mod.rs @@ -0,0 +1 @@ +mod oracle_test; diff --git a/lib/ain-ocean/src/data_acces/test/oracle_test.rs b/lib/ain-ocean/src/data_acces/test/oracle_test.rs new file mode 100644 index 0000000000..234388a782 --- /dev/null +++ b/lib/ain-ocean/src/data_acces/test/oracle_test.rs @@ -0,0 +1,71 @@ +#[cfg(test)] +mod tests { + use crate::data_acces::oracle::OracleDb; + use crate::database::db_manger::{ColumnFamilyOperations, RocksDB}; + use crate::model::oracle::{Oracle, OracleBlock, PriceFeedsItem}; + use tempfile::tempdir; + use tokio::task; + + // Function to set up a test database environment + fn setup_test_db() -> OracleDb { + let temp_dir = tempdir().unwrap(); + let db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); // Adjust this according to your RocksDB struct + OracleDb { db } + } + + // Function to create a dummy Oracle instance for testing + fn create_dummy_oracle(id: &str) -> Oracle { + Oracle { + id: id.to_string(), + owner_address: "owner_address_example".to_string(), + weightage: 10, + price_feeds: vec![PriceFeedsItem { + token: "token_example".to_string(), + currency: "currency_example".to_string(), + }], + block: OracleBlock { + hash: "hash_example".to_string(), + height: 1, + time: 100000, + median_time: 100000, + }, + } + } + + #[tokio::test] + async fn test_store() { + let oracle_db = setup_test_db(); + let oracle = create_dummy_oracle("test_id_1"); + + let result = oracle_db.store(oracle).await; + assert!(result.is_ok()); + } + + #[tokio::test] + async fn test_get() { + let oracle_db = setup_test_db(); + let oracle = create_dummy_oracle("test_id_2"); + oracle_db.store(oracle.clone()).await.unwrap(); + + let result = oracle_db.get(oracle.id.clone()).await.unwrap(); + assert!(result.is_some()); + println!("the oracle result {:?}", result); + assert_eq!(result.unwrap(), oracle); + } + + #[tokio::test] + async fn test_delete() { + let oracle_db = setup_test_db(); + let oracle = create_dummy_oracle("test_id_3"); + oracle_db.store(oracle.clone()).await.unwrap(); + + let delete_result = oracle_db.delete(oracle.id.clone()).await; + assert!(delete_result.is_ok()); + + let get_result = oracle_db.get(oracle.id).await; + assert!(get_result.is_ok()); + assert!(get_result.unwrap().is_none()); + } + + // Additional tests for `query` and other edge cases can be added similarly +} diff --git a/lib/ain-ocean/src/data_acces/transaction.rs b/lib/ain-ocean/src/data_acces/transaction.rs index a30d0bd89c..f0e0d31ce9 100644 --- a/lib/ain-ocean/src/data_acces/transaction.rs +++ b/lib/ain-ocean/src/data_acces/transaction.rs @@ -1,5 +1,5 @@ -use crate::database::RocksDB; -use crate::model::transaction_vin::Transaction; +use crate::database::db_manger::RocksDB; +use crate::model::transaction::Transaction; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -8,14 +8,21 @@ use serde::{Deserialize, Serialize}; pub struct TransactionVinDb {} impl TransactionVinDb { - pub async fn get(&self, txid: String) -> Result(TransactionVin) {} - pub async fn store(&self, txn: Transaction) -> Result(TransactionVin) {} - pub async fn delete(&self, txid: String) -> Result(TransactionVin) {} + pub async fn get(&self, txid: String) -> Result { + todo!() + } + pub async fn store(&self, txn: Transaction) -> Result { + todo!() + } + pub async fn delete(&self, txid: String) -> Result { + todo!() + } pub async fn query_by_blockhash( &self, hash: String, limit: i32, lt: i32, - ) -> Result(TransactionVin) { + ) -> Result> { + todo!() } } diff --git a/lib/ain-ocean/src/data_acces/transaction_vin.rs b/lib/ain-ocean/src/data_acces/transaction_vin.rs index 8b9e1cd504..1b1ec29896 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vin.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vin.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::transaction_vin::TransactionVin; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -8,8 +8,16 @@ use serde::{Deserialize, Serialize}; pub struct TransactionVinDb {} impl TransactionVinDb { - pub async fn get(&self) -> Result(TransactionVin) {} - pub async fn store(&self, stats: TransactionVin) -> Result(TransactionVin) {} - pub async fn delete(&self, id: String) -> Result(TransactionVin) {} - pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result(TransactionVin) {} + pub async fn get(&self) -> Result { + todo!() + } + pub async fn store(&self, stats: TransactionVin) -> Result<()> { + todo!() + } + pub async fn delete(&self, id: String) -> Result<()> { + todo!() + } + pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result> { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/transaction_vout.rs b/lib/ain-ocean/src/data_acces/transaction_vout.rs index b1f9aaadf0..5c62bb5ef5 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vout.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vout.rs @@ -1,4 +1,4 @@ -use crate::database::RocksDB; +use crate::database::db_manger::RocksDB; use crate::model::transaction_vout::TransactionVout; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; @@ -8,8 +8,16 @@ use serde::{Deserialize, Serialize}; pub struct TransactionVinDb {} impl TransactionVinDb { - pub async fn get(&self) -> Result(TransactionVout) {} - pub async fn store(&self, stats: TransactionVout) -> Result(TransactionVout) {} - pub async fn delete(&self, id: String) -> Result(TransactionVout) {} - pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result(TransactionVout) {} + pub async fn get(&self) -> Result { + todo!() + } + pub async fn store(&self, stats: TransactionVout) -> Result { + todo!() + } + pub async fn delete(&self, id: String) -> Result { + todo!() + } + pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result { + todo!() + } } diff --git a/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs b/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs index e69de29bb2..8b13789179 100644 --- a/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs +++ b/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs @@ -0,0 +1 @@ + diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs index 4598ce2d2f..002a281c3c 100644 --- a/lib/ain-ocean/src/database/db_manger.rs +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -1,3 +1,4 @@ +use crate::model::oracle::Oracle; use anyhow::{anyhow, Result}; use bitcoin::blockdata::block::Block; use bitcoin::blockdata::block::Header; diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index a93b94eb9e..dd2c8caf9a 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -2,6 +2,7 @@ extern crate rocksdb; extern crate tempdir; +use crate::database::db_manger::RocksDB; use bitcoin::blockdata::block::Header; use bitcoin::blockdata::block::Version; use bitcoin::blockdata::script::ScriptBuf; @@ -10,6 +11,7 @@ use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::transaction::TxIn; use bitcoin::blockdata::transaction::TxOut; use bitcoin::blockdata::witness::Witness; +use bitcoin::consensus::deserialize; use bitcoin::hash_types::TxMerkleNode; use bitcoin::hash_types::Txid; use bitcoin::pow::CompactTarget; diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 42e26bc393..864b5c8543 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -9,6 +9,7 @@ pub use indexer::{index_block, invalidate_block}; pub mod api; pub mod database; mod api; +mod data_acces; pub mod database; mod model; diff --git a/lib/ain-ocean/src/model/oracle.rs b/lib/ain-ocean/src/model/oracle.rs index 81710c0992..97befdd9c7 100644 --- a/lib/ain-ocean/src/model/oracle.rs +++ b/lib/ain-ocean/src/model/oracle.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)] #[serde(rename_all = "camelCase")] pub struct Oracle { pub id: String, @@ -10,14 +10,14 @@ pub struct Oracle { pub block: OracleBlock, } -#[derive(Serialize, Deserialize, Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)] #[serde(rename_all = "camelCase")] pub struct PriceFeedsItem { pub token: String, pub currency: String, } -#[derive(Serialize, Deserialize, Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq)] #[serde(rename_all = "camelCase")] pub struct OracleBlock { pub hash: String, From 6c013afbf562028fd3e7a7ffddf52f97d2b3cdf2 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 4 Dec 2023 23:06:10 +0800 Subject: [PATCH 29/37] added method for oracle,price,order_history --- .../src/data_acces/oracle_price_aggregated.rs | 27 +++++++++++++--- .../oracle_price_aggregated_interval.rs | 26 +++++++++++++-- .../src/data_acces/oracle_token_currency.rs | 20 ++++++++++-- lib/ain-ocean/src/data_acces/order_history.rs | 24 +++++++++++--- lib/ain-ocean/src/data_acces/pool_swap.rs | 21 +++++++++--- .../src/data_acces/pool_swap_aggregated.rs | 30 ++++++++++++++--- lib/ain-ocean/src/data_acces/price_ticker.rs | 32 ++++++++++++++++--- lib/ain-ocean/src/database/db_manger.rs | 3 +- 8 files changed, 154 insertions(+), 29 deletions(-) diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs index b4ddd2599c..47072f49e5 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs @@ -1,9 +1,12 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::oracle_price_aggregated::OraclePriceAggregated; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct OraclePriceAggrigatedDb {} +pub struct OraclePriceAggrigatedDb { + pub db: RocksDB, +} impl OraclePriceAggrigatedDb { pub async fn query( @@ -15,12 +18,26 @@ impl OraclePriceAggrigatedDb { todo!() } pub async fn put(&self, oracle: OraclePriceAggregated) -> Result<()> { - todo!() + match serde_json::to_string(&oracle) { + Ok(value) => { + let key = oracle.id.clone(); + self.db + .put("oracle_price_aggregated", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } - pub async fn get(&self, id: String) -> Result { - todo!() + pub async fn get(&self, id: String) -> Option { + match self.db.get("oracle_price_aggregated", id.as_bytes()) { + Ok(Some(value)) => serde_json::from_slice(&value).ok(), + _ => None, + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("oracle_price_aggregated", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs index 04a4a2ea4b..109b44ed5f 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated_interval.rs @@ -1,9 +1,12 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::oracle_price_aggregated_interval::OraclePriceAggregatedInterval; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct OraclePriceAggregatedIntervalDb {} +pub struct OraclePriceAggregatedIntervalDb { + pub db: RocksDB, +} impl OraclePriceAggregatedIntervalDb { pub async fn query( @@ -15,9 +18,26 @@ impl OraclePriceAggregatedIntervalDb { todo!() } pub async fn put(&self, oracle: OraclePriceAggregatedInterval) -> Result<()> { - todo!() + match serde_json::to_string(&oracle) { + Ok(value) => { + let key = oracle.id.clone(); + self.db.put( + "oracle_price_aggregated_interval", + key.as_bytes(), + value.as_bytes(), + )?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self + .db + .delete("oracle_price_aggregated_interval", id.as_bytes()) + { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs index 79e487ba18..f920f9b7d8 100644 --- a/lib/ain-ocean/src/data_acces/oracle_token_currency.rs +++ b/lib/ain-ocean/src/data_acces/oracle_token_currency.rs @@ -1,18 +1,32 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::oracle_token_currency::OracleTokenCurrency; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct OracleTokenCurrencyDb {} +pub struct OracleTokenCurrencyDb { + pub db: RocksDB, +} impl OracleTokenCurrencyDb { pub async fn query(&self, limit: i32, lt: String) -> Result> { todo!() } pub async fn put(&self, oracle_token: OracleTokenCurrency) -> Result<()> { - todo!() + match serde_json::to_string(&oracle_token) { + Ok(value) => { + let key = oracle_token.id.clone(); + self.db + .put("oracle_token_currency", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("oracle_token_currency", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/order_history.rs b/lib/ain-ocean/src/data_acces/order_history.rs index 046e82fa4b..43a0cdebb6 100644 --- a/lib/ain-ocean/src/data_acces/order_history.rs +++ b/lib/ain-ocean/src/data_acces/order_history.rs @@ -1,18 +1,34 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::oracle_history::OracleHistory; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct OracleHistoryDB {} + +pub struct OracleHistoryDB { + pub db: RocksDB, +} impl OracleHistoryDB { pub async fn query(&self, oracleId: String, limit: i32, lt: String) -> Result { todo!() } - pub async fn put(&self, oracleHistory: OracleHistory) -> Result<()> { - todo!() + + pub async fn store(&self, oracle_history: OracleHistory) -> Result<()> { + match serde_json::to_string(&oracle_history) { + Ok(value) => { + let key = oracle_history.id.clone(); + self.db + .put("oracle_history", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("oracle_history", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/pool_swap.rs b/lib/ain-ocean/src/data_acces/pool_swap.rs index 1462b4904c..f61078f527 100644 --- a/lib/ain-ocean/src/data_acces/pool_swap.rs +++ b/lib/ain-ocean/src/data_acces/pool_swap.rs @@ -1,18 +1,31 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::poolswap::PoolSwap; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct PoolSwapDb {} +pub struct PoolSwapDb { + pub db: RocksDB, +} impl PoolSwapDb { pub async fn query(&self, key: String, limit: i32, lt: String) -> Result> { todo!() } - pub async fn put(&self, feed: PoolSwap) -> Result<()> { - todo!() + pub async fn put(&self, swap: PoolSwap) -> Result<()> { + match serde_json::to_string(&swap) { + Ok(value) => { + let key = swap.id.clone(); + self.db.put("pool_swap", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("pool_swap", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs index c8f257c699..45f756f410 100644 --- a/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/pool_swap_aggregated.rs @@ -1,9 +1,12 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::poolswap_aggregated::PoolSwapAggregated; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct PoolSwapAggregatedDb {} +pub struct PoolSwapAggregatedDb { + pub db: RocksDB, +} impl PoolSwapAggregatedDb { pub async fn query( @@ -15,12 +18,31 @@ impl PoolSwapAggregatedDb { todo!() } pub async fn put(&self, aggregated: PoolSwapAggregated) -> Result<()> { - todo!() + match serde_json::to_string(&aggregated) { + Ok(value) => { + let key = aggregated.id.clone(); + self.db + .put("pool_swap_aggregated", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn get(&self, id: String) -> Result { - todo!() + match self.db.get("pool_swap_aggregated", id.as_bytes()) { + Ok(Some(value)) => { + let pool_swap: PoolSwapAggregated = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(pool_swap) + } + Ok(None) => Err(anyhow!("No data found for the given ID")), + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("pool_swap_aggregated", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/price_ticker.rs b/lib/ain-ocean/src/data_acces/price_ticker.rs index 87645c2fa8..e1987dde9b 100644 --- a/lib/ain-ocean/src/data_acces/price_ticker.rs +++ b/lib/ain-ocean/src/data_acces/price_ticker.rs @@ -1,21 +1,43 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::price_ticker::PriceTicker; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct OraclePriceAggregatedIntervalDb {} +pub struct price_ticker { + pub db: RocksDB, +} -impl OraclePriceAggregatedIntervalDb { +impl price_ticker { pub async fn query(&self, limit: i32, lt: String) -> Result> { todo!() } pub async fn get(&self, id: String) -> Result { - todo!() + match self.db.get("price_ticker", id.as_bytes()) { + Ok(Some(value)) => { + let pool_swap: PriceTicker = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(pool_swap) + } + Ok(None) => Err(anyhow!("No data found for the given ID")), + Err(e) => Err(anyhow!(e)), + } } pub async fn put(&self, price: PriceTicker) -> Result<()> { - todo!() + match serde_json::to_string(&price) { + Ok(value) => { + let key = price.id.clone(); + self.db + .put("price_ticker", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("price_ticker", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs index 002a281c3c..47a22e9899 100644 --- a/lib/ain-ocean/src/database/db_manger.rs +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -5,10 +5,11 @@ use bitcoin::blockdata::block::Header; use bitcoin::consensus::encode::serialize; use rocksdb::Options; use rocksdb::{ColumnFamilyDescriptor, IteratorMode, DB}; +use serde::Deserialize; use std::collections::HashSet; use std::sync::Arc; -#[derive(Clone, Debug)] +#[derive(Debug)] pub struct RocksDB { db: Arc, cfs: HashSet, From 69b09dcda9d9acacc601d9e68d430b659c1e1960 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Mon, 4 Dec 2023 23:07:16 +0800 Subject: [PATCH 30/37] added method for oracle,price,order_history --- lib/ain-ocean/src/data_acces/order_history.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ain-ocean/src/data_acces/order_history.rs b/lib/ain-ocean/src/data_acces/order_history.rs index 43a0cdebb6..abe2f673fa 100644 --- a/lib/ain-ocean/src/data_acces/order_history.rs +++ b/lib/ain-ocean/src/data_acces/order_history.rs @@ -4,7 +4,6 @@ use crate::model::oracle_history::OracleHistory; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; - pub struct OracleHistoryDB { pub db: RocksDB, } From 6bd15ea55256b554a6252980bd75a2a799de5416 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 5 Dec 2023 11:59:56 +0800 Subject: [PATCH 31/37] merged feature/ain-ocean --- lib/ain-ocean/Cargo.toml | 2 -- lib/ain-ocean/src/api/mod.rs | 36 ------------------- lib/ain-ocean/src/api/rpc/block.rs | 16 --------- lib/ain-ocean/src/api/rpc/config.rs | 13 ------- lib/ain-ocean/src/api/rpc/mod.rs | 4 --- lib/ain-ocean/src/api/rpc/server.rs | 25 ------------- lib/ain-ocean/src/api/rpc/version.rs | 1 - .../src/data_acces/oracle_price_aggregated.rs | 2 +- lib/ain-ocean/src/database/mod.rs | 1 - lib/ain-ocean/src/lib.rs | 2 -- 10 files changed, 1 insertion(+), 101 deletions(-) delete mode 100644 lib/ain-ocean/src/api/rpc/block.rs delete mode 100644 lib/ain-ocean/src/api/rpc/config.rs delete mode 100644 lib/ain-ocean/src/api/rpc/mod.rs delete mode 100644 lib/ain-ocean/src/api/rpc/server.rs delete mode 100644 lib/ain-ocean/src/api/rpc/version.rs diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index 587c9b5c01..6621885886 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -21,8 +21,6 @@ serde_json = "1.0" json = "0.12.4" tokio-serde = {version="0.8.0", features = ["full"]} futures = "0.3.29" -async-trait = "0.1.74" -anyhow = "1.0.75" jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] } rocksdb = "0.21.0" ctrlc = "3.1.9" diff --git a/lib/ain-ocean/src/api/mod.rs b/lib/ain-ocean/src/api/mod.rs index 863f0c2181..4fc8d80da8 100644 --- a/lib/ain-ocean/src/api/mod.rs +++ b/lib/ain-ocean/src/api/mod.rs @@ -30,39 +30,3 @@ pub fn ocean_router() -> Router { .nest("/transactions", transactions::router()) .nest("/blocks", block::router()) } -pub mod rpc; -use std::net::SocketAddr; - -use axum::{Router, Server}; -use axum::Router; - -mod address; -mod block; -mod fee; -mod governance; -mod loan; -mod masternode; -mod oracle; -mod poolpairs; -mod prices; -mod rawtx; -mod stats; -mod tokens; -mod transactions; - -pub fn ocean_router() -> Router { - Router::new() - .nest("/address", address::router()) - .nest("/governance", governance::router()) - .nest("/loans", loan::router()) - .nest("/fee", fee::router()) - .nest("/masternodes", masternode::router()) - .nest("/oracles", oracle::router()) - .nest("/poolpairs", poolpairs::router()) - .nest("/prices", prices::router()) - .nest("/rawtx", rawtx::router()) - .nest("/stats", stats::router()) - .nest("/tokens", tokens::router()) - .nest("/transactions", transactions::router()) - .nest("/blocks", block::router()) -} diff --git a/lib/ain-ocean/src/api/rpc/block.rs b/lib/ain-ocean/src/api/rpc/block.rs deleted file mode 100644 index 6ed0fffce0..0000000000 --- a/lib/ain-ocean/src/api/rpc/block.rs +++ /dev/null @@ -1,16 +0,0 @@ -use anyhow::Result; -use jsonrpsee::RpcModule; - -pub fn register_block_methods(module: &mut RpcModule<()>) -> Result<()> { - module.register_method("block_getBlock", |_params, _| { - // Implement your method logic here - todo!() - })?; - - module.register_method("block_submitBlock", |_params, _| { - // Implement your method logic here - todo!() - })?; - - Ok(()) -} diff --git a/lib/ain-ocean/src/api/rpc/config.rs b/lib/ain-ocean/src/api/rpc/config.rs deleted file mode 100644 index b695fc6ba0..0000000000 --- a/lib/ain-ocean/src/api/rpc/config.rs +++ /dev/null @@ -1,13 +0,0 @@ -#[derive(Clone, Debug)] -pub struct Config { - pub enable: bool, - pub listen: String, -} -impl Default for Config { - fn default() -> Self { - Config { - enable: true, - listen: "127.0.0.1:3000".to_string(), - } - } -} diff --git a/lib/ain-ocean/src/api/rpc/mod.rs b/lib/ain-ocean/src/api/rpc/mod.rs deleted file mode 100644 index ae518690fd..0000000000 --- a/lib/ain-ocean/src/api/rpc/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod block; -pub mod config; -pub mod server; -pub mod version; diff --git a/lib/ain-ocean/src/api/rpc/server.rs b/lib/ain-ocean/src/api/rpc/server.rs deleted file mode 100644 index 158b0ddb1f..0000000000 --- a/lib/ain-ocean/src/api/rpc/server.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::api::rpc::block::register_block_methods; -use crate::api::rpc::config::Config; -use jsonrpsee::server::ServerBuilder; -use jsonrpsee::RpcModule; -use tokio::signal; - -#[derive(Clone, Debug)] -pub struct OceanRPCServer { - pub rpc_config: Config, -} - -impl OceanRPCServer { - pub async fn start_rpc_server(&self) -> anyhow::Result<()> { - let listen = self.rpc_config.listen.clone(); - let server = ServerBuilder::default().build(listen).await?; - let mut block_module = RpcModule::new(()); - register_block_methods(&mut block_module)?; - let server_handle = server.start(block_module); - // Wait for a Ctrl+C signal before shutting down - signal::ctrl_c().await.expect("failed to listen for event"); - let _ = server_handle.stop(); - - Ok(()) - } -} diff --git a/lib/ain-ocean/src/api/rpc/version.rs b/lib/ain-ocean/src/api/rpc/version.rs deleted file mode 100644 index 8b13789179..0000000000 --- a/lib/ain-ocean/src/api/rpc/version.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs index 47072f49e5..a16719eebe 100644 --- a/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs +++ b/lib/ain-ocean/src/data_acces/oracle_price_aggregated.rs @@ -14,7 +14,7 @@ impl OraclePriceAggrigatedDb { key: String, limit: i32, lt: String, - ) -> Result<(Vec)> { + ) -> Result> { todo!() } pub async fn put(&self, oracle: OraclePriceAggregated) -> Result<()> { diff --git a/lib/ain-ocean/src/database/mod.rs b/lib/ain-ocean/src/database/mod.rs index eeeec3caaf..cec892297a 100644 --- a/lib/ain-ocean/src/database/mod.rs +++ b/lib/ain-ocean/src/database/mod.rs @@ -1,4 +1,3 @@ pub mod config; pub mod db_manger; pub mod db_test; -pub mod db_test; diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 864b5c8543..5694e4c8f9 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -7,8 +7,6 @@ mod model; pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; pub mod api; -pub mod database; -mod api; mod data_acces; pub mod database; mod model; From 7d6d53edfa0d9ce6ec8e642d3a888ca4dd0a8ca7 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 5 Dec 2023 12:00:44 +0800 Subject: [PATCH 32/37] merged to feature/ain-ocean --- lib/Cargo.lock | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 641ec8cae8..4c77b73767 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -239,8 +239,6 @@ dependencies = [ "hex", "hyper 0.14.27", "jsonrpsee 0.20.3", - "hex", - "hyper 0.14.27", "log", "rocksdb", "serde", @@ -4953,15 +4951,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" -[[package]] -name = "signal-hook-registry" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" -dependencies = [ - "libc", -] - [[package]] name = "signature" version = "1.6.4" @@ -5744,9 +5733,7 @@ dependencies = [ "libc", "mio", "num_cpus", - "parking_lot", "pin-project-lite", - "signal-hook-registry", "socket2 0.5.5", "tokio-macros", "windows-sys 0.48.0", From 0df6dc39f49ca0f576ee62de353658e8f67049eb Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Tue, 5 Dec 2023 15:49:40 +0800 Subject: [PATCH 33/37] update data_acess module --- lib/ain-ocean/src/data_acces/block.rs | 26 ++++++--- lib/ain-ocean/src/data_acces/masternode.rs | 38 ++++++++++--- .../src/data_acces/masternode_states.rs | 53 ++++++++++++++++--- lib/ain-ocean/src/data_acces/raw_block.rs | 39 ++++++++++++++ lib/ain-ocean/src/data_acces/transaction.rs | 38 +++++++++---- .../src/data_acces/transaction_vin.rs | 27 ++++++---- .../src/data_acces/transaction_vout.rs | 39 ++++++++++---- lib/ain-ocean/src/database/db_manger.rs | 13 ++++- 8 files changed, 222 insertions(+), 51 deletions(-) diff --git a/lib/ain-ocean/src/data_acces/block.rs b/lib/ain-ocean/src/data_acces/block.rs index 09e2acf16a..200fd033f7 100644 --- a/lib/ain-ocean/src/data_acces/block.rs +++ b/lib/ain-ocean/src/data_acces/block.rs @@ -1,11 +1,13 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::block::Block; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Default)] -#[serde(rename_all = "camelCase")] -pub struct BlockDb {} +#[derive(Debug)] +pub struct BlockDb { + pub db: RocksDB, +} impl BlockDb { pub async fn get_by_hash(&self) -> Result { @@ -20,10 +22,20 @@ impl BlockDb { pub async fn query_by_height(&self, limit: i32, lt: i32) -> Result> { todo!() } - pub async fn store_block(&self, block: Block) -> Result { - todo!() + pub async fn store_block(&self, block: Block) -> Result<()> { + match serde_json::to_string(&block) { + Ok(value) => { + let key = block.id.clone(); + self.db.put("raw_block", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } - pub async fn delete_block(&self, hash: String) -> Result { - todo!() + pub async fn delete_block(&self, hash: String) -> Result<()> { + match self.db.delete("raw_block", hash.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/masternode.rs b/lib/ain-ocean/src/data_acces/masternode.rs index c97c1a882e..06ed84847d 100644 --- a/lib/ain-ocean/src/data_acces/masternode.rs +++ b/lib/ain-ocean/src/data_acces/masternode.rs @@ -1,21 +1,43 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::masternode::Masternode; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -pub struct MasterNode {} +pub struct MasterNodeDB { + pub db: RocksDB, +} -impl MasterNode { - pub async fn query(&self, limit: i32, lt: i32) -> Result> { +impl MasterNodeDB { + pub async fn query(&self, limit: i32, lt: i32) -> Result> { todo!() } - pub async fn get(&self) -> Result { - todo!() + pub async fn get(&self, id: String) -> Result> { + match self.db.get("masternode", id.as_bytes()) { + Ok(Some(value)) => { + let master_node: Masternode = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(master_node)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } } - pub async fn store(&self, stats: MasterNode) -> Result<()> { - todo!() + pub async fn store(&self, stats: Masternode) -> Result<()> { + match serde_json::to_string(&stats) { + Ok(value) => { + let key = stats.id.clone(); + self.db + .put("masternode", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("masternode", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/masternode_states.rs b/lib/ain-ocean/src/data_acces/masternode_states.rs index fe557c9c64..1ab26b4587 100644 --- a/lib/ain-ocean/src/data_acces/masternode_states.rs +++ b/lib/ain-ocean/src/data_acces/masternode_states.rs @@ -1,25 +1,64 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::masternode_stats::MasternodeStats; use anyhow::{anyhow, Result}; +use bitcoin::absolute::Height; +use rocksdb::{ColumnFamilyDescriptor, IteratorMode, DB}; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Default)] -#[serde(rename_all = "camelCase")] -pub struct MasterStatsDb {} +#[derive(Debug)] +pub struct MasterStatsDb { + pub db: RocksDB, +} impl MasterStatsDb { pub async fn get_latest(&self) -> Result { + // let mut latest_stats: Option = None; + // let mut highest_height = -1; + + // let iter = self.db.iterator("masternode_stats", IteratorMode::End); // Start from the end of the DB + + // for (key, value) in iter { + // let stats: MasternodeStats = serde_json::from_slice(&value)?; + // if stats.block.height > highest_height { + // highest_height = stats.block.height; + // latest_stats = Some(stats); + // } + // } + + // Ok(latest_stats); todo!() } pub async fn query(&self, limit: i32, lt: i32) -> Result> { todo!() } - pub async fn get(&self) -> Result { - todo!() + pub async fn get(&self, height: i32) -> Result> { + let bytes: &[u8] = &height.to_be_bytes(); + match self.db.get("masternode_stats", bytes) { + Ok(Some(value)) => { + let master_states: MasternodeStats = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(master_states)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } } pub async fn store(&self, stats: MasternodeStats) -> Result<()> { - todo!() + match serde_json::to_string(&stats) { + Ok(value) => { + let key = stats.block.height.clone(); + let bytes: &[u8] = &key.to_be_bytes(); + self.db.put("masternode_stats", bytes, value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, height: i32) -> Result<()> { - todo!() + let bytes: &[u8] = &height.to_be_bytes(); + match self.db.delete("masternode_stats", bytes) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } } diff --git a/lib/ain-ocean/src/data_acces/raw_block.rs b/lib/ain-ocean/src/data_acces/raw_block.rs index 8b13789179..f859fc80f5 100644 --- a/lib/ain-ocean/src/data_acces/raw_block.rs +++ b/lib/ain-ocean/src/data_acces/raw_block.rs @@ -1 +1,40 @@ +use crate::database::db_manger::ColumnFamilyOperations; +use crate::database::db_manger::RocksDB; +use crate::model::raw_block::RawBlock; +use anyhow::{anyhow, Result}; +use serde::{Deserialize, Serialize}; +#[derive(Debug)] +pub struct RawBlockDb { + pub db: RocksDB, +} + +impl RawBlockDb { + pub async fn get(&self, hash: String) -> Result> { + match self.db.get("raw_block", hash.as_bytes()) { + Ok(Some(value)) => { + let trx: RawBlock = serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(trx)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } + } + + pub async fn store(&self, block: RawBlock) -> Result<()> { + match serde_json::to_string(&block) { + Ok(value) => { + let key = block.id.clone(); + self.db.put("raw_block", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } + } + pub async fn delete(&self, hash: String) -> Result<()> { + match self.db.delete("raw_block", hash.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } + } +} diff --git a/lib/ain-ocean/src/data_acces/transaction.rs b/lib/ain-ocean/src/data_acces/transaction.rs index f0e0d31ce9..e16701aa77 100644 --- a/lib/ain-ocean/src/data_acces/transaction.rs +++ b/lib/ain-ocean/src/data_acces/transaction.rs @@ -1,21 +1,41 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::transaction::Transaction; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Default)] -#[serde(rename_all = "camelCase")] -pub struct TransactionVinDb {} +#[derive(Debug)] +pub struct TransactionVinDb { + pub db: RocksDB, +} impl TransactionVinDb { - pub async fn get(&self, txid: String) -> Result { - todo!() + pub async fn get(&self, txid: String) -> Result> { + match self.db.get("transaction", txid.as_bytes()) { + Ok(Some(value)) => { + let trx: Transaction = serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(trx)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } } - pub async fn store(&self, txn: Transaction) -> Result { - todo!() + pub async fn store(&self, txn: Transaction) -> Result<()> { + match serde_json::to_string(&txn) { + Ok(value) => { + let key = txn.id.clone(); + self.db + .put("transaction", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } - pub async fn delete(&self, txid: String) -> Result { - todo!() + pub async fn delete(&self, txid: String) -> Result<()> { + match self.db.delete("transaction", txid.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } pub async fn query_by_blockhash( &self, diff --git a/lib/ain-ocean/src/data_acces/transaction_vin.rs b/lib/ain-ocean/src/data_acces/transaction_vin.rs index 1b1ec29896..1aae77ab1f 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vin.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vin.rs @@ -1,21 +1,30 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::transaction_vin::TransactionVin; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Default)] -#[serde(rename_all = "camelCase")] -pub struct TransactionVinDb {} +#[derive(Debug)] +pub struct TransactionVinDb { + pub db: RocksDB, +} impl TransactionVinDb { - pub async fn get(&self) -> Result { - todo!() - } - pub async fn store(&self, stats: TransactionVin) -> Result<()> { - todo!() + pub async fn store(&self, trx_vin: TransactionVin) -> Result<()> { + match serde_json::to_string(&trx_vin) { + Ok(value) => { + let key = trx_vin.id.clone(); + self.db.put("oracle", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } pub async fn delete(&self, id: String) -> Result<()> { - todo!() + match self.db.delete("transaction_vin", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result> { todo!() diff --git a/lib/ain-ocean/src/data_acces/transaction_vout.rs b/lib/ain-ocean/src/data_acces/transaction_vout.rs index 5c62bb5ef5..7cf54ec7c0 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vout.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vout.rs @@ -1,21 +1,42 @@ +use crate::database::db_manger::ColumnFamilyOperations; use crate::database::db_manger::RocksDB; use crate::model::transaction_vout::TransactionVout; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Debug, Default)] -#[serde(rename_all = "camelCase")] -pub struct TransactionVinDb {} +#[derive(Debug)] +pub struct TransactionVinDb { + pub db: RocksDB, +} impl TransactionVinDb { - pub async fn get(&self) -> Result { - todo!() + pub async fn get(&self, txid: String, n: i64) -> Result> { + match self.db.get("transaction_vout", txid.as_bytes()) { + Ok(Some(value)) => { + let master_node: TransactionVout = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(master_node)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } } - pub async fn store(&self, stats: TransactionVout) -> Result { - todo!() + pub async fn store(&self, trx_out: TransactionVout) -> Result<()> { + match serde_json::to_string(&trx_out) { + Ok(value) => { + let key = trx_out.id.clone(); + self.db + .put("transaction_vout", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } } - pub async fn delete(&self, id: String) -> Result { - todo!() + pub async fn delete(&self, id: String) -> Result<()> { + match self.db.delete("transaction_vout", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } } pub async fn query(&self, txid: String, limit: i32, lt: i32) -> Result { todo!() diff --git a/lib/ain-ocean/src/database/db_manger.rs b/lib/ain-ocean/src/database/db_manger.rs index 47a22e9899..bdf9b85192 100644 --- a/lib/ain-ocean/src/database/db_manger.rs +++ b/lib/ain-ocean/src/database/db_manger.rs @@ -4,8 +4,8 @@ use bitcoin::blockdata::block::Block; use bitcoin::blockdata::block::Header; use bitcoin::consensus::encode::serialize; use rocksdb::Options; -use rocksdb::{ColumnFamilyDescriptor, IteratorMode, DB}; -use serde::Deserialize; +use rocksdb::{ColumnFamilyDescriptor, DBIterator, IteratorMode, DB}; +use serde::{Deserialize, Serialize}; use std::collections::HashSet; use std::sync::Arc; @@ -20,6 +20,7 @@ pub trait ColumnFamilyOperations { fn put(&self, cf_name: &str, key: &[u8], value: &[u8]) -> Result<()>; fn delete(&self, cf_name: &str, key: &[u8]) -> Result<()>; fn get_total_row(&self) -> Result<()>; + fn iterator(&self, cf_name: &str, mode: IteratorMode) -> Result<(DBIterator)>; } impl RocksDB { @@ -270,4 +271,12 @@ impl ColumnFamilyOperations for RocksDB { println!("Total rows in 'block': {}", block_count); Ok(()) } + + fn iterator(&self, cf_name: &str, mode: IteratorMode) -> Result { + if let Some(cf_handle) = self.db.cf_handle(cf_name) { + Ok(self.db.iterator_cf(cf_handle, mode)) + } else { + Err(anyhow!("Column family not found")) + } + } } From f2d477bd63eda024c6f7965e70210c38e3c3ff58 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Wed, 6 Dec 2023 13:55:56 +0800 Subject: [PATCH 34/37] added more functionality to module --- .../src/data_acces/script_activity.rs | 32 ++++++++++++++ .../src/data_acces/script_aggregation.rs | 43 +++++++++++++++++++ .../src/data_acces/script_unspent.rs | 32 ++++++++++++++ .../data_acces/vault_auction_batch_history.rs | 40 +++++++++++++++++ 4 files changed, 147 insertions(+) diff --git a/lib/ain-ocean/src/data_acces/script_activity.rs b/lib/ain-ocean/src/data_acces/script_activity.rs index 8b13789179..fcdc9985fd 100644 --- a/lib/ain-ocean/src/data_acces/script_activity.rs +++ b/lib/ain-ocean/src/data_acces/script_activity.rs @@ -1 +1,33 @@ +use crate::database::db_manger::ColumnFamilyOperations; +use crate::database::db_manger::RocksDB; +use crate::model::script_activity::ScriptActivity; +use anyhow::{anyhow, Error, Result}; +use serde::{Deserialize, Serialize}; +use serde_json; +pub struct ScriptUnspentDB { + pub db: RocksDB, +} + +impl ScriptUnspentDB { + pub async fn query(&self, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn store(&self, unspent: ScriptActivity) -> Result<()> { + match serde_json::to_string(&unspent) { + Ok(value) => { + let key = unspent.id.clone(); + self.db + .put("script_activity", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } + } + pub async fn delete(&self, id: String) -> Result<()> { + match self.db.delete("script_activity", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } + } +} diff --git a/lib/ain-ocean/src/data_acces/script_aggregation.rs b/lib/ain-ocean/src/data_acces/script_aggregation.rs index 8b13789179..0b630ff016 100644 --- a/lib/ain-ocean/src/data_acces/script_aggregation.rs +++ b/lib/ain-ocean/src/data_acces/script_aggregation.rs @@ -1 +1,44 @@ +use crate::database::db_manger::ColumnFamilyOperations; +use crate::database::db_manger::RocksDB; +use crate::model::script_aggregation::ScriptAggregation; +use anyhow::{anyhow, Error, Result}; +use serde::{Deserialize, Serialize}; +use serde_json; +pub struct ScriptAggretionDB { + pub db: RocksDB, +} + +impl ScriptAggretionDB { + pub async fn query(&self, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn store(&self, aggregation: ScriptAggregation) -> Result<()> { + match serde_json::to_string(&aggregation) { + Ok(value) => { + let key = aggregation.id.clone(); + self.db + .put("script_aggregation", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } + } + pub async fn get(&self, id: String) -> Result> { + match self.db.get("script_aggregation", id.as_bytes()) { + Ok(Some(value)) => { + let oracle: ScriptAggregation = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(oracle)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } + } + pub async fn delete(&self, id: String) -> Result<()> { + match self.db.delete("script_aggregation", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } + } +} diff --git a/lib/ain-ocean/src/data_acces/script_unspent.rs b/lib/ain-ocean/src/data_acces/script_unspent.rs index 8b13789179..e414384ac4 100644 --- a/lib/ain-ocean/src/data_acces/script_unspent.rs +++ b/lib/ain-ocean/src/data_acces/script_unspent.rs @@ -1 +1,33 @@ +use crate::database::db_manger::ColumnFamilyOperations; +use crate::database::db_manger::RocksDB; +use crate::model::script_unspent::ScriptUnspent; +use anyhow::{anyhow, Error, Result}; +use serde::{Deserialize, Serialize}; +use serde_json; +pub struct ScriptUnspentDB { + pub db: RocksDB, +} + +impl ScriptUnspentDB { + pub async fn query(&self, limit: i32, lt: String) -> Result> { + todo!() + } + pub async fn store(&self, unspent: ScriptUnspent) -> Result<()> { + match serde_json::to_string(&unspent) { + Ok(value) => { + let key = unspent.id.clone(); + self.db + .put("script_unspent", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } + } + pub async fn delete(&self, id: String) -> Result<()> { + match self.db.delete("script_unspent", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } + } +} diff --git a/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs b/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs index 8b13789179..7d038be647 100644 --- a/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs +++ b/lib/ain-ocean/src/data_acces/vault_auction_batch_history.rs @@ -1 +1,41 @@ +use crate::database::db_manger::ColumnFamilyOperations; +use crate::database::db_manger::RocksDB; +use crate::model::vault_auction_batch_history::VaultAuctionBatchHistory; +use anyhow::{anyhow, Error, Result}; +use serde::{Deserialize, Serialize}; +use serde_json; +pub struct VaultAuctionDB { + pub db: RocksDB, +} + +impl VaultAuctionDB { + pub async fn store(&self, auction: VaultAuctionBatchHistory) -> Result<()> { + match serde_json::to_string(&auction) { + Ok(value) => { + let key = auction.id.clone(); + self.db + .put("vault_auction_history", key.as_bytes(), value.as_bytes())?; + Ok(()) + } + Err(e) => Err(anyhow!(e)), + } + } + pub async fn get(&self, id: String) -> Result> { + match self.db.get("vault_auction_history", id.as_bytes()) { + Ok(Some(value)) => { + let oracle: VaultAuctionBatchHistory = + serde_json::from_slice(&value).map_err(|e| anyhow!(e))?; + Ok(Some(oracle)) + } + Ok(None) => Ok(None), + Err(e) => Err(anyhow!(e)), + } + } + pub async fn delete(&self, id: String) -> Result<()> { + match self.db.delete("vault_auction_history", id.as_bytes()) { + Ok(_) => Ok(()), + Err(e) => Err(anyhow!(e)), + } + } +} From 0e7efd886cf4db2f1353a5c8e94e604961c8ce7f Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Fri, 8 Dec 2023 16:18:59 +0800 Subject: [PATCH 35/37] fixed transaction module --- lib/ain-ocean/src/data_acces/test/mod.rs | 1 + .../src/data_acces/test/transaction_test.rs | 99 +++++++++++++++++++ .../src/data_acces/transaction_vout.rs | 4 +- lib/ain-ocean/src/model/transaction.rs | 8 +- 4 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 lib/ain-ocean/src/data_acces/test/transaction_test.rs diff --git a/lib/ain-ocean/src/data_acces/test/mod.rs b/lib/ain-ocean/src/data_acces/test/mod.rs index d490f93649..bd9b68cbdc 100644 --- a/lib/ain-ocean/src/data_acces/test/mod.rs +++ b/lib/ain-ocean/src/data_acces/test/mod.rs @@ -1 +1,2 @@ mod oracle_test; +mod transaction_test; diff --git a/lib/ain-ocean/src/data_acces/test/transaction_test.rs b/lib/ain-ocean/src/data_acces/test/transaction_test.rs new file mode 100644 index 0000000000..f835b40e3e --- /dev/null +++ b/lib/ain-ocean/src/data_acces/test/transaction_test.rs @@ -0,0 +1,99 @@ +#[cfg(test)] +mod tests { + use crate::data_acces::transaction::TransactionVinDb; + use crate::data_acces::transaction_vout::TransactionVoutDb; + use crate::database::db_manger::{ColumnFamilyOperations, RocksDB}; + use crate::model::transaction::{Transaction, TransactionBlock}; + use crate::model::transaction_vout::{TransactionVout, TransactionVoutScript}; + use tempfile::tempdir; + use tokio::task; + + fn setup_test_db() -> TransactionVinDb { + let temp_dir = tempdir().unwrap(); + let db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); + TransactionVinDb { db } + } + + // Sample transaction for testing + fn sample_transaction(id: &str) -> Transaction { + Transaction { + id: id.to_string(), + order: 1, + block: TransactionBlock { + hash: "sample_hash".to_string(), + height: 123, + time: 1000, + median_time: 1000, + }, + txid: id.to_string(), + hash: "sample_transaction_hash".to_string(), + version: 1, + size: 200, + v_size: 200, + weight: 300, + total_vout_value: "1000".to_string(), + lock_time: 0, + vin_count: 2, + vout_count: 2, + } + } + + fn sample_transaction_Vout() { + // Instantiate a sample TransactionVoutScript + let sample_script = TransactionVoutScript { + hex: "76a91488ac".to_string(), // Sample hex string + r#type: "pubkeyhash".to_string(), // Sample type + }; + + // Instantiate a sample TransactionVout + let sample_vout = TransactionVout { + id: "vout1".to_string(), // Sample ID + txid: "tx12345".to_string(), // Sample transaction ID + n: 0, // Sample index + value: "0.0001".to_string(), // Sample value in BTC or similar currency + token_id: 123, // Sample token ID + script: sample_script, // Use the sample script created above + }; + } + + #[tokio::test] + async fn test_store_transaction() { + let txn_vin_db = setup_test_db(); + + let test_transaction = sample_transaction("tx1"); + + let result = txn_vin_db.store(test_transaction).await; + assert!(result.is_ok()); + } + + #[tokio::test] + async fn test_store_get_transaction() { + let txn_vin_db = setup_test_db(); + + let test_transaction = sample_transaction("tx1"); + let trx_id = test_transaction.id.clone(); + + let result = txn_vin_db.store(test_transaction.clone()).await; + assert!(result.is_ok()); + + let result = txn_vin_db.get(trx_id).await.unwrap().unwrap(); + assert_eq!(test_transaction, result); + } + + #[tokio::test] + async fn test_store_get_delete_transaction() { + let txn_vin_db = setup_test_db(); + + let test_transaction = sample_transaction("tx1"); + let trx_id = test_transaction.id.clone(); + + let result = txn_vin_db.store(test_transaction.clone()).await; + assert!(result.is_ok()); + + let result = txn_vin_db.get(trx_id.clone()).await.unwrap().unwrap(); + assert_eq!(test_transaction, result); + + let result = txn_vin_db.delete(trx_id).await; + assert!(result.is_ok()); + } +} diff --git a/lib/ain-ocean/src/data_acces/transaction_vout.rs b/lib/ain-ocean/src/data_acces/transaction_vout.rs index 7cf54ec7c0..145e68b282 100644 --- a/lib/ain-ocean/src/data_acces/transaction_vout.rs +++ b/lib/ain-ocean/src/data_acces/transaction_vout.rs @@ -5,11 +5,11 @@ use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; #[derive(Debug)] -pub struct TransactionVinDb { +pub struct TransactionVoutDb { pub db: RocksDB, } -impl TransactionVinDb { +impl TransactionVoutDb { pub async fn get(&self, txid: String, n: i64) -> Result> { match self.db.get("transaction_vout", txid.as_bytes()) { Ok(Some(value)) => { diff --git a/lib/ain-ocean/src/model/transaction.rs b/lib/ain-ocean/src/model/transaction.rs index 3edcb78a8a..911c2d22b8 100644 --- a/lib/ain-ocean/src/model/transaction.rs +++ b/lib/ain-ocean/src/model/transaction.rs @@ -1,4 +1,7 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] pub struct Transaction { pub id: String, pub order: i32, @@ -15,7 +18,8 @@ pub struct Transaction { pub vout_count: i32, } -#[derive(Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] pub struct TransactionBlock { pub hash: String, pub height: i32, From 7aacc02d7e8a005176325e7dd4514cdf69230b2d Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Fri, 8 Dec 2023 17:39:52 +0800 Subject: [PATCH 36/37] rebased ocean-archive --- lib/Cargo.lock | 293 +++++++------- lib/ain-ocean/Cargo.toml | 3 +- lib/ain-ocean/src/api/block.rs | 23 +- lib/ain-ocean/src/api_paged_response.rs | 65 ++-- lib/ain-ocean/src/data_acces/masternode.rs | 1 + lib/ain-ocean/src/database/db_test.rs | 360 +++++++++--------- lib/ain-ocean/src/error.rs | 8 +- lib/ain-ocean/src/lib.rs | 4 - lib/ain-ocean/src/model/masternode.rs | 2 +- .../model/oracle_price_aggregated_interval.rs | 3 - lib/ain-ocean/src/model/price_ticker.rs | 2 +- lib/ain-ocean/src/model/script_activity.rs | 22 +- lib/ain-ocean/src/model/script_aggregation.rs | 11 +- lib/ain-ocean/src/model/script_unspent.rs | 9 +- lib/ain-ocean/src/model/transaction_vin.rs | 9 +- lib/ain-ocean/src/model/transaction_vout.rs | 5 +- 16 files changed, 408 insertions(+), 412 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index 4c77b73767..c1703522ea 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -113,7 +113,6 @@ dependencies = [ "ain-cpp-imports", "anyhow", "axum 0.7.2", - "axum 0.7.1", "bincode", "ethabi", "ethbloom", @@ -165,7 +164,6 @@ dependencies = [ "anyhow", "async-trait", "axum 0.7.2", - "axum 0.7.1", "cxx", "env_logger", "ethereum", @@ -210,42 +208,31 @@ dependencies = [ "syn 2.0.39", ] -[[package]] -name = "ain-ocean" -version = "0.1.0" -dependencies = [ - "axum 0.7.2", - "bitcoin", - "dftx-rs", - "hex", - "hyper 0.14.27", - "keccak-hash", - "log", - "serde", - "thiserror", -] - [[package]] name = "ain-ocean" version = "0.1.0" dependencies = [ "anyhow", - "async-trait", - "axum 0.7.1", + "axum 0.7.2", "bitcoin", - "bitcoin_hashes", + "bitcoin_hashes 0.12.0", "ctrlc", + "dftx-rs", "futures", "hex", "hyper 0.14.27", + "json", "jsonrpsee 0.20.3", + "keccak-hash", "log", "rocksdb", "serde", "serde_json", "structopt", + "tarpc", "tempdir", "tempfile", + "thiserror", "tokio", "tokio-serde", ] @@ -433,7 +420,6 @@ dependencies = [ "bytes", "futures-util", "http 0.2.11", - "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", "itoa", @@ -444,11 +430,6 @@ dependencies = [ "pin-project-lite", "rustversion", "serde", - "serde_json", - "serde_path_to_error", - "serde_urlencoded", - "sync_wrapper", - "tokio", "sync_wrapper", "tower", "tower-layer", @@ -464,12 +445,6 @@ dependencies = [ "async-trait", "axum-core 0.4.1", "axum-macros", -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "810a80b128d70e6ed2bdf3fe8ed72c0ae56f5f5948d01c2753282dd92a84fce8" -dependencies = [ - "async-trait", - "axum-core 0.4.0", "bytes", "futures-util", "http 1.0.0", @@ -505,7 +480,6 @@ dependencies = [ "bytes", "futures-util", "http 0.2.11", - "http 0.2.9", "http-body 0.4.5", "mime", "rustversion", @@ -518,9 +492,6 @@ name = "axum-core" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77cb22c689c44d4c07b0ab44ebc25d69d8ae601a2f28fb8d672d344178fa17aa" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de0ddc355eab88f4955090a823715df47acf0b7660aab7a69ad5ce6301ee3b73" dependencies = [ "async-trait", "bytes", @@ -602,9 +573,6 @@ name = "bech32" version = "0.10.0-beta" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" [[package]] name = "beef" @@ -668,7 +636,7 @@ dependencies = [ "bech32", "bitcoin-internals", "bitcoin-io", - "bitcoin_hashes", + "bitcoin_hashes 0.13.0", "hex-conservative", "hex_lit", "secp256k1 0.28.0", @@ -685,25 +653,6 @@ name = "bitcoin-io" version = "0.1.0" source = "git+https://github.com/Jouzo/rust-bitcoin.git#d540306b57be5ff16b120c0e70b87512d289c262" -[[package]] -name = "bitcoin_hashes" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" -dependencies = [ - "bitcoin-internals", - "hex-conservative", -version = "0.30.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1945a5048598e4189e239d3f809b19bdad4845c4b2ba400d304d2dcf26d2c462" -dependencies = [ - "bech32", - "bitcoin-private", - "bitcoin_hashes", - "hex_lit", - "secp256k1 0.27.0", -] - [[package]] name = "bitcoin-private" version = "0.1.0" @@ -719,6 +668,16 @@ dependencies = [ "bitcoin-private", ] +[[package]] +name = "bitcoin_hashes" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" +dependencies = [ + "bitcoin-internals", + "hex-conservative", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -1078,6 +1037,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + [[package]] name = "crossbeam-deque" version = "0.8.3" @@ -1505,18 +1474,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "educe" -version = "0.4.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f0042ff8246a363dbe77d2ceedb073339e85a804b9a47636c6e016a9a32c05f" -dependencies = [ - "enum-ordinalize", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "either" version = "1.9.0" @@ -1560,19 +1517,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "enum-ordinalize" -version = "3.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf1fa3f06bbff1ea5b1a9c7b14aa992a39657db60a2759457328d7e058f49ee" -dependencies = [ - "num-bigint", - "num-traits", - "proc-macro2", - "quote", - "syn 2.0.38", -] - [[package]] name = "env_logger" version = "0.10.1" @@ -2134,27 +2078,6 @@ dependencies = [ "futures-util", "http 1.0.0", "indexmap 2.1.0", - "http 0.2.9", - "indexmap 1.9.3", - "slab", - "tokio", - "tokio-util", - "tracing", -] - -[[package]] -name = "h2" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1d308f63daf4181410c242d34c11f928dcb3aa105852019e043c9d1f4e4368a" -dependencies = [ - "bytes", - "fnv", - "futures-core", - "futures-sink", - "futures-util", - "http 1.0.0", - "indexmap 2.0.2", "slab", "tokio", "tokio-util", @@ -2335,17 +2258,6 @@ dependencies = [ "itoa", ] -[[package]] -name = "http" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - [[package]] name = "http-body" version = "0.4.5" @@ -2354,7 +2266,6 @@ checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" dependencies = [ "bytes", "http 0.2.11", - "http 0.2.9", "pin-project-lite", ] @@ -2417,8 +2328,6 @@ dependencies = [ "futures-util", "h2 0.3.22", "http 0.2.11", - "h2 0.3.21", - "http 0.2.9", "http-body 0.4.5", "httparse", "httpdate", @@ -2458,7 +2367,6 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", "http 0.2.11", - "http 0.2.9", "hyper 0.14.27", "log", "rustls", @@ -2688,6 +2596,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd" + [[package]] name = "jsonrpsee" version = "0.16.3" @@ -2889,7 +2803,6 @@ dependencies = [ "futures-channel", "futures-util", "http 0.2.11", - "http 0.2.9", "hyper 0.14.27", "jsonrpsee-core 0.16.3", "jsonrpsee-types 0.16.3", @@ -2910,7 +2823,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82c39a00449c9ef3f50b84fc00fc4acba20ef8f559f07902244abf4c15c5ab9c" dependencies = [ "futures-util", - "http 0.2.9", + "http 0.2.11", "hyper 0.14.27", "jsonrpsee-core 0.20.3", "jsonrpsee-types 0.20.3", @@ -3650,6 +3563,49 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" +[[package]] +name = "opentelemetry" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69d6c3d7288a106c0a363e4b0e8d308058d56902adefb16f4936f417ffef086e" +dependencies = [ + "opentelemetry_api", + "opentelemetry_sdk", +] + +[[package]] +name = "opentelemetry_api" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c24f96e21e7acc813c7a8394ee94978929db2bcc46cf6b5014fc612bf7760c22" +dependencies = [ + "futures-channel", + "futures-util", + "indexmap 1.9.3", + "js-sys", + "once_cell", + "pin-project-lite", + "thiserror", +] + +[[package]] +name = "opentelemetry_sdk" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ca41c4933371b61c2a2f214bf16931499af4ec90543604ec828f7a625c09113" +dependencies = [ + "async-trait", + "crossbeam-channel", + "futures-channel", + "futures-executor", + "futures-util", + "once_cell", + "opentelemetry_api", + "percent-encoding", + "rand 0.8.5", + "thiserror", +] + [[package]] name = "option-ext" version = "0.2.0" @@ -4335,8 +4291,6 @@ dependencies = [ "futures-util", "h2 0.3.22", "http 0.2.11", - "h2 0.3.21", - "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", "hyper-rustls", @@ -4697,14 +4651,8 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2acea373acb8c21ecb5a23741452acd2593ed44ee3d343e72baaa143bc89d0d5" dependencies = [ - "bitcoin_hashes", + "bitcoin_hashes 0.13.0", "secp256k1-sys 0.9.0", -version = "0.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" -dependencies = [ - "bitcoin_hashes", - "secp256k1-sys 0.8.1", ] [[package]] @@ -4721,9 +4669,6 @@ name = "secp256k1-sys" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09e67c467c38fd24bd5499dc9a18183b31575c12ee549197e3e20d57aa4fe3b7" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70a129b9e9efbfb223753b9163c4ab3b13cff7fd9c7f010fbac25ab4099fa07e" dependencies = [ "cc", ] @@ -4951,6 +4896,15 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7cee0529a6d40f580e7a5e6c495c8fbfe21b7b52795ed4bb5e62cdf92bc6380" +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "1.6.4" @@ -5031,7 +4985,6 @@ dependencies = [ "bytes", "futures", "http 0.2.11", - "http 0.2.9", "httparse", "log", "rand 0.8.5", @@ -5269,7 +5222,7 @@ dependencies = [ "sp-std", "tracing", "tracing-core", - "tracing-subscriber", + "tracing-subscriber 0.2.25", ] [[package]] @@ -5559,6 +5512,41 @@ version = "0.12.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" +[[package]] +name = "tarpc" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f41bce44d290df0598ae4b9cd6ea7f58f651fd3aa4af1b26060c4fa32b08af7" +dependencies = [ + "anyhow", + "fnv", + "futures", + "humantime", + "opentelemetry", + "pin-project", + "rand 0.8.5", + "serde", + "static_assertions", + "tarpc-plugins", + "thiserror", + "tokio", + "tokio-serde", + "tokio-util", + "tracing", + "tracing-opentelemetry", +] + +[[package]] +name = "tarpc-plugins" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee42b4e559f17bce0385ebf511a7beb67d5cc33c12c96b7f4e9789919d9c10f" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "tempdir" version = "0.3.7" @@ -5733,7 +5721,9 @@ dependencies = [ "libc", "mio", "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2 0.5.5", "tokio-macros", "windows-sys 0.48.0", @@ -5777,12 +5767,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "911a61637386b789af998ee23f50aa30d5fd7edcec8d6d3dedae5e5815205466" dependencies = [ "bytes", - "educe", "futures-core", "futures-sink", "pin-project", - "serde", - "serde_json", ] [[package]] @@ -5807,6 +5794,7 @@ dependencies = [ "futures-io", "futures-sink", "pin-project-lite", + "slab", "tokio", "tracing", ] @@ -5853,8 +5841,6 @@ dependencies = [ "futures-util", "h2 0.3.22", "http 0.2.11", - "h2 0.3.21", - "http 0.2.9", "http-body 0.4.5", "hyper 0.14.27", "hyper-timeout", @@ -5915,7 +5901,6 @@ dependencies = [ "futures-core", "futures-util", "http 0.2.11", - "http 0.2.9", "http-body 0.4.5", "http-range-header", "httpdate", @@ -5989,6 +5974,19 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-opentelemetry" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21ebb87a95ea13271332df069020513ab70bdb5637ca42d6e492dc3bbbad48de" +dependencies = [ + "once_cell", + "opentelemetry", + "tracing", + "tracing-core", + "tracing-subscriber 0.3.18", +] + [[package]] name = "tracing-serde" version = "0.1.3" @@ -6021,6 +6019,17 @@ dependencies = [ "tracing-serde", ] +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", +] + [[package]] name = "trie-db" version = "0.27.1" diff --git a/lib/ain-ocean/Cargo.toml b/lib/ain-ocean/Cargo.toml index 6621885886..a695e522ca 100644 --- a/lib/ain-ocean/Cargo.toml +++ b/lib/ain-ocean/Cargo.toml @@ -19,7 +19,7 @@ tarpc = { version = "0.33", features = ["tokio1", "serde1", "serde-transport","t tokio = { version = "1", features = ["full"] } serde_json = "1.0" json = "0.12.4" -tokio-serde = {version="0.8.0", features = ["full"]} +tokio-serde = {version="0.8.0"} futures = "0.3.29" jsonrpsee = { version = "0.20.3", features = ["server", "macros", "http-client", "jsonrpsee-server"] } rocksdb = "0.21.0" @@ -28,3 +28,4 @@ tempdir = "0.3.7" bitcoin_hashes = "0.12.0" structopt = { version = "0.3", default-features = false } tempfile = "3.8.1" +anyhow.workspace = true diff --git a/lib/ain-ocean/src/api/block.rs b/lib/ain-ocean/src/api/block.rs index 09982a233f..80c8191b90 100644 --- a/lib/ain-ocean/src/api/block.rs +++ b/lib/ain-ocean/src/api/block.rs @@ -1,9 +1,12 @@ -use axum::{debug_handler, extract::{Path, Query}, Json, routing::get, Router}; +use axum::{ + debug_handler, + extract::{Path, Query}, + routing::get, + Json, Router, +}; use serde::{Deserialize, Serialize}; use crate::api_paged_response::ApiPagedResponse; -use axum::{extract::Path, routing::get, Router}; -use serde::Deserialize; #[derive(Deserialize)] struct BlockId { @@ -18,7 +21,7 @@ struct BlockHash { #[derive(Deserialize)] pub struct ListBlocksQuery { pub size: usize, - pub next: Option + pub next: Option, } #[debug_handler] @@ -31,15 +34,9 @@ async fn list_blocks(Query(query): Query) -> Json String { - "List of blocks".to_string() + Json(ApiPagedResponse::of(blocks, query.size, |block| { + block.clone().id + })) } async fn get_block(Path(BlockId { id }): Path) -> String { diff --git a/lib/ain-ocean/src/api_paged_response.rs b/lib/ain-ocean/src/api_paged_response.rs index 83ac978df9..24a877707f 100644 --- a/lib/ain-ocean/src/api_paged_response.rs +++ b/lib/ain-ocean/src/api_paged_response.rs @@ -5,17 +5,17 @@ use serde::Serialize; /// Each ApiPagedResponse holds the data array and the "token" for next part of the slice. /// The next token should be passed via query 'next' and only used when getting the next slice. /// Hence the first request, the next token is always empty and not provided. -/// +/// /// With ascending sorted list and a limit of 3 items per slice will have the behaviour as such. -/// +/// /// SORTED : | [1] [2] [3] | [4] [5] [6] | [7] [8] [9] | [10] /// Query 1 : Data: [1] [2] [3], Next: 3, Operator: GT (>) /// Query 2 : Data: [4] [5] [6], Next: 6, Operator: GT (>) /// Query 3 : Data: [7] [8] [9], Next: 3, Operator: GT (>) /// Query 4 : Data: [10], Next: undefined -/// +/// /// This design is resilient also mutating sorted list, where pagination is not. -/// +/// /// SORTED : [2] [4] [6] [8] [10] [12] [14] /// Query 1 : Data: [2] [4] [6], Next: 6, Operator: GT (>) /// @@ -27,7 +27,7 @@ use serde::Serialize; /// Limitations of this requires your dat astructure to always be sorted in one direction and your sort /// indexes always fixed. Hence the moving down of the slice window, your operator will be greater than (GT). /// While moving up your operator will be less than (GT). -/// +/// /// ASC : | [1] [2] [3] | [4] [5] [6] | [7] [8] [9] | /// >3 >6 >9 /// DESC : | [9] [8] [7] | [6] [5] [4] | [3] [2] [1] | @@ -37,15 +37,15 @@ use serde::Serialize; /// when the usage narrative is clear and so will the use of ease. LIST query must be dead simple. /// Image travelling down the path, and getting a "next token" to get the next set of itmes to /// continue walking. -/// +/// /// Because the limit is not part of the slice window your query mechanism should support varying size windows. -/// +/// /// DATA: | [1] [2] [3] | [4] [5] [6] [7] | [8] [9] | ... /// | limit 3, >3 | limit 4, >7 | limit 2, >9 /// For simplicity your API should not attempt to allow access to different sort indexes, be cognizant of /// how our APIs are consumed. If we create a GET /blocks operation to list blocks what would the correct indexes /// be 99% of the time? -/// +/// /// Answer: Blocks sorted by height in descending order, that's your sorted list and your slice window. /// : <- Latest | [100] [99] [98] [97] [...] | Oldest -> /// @@ -62,7 +62,12 @@ struct ApiPage { impl ApiPagedResponse { pub fn new(data: Vec, next: Option<&str>) -> Self { - Self { data, page: ApiPage{ next: next.map(Into::into) } } // Option<&str> -> Option + Self { + data, + page: ApiPage { + next: next.map(Into::into), + }, + } // Option<&str> -> Option } pub fn next(data: Vec, next: Option<&str>) -> Self { @@ -77,7 +82,7 @@ impl ApiPagedResponse { Self::next(data, None) } } - + pub fn empty() -> Self { Self::new(Vec::new(), None) } @@ -85,14 +90,14 @@ impl ApiPagedResponse { #[cfg(test)] mod tests { - use super::{ApiPagedResponse, ApiPage}; - + use super::{ApiPage, ApiPagedResponse}; + #[derive(Clone, Debug)] struct Item { id: String, sort: String, } - + impl Item { fn new(id: &str, sort: &str) -> Self { Self { @@ -101,25 +106,19 @@ mod tests { } } } - + #[test] fn should_next_with_none() { - let items: Vec = vec![ - Item::new("0", "a"), - Item::new("1", "b"), - ]; - + let items: Vec = vec![Item::new("0", "a"), Item::new("1", "b")]; + let next = ApiPagedResponse::next(items, None).page.next; assert_eq!(next, None); } #[test] fn should_next_with_value() { - let items: Vec = vec![ - Item::new("0", "a"), - Item::new("1", "b"), - ]; - + let items: Vec = vec![Item::new("0", "a"), Item::new("1", "b")]; + let next = ApiPagedResponse::next(items, Some("b")).page.next; assert_eq!(next, Some("b".into())); } @@ -131,20 +130,18 @@ mod tests { Item::new("1", "b"), Item::new("2", "c"), ]; - - let next = ApiPagedResponse::of(items, 3, |item| item.clone().sort).page.next; + + let next = ApiPagedResponse::of(items, 3, |item| item.clone().sort) + .page + .next; assert_eq!(next, Some("c".into())) } - + #[test] fn should_not_create_with_limit_3_while_size_2() { - let items: Vec = vec![ - Item::new("0", "a"), - Item::new("1", "b"), - ]; - + let items: Vec = vec![Item::new("0", "a"), Item::new("1", "b")]; + let page = ApiPagedResponse::of(items, 3, |item| item.clone().sort).page; - assert_eq!(page, ApiPage{next: None}) + assert_eq!(page, ApiPage { next: None }) } - } diff --git a/lib/ain-ocean/src/data_acces/masternode.rs b/lib/ain-ocean/src/data_acces/masternode.rs index 06ed84847d..918a51ff0c 100644 --- a/lib/ain-ocean/src/data_acces/masternode.rs +++ b/lib/ain-ocean/src/data_acces/masternode.rs @@ -3,6 +3,7 @@ use crate::database::db_manger::RocksDB; use crate::model::masternode::Masternode; use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; +use serde_json; pub struct MasterNodeDB { pub db: RocksDB, diff --git a/lib/ain-ocean/src/database/db_test.rs b/lib/ain-ocean/src/database/db_test.rs index dd2c8caf9a..8131a626ed 100644 --- a/lib/ain-ocean/src/database/db_test.rs +++ b/lib/ain-ocean/src/database/db_test.rs @@ -1,180 +1,180 @@ -// In your src/lib.rs or a dedicated module for RocksDB interaction - -extern crate rocksdb; -extern crate tempdir; -use crate::database::db_manger::RocksDB; -use bitcoin::blockdata::block::Header; -use bitcoin::blockdata::block::Version; -use bitcoin::blockdata::script::ScriptBuf; -use bitcoin::blockdata::transaction::OutPoint; -use bitcoin::blockdata::transaction::Transaction; -use bitcoin::blockdata::transaction::TxIn; -use bitcoin::blockdata::transaction::TxOut; -use bitcoin::blockdata::witness::Witness; -use bitcoin::consensus::deserialize; -use bitcoin::hash_types::TxMerkleNode; -use bitcoin::hash_types::Txid; -use bitcoin::pow::CompactTarget; -use bitcoin::Block; -use bitcoin::BlockHash; -use bitcoin_hashes::sha256d; -use bitcoin_hashes::Hash; -use hex; -use rocksdb::{Options, DB}; - -// Function to initialize a RocksDB instance -pub fn init_db(path: &str) -> DB { - let mut opts = Options::default(); - opts.create_if_missing(true); - DB::open(&opts, path).expect("failed to open database") -} - -pub fn create_mock_header() -> Header { - // Convert hex string to a byte array - - let hash: BlockHash = BlockHash::from_slice(&[0u8; 32]).unwrap(); - let merkle_root_bytes = - hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") - .expect("Invalid hex string for merkle root"); - - // Convert the byte array to a Hash type - let merkle_root_hash = - sha256d::Hash::from_slice(&merkle_root_bytes).expect("Invalid bytes for merkle root hash"); - - Header { - version: Version::from_consensus(1527281788), - prev_blockhash: hash, // Mock previous block hash - merkle_root: TxMerkleNode::from_raw_hash(merkle_root_hash), - time: 2236946890, - bits: CompactTarget::from_consensus(3633759788), - nonce: 491612868, - } -} - -pub fn create_mock_block() -> Block { - // Convert hex string to a byte array - - let hash: BlockHash = BlockHash::from_slice(&[0u8; 32]).unwrap(); - let merkle_root_bytes = - hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") - .expect("Invalid hex string for merkle root"); - - // Convert the byte array to a Hash type - let merkle_root_hash = - sha256d::Hash::from_slice(&merkle_root_bytes).expect("Invalid bytes for merkle root hash"); - - let tx_bytes = hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") - .expect("Invalid hex string for merkle root"); - let tx_id = sha256d::Hash::from_slice(&tx_bytes).expect("Invalid bytes for merkle root hash"); - - let header = Header { - version: Version::from_consensus(1527281788), - prev_blockhash: hash, // Mock previous block hash - merkle_root: TxMerkleNode::from_raw_hash(merkle_root_hash), - time: 2236946890, - bits: CompactTarget::from_consensus(3633759788), - nonce: 491612868, - }; - - let input = TxIn { - previous_output: OutPoint { - txid: Txid::from_raw_hash(tx_id), - - vout: 4294967295, - }, - script_sig: ScriptBuf::new(), - sequence: bitcoin::Sequence(4294967295), - witness: Witness::default(), - }; - - let output1 = TxOut { - value: 5000000000, - script_pubkey: ScriptBuf::new(), - }; - - let output2 = TxOut { - value: 0, - script_pubkey: ScriptBuf::new(), - }; - - // Construct the transaction - let transaction = Transaction { - version: 2, - lock_time: bitcoin::absolute::LockTime::from_height(0).unwrap(), - input: vec![input], - output: vec![output1, output2], - }; - - Block { - header, - txdata: vec![transaction], - } -} - -#[cfg(test)] -mod tests { - use super::*; - use tempdir::TempDir; - - #[test] - fn test_db_headers_store_retrieve() { - let temp_dir = TempDir::new("rocksdb_test").unwrap(); - let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); - // Create a mock header - let header = create_mock_header(); - rocks_db.put_block_header(&header).unwrap(); - let bh = header.block_hash().to_string(); - - let retrieved_data = rocks_db.get_block_header(bh.as_bytes()).unwrap(); - - assert!( - retrieved_data.is_some(), - "No data was retrieved for the header" - ); - let retrieved_header: Header = deserialize(&retrieved_data.unwrap()).unwrap(); - assert_eq!( - header, retrieved_header, - "Retrieved header should match the original header" - ); - } - - #[test] - fn test_db_block_store_retrieve() { - let temp_dir = TempDir::new("rocksdb_test").unwrap(); - let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); - // Create a mock header - let new_block = create_mock_block(); - rocks_db.put_block(&new_block).unwrap(); - let bh = new_block.block_hash().to_string(); - let retrieved_data = rocks_db.get_block(bh.as_bytes()).unwrap(); - assert!( - retrieved_data.is_some(), - "No data was retrieved for the header" - ); - let retrieved_block: Block = deserialize(&retrieved_data.unwrap()).unwrap(); - assert_eq!( - new_block, retrieved_block, - "Retrieved header should match the original header" - ); - } - - #[test] - fn test_db_latest_block_hash_store_retrieve() { - let temp_dir = TempDir::new("rocksdb_test").unwrap(); - let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); - - // Create and store a test block header - let header = create_mock_header(); - rocks_db.put_block_header(&header).unwrap(); - - // Retrieve the latest block hash from the database - let stored_latest_block_hash = rocks_db.get_latest_block_hash().unwrap(); - - // Check that the retrieved hash matches the test header's block hash - assert_eq!( - Some(header.block_hash().to_string()), - stored_latest_block_hash, - "The retrieved latest block hash does not match the expected value." - ); - } -} +// // In your src/lib.rs or a dedicated module for RocksDB interaction + +// extern crate rocksdb; +// extern crate tempdir; +// use crate::database::db_manger::RocksDB; +// use bitcoin::blockdata::block::Header; +// use bitcoin::blockdata::block::Version; +// use bitcoin::blockdata::script::ScriptBuf; +// use bitcoin::blockdata::transaction::OutPoint; +// use bitcoin::blockdata::transaction::Transaction; +// use bitcoin::blockdata::transaction::TxIn; +// use bitcoin::blockdata::transaction::TxOut; +// use bitcoin::blockdata::witness::Witness; +// use bitcoin::consensus::deserialize; +// use bitcoin::hash_types::TxMerkleNode; +// use bitcoin::hash_types::Txid; +// use bitcoin::pow::CompactTarget; +// use bitcoin::Block; +// use bitcoin::BlockHash; +// use bitcoin_hashes::sha256d; +// use bitcoin_hashes::Hash; +// use hex; +// use rocksdb::{Options, DB}; + +// // Function to initialize a RocksDB instance +// pub fn init_db(path: &str) -> DB { +// let mut opts = Options::default(); +// opts.create_if_missing(true); +// DB::open(&opts, path).expect("failed to open database") +// } + +// pub fn create_mock_header() -> Header { +// // Convert hex string to a byte array + +// let hash: BlockHash = BlockHash::from_slice(&[0u8; 32]).unwrap(); +// let merkle_root_bytes = +// hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") +// .expect("Invalid hex string for merkle root"); + +// // Convert the byte array to a Hash type +// let merkle_root_hash = +// sha256d::Hash::from_slice(&merkle_root_bytes).expect("Invalid bytes for merkle root hash"); + +// Header { +// version: Version::from_consensus(1527281788), +// prev_blockhash: hash, // Mock previous block hash +// merkle_root: TxMerkleNode::from_raw_hash(merkle_root_hash), +// time: 2236946890, +// bits: CompactTarget::from_consensus(3633759788), +// nonce: 491612868, +// } +// } + +// pub fn create_mock_block() -> Block { +// // Convert hex string to a byte array + +// let hash: BlockHash = BlockHash::from_slice(&[0u8; 32]).unwrap(); +// let merkle_root_bytes = +// hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") +// .expect("Invalid hex string for merkle root"); + +// // Convert the byte array to a Hash type +// let merkle_root_hash = +// sha256d::Hash::from_slice(&merkle_root_bytes).expect("Invalid bytes for merkle root hash"); + +// let tx_bytes = hex::decode("c9a4892e8ab7704e5078797f74c4d684ff493e22750a528cec21bf30ef73a3b7") +// .expect("Invalid hex string for merkle root"); +// let tx_id = sha256d::Hash::from_slice(&tx_bytes).expect("Invalid bytes for merkle root hash"); + +// let header = Header { +// version: Version::from_consensus(1527281788), +// prev_blockhash: hash, // Mock previous block hash +// merkle_root: TxMerkleNode::from_raw_hash(merkle_root_hash), +// time: 2236946890, +// bits: CompactTarget::from_consensus(3633759788), +// nonce: 491612868, +// }; + +// let input = TxIn { +// previous_output: OutPoint { +// txid: Txid::from_raw_hash(tx_id), + +// vout: 4294967295, +// }, +// script_sig: ScriptBuf::new(), +// sequence: bitcoin::Sequence(4294967295), +// witness: Witness::default(), +// }; + +// let output1 = TxOut { +// value: 5000000000, +// script_pubkey: ScriptBuf::new(), +// }; + +// let output2 = TxOut { +// value: 0, +// script_pubkey: ScriptBuf::new(), +// }; + +// // Construct the transaction +// let transaction = Transaction { +// version: 2, +// lock_time: bitcoin::absolute::LockTime::from_height(0).unwrap(), +// input: vec![input], +// output: vec![output1, output2], +// }; + +// Block { +// header, +// txdata: vec![transaction], +// } +// } + +// #[cfg(test)] +// mod tests { +// use super::*; +// use tempdir::TempDir; + +// #[test] +// fn test_db_headers_store_retrieve() { +// let temp_dir = TempDir::new("rocksdb_test").unwrap(); +// let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); +// // Create a mock header +// let header = create_mock_header(); +// rocks_db.put_block_header(&header).unwrap(); +// let bh = header.block_hash().to_string(); + +// let retrieved_data = rocks_db.get_block_header(bh.as_bytes()).unwrap(); + +// assert!( +// retrieved_data.is_some(), +// "No data was retrieved for the header" +// ); +// let retrieved_header: Header = deserialize(&retrieved_data.unwrap()).unwrap(); +// assert_eq!( +// header, retrieved_header, +// "Retrieved header should match the original header" +// ); +// } + +// #[test] +// fn test_db_block_store_retrieve() { +// let temp_dir = TempDir::new("rocksdb_test").unwrap(); +// let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); +// // Create a mock header +// let new_block = create_mock_block(); +// rocks_db.put_block(&new_block).unwrap(); +// let bh = new_block.block_hash().to_string(); +// let retrieved_data = rocks_db.get_block(bh.as_bytes()).unwrap(); +// assert!( +// retrieved_data.is_some(), +// "No data was retrieved for the header" +// ); +// let retrieved_block: Block = deserialize(&retrieved_data.unwrap()).unwrap(); +// assert_eq!( +// new_block, retrieved_block, +// "Retrieved header should match the original header" +// ); +// } + +// #[test] +// fn test_db_latest_block_hash_store_retrieve() { +// let temp_dir = TempDir::new("rocksdb_test").unwrap(); +// let rocks_db = RocksDB::new(temp_dir.path().to_str().unwrap()).unwrap(); + +// // Create and store a test block header +// let header = create_mock_header(); +// rocks_db.put_block_header(&header).unwrap(); + +// // Retrieve the latest block hash from the database +// let stored_latest_block_hash = rocks_db.get_latest_block_hash().unwrap(); + +// // Check that the retrieved hash matches the test header's block hash +// assert_eq!( +// Some(header.block_hash().to_string()), +// stored_latest_block_hash, +// "The retrieved latest block hash does not match the expected value." +// ); +// } +// } diff --git a/lib/ain-ocean/src/error.rs b/lib/ain-ocean/src/error.rs index 7064679280..1a037199cc 100644 --- a/lib/ain-ocean/src/error.rs +++ b/lib/ain-ocean/src/error.rs @@ -1,11 +1,13 @@ -use axum::{http::StatusCode, response::{IntoResponse, Response}}; +use axum::{ + http::StatusCode, + response::{IntoResponse, Response}, +}; use thiserror::Error; pub type OceanResult = Result; #[derive(Error, Debug)] -pub enum OceanError { -} +pub enum OceanError {} impl IntoResponse for OceanError { fn into_response(self) -> Response { diff --git a/lib/ain-ocean/src/lib.rs b/lib/ain-ocean/src/lib.rs index 5694e4c8f9..547943eaaa 100644 --- a/lib/ain-ocean/src/lib.rs +++ b/lib/ain-ocean/src/lib.rs @@ -1,8 +1,6 @@ -mod api; pub mod api_paged_response; pub mod error; mod indexer; -mod model; pub use api::ocean_router; pub use indexer::{index_block, invalidate_block}; @@ -10,5 +8,3 @@ pub mod api; mod data_acces; pub mod database; mod model; - -pub use api::ocean_router; diff --git a/lib/ain-ocean/src/model/masternode.rs b/lib/ain-ocean/src/model/masternode.rs index d076e37d9b..27f1608dca 100644 --- a/lib/ain-ocean/src/model/masternode.rs +++ b/lib/ain-ocean/src/model/masternode.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -#[derive(Debug)] +#[derive(Debug, Serialize, Deserialize)] pub struct Masternode { pub id: String, pub sort: Option, diff --git a/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs b/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs index b6834f49c4..867e0030d3 100644 --- a/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs +++ b/lib/ain-ocean/src/model/oracle_price_aggregated_interval.rs @@ -12,7 +12,6 @@ pub struct OraclePriceAggregatedInterval { pub block: OraclePriceAggregatedIntervalBlock, } -#[derive(Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedIntervalAggregated { @@ -22,7 +21,6 @@ pub struct OraclePriceAggregatedIntervalAggregated { pub oracles: OraclePriceAggregatedIntervalAggregatedOracles, } -#[derive(Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedIntervalBlock { @@ -32,7 +30,6 @@ pub struct OraclePriceAggregatedIntervalBlock { pub median_time: i32, } -#[derive(Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)] #[serde(rename_all = "camelCase")] pub struct OraclePriceAggregatedIntervalAggregatedOracles { diff --git a/lib/ain-ocean/src/model/price_ticker.rs b/lib/ain-ocean/src/model/price_ticker.rs index e7790e56cc..509350976e 100644 --- a/lib/ain-ocean/src/model/price_ticker.rs +++ b/lib/ain-ocean/src/model/price_ticker.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use super::oracle_price_aggregated::OraclePriceAggregated; -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct PriceTicker { pub id: String, pub sort: String, diff --git a/lib/ain-ocean/src/model/script_activity.rs b/lib/ain-ocean/src/model/script_activity.rs index 3edb9f041f..bed279a82c 100644 --- a/lib/ain-ocean/src/model/script_activity.rs +++ b/lib/ain-ocean/src/model/script_activity.rs @@ -1,10 +1,4 @@ -#[derive(Debug, Default, PartialEq, Eq)] -pub enum ScriptActivityType { - #[default] - Vin, - Vout, -} - +use serde::{Deserialize, Serialize}; #[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub enum ScriptActivityType { #[default] @@ -21,7 +15,6 @@ impl ScriptActivityType { } } -#[derive(Debug, Default, PartialEq, Eq)] #[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub enum ScriptActivityTypeHex { #[default] @@ -38,9 +31,8 @@ impl ScriptActivityTypeHex { } } -#[derive(Debug, Default)] -#[derive(Serialize, Deserialize, Debug, Default)] -#[serde(rename_all = "camelCase")] +#[derive(Debug, Default, Serialize, Deserialize)] + pub struct ScriptActivity { pub id: String, pub hid: String, @@ -55,7 +47,7 @@ pub struct ScriptActivity { pub token_id: i32, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptActivityBlock { pub hash: String, pub height: i32, @@ -63,19 +55,19 @@ pub struct ScriptActivityBlock { pub median_time: i32, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptActivityScript { pub r#type: String, pub hex: String, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptActivityVin { pub txid: String, pub n: i32, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptActivityVout { pub txid: String, pub n: i32, diff --git a/lib/ain-ocean/src/model/script_aggregation.rs b/lib/ain-ocean/src/model/script_aggregation.rs index e0a6b838b1..1345920cc5 100644 --- a/lib/ain-ocean/src/model/script_aggregation.rs +++ b/lib/ain-ocean/src/model/script_aggregation.rs @@ -1,4 +1,5 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptAggregation { pub id: String, pub hid: String, @@ -8,7 +9,7 @@ pub struct ScriptAggregation { pub amount: ScriptAggregationAmount, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptAggregationBlock { pub hash: String, pub height: i32, @@ -16,20 +17,20 @@ pub struct ScriptAggregationBlock { pub median_time: i32, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptAggregationScript { pub r#type: String, pub hex: String, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptAggregationStatistic { pub tx_count: i32, pub tx_in_count: i32, pub tx_out_count: i32, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptAggregationAmount { pub tx_in: String, pub tx_out: String, diff --git a/lib/ain-ocean/src/model/script_unspent.rs b/lib/ain-ocean/src/model/script_unspent.rs index eecbfbdecf..1c7787247b 100644 --- a/lib/ain-ocean/src/model/script_unspent.rs +++ b/lib/ain-ocean/src/model/script_unspent.rs @@ -1,4 +1,5 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptUnspent { pub id: String, pub hid: String, @@ -8,7 +9,7 @@ pub struct ScriptUnspent { pub vout: ScriptUnspentVout, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptUnspentBlock { pub hash: String, pub height: i32, @@ -16,13 +17,13 @@ pub struct ScriptUnspentBlock { pub median_time: i32, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptUnspentScript { pub r#type: String, pub hex: String, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct ScriptUnspentVout { pub txid: String, pub n: i32, diff --git a/lib/ain-ocean/src/model/transaction_vin.rs b/lib/ain-ocean/src/model/transaction_vin.rs index 14081d06ff..f3317ec5ca 100644 --- a/lib/ain-ocean/src/model/transaction_vin.rs +++ b/lib/ain-ocean/src/model/transaction_vin.rs @@ -1,4 +1,5 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; +#[derive(Debug, Default, Serialize, Deserialize)] pub struct TransactionVin { pub id: String, pub txid: String, @@ -9,7 +10,7 @@ pub struct TransactionVin { pub sequence: String, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct TransactionVinVout { pub id: String, pub txid: String, @@ -19,12 +20,12 @@ pub struct TransactionVinVout { pub script: TransactionVinVoutScript, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct TransactionVinScript { pub hex: String, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct TransactionVinVoutScript { pub hex: String, } diff --git a/lib/ain-ocean/src/model/transaction_vout.rs b/lib/ain-ocean/src/model/transaction_vout.rs index 1ba65ebf98..6e91cf07ab 100644 --- a/lib/ain-ocean/src/model/transaction_vout.rs +++ b/lib/ain-ocean/src/model/transaction_vout.rs @@ -1,4 +1,5 @@ -#[derive(Debug, Default)] +use serde::{Deserialize, Serialize}; +#[derive(Debug, Default, Serialize, Deserialize)] pub struct TransactionVout { pub id: String, pub txid: String, @@ -8,7 +9,7 @@ pub struct TransactionVout { pub script: TransactionVoutScript, } -#[derive(Debug, Default)] +#[derive(Debug, Default, Serialize, Deserialize)] pub struct TransactionVoutScript { pub hex: String, pub r#type: String, From a2e8868cb9070029d1538d17af83c7ab499057a9 Mon Sep 17 00:00:00 2001 From: nagarajm22 Date: Fri, 8 Dec 2023 17:46:32 +0800 Subject: [PATCH 37/37] rebased ocean-archive --- src/dfi/validation.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dfi/validation.cpp b/src/dfi/validation.cpp index bdf2543634..5416154d2f 100644 --- a/src/dfi/validation.cpp +++ b/src/dfi/validation.cpp @@ -2857,7 +2857,6 @@ void ProcessDeFiEvent(const CBlock &block, // construct undo FlushCacheCreateUndo(pindex, mnview, cache, uint256()); - // Ocean archive const auto oceanArchive{true}; if (oceanArchive) {