@@ -79,7 +79,7 @@ pub async fn start_predicate_api_server(
79
79
#[ openapi( tag = "Chainhooks" ) ]
80
80
#[ get( "/ping" ) ]
81
81
fn handle_ping ( ctx : & State < Context > ) -> Json < JsonValue > {
82
- ctx. try_log ( |logger| slog:: info!( logger, "GET /ping" ) ) ;
82
+ ctx. try_log ( |logger| slog:: info!( logger, "Handling HTTP GET /ping" ) ) ;
83
83
Json ( json ! ( {
84
84
"status" : 200 ,
85
85
"result" : "Ok" ,
@@ -92,42 +92,21 @@ fn handle_get_predicates(
92
92
predicate_db : & State < Arc < RwLock < Connection > > > ,
93
93
ctx : & State < Context > ,
94
94
) -> Json < JsonValue > {
95
- ctx. try_log ( |logger| slog:: info!( logger, "GET /v1/chainhooks" ) ) ;
96
- if let Ok ( chainhook_store_reader) = chainhook_store. inner ( ) . read ( ) {
97
- let mut predicates = vec ! [ ] ;
98
- let mut stacks_predicates = chainhook_store_reader
99
- . predicates
100
- . get_serialized_stacks_predicates ( )
101
- . iter ( )
102
- . map ( |( uuid, network, predicate) | {
103
- json ! ( {
104
- "chain" : "stacks" ,
105
- "uuid" : uuid,
106
- "network" : network,
107
- "predicate" : predicate,
108
- } )
109
- } )
110
- . collect :: < Vec < _ > > ( ) ;
111
- predicates. append ( & mut stacks_predicates) ;
95
+ ctx. try_log ( |logger| slog:: info!( logger, "Handling HTTP GET /v1/chainhooks" ) ) ;
96
+ if let Ok ( mut predicates_db_conn) = predicate_db. inner ( ) . write ( ) {
97
+ let predicates = match get_entries_from_predicates_db ( & mut predicates_db_conn, & ctx) {
98
+ Ok ( predicates) => predicates,
99
+ Err ( e) => unimplemented ! ( ) ,
100
+ } ;
112
101
113
- let mut bitcoin_predicates = chainhook_store_reader
114
- . predicates
115
- . get_serialized_bitcoin_predicates ( )
102
+ let serialized_predicates = predicates
116
103
. iter ( )
117
- . map ( |( uuid, network, predicate) | {
118
- json ! ( {
119
- "chain" : "bitcoin" ,
120
- "uuid" : uuid,
121
- "network" : network,
122
- "predicate" : predicate,
123
- } )
124
- } )
104
+ . map ( |( p, _) | p. into_serialized_json ( ) )
125
105
. collect :: < Vec < _ > > ( ) ;
126
- predicates. append ( & mut bitcoin_predicates) ;
127
106
128
107
Json ( json ! ( {
129
108
"status" : 200 ,
130
- "result" : predicates
109
+ "result" : serialized_predicates
131
110
} ) )
132
111
} else {
133
112
Json ( json ! ( {
@@ -145,7 +124,7 @@ fn handle_create_predicate(
145
124
background_job_tx : & State < Arc < Mutex < Sender < ObserverCommand > > > > ,
146
125
ctx : & State < Context > ,
147
126
) -> Json < JsonValue > {
148
- ctx. try_log ( |logger| slog:: info!( logger, "POST /v1/chainhooks" ) ) ;
127
+ ctx. try_log ( |logger| slog:: info!( logger, "Handling HTTP POST /v1/chainhooks" ) ) ;
149
128
let predicate = predicate. into_inner ( ) ;
150
129
if let Err ( e) = predicate. validate ( ) {
151
130
return Json ( json ! ( {
@@ -155,7 +134,11 @@ fn handle_create_predicate(
155
134
}
156
135
157
136
if let Ok ( mut predicates_db_conn) = predicate_db. inner ( ) . write ( ) {
158
- match get_entry_from_predicates_db ( & predicate. get_uuid ( ) , & mut predicates_db_conn, & ctx) {
137
+ match get_entry_from_predicates_db (
138
+ & ChainhookSpecification :: either_stx_or_btc_key ( predicate. get_uuid ( ) ) ,
139
+ & mut predicates_db_conn,
140
+ & ctx,
141
+ ) {
159
142
Ok ( Some ( _) ) => {
160
143
return Json ( json ! ( {
161
144
"status" : 409 ,
@@ -187,28 +170,44 @@ fn handle_get_predicate(
187
170
predicate_db : & State < Arc < RwLock < Connection > > > ,
188
171
ctx : & State < Context > ,
189
172
) -> Json < JsonValue > {
190
- ctx. try_log ( |logger| slog:: info!( logger, "GET /v1/chainhooks/{}" , predicate_uuid) ) ;
173
+ ctx. try_log ( |logger| {
174
+ slog:: info!(
175
+ logger,
176
+ "Handling HTTP GET /v1/chainhooks/{}" ,
177
+ predicate_uuid
178
+ )
179
+ } ) ;
191
180
192
181
if let Ok ( mut predicates_db_conn) = predicate_db. inner ( ) . write ( ) {
193
- match get_entry_from_predicates_db ( & predicate_uuid, & mut predicates_db_conn, & ctx) {
194
- Ok ( Some ( ( ChainhookSpecification :: Stacks ( spec) , status) ) ) => Json ( json ! ( {
182
+ let entry = match get_entry_from_predicates_db (
183
+ & ChainhookSpecification :: either_stx_or_btc_key ( & predicate_uuid) ,
184
+ & mut predicates_db_conn,
185
+ & ctx,
186
+ ) {
187
+ Ok ( Some ( ( ChainhookSpecification :: Stacks ( spec) , status) ) ) => json ! ( {
195
188
"chain" : "stacks" ,
196
189
"uuid" : spec. uuid,
197
190
"network" : spec. network,
198
191
"predicate" : spec. predicate,
199
192
"status" : status
200
- } ) ) ,
201
- Ok ( Some ( ( ChainhookSpecification :: Bitcoin ( spec) , status) ) ) => Json ( json ! ( {
193
+ } ) ,
194
+ Ok ( Some ( ( ChainhookSpecification :: Bitcoin ( spec) , status) ) ) => json ! ( {
202
195
"chain" : "bitcoin" ,
203
196
"uuid" : spec. uuid,
204
197
"network" : spec. network,
205
198
"predicate" : spec. predicate,
206
199
"status" : status
207
- } ) ) ,
208
- _ => Json ( json ! ( {
209
- "status" : 404 ,
210
- } ) ) ,
211
- }
200
+ } ) ,
201
+ _ => {
202
+ return Json ( json ! ( {
203
+ "status" : 404 ,
204
+ } ) )
205
+ }
206
+ } ;
207
+ Json ( json ! ( {
208
+ "status" : 200 ,
209
+ "result" : entry
210
+ } ) )
212
211
} else {
213
212
Json ( json ! ( {
214
213
"status" : 500 ,
@@ -224,7 +223,13 @@ fn handle_delete_stacks_predicate(
224
223
background_job_tx : & State < Arc < Mutex < Sender < ObserverCommand > > > > ,
225
224
ctx : & State < Context > ,
226
225
) -> Json < JsonValue > {
227
- ctx. try_log ( |logger| slog:: info!( logger, "DELETE /v1/chainhooks/stacks/{}" , predicate_uuid) ) ;
226
+ ctx. try_log ( |logger| {
227
+ slog:: info!(
228
+ logger,
229
+ "Handling HTTP DELETE /v1/chainhooks/stacks/{}" ,
230
+ predicate_uuid
231
+ )
232
+ } ) ;
228
233
229
234
let background_job_tx = background_job_tx. inner ( ) ;
230
235
match background_job_tx. lock ( ) {
@@ -247,7 +252,13 @@ fn handle_delete_bitcoin_predicate(
247
252
background_job_tx : & State < Arc < Mutex < Sender < ObserverCommand > > > > ,
248
253
ctx : & State < Context > ,
249
254
) -> Json < JsonValue > {
250
- ctx. try_log ( |logger| slog:: info!( logger, "DELETE /v1/chainhooks/bitcoin/{}" , predicate_uuid) ) ;
255
+ ctx. try_log ( |logger| {
256
+ slog:: info!(
257
+ logger,
258
+ "Handling HTTP DELETE /v1/chainhooks/bitcoin/{}" ,
259
+ predicate_uuid
260
+ )
261
+ } ) ;
251
262
252
263
let background_job_tx = background_job_tx. inner ( ) ;
253
264
match background_job_tx. lock ( ) {
@@ -264,19 +275,17 @@ fn handle_delete_bitcoin_predicate(
264
275
}
265
276
266
277
pub fn get_entry_from_predicates_db (
267
- uuid : & str ,
278
+ predicate_key : & str ,
268
279
predicate_db_conn : & mut Connection ,
269
280
_ctx : & Context ,
270
281
) -> Result < Option < ( ChainhookSpecification , PredicateStatus ) > , String > {
271
- let entry: HashMap < String , String > = predicate_db_conn
272
- . hgetall ( ChainhookSpecification :: either_stx_or_btc_key ( uuid) )
273
- . map_err ( |e| {
274
- format ! (
275
- "unable to load chainhook associated with key {}: {}" ,
276
- uuid,
277
- e. to_string( )
278
- )
279
- } ) ?;
282
+ let entry: HashMap < String , String > = predicate_db_conn. hgetall ( predicate_key) . map_err ( |e| {
283
+ format ! (
284
+ "unable to load chainhook associated with key {}: {}" ,
285
+ predicate_key,
286
+ e. to_string( )
287
+ )
288
+ } ) ?;
280
289
281
290
let encoded_spec = match entry. get ( "specification" ) {
282
291
None => return Ok ( None ) ,
@@ -294,7 +303,7 @@ pub fn get_entry_from_predicates_db(
294
303
} ;
295
304
296
305
let status = match serde_json:: from_str ( & encoded_status) {
297
- Err ( e) => unimplemented ! ( ) ,
306
+ Err ( e) => unimplemented ! ( ) , // TODO
298
307
Ok ( status) => status,
299
308
} ;
300
309
@@ -304,33 +313,29 @@ pub fn get_entry_from_predicates_db(
304
313
pub fn get_entries_from_predicates_db (
305
314
predicate_db_conn : & mut Connection ,
306
315
ctx : & Context ,
307
- ) -> Result < Vec < ChainhookSpecification > , String > {
316
+ ) -> Result < Vec < ( ChainhookSpecification , PredicateStatus ) > , String > {
308
317
let chainhooks_to_load: Vec < String > = predicate_db_conn
309
318
. scan_match ( ChainhookSpecification :: either_stx_or_btc_key ( "*" ) )
310
319
. map_err ( |e| format ! ( "unable to connect to redis: {}" , e. to_string( ) ) ) ?
311
320
. into_iter ( )
312
321
. collect ( ) ;
313
322
314
323
let mut predicates = vec ! [ ] ;
315
- for key in chainhooks_to_load. iter ( ) {
316
- let chainhook = match predicate_db_conn. hget :: < _ , _ , String > ( key, "specification" ) {
317
- Ok ( spec) => match ChainhookSpecification :: deserialize_specification ( & spec) {
318
- Ok ( spec) => spec,
319
- Err ( e) => {
320
- error ! (
321
- ctx. expect_logger( ) ,
322
- "unable to load chainhook associated with key {}: {}" ,
323
- key,
324
- e. to_string( )
325
- ) ;
326
- continue ;
327
- }
328
- } ,
324
+ for predicate_key in chainhooks_to_load. iter ( ) {
325
+ let chainhook = match get_entry_from_predicates_db ( predicate_key, predicate_db_conn, ctx) {
326
+ Ok ( Some ( ( spec, status) ) ) => ( spec, status) ,
327
+ Ok ( None ) => {
328
+ warn ! (
329
+ ctx. expect_logger( ) ,
330
+ "unable to load chainhook associated with key {}" , predicate_key,
331
+ ) ;
332
+ continue ;
333
+ }
329
334
Err ( e) => {
330
335
error ! (
331
336
ctx. expect_logger( ) ,
332
337
"unable to load chainhook associated with key {}: {}" ,
333
- key ,
338
+ predicate_key ,
334
339
e. to_string( )
335
340
) ;
336
341
continue ;
@@ -344,7 +349,7 @@ pub fn get_entries_from_predicates_db(
344
349
pub fn load_predicates_from_redis (
345
350
config : & crate :: config:: Config ,
346
351
ctx : & Context ,
347
- ) -> Result < Vec < ChainhookSpecification > , String > {
352
+ ) -> Result < Vec < ( ChainhookSpecification , PredicateStatus ) > , String > {
348
353
let redis_uri: & str = config. expected_api_database_uri ( ) ;
349
354
let client = redis:: Client :: open ( redis_uri. clone ( ) )
350
355
. map_err ( |e| format ! ( "unable to connect to redis: {}" , e. to_string( ) ) ) ?;
0 commit comments