feat: start search corerct

This commit is contained in:
Pravdyvy 2026-02-16 17:54:54 +02:00
parent adceaebd06
commit 10c718f966
2 changed files with 71 additions and 29 deletions

View File

@ -152,47 +152,89 @@ impl IndexerCore {
.map(|info| info.header_id) .map(|info| info.header_id)
} }
/// WARNING: depending on chain behaviour,
/// may take indefinite amount of time
pub async fn search_for_channel_start( pub async fn search_for_channel_start(
&self, &self,
) -> Result<VecDeque<bedrock_client::Block<SignedMantleTx>>> { ) -> Result<VecDeque<bedrock_client::Block<SignedMantleTx>>> {
let mut curr_last_header = self.wait_last_finalized_block_header().await?; let mut curr_last_header = self.wait_last_finalized_block_header().await?;
// Storing start for future use
let mut rollback_start = curr_last_header;
// ToDo: How to get root?
let mut rollback_limit = HeaderId::from([0; 32]);
// ToDo: Not scalable, initial buffer should be stored in DB to not run out of memory // ToDo: Not scalable, initial buffer should be stored in DB to not run out of memory
// Don't want to complicate DB even more right now. // Don't want to complicate DB even more right now.
let mut initial_block_buffer = VecDeque::new(); let mut block_buffer = VecDeque::new();
loop { 'outer: loop {
let Some(curr_last_block) = self loop {
.bedrock_client // let res = self
.get_block_by_id(curr_last_header) // .bedrock_client
.await? // .get_block_by_id(curr_last_header)
else { // .await?;
return Err(anyhow::anyhow!("Chain inconsistency"));
};
initial_block_buffer.push_front(curr_last_block.clone()); // let curr_last_block;
if let Some(_) = curr_last_block.transactions().find_map(|tx| { // match res {
tx.mantle_tx.ops.iter().find_map(|op| match op { // Some(block) => {curr_last_block = block},
Op::ChannelInscribe(InscriptionOp { // None => {
channel_id, parent, .. // break;
}) => { // }
if (channel_id == &self.config.channel_id) && (parent == &MsgId::root()) { // }
Some(curr_last_block.header().id())
} else { let Some(curr_last_block) = self
None .bedrock_client
.get_block_by_id(curr_last_header)
.await?
else {
log::error!("Failed to get block for header {curr_last_header}");
return Err(anyhow::anyhow!("Chain inconsistency"));
};
info!("INITIAL_SEARCH: Observed L1 block at height {}", curr_last_block.header().slot().into_inner());
info!("INITIAL_SEARCH: This block header is {}", curr_last_block.header().id());
info!("INITIAL_SEARCH: This block parent is {}", curr_last_block.header().parent());
block_buffer.push_front(curr_last_block.clone());
if let Some(_) = curr_last_block.transactions().find_map(|tx| {
tx.mantle_tx.ops.iter().find_map(|op| match op {
Op::ChannelInscribe(InscriptionOp {
channel_id, parent, ..
}) => {
if (channel_id == &self.config.channel_id) && (parent == &MsgId::root())
{
Some(curr_last_block.header().id())
} else {
None
}
} }
_ => None,
})
}) {
info!("INITIAL_SEARCH: Found channel start");
break 'outer;
} else {
// Step back to parent
let parent = curr_last_block.header().parent();
if parent == rollback_limit {
break;
} }
_ => None,
}) curr_last_header = parent;
}) { };
break; }
} else {
// Step back to parent info!("INITIAL_SEARCH: Reached rollback limit, refetching last block");
curr_last_header = curr_last_block.header().parent();
}; block_buffer.clear();
rollback_limit = rollback_start;
curr_last_header = self.wait_last_finalized_block_header().await?;
rollback_start = curr_last_header;
} }
Ok(initial_block_buffer) Ok(block_buffer)
} }
pub async fn rollback_to_last_known_finalized_l1_id( pub async fn rollback_to_last_known_finalized_l1_id(

View File

@ -12,7 +12,7 @@ use tokio::test;
use wallet::cli::{Command, programs::native_token_transfer::AuthTransferSubcommand}; use wallet::cli::{Command, programs::native_token_transfer::AuthTransferSubcommand};
/// Timeout in milliseconds to reliably await for block finalization /// Timeout in milliseconds to reliably await for block finalization
const L2_TO_L1_TIMEOUT_MILLIS: u64 = 220000; const L2_TO_L1_TIMEOUT_MILLIS: u64 = 120000;
#[test] #[test]
async fn indexer_test_run() -> Result<()> { async fn indexer_test_run() -> Result<()> {