Skip to content

Commit

Permalink
review comments: use candidate naming consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
kpinter-iohk committed Dec 6, 2024
1 parent 79488f9 commit ac2988a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
8 changes: 4 additions & 4 deletions toolkit/cli/smart-contracts-commands/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use jsonrpsee::http_client::HttpClient;
use partner_chains_cardano_offchain::{await_tx::FixedDelayRetries, register::run_register};
use sidechain_domain::{
AdaBasedStaking, AuraPublicKey, BlockProducerRegistration, GrandpaPublicKey,
MainchainPublicKey, MainchainSignature, SidechainPublicKey, SidechainSignature, UtxoId,
AdaBasedStaking, AuraPublicKey, CandidateRegistration, GrandpaPublicKey, MainchainPublicKey,
MainchainSignature, SidechainPublicKey, SidechainSignature, UtxoId,
};

use crate::{parse_sidechain_public_keys, read_private_key_from_file};
Expand Down Expand Up @@ -31,7 +31,7 @@ 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 block_producer_registration = BlockProducerRegistration {
let candidate_registration = CandidateRegistration {
stake_ownership: AdaBasedStaking {
pub_key: self.spo_public_key,
signature: self.spo_signature,
Expand All @@ -46,7 +46,7 @@ impl RegisterCmd {

run_register(
self.genesis_utxo,
&block_producer_registration,
&candidate_registration,
payment_key,
&client,
FixedDelayRetries::two_minutes(),
Expand Down
64 changes: 31 additions & 33 deletions toolkit/offchain/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ogmios_client::{
types::OgmiosUtxo,
};
use partner_chains_plutus_data::registered_candidates::{
block_producer_registration_to_plutus_data, RegisterValidatorDatum,
candidate_registration_to_plutus_data, RegisterValidatorDatum,
};
use sidechain_domain::*;

Expand All @@ -31,7 +31,7 @@ pub trait Register {
async fn register(
&self,
genesis_utxo: UtxoId,
block_producer_registration: &BlockProducerRegistration,
candidate_registration: &CandidateRegistration,
payment_signing_key: MainchainPrivateKey,
) -> Result<Option<OgmiosTx>, OffchainError>;
}
Expand All @@ -43,12 +43,12 @@ where
async fn register(
&self,
genesis_utxo: UtxoId,
block_producer_registration: &BlockProducerRegistration,
candidate_registration: &CandidateRegistration,
payment_signing_key: MainchainPrivateKey,
) -> Result<Option<OgmiosTx>, OffchainError> {
run_register(
genesis_utxo,
block_producer_registration,
candidate_registration,
payment_signing_key,
self,
FixedDelayRetries::two_minutes(),
Expand All @@ -63,7 +63,7 @@ pub async fn run_register<
A: AwaitTx,
>(
genesis_utxo: UtxoId,
block_producer_registration: &BlockProducerRegistration,
candidate_registration: &CandidateRegistration,
payment_signing_key: MainchainPrivateKey,
ogmios_client: &C,
await_tx: A,
Expand All @@ -74,28 +74,28 @@ pub async fn run_register<
let registration_utxo = ctx
.payment_key_utxos
.iter()
.find(|u| u.to_domain() == block_producer_registration.registration_utxo)
.find(|u| u.to_domain() == candidate_registration.registration_utxo)
.ok_or(anyhow!("registration utxo not found at payment address"))?;
let all_registration_utxos = ogmios_client.query_utxos(&[validator_address]).await?;
let own_registrations = get_own_registrations(
block_producer_registration.own_pkh,
block_producer_registration.stake_ownership.pub_key.clone(),
candidate_registration.own_pkh,
candidate_registration.stake_ownership.pub_key.clone(),
&all_registration_utxos,
)?;
let own_registration_utxos = own_registrations.iter().map(|r| r.0.clone()).collect::<Vec<_>>();

if own_registrations
.iter()
.any(|(_, existing_registration)| block_producer_registration == existing_registration)
.any(|(_, existing_registration)| candidate_registration == existing_registration)
{
log::info!("✅ Block producer already registered with same keys.");
log::info!("✅ Candidate already registered with same keys.");
return Ok(None);
}

let zero_ex_units = ExUnits::new(&0u64.into(), &0u64.into());
let tx = register_tx(
&validator,
block_producer_registration,
candidate_registration,
registration_utxo,
&own_registration_utxos,
&ctx,
Expand All @@ -113,7 +113,7 @@ pub async fn run_register<
let validator_redeemer_ex_units = get_first_validator_budget(evaluate_response)?;
let tx = register_tx(
&validator,
block_producer_registration,
candidate_registration,
registration_utxo,
&own_registration_utxos,
&ctx,
Expand All @@ -140,7 +140,7 @@ fn get_own_registrations(
own_pkh: MainchainAddressHash,
spo_pub_key: MainchainPublicKey,
validator_utxos: &[OgmiosUtxo],
) -> Result<Vec<(OgmiosUtxo, BlockProducerRegistration)>, anyhow::Error> {
) -> Result<Vec<(OgmiosUtxo, CandidateRegistration)>, anyhow::Error> {
let mut own_registrations = Vec::new();
for validator_utxo in validator_utxos {
let datum = validator_utxo.datum.clone().ok_or_else(|| {
Expand All @@ -149,24 +149,24 @@ fn get_own_registrations(
let datum_plutus_data = PlutusData::from_bytes(datum.bytes).map_err(|e| {
anyhow!("Internal error: could not decode datum of validator script: {}", e)
})?;
let block_producer_registration: BlockProducerRegistration =
let candidate_registration: CandidateRegistration =
RegisterValidatorDatum::try_from(datum_plutus_data)
.map_err(|e| {
anyhow!("Internal error: could not decode datum of validator script: {}", e)
})?
.into();
if block_producer_registration.stake_ownership.pub_key == spo_pub_key
&& block_producer_registration.own_pkh == own_pkh
if candidate_registration.stake_ownership.pub_key == spo_pub_key
&& candidate_registration.own_pkh == own_pkh
{
own_registrations.push((validator_utxo.clone(), block_producer_registration))
own_registrations.push((validator_utxo.clone(), candidate_registration))
}
}
Ok(own_registrations)
}

fn register_tx(
validator: &PlutusScript,
block_producer_registration: &BlockProducerRegistration,
candidate_registration: &CandidateRegistration,
registration_utxo: &OgmiosUtxo,
own_registration_utxos: &[OgmiosUtxo],
ctx: &TransactionContext,
Expand All @@ -189,7 +189,7 @@ fn register_tx(
}

{
let datum = block_producer_registration_to_plutus_data(block_producer_registration);
let datum = candidate_registration_to_plutus_data(candidate_registration);
let amount_builder = TransactionOutputBuilder::new()
.with_address(&validator.address(ctx.network))
.with_plutus_data(&datum)
Expand Down Expand Up @@ -219,15 +219,15 @@ mod tests {
};
use ogmios_client::types::OgmiosValue;
use ogmios_client::types::{OgmiosTx, OgmiosUtxo};
use partner_chains_plutus_data::registered_candidates::block_producer_registration_to_plutus_data;
use partner_chains_plutus_data::registered_candidates::candidate_registration_to_plutus_data;
use proptest::{
array::uniform32,
collection::{hash_set, vec},
prelude::*,
};

use sidechain_domain::{
AdaBasedStaking, AuraPublicKey, BlockProducerRegistration, GrandpaPublicKey,
AdaBasedStaking, AuraPublicKey, CandidateRegistration, GrandpaPublicKey,
MainchainAddressHash, MainchainSignature, McTxHash, SidechainPublicKey, SidechainSignature,
UtxoId, UtxoIndex,
};
Expand All @@ -242,8 +242,8 @@ mod tests {
fn own_pkh() -> MainchainAddressHash {
MainchainAddressHash([0; 28])
}
fn block_producer_registration(registration_utxo: UtxoId) -> BlockProducerRegistration {
BlockProducerRegistration {
fn candidate_registration(registration_utxo: UtxoId) -> CandidateRegistration {
CandidateRegistration {
stake_ownership: AdaBasedStaking {
pub_key: test_values::mainchain_pub_key(),
signature: MainchainSignature(Vec::new()),
Expand Down Expand Up @@ -287,11 +287,10 @@ mod tests {
};
let own_registration_utxos = vec![payment_key_utxos.get(1).unwrap().clone()];
let registration_utxo = payment_key_utxos.first().unwrap();
let block_producer_registration =
block_producer_registration(registration_utxo.to_domain());
let candidate_registration = candidate_registration(registration_utxo.to_domain());
let tx = register_tx(
&test_values::test_validator(),
&block_producer_registration,
&candidate_registration,
registration_utxo,
&own_registration_utxos,
&ctx,
Expand Down Expand Up @@ -320,7 +319,7 @@ mod tests {
);
assert_eq!(
script_output.plutus_data().unwrap(),
block_producer_registration_to_plutus_data(&block_producer_registration)
candidate_registration_to_plutus_data(&candidate_registration)
);
}

Expand All @@ -333,16 +332,15 @@ mod tests {
protocol_parameters: protocol_parameters(),
};
let registration_utxo = payment_key_utxos.first().unwrap();
let block_producer_registration =
block_producer_registration(registration_utxo.to_domain());
let candidate_registration = candidate_registration(registration_utxo.to_domain());
let own_registration_utxos = if payment_utxos.len() >= 2 {
vec![payment_utxos.get(1).unwrap().clone()]
} else {
Vec::new()
};
let tx = register_tx(
&test_values::test_validator(),
&block_producer_registration,
&candidate_registration,
registration_utxo,
&own_registration_utxos,
&ctx,
Expand All @@ -356,7 +354,7 @@ mod tests {
fee_is_less_than_one_and_half_ada(&tx);
output_at_validator_has_register_candidate_datum(
&tx,
&block_producer_registration,
&candidate_registration,
validator_address,
);
spends_own_registration_utxos(&tx, &own_registration_utxos);
Expand Down Expand Up @@ -399,15 +397,15 @@ mod tests {

fn output_at_validator_has_register_candidate_datum(
tx: &Transaction,
block_producer_registration: &BlockProducerRegistration,
candidate_registration: &CandidateRegistration,
validator_address: &Address,
) {
let outputs = tx.body().outputs();
let validator_output =
outputs.into_iter().find(|o| o.address() == *validator_address).unwrap();
assert_eq!(
validator_output.plutus_data().unwrap(),
block_producer_registration_to_plutus_data(block_producer_registration)
candidate_registration_to_plutus_data(candidate_registration)
);
}

Expand Down
2 changes: 1 addition & 1 deletion toolkit/primitives/domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ pub struct PermissionedCandidateData {
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BlockProducerRegistration {
pub struct CandidateRegistration {
pub stake_ownership: AdaBasedStaking,
pub sidechain_pub_key: SidechainPublicKey,
pub sidechain_signature: SidechainSignature,
Expand Down
22 changes: 11 additions & 11 deletions toolkit/primitives/plutus-data/src/registered_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ impl VersionedDatum for RegisterValidatorDatum {
}
}

pub fn block_producer_registration_to_plutus_data(
block_producer_registration: &sidechain_domain::BlockProducerRegistration,
pub fn candidate_registration_to_plutus_data(
candidate_registration: &sidechain_domain::CandidateRegistration,
) -> PlutusData {
RegisterValidatorDatum::V0 {
stake_ownership: block_producer_registration.stake_ownership.clone(),
sidechain_pub_key: block_producer_registration.sidechain_pub_key.clone(),
sidechain_signature: block_producer_registration.sidechain_signature.clone(),
registration_utxo: block_producer_registration.registration_utxo,
own_pkh: block_producer_registration.own_pkh,
aura_pub_key: block_producer_registration.aura_pub_key.clone(),
grandpa_pub_key: block_producer_registration.grandpa_pub_key.clone(),
stake_ownership: candidate_registration.stake_ownership.clone(),
sidechain_pub_key: candidate_registration.sidechain_pub_key.clone(),
sidechain_signature: candidate_registration.sidechain_signature.clone(),
registration_utxo: candidate_registration.registration_utxo,
own_pkh: candidate_registration.own_pkh,
aura_pub_key: candidate_registration.aura_pub_key.clone(),
grandpa_pub_key: candidate_registration.grandpa_pub_key.clone(),
}
.into()
}

impl From<RegisterValidatorDatum> for BlockProducerRegistration {
impl From<RegisterValidatorDatum> for CandidateRegistration {
fn from(value: RegisterValidatorDatum) -> Self {
match value {
RegisterValidatorDatum::V0 {
Expand All @@ -101,7 +101,7 @@ impl From<RegisterValidatorDatum> for BlockProducerRegistration {
own_pkh,
aura_pub_key,
grandpa_pub_key,
} => BlockProducerRegistration {
} => CandidateRegistration {
stake_ownership,
sidechain_pub_key,
sidechain_signature,
Expand Down

0 comments on commit ac2988a

Please sign in to comment.