1+ use crate :: chain:: reader:: BlockNumber ;
12use {
23 crate :: api:: { ChainId , RequestLabel , RestError } ,
34 anyhow:: Result ,
2021/// Callers must pass the appropriate chain_id to ensure they fetch the correct random number.
2122#[ utoipa:: path(
2223get,
23- path = "/v1/chains/{chain_id}/revelations/{sequence}" ,
24+ path = "/v1/chains/{chain_id}/revelations/{block_number}/{ sequence}" ,
2425responses(
2526( status = 200 , description = "Random value successfully retrieved" , body = GetRandomValueResponse ) ,
2627( status = 403 , description = "Random value cannot currently be retrieved" , body = String )
@@ -29,7 +30,11 @@ params(RevelationPathParams, RevelationQueryParams)
2930) ]
3031pub async fn revelation (
3132 State ( state) : State < crate :: api:: ApiState > ,
32- Path ( RevelationPathParams { chain_id, sequence } ) : Path < RevelationPathParams > ,
33+ Path ( RevelationPathParams {
34+ chain_id,
35+ sequence,
36+ block_number,
37+ } ) : Path < RevelationPathParams > ,
3338 Query ( RevelationQueryParams { encoding } ) : Query < RevelationQueryParams > ,
3439) -> Result < Json < GetRandomValueResponse > , RestError > {
3540 state
@@ -45,7 +50,11 @@ pub async fn revelation(
4550 . get ( & chain_id)
4651 . ok_or ( RestError :: InvalidChainId ) ?;
4752
48- let maybe_request_fut = state. contract . get_request ( state. provider_address , sequence) ;
53+ let maybe_request_fut = state. contract . get_request_with_callback_events (
54+ block_number,
55+ block_number,
56+ state. provider_address ,
57+ ) ;
4958
5059 let current_block_number_fut = state
5160 . contract
@@ -57,28 +66,29 @@ pub async fn revelation(
5766 RestError :: TemporarilyUnavailable
5867 } ) ?;
5968
60- match maybe_request {
61- Some ( r)
62- if current_block_number. saturating_sub ( state. reveal_delay_blocks ) >= r. block_number =>
63- {
64- let value = & state. state . reveal ( sequence) . map_err ( |e| {
65- tracing:: error!(
66- chain_id = chain_id,
67- sequence = sequence,
68- "Reveal failed {}" ,
69- e
70- ) ;
71- RestError :: Unknown
72- } ) ?;
73- let encoded_value = Blob :: new ( encoding. unwrap_or ( BinaryEncoding :: Hex ) , * value) ;
74-
75- Ok ( Json ( GetRandomValueResponse {
76- value : encoded_value,
77- } ) )
78- }
79- Some ( _) => Err ( RestError :: PendingConfirmation ) ,
80- None => Err ( RestError :: NoPendingRequest ) ,
69+ if current_block_number. saturating_sub ( state. reveal_delay_blocks ) < block_number {
70+ return Err ( RestError :: PendingConfirmation ) ;
8171 }
72+
73+ maybe_request
74+ . iter ( )
75+ . find ( |r| r. sequence_number == sequence)
76+ . ok_or ( RestError :: NoPendingRequest ) ?;
77+
78+ let value = & state. state . reveal ( sequence) . map_err ( |e| {
79+ tracing:: error!(
80+ chain_id = chain_id,
81+ sequence = sequence,
82+ "Reveal failed {}" ,
83+ e
84+ ) ;
85+ RestError :: Unknown
86+ } ) ?;
87+ let encoded_value = Blob :: new ( encoding. unwrap_or ( BinaryEncoding :: Hex ) , * value) ;
88+
89+ Ok ( Json ( GetRandomValueResponse {
90+ value : encoded_value,
91+ } ) )
8292}
8393
8494#[ derive( Debug , serde:: Serialize , serde:: Deserialize , IntoParams ) ]
@@ -87,6 +97,8 @@ pub struct RevelationPathParams {
8797 #[ param( value_type = String ) ]
8898 pub chain_id : ChainId ,
8999 pub sequence : u64 ,
100+ #[ param( value_type = u64 ) ]
101+ pub block_number : BlockNumber ,
90102}
91103
92104#[ derive( Debug , serde:: Serialize , serde:: Deserialize , IntoParams ) ]
0 commit comments