Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding API endpoint to get fee balance per account #2165

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/: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 @@ -1326,6 +1326,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/{}", 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>>
fkrell marked this conversation as resolved.
Show resolved Hide resolved
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?;
sveitser marked this conversation as resolved.
Show resolved Hide resolved
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
sveitser marked this conversation as resolved.
Show resolved Hide resolved
app.register_module(
"fee-state",
endpoints::merklized_state::<N, P, _, FeeMerkleTree, _, 256>()?,
endpoints::get_balance::<_, SequencerApiVersion>()?,
sveitser marked this conversation as resolved.
Show resolved Hide resolved
)?;

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 @@ -181,7 +181,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