From 2a10d8f302a84d7381493f862059a4214b3fadeb Mon Sep 17 00:00:00 2001 From: Krisztian Pinter <159046756+kpinter-iohk@users.noreply.github.com> Date: Wed, 11 Dec 2024 13:08:35 +0100 Subject: [PATCH] ETCM-8754: fix register command (#320) --- toolkit/offchain/src/register.rs | 46 +++++++++++---------- toolkit/offchain/tests/integration_tests.rs | 1 - toolkit/primitives/domain/src/lib.rs | 10 +++++ 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/toolkit/offchain/src/register.rs b/toolkit/offchain/src/register.rs index 9caed27eb..22431ed5f 100644 --- a/toolkit/offchain/src/register.rs +++ b/toolkit/offchain/src/register.rs @@ -82,17 +82,16 @@ pub async fn run_register< &all_registration_utxos, ); - if own_registrations - .iter() - .any(|(_, existing_registration)| candidate_registration == existing_registration) - { + if own_registrations.iter().any(|(_, existing_registration)| { + candidate_registration.matches_keys(existing_registration) + }) { log::info!("✅ Candidate already registered with same keys."); return Ok(None); } let own_registration_utxos = own_registrations.iter().map(|r| r.0.clone()).collect::>(); let zero_ex_units = ExUnits::new(&0u64.into(), &0u64.into()); - let tx = register_tx( + let mut tx = register_tx( &validator, candidate_registration, registration_utxo, @@ -101,23 +100,26 @@ pub async fn run_register< zero_ex_units, )?; - let evaluate_response = - ogmios_client.evaluate_transaction(&tx.to_bytes()).await.map_err(|e| { - anyhow!( - "Evaluate candidate registration transaction request failed: {}, bytes: {}", - e, - hex::encode(tx.to_bytes()) - ) - })?; - let validator_redeemer_ex_units = get_first_validator_budget(evaluate_response)?; - let tx = register_tx( - &validator, - candidate_registration, - registration_utxo, - &own_registration_utxos, - &ctx, - validator_redeemer_ex_units, - )?; + if !own_registration_utxos.is_empty() { + let evaluate_response = + ogmios_client.evaluate_transaction(&tx.to_bytes()).await.map_err(|e| { + anyhow!( + "Evaluate candidate registration transaction request failed: {}, bytes: {}", + e, + hex::encode(tx.to_bytes()) + ) + })?; + let validator_redeemer_ex_units = get_first_validator_budget(evaluate_response)?; + tx = register_tx( + &validator, + candidate_registration, + registration_utxo, + &own_registration_utxos, + &ctx, + validator_redeemer_ex_units, + )?; + } + let signed_tx = ctx.sign(&tx).to_bytes(); let result = ogmios_client.submit_transaction(&signed_tx).await.map_err(|e| { anyhow!( diff --git a/toolkit/offchain/tests/integration_tests.rs b/toolkit/offchain/tests/integration_tests.rs index 86cf7489d..b13532a31 100644 --- a/toolkit/offchain/tests/integration_tests.rs +++ b/toolkit/offchain/tests/integration_tests.rs @@ -76,7 +76,6 @@ async fn upsert_d_param() { assert!(run_upsert_d_param(genesis_utxo, 1, 1, &client).await.is_some()) } -#[ignore = "Internal error: cannot use evaluateTransaction response"] #[tokio::test] async fn register() { let _ = env_logger::builder().is_test(true).try_init(); diff --git a/toolkit/primitives/domain/src/lib.rs b/toolkit/primitives/domain/src/lib.rs index 5063b3cac..9370a314e 100644 --- a/toolkit/primitives/domain/src/lib.rs +++ b/toolkit/primitives/domain/src/lib.rs @@ -635,6 +635,16 @@ pub struct CandidateRegistration { pub grandpa_pub_key: GrandpaPublicKey, } +impl CandidateRegistration { + pub fn matches_keys(&self, other: &Self) -> bool { + self.stake_ownership == other.stake_ownership + && self.partnerchain_pub_key == other.partnerchain_pub_key + && self.partnerchain_signature == other.partnerchain_signature + && self.aura_pub_key == other.aura_pub_key + && self.grandpa_pub_key == other.grandpa_pub_key + } +} + /// AdaBasedStaking is a variant of Plutus type StakeOwnership. /// The other variant, TokenBasedStaking, is not supported #[derive(Clone, Debug, PartialEq, Eq)]