Ensure the transaction-chaining scheduler doesn't accumulate the same output multiple times

This commit is contained in:
Luke Parker 2024-09-03 16:42:47 -04:00
parent 75b4707002
commit ebef38d93b
2 changed files with 29 additions and 4 deletions

View file

@ -13,6 +13,7 @@ create_db! {
TransactionChainingScheduler {
OperatingCosts: (coin: Coin) -> Amount,
SerializedOutputs: (key: &[u8], coin: Coin) -> Vec<u8>,
AlreadyAccumulatedOutput: (id: &[u8]) -> (),
// We should be immediately able to schedule the fulfillment of payments, yet this may not be
// possible if we're in the middle of a multisig rotation (as our output set will be split)
SerializedQueuedPayments: (key: &[u8], coin: Coin) -> Vec<u8>,
@ -58,6 +59,21 @@ impl<S: ScannerFeed> Db<S> {
SerializedOutputs::del(txn, key.to_bytes().as_ref(), coin);
}
pub(crate) fn set_already_accumulated_output(
txn: &mut impl DbTxn,
output: <OutputFor<S> as ReceivedOutput<KeyFor<S>, AddressFor<S>>>::Id,
) {
AlreadyAccumulatedOutput::set(txn, output.as_ref(), &());
}
pub(crate) fn take_if_already_accumulated_output(
txn: &mut impl DbTxn,
output: <OutputFor<S> as ReceivedOutput<KeyFor<S>, AddressFor<S>>>::Id,
) -> bool {
let res = AlreadyAccumulatedOutput::get(txn, output.as_ref()).is_some();
AlreadyAccumulatedOutput::del(txn, output.as_ref());
res
}
pub(crate) fn queued_payments(
getter: &impl Get,
key: KeyFor<S>,

View file

@ -60,7 +60,9 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, EffectedReceivedOutputs<S>>> Sched
// that'd risk underflow
let available =
operating_costs + outputs.iter().map(|output| output.balance().amount.0).sum::<u64>();
assert!(available >= payments.iter().map(|payment| payment.balance().amount.0).sum::<u64>());
assert!(
available >= payments.iter().map(|payment| payment.balance().amount.0).sum::<u64>()
);
}
let amount_of_payments_that_can_be_handled =
@ -179,6 +181,9 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, EffectedReceivedOutputs<S>>> Sched
// Only handle Change so if someone burns to an External address, we don't use it here
// when the scanner will tell us to return it (without accumulating it)
effected_received_outputs.retain(|output| output.kind() == OutputType::Change);
for output in &effected_received_outputs {
Db::<S>::set_already_accumulated_output(txn, output.id());
}
outputs.append(&mut effected_received_outputs);
}
@ -236,9 +241,13 @@ impl<S: ScannerFeed, P: TransactionPlanner<S, EffectedReceivedOutputs<S>>> Sched
let mut outputs_by_coin = HashMap::with_capacity(1);
for output in update.outputs().iter().filter(|output| output.key() == *key) {
match output.kind() {
OutputType::External | OutputType::Forwarded => {},
// TODO: Only accumulate these if we haven't already, but do accumulate if not
OutputType::Branch | OutputType::Change => todo!("TODO"),
OutputType::External | OutputType::Forwarded => {}
// Only accumulate these if we haven't already
OutputType::Branch | OutputType::Change => {
if Db::<S>::take_if_already_accumulated_output(txn, output.id()) {
continue;
}
}
}
let coin = output.balance().coin;
if let std::collections::hash_map::Entry::Vacant(e) = outputs_by_coin.entry(coin) {