@@ -4,10 +4,15 @@ pub mod ord;
4
4
5
5
use bitcoincore_rpc:: bitcoin:: hashes:: hex:: FromHex ;
6
6
use bitcoincore_rpc:: bitcoin:: { Address , Network , Script } ;
7
- use chainhook_types:: { BitcoinBlockData , OrdinalInscriptionTransferData , OrdinalOperation } ;
7
+ use chainhook_types:: {
8
+ BitcoinBlockData , OrdinalInscriptionTransferData , OrdinalOperation , TransactionIdentifier ,
9
+ } ;
8
10
use hiro_system_kit:: slog;
9
11
use rusqlite:: Connection ;
10
- use std:: collections:: VecDeque ;
12
+ use std:: collections:: { HashMap , VecDeque } ;
13
+ use std:: path:: PathBuf ;
14
+ use std:: sync:: mpsc:: channel;
15
+ use threadpool:: ThreadPool ;
11
16
12
17
use crate :: {
13
18
hord:: {
@@ -22,7 +27,10 @@ use crate::{
22
27
utils:: Context ,
23
28
} ;
24
29
25
- use self :: db:: { remove_entry_from_blocks, remove_entry_from_inscriptions} ;
30
+ use self :: db:: {
31
+ open_readonly_hord_db_conn, remove_entry_from_blocks, remove_entry_from_inscriptions,
32
+ TraversalResult ,
33
+ } ;
26
34
27
35
pub fn revert_hord_db_with_augmented_bitcoin_block (
28
36
block : & BitcoinBlockData ,
@@ -64,8 +72,9 @@ pub fn revert_hord_db_with_augmented_bitcoin_block(
64
72
pub fn update_hord_db_and_augment_bitcoin_block (
65
73
new_block : & mut BitcoinBlockData ,
66
74
rw_hord_db_conn : & Connection ,
67
- ctx : & Context ,
68
75
write_block : bool ,
76
+ hord_db_path : & PathBuf ,
77
+ ctx : & Context ,
69
78
) -> Result < ( ) , String > {
70
79
if write_block {
71
80
ctx. try_log ( |logger| {
@@ -92,60 +101,86 @@ pub fn update_hord_db_and_augment_bitcoin_block(
92
101
. clone ( ) ;
93
102
let first_sat_post_subsidy = Height ( new_block. block_identifier . index ) . starting_sat ( ) . 0 ;
94
103
104
+ let mut transactions_ids = vec ! [ ] ;
105
+ for new_tx in new_block. transactions . iter_mut ( ) . skip ( 1 ) {
106
+ // Have a new inscription been revealed, if so, are looking at a re-inscription
107
+ for ordinal_event in
108
+ new_tx. metadata . ordinal_operations . iter_mut ( )
109
+ {
110
+ if let OrdinalOperation :: InscriptionRevealed ( _) = ordinal_event {
111
+ transactions_ids. push ( new_tx. transaction_identifier . clone ( ) ) ;
112
+ }
113
+ }
114
+ }
115
+ let expected_traversals = transactions_ids. len ( ) ;
116
+ let ( traversal_tx, traversal_rx) = channel :: < ( TransactionIdentifier , TraversalResult ) > ( ) ;
117
+ let traversal_data_pool = ThreadPool :: new ( 10 ) ;
118
+
119
+ for transaction_id in transactions_ids. into_iter ( ) {
120
+ let moved_traversal_tx = traversal_tx. clone ( ) ;
121
+ let moved_ctx = ctx. clone ( ) ;
122
+ let block_identifier = new_block. block_identifier . clone ( ) ;
123
+ let hord_db_path = hord_db_path. clone ( ) ;
124
+ traversal_data_pool. execute ( move || {
125
+ let hord_db_conn = open_readonly_hord_db_conn ( & hord_db_path, & moved_ctx) . unwrap ( ) ;
126
+ let traversal = retrieve_satoshi_point_using_local_storage (
127
+ & hord_db_conn,
128
+ & block_identifier,
129
+ & transaction_id,
130
+ & moved_ctx,
131
+ )
132
+ . unwrap ( ) ;
133
+ let _ = moved_traversal_tx. send ( ( transaction_id, traversal) ) ;
134
+ } ) ;
135
+ }
136
+
137
+ let mut traversals = HashMap :: new ( ) ;
138
+ let mut traversals_received = 0 ;
139
+ while let Ok ( ( transaction_identifier, traversal_result) ) = traversal_rx. recv ( ) {
140
+ traversals_received += 1 ;
141
+ traversals. insert ( transaction_identifier, traversal_result) ;
142
+ if traversals_received == expected_traversals {
143
+ break ;
144
+ }
145
+ }
146
+
95
147
for new_tx in new_block. transactions . iter_mut ( ) . skip ( 1 ) {
96
148
let mut ordinals_events_indexes_to_discard = VecDeque :: new ( ) ;
97
149
// Have a new inscription been revealed, if so, are looking at a re-inscription
98
150
for ( ordinal_event_index, ordinal_event) in
99
151
new_tx. metadata . ordinal_operations . iter_mut ( ) . enumerate ( )
100
152
{
101
153
if let OrdinalOperation :: InscriptionRevealed ( inscription) = ordinal_event {
102
- let (
103
- ordinal_block_height,
104
- ordinal_offset,
105
- ordinal_number,
106
- transfers_pre_inscription,
107
- ) = {
108
- // Are we looking at a re-inscription?
109
- let res = retrieve_satoshi_point_using_local_storage (
110
- & rw_hord_db_conn,
111
- & new_block. block_identifier ,
112
- & new_tx. transaction_identifier ,
113
- & ctx,
114
- ) ;
115
-
116
- match res {
117
- Ok ( res) => res,
118
- Err ( e) => {
119
- ctx. try_log ( |logger| {
120
- slog:: error!(
121
- logger,
122
- "unable to retrieve satoshi point: {}" ,
123
- e. to_string( )
124
- ) ;
125
- } ) ;
126
- continue ;
127
- }
154
+ let traversal = match traversals. get ( & new_tx. transaction_identifier ) {
155
+ Some ( traversal) => traversal,
156
+ None => {
157
+ ctx. try_log ( |logger| {
158
+ slog:: error!( logger, "unable to retrieve satoshi point" , ) ;
159
+ } ) ;
160
+ continue ;
128
161
}
129
162
} ;
130
163
131
- if let Some ( _entry) =
132
- find_inscription_with_ordinal_number ( & ordinal_number, & rw_hord_db_conn, & ctx)
133
- {
164
+ if let Some ( _entry) = find_inscription_with_ordinal_number (
165
+ & traversal. ordinal_number ,
166
+ & rw_hord_db_conn,
167
+ & ctx,
168
+ ) {
134
169
ctx. try_log ( |logger| {
135
170
slog:: warn!(
136
171
logger,
137
172
"Transaction {} in block {} is overriding an existing inscription {}" ,
138
173
new_tx. transaction_identifier. hash,
139
174
new_block. block_identifier. index,
140
- ordinal_number
175
+ traversal . ordinal_number
141
176
) ;
142
177
} ) ;
143
178
ordinals_events_indexes_to_discard. push_front ( ordinal_event_index) ;
144
179
} else {
145
- inscription. ordinal_offset = ordinal_offset;
146
- inscription. ordinal_block_height = ordinal_block_height ;
147
- inscription. ordinal_number = ordinal_number;
148
- inscription. transfers_pre_inscription = transfers_pre_inscription ;
180
+ inscription. ordinal_offset = traversal . ordinal_offset ;
181
+ inscription. ordinal_block_height = traversal . ordinal_block_number ;
182
+ inscription. ordinal_number = traversal . ordinal_number ;
183
+ inscription. transfers_pre_inscription = traversal . transfers ;
149
184
inscription. inscription_number =
150
185
match find_latest_inscription_number ( & rw_hord_db_conn, & ctx) {
151
186
Ok ( inscription_number) => inscription_number,
@@ -167,7 +202,7 @@ pub fn update_hord_db_and_augment_bitcoin_block(
167
202
new_tx. transaction_identifier. hash,
168
203
new_block. block_identifier. index,
169
204
inscription. content_type,
170
- ordinal_number
205
+ traversal . ordinal_number
171
206
) ;
172
207
} ) ;
173
208
0 commit comments