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 2 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"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a convention we usually use hyphens in URL segments eg fee-balance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d19a139

":address" = "Literal"
DOC = "Get current balance in address account."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Balance in fee state" or something like that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And mention that we are expecting an ethereum address in hex format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d19a139

48 changes: 42 additions & 6 deletions sequencer/src/api/endpoints.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand All @@ -46,6 +46,42 @@ 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, 256>
+ MerklizedStateHeightPersistence,
//for<'a> <<UniversalMerkleTree<FeeAmount,Sha3Digest,FeeAccount,256,Sha3Node> as hotshot_query_service::merklized_state::MerklizedState<Types, ARITY>>::Commit as TryFrom<&'a TaggedBase64>>::Error: Display,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d19a139

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete before merging

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d19a139

{
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 Key param".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to parse address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d19a139

status: StatusCode::INTERNAL_SERVER_ERROR,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a 400 / BadRequest error because it's the client's fault.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d19a139

})?;
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 @@ -356,7 +356,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