Skip to content

Commit

Permalink
Update processor tests to refund logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Nov 9, 2023
1 parent 06e627a commit ffedba7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
7 changes: 7 additions & 0 deletions processor/src/multisigs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ impl<D: Db, N: Network> MultisigManager<D, N> {
}

fn refund_plan(output: &N::Output, refund_to: N::Address) -> Plan<N> {
log::info!("creating refund plan for {}", hex::encode(output.id()));
assert_eq!(output.kind(), OutputType::External);
Plan {
key: output.key(),
inputs: vec![output.clone()],
Expand Down Expand Up @@ -639,6 +641,10 @@ impl<D: Db, N: Network> MultisigManager<D, N> {
if let Some(refund_to) = MultisigsDb::<N, D>::take_refund(txn, output.id().as_ref()) {
// If this isn't a valid refund address, accumulate this output
let Ok(refund_to) = refund_to.consume().try_into() else {
log::info!(
"set refund for {} didn't have a valid address to refund to",
hex::encode(output.id())
);
return true;
};
plans.push(Self::refund_plan(output, refund_to));
Expand Down Expand Up @@ -830,6 +836,7 @@ impl<D: Db, N: Network> MultisigManager<D, N> {
let (refund_to, instruction) = instruction_from_output::<N>(&output);
let refund = |txn| {
if let Some(refund_to) = refund_to {
log::info!("setting refund for output {}", hex::encode(output.id()));
MultisigsDb::<N, D>::set_refund(txn, output.id().as_ref(), refund_to);
}
};
Expand Down
39 changes: 33 additions & 6 deletions tests/processor/src/tests/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn batch_test() {
let mut serai_address = [0; 32];
OsRng.fill_bytes(&mut serai_address);
let instruction =
if i == 1 { Some(InInstruction::Transfer(SeraiAddress(serai_address))) } else { None };
if i == 0 { Some(InInstruction::Transfer(SeraiAddress(serai_address))) } else { None };

// Send into the processor's wallet
let (tx, balance_sent) =
Expand All @@ -258,9 +258,9 @@ fn batch_test() {
network,
id: i,
block: BlockHash(block_with_tx.unwrap()),
instructions: if let Some(instruction) = instruction {
instructions: if let Some(instruction) = &instruction {
vec![InInstructionWithBalance {
instruction,
instruction: instruction.clone(),
balance: Balance {
coin: balance_sent.coin,
amount: Amount(
Expand Down Expand Up @@ -311,7 +311,7 @@ fn batch_test() {
let serai_time =
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
for coordinator in &mut coordinators {
assert!(substrate_block(
let plans = substrate_block(
coordinator,
messages::substrate::CoordinatorMessage::SubstrateBlock {
context: SubstrateContext {
Expand All @@ -324,8 +324,35 @@ fn batch_test() {
batches: vec![batch.batch.id],
},
)
.await
.is_empty());
.await;
if instruction.is_some() || (instruction.is_none() && (network == NetworkId::Monero)) {
assert!(plans.is_empty());
} else {
// If no instruction was used, and the processor csn presume the origin, it'd have
// created a refund Plan
assert_eq!(plans.len(), 1);
}
}
}

// With the latter InInstruction not existing, we should've triggered a refund if the origin
// was detectable
// Check this is trying to sign a Plan
if network != NetworkId::Monero {
let mut refund_id = None;
for coordinator in &mut coordinators {
match coordinator.recv_message().await {
messages::ProcessorMessage::Sign(messages::sign::ProcessorMessage::Preprocess {
id,
..
}) => {
if refund_id.is_none() {
refund_id = Some(id.clone());
}
assert_eq!(refund_id.as_ref().unwrap(), &id);
}
_ => panic!("processor didn't send preprocess for expected refund transaction"),
}
}
}
});
Expand Down

0 comments on commit ffedba7

Please sign in to comment.