@@ -43,7 +43,7 @@ pub struct ApiMetrics {
4343
4444#[ derive( Clone ) ]
4545pub struct ApiState {
46- pub chains : Arc < RwLock < HashMap < ChainId , BlockchainState > > > ,
46+ pub chains : Arc < RwLock < HashMap < ChainId , ApiBlockChainState > > > ,
4747
4848 pub metrics_registry : Arc < RwLock < Registry > > ,
4949
@@ -53,7 +53,7 @@ pub struct ApiState {
5353
5454impl ApiState {
5555 pub async fn new (
56- chains : Arc < RwLock < HashMap < ChainId , BlockchainState > > > ,
56+ chains : Arc < RwLock < HashMap < ChainId , ApiBlockChainState > > > ,
5757 metrics_registry : Arc < RwLock < Registry > > ,
5858 ) -> ApiState {
5959 let metrics = ApiMetrics {
@@ -94,6 +94,12 @@ pub struct BlockchainState {
9494 pub confirmed_block_status : BlockStatus ,
9595}
9696
97+ #[ derive( Clone ) ]
98+ pub enum ApiBlockChainState {
99+ Uninitialized ,
100+ Initialized ( BlockchainState ) ,
101+ }
102+
97103pub enum RestError {
98104 /// The caller passed a sequence number that isn't within the supported range
99105 InvalidSequenceNumber ,
@@ -108,6 +114,9 @@ pub enum RestError {
108114 /// The server cannot currently communicate with the blockchain, so is not able to verify
109115 /// which random values have been requested.
110116 TemporarilyUnavailable ,
117+ /// The server is not able to process the request because the blockchain initialization
118+ /// has not been completed yet.
119+ Uninitialized ,
111120 /// A catch-all error for all other types of errors that could occur during processing.
112121 Unknown ,
113122}
@@ -137,6 +146,11 @@ impl IntoResponse for RestError {
137146 "This service is temporarily unavailable" ,
138147 )
139148 . into_response ( ) ,
149+ RestError :: Uninitialized => (
150+ StatusCode :: SERVICE_UNAVAILABLE ,
151+ "The service is not yet initialized for this chain, please try again in a few minutes" ,
152+ )
153+ . into_response ( ) ,
140154 RestError :: Unknown => (
141155 StatusCode :: INTERNAL_SERVER_ERROR ,
142156 "An unknown error occurred processing the request" ,
@@ -172,6 +186,7 @@ pub fn get_register_uri(base_uri: &str, chain_id: &str) -> Result<String> {
172186
173187#[ cfg( test) ]
174188mod test {
189+ use crate :: api:: ApiBlockChainState ;
175190 use {
176191 crate :: {
177192 api:: { self , ApiState , BinaryEncoding , Blob , BlockchainState , GetRandomValueResponse } ,
@@ -228,8 +243,14 @@ mod test {
228243 } ;
229244
230245 let mut chains = HashMap :: new ( ) ;
231- chains. insert ( "ethereum" . into ( ) , eth_state) ;
232- chains. insert ( "avalanche" . into ( ) , avax_state) ;
246+ chains. insert (
247+ "ethereum" . into ( ) ,
248+ ApiBlockChainState :: Initialized ( eth_state) ,
249+ ) ;
250+ chains. insert (
251+ "avalanche" . into ( ) ,
252+ ApiBlockChainState :: Initialized ( avax_state) ,
253+ ) ;
233254
234255 let api_state = ApiState :: new ( Arc :: new ( RwLock :: new ( chains) ) , metrics_registry) . await ;
235256
0 commit comments