Skip to content

Commit

Permalink
Merge pull request #2564 from dusk-network/archive_api
Browse files Browse the repository at this point in the history
Make archive available in graphql
  • Loading branch information
Neotamandua authored Oct 2, 2024
2 parents c5a1253 + 32aca6c commit 921cfae
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
11 changes: 9 additions & 2 deletions rusk/src/lib/builder/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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?;
Expand Down
7 changes: 6 additions & 1 deletion rusk/src/lib/http/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,13 @@ impl RuskNode {
) -> anyhow::Result<ResponseData> {
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() {
Expand Down
8 changes: 7 additions & 1 deletion rusk/src/lib/http/chain/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RwLock<Backend>>;
#[cfg(feature = "archive")]
pub type DBContext = (Arc<RwLock<Backend>>, Archive);
#[cfg(not(feature = "archive"))]
pub type DBContext = (Arc<RwLock<Backend>>, ());

pub type OptResult<T> = FieldResult<Option<T>>;

pub struct Query;
Expand Down
10 changes: 5 additions & 5 deletions rusk/src/lib/http/chain/graphql/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn block_by_height(
ctx: &Context<'_>,
height: f64,
) -> OptResult<Block> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let block_hash = db.read().await.view(|t| {
if height >= 0f64 {
t.fetch_block_hash_by_height(height as u64)
Expand All @@ -29,7 +29,7 @@ pub async fn block_by_height(
}

pub async fn last_block(ctx: &Context<'_>) -> FieldResult<Block> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let block = db.read().await.view(|t| {
let hash = t.op_read(MD_HASH_KEY)?;
match hash {
Expand All @@ -47,7 +47,7 @@ pub async fn block_by_hash(
ctx: &Context<'_>,
hash: String,
) -> OptResult<Block> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let hash = hex::decode(hash)?;
let header = db.read().await.view(|t| t.fetch_light_block(&hash))?;
Ok(header.map(Block::from))
Expand All @@ -60,7 +60,7 @@ pub async fn last_blocks(
if (count < 1) {
return Err(FieldError::new("count must be positive"));
}
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
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| {
Expand All @@ -86,7 +86,7 @@ pub async fn blocks_range(
from: u64,
to: u64,
) -> FieldResult<Vec<Block>> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let mut blocks = db.read().await.view(|t| {
let mut blocks = vec![];
let mut hash_to_search = None;
Expand Down
6 changes: 3 additions & 3 deletions rusk/src/lib/http/chain/graphql/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Block {
&self,
ctx: &async_graphql::Context<'_>,
) -> FieldResult<Vec<SpentTransaction>> {
let db = ctx.data::<super::DBContext>()?.read().await;
let db = ctx.data::<super::DBContext>()?.0.read().await;
let mut ret = vec![];

db.view(|t| {
Expand Down Expand Up @@ -189,7 +189,7 @@ impl SpentTransaction {
&self,
ctx: &async_graphql::Context<'_>,
) -> FieldResult<String> {
let db = ctx.data::<super::DBContext>()?.read().await;
let db = ctx.data::<super::DBContext>()?.0.read().await;
let block_height = self.0.block_height;

let block_hash = db.view(|t| {
Expand All @@ -209,7 +209,7 @@ impl SpentTransaction {
&self,
ctx: &async_graphql::Context<'_>,
) -> FieldResult<u64> {
let db = ctx.data::<super::DBContext>()?.read().await;
let db = ctx.data::<super::DBContext>()?.0.read().await;
let block_height = self.0.block_height;

let header = db.view(|t| {
Expand Down
8 changes: 4 additions & 4 deletions rusk/src/lib/http/chain/graphql/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub async fn tx_by_hash(
ctx: &Context<'_>,
hash: String,
) -> OptResult<SpentTransaction> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let hash = hex::decode(hash)?;
let tx = db.read().await.view(|t| t.get_ledger_tx_by_hash(&hash))?;
Ok(tx.map(SpentTransaction))
Expand All @@ -27,7 +27,7 @@ pub async fn last_transactions(
return Err(FieldError::new("count must be positive"));
}

let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let transactions = db.read().await.view(|t| {
let mut txs = vec![];
let mut current_block =
Expand Down Expand Up @@ -59,7 +59,7 @@ pub async fn last_transactions(
pub async fn mempool<'a>(
ctx: &Context<'_>,
) -> FieldResult<Vec<Transaction<'a>>> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
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)
Expand All @@ -71,7 +71,7 @@ pub async fn mempool_by_hash<'a>(
ctx: &Context<'_>,
hash: String,
) -> OptResult<Transaction<'a>> {
let db = ctx.data::<DBContext>()?;
let (db, _) = ctx.data::<DBContext>()?;
let hash = &hex::decode(hash)?[..];
let hash = hash.try_into()?;
let tx = db.read().await.view(|t| t.get_tx(hash))?;
Expand Down
28 changes: 25 additions & 3 deletions rusk/src/lib/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,11 +56,26 @@ pub(crate) type Services =
#[derive(Clone)]
pub struct RuskNode {
inner: node::Node<Kadcast<255>, Backend, Rusk>,
#[cfg(feature = "archive")]
archive: Archive,
}

impl RuskNode {
pub fn new(inner: node::Node<Kadcast<255>, Backend, Rusk>) -> Self {
Self { inner }
pub fn new(
inner: node::Node<Kadcast<255>, 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
}
}

Expand All @@ -67,6 +84,11 @@ impl RuskNode {
self.inner.database() as Arc<tokio::sync::RwLock<Backend>>
}

#[cfg(feature = "archive")]
pub fn archive(&self) -> Archive {
self.archive.clone()
}

pub fn network(&self) -> Arc<tokio::sync::RwLock<Kadcast<255>>> {
self.inner.network() as Arc<tokio::sync::RwLock<Kadcast<255>>>
}
Expand Down

0 comments on commit 921cfae

Please sign in to comment.