Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kairos-server/config: make the contract hash typed #137

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kairos-cli/tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kairos-server/.env
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions kairos-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 14 additions & 3 deletions kairos-server/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use casper_types::ContractHash;
use hex::FromHex;
use reqwest::Url;
use std::net::SocketAddr;
use std::time::Duration;
Expand All @@ -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,
}

Expand All @@ -16,12 +18,21 @@ impl ServerConfig {
let socket_addr = parse_env_as::<SocketAddr>("KAIROS_SERVER_SOCKET_ADDR")?;
let casper_rpc = parse_env_as::<Url>("KAIROS_SERVER_CASPER_RPC")?;
let batch_config = BatchConfig::from_env()?;
let casper_contract_hash = parse_env_as::<String>("KAIROS_SERVER_CASPER_CONTRACT_HASH")?;
let kairos_demo_contract_hash = parse_env_as::<String>("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,
})
}
Expand Down
7 changes: 5 additions & 2 deletions kairos-server/src/l1_sync/event_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions kairos-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::Arc;

use axum::Router;
use axum_extra::routing::RouterExt;
use casper_types::ContractHash;

pub use errors::AppErr;

Expand Down Expand Up @@ -42,8 +43,8 @@ pub fn app_router(state: ServerState) -> Router {

pub async fn run_l1_sync(server_state: Arc<ServerStateInner>) {
// 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."
);
Expand Down
4 changes: 2 additions & 2 deletions kairos-server/tests/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions kairos-test-utils/src/kairos.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -26,6 +27,7 @@ impl Kairos {
pub async fn run(
casper_rpc: Url,
proving_server_batch_config: Option<BatchConfig>,
kairos_demo_contract_hash: Option<ContractHash>,
) -> Result<Kairos, io::Error> {
let socket_addr = TcpListener::bind("0.0.0.0:0")?.local_addr()?;
let port = socket_addr.port().to_string();
Expand All @@ -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,
};

Expand Down Expand Up @@ -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();
}
}
5 changes: 4 additions & 1 deletion nixos/configurations/kairos-host/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
};
};

services.kairos.enable = true;
services.kairos = {
enable = true;
demoContractHash = "0000000000000000000000000000000000000000000000000000000000000000";
};
}
16 changes: 12 additions & 4 deletions nixos/modules/kairos.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ in
options.services.kairos = {

enable = mkEnableOption (mdDoc "kairos");

package = mkOption {
type = types.package;
};
Expand All @@ -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.
'';
};

Expand Down Expand Up @@ -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;
Expand Down
Loading