Skip to content

Commit

Permalink
feat(rpc): get state root
Browse files Browse the repository at this point in the history
  • Loading branch information
snormore committed Aug 28, 2024
1 parent da376d2 commit d3df76d
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6941,6 +6941,7 @@ dependencies = [
"lightning-schema",
"lightning-types",
"lightning-workspace-hack",
"merklize",
"schemars",
"serde",
"serde-big-array",
Expand Down Expand Up @@ -7302,6 +7303,7 @@ dependencies = [
"lightning-types",
"lightning-utils",
"lightning-workspace-hack",
"merklize",
"once_cell",
"rand 0.8.5",
"reqwest",
Expand Down Expand Up @@ -7605,7 +7607,6 @@ dependencies = [
"blake3",
"block-buffer 0.9.0",
"block-padding 0.3.3",
"borsh",
"bytemuck",
"byteorder",
"bytes",
Expand Down
8 changes: 8 additions & 0 deletions core/application/src/state/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeSet;
use std::path::Path;
use std::time::Duration;

use anyhow::Result;
use atomo::{
Atomo,
AtomoBuilder,
Expand Down Expand Up @@ -35,7 +36,9 @@ use lightning_interfaces::types::{
Value,
};
use lightning_interfaces::SyncQueryRunnerInterface;
use merklize::{StateRootHash, StateTree};

use crate::env::ApplicationStateTree;
use crate::state::ApplicationState;
use crate::storage::{AtomoStorage, AtomoStorageBuilder};

Expand Down Expand Up @@ -260,4 +263,9 @@ impl SyncQueryRunnerInterface for QueryRunner {
self.inner
.run(|ctx| self.node_to_uri.get(ctx).get(node_index))
}

/// Returns the state tree root hash from the application state.
fn get_state_root(&self) -> Result<StateRootHash> {
self.run(|ctx| ApplicationStateTree::get_state_root(ctx))
}
}
1 change: 1 addition & 0 deletions core/interfaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ hp-fixed.workspace = true
ink-quill.workspace = true
blake3-tree = { path = "../../lib/blake3-tree" }
better-shutdown.workspace = true
merklize.workspace = true

# Currently we need the SDK because of the FFI types.
fn-sdk = { path = "../../lib/sdk" }
Expand Down
4 changes: 4 additions & 0 deletions core/interfaces/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use lightning_types::{
TxHash,
Value,
};
use merklize::StateRootHash;
use serde::{Deserialize, Serialize};

use crate::collection::Collection;
Expand Down Expand Up @@ -187,6 +188,9 @@ pub trait SyncQueryRunnerInterface: Clone + Send + Sync + 'static {

/// Returns the node's content registry.
fn get_content_registry(&self, node_index: &NodeIndex) -> Option<BTreeSet<Blake3Hash>>;

/// Returns the state root hash from the application state.
fn get_state_root(&self) -> Result<StateRootHash>;
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions core/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ lightning-interfaces = { path = "../interfaces" }
lightning-openrpc = { path = "../rpc-openrpc" }
lightning-openrpc-macros = { path = "../rpc-openrpc-macros" }
lightning-utils = { path = "../utils" }
merklize.workspace = true
alloy-primitives = "0.5.2"
resolved-pathbuf = { path = "../../lib/resolved-pathbuf" }

Expand Down
4 changes: 4 additions & 0 deletions core/rpc/src/api/flk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use lightning_interfaces::types::{
};
use lightning_interfaces::PagingParams;
use lightning_openrpc_macros::open_rpc;
use merklize::StateRootHash;

#[open_rpc(namespace = "flk", tag = "1.0.0")]
#[rpc(client, server, namespace = "flk")]
Expand Down Expand Up @@ -185,6 +186,9 @@ pub trait FleekApi {
#[method(name = "get_sub_dag_index")]
async fn get_sub_dag_index(&self) -> RpcResult<(u64, Epoch)>;

#[method(name = "get_state_root")]
async fn get_state_root(&self, epoch: Option<u64>) -> RpcResult<StateRootHash>;

#[method(name = "send_txn")]
async fn send_txn(&self, tx: TransactionRequest) -> RpcResult<()>;

Expand Down
10 changes: 10 additions & 0 deletions core/rpc/src/logic/flk_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use lightning_interfaces::types::{
};
use lightning_interfaces::PagingParams;
use lightning_utils::application::QueryRunnerExt;
use merklize::StateRootHash;

use crate::api::FleekApiServer;
use crate::error::RPCError;
Expand Down Expand Up @@ -387,6 +388,15 @@ impl<C: Collection> FleekApiServer for FleekApi<C> {
Ok((sub_dag_index, self.data.query_runner.get_epoch_info().epoch))
}

async fn get_state_root(&self, epoch: Option<u64>) -> RpcResult<StateRootHash> {
Ok(self
.data
.query_runner(epoch)
.await?
.get_state_root()
.map_err(|e| RPCError::custom(e.to_string()))?)
}

async fn send_txn(&self, tx: TransactionRequest) -> RpcResult<()> {
Ok(self
.data
Expand Down
38 changes: 38 additions & 0 deletions core/rpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,41 @@ async fn test_rpc_events() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn test_rpc_get_state_root() -> Result<()> {
let temp_dir = tempdir()?;

// Create keys
let owner_secret_key = AccountOwnerSecretKey::generate();
let owner_public_key = owner_secret_key.to_pk();

// Init application service
let mut genesis = Genesis::default();
genesis.account.push(GenesisAccount {
public_key: owner_public_key.into(),
flk_balance: 1000u64.into(),
stables_balance: 0,
bandwidth_balance: 0,
});

let genesis_path = genesis
.write_to_dir(temp_dir.path().to_path_buf().try_into().unwrap())
.unwrap();

let port = 30024;
let node = init_rpc(&temp_dir, genesis_path, port).await;

wait_for_server_start(port).await?;

let client = RpcClient::new_no_auth(&format!("http://127.0.0.1:{port}/rpc/v0"))?;
let root_hash = FleekApiClient::get_state_root(&client, None)
.await?
.to_string();

assert_eq!(root_hash.len(), 64);
assert!(root_hash.chars().all(|c| c.is_ascii_hexdigit()));

node.shutdown().await;
Ok(())
}
2 changes: 0 additions & 2 deletions etc/workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ blake2 = { version = "0.10" }
blake3 = { version = "1" }
block-buffer = { version = "0.9", default-features = false, features = ["block-padding"] }
block-padding = { version = "0.3", default-features = false, features = ["std"] }
borsh = { version = "1", features = ["de_strict_order", "derive"] }
bytemuck = { version = "1", default-features = false, features = ["extern_crate_alloc"] }
byteorder = { version = "1", features = ["i128"] }
bytes = { version = "1", features = ["serde"] }
Expand Down Expand Up @@ -184,7 +183,6 @@ blake2 = { version = "0.10" }
blake3 = { version = "1" }
block-buffer = { version = "0.9", default-features = false, features = ["block-padding"] }
block-padding = { version = "0.3", default-features = false, features = ["std"] }
borsh = { version = "1", features = ["de_strict_order", "derive"] }
bytemuck = { version = "1", default-features = false, features = ["extern_crate_alloc"] }
byteorder = { version = "1", features = ["i128"] }
bytes = { version = "1", features = ["serde"] }
Expand Down

0 comments on commit d3df76d

Please sign in to comment.