diff --git a/Cargo.lock b/Cargo.lock index f1458188..39fa9b91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2505,6 +2505,7 @@ dependencies = [ "casper-client", "casper-event-toolkit", "casper-types 3.0.0", + "casper-types 4.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "contract-utils", "dotenvy", "hex", diff --git a/kairos-cli/tests/cli_tests.rs b/kairos-cli/tests/cli_tests.rs index fe9c6f2c..376d850e 100644 --- a/kairos-cli/tests/cli_tests.rs +++ b/kairos-cli/tests/cli_tests.rs @@ -25,7 +25,7 @@ async fn deposit_successful_with_ed25519() { .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 kairos = kairos::Kairos::run(node_url, None).await.unwrap(); + let kairos = kairos::Kairos::run(node_url, None, 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..256ff991 100644 --- a/kairos-server/.env +++ b/kairos-server/.env @@ -1,3 +1,3 @@ KAIROS_SERVER_SOCKET_ADDR="127.0.0.1:7893" KAIROS_SERVER_CASPER_RPC="http://127.0.0.1:11101/rpc" -KAIROS_SERVER_CASPER_CONTRACT_HASH="0000000000000000000000000000000000000000000000000000000000000000" +KAIROS_SERVER_DEMO_CONTRACT_HASH="0000000000000000000000000000000000000000000000000000000000000000" diff --git a/kairos-server/Cargo.toml b/kairos-server/Cargo.toml index f12a8cc4..133502ef 100644 --- a/kairos-server/Cargo.toml +++ b/kairos-server/Cargo.toml @@ -27,6 +27,7 @@ axum-extra = { version = "0.9", features = [ ] } anyhow = "1" casper-client.workspace = true +casper-types.workspace = true rand = "0.8" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/kairos-server/src/config.rs b/kairos-server/src/config.rs index b0952370..95bd98ca 100644 --- a/kairos-server/src/config.rs +++ b/kairos-server/src/config.rs @@ -1,3 +1,5 @@ +use casper_types::ContractHash; +use hex::FromHex; use reqwest::Url; use std::net::SocketAddr; use std::time::Duration; @@ -7,7 +9,7 @@ use std::{fmt, str::FromStr}; pub struct ServerConfig { pub socket_addr: SocketAddr, pub casper_rpc: Url, - pub casper_contract_hash: String, + pub kairos_demo_contract_hash: ContractHash, pub batch_config: BatchConfig, } @@ -16,12 +18,21 @@ impl ServerConfig { let socket_addr = parse_env_as::("KAIROS_SERVER_SOCKET_ADDR")?; let casper_rpc = parse_env_as::("KAIROS_SERVER_CASPER_RPC")?; let batch_config = BatchConfig::from_env()?; - let casper_contract_hash = parse_env_as::("KAIROS_SERVER_CASPER_CONTRACT_HASH")?; + let kairos_demo_contract_hash = parse_env_as::("KAIROS_SERVER_DEMO_CONTRACT_HASH") + .and_then(|contract_hash_string| { + <[u8; 32]>::from_hex(&contract_hash_string).map_err(|err| { + panic!( + "Failed to decode kairos-demo-contract-hash {}: {}", + contract_hash_string, err + ) + }) + }) + .map(ContractHash::new)?; Ok(Self { socket_addr, casper_rpc, - casper_contract_hash, + kairos_demo_contract_hash, batch_config, }) } diff --git a/kairos-server/src/l1_sync/event_manager.rs b/kairos-server/src/l1_sync/event_manager.rs index 17a3b27f..e42acd9e 100644 --- a/kairos-server/src/l1_sync/event_manager.rs +++ b/kairos-server/src/l1_sync/event_manager.rs @@ -23,9 +23,12 @@ impl EventManager { tracing::info!("Initializing event manager"); let rpc_url = server_state.server_config.casper_rpc.as_str(); - let contract_hash = server_state.server_config.casper_contract_hash.as_str(); + let contract_hash = server_state + .server_config + .kairos_demo_contract_hash + .to_string(); let client = CasperClient::new(rpc_url); - let metadata = CesMetadataRef::fetch_metadata(&client, contract_hash).await?; + let metadata = CesMetadataRef::fetch_metadata(&client, &contract_hash).await?; tracing::debug!("Metadata fetched successfully"); let fetcher = Fetcher { diff --git a/kairos-server/src/lib.rs b/kairos-server/src/lib.rs index 44ce19f4..c10686bb 100644 --- a/kairos-server/src/lib.rs +++ b/kairos-server/src/lib.rs @@ -10,6 +10,7 @@ use std::sync::Arc; use axum::Router; use axum_extra::routing::RouterExt; +use casper_types::ContractHash; pub use errors::AppErr; @@ -42,8 +43,8 @@ pub fn app_router(state: ServerState) -> Router { pub async fn run_l1_sync(server_state: Arc) { // Extra check: make sure the default dummy value of contract hash was changed. - let contract_hash = server_state.server_config.casper_contract_hash.as_str(); - if contract_hash == "0000000000000000000000000000000000000000000000000000000000000000" { + let contract_hash = server_state.server_config.kairos_demo_contract_hash; + if contract_hash == ContractHash::default() { tracing::warn!( "Casper contract hash not configured, L1 synchronization will NOT be enabled." ); diff --git a/kairos-server/tests/transactions.rs b/kairos-server/tests/transactions.rs index c3ac17ed..3e8db3bf 100644 --- a/kairos-server/tests/transactions.rs +++ b/kairos-server/tests/transactions.rs @@ -10,6 +10,7 @@ use casper_client_types::{ crypto::{PublicKey, SecretKey}, AsymmetricType, }; +use casper_types::ContractHash; use kairos_server::{ config::{BatchConfig, ServerConfig}, routes::deposit::DepositPath, @@ -47,8 +48,7 @@ fn new_test_app_with_casper_node(casper_node_url: &Url) -> TestServer { let server_config = ServerConfig { socket_addr: "0.0.0.0:0".parse().unwrap(), casper_rpc: casper_node_url.clone(), - casper_contract_hash: "0000000000000000000000000000000000000000000000000000000000000000" - .to_string(), + kairos_demo_contract_hash: ContractHash::default(), batch_config: BatchConfig { max_batch_size: None, max_batch_duration: None, diff --git a/kairos-test-utils/src/kairos.rs b/kairos-test-utils/src/kairos.rs index ce9e6d0f..845d2b2f 100644 --- a/kairos-test-utils/src/kairos.rs +++ b/kairos-test-utils/src/kairos.rs @@ -1,5 +1,6 @@ use backoff::future::retry; use backoff::ExponentialBackoff; +use casper_types::ContractHash; use reqwest::Url; use std::io; use std::net::{SocketAddr, TcpListener}; @@ -26,6 +27,7 @@ impl Kairos { pub async fn run( casper_rpc: Url, proving_server_batch_config: Option, + kairos_demo_contract_hash: Option, ) -> Result { let socket_addr = TcpListener::bind("0.0.0.0:0")?.local_addr()?; let port = socket_addr.port().to_string(); @@ -42,9 +44,7 @@ impl Kairos { let config = ServerConfig { socket_addr, casper_rpc, - casper_contract_hash: String::from( - "0000000000000000000000000000000000000000000000000000000000000000", - ), + kairos_demo_contract_hash: kairos_demo_contract_hash.unwrap_or_default(), batch_config, }; @@ -116,6 +116,6 @@ 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 _kairos = Kairos::run(dummy_rpc, None, None).await.unwrap(); } } diff --git a/nixos/configurations/kairos-host/default.nix b/nixos/configurations/kairos-host/default.nix index a56a8729..d60f2edf 100644 --- a/nixos/configurations/kairos-host/default.nix +++ b/nixos/configurations/kairos-host/default.nix @@ -22,5 +22,8 @@ }; }; - services.kairos.enable = true; + services.kairos = { + enable = true; + demoContractHash = "0000000000000000000000000000000000000000000000000000000000000000"; + }; } diff --git a/nixos/modules/kairos.nix b/nixos/modules/kairos.nix index 3546ea0c..3f0f06a2 100644 --- a/nixos/modules/kairos.nix +++ b/nixos/modules/kairos.nix @@ -15,7 +15,6 @@ in options.services.kairos = { enable = mkEnableOption (mdDoc "kairos"); - package = mkOption { type = types.package; }; @@ -42,7 +41,16 @@ in type = types.str; example = "http://127.0.0.1:11101/rpc"; description = '' - A casper node URL. + The casper node URL to the RPC endpoint. + ''; + }; + + demoContractHash = mkOption { + type = types.str; + example = "TODO put a contract hash here"; + description = '' + The hash of the deployed demo contract. + Use an empty string when testing with cctl. ''; }; @@ -124,8 +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_CONTRACT_HASH = "0000000000000000000000000000000000000000000000000000000000000000"; + KAIROS_SERVER_CASPER_RPC = cfg.casperRpcUrl; + KAIROS_SERVER_DEMO_CONTRACT_HASH = cfg.demoContractHash; KAIROS_PROVER_SERVER_URL = "${cfg.prover.protocol}://${cfg.prover.bindAddress}:${builtins.toString cfg.prover.port}"; } // optionalAttrs (!builtins.isNull cfg.prover.maxBatchSize) { KAIROS_SERVER_MAX_BATCH_SIZE = cfg.maxBatchSize;