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

fix: await upsert-d-parameter and upsert-permissioned-candidates transaction outputs being present #360

Merged
merged 1 commit into from
Jan 10, 2025
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
10 changes: 9 additions & 1 deletion toolkit/cli/smart-contracts-commands/src/d_parameter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::PaymentFilePath;
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::await_tx::FixedDelayRetries;
use partner_chains_cardano_offchain::d_param::upsert_d_param;
use sidechain_domain::DParameter;
use sidechain_domain::UtxoId;
Expand Down Expand Up @@ -27,7 +28,14 @@ impl UpsertDParameterCmd {
};
let client = HttpClient::builder().build(self.common_arguments.ogmios_url)?;

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

Ok(())
}
Expand Down
35 changes: 26 additions & 9 deletions toolkit/offchain/src/d_param/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! The datum encodes D-parameter using VersionedGenericDatum envelope with the D-parameter being
//! `datum` field being `[num_permissioned_candidates, num_registered_candidates]`.

use crate::await_tx::{AwaitTx, FixedDelayRetries};
use crate::csl::{
get_builder_config, get_validator_budgets, zero_ex_units, InputsBuilderExt, ScriptExUnits,
TransactionBuilderExt, TransactionContext,
Expand All @@ -14,6 +15,7 @@ use anyhow::anyhow;
use cardano_serialization_lib::{
ExUnits, JsError, PlutusData, ScriptHash, Transaction, TransactionBuilder, TxInputsBuilder,
};
use ogmios_client::query_ledger_state::QueryUtxoByUtxoId;
use ogmios_client::{
query_ledger_state::QueryLedgerState, query_network::QueryNetwork, transactions::Transactions,
types::OgmiosUtxo,
Expand All @@ -35,36 +37,47 @@ pub trait UpsertDParam {
) -> anyhow::Result<Option<McTxHash>>;
}

impl<C: QueryLedgerState + QueryNetwork + Transactions> UpsertDParam for C {
impl<C: QueryLedgerState + QueryNetwork + Transactions + QueryUtxoByUtxoId> UpsertDParam for C {
async fn upsert_d_param(
&self,
genesis_utxo: UtxoId,
d_parameter: &DParameter,
payment_signing_key: [u8; 32],
) -> anyhow::Result<Option<McTxHash>> {
upsert_d_param(genesis_utxo, d_parameter, payment_signing_key, self).await
upsert_d_param(
genesis_utxo,
d_parameter,
payment_signing_key,
self,
&FixedDelayRetries::two_minutes(),
)
.await
}
}

pub async fn upsert_d_param<C: QueryLedgerState + QueryNetwork + Transactions>(
pub async fn upsert_d_param<
C: QueryLedgerState + QueryNetwork + Transactions + QueryUtxoByUtxoId,
A: AwaitTx,
>(
genesis_utxo: UtxoId,
d_parameter: &DParameter,
payment_signing_key: [u8; 32],
ogmios_client: &C,
await_tx: &A,
) -> anyhow::Result<Option<McTxHash>> {
let ctx = TransactionContext::for_payment_key(payment_signing_key, ogmios_client).await?;
let (validator, policy) = crate::scripts_data::d_parameter_scripts(genesis_utxo, ctx.network)?;
let validator_address = validator.address_bech32(ctx.network)?;
let validator_utxos = ogmios_client.query_utxos(&[validator_address]).await?;

match get_current_d_parameter(validator_utxos)? {
let tx_hash_opt = match get_current_d_parameter(validator_utxos)? {
Some((_, current_d_param)) if current_d_param == *d_parameter => {
log::info!("Current D-parameter value is equal to the one to be set.");
Ok(None)
None
},
Some((current_utxo, _)) => {
log::info!("Current D-parameter is different to the one to be set. Updating.");
Ok(Some(
Some(
update_d_param(
&validator,
&policy,
Expand All @@ -75,16 +88,20 @@ pub async fn upsert_d_param<C: QueryLedgerState + QueryNetwork + Transactions>(
ogmios_client,
)
.await?,
))
)
},
None => {
log::info!("There is no D-parameter set. Inserting new one.");
Ok(Some(
Some(
insert_d_param(&validator, &policy, d_parameter, ctx, genesis_utxo, ogmios_client)
.await?,
))
)
},
};
if let Some(tx_hash) = tx_hash_opt {
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_hash.0, 0)).await?;
}
Ok(tx_hash_opt)
}

fn get_current_d_parameter(
Expand Down
38 changes: 28 additions & 10 deletions toolkit/offchain/src/permissioned_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use std::collections::HashMap;

use crate::await_tx::{AwaitTx, FixedDelayRetries};
use crate::csl::{
convert_value, empty_asset_name, get_builder_config, get_validator_budgets, zero_ex_units,
OgmiosUtxoExt, TransactionBuilderExt, TransactionContext,
Expand All @@ -21,7 +22,7 @@ use cardano_serialization_lib::{
PlutusWitness, Redeemer, RedeemerTag, ScriptHash, Transaction, TransactionBuilder,
TxInputsBuilder,
};
use ogmios_client::query_ledger_state::QueryLedgerState;
use ogmios_client::query_ledger_state::{QueryLedgerState, QueryUtxoByUtxoId};
use ogmios_client::query_network::QueryNetwork;
use ogmios_client::transactions::Transactions;
use ogmios_client::types::OgmiosUtxo;
Expand All @@ -40,22 +41,35 @@ pub trait UpsertPermissionedCandidates {
) -> anyhow::Result<Option<McTxHash>>;
}

impl<C: QueryLedgerState + QueryNetwork + Transactions> UpsertPermissionedCandidates for C {
impl<C: QueryLedgerState + QueryNetwork + Transactions + QueryUtxoByUtxoId>
UpsertPermissionedCandidates for C
{
async fn upsert_permissioned_candidates(
&self,
genesis_utxo: UtxoId,
candidates: &[PermissionedCandidateData],
payment_signing_key: [u8; 32],
) -> anyhow::Result<Option<McTxHash>> {
upsert_permissioned_candidates(genesis_utxo, candidates, payment_signing_key, self).await
upsert_permissioned_candidates(
genesis_utxo,
candidates,
payment_signing_key,
self,
&FixedDelayRetries::two_minutes(),
)
.await
}
}

pub async fn upsert_permissioned_candidates<C: QueryLedgerState + QueryNetwork + Transactions>(
pub async fn upsert_permissioned_candidates<
C: QueryLedgerState + QueryNetwork + Transactions + QueryUtxoByUtxoId,
A: AwaitTx,
>(
genesis_utxo: UtxoId,
candidates: &[PermissionedCandidateData],
payment_signing_key: [u8; 32],
ogmios_client: &C,
await_tx: &A,
) -> anyhow::Result<Option<McTxHash>> {
let ctx = TransactionContext::for_payment_key(payment_signing_key, ogmios_client).await?;
let (validator, policy) =
Expand All @@ -66,18 +80,18 @@ pub async fn upsert_permissioned_candidates<C: QueryLedgerState + QueryNetwork +
let mut candidates = candidates.to_owned();
candidates.sort();

match get_current_permissioned_candidates(validator_utxos)? {
let tx_hash_opt = match get_current_permissioned_candidates(validator_utxos)? {
Some((_, current_permissioned_candidates))
if current_permissioned_candidates == *candidates =>
{
log::info!("Current permissioned candidates are equal to the one to be set.");
Ok(None)
None
},
Some((current_utxo, _)) => {
log::info!(
"Current permissioned candidates are different to the one to be set. Updating."
);
Ok(Some(
Some(
update_permissioned_candidates(
&validator,
&policy,
Expand All @@ -88,11 +102,11 @@ pub async fn upsert_permissioned_candidates<C: QueryLedgerState + QueryNetwork +
ogmios_client,
)
.await?,
))
)
},
None => {
log::info!("There are permissioned candidates. Inserting new ones.");
Ok(Some(
Some(
insert_permissioned_candidates(
&validator,
&policy,
Expand All @@ -102,9 +116,13 @@ pub async fn upsert_permissioned_candidates<C: QueryLedgerState + QueryNetwork +
ogmios_client,
)
.await?,
))
)
},
};
if let Some(tx_hash) = tx_hash_opt {
await_tx.await_tx_output(ogmios_client, UtxoId::new(tx_hash.0, 0)).await?;
}
Ok(tx_hash_opt)
}

fn get_current_permissioned_candidates(
Expand Down
2 changes: 2 additions & 0 deletions toolkit/offchain/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ async fn run_upsert_d_param<
&DParameter { num_permissioned_candidates, num_registered_candidates },
pkey.0,
client,
&FixedDelayRetries::new(Duration::from_millis(500), 100),
)
.await
.unwrap();
Expand Down Expand Up @@ -253,6 +254,7 @@ async fn run_upsert_permissioned_candidates<
&candidates,
GOVERNANCE_AUTHORITY_PAYMENT_KEY.0,
client,
&FixedDelayRetries::new(Duration::from_millis(500), 100),
)
.await
.unwrap();
Expand Down
Loading