Skip to content

Commit

Permalink
rework allotment again
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Feb 15, 2024
1 parent e3b9bda commit c24587a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 56 deletions.
12 changes: 6 additions & 6 deletions sdk/src/client/api/block_builder/input_selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl InputSelection {
.0;
let added_mana = self.remainders.added_mana;
if let Some(output) = self.get_output_for_added_mana(&remainder_address) {
log::debug!("Adding {added_mana} excess input mana to output with address {remainder_address}");
println!("Adding {added_mana} excess input mana to output with address {remainder_address}");
let new_mana = output.mana() + added_mana;
*output = match output {
Output::Basic(b) => BasicOutputBuilder::from(&*b).with_mana(new_mana).finish_output()?,
Expand Down Expand Up @@ -325,7 +325,7 @@ impl InputSelection {
}

fn select_input(&mut self, input: InputSigningData) -> Result<(), Error> {
log::debug!("Selecting input {:?}", input.output_id());
println!("Selecting input {:?}", input.output_id());

if let Some(output) = self.transition_input(&input)? {
// No need to check for `outputs_requirements` because
Expand All @@ -337,7 +337,7 @@ impl InputSelection {
}

if let Some(requirement) = self.required_account_nft_addresses(&input)? {
log::debug!("Adding {requirement:?} from input {:?}", input.output_id());
println!("Adding {requirement:?} from input {:?}", input.output_id());
self.requirements.push(requirement);
}

Expand Down Expand Up @@ -682,7 +682,7 @@ impl InputSelection {
&input_chains_foundries,
outputs,
) {
log::debug!("validate_transitions error {err:?}");
println!("validate_transitions error {err:?}");
return Err(Error::UnfulfillableRequirement(Requirement::Account(
*account_output.account_id(),
)));
Expand Down Expand Up @@ -715,7 +715,7 @@ impl InputSelection {
// native tokens, and validation will fail without the capability.
&TransactionCapabilities::all(),
) {
log::debug!("validate_transitions error {err:?}");
println!("validate_transitions error {err:?}");
return Err(Error::UnfulfillableRequirement(Requirement::Foundry(
foundry_output.id(),
)));
Expand All @@ -740,7 +740,7 @@ impl InputSelection {
.expect("ISA is broken because there is no nft input");

if let Err(err) = NftOutput::transition_inner(nft_input.output.as_nft(), nft_output) {
log::debug!("validate_transitions error {err:?}");
println!("validate_transitions error {err:?}");
return Err(Error::UnfulfillableRequirement(Requirement::Nft(*nft_output.nft_id())));
}
}
Expand Down
12 changes: 6 additions & 6 deletions sdk/src/client/api/block_builder/input_selection/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl InputSelection {

// TODO verify_storage_deposit ?

log::debug!("Created storage deposit return output of {diff} for {address:?}");
println!("Created storage deposit return output of {diff} for {address:?}");

storage_deposit_returns.push(srd_output);
}
Expand All @@ -126,7 +126,7 @@ impl InputSelection {
let (input_mana, output_mana) = self.mana_sums(false)?;

if input_amount == output_amount && input_mana == output_mana && native_tokens_diff.is_none() {
log::debug!("No remainder required");
println!("No remainder required");
return Ok((storage_deposit_returns, Vec::new()));
}

Expand All @@ -144,11 +144,11 @@ impl InputSelection {
// If there is a mana remainder, try to fit it in an existing output
if input_mana > output_mana {
if self.output_for_added_mana_exists(&remainder_address) {
log::debug!("Allocating {mana_diff} excess input mana for output with address {remainder_address}");
println!("Allocating {mana_diff} excess input mana for output with address {remainder_address}");
self.remainders.added_mana = std::mem::take(&mut mana_diff);
// If we have no other remainders, we are done
if input_amount == output_amount && native_tokens_diff.is_none() {
log::debug!("No more remainder required");
println!("No more remainder required");
return Ok((storage_deposit_returns, Vec::new()));
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ fn create_remainder_outputs(
.add_unlock_condition(AddressUnlockCondition::new(remainder_address.clone()))
.with_native_token(*native_token)
.finish_output()?;
log::debug!(
println!(
"Created remainder output of amount {}, mana {} and native token {native_token:?} for {remainder_address:?}",
output.amount(),
output.mana()
Expand All @@ -287,7 +287,7 @@ fn create_remainder_outputs(
}
let catchall = catchall.finish_output()?;
catchall.verify_storage_deposit(storage_score_parameters)?;
log::debug!(
println!(
"Created remainder output of amount {}, mana {} and native token {:?} for {remainder_address:?}",
catchall.amount(),
catchall.mana(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ impl InputSelection {
};

if !self.selected_inputs.is_empty() {
// Remainders can only be calculated when the input mana is >= the output mana
let (input_mana, output_mana) = self.mana_sums(false)?;
if input_mana >= output_mana {
// Update remainders so the transaction is valid
self.update_remainders()?;
}

self.selected_inputs = Self::sort_input_signing_data(
std::mem::take(&mut self.selected_inputs),
self.creation_slot,
Expand All @@ -67,46 +60,48 @@ impl InputSelection {
// Add the empty allotment so the work score includes it
self.mana_allotments.entry(issuer_id).or_default();

let transaction = builder
// If the transaction fails to build, just keep going in case another requirement helps
if let Ok(transaction) = builder
.with_context_inputs(self.context_inputs.clone())
.with_mana_allotments(
self.mana_allotments
.iter()
.map(|(&account_id, &mana)| ManaAllotment { account_id, mana }),
)
.finish_with_params(&self.protocol_parameters)?;

let signed_transaction = SignedTransactionPayload::new(transaction, self.null_transaction_unlocks()?)?;

let block_work_score = self.protocol_parameters.work_score(&signed_transaction)
+ self.protocol_parameters.work_score_parameters().block();

let required_allotment_mana = block_work_score as u64 * reference_mana_cost;

let MinManaAllotment {
issuer_id,
allotment_debt,
..
} = self
.min_mana_allotment
.as_mut()
.ok_or(Error::UnfulfillableRequirement(Requirement::Mana))?;

// Add the required allotment to the issuing allotment
if required_allotment_mana > self.mana_allotments[issuer_id] {
log::debug!("Allotting at least {required_allotment_mana} mana to account ID {issuer_id}");
let additional_allotment = required_allotment_mana - self.mana_allotments[&issuer_id];
log::debug!("{additional_allotment} additional mana required to meet minimum allotment");
// Unwrap: safe because we always add the record above
*self.mana_allotments.get_mut(issuer_id).unwrap() = required_allotment_mana;
log::debug!("Adding {additional_allotment} to allotment debt {allotment_debt}");
*allotment_debt += additional_allotment;
} else {
log::debug!("Setting allotment debt to {}", self.mana_allotments[issuer_id]);
*allotment_debt = self.mana_allotments[issuer_id];
}
.finish_with_params(&self.protocol_parameters)
{
let signed_transaction = SignedTransactionPayload::new(transaction, self.null_transaction_unlocks()?)?;

let block_work_score = self.protocol_parameters.work_score(&signed_transaction)
+ self.protocol_parameters.work_score_parameters().block();

let required_allotment_mana = block_work_score as u64 * reference_mana_cost;

let MinManaAllotment {
issuer_id,
allotment_debt,
..
} = self
.min_mana_allotment
.as_mut()
.ok_or(Error::UnfulfillableRequirement(Requirement::Mana))?;

// Add the required allotment to the issuing allotment
if required_allotment_mana > self.mana_allotments[issuer_id] {
println!("Allotting at least {required_allotment_mana} mana to account ID {issuer_id}");
let additional_allotment = required_allotment_mana - self.mana_allotments[&issuer_id];
println!("{additional_allotment} additional mana required to meet minimum allotment");
// Unwrap: safe because we always add the record above
*self.mana_allotments.get_mut(issuer_id).unwrap() = required_allotment_mana;
println!("Adding {additional_allotment} to allotment debt {allotment_debt}");
*allotment_debt += additional_allotment;
} else {
println!("Setting allotment debt to {}", self.mana_allotments[issuer_id]);
*allotment_debt = self.mana_allotments[issuer_id];
}

self.reduce_account_output()?;
self.reduce_account_output()?;
}
}

// Remainders can only be calculated when the input mana is >= the output mana
Expand Down Expand Up @@ -142,7 +137,7 @@ impl InputSelection {
.filter(|o| o.is_account() && o.mana() != 0)
.find(|o| o.as_account().account_id() == issuer_id)
{
log::debug!(
println!(
"Reducing account mana of {} by {} for allotment",
output.as_account().account_id(),
allotment_debt
Expand All @@ -152,7 +147,7 @@ impl InputSelection {
.with_mana(output_mana.saturating_sub(*allotment_debt))
.finish_output()?;
*allotment_debt = allotment_debt.saturating_sub(output_mana);
log::debug!("Allotment debt after reduction: {}", allotment_debt);
println!("Allotment debt after reduction: {}", allotment_debt);
}
Ok(())
}
Expand Down Expand Up @@ -238,10 +233,10 @@ impl InputSelection {
pub(crate) fn get_inputs_for_mana_balance(&mut self) -> Result<Vec<InputSigningData>, Error> {
let (mut selected_mana, required_mana) = self.mana_sums(true)?;

log::debug!("Mana requirement selected mana: {selected_mana}, required mana: {required_mana}");
println!("Mana requirement selected mana: {selected_mana}, required mana: {required_mana}");

if selected_mana >= required_mana {
log::debug!("Mana requirement already fulfilled");
println!("Mana requirement already fulfilled");
Ok(Vec::new())
} else {
let mut inputs = Vec::new();
Expand Down

0 comments on commit c24587a

Please sign in to comment.