Skip to content

Commit fc9e2e8

Browse files
authored
temporarily remove balance guard (paritytech#2121)
1 parent 2e05ce0 commit fc9e2e8

File tree

4 files changed

+5
-253
lines changed

4 files changed

+5
-253
lines changed

relays/client-substrate/src/client.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! Substrate node client.
1818
1919
use crate::{
20-
chain::{Chain, ChainWithBalances, ChainWithTransactions},
20+
chain::{Chain, ChainWithTransactions},
2121
rpc::{
2222
SubstrateAuthorClient, SubstrateChainClient, SubstrateFinalityClient,
2323
SubstrateFrameSystemClient, SubstrateStateClient, SubstrateSystemClient,
@@ -31,14 +31,12 @@ use async_trait::async_trait;
3131
use bp_runtime::{HeaderIdProvider, StorageDoubleMapKeyProvider, StorageMapKeyProvider};
3232
use codec::{Decode, Encode};
3333
use frame_support::weights::Weight;
34-
use frame_system::AccountInfo;
3534
use futures::{SinkExt, StreamExt};
3635
use jsonrpsee::{
3736
core::DeserializeOwned,
3837
ws_client::{WsClient as RpcClient, WsClientBuilder as RpcClientBuilder},
3938
};
4039
use num_traits::{Saturating, Zero};
41-
use pallet_balances::AccountData;
4240
use pallet_transaction_payment::RuntimeDispatchInfo;
4341
use relay_utils::{relay_loop::RECONNECT_DELAY, STALL_TIMEOUT};
4442
use sp_core::{
@@ -424,26 +422,6 @@ impl<C: Chain> Client<C> {
424422
})
425423
}
426424

427-
/// Return native tokens balance of the account.
428-
pub async fn free_native_balance(&self, account: C::AccountId) -> Result<C::Balance>
429-
where
430-
C: ChainWithBalances,
431-
{
432-
self.jsonrpsee_execute(move |client| async move {
433-
let storage_key = C::account_info_storage_key(&account);
434-
let encoded_account_data =
435-
SubstrateStateClient::<C>::storage(&*client, storage_key, None)
436-
.await?
437-
.ok_or(Error::AccountDoesNotExist)?;
438-
let decoded_account_data = AccountInfo::<C::Index, AccountData<C::Balance>>::decode(
439-
&mut &encoded_account_data.0[..],
440-
)
441-
.map_err(Error::ResponseParseFailed)?;
442-
Ok(decoded_account_data.data.free)
443-
})
444-
.await
445-
}
446-
447425
/// Get the nonce of the given Substrate account.
448426
///
449427
/// Note: It's the caller's responsibility to make sure `account` is a valid SS58 address.

relays/client-substrate/src/guard.rs

Lines changed: 4 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
use crate::{error::Error, Chain, ChainWithBalances, Client};
2121

2222
use async_trait::async_trait;
23-
use num_traits::CheckedSub;
2423
use sp_version::RuntimeVersion;
2524
use 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.
17796
fn 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
}

relays/lib-substrate-relay/src/finality/guards.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

relays/lib-substrate-relay/src/finality/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use sp_core::Pair;
3939
use std::{fmt::Debug, marker::PhantomData};
4040

4141
pub mod engine;
42-
pub mod guards;
4342
pub mod initialize;
4443
pub mod source;
4544
pub mod target;

0 commit comments

Comments
 (0)