Skip to content

Commit

Permalink
Address wrap DelegationOutput::validator_address (#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Dec 22, 2023
1 parent e8de1e8 commit bf43541
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
30 changes: 19 additions & 11 deletions sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ impl DelegationOutputBuilder {

/// Finishes the builder into a [`DelegationOutput`] without parameters verification.
pub fn finish(self) -> Result<DelegationOutput, Error> {
verify_validator_address::<true>(&self.validator_address)?;
let validator_address = Address::from(self.validator_address);

verify_validator_address::<true>(&validator_address)?;

let unlock_conditions = UnlockConditions::from_set(self.unlock_conditions)?;

Expand All @@ -175,7 +177,7 @@ impl DelegationOutputBuilder {
amount: 0,
delegated_amount: self.delegated_amount,
delegation_id: self.delegation_id,
validator_address: self.validator_address,
validator_address,
start_epoch: self.start_epoch,
end_epoch: self.end_epoch,
unlock_conditions,
Expand All @@ -201,7 +203,7 @@ impl From<&DelegationOutput> for DelegationOutputBuilder {
amount: OutputBuilderAmount::Amount(output.amount),
delegated_amount: output.delegated_amount,
delegation_id: output.delegation_id,
validator_address: output.validator_address,
validator_address: *output.validator_address.as_account(),
start_epoch: output.start_epoch,
end_epoch: output.end_epoch,
unlock_conditions: output.unlock_conditions.iter().cloned().collect(),
Expand All @@ -222,7 +224,7 @@ pub struct DelegationOutput {
delegation_id: DelegationId,
/// Account address of the validator to which this output is delegating.
#[packable(verify_with = verify_validator_address_packable)]
validator_address: AccountAddress,
validator_address: Address,
/// Index of the first epoch for which this output delegates.
start_epoch: EpochIndex,
/// Index of the last epoch for which this output delegates.
Expand Down Expand Up @@ -281,7 +283,7 @@ impl DelegationOutput {

/// Returns the validator address of the [`DelegationOutput`].
pub fn validator_address(&self) -> &AccountAddress {
&self.validator_address
&self.validator_address.as_account()
}

/// Returns the start epoch of the [`DelegationOutput`].
Expand Down Expand Up @@ -350,16 +352,22 @@ impl WorkScore for DelegationOutput {

impl MinimumOutputAmount for DelegationOutput {}

fn verify_validator_address<const VERIFY: bool>(validator_address: &AccountAddress) -> Result<(), Error> {
if VERIFY && validator_address.is_null() {
Err(Error::NullDelegationValidatorId)
} else {
Ok(())
fn verify_validator_address<const VERIFY: bool>(validator_address: &Address) -> Result<(), Error> {
if VERIFY {
if let Address::Account(validator_address) = validator_address {
if validator_address.is_null() {
return Err(Error::NullDelegationValidatorId);
}
} else {
return Err(Error::InvalidAddressKind(validator_address.kind()));
}
}

Ok(())
}

fn verify_validator_address_packable<const VERIFY: bool>(
validator_address: &AccountAddress,
validator_address: &Address,
_: &ProtocolParameters,
) -> Result<(), Error> {
verify_validator_address::<VERIFY>(validator_address)
Expand Down
6 changes: 3 additions & 3 deletions sdk/src/types/block/output/feature/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub(crate) mod irc_27 {
use pretty_assertions::assert_eq;

use super::*;
use crate::types::block::{address::ToBech32Ext, rand::address::rand_address};
use crate::types::block::{address::ToBech32Ext, rand::address::rand_base_address};

#[test]
fn serialization() {
Expand All @@ -278,8 +278,8 @@ pub(crate) mod irc_27 {
"My NFT #0001",
)
.with_collection_name("My Collection of Art")
.add_royalty(rand_address().to_bech32_unchecked("iota1"), 0.025)
.add_royalty(rand_address().to_bech32_unchecked("iota1"), 0.025)
.add_royalty(rand_base_address().to_bech32_unchecked("iota1"), 0.025)
.add_royalty(rand_base_address().to_bech32_unchecked("iota1"), 0.025)
.with_issuer_name("My Artist Name")
.with_description("A little information about my NFT collection")
.add_attribute(Attribute::new("Background", "Purple"))
Expand Down

0 comments on commit bf43541

Please sign in to comment.