Skip to content

Commit

Permalink
fix: crash of partner-chains-node smart-contracts subcommand, removal…
Browse files Browse the repository at this point in the history
… of dead code, missing return of selected genesis-utxo in init-goveranance (#312)
  • Loading branch information
LGLO authored Dec 10, 2024
1 parent de7662d commit eaca09d
Show file tree
Hide file tree
Showing 17 changed files with 61 additions and 140 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ This changelog is based on [Keep A Changelog](https://keepachangelog.com/en/1.1.

## Removed

* Separate binary partner-chains-smart-contracts-commands.

## Fixed

* Crash of parnter-chain-node smart-contracts command. Logging is now set independently.
* Renamed of argument 'ogmios-host' to 'ogmios-url' in smart-contracts subcommands.

## Added

# v1.4.0
Expand Down
1 change: 1 addition & 0 deletions node/node/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn run() -> sc_cli::Result<()> {
match &cli.subcommand {
Some(Subcommand::Key(cmd)) => cmd.run(&cli),
Some(Subcommand::PartnerChains(cmd)) => {
partner_chains_node_commands::setup_log4rs()?;
let make_dependencies = |config| {
let components = service::new_partial(&config)?;
Ok((
Expand Down
2 changes: 2 additions & 0 deletions toolkit/cli/node-commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ clap = { workspace = true }
cli-commands = { workspace = true }
env_logger = { workspace = true }
frame-support = { workspace = true }
log = { workspace = true }
log4rs = { workspace = true }
parity-scale-codec = { workspace = true }
partner-chains-smart-contracts-commands = { workspace = true }
plutus = { workspace = true }
Expand Down
23 changes: 22 additions & 1 deletion toolkit/cli/node-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ where
SessionKeys: Decode + Send + Sync + 'static,
CrossChainPublic: Decode + Encode + AsRef<[u8]> + Send + Sync + 'static,
{
env_logger::init();
match cmd {
PartnerChainsSubcommand::SidechainParams(cmd) => {
let runner = cli.create_runner(&cmd)?;
Expand Down Expand Up @@ -158,6 +157,28 @@ where
}
}

/// This sets logging in a very opinionated way.
/// Because Rust env_logger clashes with log4rs, this is exposed to be invoked by users of smart-contracts commands.
pub fn setup_log4rs() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let stdout = log4rs::append::console::ConsoleAppender::builder().build();
let ogmios_log = log4rs::append::file::FileAppender::builder().build("ogmios_client.log")?;

let log_config = log4rs::config::Config::builder()
.appender(log4rs::config::Appender::builder().build("stdout", Box::new(stdout)))
.appender(log4rs::config::Appender::builder().build("ogmios-log", Box::new(ogmios_log)))
.logger(
log4rs::config::Logger::builder()
.appender("ogmios-log")
.additive(false)
.build("ogmios_client::jsonrpsee", log::LevelFilter::Debug),
)
.build(log4rs::config::Root::builder().appender("stdout").build(log::LevelFilter::Info))?;

log4rs::init_config(log_config)?;

Ok(())
}

async fn print_result<FIn>(command_future: FIn) -> Result<(), sc_cli::Error>
where
FIn: Future<Output = Result<String, String>>,
Expand Down
2 changes: 0 additions & 2 deletions toolkit/cli/smart-contracts-commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ jsonrpsee = { workspace = true, features = ["client-core", "http-client", "macro
tokio = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
log4rs = { workspace = true }
cardano-serialization-lib = { workspace = true }
2 changes: 1 addition & 1 deletion toolkit/cli/smart-contracts-commands/src/d_parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl UpsertDParameterCmd {
num_permissioned_candidates: self.permissioned_candidates_count,
num_registered_candidates: self.registered_candidates_count,
};
let client = HttpClient::builder().build(self.common_arguments.ogmios_host)?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;

upsert_d_param(self.genesis_utxo, &d_param, payment_key.0, &client).await?;

Expand Down
3 changes: 2 additions & 1 deletion toolkit/cli/smart-contracts-commands/src/get_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ use sidechain_domain::UtxoId;
pub struct GetScripts {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
/// Genesis UTXO that identifies the partner chain.
#[arg(long, short = 'c')]
genesis_utxo: UtxoId,
}

impl GetScripts {
pub async fn execute(self) -> crate::CmdResult<()> {
let client = HttpClient::builder().build(self.common_arguments.ogmios_host)?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;
let scripts_data = get_scripts_data_with_ogmios(self.genesis_utxo, client).await?;

let json = serde_json::to_string_pretty(&scripts_data)?;
Expand Down
5 changes: 4 additions & 1 deletion toolkit/cli/smart-contracts-commands/src/init_governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ use crate::read_private_key_from_file;
pub struct InitGovernanceCmd {
#[clap(flatten)]
common_arguments: crate::CommonArguments,
/// Governance authority hash to be set.
#[arg(long, short = 'g')]
governance_authority: MainchainAddressHash,
/// Path to the Cardano Payment Key file.
#[arg(long, short = 'k')]
payment_key_file: String,
/// Genesis UTXO of the new chain, it will be spent durning initialization. If not set, then one will be selected from UTXOs of the payment key.
#[arg(long, short = 'c')]
genesis_utxo: Option<UtxoId>,
}

impl InitGovernanceCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = read_private_key_from_file(&self.payment_key_file)?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_host)?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;

run_init_governance(
self.governance_authority,
Expand Down
30 changes: 2 additions & 28 deletions toolkit/cli/smart-contracts-commands/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use log4rs::{
append::{console::ConsoleAppender, file::FileAppender},
config::Appender,
};
use sidechain_domain::*;

pub mod get_scripts;
pub mod d_parameter;
pub mod get_scripts;
pub mod init_governance;
pub mod register;

Expand All @@ -26,7 +22,7 @@ pub enum SmartContractsCmd {
#[command(author, version, about, long_about = None)]
pub struct CommonArguments {
#[arg(default_value = "http://localhost:1337", long, short = 'O')]
ogmios_host: String,
ogmios_url: String,
}

type CmdResult<T> = Result<T, Box<dyn std::error::Error + Send + Sync>>;
Expand All @@ -42,8 +38,6 @@ impl SmartContractsCmd {
}

pub fn execute_blocking(self) -> CmdResult<()> {
setup_logging()?;

tokio::runtime::Runtime::new()?.block_on(self.execute())
}
}
Expand All @@ -64,26 +58,6 @@ pub(crate) fn read_private_key_from_file(path: &str) -> CmdResult<MainchainPriva
Ok(MainchainPrivateKey(key_bytes))
}

pub fn setup_logging() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let stdout = ConsoleAppender::builder().build();
let ogmios_log = FileAppender::builder().build("ogmios_client.log")?;

let log_config = log4rs::config::Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("ogmios-log", Box::new(ogmios_log)))
.logger(
log4rs::config::Logger::builder()
.appender("ogmios-log")
.additive(false)
.build("ogmios_client::jsonrpsee", log::LevelFilter::Debug),
)
.build(log4rs::config::Root::builder().appender("stdout").build(log::LevelFilter::Info))?;

log4rs::init_config(log_config)?;

Ok(())
}

// Parses public keys in formatted as SIDECHAIN_KEY:AURA_KEY:GRANDPA_KEY
pub(crate) fn parse_partnerchain_public_keys(
partner_chain_public_keys: &str,
Expand Down
17 changes: 0 additions & 17 deletions toolkit/cli/smart-contracts-commands/src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion toolkit/cli/smart-contracts-commands/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct RegisterCmd {
impl RegisterCmd {
pub async fn execute(self) -> crate::CmdResult<()> {
let payment_key = read_private_key_from_file(&self.payment_key_file)?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_host)?;
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;
let candidate_registration = CandidateRegistration {
stake_ownership: AdaBasedStaking {
pub_key: self.spo_public_key,
Expand Down
77 changes: 0 additions & 77 deletions toolkit/offchain/src/collateral_selection.rs

This file was deleted.

10 changes: 7 additions & 3 deletions toolkit/offchain/src/init_governance/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::csl::OgmiosUtxoExt;
use crate::scripts_data;
use crate::{
await_tx::{AwaitTx, FixedDelayRetries},
Expand All @@ -21,6 +22,8 @@ mod tests;
pub(crate) mod transaction;

pub trait InitGovernance {
/// Initializes goveranance mechanism with the authority being `governance_authority`,
/// for the chain identified by `genesis_utxo_id`.
#[allow(async_fn_in_trait)]
async fn init_governance(
&self,
Expand Down Expand Up @@ -48,6 +51,7 @@ where
FixedDelayRetries::two_minutes(),
)
.await
.map(|(_, tx)| tx)
.map_err(|e| OffchainError::InternalError(e.to_string()))
}
}
Expand All @@ -61,7 +65,7 @@ pub async fn run_init_governance<
genesis_utxo_id: Option<UtxoId>,
client: &T,
await_tx: A,
) -> anyhow::Result<OgmiosTx> {
) -> anyhow::Result<(UtxoId, OgmiosTx)> {
let payment_key = PrivateKey::from_normal_bytes(&payment_key.0)
.expect("MainchainPrivateKey is a valid PrivateKey");

Expand Down Expand Up @@ -116,7 +120,7 @@ pub async fn run_init_governance<
raw_scripts::VERSION_ORACLE_POLICY,
governance_authority,
&tx_context,
genesis_utxo,
genesis_utxo.clone(),
cost,
)?;
let signed_transaction = tx_context.sign(&unsigned_transaction);
Expand All @@ -128,7 +132,7 @@ pub async fn run_init_governance<
.await_tx_output(client, UtxoId { tx_hash: McTxHash(tx_id), index: UtxoIndex(0) })
.await?;

Ok(result.transaction)
Ok((genesis_utxo.to_domain(), result.transaction))
}

pub async fn get_governance_utxo<T: QueryLedgerState + Transactions + QueryNetwork>(
Expand Down
8 changes: 5 additions & 3 deletions toolkit/offchain/src/init_governance/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,19 @@ async fn transaction_run() {
}])
.with_submit_result(SubmitTransactionResponse { transaction });

let result = run_init_governance(
let genesis_utxo = genesis_utxo().to_domain();
let (result_genesis_utxo, result_tx) = run_init_governance(
governance_authority(),
payment_key_domain(),
Some(genesis_utxo().to_domain()),
Some(genesis_utxo),
&mock_client,
ImmediateSuccess,
)
.await
.expect("Should succeed");

assert_eq!(result.id, transaction_id);
assert_eq!(result_tx.id, transaction_id);
assert_eq!(result_genesis_utxo, genesis_utxo);
}

fn genesis_utxo() -> OgmiosUtxo {
Expand Down
2 changes: 0 additions & 2 deletions toolkit/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
/// Primitives used for awaiting for tx being observed on the blockchain
pub mod await_tx;
/// Collateral selection algorithm
pub mod collateral_selection;
/// General purpose code for interacting with cardano-serialization-lib
pub mod csl;
/// Supports D-Parameter upsert
Expand Down
Loading

0 comments on commit eaca09d

Please sign in to comment.