From 2566f72c2da9c01f6da3a97ee06ae404555b6d66 Mon Sep 17 00:00:00 2001 From: Neotamandua <107320179+Neotamandua@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:05:50 +0300 Subject: [PATCH 1/2] rusk: add archive field to RuskNode - Set archive field in RuskNode during build --- rusk/src/lib/builder/node.rs | 11 +++++++++-- rusk/src/lib/node.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/rusk/src/lib/builder/node.rs b/rusk/src/lib/builder/node.rs index adbc38e91d..c2d1d23e8f 100644 --- a/rusk/src/lib/builder/node.rs +++ b/rusk/src/lib/builder/node.rs @@ -182,13 +182,20 @@ impl RuskNodeBuilder { .map_err(|e| anyhow::anyhow!("Cannot instantiate VM {e}"))?; info!("Rusk VM loaded"); + #[cfg(feature = "archive")] + let archive = Archive::create_or_open(self.db_path.clone()).await; + let node = { let db = rocksdb::Backend::create_or_open( self.db_path.clone(), self.db_options.clone(), ); let net = Kadcast::new(self.kadcast.clone())?; - RuskNode::new(Node::new(net, db, rusk.clone())) + RuskNode::new( + Node::new(net, db, rusk.clone()), + #[cfg(feature = "archive")] + archive.clone(), + ) }; let mut chain_srv = ChainSrv::new( @@ -254,7 +261,7 @@ impl RuskNodeBuilder { #[cfg(feature = "archive")] service_list.push(Box::new(ArchivistSrv { archive_receiver, - archivist: Archive::create_or_open(self.db_path.clone()).await, + archivist: archive, })); node.inner().initialize(&mut service_list).await?; diff --git a/rusk/src/lib/node.rs b/rusk/src/lib/node.rs index 6221b64b0f..9e224b40ee 100644 --- a/rusk/src/lib/node.rs +++ b/rusk/src/lib/node.rs @@ -24,7 +24,9 @@ use tokio::sync::broadcast; use crate::http::RuesEvent; pub(crate) use events::ChainEventStreamer; #[cfg(feature = "archive")] -use {node_data::archive::ArchivalData, tokio::sync::mpsc}; +use { + node::archive::Archive, node_data::archive::ArchivalData, tokio::sync::mpsc, +}; #[derive(Debug, Clone, Copy)] pub struct RuskTip { @@ -54,11 +56,26 @@ pub(crate) type Services = #[derive(Clone)] pub struct RuskNode { inner: node::Node, Backend, Rusk>, + #[cfg(feature = "archive")] + archive: Archive, } impl RuskNode { - pub fn new(inner: node::Node, Backend, Rusk>) -> Self { - Self { inner } + pub fn new( + inner: node::Node, Backend, Rusk>, + #[cfg(feature = "archive")] archive: Archive, + ) -> Self { + Self { + inner, + #[cfg(feature = "archive")] + archive, + } + } + + #[cfg(feature = "archive")] + pub fn with_archive(mut self, archive: Archive) -> Self { + self.archive = archive; + self } } @@ -67,6 +84,11 @@ impl RuskNode { self.inner.database() as Arc> } + #[cfg(feature = "archive")] + pub fn archive(&self) -> Archive { + self.archive.clone() + } + pub fn network(&self) -> Arc>> { self.inner.network() as Arc>> } From 32aca6cb91c49f888b9e0f9e155f6cbe6397ae95 Mon Sep 17 00:00:00 2001 From: Neotamandua <107320179+Neotamandua@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:10:54 +0300 Subject: [PATCH 2/2] rusk: add Archive to DBContext & Schema --- rusk/src/lib/http/chain.rs | 7 ++++++- rusk/src/lib/http/chain/graphql.rs | 8 +++++++- rusk/src/lib/http/chain/graphql/block.rs | 10 +++++----- rusk/src/lib/http/chain/graphql/data.rs | 6 +++--- rusk/src/lib/http/chain/graphql/tx.rs | 8 ++++---- 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/rusk/src/lib/http/chain.rs b/rusk/src/lib/http/chain.rs index c369685bb4..cc2da7e907 100644 --- a/rusk/src/lib/http/chain.rs +++ b/rusk/src/lib/http/chain.rs @@ -143,8 +143,13 @@ impl RuskNode { ) -> anyhow::Result { let gql_query = data.as_string(); + #[cfg(feature = "archive")] let schema = Schema::build(Query, EmptyMutation, EmptySubscription) - .data(self.db()) + .data((self.db(), self.archive())) + .finish(); + #[cfg(not(feature = "archive"))] + let schema = Schema::build(Query, EmptyMutation, EmptySubscription) + .data((self.db(), ())) .finish(); if gql_query.trim().is_empty() { diff --git a/rusk/src/lib/http/chain/graphql.rs b/rusk/src/lib/http/chain/graphql.rs index ed664150de..92b69b8711 100644 --- a/rusk/src/lib/http/chain/graphql.rs +++ b/rusk/src/lib/http/chain/graphql.rs @@ -14,13 +14,19 @@ use tx::*; use async_graphql::{Context, FieldError, FieldResult, Object}; use execution_core::{transfer::TRANSFER_CONTRACT, ContractId}; +#[cfg(feature = "archive")] +use node::archive::{Archive, MoonlightTxEvents}; use node::database::rocksdb::Backend; use node::database::{Ledger, DB}; use std::sync::Arc; use tokio::sync::RwLock; -pub type DBContext = Arc>; +#[cfg(feature = "archive")] +pub type DBContext = (Arc>, Archive); +#[cfg(not(feature = "archive"))] +pub type DBContext = (Arc>, ()); + pub type OptResult = FieldResult>; pub struct Query; diff --git a/rusk/src/lib/http/chain/graphql/block.rs b/rusk/src/lib/http/chain/graphql/block.rs index cdbcab98ba..52c7681b3e 100644 --- a/rusk/src/lib/http/chain/graphql/block.rs +++ b/rusk/src/lib/http/chain/graphql/block.rs @@ -12,7 +12,7 @@ pub async fn block_by_height( ctx: &Context<'_>, height: f64, ) -> OptResult { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let block_hash = db.read().await.view(|t| { if height >= 0f64 { t.fetch_block_hash_by_height(height as u64) @@ -29,7 +29,7 @@ pub async fn block_by_height( } pub async fn last_block(ctx: &Context<'_>) -> FieldResult { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let block = db.read().await.view(|t| { let hash = t.op_read(MD_HASH_KEY)?; match hash { @@ -47,7 +47,7 @@ pub async fn block_by_hash( ctx: &Context<'_>, hash: String, ) -> OptResult { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let hash = hex::decode(hash)?; let header = db.read().await.view(|t| t.fetch_light_block(&hash))?; Ok(header.map(Block::from)) @@ -60,7 +60,7 @@ pub async fn last_blocks( if (count < 1) { return Err(FieldError::new("count must be positive")); } - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let last_block = last_block(ctx).await?; let mut hash_to_search = last_block.header().prev_block_hash; let blocks = db.read().await.view(|t| { @@ -86,7 +86,7 @@ pub async fn blocks_range( from: u64, to: u64, ) -> FieldResult> { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let mut blocks = db.read().await.view(|t| { let mut blocks = vec![]; let mut hash_to_search = None; diff --git a/rusk/src/lib/http/chain/graphql/data.rs b/rusk/src/lib/http/chain/graphql/data.rs index 9c10ad0391..1e99abc454 100644 --- a/rusk/src/lib/http/chain/graphql/data.rs +++ b/rusk/src/lib/http/chain/graphql/data.rs @@ -72,7 +72,7 @@ impl Block { &self, ctx: &async_graphql::Context<'_>, ) -> FieldResult> { - let db = ctx.data::()?.read().await; + let db = ctx.data::()?.0.read().await; let mut ret = vec![]; db.view(|t| { @@ -189,7 +189,7 @@ impl SpentTransaction { &self, ctx: &async_graphql::Context<'_>, ) -> FieldResult { - let db = ctx.data::()?.read().await; + let db = ctx.data::()?.0.read().await; let block_height = self.0.block_height; let block_hash = db.view(|t| { @@ -209,7 +209,7 @@ impl SpentTransaction { &self, ctx: &async_graphql::Context<'_>, ) -> FieldResult { - let db = ctx.data::()?.read().await; + let db = ctx.data::()?.0.read().await; let block_height = self.0.block_height; let header = db.view(|t| { diff --git a/rusk/src/lib/http/chain/graphql/tx.rs b/rusk/src/lib/http/chain/graphql/tx.rs index 98b5dc69ac..d7f0eefee8 100644 --- a/rusk/src/lib/http/chain/graphql/tx.rs +++ b/rusk/src/lib/http/chain/graphql/tx.rs @@ -13,7 +13,7 @@ pub async fn tx_by_hash( ctx: &Context<'_>, hash: String, ) -> OptResult { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let hash = hex::decode(hash)?; let tx = db.read().await.view(|t| t.get_ledger_tx_by_hash(&hash))?; Ok(tx.map(SpentTransaction)) @@ -27,7 +27,7 @@ pub async fn last_transactions( return Err(FieldError::new("count must be positive")); } - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let transactions = db.read().await.view(|t| { let mut txs = vec![]; let mut current_block = @@ -59,7 +59,7 @@ pub async fn last_transactions( pub async fn mempool<'a>( ctx: &Context<'_>, ) -> FieldResult>> { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let transactions = db.read().await.view(|t| { let txs = t.get_txs_sorted_by_fee()?.map(|t| t.into()).collect(); Ok::<_, async_graphql::Error>(txs) @@ -71,7 +71,7 @@ pub async fn mempool_by_hash<'a>( ctx: &Context<'_>, hash: String, ) -> OptResult> { - let db = ctx.data::()?; + let (db, _) = ctx.data::()?; let hash = &hex::decode(hash)?[..]; let hash = hash.try_into()?; let tx = db.read().await.view(|t| t.get_tx(hash))?;