Update processor tests to refund logic

This commit is contained in:
Luke Parker 2023-11-08 21:59:11 -05:00
parent 06e627a562
commit ffedba7a05
No known key found for this signature in database
2 changed files with 40 additions and 6 deletions

View file

@ -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()],
@ -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));
@ -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);
}
};

View file

@ -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) =
@ -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(
@ -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 {
@ -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"),
}
}
}
});