@@ -305,17 +305,17 @@ pub fn update_transfered_inscription(
305
305
pub fn find_latest_inscription_block_height (
306
306
hord_db_conn : & Connection ,
307
307
_ctx : & Context ,
308
- ) -> Result < u64 , String > {
308
+ ) -> Result < Option < u64 > , String > {
309
309
let args: & [ & dyn ToSql ] = & [ ] ;
310
310
let mut stmt = hord_db_conn
311
311
. prepare ( "SELECT block_height FROM inscriptions ORDER BY block_height DESC LIMIT 1" )
312
312
. unwrap ( ) ;
313
313
let mut rows = stmt. query ( args) . unwrap ( ) ;
314
314
while let Ok ( Some ( row) ) = rows. next ( ) {
315
315
let block_height: u64 = row. get ( 0 ) . unwrap ( ) ;
316
- return Ok ( block_height) ;
316
+ return Ok ( Some ( block_height) ) ;
317
317
}
318
- Ok ( 0 )
318
+ Ok ( None )
319
319
}
320
320
321
321
pub fn find_latest_inscription_number (
@@ -418,6 +418,34 @@ pub fn remove_entry_from_blocks(block_id: u32, hord_db_conn: &Connection, ctx: &
418
418
}
419
419
}
420
420
421
+ pub fn delete_blocks_in_block_range (
422
+ start_block : u32 ,
423
+ end_block : u32 ,
424
+ rw_hord_db_conn : & Connection ,
425
+ ctx : & Context ,
426
+ ) {
427
+ if let Err ( e) = rw_hord_db_conn. execute (
428
+ "DELETE FROM blocks WHERE id >= ?1 AND id <= ?2" ,
429
+ rusqlite:: params![ & start_block, & end_block] ,
430
+ ) {
431
+ ctx. try_log ( |logger| slog:: error!( logger, "{}" , e. to_string( ) ) ) ;
432
+ }
433
+ }
434
+
435
+ pub fn delete_inscriptions_in_block_range (
436
+ start_block : u32 ,
437
+ end_block : u32 ,
438
+ rw_hord_db_conn : & Connection ,
439
+ ctx : & Context ,
440
+ ) {
441
+ if let Err ( e) = rw_hord_db_conn. execute (
442
+ "DELETE FROM inscriptions WHERE block_height >= ?1 AND block_height <= ?2" ,
443
+ rusqlite:: params![ & start_block, & end_block] ,
444
+ ) {
445
+ ctx. try_log ( |logger| slog:: error!( logger, "{}" , e. to_string( ) ) ) ;
446
+ }
447
+ }
448
+
421
449
pub fn remove_entry_from_inscriptions (
422
450
inscription_id : & str ,
423
451
hord_db_conn : & Connection ,
@@ -513,6 +541,17 @@ pub fn remove_entry_from_inscriptions(
513
541
// Ok(())
514
542
// }
515
543
544
+ pub fn delete_data_in_hord_db (
545
+ start_block : u64 ,
546
+ end_block : u64 ,
547
+ rw_hord_db_conn : & Connection ,
548
+ ctx : & Context ,
549
+ ) -> Result < ( ) , String > {
550
+ delete_blocks_in_block_range ( start_block as u32 , end_block as u32 , rw_hord_db_conn, & ctx) ;
551
+ delete_inscriptions_in_block_range ( start_block as u32 , end_block as u32 , rw_hord_db_conn, & ctx) ;
552
+ Ok ( ( ) )
553
+ }
554
+
516
555
pub async fn fetch_and_cache_blocks_in_hord_db (
517
556
bitcoin_config : & BitcoinConfig ,
518
557
rw_hord_db_conn : & Connection ,
@@ -521,6 +560,7 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
521
560
ctx : & Context ,
522
561
network_thread : usize ,
523
562
) -> Result < ( ) , String > {
563
+ let number_of_blocks_to_process = end_block - start_block + 1 ;
524
564
let retrieve_block_hash_pool = ThreadPool :: new ( network_thread) ;
525
565
let ( block_hash_tx, block_hash_rx) = crossbeam_channel:: unbounded ( ) ;
526
566
let retrieve_block_data_pool = ThreadPool :: new ( network_thread) ;
@@ -589,29 +629,32 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
589
629
. expect ( "unable to spawn thread" ) ;
590
630
591
631
let mut blocks_stored = 0 ;
592
- let mut cursor = find_latest_inscription_block_height ( & rw_hord_db_conn, & ctx)
632
+ let mut cursor = 1 + find_latest_inscription_block_height ( & rw_hord_db_conn, & ctx) ?
593
633
. unwrap_or ( first_inscription_block_height) as usize ;
594
634
let mut inbox = HashMap :: new ( ) ;
595
635
596
636
while let Ok ( Some ( ( block_height, compacted_block, raw_block) ) ) = block_compressed_rx. recv ( ) {
597
637
ctx. try_log ( |logger| slog:: info!( logger, "Storing compacted block #{block_height}" ) ) ;
598
638
599
639
insert_entry_in_blocks ( block_height, & compacted_block, & rw_hord_db_conn, & ctx) ;
640
+ blocks_stored += 1 ;
600
641
642
+ // println!("{} < {}", raw_block.height, cursor);
601
643
// Early return, only considering blocks after 1st inscription
602
- if raw_block. height < cursor {
603
- continue ;
604
- }
605
- let block_height = raw_block. height ;
644
+ // if raw_block.height < cursor {
645
+ // continue;
646
+ // }
647
+
648
+ // let block_height = raw_block.height;
606
649
inbox. insert ( raw_block. height , raw_block) ;
607
650
608
651
// In the context of ordinals, we're constrained to process blocks sequentially
609
652
// Blocks are processed by a threadpool and could be coming out of order.
610
653
// Inbox block for later if the current block is not the one we should be
611
654
// processing.
612
- if block_height != cursor {
613
- continue ;
614
- }
655
+ // if block_height != cursor {
656
+ // continue;
657
+ // }
615
658
616
659
// Is the action of processing a block allows us
617
660
// to process more blocks present in the inbox?
@@ -627,9 +670,12 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
627
670
}
628
671
} ;
629
672
630
- if let Err ( e) =
631
- update_hord_db_and_augment_bitcoin_block ( & mut new_block, & rw_hord_db_conn, & ctx)
632
- {
673
+ if let Err ( e) = update_hord_db_and_augment_bitcoin_block (
674
+ & mut new_block,
675
+ & rw_hord_db_conn,
676
+ & ctx,
677
+ false ,
678
+ ) {
633
679
ctx. try_log ( |logger| {
634
680
slog:: error!( logger, "Unable to augment bitcoin block with hord_db: {e}" , )
635
681
} ) ;
@@ -638,8 +684,7 @@ pub async fn fetch_and_cache_blocks_in_hord_db(
638
684
cursor += 1 ;
639
685
}
640
686
641
- blocks_stored += 1 ;
642
- if blocks_stored == end_block - start_block {
687
+ if blocks_stored == number_of_blocks_to_process {
643
688
let _ = block_data_tx. send ( None ) ;
644
689
let _ = block_hash_tx. send ( None ) ;
645
690
ctx. try_log ( |logger| {
@@ -663,6 +708,15 @@ pub fn retrieve_satoshi_point_using_local_storage(
663
708
transaction_identifier : & TransactionIdentifier ,
664
709
ctx : & Context ,
665
710
) -> Result < ( u64 , u64 , u64 , u32 ) , String > {
711
+ ctx. try_log ( |logger| {
712
+ slog:: info!(
713
+ logger,
714
+ "Computing Satoshi # for sat_point {}:0:0 (block #{})" ,
715
+ transaction_identifier. hash,
716
+ block_identifier. index
717
+ )
718
+ } ) ;
719
+
666
720
let mut ordinal_offset = 0 ;
667
721
let mut ordinal_block_number = block_identifier. index as u32 ;
668
722
let txid = {
@@ -676,7 +730,7 @@ pub fn retrieve_satoshi_point_using_local_storage(
676
730
let res = match find_compacted_block_at_block_height ( ordinal_block_number, & hord_db_conn) {
677
731
Some ( res) => res,
678
732
None => {
679
- return Err ( format ! ( "unable to retrieve block ## {ordinal_block_number}" ) ) ;
733
+ return Err ( format ! ( "unable to retrieve block #{ordinal_block_number}" ) ) ;
680
734
}
681
735
} ;
682
736
0 commit comments