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
  • Loading branch information
LGLO committed Dec 10, 2024
1 parent c60c8c3 commit 38b835d
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 155 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,5 +14,3 @@ 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 }
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
28 changes: 1 addition & 27 deletions toolkit/cli/smart-contracts-commands/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
use log4rs::{
append::{console::ConsoleAppender, file::FileAppender},
config::Appender,
};
use sidechain_domain::MainchainPrivateKey;

pub mod get_scripts;
Expand All @@ -23,7 +19,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 @@ -38,8 +34,6 @@ impl SmartContractsCmd {
}

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

tokio::runtime::Runtime::new()?.block_on(self.execute())
}
}
Expand All @@ -59,23 +53,3 @@ pub(crate) fn read_private_key_from_file(path: &str) -> CmdResult<MainchainPriva
.map_err(|_| format!("{} is not the valid lengh of 32", key_hex))?;
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(())
}
17 changes: 0 additions & 17 deletions toolkit/cli/smart-contracts-commands/src/main.rs

This file was deleted.

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
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
20 changes: 0 additions & 20 deletions toolkit/partner-chains-cli/src/select_utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,6 @@ pub(crate) mod tests {
]
}

pub(crate) fn mock_result_0_valid() -> Vec<OgmiosUtxo> {
vec![OgmiosUtxo {
transaction: OgmiosTx {
id: hex!("8a0d3e5644b3e84a775556b44e6407971d01b8bfa3f339294b7228ac18ddb29c"),
},
index: 0,
value: OgmiosValue {
lovelace: 10000000,
native_tokens: HashMap::from([(
hex!("244d83c5418732113e891db15ede8f0d15df75b705a1542d86937875"),
vec![Asset {
name: hex!("4c757854657374546f6b656e54727932").to_vec(),
amount: 1,
}],
)]),
},
..Default::default()
}]
}

pub(crate) fn query_utxos_io(
cardano_addr: &str,
ogmios_addr: &'static str,
Expand Down
8 changes: 7 additions & 1 deletion toolkit/utils/ogmios-client/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Common types used in the Ogmios API.
use serde::{Deserialize, Deserializer};
use sidechain_domain::McTxHash;
use sidechain_domain::{McTxHash, UtxoId};
use std::collections::HashMap;
use std::fmt::Debug;
use std::str::FromStr;
Expand Down Expand Up @@ -34,6 +34,12 @@ pub struct OgmiosUtxo {
pub script: Option<OgmiosScript>,
}

impl OgmiosUtxo {
pub fn utxo_id(&self) -> UtxoId {
UtxoId::new(self.transaction.id, self.index)
}
}

impl core::fmt::Display for OgmiosUtxo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}#{}", hex::encode(self.transaction.id), self.index)
Expand Down

0 comments on commit 38b835d

Please sign in to comment.