@@ -11,7 +11,7 @@ use axum::{Extension, Router};
1111use futures:: lock:: Mutex ;
1212use linera_base:: {
1313 crypto:: { CryptoHash , ValidatorPublicKey } ,
14- data_types:: { Amount , ApplicationPermissions , BlockHeight , Timestamp } ,
14+ data_types:: { Amount , ApplicationPermissions , Timestamp } ,
1515 identifiers:: { AccountOwner , ChainId , MessageId } ,
1616 ownership:: ChainOwnership ,
1717} ;
@@ -42,15 +42,14 @@ mod tests;
4242pub struct QueryRoot < C > {
4343 context : Arc < Mutex < C > > ,
4444 genesis_config : Arc < GenesisConfig > ,
45- chain_id : Arc < Mutex < ChainId > > ,
45+ chain_id : ChainId ,
4646}
4747
4848/// The root GraphQL mutation type.
4949pub struct MutationRoot < C > {
50- chain_id : Arc < Mutex < ChainId > > ,
50+ chain_id : ChainId ,
5151 context : Arc < Mutex < C > > ,
5252 amount : Amount ,
53- end_block_height : BlockHeight ,
5453 end_timestamp : Timestamp ,
5554 start_timestamp : Timestamp ,
5655 start_balance : Amount ,
9089
9190 /// Returns the current committee's validators.
9291 async fn current_validators ( & self ) -> Result < Vec < Validator > , Error > {
93- let chain_id = * self . chain_id . lock ( ) . await ;
94- let client = self . context . lock ( ) . await . make_chain_client ( chain_id) ?;
92+ let client = self . context . lock ( ) . await . make_chain_client ( self . chain_id ) ?;
9593 let committee = client. local_committee ( ) . await ?;
9694 Ok ( committee
9795 . validators ( )
@@ -120,8 +118,7 @@ where
120118 C : ClientContext ,
121119{
122120 async fn do_claim ( & self , owner : AccountOwner ) -> Result < ClaimOutcome , Error > {
123- let chain_id = * self . chain_id . lock ( ) . await ;
124- let client = self . context . lock ( ) . await . make_chain_client ( chain_id) ?;
121+ let client = self . context . lock ( ) . await . make_chain_client ( self . chain_id ) ?;
125122
126123 if self . start_timestamp < self . end_timestamp {
127124 let local_time = client. storage_client ( ) . clock ( ) . current_time ( ) ;
@@ -152,32 +149,16 @@ where
152149 . open_chain ( ownership, ApplicationPermissions :: default ( ) , self . amount )
153150 . await ;
154151 self . context . lock ( ) . await . update_wallet ( & client) . await ?;
155- let ( message_id, certificate) = result?. try_unwrap ( ) ?;
156-
157- if client. next_block_height ( ) >= self . end_block_height {
158- let key_pair = client. key_pair ( ) . await ?;
159- let balance = client. local_balance ( ) . await ?. try_sub ( Amount :: ONE ) ?;
160- let ownership = client. chain_state_view ( ) . await ?. ownership ( ) . clone ( ) ;
161- let ( message_id, certificate) = client
162- . open_chain ( ownership, ApplicationPermissions :: default ( ) , balance)
163- . await ?
164- . try_unwrap ( ) ?;
165- // TODO(#1795): Move the remaining tokens to the new chain.
166- client. close_chain ( ) . await ?. try_unwrap ( ) ?;
167- let chain_id = ChainId :: child ( message_id) ;
168- info ! ( "Switching to a new faucet chain {chain_id:8}; remaining balance: {balance}" ) ;
169- self . context
170- . lock ( )
171- . await
172- . update_wallet_for_new_chain (
173- chain_id,
174- Some ( key_pair) ,
175- certificate. block ( ) . header . timestamp ,
176- )
177- . await ?;
178- * self . chain_id . lock ( ) . await = chain_id;
179- }
180-
152+ let ( message_id, certificate) = match result? {
153+ ClientOutcome :: Committed ( result) => result,
154+ ClientOutcome :: WaitForTimeout ( timeout) => {
155+ return Err ( Error :: new ( format ! (
156+ "This faucet is using a multi-owner chain and is not the leader right now. \
157+ Try again at {}",
158+ timeout. timestamp,
159+ ) ) ) ;
160+ }
161+ } ;
181162 let chain_id = ChainId :: child ( message_id) ;
182163 Ok ( ClaimOutcome {
183164 message_id,
@@ -204,15 +185,14 @@ pub struct FaucetService<C>
204185where
205186 C : ClientContext ,
206187{
207- chain_id : Arc < Mutex < ChainId > > ,
188+ chain_id : ChainId ,
208189 context : Arc < Mutex < C > > ,
209190 genesis_config : Arc < GenesisConfig > ,
210191 config : ChainListenerConfig ,
211192 storage : C :: Storage ,
212193 port : NonZeroU16 ,
213194 amount : Amount ,
214195 end_timestamp : Timestamp ,
215- end_block_height : BlockHeight ,
216196 start_timestamp : Timestamp ,
217197 start_balance : Amount ,
218198}
@@ -223,14 +203,13 @@ where
223203{
224204 fn clone ( & self ) -> Self {
225205 Self {
226- chain_id : self . chain_id . clone ( ) ,
206+ chain_id : self . chain_id ,
227207 context : Arc :: clone ( & self . context ) ,
228208 genesis_config : Arc :: clone ( & self . genesis_config ) ,
229209 config : self . config . clone ( ) ,
230210 storage : self . storage . clone ( ) ,
231211 port : self . port ,
232212 amount : self . amount ,
233- end_block_height : self . end_block_height ,
234213 end_timestamp : self . end_timestamp ,
235214 start_timestamp : self . start_timestamp ,
236215 start_balance : self . start_balance ,
@@ -249,7 +228,6 @@ where
249228 chain_id : ChainId ,
250229 context : C ,
251230 amount : Amount ,
252- end_block_height : BlockHeight ,
253231 end_timestamp : Timestamp ,
254232 genesis_config : Arc < GenesisConfig > ,
255233 config : ChainListenerConfig ,
@@ -261,14 +239,13 @@ where
261239 client. process_inbox ( ) . await ?;
262240 let start_balance = client. local_balance ( ) . await ?;
263241 Ok ( Self {
264- chain_id : Arc :: new ( Mutex :: new ( chain_id ) ) ,
242+ chain_id,
265243 context,
266244 genesis_config,
267245 config,
268246 storage,
269247 port,
270248 amount,
271- end_block_height,
272249 end_timestamp,
273250 start_timestamp,
274251 start_balance,
@@ -277,18 +254,17 @@ where
277254
278255 pub fn schema ( & self ) -> Schema < QueryRoot < C > , MutationRoot < C > , EmptySubscription > {
279256 let mutation_root = MutationRoot {
280- chain_id : self . chain_id . clone ( ) ,
257+ chain_id : self . chain_id ,
281258 context : Arc :: clone ( & self . context ) ,
282259 amount : self . amount ,
283- end_block_height : self . end_block_height ,
284260 end_timestamp : self . end_timestamp ,
285261 start_timestamp : self . start_timestamp ,
286262 start_balance : self . start_balance ,
287263 } ;
288264 let query_root = QueryRoot {
289265 genesis_config : Arc :: clone ( & self . genesis_config ) ,
290266 context : Arc :: clone ( & self . context ) ,
291- chain_id : self . chain_id . clone ( ) ,
267+ chain_id : self . chain_id ,
292268 } ;
293269 Schema :: build ( query_root, mutation_root, EmptySubscription ) . finish ( )
294270 }
@@ -327,27 +303,3 @@ where
327303 schema. execute ( request. into_inner ( ) ) . await . into ( )
328304 }
329305}
330-
331- trait ClientOutcomeExt {
332- type Output ;
333-
334- /// Returns the committed result or an error if we are not the leader.
335- ///
336- /// It is recommended to use single-owner chains for the faucet to avoid this error.
337- fn try_unwrap ( self ) -> Result < Self :: Output , Error > ;
338- }
339-
340- impl < T > ClientOutcomeExt for ClientOutcome < T > {
341- type Output = T ;
342-
343- fn try_unwrap ( self ) -> Result < Self :: Output , Error > {
344- match self {
345- ClientOutcome :: Committed ( result) => Ok ( result) ,
346- ClientOutcome :: WaitForTimeout ( timeout) => Err ( Error :: new ( format ! (
347- "This faucet is using a multi-owner chain and is not the leader right now. \
348- Try again at {}",
349- timeout. timestamp,
350- ) ) ) ,
351- }
352- }
353- }
0 commit comments