@@ -4,6 +4,9 @@ use crate::config::{Config, PredicatesApi};
4
4
use crate :: scan:: bitcoin:: scan_bitcoin_chainstate_via_rpc_using_predicate;
5
5
use crate :: scan:: stacks:: scan_stacks_chainstate_via_csv_using_predicate;
6
6
use crate :: service:: Service ;
7
+ use crate :: storage:: {
8
+ get_last_block_height_inserted, is_stacks_block_present, open_readonly_stacks_db_conn,
9
+ } ;
7
10
8
11
use chainhook_sdk:: bitcoincore_rpc:: { Auth , Client , RpcApi } ;
9
12
use chainhook_sdk:: chainhooks:: types:: {
@@ -59,9 +62,12 @@ enum Command {
59
62
/// Run a service streaming blocks and evaluating registered predicates
60
63
#[ clap( subcommand) ]
61
64
Service ( ServiceCommand ) ,
62
- /// Explore the Ordinal Theory
65
+ /// Ordinals related subcommands
63
66
#[ clap( subcommand) ]
64
67
Hord ( HordCommand ) ,
68
+ /// Stacks related subcommands
69
+ #[ clap( subcommand) ]
70
+ Stacks ( StacksCommand ) ,
65
71
}
66
72
67
73
#[ derive( Subcommand , PartialEq , Clone , Debug ) ]
@@ -192,14 +198,14 @@ struct StartCommand {
192
198
enum HordCommand {
193
199
/// Db maintenance related commands
194
200
#[ clap( subcommand) ]
195
- Db ( DbCommand ) ,
201
+ Db ( HordDbCommand ) ,
196
202
/// Db maintenance related commands
197
203
#[ clap( subcommand) ]
198
204
Scan ( ScanCommand ) ,
199
205
}
200
206
201
207
#[ derive( Subcommand , PartialEq , Clone , Debug ) ]
202
- enum DbCommand {
208
+ enum HordDbCommand {
203
209
/// Rewrite hord db
204
210
#[ clap( name = "rewrite" , bin_name = "rewrite" ) ]
205
211
Rewrite ( UpdateHordDbCommand ) ,
@@ -211,7 +217,7 @@ enum DbCommand {
211
217
Drop ( DropHordDbCommand ) ,
212
218
/// Check integrity
213
219
#[ clap( name = "check" , bin_name = "check" ) ]
214
- Check ( CheckHordDbCommand ) ,
220
+ Check ( CheckDbCommand ) ,
215
221
/// Patch DB
216
222
#[ clap( name = "patch" , bin_name = "patch" ) ]
217
223
Patch ( PatchHordDbCommand ) ,
@@ -220,6 +226,20 @@ enum DbCommand {
220
226
Migrate ( MigrateHordDbCommand ) ,
221
227
}
222
228
229
+ #[ derive( Subcommand , PartialEq , Clone , Debug ) ]
230
+ enum StacksCommand {
231
+ /// Db maintenance related commands
232
+ #[ clap( subcommand) ]
233
+ Db ( StacksDbCommand ) ,
234
+ }
235
+
236
+ #[ derive( Subcommand , PartialEq , Clone , Debug ) ]
237
+ enum StacksDbCommand {
238
+ /// Check integrity
239
+ #[ clap( name = "check" , bin_name = "check" ) ]
240
+ Check ( CheckDbCommand ) ,
241
+ }
242
+
223
243
#[ derive( Subcommand , PartialEq , Clone , Debug ) ]
224
244
enum ScanCommand {
225
245
/// Compute ordinal number of the 1st satoshi of the 1st input of a given transaction
@@ -348,7 +368,7 @@ struct MigrateHordDbCommand {
348
368
}
349
369
350
370
#[ derive( Parser , PartialEq , Clone , Debug ) ]
351
- struct CheckHordDbCommand {
371
+ struct CheckDbCommand {
352
372
/// Load config file path
353
373
#[ clap( long = "config-path" ) ]
354
374
pub config_path : Option < String > ,
@@ -752,7 +772,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
752
772
}
753
773
} ,
754
774
Command :: Hord ( HordCommand :: Db ( subcmd) ) => match subcmd {
755
- DbCommand :: Sync ( cmd) => {
775
+ HordDbCommand :: Sync ( cmd) => {
756
776
let config = Config :: default ( false , false , false , & cmd. config_path ) ?;
757
777
if let Some ( ( start_block, end_block) ) = should_sync_hord_db ( & config, & ctx) ? {
758
778
if start_block == 0 {
@@ -778,7 +798,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
778
798
info ! ( ctx. expect_logger( ) , "Database hord up to date" ) ;
779
799
}
780
800
}
781
- DbCommand :: Rewrite ( cmd) => {
801
+ HordDbCommand :: Rewrite ( cmd) => {
782
802
let config = Config :: default ( false , false , false , & cmd. config_path ) ?;
783
803
// Delete data, if any
784
804
{
@@ -805,15 +825,15 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
805
825
)
806
826
. await ?;
807
827
}
808
- DbCommand :: Check ( cmd) => {
828
+ HordDbCommand :: Check ( cmd) => {
809
829
let config = Config :: default ( false , false , false , & cmd. config_path ) ?;
810
830
// Delete data, if any
811
831
{
812
832
let blocks_db_rw =
813
833
open_readwrite_hord_db_conn_rocks_db ( & config. expected_cache_path ( ) , & ctx) ?;
814
834
815
835
let mut missing_blocks = vec ! [ ] ;
816
- for i in 1 ..=780000 {
836
+ for i in 1 ..=790000 {
817
837
if find_lazy_block_at_block_height ( i, 3 , & blocks_db_rw) . is_none ( ) {
818
838
println ! ( "Missing block {i}" ) ;
819
839
missing_blocks. push ( i) ;
@@ -822,7 +842,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
822
842
println ! ( "{:?}" , missing_blocks) ;
823
843
}
824
844
}
825
- DbCommand :: Drop ( cmd) => {
845
+ HordDbCommand :: Drop ( cmd) => {
826
846
let config = Config :: default ( false , false , false , & cmd. config_path ) ?;
827
847
let blocks_db =
828
848
open_readwrite_hord_db_conn_rocks_db ( & config. expected_cache_path ( ) , & ctx) ?;
@@ -842,13 +862,42 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
842
862
cmd. end_block - cmd. start_block + 1
843
863
) ;
844
864
}
845
- DbCommand :: Patch ( _cmd) => {
865
+ HordDbCommand :: Patch ( _cmd) => {
846
866
unimplemented ! ( )
847
867
}
848
- DbCommand :: Migrate ( _cmd) => {
868
+ HordDbCommand :: Migrate ( _cmd) => {
849
869
unimplemented ! ( )
850
870
}
851
871
} ,
872
+ Command :: Stacks ( StacksCommand :: Db ( StacksDbCommand :: Check ( cmd) ) ) => {
873
+ let config = Config :: default ( false , false , false , & cmd. config_path ) ?;
874
+ // Delete data, if any
875
+ {
876
+ let stacks_db = open_readonly_stacks_db_conn ( & config. expected_cache_path ( ) , & ctx) ?;
877
+ let mut missing_blocks = vec ! [ ] ;
878
+ if let Some ( tip) = get_last_block_height_inserted ( & stacks_db, & ctx) {
879
+ for index in 1 ..=tip {
880
+ let block_identifier = BlockIdentifier {
881
+ index : i,
882
+ hash : "" . into ( ) ,
883
+ } ;
884
+ if !is_stacks_block_present ( & block_identifier, 3 , & stacks_db) {
885
+ missing_blocks. push ( i) ;
886
+ }
887
+ }
888
+ }
889
+ if missing_blocks. is_empty ( ) {
890
+ info ! ( ctx. expect_logger( ) , "Stacks db successfully checked" ) ;
891
+ } else {
892
+ warn ! (
893
+ ctx. expect_logger( ) ,
894
+ "Stacks db includes {} missing entries: {:?}" ,
895
+ missing_blocks. len( ) ,
896
+ missing_blocks
897
+ ) ;
898
+ }
899
+ }
900
+ }
852
901
}
853
902
Ok ( ( ) )
854
903
}
0 commit comments