@@ -15,7 +15,7 @@ use hiro_system_kit::slog;
15
15
use rand:: { thread_rng, Rng } ;
16
16
17
17
use rocksdb:: DB ;
18
- use rusqlite:: { Connection , OpenFlags , ToSql } ;
18
+ use rusqlite:: { Connection , OpenFlags , ToSql , Transaction } ;
19
19
use std:: io:: Cursor ;
20
20
use std:: io:: { Read , Write } ;
21
21
use threadpool:: ThreadPool ;
@@ -487,21 +487,24 @@ pub fn find_initial_inscription_transfer_data(
487
487
inscription_id : & str ,
488
488
inscriptions_db_conn : & Connection ,
489
489
_ctx : & Context ,
490
- ) -> Result < Option < ( TransactionIdentifier , usize , u64 ) > , String > {
490
+ ) -> Result < Option < TransferData > , String > {
491
491
let args: & [ & dyn ToSql ] = & [ & inscription_id. to_sql ( ) . unwrap ( ) ] ;
492
492
let mut stmt = inscriptions_db_conn
493
- . prepare ( "SELECT outpoint_to_watch, offset FROM locations WHERE inscription_id = ? ORDER BY block_height ASC, tx_index ASC LIMIT 1" )
493
+ . prepare ( "SELECT outpoint_to_watch, offset, tx_index FROM locations WHERE inscription_id = ? ORDER BY block_height ASC, tx_index ASC LIMIT 1" )
494
494
. unwrap ( ) ;
495
495
let mut rows = stmt. query ( args) . unwrap ( ) ;
496
496
while let Ok ( Some ( row) ) = rows. next ( ) {
497
497
let outpoint_to_watch: String = row. get ( 0 ) . unwrap ( ) ;
498
- let ( transaction_identifier, output_index) = parse_outpoint_to_watch ( & outpoint_to_watch) ;
498
+ let ( transaction_identifier_location, output_index) =
499
+ parse_outpoint_to_watch ( & outpoint_to_watch) ;
499
500
let inscription_offset_intra_output: u64 = row. get ( 1 ) . unwrap ( ) ;
500
- return Ok ( Some ( (
501
- transaction_identifier,
501
+ let tx_index: u64 = row. get ( 2 ) . unwrap ( ) ;
502
+ return Ok ( Some ( TransferData {
503
+ transaction_identifier_location,
502
504
output_index,
503
505
inscription_offset_intra_output,
504
- ) ) ) ;
506
+ tx_index,
507
+ } ) ) ;
505
508
}
506
509
Ok ( None )
507
510
}
@@ -534,6 +537,7 @@ pub struct TransferData {
534
537
pub inscription_offset_intra_output : u64 ,
535
538
pub transaction_identifier_location : TransactionIdentifier ,
536
539
pub output_index : usize ,
540
+ pub tx_index : u64 ,
537
541
}
538
542
539
543
pub fn find_all_transfers_in_block (
@@ -551,12 +555,14 @@ pub fn find_all_transfers_in_block(
551
555
let inscription_id: String = row. get ( 0 ) . unwrap ( ) ;
552
556
let inscription_offset_intra_output: u64 = row. get ( 1 ) . unwrap ( ) ;
553
557
let outpoint_to_watch: String = row. get ( 2 ) . unwrap ( ) ;
558
+ let tx_index: u64 = row. get ( 3 ) . unwrap ( ) ;
554
559
let ( transaction_identifier_location, output_index) =
555
560
parse_outpoint_to_watch ( & outpoint_to_watch) ;
556
561
let transfer = TransferData {
557
562
inscription_offset_intra_output,
558
563
transaction_identifier_location,
559
564
output_index,
565
+ tx_index,
560
566
} ;
561
567
results
562
568
. entry ( inscription_id)
@@ -650,7 +656,7 @@ pub fn find_inscription_with_id(
650
656
} ;
651
657
let mut rows = stmt. query ( args) . unwrap ( ) ;
652
658
653
- if let Some ( ( transaction_identifier_location , output_index , inscription_offset_intra_output ) ) =
659
+ if let Some ( transfer_data ) =
654
660
find_initial_inscription_transfer_data ( inscription_id, inscriptions_db_conn, ctx) ?
655
661
{
656
662
while let Ok ( Some ( row) ) = rows. next ( ) {
@@ -665,11 +671,7 @@ pub fn find_inscription_with_id(
665
671
inscription_input_index,
666
672
transaction_identifier_inscription,
667
673
transfers : 0 ,
668
- transfer_data : TransferData {
669
- inscription_offset_intra_output,
670
- transaction_identifier_location,
671
- output_index,
672
- } ,
674
+ transfer_data,
673
675
} ;
674
676
return Ok ( Some ( traversal) ) ;
675
677
}
@@ -681,12 +683,12 @@ pub fn find_all_inscriptions_in_block(
681
683
block_height : & u64 ,
682
684
inscriptions_db_conn : & Connection ,
683
685
ctx : & Context ,
684
- ) -> BTreeMap < u64 , Vec < ( TransactionIdentifier , TraversalResult ) > > {
686
+ ) -> Vec < ( TransactionIdentifier , TraversalResult ) > {
685
687
let args: & [ & dyn ToSql ] = & [ & block_height. to_sql ( ) . unwrap ( ) ] ;
686
688
let mut stmt = inscriptions_db_conn
687
689
. prepare ( "SELECT inscription_number, ordinal_number, inscription_id FROM inscriptions where block_height = ? ORDER BY inscription_number ASC" )
688
690
. unwrap ( ) ;
689
- let mut results: BTreeMap < u64 , Vec < ( TransactionIdentifier , TraversalResult ) > > = BTreeMap :: new ( ) ;
691
+ let mut results = vec ! [ ] ;
690
692
let mut rows = stmt. query ( args) . unwrap ( ) ;
691
693
692
694
let transfers_data = find_all_transfers_in_block ( block_height, inscriptions_db_conn, ctx) ;
@@ -710,15 +712,7 @@ pub fn find_all_inscriptions_in_block(
710
712
transaction_identifier_inscription : transaction_identifier_inscription. clone ( ) ,
711
713
transfer_data : transfer_data,
712
714
} ;
713
- results
714
- . entry ( * block_height)
715
- . and_modify ( |v| {
716
- v. push ( (
717
- transaction_identifier_inscription. clone ( ) ,
718
- traversal. clone ( ) ,
719
- ) )
720
- } )
721
- . or_insert ( vec ! [ ( transaction_identifier_inscription, traversal) ] ) ;
715
+ results. push ( ( transaction_identifier_inscription, traversal) ) ;
722
716
}
723
717
return results;
724
718
}
@@ -827,6 +821,38 @@ pub fn remove_entry_from_inscriptions(
827
821
}
828
822
}
829
823
824
+ pub fn remove_entry_from_locations (
825
+ inscription_id : & str ,
826
+ inscriptions_db_rw_conn : & Transaction ,
827
+ ctx : & Context ,
828
+ ) {
829
+ if let Err ( e) = inscriptions_db_rw_conn. execute (
830
+ "DELETE FROM locations WHERE inscription_id = ?1" ,
831
+ rusqlite:: params![ & inscription_id] ,
832
+ ) {
833
+ ctx. try_log ( |logger| slog:: error!( logger, "{}" , e. to_string( ) ) ) ;
834
+ }
835
+ }
836
+
837
+ pub fn insert_entry_in_locations (
838
+ inscription_id : & str ,
839
+ block_height : u64 ,
840
+ transfer_data : & TransferData ,
841
+ inscriptions_db_rw_conn : & Transaction ,
842
+ ctx : & Context ,
843
+ ) {
844
+ let outpoint_to_watch = format_outpoint_to_watch (
845
+ & transfer_data. transaction_identifier_location ,
846
+ transfer_data. output_index ,
847
+ ) ;
848
+ if let Err ( e) = inscriptions_db_rw_conn. execute (
849
+ "INSERT INTO locations (inscription_id, outpoint_to_watch, offset, block_height, tx_index) VALUES (?1, ?2, ?3, ?4, ?5)" ,
850
+ rusqlite:: params![ & inscription_id, & outpoint_to_watch, & transfer_data. inscription_offset_intra_output, & block_height, & transfer_data. tx_index] ,
851
+ ) {
852
+ ctx. try_log ( |logger| slog:: error!( logger, "{}" , e. to_string( ) ) ) ;
853
+ }
854
+ }
855
+
830
856
pub fn delete_data_in_hord_db (
831
857
start_block : u64 ,
832
858
end_block : u64 ,
@@ -1090,6 +1116,14 @@ impl TraversalResult {
1090
1116
let sat = Sat ( self . ordinal_number ) ;
1091
1117
self . ordinal_number - sat. height ( ) . starting_sat ( ) . n ( )
1092
1118
}
1119
+
1120
+ pub fn get_inscription_id ( & self ) -> String {
1121
+ format ! (
1122
+ "{}i{}" ,
1123
+ self . transaction_identifier_inscription. get_hash_bytes_str( ) ,
1124
+ self . inscription_input_index
1125
+ )
1126
+ }
1093
1127
}
1094
1128
1095
1129
pub fn format_satpoint_to_watch (
@@ -1280,6 +1314,7 @@ pub fn retrieve_satoshi_point_using_lazy_storage(
1280
1314
inscription_offset_intra_output,
1281
1315
transaction_identifier_location : transaction_identifier. clone ( ) ,
1282
1316
output_index : inscription_output_index,
1317
+ tx_index : 0 ,
1283
1318
} ,
1284
1319
} ) ;
1285
1320
}
@@ -1404,6 +1439,7 @@ pub fn retrieve_satoshi_point_using_lazy_storage(
1404
1439
inscription_offset_intra_output,
1405
1440
transaction_identifier_location : transaction_identifier. clone ( ) ,
1406
1441
output_index : inscription_output_index,
1442
+ tx_index : 0 ,
1407
1443
} ,
1408
1444
} ) ;
1409
1445
}
@@ -1423,6 +1459,7 @@ pub fn retrieve_satoshi_point_using_lazy_storage(
1423
1459
inscription_offset_intra_output,
1424
1460
transaction_identifier_location : transaction_identifier. clone ( ) ,
1425
1461
output_index : inscription_output_index,
1462
+ tx_index : 0 ,
1426
1463
} ,
1427
1464
} )
1428
1465
}
0 commit comments