1- use std:: collections:: { BTreeMap , VecDeque } ;
21use chrono:: DateTime ;
32use ethers:: types:: TxHash ;
4- use serde:: { Deserialize , Serialize } ;
3+ use serde:: Serialize ;
4+ use std:: collections:: BTreeMap ;
55use utoipa:: ToSchema ;
66use {
77 crate :: {
@@ -27,14 +27,15 @@ use {
2727 url:: Url ,
2828} ;
2929pub use { chain_ids:: * , index:: * , live:: * , metrics:: * , ready:: * , revelation:: * } ;
30+ use crate :: api:: explorer:: get_requests;
3031
3132mod chain_ids;
33+ mod explorer;
3234mod index;
3335mod live;
3436mod metrics;
3537mod ready;
3638mod revelation;
37- mod explorer;
3839
3940pub type ChainId = String ;
4041
@@ -47,22 +48,12 @@ pub struct ApiMetrics {
4748 pub http_requests : Family < RequestLabel , Counter > ,
4849}
4950
50-
51-
5251#[ derive( Clone , Debug , Serialize ) ]
53- enum JournalLog {
54- Observed {
55- tx_hash : TxHash
56- } ,
57- FailedToReveal {
58- reason : String ,
59- } ,
60- Revealed {
61- tx_hash : TxHash
62- } ,
63- Landed {
64- block_number : BlockNumber ,
65- }
52+ pub enum JournalLog {
53+ Observed { tx_hash : TxHash } ,
54+ FailedToReveal { reason : String } ,
55+ Revealed { tx_hash : TxHash } ,
56+ Landed { block_number : BlockNumber } ,
6657}
6758
6859impl JournalLog {
@@ -77,13 +68,13 @@ impl JournalLog {
7768}
7869
7970#[ derive( Clone , Debug , Serialize ) ]
80- struct TimedJournalLog {
71+ pub struct TimedJournalLog {
8172 pub timestamp : DateTime < chrono:: Utc > ,
82- pub log : JournalLog ,
73+ pub log : JournalLog ,
8374}
8475
8576#[ derive( Clone , Debug , Serialize , ToSchema ) ]
86- struct RequestJournal {
77+ pub struct RequestJournal {
8778 pub chain_id : ChainId ,
8879 pub sequence : u64 ,
8980 pub journal : Vec < TimedJournalLog > ,
@@ -105,27 +96,32 @@ impl History {
10596 Self :: default ( )
10697 }
10798
108- pub fn add ( & mut self , ( chain_id, sequence) : RequestKey , request_journal_log : TimedJournalLog ) {
99+ pub fn add ( & mut self , ( chain_id, sequence) : RequestKey , request_journal_log : TimedJournalLog ) {
109100 let mut new_entry = false ;
110- let entry = self . by_request_key . entry ( ( chain_id. clone ( ) , sequence) ) . or_insert_with ( || {
111- new_entry = true ;
112- RequestJournal {
113- chain_id : chain_id. clone ( ) ,
114- sequence,
115- journal : vec ! [ ] ,
116- }
117- } ) ;
118- request_journal_log. log . get_tx_hash ( ) . map ( |tx_hash| {
101+ let entry = self
102+ . by_request_key
103+ . entry ( ( chain_id. clone ( ) , sequence) )
104+ . or_insert_with ( || {
105+ new_entry = true ;
106+ RequestJournal {
107+ chain_id : chain_id. clone ( ) ,
108+ sequence,
109+ journal : vec ! [ ] ,
110+ }
111+ } ) ;
112+ if let Some ( tx_hash) = request_journal_log. log . get_tx_hash ( ) {
119113 self . by_hash
120114 . entry ( tx_hash)
121- . or_insert_with ( Vec :: new )
115+ . or_default ( )
122116 . push ( ( chain_id. clone ( ) , sequence) ) ;
123- } ) ;
117+ }
124118 entry. journal . push ( request_journal_log) ;
125119 if new_entry {
126120 let current_time = chrono:: Utc :: now ( ) ;
127- self . by_chain_and_time
128- . insert ( ( chain_id. clone ( ) , current_time) , ( chain_id. clone ( ) , sequence) ) ;
121+ self . by_chain_and_time . insert (
122+ ( chain_id. clone ( ) , current_time) ,
123+ ( chain_id. clone ( ) , sequence) ,
124+ ) ;
129125 self . by_time
130126 . insert ( current_time, ( chain_id. clone ( ) , sequence) ) ;
131127
@@ -140,42 +136,56 @@ impl History {
140136 }
141137
142138 pub fn get_request_logs_by_tx_hash ( & self , tx_hash : TxHash ) -> Vec < RequestJournal > {
143- self . by_hash . get ( & tx_hash) . map ( |request_keys| {
144- request_keys. iter ( )
145- . map ( |request_key| self . by_request_key . get ( request_key) . unwrap ( ) . clone ( ) )
146- . collect ( )
147- } ) . unwrap_or_default ( )
139+ self . by_hash
140+ . get ( & tx_hash)
141+ . map ( |request_keys| {
142+ request_keys
143+ . iter ( )
144+ . map ( |request_key| self . by_request_key . get ( request_key) . unwrap ( ) . clone ( ) )
145+ . collect ( )
146+ } )
147+ . unwrap_or_default ( )
148148 }
149149
150- pub fn get_latest_requests ( & self , chain_id : Option < & ChainId > , limit : u64 ,
151- min_timestamp : Option < DateTime < chrono:: Utc > > ,
152- max_timestamp : Option < DateTime < chrono:: Utc > > ) -> Vec < RequestJournal > {
150+ pub fn get_latest_requests (
151+ & self ,
152+ chain_id : Option < & ChainId > ,
153+ limit : u64 ,
154+ min_timestamp : Option < DateTime < chrono:: Utc > > ,
155+ max_timestamp : Option < DateTime < chrono:: Utc > > ,
156+ ) -> Vec < RequestJournal > {
153157 match chain_id {
154158 Some ( chain_id) => {
155- let range = self . by_chain_and_time . range ( ( chain_id. clone ( ) , min_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MIN_UTC ) ) ..( chain_id. clone ( ) , max_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MAX_UTC ) ) ) ;
156- range. rev ( )
157- . take ( limit as usize )
158- . map ( |( _, request_key) | {
159- self . by_request_key . get ( request_key) . unwrap ( ) . clone ( )
160- } )
161- . collect ( )
162-
163- } ,
164- None => {
165- self . by_time
166- . range ( min_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MIN_UTC ) ..max_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MAX_UTC ) )
159+ let range = self . by_chain_and_time . range (
160+ (
161+ chain_id. clone ( ) ,
162+ min_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MIN_UTC ) ,
163+ )
164+ ..(
165+ chain_id. clone ( ) ,
166+ max_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MAX_UTC ) ,
167+ ) ,
168+ ) ;
169+ range
167170 . rev ( )
168171 . take ( limit as usize )
169- . map ( |( _time, request_key) | {
170- self . by_request_key . get ( request_key) . unwrap ( ) . clone ( )
171- } )
172- . collect :: < Vec < _ > > ( )
173- } ,
172+ . map ( |( _, request_key) | self . by_request_key . get ( request_key) . unwrap ( ) . clone ( ) )
173+ . collect ( )
174+ }
175+ None => self
176+ . by_time
177+ . range (
178+ min_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MIN_UTC )
179+ ..max_timestamp. unwrap_or ( DateTime :: < chrono:: Utc > :: MAX_UTC ) ,
180+ )
181+ . rev ( )
182+ . take ( limit as usize )
183+ . map ( |( _time, request_key) | self . by_request_key . get ( request_key) . unwrap ( ) . clone ( ) )
184+ . collect :: < Vec < _ > > ( ) ,
174185 }
175186 }
176187}
177188
178-
179189#[ derive( Clone ) ]
180190pub struct ApiState {
181191 pub chains : Arc < RwLock < HashMap < ChainId , ApiBlockChainState > > > ,
@@ -192,7 +202,7 @@ impl ApiState {
192202 pub async fn new (
193203 chains : Arc < RwLock < HashMap < ChainId , ApiBlockChainState > > > ,
194204 metrics_registry : Arc < RwLock < Registry > > ,
195- history : Arc < RwLock < History > >
205+ history : Arc < RwLock < History > > ,
196206 ) -> ApiState {
197207 let metrics = ApiMetrics {
198208 http_requests : Family :: default ( ) ,
@@ -312,6 +322,7 @@ pub fn routes(state: ApiState) -> Router<(), Body> {
312322 . route ( "/metrics" , get ( metrics) )
313323 . route ( "/ready" , get ( ready) )
314324 . route ( "/v1/chains" , get ( chain_ids) )
325+ . route ( "/v1/explorer" , get ( get_requests) )
315326 . route (
316327 "/v1/chains/:chain_id/revelations/:sequence" ,
317328 get ( revelation) ,
@@ -397,7 +408,7 @@ mod test {
397408 ApiBlockChainState :: Initialized ( avax_state) ,
398409 ) ;
399410
400- let api_state = ApiState :: new ( Arc :: new ( RwLock :: new ( chains) ) , metrics_registry) . await ;
411+ let api_state = ApiState :: new ( Arc :: new ( RwLock :: new ( chains) ) , metrics_registry, Default :: default ( ) ) . await ;
401412
402413 let app = api:: routes ( api_state) ;
403414 ( TestServer :: new ( app) . unwrap ( ) , eth_read, avax_read)
0 commit comments