@@ -260,16 +260,16 @@ impl IndexerService {
260
260
opts
261
261
}
262
262
263
- pub ( crate ) fn apply_init_tip (
263
+ pub ( crate ) fn apply_init_tip < T : ChainStore > (
264
264
is_store_initialized : bool ,
265
265
init_tip_hash : & Option < H256 > ,
266
266
store : & RocksdbStore ,
267
- secondary_db : & SecondaryDB ,
267
+ secondary_db : & T ,
268
268
) -> Result < ( ) , Error > {
269
269
if let ( true , Some ( init_tip_hash) ) = ( !is_store_initialized, init_tip_hash) {
270
270
let init_tip_number = secondary_db
271
271
. get_block_header ( & init_tip_hash. pack ( ) )
272
- . ok_or ( Error :: Params ( "Setting the initial tip failed: could not find the block corresponding to the init tip hash."
272
+ . ok_or ( Error :: Params ( "setting the initial tip failed: could not find the block corresponding to the init tip hash."
273
273
. to_string ( ) ,
274
274
) ) ?
275
275
. number ( ) ;
@@ -983,13 +983,20 @@ impl TryInto<FilterOptions> for IndexerSearchKey {
983
983
mod tests {
984
984
use super :: * ;
985
985
use crate :: store:: RocksdbStore ;
986
+ use ckb_db:: {
987
+ iter:: { DBIter , IteratorMode } ,
988
+ DBPinnableSlice ,
989
+ } ;
990
+ use ckb_db_schema:: Col ;
986
991
use ckb_jsonrpc_types:: { IndexerRange , IndexerSearchKeyFilter } ;
992
+ use ckb_store:: { Freezer , StoreCache } ;
987
993
use ckb_types:: {
988
994
bytes:: Bytes ,
989
995
core:: {
990
996
capacity_bytes, BlockBuilder , Capacity , EpochNumberWithFraction , HeaderBuilder ,
991
- ScriptHashType , TransactionBuilder ,
997
+ HeaderView , ScriptHashType , TransactionBuilder ,
992
998
} ,
999
+ h256,
993
1000
packed:: { CellInput , CellOutputBuilder , OutPoint , Script , ScriptBuilder } ,
994
1001
H256 ,
995
1002
} ;
@@ -1003,6 +1010,49 @@ mod tests {
1003
1010
// Indexer::new(store, 10, 1)
1004
1011
}
1005
1012
1013
+ fn new_chain_store ( prefix : & str ) -> impl ChainStore {
1014
+ struct DummyChainStore {
1015
+ inner : RocksdbStore ,
1016
+ }
1017
+ impl ChainStore for DummyChainStore {
1018
+ fn cache ( & self ) -> Option < & StoreCache > {
1019
+ None
1020
+ }
1021
+ fn freezer ( & self ) -> Option < & Freezer > {
1022
+ None
1023
+ }
1024
+ fn get ( & self , _col : Col , _key : & [ u8 ] ) -> Option < DBPinnableSlice > {
1025
+ None
1026
+ }
1027
+ fn get_iter ( & self , _col : Col , mode : IteratorMode ) -> DBIter {
1028
+ let opts = ReadOptions :: default ( ) ;
1029
+ self . inner . inner ( ) . get_iter ( & opts, mode)
1030
+ }
1031
+ fn get_block_header ( & self , hash : & packed:: Byte32 ) -> Option < HeaderView > {
1032
+ match hash {
1033
+ _ if hash
1034
+ == & h256 ! (
1035
+ "0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"
1036
+ )
1037
+ . pack ( ) =>
1038
+ {
1039
+ let epoch = EpochNumberWithFraction :: new ( 0 , 0 , 10 ) ;
1040
+ Some (
1041
+ HeaderView :: new_advanced_builder ( )
1042
+ . number ( 100000 . pack ( ) )
1043
+ . epoch ( epoch. pack ( ) )
1044
+ . build ( ) ,
1045
+ )
1046
+ }
1047
+ _ => None ,
1048
+ }
1049
+ }
1050
+ }
1051
+ DummyChainStore {
1052
+ inner : new_store ( prefix) ,
1053
+ }
1054
+ }
1055
+
1006
1056
#[ test]
1007
1057
fn rpc ( ) {
1008
1058
let store = new_store ( "rpc" ) ;
@@ -1591,6 +1641,38 @@ mod tests {
1591
1641
) ;
1592
1642
}
1593
1643
1644
+ #[ test]
1645
+ fn rpc_get_indexer_tip_with_set_init_tip ( ) {
1646
+ let store = new_store ( "rpc_get_indexer_tip_with_set_init_tip" ) ;
1647
+ let ckb_db = new_chain_store ( "rpc_get_indexer_tip_with_set_init_tip_ckb" ) ;
1648
+
1649
+ // test setting the initial tip failed
1650
+ let ret = IndexerService :: apply_init_tip ( false , & Some ( H256 :: default ( ) ) , & store, & ckb_db) ;
1651
+ assert_eq ! ( ret. unwrap_err( ) . to_string( ) , "Invalid params setting the initial tip failed: could not find the block corresponding to the init tip hash." ) ;
1652
+
1653
+ // test get_tip rpc
1654
+ IndexerService :: apply_init_tip (
1655
+ false ,
1656
+ & Some ( h256 ! (
1657
+ "0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44"
1658
+ ) ) ,
1659
+ & store,
1660
+ & ckb_db,
1661
+ )
1662
+ . unwrap ( ) ;
1663
+ let pool = Arc :: new ( RwLock :: new ( Pool :: default ( ) ) ) ;
1664
+ let rpc = IndexerHandle {
1665
+ store,
1666
+ pool : Some ( Arc :: clone ( & pool) ) ,
1667
+ } ;
1668
+ let tip = rpc. get_indexer_tip ( ) . unwrap ( ) . unwrap ( ) ;
1669
+ assert_eq ! (
1670
+ h256!( "0x8fbd0ec887159d2814cee475911600e3589849670f5ee1ed9798b38fdeef4e44" ) ,
1671
+ tip. block_hash
1672
+ ) ;
1673
+ assert_eq ! ( 100000 , tip. block_number. value( ) ) ;
1674
+ }
1675
+
1594
1676
#[ test]
1595
1677
fn script_search_mode_rpc ( ) {
1596
1678
let store = new_store ( "script_search_mode_rpc" ) ;
0 commit comments