Skip to content

Commit

Permalink
Adding API endpoint to get fee balance per account (#2165)
Browse files Browse the repository at this point in the history
* Adding API point to get fee balance per account

* Defining FEE_MERKLE_TREE_ARITY constant

* balance path to fee-balance/latest/:address

* Adding basic test for fee balance
  • Loading branch information
fkrell authored Nov 5, 2024
1 parent a8d5245 commit 86cbb62
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
4 changes: 4 additions & 0 deletions sequencer/api/merklized_state.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[route.getfeebalance]
PATH = ["fee-balance/latest/:address"]
":address" = "Literal"
DOC = "Get current balance in fee state. Expected parameter is an Ethereum address in hex format."
11 changes: 11 additions & 0 deletions sequencer/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,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::<Option<FeeAmount>>(&format!("fee-state/fee-balance/latest/{}", account))
.send()
.await
.unwrap()
.unwrap();
let expected = ethers::types::U256::max_value();
assert_eq!(expected, amount.0);
}

#[async_std::test]
Expand Down
40 changes: 39 additions & 1 deletion sequencer/src/api/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use std::{

use anyhow::Result;
use committable::Committable;
use espresso_types::{FeeAccount, 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::{
availability::{self, AvailabilityDataSource, CustomSnafu, FetchBlockSnafu},
explorer::{self, ExplorerDataSource},
Expand Down Expand Up @@ -45,6 +48,41 @@ pub struct NamespaceProofQueryData {
pub transactions: Vec<Transaction>,
}

pub(super) fn get_balance<State, Ver>() -> Result<Api<State, merklized_state::Error, Ver>>
where
State: 'static + Send + Sync + ReadState,
Ver: 'static + StaticVersionType,
<State as ReadState>::State: Send
+ Sync
+ MerklizedStateDataSource<SeqTypes, FeeMerkleTree, FEE_MERKLE_TREE_ARITY>
+ MerklizedStateHeightPersistence,
{
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::<State, SeqTypes, FeeMerkleTree, Ver, 256>(&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 address".to_string(),
status: StatusCode::BAD_REQUEST,
})?;
let path = state.get_path(snapshot, key).await?;
Ok(path.elem().copied())
}
.boxed()
})?;
Ok(api)
}

pub(super) type AvailState<N, P, D, ApiVer> = ApiState<StorageState<N, P, D, ApiVer>>;

type AvailabilityApi<N, P, D, V, ApiVer> = Api<AvailState<N, P, D, V>, availability::Error, ApiVer>;
Expand Down
4 changes: 2 additions & 2 deletions sequencer/src/api/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -357,7 +357,7 @@ impl Options {
// Initialize merklized state module for fee merkle tree
app.register_module(
"fee-state",
endpoints::merklized_state::<N, P, _, FeeMerkleTree, _, 256>()?,
endpoints::get_balance::<_, SequencerApiVersion>()?,
)?;

let state = state.clone();
Expand Down
4 changes: 2 additions & 2 deletions types/src/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub type NetworkConfig = hotshot_types::network::NetworkConfig<PubKey>;

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;
3 changes: 2 additions & 1 deletion types/src/v0/v0_1/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use std::collections::HashSet;
pub type BlockMerkleTree = LightWeightSHA3MerkleTree<Commitment<Header>>;
pub type BlockMerkleCommitment = <BlockMerkleTree as MerkleTreeScheme>::Commitment;

pub type FeeMerkleTree = UniversalMerkleTree<FeeAmount, Sha3Digest, FeeAccount, 256, Sha3Node>;
pub type FeeMerkleTree = UniversalMerkleTree<FeeAmount, Sha3Digest, FeeAccount, FEE_MERKLE_TREE_ARITY, Sha3Node>;
pub type FeeMerkleCommitment = <FeeMerkleTree as MerkleTreeScheme>::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 {
Expand Down

0 comments on commit 86cbb62

Please sign in to comment.