From ce7d709931fbfcab42de900e987b8b18ef09b11b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Thu, 4 Jul 2024 11:36:04 +0200 Subject: [PATCH] kairos-server: remember deposit deploys routed through the node --- kairos-server/src/lib.rs | 10 ++++++---- kairos-server/src/routes/deposit.rs | 27 ++++++++++++++++----------- kairos-server/src/state.rs | 9 ++++++++- kairos-server/tests/transactions.rs | 11 +++++++---- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/kairos-server/src/lib.rs b/kairos-server/src/lib.rs index 23397a46..0f26b34c 100644 --- a/kairos-server/src/lib.rs +++ b/kairos-server/src/lib.rs @@ -6,17 +6,18 @@ pub mod state; mod l1_sync; mod utils; -use std::sync::Arc; - use axum::Router; use axum_extra::routing::RouterExt; -use casper_client_types::ContractHash; +use std::collections::HashSet; +use std::sync::Arc; +use tokio::sync::RwLock; -pub use errors::AppErr; +use casper_client_types::ContractHash; use crate::config::ServerConfig; use crate::l1_sync::service::L1SyncService; use crate::state::{BatchStateManager, ServerState, ServerStateInner}; +pub use errors::AppErr; /// TODO: support secp256k1 type PublicKey = Vec; @@ -72,6 +73,7 @@ pub async fn run(config: ServerConfig) { let state = Arc::new(ServerStateInner { batch_state_manager: BatchStateManager::new_empty(&config), server_config: config.clone(), + known_deposit_deploys: RwLock::new(HashSet::new()), }); run_l1_sync(state.clone()).await; diff --git a/kairos-server/src/routes/deposit.rs b/kairos-server/src/routes/deposit.rs index 79870021..19906b3d 100644 --- a/kairos-server/src/routes/deposit.rs +++ b/kairos-server/src/routes/deposit.rs @@ -29,20 +29,25 @@ pub async fn deposit_handler( .find(|approval| approval.signer() == depositor_account) { None => return Err(anyhow!("Deploy not signed by depositor").into()), - Some(_) => put_deploy( - expected_rpc_id.clone(), - state.server_config.casper_rpc.as_str(), - casper_client::Verbosity::High, - body, - ) - .await - .map_err(Into::::into) - .map(|response| { + Some(_) => { + let response = put_deploy( + expected_rpc_id.clone(), + state.server_config.casper_rpc.as_str(), + casper_client::Verbosity::High, + body, + ) + .await + .map_err(Into::::into)?; if response.id == expected_rpc_id { + assert!(state + .known_deposit_deploys + .write() + .await + .insert(response.result.deploy_hash)); Ok(Json(response.result.deploy_hash)) } else { - Err(anyhow!("Deploy not signed by depositor").into()) + Err(anyhow!("JSON RPC Id missmatch").into()) } - })?, + } } } diff --git a/kairos-server/src/state.rs b/kairos-server/src/state.rs index 5628124f..dddd3f3c 100644 --- a/kairos-server/src/state.rs +++ b/kairos-server/src/state.rs @@ -2,9 +2,15 @@ pub mod submit_batch; pub mod transactions; mod trie; +use std::collections::HashSet; use std::{sync::Arc, thread}; -use tokio::{sync::mpsc, task}; +use tokio::{ + sync::{mpsc, RwLock}, + task, +}; + +use casper_client::types::DeployHash; pub use self::trie::TrieStateThreadMsg; use crate::{config::ServerConfig, state::submit_batch::submit_proof_to_contract}; @@ -17,6 +23,7 @@ pub type ServerState = Arc; pub struct ServerStateInner { pub batch_state_manager: BatchStateManager, pub server_config: ServerConfig, + pub known_deposit_deploys: RwLock>, } /// The `BatchStateManager` is a piece of Axum state. diff --git a/kairos-server/tests/transactions.rs b/kairos-server/tests/transactions.rs index 350c8c5f..435b2640 100644 --- a/kairos-server/tests/transactions.rs +++ b/kairos-server/tests/transactions.rs @@ -1,7 +1,11 @@ -use std::sync::{Arc, OnceLock}; - use axum_extra::routing::TypedPath; use axum_test::{TestServer, TestServerConfig}; +use reqwest::Url; +use std::collections::HashSet; +use std::sync::{Arc, OnceLock}; +use tokio::sync::RwLock; +use tracing_subscriber::{prelude::*, EnvFilter}; + use casper_client::{ types::{DeployBuilder, Timestamp}, TransferTarget, @@ -16,8 +20,6 @@ use kairos_server::{ state::{BatchStateManager, ServerStateInner}, }; use kairos_test_utils::cctl::CCTLNetwork; -use reqwest::Url; -use tracing_subscriber::{prelude::*, EnvFilter}; #[cfg(feature = "deposit-mock")] use kairos_server::routes::{ @@ -62,6 +64,7 @@ fn new_test_app_with_casper_node(casper_rpc_url: &Url, casper_sse_url: &Url) -> let state = Arc::new(ServerStateInner { batch_state_manager: BatchStateManager::new_empty(&server_config), server_config, + known_deposit_deploys: RwLock::new(HashSet::new()), }); TestServer::new_with_config(kairos_server::app_router(state), config).unwrap()