Have acknowledge_block take in the results of the InInstructions executed

If any failed, the scanner now creates a Burn for the return.
This commit is contained in:
Luke Parker
2024-08-30 01:19:29 -04:00
parent 5999f5d65a
commit 76cbe6cf1e
6 changed files with 203 additions and 31 deletions

View File

@@ -7,7 +7,7 @@ use serai_db::{Get, DbTxn, Db};
use serai_primitives::{NetworkId, Coin, Amount};
use serai_in_instructions_primitives::Batch;
use serai_coins_primitives::OutInstructionWithBalance;
use serai_coins_primitives::{OutInstruction, OutInstructionWithBalance};
use primitives::{task::*, Address, ReceivedOutput, Block};
@@ -327,6 +327,8 @@ impl<S: ScannerFeed> Scanner<S> {
&mut self,
mut txn: impl DbTxn,
block_number: u64,
batch_id: u32,
in_instruction_succeededs: Vec<bool>,
key_to_activate: Option<KeyFor<S>>,
) {
log::info!("acknowledging block {block_number}");
@@ -338,8 +340,12 @@ impl<S: ScannerFeed> Scanner<S> {
if let Some(prior_highest_acknowledged_block) =
ScannerGlobalDb::<S>::highest_acknowledged_block(&txn)
{
assert!(block_number > prior_highest_acknowledged_block, "acknowledging blocks out-of-order");
for b in (prior_highest_acknowledged_block + 1) .. (block_number - 1) {
// If a single block produced multiple Batches, the block number won't increment
assert!(
block_number >= prior_highest_acknowledged_block,
"acknowledging blocks out-of-order"
);
for b in (prior_highest_acknowledged_block + 1) .. block_number {
assert!(
!ScannerGlobalDb::<S>::is_block_notable(&txn, b),
"skipped acknowledging a block which was notable"
@@ -352,6 +358,37 @@ impl<S: ScannerFeed> Scanner<S> {
ScannerGlobalDb::<S>::queue_key(&mut txn, block_number + S::WINDOW_LENGTH, key_to_activate);
}
// Return the balances for any InInstructions which failed to execute
{
let return_information = report::take_return_information::<S>(&mut txn, batch_id)
.expect("didn't save the return information for Batch we published");
assert_eq!(
in_instruction_succeededs.len(),
return_information.len(),
"amount of InInstruction succeededs differed from amount of return information saved"
);
// We map these into standard Burns
let mut returns = vec![];
for (succeeded, return_information) in
in_instruction_succeededs.into_iter().zip(return_information)
{
if succeeded {
continue;
}
if let Some(report::ReturnInformation { address, balance }) = return_information {
returns.push(OutInstructionWithBalance {
instruction: OutInstruction { address: address.into(), data: None },
balance,
});
}
}
// We send them as stemming from this block
// TODO: These should be handled with any Burns from this block
SubstrateToEventualityDb::send_burns(&mut txn, block_number, &returns);
}
// Commit the txn
txn.commit();
// Run the Eventuality task since we've advanced it