Skip to content

Commit

Permalink
Merge pull request #142 from cspr-rad/add-known-deposit-deploys
Browse files Browse the repository at this point in the history
kairos-server: remember deposit deploys routed through the node
  • Loading branch information
marijanp authored Jul 8, 2024
2 parents f8c6f6f + 591eed1 commit 99b3e60
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
10 changes: 6 additions & 4 deletions kairos-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>;
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 16 additions & 11 deletions kairos-server/src/routes/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AppErr>::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::<AppErr>::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())
}
})?,
}
}
}
9 changes: 8 additions & 1 deletion kairos-server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -17,6 +23,7 @@ pub type ServerState = Arc<ServerStateInner>;
pub struct ServerStateInner {
pub batch_state_manager: BatchStateManager,
pub server_config: ServerConfig,
pub known_deposit_deploys: RwLock<HashSet<DeployHash>>,
}

/// The `BatchStateManager` is a piece of Axum state.
Expand Down
11 changes: 7 additions & 4 deletions kairos-server/tests/transactions.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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::{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 99b3e60

Please sign in to comment.