From 18a6807355415b11ec0fb75d9341a1a3ff778daa Mon Sep 17 00:00:00 2001 From: Matthias Wright Date: Mon, 23 Dec 2024 01:36:00 +0100 Subject: [PATCH] chore: simplify withdraw state tables --- core/application/src/state/executor.rs | 31 ++++++++++++----- .../src/state/executor/epoch_change.rs | 3 +- core/application/src/state/query.rs | 34 +++++-------------- core/application/src/state/writer.rs | 7 ++-- core/application/src/tests/balances.rs | 12 +++---- core/interfaces/src/application.rs | 15 ++------ core/rpc/src/api/flk.rs | 15 +++----- core/rpc/src/logic/flk_impl.rs | 24 +++---------- core/types/src/application.rs | 16 ++++++++- 9 files changed, 67 insertions(+), 90 deletions(-) diff --git a/core/application/src/state/executor.rs b/core/application/src/state/executor.rs index 9ca61ab32..c7dc703a2 100644 --- a/core/application/src/state/executor.rs +++ b/core/application/src/state/executor.rs @@ -52,6 +52,7 @@ use lightning_interfaces::types::{ UpdateMethod, UpdateRequest, Value, + WithdrawInfo, MAX_MEASUREMENTS_PER_TX, MAX_MEASUREMENTS_SUBMIT, MAX_UPDATES_CONTENT_REGISTRY, @@ -131,8 +132,7 @@ pub struct StateExecutor { ), >, pub committee_selection_beacon_non_revealing_node: B::Ref, - pub flk_withdraws: B::Ref)>, - pub usdc_withdraws: B::Ref)>, + pub withdraws: B::Ref, pub backend: B, } @@ -164,8 +164,7 @@ impl StateExecutor { committee_selection_beacon: backend.get_table_reference("committee_selection_beacon"), committee_selection_beacon_non_revealing_node: backend .get_table_reference("committee_selection_beacon_non_revealing_node"), - flk_withdraws: backend.get_table_reference("flk_withdraws"), - usdc_withdraws: backend.get_table_reference("usdc_withdraws"), + withdraws: backend.get_table_reference("withdraws"), backend, } } @@ -477,7 +476,7 @@ impl StateExecutor { fn withdraw( &self, sender: TransactionSender, - reciever: EthAddress, + receiver: EthAddress, amount: HpUfixed<18>, token: Tokens, ) -> TransactionResponse { @@ -501,9 +500,16 @@ impl StateExecutor { if amount > account.flk_balance { return TransactionResponse::Revert(ExecutionError::InsufficientBalance); } - account.flk_balance -= amount.clone(); - self.flk_withdraws.set(withdraw_id, (reciever, amount)); + self.withdraws.set( + withdraw_id, + WithdrawInfo { + epoch: 0, + token: Tokens::FLK, + receiver, + amount, + }, + ); self.metadata .set(Metadata::WithdrawId, Value::WithdrawId(withdraw_id + 1)); }, @@ -513,9 +519,16 @@ impl StateExecutor { if amount > account.stables_balance { return TransactionResponse::Revert(ExecutionError::InsufficientBalance); } - account.stables_balance -= amount.clone(); - self.usdc_withdraws.set(withdraw_id, (reciever, amount)); + self.withdraws.set( + withdraw_id, + WithdrawInfo { + epoch: 0, + token: Tokens::USDC, + receiver, + amount: amount.convert_precision::<18>(), + }, + ); self.metadata .set(Metadata::WithdrawId, Value::WithdrawId(withdraw_id + 1)); }, diff --git a/core/application/src/state/executor/epoch_change.rs b/core/application/src/state/executor/epoch_change.rs index c4a6573ae..1deb9ab04 100644 --- a/core/application/src/state/executor/epoch_change.rs +++ b/core/application/src/state/executor/epoch_change.rs @@ -677,8 +677,7 @@ impl StateExecutor { self.executed_digests.clear(); // Clear withdraws - self.flk_withdraws.clear(); - self.usdc_withdraws.clear(); + self.withdraws.clear(); self.metadata .set(Metadata::WithdrawId, Value::WithdrawId(0)); diff --git a/core/application/src/state/query.rs b/core/application/src/state/query.rs index 3feb84cd5..bc5032535 100644 --- a/core/application/src/state/query.rs +++ b/core/application/src/state/query.rs @@ -40,6 +40,8 @@ use lightning_interfaces::types::{ TransactionResponse, TxHash, Value, + WithdrawInfo, + WithdrawInfoWithId, }; use lightning_interfaces::{SyncQueryRunnerInterface, WithdrawPagingParams}; use merklize::{StateRootHash, StateTree}; @@ -79,8 +81,7 @@ pub struct QueryRunner { ), >, committee_selection_beacon_non_revealing_node: ResolvedTableReference, - flk_withdraws: ResolvedTableReference)>, - usdc_withdraws: ResolvedTableReference)>, + withdraws: ResolvedTableReference, } impl QueryRunner { @@ -124,8 +125,7 @@ impl SyncQueryRunnerInterface for QueryRunner { )>("committee_selection_beacon"), committee_selection_beacon_non_revealing_node: atomo .resolve::("committee_selection_beacon_non_revealing_node"), - flk_withdraws: atomo.resolve::)>("flk_withdraws"), - usdc_withdraws: atomo.resolve::)>("usdc_withdraws"), + withdraws: atomo.resolve::("withdraws"), inner: atomo, } } @@ -376,28 +376,12 @@ impl SyncQueryRunnerInterface for QueryRunner { self.get_metadata(&Metadata::Epoch).is_some() } - 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, - paging: WithdrawPagingParams, - ) -> Vec<(u64, EthAddress, HpUfixed<6>)> { + fn get_withdraws(&self, paging: WithdrawPagingParams) -> Vec { 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) + .run(|ctx| self.withdraws.get(ctx).as_map()) + .into_iter() + .map(|(id, info)| WithdrawInfoWithId { id, info }) + .filter(|info| info.id >= paging.start) .take(paging.limit) .collect() } diff --git a/core/application/src/state/writer.rs b/core/application/src/state/writer.rs index 898aa8e79..1f712c0cd 100644 --- a/core/application/src/state/writer.rs +++ b/core/application/src/state/writer.rs @@ -36,6 +36,7 @@ use lightning_interfaces::types::{ TotalServed, TxHash, Value, + WithdrawInfo, }; use lightning_interfaces::SyncQueryRunnerInterface; use merklize::StateTree; @@ -188,8 +189,7 @@ impl ApplicationState { Option, )>("committee_selection_beacon") .with_table::("committee_selection_beacon_non_revealing_node") - .with_table::)>("flk_withdraws") - .with_table::)>("usdc_withdraws") + .with_table::("withdraws") .enable_iter("current_epoch_served") .enable_iter("rep_measurements") .enable_iter("submitted_rep_measurements") @@ -203,8 +203,7 @@ impl ApplicationState { .enable_iter("node_to_uri") .enable_iter("committee_selection_beacon") .enable_iter("committee_selection_beacon_non_revealing_node") - .enable_iter("flk_withdraws") - .enable_iter("usdc_withdraws"); + .enable_iter("withdraws"); #[cfg(debug_assertions)] { diff --git a/core/application/src/tests/balances.rs b/core/application/src/tests/balances.rs index c73ab285f..0ecddda61 100644 --- a/core/application/src/tests/balances.rs +++ b/core/application/src/tests/balances.rs @@ -255,12 +255,12 @@ 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(WithdrawPagingParams { + let withdraws = query_runner.get_withdraws(WithdrawPagingParams { start: 0, limit: 100, }); - assert_eq!(withdraws[0].1, receiver); - assert_eq!(withdraws[0].2, withdraw_amount.into()); + assert_eq!(withdraws[0].info.receiver, receiver); + assert_eq!(withdraws[0].info.amount, withdraw_amount.into()); } #[tokio::test] @@ -294,10 +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(WithdrawPagingParams { + let withdraws = query_runner.get_withdraws(WithdrawPagingParams { start: 0, limit: 100, }); - assert_eq!(withdraws[0].1, receiver); - assert_eq!(withdraws[0].2, withdraw_amount.into()); + assert_eq!(withdraws[0].info.receiver, receiver); + assert_eq!(withdraws[0].info.amount, withdraw_amount.into()); } diff --git a/core/interfaces/src/application.rs b/core/interfaces/src/application.rs index b94534b61..361e16602 100644 --- a/core/interfaces/src/application.rs +++ b/core/interfaces/src/application.rs @@ -8,7 +8,6 @@ use atomo::{Atomo, InMemoryStorage, KeyIterator, QueryPerm, StorageBackend}; use fdi::BuildGraph; use fleek_crypto::{ClientPublicKey, EthAddress, NodePublicKey}; use fxhash::FxHashMap; -use hp_fixed::unsigned::HpUfixed; use lightning_types::{ AccountInfo, Blake3Hash, @@ -25,6 +24,7 @@ use lightning_types::{ TransactionRequest, TxHash, Value, + WithdrawInfoWithId, }; use merklize::trees::mpt::MptStateProof; use merklize::StateRootHash; @@ -242,17 +242,8 @@ pub trait SyncQueryRunnerInterface: Clone + Send + Sync + 'static { // Returns whether the genesis block has been applied. fn has_genesis(&self) -> bool; - /// Returns a list of FLK withdraws - fn get_flk_withdraws( - &self, - paging: WithdrawPagingParams, - ) -> Vec<(u64, EthAddress, HpUfixed<18>)>; - - /// Returns a list of USDC withdraws - fn get_usdc_withdraws( - &self, - paging: WithdrawPagingParams, - ) -> Vec<(u64, EthAddress, HpUfixed<6>)>; + /// Returns a list of withdraws + fn get_withdraws(&self, paging: WithdrawPagingParams) -> Vec; } #[derive(Clone, Debug)] diff --git a/core/rpc/src/api/flk.rs b/core/rpc/src/api/flk.rs index 54d82941b..b728fe3c4 100644 --- a/core/rpc/src/api/flk.rs +++ b/core/rpc/src/api/flk.rs @@ -24,7 +24,7 @@ use lightning_interfaces::types::{ }; use lightning_interfaces::{NodePagingParams, WithdrawPagingParams}; use lightning_openrpc_macros::open_rpc; -use lightning_types::{ProtocolParamKey, StateProofKey, StateProofValue}; +use lightning_types::{ProtocolParamKey, StateProofKey, StateProofValue, WithdrawInfoWithId}; use merklize::{StateRootHash, StateTree}; #[open_rpc(namespace = "flk", tag = "1.0.0")] @@ -220,19 +220,12 @@ pub trait FleekApi { #[method(name = "metrics")] async fn metrics(&self) -> RpcResult; - #[method(name = "get_flk_withdraws")] - async fn get_flk_withdraws( + #[method(name = "get_withdraws")] + async fn get_withdraws( &self, epoch: Option, paging: WithdrawPagingParams, - ) -> RpcResult)>>; - - #[method(name = "get_usdc_withdraws")] - async fn get_usdc_withdraws( - &self, - epoch: Option, - paging: WithdrawPagingParams, - ) -> RpcResult)>>; + ) -> RpcResult>; #[subscription(name = "subscribe", item = Event)] async fn handle_subscription(&self, event_type: Option) -> SubscriptionResult; diff --git a/core/rpc/src/logic/flk_impl.rs b/core/rpc/src/logic/flk_impl.rs index 4f8c840d4..0ca4f02d5 100644 --- a/core/rpc/src/logic/flk_impl.rs +++ b/core/rpc/src/logic/flk_impl.rs @@ -33,7 +33,7 @@ use lightning_types::{AggregateCheckpoint, StateProofKey, StateProofValue}; use lightning_utils::application::QueryRunnerExt; use merklize::{StateRootHash, StateTree}; use serde_json::Value as JsonValue; -use types::ProtocolParamKey; +use types::{ProtocolParamKey, WithdrawInfoWithId}; use crate::api::FleekApiServer; use crate::error::RPCError; @@ -468,28 +468,12 @@ impl FleekApiServer for FleekApi { } } - async fn get_flk_withdraws( + async fn get_withdraws( &self, epoch: Option, paging: WithdrawPagingParams, - ) -> RpcResult)>> { - Ok(self - .data - .query_runner(epoch) - .await? - .get_flk_withdraws(paging)) - } - - async fn get_usdc_withdraws( - &self, - epoch: Option, - paging: WithdrawPagingParams, - ) -> RpcResult)>> { - Ok(self - .data - .query_runner(epoch) - .await? - .get_usdc_withdraws(paging)) + ) -> RpcResult> { + Ok(self.data.query_runner(epoch).await?.get_withdraws(paging)) } async fn handle_subscription( diff --git a/core/types/src/application.rs b/core/types/src/application.rs index 9e57b8989..aa7bc2dff 100644 --- a/core/types/src/application.rs +++ b/core/types/src/application.rs @@ -7,7 +7,7 @@ use fleek_crypto::{EthAddress, NodePublicKey}; use hp_fixed::unsigned::HpUfixed; use serde::{Deserialize, Serialize}; -use crate::{BlockNumber, Staking, TransactionReceipt}; +use crate::{BlockNumber, Epoch, Staking, Tokens, TransactionReceipt}; /// Max number of updates allowed in a content registry update transaction. pub const MAX_UPDATES_CONTENT_REGISTRY: usize = 100; @@ -211,3 +211,17 @@ pub struct AccountInfo { /// enforce ordering pub nonce: u64, } + +#[derive(Debug, Hash, Serialize, Deserialize, Clone, schemars::JsonSchema)] +pub struct WithdrawInfo { + pub epoch: Epoch, + pub token: Tokens, + pub receiver: EthAddress, + pub amount: HpUfixed<18>, +} + +#[derive(Debug, Hash, Serialize, Deserialize, Clone, schemars::JsonSchema)] +pub struct WithdrawInfoWithId { + pub id: u64, + pub info: WithdrawInfo, +}