2020use crate :: { error:: Error , Chain , ChainWithBalances , Client } ;
2121
2222use async_trait:: async_trait;
23- use num_traits:: CheckedSub ;
2423use sp_version:: RuntimeVersion ;
2524use std:: {
26- collections:: VecDeque ,
2725 fmt:: Display ,
2826 time:: { Duration , Instant } ,
2927} ;
@@ -36,11 +34,6 @@ pub trait Environment<C: ChainWithBalances>: Send + Sync + 'static {
3634
3735 /// Return current runtime version.
3836 async fn runtime_version ( & mut self ) -> Result < RuntimeVersion , Self :: Error > ;
39- /// Return free native balance of the account on the chain.
40- async fn free_native_balance (
41- & mut self ,
42- account : C :: AccountId ,
43- ) -> Result < C :: Balance , Self :: Error > ;
4437
4538 /// Return current time.
4639 fn now ( & self ) -> Instant {
@@ -99,80 +92,6 @@ pub fn abort_on_spec_version_change<C: ChainWithBalances>(
9992 } ) ;
10093}
10194
102- /// Abort if, during 24 hours, free balance of given account is decreased at least by given value.
103- /// Other components may increase (or decrease) balance of account and it WILL affect logic of the
104- /// guard.
105- pub fn abort_when_account_balance_decreased < C : ChainWithBalances > (
106- mut env : impl Environment < C > ,
107- account_id : C :: AccountId ,
108- maximal_decrease : C :: Balance ,
109- ) {
110- const DAY : Duration = Duration :: from_secs ( 60 * 60 * 24 ) ;
111-
112- async_std:: task:: spawn ( async move {
113- log:: info!(
114- target: "bridge-guard" ,
115- "Starting balance guard for {}/{:?}. Maximal decrease: {:?}" ,
116- C :: NAME ,
117- account_id,
118- maximal_decrease,
119- ) ;
120-
121- let mut balances = VecDeque :: new ( ) ;
122-
123- loop {
124- let current_time = env. now ( ) ;
125-
126- // remember balances that are beyound 24h border
127- if let Some ( time_border) = current_time. checked_sub ( DAY ) {
128- while balances. front ( ) . map ( |( time, _) | * time < time_border) . unwrap_or ( false ) {
129- balances. pop_front ( ) ;
130- }
131- }
132-
133- // read balance of the account
134- let current_balance = env. free_native_balance ( account_id. clone ( ) ) . await ;
135-
136- // remember balance and check difference
137- match current_balance {
138- Ok ( current_balance) => {
139- // remember balance
140- balances. push_back ( ( current_time, current_balance) ) ;
141-
142- // check if difference between current and oldest balance is too large
143- let ( oldest_time, oldest_balance) =
144- balances. front ( ) . expect ( "pushed to queue couple of lines above; qed" ) ;
145- let balances_difference = oldest_balance. checked_sub ( & current_balance) ;
146- if balances_difference > Some ( maximal_decrease) {
147- log:: error!(
148- target: "bridge-guard" ,
149- "Balance of {} account {:?} has decreased from {:?} to {:?} in {} minutes. Aborting relay" ,
150- C :: NAME ,
151- account_id,
152- oldest_balance,
153- current_balance,
154- current_time. duration_since( * oldest_time) . as_secs( ) / 60 ,
155- ) ;
156-
157- env. abort ( ) . await ;
158- }
159- } ,
160- Err ( error) => {
161- log:: warn!(
162- target: "bridge-guard" ,
163- "Failed to read {} account {:?} balance: {}. Relay may need to be stopped manually" ,
164- C :: NAME ,
165- account_id,
166- error,
167- ) ;
168- } ,
169- } ;
170-
171- env. sleep ( conditions_check_delay :: < C > ( ) ) . await ;
172- }
173- } ) ;
174- }
175-
17695/// Delay between conditions check.
17796fn conditions_check_delay < C : Chain > ( ) -> Duration {
17897 C :: AVERAGE_BLOCK_INTERVAL * ( 10 + rand:: random :: < u32 > ( ) % 10 )
@@ -185,13 +104,6 @@ impl<C: ChainWithBalances> Environment<C> for Client<C> {
185104 async fn runtime_version ( & mut self ) -> Result < RuntimeVersion , Self :: Error > {
186105 Client :: < C > :: runtime_version ( self ) . await
187106 }
188-
189- async fn free_native_balance (
190- & mut self ,
191- account : C :: AccountId ,
192- ) -> Result < C :: Balance , Self :: Error > {
193- Client :: < C > :: free_native_balance ( self , account) . await
194- }
195107}
196108
197109#[ cfg( test) ]
@@ -207,7 +119,6 @@ mod tests {
207119
208120 struct TestEnvironment {
209121 runtime_version_rx : UnboundedReceiver < RuntimeVersion > ,
210- free_native_balance_rx : UnboundedReceiver < u32 > ,
211122 slept_tx : UnboundedSender < ( ) > ,
212123 aborted_tx : UnboundedSender < ( ) > ,
213124 }
@@ -220,10 +131,6 @@ mod tests {
220131 Ok ( self . runtime_version_rx . next ( ) . await . unwrap_or_default ( ) )
221132 }
222133
223- async fn free_native_balance ( & mut self , _account : u32 ) -> Result < u32 , Self :: Error > {
224- Ok ( self . free_native_balance_rx . next ( ) . await . unwrap_or_default ( ) )
225- }
226-
227134 async fn sleep ( & mut self , _duration : Duration ) {
228135 let _ = self . slept_tx . send ( ( ) ) . await ;
229136 }
@@ -240,17 +147,11 @@ mod tests {
240147 async_std:: task:: block_on ( async {
241148 let (
242149 ( mut runtime_version_tx, runtime_version_rx) ,
243- ( _free_native_balance_tx, free_native_balance_rx) ,
244150 ( slept_tx, mut slept_rx) ,
245151 ( aborted_tx, mut aborted_rx) ,
246- ) = ( unbounded ( ) , unbounded ( ) , unbounded ( ) , unbounded ( ) ) ;
152+ ) = ( unbounded ( ) , unbounded ( ) , unbounded ( ) ) ;
247153 abort_on_spec_version_change (
248- TestEnvironment {
249- runtime_version_rx,
250- free_native_balance_rx,
251- slept_tx,
252- aborted_tx,
253- } ,
154+ TestEnvironment { runtime_version_rx, slept_tx, aborted_tx } ,
254155 0 ,
255156 ) ;
256157
@@ -272,17 +173,11 @@ mod tests {
272173 async_std:: task:: block_on ( async {
273174 let (
274175 ( mut runtime_version_tx, runtime_version_rx) ,
275- ( _free_native_balance_tx, free_native_balance_rx) ,
276176 ( slept_tx, mut slept_rx) ,
277177 ( aborted_tx, mut aborted_rx) ,
278- ) = ( unbounded ( ) , unbounded ( ) , unbounded ( ) , unbounded ( ) ) ;
178+ ) = ( unbounded ( ) , unbounded ( ) , unbounded ( ) ) ;
279179 abort_on_spec_version_change (
280- TestEnvironment {
281- runtime_version_rx,
282- free_native_balance_rx,
283- slept_tx,
284- aborted_tx,
285- } ,
180+ TestEnvironment { runtime_version_rx, slept_tx, aborted_tx } ,
286181 42 ,
287182 ) ;
288183
@@ -298,76 +193,4 @@ mod tests {
298193 assert ! ( aborted_rx. next( ) . now_or_never( ) . is_none( ) ) ;
299194 } ) ;
300195 }
301-
302- #[ test]
303- fn aborts_when_balance_is_too_low ( ) {
304- async_std:: task:: block_on ( async {
305- let (
306- ( _runtime_version_tx, runtime_version_rx) ,
307- ( mut free_native_balance_tx, free_native_balance_rx) ,
308- ( slept_tx, mut slept_rx) ,
309- ( aborted_tx, mut aborted_rx) ,
310- ) = ( unbounded ( ) , unbounded ( ) , unbounded ( ) , unbounded ( ) ) ;
311- abort_when_account_balance_decreased (
312- TestEnvironment {
313- runtime_version_rx,
314- free_native_balance_rx,
315- slept_tx,
316- aborted_tx,
317- } ,
318- 0 ,
319- 100 ,
320- ) ;
321-
322- // client responds with initial balance
323- free_native_balance_tx. send ( 1000 ) . await . unwrap ( ) ;
324-
325- // then the guard sleeps
326- slept_rx. next ( ) . await ;
327-
328- // and then client responds with updated balance, which is too low
329- free_native_balance_tx. send ( 899 ) . await . unwrap ( ) ;
330-
331- // then the `abort` function is called
332- aborted_rx. next ( ) . await ;
333- // and we do not reach next `sleep` function call
334- assert ! ( slept_rx. next( ) . now_or_never( ) . is_none( ) ) ;
335- } ) ;
336- }
337-
338- #[ test]
339- fn does_not_aborts_when_balance_is_enough ( ) {
340- async_std:: task:: block_on ( async {
341- let (
342- ( _runtime_version_tx, runtime_version_rx) ,
343- ( mut free_native_balance_tx, free_native_balance_rx) ,
344- ( slept_tx, mut slept_rx) ,
345- ( aborted_tx, mut aborted_rx) ,
346- ) = ( unbounded ( ) , unbounded ( ) , unbounded ( ) , unbounded ( ) ) ;
347- abort_when_account_balance_decreased (
348- TestEnvironment {
349- runtime_version_rx,
350- free_native_balance_rx,
351- slept_tx,
352- aborted_tx,
353- } ,
354- 0 ,
355- 100 ,
356- ) ;
357-
358- // client responds with initial balance
359- free_native_balance_tx. send ( 1000 ) . await . unwrap ( ) ;
360-
361- // then the guard sleeps
362- slept_rx. next ( ) . await ;
363-
364- // and then client responds with updated balance, which is enough
365- free_native_balance_tx. send ( 950 ) . await . unwrap ( ) ;
366-
367- // then the `sleep` function is called
368- slept_rx. next ( ) . await ;
369- // and `abort` is not called
370- assert ! ( aborted_rx. next( ) . now_or_never( ) . is_none( ) ) ;
371- } ) ;
372- }
373196}
0 commit comments