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

Dynamic chainname fetch #175

Merged
merged 8 commits into from
Jul 30, 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
2 changes: 0 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@
pkgs.postgresql
];

CASPER_CHAIN_NAME = "cspr-dev-cctl";
PATH_TO_WASM_BINARIES = "${self'.packages.kairos-contracts}/bin";
PATH_TO_SESSION_BINARIES = "${self'.packages.kairos-session-code}/bin";
CCTL_CONFIG = "${cctlConfig.config}";
Expand All @@ -193,7 +192,6 @@
# Rust Analyzer needs to be able to find the path to default crate
RUST_SRC_PATH = "${rustToolchain}/lib/rustlib/src/rust/library";
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
CASPER_CHAIN_NAME = "cspr-dev-cctl";
PATH_TO_WASM_BINARIES = "${self'.packages.kairos-contracts}/bin";
PATH_TO_SESSION_BINARIES = "${self'.packages.kairos-session-code}/bin";
CCTL_CONFIG = "${cctlConfig.config}";
Expand Down
25 changes: 21 additions & 4 deletions kairos-server/src/routes/get_chain_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::{extract::State, Json};
use axum_extra::routing::TypedPath;
use reqwest::Url;
use tracing::*;

use crate::{state::ServerState, AppErr};
Expand All @@ -8,11 +9,27 @@ use crate::{state::ServerState, AppErr};
#[typed_path("/api/v1/chain_name")]
pub struct GetChainNamePath;

#[instrument(level = "trace", skip(_state), ret)]
#[instrument(level = "trace", skip(state), ret)]
pub async fn get_chain_name_handler(
_: GetChainNamePath,
_state: State<ServerState>,
state: State<ServerState>,
) -> Result<Json<String>, AppErr> {
let chain_name = env!("CASPER_CHAIN_NAME"); // NOTE: This should be obtained from RPC, rather than from hardcoded value.
Ok(Json(String::from(chain_name)))
// Call RPC to get chain name.
let rpc_url = &state.server_config.casper_rpc;
let chain_name = get_chain_name_from_rpc(rpc_url)
.await
.expect("RPC request failed");

Ok(Json(chain_name))
}

pub async fn get_chain_name_from_rpc(rpc_url: &Url) -> Result<String, ()> {
let request_id = casper_client::JsonRpcId::Number(1);
let verbosity = casper_client::Verbosity::Low;
let response = casper_client::get_node_status(request_id, rpc_url.as_str(), verbosity)
.await
.expect("RPC request failed");
let chain_name = response.result.chainspec_name;

Ok(chain_name)
}
7 changes: 6 additions & 1 deletion kairos-server/src/state/submit_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use casper_client_types::{
use rand::random;
use reqwest::Url;

use crate::routes::get_chain_name::get_chain_name_from_rpc;

pub const MAX_GAS_FEE_PAYMENT_AMOUNT: u64 = 10_000_000_000_000;

// TODO: retry request on failure, improve error handling
Expand All @@ -29,7 +31,10 @@ pub async fn submit_proof_to_contract(
},
};

let deploy = DeployBuilder::new(env!("CASPER_CHAIN_NAME"), submit_batch, signer)
let chain_name = get_chain_name_from_rpc(&casper_rpc)
.await
.expect("RPC request failed");
let deploy = DeployBuilder::new(chain_name, submit_batch, signer)
.with_standard_payment(MAX_GAS_FEE_PAYMENT_AMOUNT)
.with_timestamp(Timestamp::now())
.with_ttl(TimeDiff::from_millis(60_000))
Expand Down
Loading