Skip to content

Commit

Permalink
Cleanup unused kupo and pc-contracts-cli, query ogmios for network
Browse files Browse the repository at this point in the history
  • Loading branch information
LGLO committed Dec 20, 2024
1 parent f0e6dac commit 3a44c72
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 414 deletions.
50 changes: 2 additions & 48 deletions toolkit/partner-chains-cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::config::config_fields::{
CARDANO_ACTIVE_SLOTS_COEFF, CARDANO_EPOCH_DURATION_MILLIS, CARDANO_FIRST_EPOCH_NUMBER,
CARDANO_FIRST_EPOCH_TIMESTAMP_MILLIS, CARDANO_FIRST_SLOT_NUMBER, CARDANO_NETWORK,
CARDANO_SECURITY_PARAMETER,
CARDANO_FIRST_EPOCH_TIMESTAMP_MILLIS, CARDANO_FIRST_SLOT_NUMBER, CARDANO_SECURITY_PARAMETER,
};
use crate::io::IOContext;
use anyhow::anyhow;
use clap::{arg, Parser};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sidechain_domain::{NetworkType, UtxoId};
use sidechain_domain::UtxoId;
use sp_core::offchain::{Duration, Timestamp};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
Expand Down Expand Up @@ -275,12 +273,10 @@ pub struct CardanoParameters {
pub first_slot_number: u64,
pub epoch_duration_millis: u64,
pub first_epoch_timestamp_millis: u64,
pub network: NetworkType,
}

impl CardanoParameters {
pub fn save(&self, context: &impl IOContext) {
CARDANO_NETWORK.save_to_file(&self.network, context);
CARDANO_SECURITY_PARAMETER.save_to_file(&self.security_parameter, context);
CARDANO_ACTIVE_SLOTS_COEFF.save_to_file(&self.active_slots_coeff, context);
CARDANO_FIRST_EPOCH_NUMBER.save_to_file(&self.first_epoch_number, context);
Expand All @@ -299,7 +295,6 @@ impl CardanoParameters {
epoch_duration_millis: CARDANO_EPOCH_DURATION_MILLIS.load_from_file(context)?,
first_epoch_timestamp_millis: CARDANO_FIRST_EPOCH_TIMESTAMP_MILLIS
.load_from_file(context)?,
network: CARDANO_NETWORK.load_from_file(context)?,
})
}
}
Expand Down Expand Up @@ -353,7 +348,6 @@ pub const KEYS_FILE_PATH: &str = "partner-chains-public-keys.json";
pub const CHAIN_CONFIG_FILE_PATH: &str = "partner-chains-cli-chain-config.json";
pub const RESOURCES_CONFIG_FILE_PATH: &str = "partner-chains-cli-resources-config.json";
pub const CHAIN_SPEC_PATH: &str = "chain-spec.json";
pub const PC_CONTRACTS_CLI_PATH: &str = "./pc-contracts-cli";

pub fn load_chain_config(context: &impl IOContext) -> anyhow::Result<ChainConfig> {
if let Some(chain_config_file) = context.read_file(CHAIN_CONFIG_FILE_PATH) {
Expand All @@ -364,12 +358,6 @@ pub fn load_chain_config(context: &impl IOContext) -> anyhow::Result<ChainConfig
}
}

pub fn get_cardano_network_from_file(context: &impl IOContext) -> anyhow::Result<NetworkType> {
CARDANO_NETWORK.load_from_file(context).ok_or(anyhow!(
"Cardano network not configured. Please run prepare-main-chain-config command first."
))
}

pub mod config_fields {
use super::*;
use sidechain_domain::UtxoId;
Expand Down Expand Up @@ -446,15 +434,6 @@ pub mod config_fields {
_marker: PhantomData,
};

pub const CARDANO_NETWORK: ConfigFieldDefinition<'static, NetworkType> =
ConfigFieldDefinition {
config_file: CHAIN_CONFIG_FILE_PATH,
path: &["cardano", "network"],
name: "cardano network",
default: None,
_marker: PhantomData,
};

pub const GENESIS_UTXO: ConfigFieldDefinition<'static, UtxoId> = ConfigFieldDefinition {
config_file: CHAIN_CONFIG_FILE_PATH,
path: &["chain_parameters", "genesis_utxo"],
Expand Down Expand Up @@ -491,31 +470,6 @@ pub mod config_fields {
_marker: PhantomData,
};

pub const KUPO_PROTOCOL: ConfigFieldDefinition<'static, NetworkProtocol> =
ConfigFieldDefinition {
config_file: RESOURCES_CONFIG_FILE_PATH,
path: &["kupo", "protocol"],
name: "Kupo protocol (http/https)",
default: Some("http"),
_marker: PhantomData,
};

pub const KUPO_HOSTNAME: ConfigFieldDefinition<'static, String> = ConfigFieldDefinition {
config_file: RESOURCES_CONFIG_FILE_PATH,
path: &["kupo", "hostname"],
name: "Kupo hostname",
default: Some("localhost"),
_marker: PhantomData,
};

pub const KUPO_PORT: ConfigFieldDefinition<'static, u16> = ConfigFieldDefinition {
config_file: RESOURCES_CONFIG_FILE_PATH,
path: &["kupo", "port"],
name: "Kupo port",
default: Some("1442"),
_marker: PhantomData,
};

pub const OGMIOS_PROTOCOL: ConfigFieldDefinition<'static, NetworkProtocol> =
ConfigFieldDefinition {
config_file: RESOURCES_CONFIG_FILE_PATH,
Expand Down
6 changes: 3 additions & 3 deletions toolkit/partner-chains-cli/src/deregister/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::config::config_fields::{
};
use crate::config::CHAIN_CONFIG_FILE_PATH;
use crate::io::IOContext;
use crate::pc_contracts_cli_resources::establish_pc_contracts_cli_configuration;
use crate::ogmios::config::establish_ogmios_configuration;
use crate::CmdRun;
use anyhow::anyhow;
use partner_chains_cardano_offchain::register::Deregister;
Expand All @@ -30,8 +30,8 @@ impl CmdRun for DeregisterCmd {
let cold_vkey_path =
CARDANO_COLD_VERIFICATION_KEY_FILE.prompt_with_default_from_file_and_save(context);
let stake_ownership_pub_key = get_mc_pubkey_from_file(&cold_vkey_path, context)?;
let pc_contracts_cli_resources = establish_pc_contracts_cli_configuration(context)?;
let offchain = context.offchain_impl(&pc_contracts_cli_resources.ogmios)?;
let ogmios_config = establish_ogmios_configuration(context)?;
let offchain = context.offchain_impl(&ogmios_config)?;

let runtime = tokio::runtime::Runtime::new().map_err(|e| anyhow::anyhow!(e))?;
runtime
Expand Down
9 changes: 5 additions & 4 deletions toolkit/partner-chains-cli/src/deregister/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::config::{CHAIN_CONFIG_FILE_PATH, RESOURCES_CONFIG_FILE_PATH};
use crate::deregister::DeregisterCmd;
use crate::pc_contracts_cli_resources::tests::establish_pc_contracts_cli_configuration_io;
use crate::pc_contracts_cli_resources::PcContractsCliResources;
use crate::ogmios::config::tests::{
default_ogmios_service_config, establish_ogmios_configuration_io,
};
use crate::tests::{MockIO, MockIOContext, OffchainMock, OffchainMocks};
use crate::CmdRun;
use hex_literal::hex;
Expand Down Expand Up @@ -32,7 +33,7 @@ fn happy_path() {
MockIO::file_read(CHAIN_CONFIG_FILE_PATH),
print_info_io(),
read_keys_io(),
establish_pc_contracts_cli_configuration_io(None, PcContractsCliResources::default()),
establish_ogmios_configuration_io(None, default_ogmios_service_config()),
]);
let result = DeregisterCmd.run(&mock_context);
assert!(result.is_ok());
Expand All @@ -56,7 +57,7 @@ fn errors_if_smart_contracts_dont_output_transaction_id() {
MockIO::file_read(CHAIN_CONFIG_FILE_PATH),
print_info_io(),
read_keys_io(),
establish_pc_contracts_cli_configuration_io(None, PcContractsCliResources::default()),
establish_ogmios_configuration_io(None, default_ogmios_service_config()),
]);
let result = DeregisterCmd.run(&mock_context);
assert_eq!(
Expand Down
2 changes: 0 additions & 2 deletions toolkit/partner-chains-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ pub mod io;
pub mod keystore;
pub(crate) mod main_chain_follower;
pub(crate) mod ogmios;
pub(crate) mod pc_contracts_cli_resources;
pub(crate) mod permissioned_candidates;
mod prepare_configuration;
pub mod register;
pub(crate) mod select_utxo;
mod setup_main_chain_state;
pub(crate) mod smart_contracts;
pub mod start_node;

#[cfg(test)]
Expand Down
82 changes: 82 additions & 0 deletions toolkit/partner-chains-cli/src/ogmios/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::config::config_fields::*;
use crate::config::ServiceConfig;
use crate::io::IOContext;

pub(crate) const OGMIOS_REQUIRED: &str =
"Partner Chains Smart Contracts require access to Ogmios. Please provide its configuration.";

pub(crate) fn establish_ogmios_configuration<C: IOContext>(
context: &C,
) -> anyhow::Result<ServiceConfig> {
context.print(OGMIOS_REQUIRED);
prompt_ogmios_configuration(context)
}

pub(crate) fn prompt_ogmios_configuration<C: IOContext>(
context: &C,
) -> anyhow::Result<ServiceConfig> {
let ogmios_protocol = OGMIOS_PROTOCOL
.select_options_with_default_from_file_and_save(OGMIOS_PROTOCOL.name, context)
.map_err(anyhow::Error::msg)?;
let ogmios_hostname = OGMIOS_HOSTNAME.prompt_with_default_from_file_and_save(context);
let ogmios_port = OGMIOS_PORT.prompt_with_default_from_file_parse_and_save(context)?;
Ok(ServiceConfig { protocol: ogmios_protocol, hostname: ogmios_hostname, port: ogmios_port })
}

#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::config::NetworkProtocol;
use crate::prepare_configuration::tests::{
prompt_multi_option_with_default_and_save_to_existing_file,
prompt_with_default_and_save_to_existing_file,
};
use crate::tests::MockIO;
use std::str::FromStr;

pub(crate) fn default_ogmios_service_config() -> ServiceConfig {
ServiceConfig {
protocol: OGMIOS_PROTOCOL
.default
.and_then(|p| NetworkProtocol::from_str(p).ok())
.unwrap_or(NetworkProtocol::Http),
hostname: OGMIOS_HOSTNAME.default.unwrap_or("localhost").to_string(),
port: OGMIOS_PORT.default.unwrap_or("1337").parse().unwrap(),
}
}

/// Assumption for this function is that resources config file exists, so tests context should have it.
pub(crate) fn establish_ogmios_configuration_io(
existing_config: Option<ServiceConfig>,
config_to_set: ServiceConfig,
) -> MockIO {
let default_config = existing_config.unwrap_or(default_ogmios_service_config());
MockIO::Group(vec![
MockIO::print(OGMIOS_REQUIRED),
prompt_ogmios_configuration_io(&default_config, &config_to_set),
])
}

pub(crate) fn prompt_ogmios_configuration_io(
default_config: &ServiceConfig,
config_to_set: &ServiceConfig,
) -> MockIO {
MockIO::Group(vec![
prompt_multi_option_with_default_and_save_to_existing_file(
OGMIOS_PROTOCOL,
Some(&default_config.protocol.to_string()),
&config_to_set.protocol.to_string(),
),
prompt_with_default_and_save_to_existing_file(
OGMIOS_HOSTNAME,
Some(&default_config.hostname),
&config_to_set.hostname,
),
prompt_with_default_and_save_to_existing_file(
OGMIOS_PORT,
Some(&default_config.port.to_string()),
&config_to_set.port.to_string(),
),
])
}
}
115 changes: 115 additions & 0 deletions toolkit/partner-chains-cli/src/ogmios/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::IOContext;
use anyhow::anyhow;
use jsonrpsee::http_client::HttpClient;
use ogmios_client::{
query_ledger_state::QueryLedgerState, query_network::QueryNetwork, types::OgmiosUtxo,
};
use sidechain_domain::NetworkType;

pub(crate) mod config;

#[derive(Debug, Eq, PartialEq)]
pub enum OgmiosRequest {
QueryLedgerStateEraSummaries,
Expand Down Expand Up @@ -139,3 +142,115 @@ impl TryFrom<ogmios_client::query_network::ShelleyGenesisConfigurationResponse>
})
}
}

pub(crate) fn get_shelley_config<C: IOContext>(
addr: &str,
context: &C,
) -> anyhow::Result<ShelleyGenesisConfiguration> {
let response = context.ogmios_rpc(addr, OgmiosRequest::QueryNetworkShelleyGenesis)?;
match response {
OgmiosResponse::QueryNetworkShelleyGenesis(shelley_config) => Ok(shelley_config),
other => Err(anyhow::anyhow!(format!("Unexpected response from Ogmios when quering for shelley genesis configuration: {other:?}"))),
}
}

#[cfg(test)]
pub(crate) mod test_values {
use crate::ogmios::EpochParameters;

use super::ShelleyGenesisConfiguration;
use super::{EpochBoundary, EraSummary};
use sidechain_domain::NetworkType;

pub(crate) fn preprod_eras_summaries() -> Vec<EraSummary> {
vec![
EraSummary {
start: EpochBoundary { time_seconds: 0, slot: 0, epoch: 0 },
parameters: EpochParameters { epoch_length: 21600, slot_length_millis: 20000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 1728000, slot: 86400, epoch: 4 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 2160000, slot: 518400, epoch: 5 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 2592000, slot: 950400, epoch: 6 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 3024000, slot: 1382400, epoch: 7 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 5184000, slot: 3542400, epoch: 12 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 5184000, slot: 3542400, epoch: 12 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 70416000, slot: 68774400, epoch: 163 },
parameters: EpochParameters { epoch_length: 432000, slot_length_millis: 1000 },
},
]
}

pub(crate) fn preprod_shelley_config() -> ShelleyGenesisConfiguration {
ShelleyGenesisConfiguration {
security_parameter: 2160,
active_slots_coefficient: 0.05,
epoch_length: 432000,
slot_length_millis: 1000,
start_time: 1654041600,
network: NetworkType::Testnet,
}
}

pub(crate) fn preview_eras_summaries() -> Vec<EraSummary> {
vec![
EraSummary {
start: EpochBoundary { time_seconds: 0, slot: 0, epoch: 0 },
parameters: EpochParameters { epoch_length: 4320, slot_length_millis: 20000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 0, slot: 0, epoch: 0 },
parameters: EpochParameters { epoch_length: 86400, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 0, slot: 0, epoch: 0 },
parameters: EpochParameters { epoch_length: 86400, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 0, slot: 0, epoch: 0 },
parameters: EpochParameters { epoch_length: 86400, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 0, slot: 0, epoch: 0 },
parameters: EpochParameters { epoch_length: 86400, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 259200, slot: 259200, epoch: 3 },
parameters: EpochParameters { epoch_length: 86400, slot_length_millis: 1000 },
},
EraSummary {
start: EpochBoundary { time_seconds: 55814400, slot: 55814400, epoch: 646 },
parameters: EpochParameters { epoch_length: 86400, slot_length_millis: 1000 },
},
]
}

pub(crate) fn preview_shelley_config() -> ShelleyGenesisConfiguration {
ShelleyGenesisConfiguration {
security_parameter: 432,
active_slots_coefficient: 0.05,
epoch_length: 86400,
slot_length_millis: 1000,
start_time: 1666656000,
network: NetworkType::Testnet,
}
}
}
Loading

0 comments on commit 3a44c72

Please sign in to comment.