Skip to content

Commit

Permalink
feat(rpc): add endpoints to query withdraw state table
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Dec 17, 2024
1 parent 4c9873c commit 930dc70
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 21 deletions.
16 changes: 13 additions & 3 deletions core/application/src/state/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use lightning_interfaces::types::{
TxHash,
Value,
};
use lightning_interfaces::SyncQueryRunnerInterface;
use lightning_interfaces::{SyncQueryRunnerInterface, WithdrawPagingParams};
use merklize::{StateRootHash, StateTree};

use crate::env::ApplicationStateTree;
Expand Down Expand Up @@ -376,19 +376,29 @@ impl SyncQueryRunnerInterface for QueryRunner {
self.get_metadata(&Metadata::Epoch).is_some()
}

fn get_flk_withdraws(&self) -> Vec<(u64, EthAddress, HpUfixed<18>)> {
fn get_flk_withdraws(
&self,
paging: WithdrawPagingParams,
) -> Vec<(u64, EthAddress, HpUfixed<18>)> {
self.inner
.run(|ctx| self.flk_withdraws.get(ctx).as_map())
.iter()
.map(|(id, (address, amount))| (*id, *address, amount.clone()))
.filter(|(id, _, _)| id >= &paging.start)
.take(paging.limit)
.collect()
}

fn get_usdc_withdraws(&self) -> Vec<(u64, EthAddress, HpUfixed<6>)> {
fn get_usdc_withdraws(
&self,
paging: WithdrawPagingParams,
) -> Vec<(u64, EthAddress, HpUfixed<6>)> {
self.inner
.run(|ctx| self.usdc_withdraws.get(ctx).as_map())
.iter()
.map(|(id, (address, amount))| (*id, *address, amount.clone()))
.filter(|(id, _, _)| id >= &paging.start)
.take(paging.limit)
.collect()
}
}
12 changes: 9 additions & 3 deletions core/application/src/tests/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use lightning_interfaces::types::{
Tokens,
UpdateMethod,
};
use lightning_interfaces::SyncQueryRunnerInterface;
use lightning_interfaces::{SyncQueryRunnerInterface, WithdrawPagingParams};
use tempfile::tempdir;

use super::utils::*;
Expand Down Expand Up @@ -255,7 +255,10 @@ async fn test_withdraw_usdc_works_properly() {
let update = prepare_update_request_account(withdraw, &owner_secret_key, 1);
expect_tx_success(update, &update_socket, ExecutionData::None).await;

let withdraws = query_runner.get_usdc_withdraws();
let withdraws = query_runner.get_usdc_withdraws(WithdrawPagingParams {
start: 0,
limit: 100,
});
assert_eq!(withdraws[0].1, receiver);
assert_eq!(withdraws[0].2, withdraw_amount.into());
}
Expand Down Expand Up @@ -291,7 +294,10 @@ async fn test_withdraw_flk_works_properly() {
let update = prepare_update_request_account(withdraw, &owner_secret_key, 1);
expect_tx_success(update, &update_socket, ExecutionData::None).await;

let withdraws = query_runner.get_flk_withdraws();
let withdraws = query_runner.get_flk_withdraws(WithdrawPagingParams {
start: 0,
limit: 100,
});
assert_eq!(withdraws[0].1, receiver);
assert_eq!(withdraws[0].2, withdraw_amount.into());
}
6 changes: 3 additions & 3 deletions core/application/src/tests/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use lightning_interfaces::types::{
ReputationMeasurements,
Staking,
};
use lightning_interfaces::PagingParams;
use lightning_interfaces::NodePagingParams;
use lightning_node::Node;
use lightning_test_utils::e2e::{try_init_tracing, TestGenesisBuilder, TestGenesisNodeBuilder};
use lightning_test_utils::json_config::JsonConfigProvider;
Expand Down Expand Up @@ -857,8 +857,8 @@ pub(crate) fn update_reputation_measurements(
}

/// Helper function that prepare `PagingParams`
pub(crate) fn paging_params(ignore_stake: bool, start: u32, limit: usize) -> PagingParams {
PagingParams {
pub(crate) fn paging_params(ignore_stake: bool, start: u32, limit: usize) -> NodePagingParams {
NodePagingParams {
ignore_stake,
start,
limit,
Expand Down
18 changes: 15 additions & 3 deletions core/interfaces/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,16 @@ pub trait SyncQueryRunnerInterface: Clone + Send + Sync + 'static {
fn has_genesis(&self) -> bool;

/// Returns a list of FLK withdraws
fn get_flk_withdraws(&self) -> Vec<(u64, EthAddress, HpUfixed<18>)>;
fn get_flk_withdraws(
&self,
paging: WithdrawPagingParams,
) -> Vec<(u64, EthAddress, HpUfixed<18>)>;

/// Returns a list of USDC withdraws
fn get_usdc_withdraws(&self) -> Vec<(u64, EthAddress, HpUfixed<6>)>;
fn get_usdc_withdraws(
&self,
paging: WithdrawPagingParams,
) -> Vec<(u64, EthAddress, HpUfixed<6>)>;
}

#[derive(Clone, Debug)]
Expand All @@ -262,7 +268,7 @@ pub enum ExecutionError {
}

#[derive(Deserialize, Serialize, schemars::JsonSchema)]
pub struct PagingParams {
pub struct NodePagingParams {
// Since some nodes may be in state without
// having staked the minimum and if at any point
// they stake the minimum amount, this would
Expand All @@ -273,3 +279,9 @@ pub struct PagingParams {
pub start: NodeIndex,
pub limit: usize,
}

#[derive(Deserialize, Serialize, schemars::JsonSchema)]
pub struct WithdrawPagingParams {
pub start: u64,
pub limit: usize,
}
21 changes: 18 additions & 3 deletions core/rpc/src/api/flk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use lightning_interfaces::types::{
TotalServed,
TransactionRequest,
};
use lightning_interfaces::PagingParams;
use lightning_interfaces::{NodePagingParams, WithdrawPagingParams};
use lightning_openrpc_macros::open_rpc;
use lightning_types::{ProtocolParamKey, StateProofKey, StateProofValue};
use merklize::{StateRootHash, StateTree};
Expand Down Expand Up @@ -154,12 +154,13 @@ pub trait FleekApi {
async fn node_has_sufficient_stake(&self, public_key: NodePublicKey) -> RpcResult<bool>;

#[method(name = "get_node_registry")]
async fn get_node_registry(&self, paging: Option<PagingParams>) -> RpcResult<Vec<NodeInfo>>;
async fn get_node_registry(&self, paging: Option<NodePagingParams>)
-> RpcResult<Vec<NodeInfo>>;

#[method(name = "get_node_registry_index")]
async fn get_node_registry_index(
&self,
paging: Option<PagingParams>,
paging: Option<NodePagingParams>,
) -> RpcResult<Vec<NodeInfoWithIndex>>;

#[method(name = "get_reputation")]
Expand Down Expand Up @@ -219,6 +220,20 @@ pub trait FleekApi {
#[method(name = "metrics")]
async fn metrics(&self) -> RpcResult<String>;

#[method(name = "get_flk_withdraws")]
async fn get_flk_withdraws(
&self,
epoch: Option<u64>,
paging: WithdrawPagingParams,
) -> RpcResult<Vec<(u64, EthAddress, HpUfixed<18>)>>;

#[method(name = "get_usdc_withdraws")]
async fn get_usdc_withdraws(
&self,
epoch: Option<u64>,
paging: WithdrawPagingParams,
) -> RpcResult<Vec<(u64, EthAddress, HpUfixed<6>)>>;

#[subscription(name = "subscribe", item = Event)]
async fn handle_subscription(&self, event_type: Option<EventType>) -> SubscriptionResult;
}
33 changes: 30 additions & 3 deletions core/rpc/src/logic/flk_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use lightning_interfaces::types::{
TransactionRequest,
Value,
};
use lightning_interfaces::PagingParams;
use lightning_interfaces::{NodePagingParams, WithdrawPagingParams};
use lightning_types::{AggregateCheckpoint, StateProofKey, StateProofValue};
use lightning_utils::application::QueryRunnerExt;
use merklize::{StateRootHash, StateTree};
Expand Down Expand Up @@ -302,7 +302,10 @@ impl<C: NodeComponents> FleekApiServer for FleekApi<C> {
Ok(self.data.query_runner.has_sufficient_stake(&pk))
}

async fn get_node_registry(&self, paging: Option<PagingParams>) -> RpcResult<Vec<NodeInfo>> {
async fn get_node_registry(
&self,
paging: Option<NodePagingParams>,
) -> RpcResult<Vec<NodeInfo>> {
Ok(self
.data
.query_runner
Expand All @@ -314,7 +317,7 @@ impl<C: NodeComponents> FleekApiServer for FleekApi<C> {

async fn get_node_registry_index(
&self,
paging: Option<PagingParams>,
paging: Option<NodePagingParams>,
) -> RpcResult<Vec<NodeInfoWithIndex>> {
Ok(self.data.query_runner.get_node_registry(paging))
}
Expand Down Expand Up @@ -465,6 +468,30 @@ impl<C: NodeComponents> FleekApiServer for FleekApi<C> {
}
}

async fn get_flk_withdraws(
&self,
epoch: Option<u64>,
paging: WithdrawPagingParams,
) -> RpcResult<Vec<(u64, EthAddress, HpUfixed<18>)>> {
Ok(self
.data
.query_runner(epoch)
.await?
.get_flk_withdraws(paging))
}

async fn get_usdc_withdraws(
&self,
epoch: Option<u64>,
paging: WithdrawPagingParams,
) -> RpcResult<Vec<(u64, EthAddress, HpUfixed<6>)>> {
Ok(self
.data
.query_runner(epoch)
.await?
.get_usdc_withdraws(paging))
}

async fn handle_subscription(
&self,
pending: PendingSubscriptionSink,
Expand Down
6 changes: 3 additions & 3 deletions core/utils/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use lightning_interfaces::types::{
ProtocolParamValue,
Value,
};
use lightning_interfaces::PagingParams;
use lightning_interfaces::NodePagingParams;
use types::{CommitteeSelectionBeaconPhase, EpochEra, Participation};

pub trait QueryRunnerExt: SyncQueryRunnerInterface {
Expand Down Expand Up @@ -156,7 +156,7 @@ pub trait QueryRunnerExt: SyncQueryRunnerInterface {
/// Returns a full copy of the entire node-registry,
/// Paging Params - filtering nodes that are still a valid node and have enough stake; Takes
/// from starting index and specified amount.
fn get_node_registry(&self, paging: Option<PagingParams>) -> Vec<NodeInfoWithIndex> {
fn get_node_registry(&self, paging: Option<NodePagingParams>) -> Vec<NodeInfoWithIndex> {
let staking_amount = self.get_staking_amount().into();

self.get_node_table_iter::<Vec<NodeInfoWithIndex>>(|nodes| -> Vec<NodeInfoWithIndex> {
Expand All @@ -168,7 +168,7 @@ pub trait QueryRunnerExt: SyncQueryRunnerInterface {
None => nodes
.filter(|node| node.info.stake.staked >= staking_amount)
.collect(),
Some(PagingParams {
Some(NodePagingParams {
ignore_stake,
limit,
start,
Expand Down

0 comments on commit 930dc70

Please sign in to comment.