From 6980e763cec0e68eac8abe9bc1f9ac836f219a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Tue, 25 Jun 2024 09:01:34 +0200 Subject: [PATCH] kairos-server: add casper sse url --- kairos-cli/tests/cli_tests.rs | 12 ++++++++++-- kairos-server/.env | 1 + kairos-server/src/config.rs | 3 +++ kairos-server/tests/transactions.rs | 17 ++++++++++++----- kairos-test-utils/src/kairos.rs | 9 ++++++--- nixos/default.nix | 7 +++++-- nixos/modules/kairos.nix | 11 ++++++++++- nixos/tests/end-to-end.nix | 5 ++++- 8 files changed, 51 insertions(+), 14 deletions(-) diff --git a/kairos-cli/tests/cli_tests.rs b/kairos-cli/tests/cli_tests.rs index 5e3b6f7b..bfc87241 100644 --- a/kairos-cli/tests/cli_tests.rs +++ b/kairos-cli/tests/cli_tests.rs @@ -23,9 +23,17 @@ async fn deposit_successful_with_ed25519() { .nodes .first() .expect("Expected at least one node after successful network run"); - let node_url = Url::parse(&format!("http://localhost:{}/rpc", node.port.rpc_port)).unwrap(); + let casper_rpc_url = + Url::parse(&format!("http://localhost:{}/rpc", node.port.rpc_port)).unwrap(); + let casper_sse_url = Url::parse(&format!( + "http://localhost:{}/events/main", + node.port.sse_port + )) + .unwrap(); - let kairos = kairos::Kairos::run(node_url, None).await.unwrap(); + let kairos = kairos::Kairos::run(&casper_rpc_url, &casper_sse_url, None) + .await + .unwrap(); tokio::task::spawn_blocking(move || { let depositor_secret_key_path = network diff --git a/kairos-server/.env b/kairos-server/.env index 9c37acae..f1504ead 100644 --- a/kairos-server/.env +++ b/kairos-server/.env @@ -1,3 +1,4 @@ KAIROS_SERVER_SOCKET_ADDR="127.0.0.1:7893" KAIROS_SERVER_CASPER_RPC="http://127.0.0.1:11101/rpc" +KAIROS_SERVER_CASPER_SSE="http://127.0.0.1:18101/events/main" KAIROS_SERVER_CASPER_CONTRACT_HASH="0000000000000000000000000000000000000000000000000000000000000000" diff --git a/kairos-server/src/config.rs b/kairos-server/src/config.rs index b0952370..27fbdd93 100644 --- a/kairos-server/src/config.rs +++ b/kairos-server/src/config.rs @@ -7,6 +7,7 @@ use std::{fmt, str::FromStr}; pub struct ServerConfig { pub socket_addr: SocketAddr, pub casper_rpc: Url, + pub casper_sse: Url, pub casper_contract_hash: String, pub batch_config: BatchConfig, } @@ -15,12 +16,14 @@ impl ServerConfig { pub fn from_env() -> Result { let socket_addr = parse_env_as::("KAIROS_SERVER_SOCKET_ADDR")?; let casper_rpc = parse_env_as::("KAIROS_SERVER_CASPER_RPC")?; + let casper_sse = parse_env_as::("KAIROS_SERVER_CASPER_SSE")?; let batch_config = BatchConfig::from_env()?; let casper_contract_hash = parse_env_as::("KAIROS_SERVER_CASPER_CONTRACT_HASH")?; Ok(Self { socket_addr, casper_rpc, + casper_sse, casper_contract_hash, batch_config, }) diff --git a/kairos-server/tests/transactions.rs b/kairos-server/tests/transactions.rs index fe6613c3..c8895183 100644 --- a/kairos-server/tests/transactions.rs +++ b/kairos-server/tests/transactions.rs @@ -30,10 +30,11 @@ static TEST_ENVIRONMENT: OnceLock<()> = OnceLock::new(); #[cfg(feature = "deposit-mock")] fn new_test_app() -> TestServer { - new_test_app_with_casper_node(&Url::parse("http://0.0.0.0:0").unwrap()) + let dummy_url = Url::parse("http://0.0.0.0:0").unwrap(); + new_test_app_with_casper_node(&dummy_url, &dummy_url) } -fn new_test_app_with_casper_node(casper_node_url: &Url) -> TestServer { +fn new_test_app_with_casper_node(casper_rpc_url: &Url, casper_sse_url: &Url) -> TestServer { TEST_ENVIRONMENT.get_or_init(|| { tracing_subscriber::registry() .with( @@ -46,7 +47,8 @@ fn new_test_app_with_casper_node(casper_node_url: &Url) -> TestServer { let config = TestServerConfig::builder().mock_transport().build(); let server_config = ServerConfig { socket_addr: "0.0.0.0:0".parse().unwrap(), - casper_rpc: casper_node_url.clone(), + casper_rpc: casper_rpc_url.clone(), + casper_sse: casper_sse_url.clone(), casper_contract_hash: "0000000000000000000000000000000000000000000000000000000000000000" .to_string(), batch_config: BatchConfig { @@ -75,10 +77,15 @@ async fn test_signed_deploy_is_forwarded_if_sender_in_approvals() { .nodes .first() .expect("Expected at least one node after successful network run"); - let casper_node_url = + let casper_rpc_url = Url::parse(&format!("http://localhost:{}/rpc", node.port.rpc_port)).unwrap(); + let casper_sse_url = Url::parse(&format!( + "http://localhost:{}/events/main", + node.port.sse_port + )) + .unwrap(); - let server = new_test_app_with_casper_node(&casper_node_url); + let server = new_test_app_with_casper_node(&casper_rpc_url, &casper_sse_url); let sender_secret_key_file = network .working_dir diff --git a/kairos-test-utils/src/kairos.rs b/kairos-test-utils/src/kairos.rs index ce9e6d0f..9e841fbc 100644 --- a/kairos-test-utils/src/kairos.rs +++ b/kairos-test-utils/src/kairos.rs @@ -24,7 +24,8 @@ impl Kairos { /// If no proving server is running, we will start the one at `BatchConfig.proving_server`. /// The caller should ensure that `BatchConfig.proving_server == KAIROS_PROVER_SERVER_URL`. pub async fn run( - casper_rpc: Url, + casper_rpc: &Url, + casper_sse: &Url, proving_server_batch_config: Option, ) -> Result { let socket_addr = TcpListener::bind("0.0.0.0:0")?.local_addr()?; @@ -41,7 +42,8 @@ impl Kairos { let config = ServerConfig { socket_addr, - casper_rpc, + casper_rpc: casper_rpc.clone(), + casper_sse: casper_sse.clone(), casper_contract_hash: String::from( "0000000000000000000000000000000000000000000000000000000000000000", ), @@ -116,6 +118,7 @@ mod tests { #[tokio::test] async fn test_kairos_starts_and_terminates() { let dummy_rpc = Url::parse("http://127.0.0.1:11101/rpc").unwrap(); - let _kairos = Kairos::run(dummy_rpc, None).await.unwrap(); + let dummy_sse = Url::parse("http://127.0.0.1:18101/events/main").unwrap(); + let _kairos = Kairos::run(&dummy_rpc, &dummy_sse, None).await.unwrap(); } } diff --git a/nixos/default.nix b/nixos/default.nix index ddfc52d9..ad6c74e8 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -35,8 +35,11 @@ in imports = [ (mkKairosHostConfig "kairos-host") ]; - # A placeholder URL to make the test pass - services.kairos.casperRpcUrl = "http://localhost:11101/rpc"; + # Placeholder values make the test pass + services.kairos = { + casperRpcUrl = "http://localhost:11101/rpc"; + casperSseUrl = "http://localhost:18101/events/main"; + }; }; verifyServices = [ "kairos.service" ]; }; diff --git a/nixos/modules/kairos.nix b/nixos/modules/kairos.nix index 3546ea0c..b6f60440 100644 --- a/nixos/modules/kairos.nix +++ b/nixos/modules/kairos.nix @@ -46,6 +46,14 @@ in ''; }; + casperSseUrl = mkOption { + type = types.str; + example = "http://127.0.0.1:18101/events/main"; + description = '' + The casper node URL to the SSE events endpoint. + ''; + }; + prover = mkOption { description = "Prover server related options"; default = { }; @@ -124,7 +132,8 @@ in environment = { RUST_LOG = cfg.logLevel; KAIROS_SERVER_SOCKET_ADDR = "${cfg.bindAddress}:${builtins.toString cfg.port}"; - KAIROS_SERVER_CASPER_RPC = "${cfg.casperRpcUrl}"; + KAIROS_SERVER_CASPER_RPC = cfg.casperRpcUrl; + KAIROS_SERVER_CASPER_SSE = cfg.casperSseUrl; KAIROS_SERVER_CASPER_CONTRACT_HASH = "0000000000000000000000000000000000000000000000000000000000000000"; KAIROS_PROVER_SERVER_URL = "${cfg.prover.protocol}://${cfg.prover.bindAddress}:${builtins.toString cfg.prover.port}"; } // optionalAttrs (!builtins.isNull cfg.prover.maxBatchSize) { diff --git a/nixos/tests/end-to-end.nix b/nixos/tests/end-to-end.nix index db982202..14836294 100644 --- a/nixos/tests/end-to-end.nix +++ b/nixos/tests/end-to-end.nix @@ -37,7 +37,10 @@ nixosTest { hash = "sha256-ZuNbxw0nBjuONEZRK8Ru96zZQak4MEQ/eM1fA6esyCM="; }; }; - services.kairos.casperRpcUrl = "http://localhost:${builtins.toString config.services.cctl.port}/rpc"; + services.kairos = { + casperRpcUrl = "http://localhost:${builtins.toString config.services.cctl.port}/rpc"; + casperSseUrl = "http://localhost:18101/events/main"; # has to be hardcoded since it's not configurable atm + }; }; client = { pkgs, ... }: {