From 26f6d6e01bae247d78ca25eba613a43da6ccc9dc Mon Sep 17 00:00:00 2001 From: Fernando Krell Date: Fri, 11 Oct 2024 13:40:13 -0600 Subject: [PATCH 1/5] Adding API point to get fee balance per account --- sequencer/api/merklized_state.toml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 sequencer/api/merklized_state.toml diff --git a/sequencer/api/merklized_state.toml b/sequencer/api/merklized_state.toml new file mode 100644 index 0000000000..098cfaef2b --- /dev/null +++ b/sequencer/api/merklized_state.toml @@ -0,0 +1,4 @@ +[route.getfeebalance] +PATH = ["/fee_balance/:address"] +":address" = "Literal" +DOC = "Get current balance in address account." \ No newline at end of file From 4401ce7a7d2ecdb7be79f7146b5280a084f42b4b Mon Sep 17 00:00:00 2001 From: Fernando Krell Date: Fri, 11 Oct 2024 14:01:55 -0600 Subject: [PATCH 2/5] Adding API point to get fee balance per account --- sequencer/src/api/endpoints.rs | 48 +++++++++++++++++++++++++++++----- sequencer/src/api/options.rs | 4 +-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/sequencer/src/api/endpoints.rs b/sequencer/src/api/endpoints.rs index 1e1fd0813e..6c92aa3d14 100644 --- a/sequencer/src/api/endpoints.rs +++ b/sequencer/src/api/endpoints.rs @@ -1,14 +1,10 @@ //! Sequencer-specific API endpoint handlers. -use std::{ - collections::{BTreeSet, HashMap}, - env, -}; - use anyhow::Result; use committable::Committable; -use espresso_types::{NamespaceId, NsProof, PubKey, Transaction}; +use espresso_types::{FeeMerkleTree, NamespaceId, NsProof, PubKey, Transaction}; use futures::{try_join, FutureExt}; +use hotshot_query_service::merklized_state::Snapshot; use hotshot_query_service::{ availability::{self, AvailabilityDataSource, CustomSnafu, FetchBlockSnafu}, data_source::storage::ExplorerStorage, @@ -27,6 +23,10 @@ use hotshot_types::{ }; use serde::{de::Error as _, Deserialize, Serialize}; use snafu::OptionExt; +use std::{ + collections::{BTreeSet, HashMap}, + env, +}; use tagged_base64::TaggedBase64; use tide_disco::{method::ReadState, Api, Error as _, StatusCode}; use vbs::version::StaticVersionType; @@ -46,6 +46,42 @@ pub struct NamespaceProofQueryData { pub transactions: Vec, } +pub(super) fn get_balance() -> Result> +where + State: 'static + Send + Sync + ReadState, + Ver: 'static + StaticVersionType, + ::State: Send + + Sync + + MerklizedStateDataSource + + MerklizedStateHeightPersistence, + //for<'a> < as hotshot_query_service::merklized_state::MerklizedState>::Commit as TryFrom<&'a TaggedBase64>>::Error: Display, +{ + let mut options = merklized_state::Options::default(); + let extension = toml::from_str(include_str!("../../api/merklized_state.toml"))?; + options.extensions.push(extension); + + let mut api = + merklized_state::define_api::(&options)?; + + api.get("getfeebalance", move |req, state| { + async move { + let address = req.string_param("address")?; + let height = state.get_last_state_height().await?; + let snapshot = Snapshot::Index(height as u64); + let key = address + .parse() + .map_err(|_| merklized_state::Error::Custom { + message: "failed to parse Key param".to_string(), + status: StatusCode::INTERNAL_SERVER_ERROR, + })?; + let path = state.get_path(snapshot, key).await?; + Ok(path.elem().copied()) + } + .boxed() + })?; + Ok(api) +} + pub(super) type AvailState = ApiState>; type AvailabilityApi = Api, availability::Error, ApiVer>; diff --git a/sequencer/src/api/options.rs b/sequencer/src/api/options.rs index e053b67112..287afd6657 100644 --- a/sequencer/src/api/options.rs +++ b/sequencer/src/api/options.rs @@ -5,7 +5,7 @@ use async_std::sync::Arc; use clap::Parser; use espresso_types::{ v0::traits::{EventConsumer, NullEventConsumer, SequencerPersistence}, - BlockMerkleTree, FeeMerkleTree, PubKey, + BlockMerkleTree, PubKey, }; use futures::{ channel::oneshot, @@ -356,7 +356,7 @@ impl Options { // Initialize merklized state module for fee merkle tree app.register_module( "fee-state", - endpoints::merklized_state::()?, + endpoints::get_balance::<_, SequencerApiVersion>()?, )?; let state = state.clone(); From 66c21d9da6191308e42304417c2f8cab3515bfd3 Mon Sep 17 00:00:00 2001 From: Fernando Krell Date: Wed, 16 Oct 2024 16:00:39 -0300 Subject: [PATCH 3/5] Adding basic test for fee balance --- sequencer/api/merklized_state.toml | 2 +- sequencer/src/api.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sequencer/api/merklized_state.toml b/sequencer/api/merklized_state.toml index 098cfaef2b..0d7e81f020 100644 --- a/sequencer/api/merklized_state.toml +++ b/sequencer/api/merklized_state.toml @@ -1,4 +1,4 @@ [route.getfeebalance] -PATH = ["/fee_balance/:address"] +PATH = ["fee_balance/:address"] ":address" = "Literal" DOC = "Get current balance in address account." \ No newline at end of file diff --git a/sequencer/src/api.rs b/sequencer/src/api.rs index 386daf6506..ff796f9639 100644 --- a/sequencer/src/api.rs +++ b/sequencer/src/api.rs @@ -1229,6 +1229,17 @@ mod test { assert_eq!(*path.index(), account); assert!(*path.elem().unwrap() > 0.into(), "{:?}", path.elem()); } + + // testing fee_balance api + let account = TestConfig::<5>::builder_key().fee_account(); + let amount = client + .get::>(&format!("fee-state/fee_balance/{}", account)) + .send() + .await + .unwrap() + .unwrap(); + let expected = ethers::types::U256::max_value(); + assert_eq!(expected, amount.0); } #[async_std::test] From d19a139fb2599f6147bd6d5413adc5f0891338e3 Mon Sep 17 00:00:00 2001 From: Fernando Krell Date: Fri, 25 Oct 2024 13:45:58 -0300 Subject: [PATCH 4/5] Defining FEE_MERKLE_TREE_ARITY constant, addressing minor PR comments --- sequencer/api/merklized_state.toml | 4 ++-- sequencer/src/api.rs | 2 +- sequencer/src/api/endpoints.rs | 11 ++++++----- types/src/v0/mod.rs | 4 ++-- types/src/v0/v0_1/state.rs | 3 ++- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/sequencer/api/merklized_state.toml b/sequencer/api/merklized_state.toml index 0d7e81f020..c3024e706d 100644 --- a/sequencer/api/merklized_state.toml +++ b/sequencer/api/merklized_state.toml @@ -1,4 +1,4 @@ [route.getfeebalance] -PATH = ["fee_balance/:address"] +PATH = ["fee-balance/:address"] ":address" = "Literal" -DOC = "Get current balance in address account." \ No newline at end of file +DOC = "Get current balance in fee state. Expected parameter is an Ethereum address in hex format." \ No newline at end of file diff --git a/sequencer/src/api.rs b/sequencer/src/api.rs index a32ab4dfeb..10549198b8 100644 --- a/sequencer/src/api.rs +++ b/sequencer/src/api.rs @@ -1330,7 +1330,7 @@ mod test { // testing fee_balance api let account = TestConfig::<5>::builder_key().fee_account(); let amount = client - .get::>(&format!("fee-state/fee_balance/{}", account)) + .get::>(&format!("fee-state/fee-balance/{}", account)) .send() .await .unwrap() diff --git a/sequencer/src/api/endpoints.rs b/sequencer/src/api/endpoints.rs index c73126a062..3ccc819c7a 100644 --- a/sequencer/src/api/endpoints.rs +++ b/sequencer/src/api/endpoints.rs @@ -7,7 +7,9 @@ use std::{ use anyhow::Result; use committable::Committable; -use espresso_types::{FeeAccount, FeeMerkleTree, NamespaceId, NsProof, PubKey, Transaction}; +use espresso_types::{ + FeeAccount, FeeMerkleTree, NamespaceId, NsProof, PubKey, Transaction, FEE_MERKLE_TREE_ARITY, +}; use futures::{try_join, FutureExt}; use hotshot_query_service::merklized_state::Snapshot; use hotshot_query_service::{ @@ -52,9 +54,8 @@ where Ver: 'static + StaticVersionType, ::State: Send + Sync - + MerklizedStateDataSource + + MerklizedStateDataSource + MerklizedStateHeightPersistence, - //for<'a> < as hotshot_query_service::merklized_state::MerklizedState>::Commit as TryFrom<&'a TaggedBase64>>::Error: Display, { let mut options = merklized_state::Options::default(); let extension = toml::from_str(include_str!("../../api/merklized_state.toml"))?; @@ -71,8 +72,8 @@ where let key = address .parse() .map_err(|_| merklized_state::Error::Custom { - message: "failed to parse Key param".to_string(), - status: StatusCode::INTERNAL_SERVER_ERROR, + message: "failed to parse address".to_string(), + status: StatusCode::BAD_REQUEST, })?; let path = state.get_path(snapshot, key).await?; Ok(path.elem().copied()) diff --git a/types/src/v0/mod.rs b/types/src/v0/mod.rs index a21ca1ade0..1429e4f7d5 100644 --- a/types/src/v0/mod.rs +++ b/types/src/v0/mod.rs @@ -181,7 +181,7 @@ pub type NetworkConfig = hotshot_types::network::NetworkConfig; pub use self::impls::{NodeState, SolverAuctionResultsProvider, ValidatedState}; pub use crate::v0_1::{ - BLOCK_MERKLE_TREE_HEIGHT, FEE_MERKLE_TREE_HEIGHT, NS_ID_BYTE_LEN, NS_OFFSET_BYTE_LEN, - NUM_NSS_BYTE_LEN, NUM_TXS_BYTE_LEN, TX_OFFSET_BYTE_LEN, + BLOCK_MERKLE_TREE_HEIGHT, FEE_MERKLE_TREE_ARITY, FEE_MERKLE_TREE_HEIGHT, NS_ID_BYTE_LEN, + NS_OFFSET_BYTE_LEN, NUM_NSS_BYTE_LEN, NUM_TXS_BYTE_LEN, TX_OFFSET_BYTE_LEN, }; use crate::v0_3::SolverAuctionResults; diff --git a/types/src/v0/v0_1/state.rs b/types/src/v0/v0_1/state.rs index 5dcdc04440..7711d37c0b 100644 --- a/types/src/v0/v0_1/state.rs +++ b/types/src/v0/v0_1/state.rs @@ -15,11 +15,12 @@ use std::collections::HashSet; pub type BlockMerkleTree = LightWeightSHA3MerkleTree>; pub type BlockMerkleCommitment = ::Commitment; -pub type FeeMerkleTree = UniversalMerkleTree; +pub type FeeMerkleTree = UniversalMerkleTree; pub type FeeMerkleCommitment = ::Commitment; pub const BLOCK_MERKLE_TREE_HEIGHT: usize = 32; pub const FEE_MERKLE_TREE_HEIGHT: usize = 20; +pub const FEE_MERKLE_TREE_ARITY: usize = 256; #[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] pub struct Delta { From 15124eb72ec30f682c69dad09c918184a2c53852 Mon Sep 17 00:00:00 2001 From: Fernando Krell Date: Tue, 29 Oct 2024 10:11:28 -0300 Subject: [PATCH 5/5] Changing fee balance path to fee-balance/latest/:address --- sequencer/api/merklized_state.toml | 2 +- sequencer/src/api.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sequencer/api/merklized_state.toml b/sequencer/api/merklized_state.toml index c3024e706d..03d04a2e28 100644 --- a/sequencer/api/merklized_state.toml +++ b/sequencer/api/merklized_state.toml @@ -1,4 +1,4 @@ [route.getfeebalance] -PATH = ["fee-balance/:address"] +PATH = ["fee-balance/latest/:address"] ":address" = "Literal" DOC = "Get current balance in fee state. Expected parameter is an Ethereum address in hex format." \ No newline at end of file diff --git a/sequencer/src/api.rs b/sequencer/src/api.rs index 10549198b8..00829ad05a 100644 --- a/sequencer/src/api.rs +++ b/sequencer/src/api.rs @@ -1330,7 +1330,7 @@ mod test { // testing fee_balance api let account = TestConfig::<5>::builder_key().fee_account(); let amount = client - .get::>(&format!("fee-state/fee-balance/{}", account)) + .get::>(&format!("fee-state/fee-balance/latest/{}", account)) .send() .await .unwrap()