@@ -8,7 +8,7 @@ use ckb_bitcoin_spv_verifier::{
8
8
utilities:: { bitcoin:: calculate_next_target, mmr} ,
9
9
} ;
10
10
11
- use crate :: result:: Result ;
11
+ use crate :: result:: { Error , Result } ;
12
12
13
13
/// A dummy service for testing the SPV client cells's bootstrap and update.
14
14
pub struct DummyService {
@@ -20,7 +20,11 @@ pub struct DummyService {
20
20
impl DummyService {
21
21
pub fn bootstrap ( height : u32 , header : core:: Header ) -> Result < Self > {
22
22
if height % DIFFCHANGE_INTERVAL != 0 {
23
- panic ! ( "bad height" ) ;
23
+ let msg = format ! (
24
+ "bad bootstrap height, expected multiples of \
25
+ {DIFFCHANGE_INTERVAL} but got {height}"
26
+ ) ;
27
+ return Err ( Error :: other ( msg) ) ;
24
28
}
25
29
let mut headers = HashMap :: new ( ) ;
26
30
let store = mmr:: lib:: util:: MemStore :: default ( ) ;
@@ -117,6 +121,26 @@ impl DummyService {
117
121
. build ( ) )
118
122
}
119
123
124
+ // The `prev_client` is not checked, since this is just a dummy service for testing purpose only.
125
+ pub fn rollback_to ( & mut self , prev_client : core:: SpvClient ) -> Result < ( ) > {
126
+ let prev_height = prev_client. headers_mmr_root . max_height ;
127
+ if prev_height < self . client . headers_mmr_root . min_height
128
+ || self . client . headers_mmr_root . max_height < prev_height
129
+ {
130
+ let msg = format ! (
131
+ "the previous header (height: {prev_height}) is not found (current: [{}, {}])" ,
132
+ self . client. headers_mmr_root. min_height, self . client. headers_mmr_root. max_height
133
+ ) ;
134
+ return Err ( Error :: other ( msg) ) ;
135
+ }
136
+ let curr_height = self . client . headers_mmr_root . max_height ;
137
+ for h in ( prev_height + 1 ) ..=curr_height {
138
+ self . headers . remove ( & h) ;
139
+ }
140
+ self . client = prev_client;
141
+ Ok ( ( ) )
142
+ }
143
+
120
144
pub fn tip_client ( & self ) -> core:: SpvClient {
121
145
self . client . clone ( )
122
146
}
0 commit comments