Skip to content

Commit

Permalink
Merge branch '2.0' into rewards-retention-period
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Jan 10, 2024
2 parents 7a8beac + 91e0a80 commit ffc65fd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ dotenvy = { version = "0.15.7", default-features = false }
fern-logger = { version = "0.5.0", default-features = false }
num_cpus = { version = "1.16.0", default-features = false }
once_cell = { version = "1.19.0", default-features = false }
regex = { version = "1.10.2", default-features = false }
tokio = { version = "1.35.1", default-features = false, features = [
"macros",
"rt",
Expand Down
11 changes: 10 additions & 1 deletion sdk/src/client/api/block_builder/input_selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,16 @@ impl InputSelection {
protocol_parameters: ProtocolParameters,
) -> Self {
let available_inputs = available_inputs.into();
let mut addresses = HashSet::from_iter(addresses);

let mut addresses = HashSet::from_iter(addresses.into_iter().map(|a| {
// Get a potential Ed25519 address directly since we're only interested in that
#[allow(clippy::option_if_let_else)] // clippy's suggestion requires a clone
if let Some(address) = a.backing_ed25519() {
Address::Ed25519(*address)
} else {
a
}
}));

addresses.extend(available_inputs.iter().filter_map(|input| match &input.output {
Output::Account(output) => Some(Address::Account(AccountAddress::from(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl InputSelection {
.required_address(self.slot_index, self.protocol_parameters.committable_age_range())?
.expect("expiration unlockable outputs already filtered out");

if required_address.is_ed25519() {
if required_address.is_ed25519_backed() {
return Ok(Some((required_address, input.chain)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ impl InputSelection {
.unwrap()
.expect("expiration unlockable outputs already filtered out");

&required_address == address
required_address
.backing_ed25519()
.map_or(false, |a| a == address.as_ed25519())
}

/// Fulfills an ed25519 sender requirement by selecting an available input that unlocks its address.
Expand Down
66 changes: 65 additions & 1 deletion sdk/tests/client/input_selection/basic_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::str::FromStr;
use iota_sdk::{
client::api::input_selection::{Error, InputSelection, Requirement},
types::block::{
address::{Address, MultiAddress, RestrictedAddress, WeightedAddress},
address::{
Address, AddressCapabilities, ImplicitAccountCreationAddress, MultiAddress, RestrictedAddress,
WeightedAddress,
},
output::{AccountId, NftId},
protocol::protocol_parameters,
},
Expand Down Expand Up @@ -2060,3 +2063,64 @@ fn multi_address_sender_already_fulfilled() {
assert!(unsorted_eq(&selected.inputs, &inputs));
assert!(unsorted_eq(&selected.outputs, &outputs));
}

#[test]
fn ed25519_backed_available_address() {
let protocol_parameters = protocol_parameters();
let ed25519 = Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap();
let restricted_address = Address::from(
RestrictedAddress::new(ed25519.clone())
.unwrap()
.with_allowed_capabilities(AddressCapabilities::all()),
);

let inputs = build_inputs([
Basic(
1_000_000,
restricted_address.clone(),
None,
None,
None,
None,
None,
None,
),
Basic(1_000_000, ed25519.clone(), None, None, None, None, None, None),
]);
let outputs = build_outputs([
Basic(
1_000_000,
Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap(),
None,
None,
None,
None,
None,
None,
),
Basic(
1_000_000,
Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap(),
None,
Some(restricted_address.clone()),
None,
None,
None,
None,
),
]);

let selected = InputSelection::new(
inputs.clone(),
outputs.clone(),
// Restricted address is provided, but it can also unlock the ed25519 one
[restricted_address],
protocol_parameters,
)
.select()
.unwrap();

assert!(unsorted_eq(&selected.inputs, &inputs));
// Provided outputs
assert_eq!(selected.outputs, outputs);
}

0 comments on commit ffc65fd

Please sign in to comment.