Match substrate's fmt (#1148)

* Alter gitlab.

* Use substrate's rustfmt.toml

* cargo +nightly fmt --all

* Fix spellcheck.

* cargo +nightly fmt --all

* format.

* Fix spellcheck and fmt

* fmt?

* Fix spellcheck

Co-authored-by: Tomasz Drwięga <tomasz@parity.io>
This commit is contained in:
hacpy
2021-09-24 19:29:31 +08:00
committed by Bastian Köcher
parent 87cbb382d9
commit bd70de8b8b
174 changed files with 6095 additions and 4962 deletions
+129 -126
View File
@@ -18,7 +18,8 @@
use async_trait::async_trait;
use relay_utils::{
relay_loop::Client as RelayClient, FailedClient, MaybeConnectionError, StringifiedMaybeConnectionError,
relay_loop::Client as RelayClient, FailedClient, MaybeConnectionError,
StringifiedMaybeConnectionError,
};
use std::{
fmt::{Debug, Display},
@@ -96,12 +97,18 @@ pub trait SourceClient<P: TransactionProofPipeline>: RelayClient {
async fn block_by_hash(&self, hash: BlockHashOf<P>) -> Result<P::Block, Self::Error>;
/// Get canonical block by number.
async fn block_by_number(&self, number: BlockNumberOf<P>) -> Result<P::Block, Self::Error>;
/// Return block + index where transaction has been **mined**. May return `Ok(None)` if transaction
/// is unknown to the source node.
async fn transaction_block(&self, hash: &TransactionHashOf<P>)
-> Result<Option<(HeaderId<P>, usize)>, Self::Error>;
/// Return block + index where transaction has been **mined**. May return `Ok(None)` if
/// transaction is unknown to the source node.
async fn transaction_block(
&self,
hash: &TransactionHashOf<P>,
) -> Result<Option<(HeaderId<P>, usize)>, Self::Error>;
/// Prepare transaction proof.
async fn transaction_proof(&self, block: &P::Block, tx_index: usize) -> Result<P::TransactionProof, Self::Error>;
async fn transaction_proof(
&self,
block: &P::Block,
tx_index: usize,
) -> Result<P::TransactionProof, Self::Error>;
}
/// Target client API.
@@ -116,9 +123,13 @@ pub trait TargetClient<P: TransactionProofPipeline>: RelayClient {
/// Returns best finalized header id.
async fn best_finalized_header_id(&self) -> Result<HeaderId<P>, Self::Error>;
/// Returns `Ok(true)` if transaction proof is need to be relayed.
async fn filter_transaction_proof(&self, proof: &P::TransactionProof) -> Result<bool, Self::Error>;
async fn filter_transaction_proof(
&self,
proof: &P::TransactionProof,
) -> Result<bool, Self::Error>;
/// Submits transaction proof to the target node.
async fn submit_transaction_proof(&self, proof: P::TransactionProof) -> Result<(), Self::Error>;
async fn submit_transaction_proof(&self, proof: P::TransactionProof)
-> Result<(), Self::Error>;
}
/// Block transaction statistics.
@@ -154,27 +165,28 @@ pub async fn relay_block_transactions<P: TransactionProofPipeline>(
for (source_tx_index, source_tx) in transactions_to_process {
let result = async {
let source_tx_id = format!("{}/{}", source_block.id().1, source_tx_index);
let source_tx_proof =
prepare_transaction_proof(source_client, &source_tx_id, source_block, source_tx_index)
.await
.map_err(|e| (FailedClient::Source, e))?;
let source_tx_proof = prepare_transaction_proof(
source_client,
&source_tx_id,
source_block,
source_tx_index,
)
.await
.map_err(|e| (FailedClient::Source, e))?;
let needs_to_be_relayed =
target_client
.filter_transaction_proof(&source_tx_proof)
.await
.map_err(|err| {
(
FailedClient::Target,
StringifiedMaybeConnectionError::new(
err.is_connection_error(),
format!("Transaction filtering has failed with {:?}", err),
),
)
})?;
target_client.filter_transaction_proof(&source_tx_proof).await.map_err(|err| {
(
FailedClient::Target,
StringifiedMaybeConnectionError::new(
err.is_connection_error(),
format!("Transaction filtering has failed with {:?}", err),
),
)
})?;
if !needs_to_be_relayed {
return Ok(false);
return Ok(false)
}
relay_ready_transaction_proof(target_client, &source_tx_id, source_tx_proof)
@@ -191,13 +203,14 @@ pub async fn relay_block_transactions<P: TransactionProofPipeline>(
// Option#1 may seems better, but:
// 1) we do not track if transaction is mined (without an error) by the target node;
// 2) error could be irrecoverable (e.g. when block is already pruned by bridge module or tx
// has invalid format) && we'll end up in infinite loop of retrying the same transaction proof.
// has invalid format) && we'll end up in infinite loop of retrying the same transaction
// proof.
//
// So we're going with option#2 here (the only exception are connection errors).
match result {
Ok(false) => {
relayed_transactions.processed += 1;
}
},
Ok(true) => {
log::info!(
target: "bridge",
@@ -209,7 +222,7 @@ pub async fn relay_block_transactions<P: TransactionProofPipeline>(
relayed_transactions.processed += 1;
relayed_transactions.relayed += 1;
}
},
Err((failed_client, err)) => {
log::error!(
target: "bridge",
@@ -226,12 +239,12 @@ pub async fn relay_block_transactions<P: TransactionProofPipeline>(
);
if err.is_connection_error() {
return Err((failed_client, relayed_transactions));
return Err((failed_client, relayed_transactions))
}
relayed_transactions.processed += 1;
relayed_transactions.failed += 1;
}
},
}
}
@@ -245,7 +258,8 @@ pub async fn relay_single_transaction_proof<P: TransactionProofPipeline>(
source_tx_hash: TransactionHashOf<P>,
) -> Result<(), String> {
// wait for transaction and header on source node
let (source_header_id, source_tx_index) = wait_transaction_mined(source_client, &source_tx_hash).await?;
let (source_header_id, source_tx_index) =
wait_transaction_mined(source_client, &source_tx_hash).await?;
let source_block = source_client.block_by_hash(source_header_id.1.clone()).await;
let source_block = source_block.map_err(|err| {
format!(
@@ -302,20 +316,17 @@ async fn relay_ready_transaction_proof<P: TransactionProofPipeline>(
source_tx_id: &str,
source_tx_proof: P::TransactionProof,
) -> Result<(), StringifiedMaybeConnectionError> {
target_client
.submit_transaction_proof(source_tx_proof)
.await
.map_err(|err| {
StringifiedMaybeConnectionError::new(
err.is_connection_error(),
format!(
"Error submitting transaction {} proof to {} node: {:?}",
source_tx_id,
P::TARGET_NAME,
err,
),
)
})
target_client.submit_transaction_proof(source_tx_proof).await.map_err(|err| {
StringifiedMaybeConnectionError::new(
err.is_connection_error(),
format!(
"Error submitting transaction {} proof to {} node: {:?}",
source_tx_id,
P::TARGET_NAME,
err,
),
)
})
}
/// Wait until transaction is mined by source node.
@@ -324,14 +335,15 @@ async fn wait_transaction_mined<P: TransactionProofPipeline>(
source_tx_hash: &TransactionHashOf<P>,
) -> Result<(HeaderId<P>, usize), String> {
loop {
let source_header_and_tx = source_client.transaction_block(source_tx_hash).await.map_err(|err| {
format!(
"Error retrieving transaction {} from {} node: {:?}",
source_tx_hash,
P::SOURCE_NAME,
err,
)
})?;
let source_header_and_tx =
source_client.transaction_block(source_tx_hash).await.map_err(|err| {
format!(
"Error retrieving transaction {} from {} node: {:?}",
source_tx_hash,
P::SOURCE_NAME,
err,
)
})?;
match source_header_and_tx {
Some((source_header_id, source_tx)) => {
log::info!(
@@ -341,8 +353,8 @@ async fn wait_transaction_mined<P: TransactionProofPipeline>(
P::SOURCE_NAME,
);
return Ok((source_header_id, source_tx));
}
return Ok((source_header_id, source_tx))
},
None => {
log::info!(
target: "bridge",
@@ -352,7 +364,7 @@ async fn wait_transaction_mined<P: TransactionProofPipeline>(
);
source_client.tick().await;
}
},
}
}
}
@@ -363,15 +375,16 @@ async fn wait_header_imported<P: TransactionProofPipeline>(
source_header_id: &HeaderId<P>,
) -> Result<(), String> {
loop {
let is_header_known = target_client.is_header_known(source_header_id).await.map_err(|err| {
format!(
"Failed to check existence of header {}/{} on {} node: {:?}",
source_header_id.0,
source_header_id.1,
P::TARGET_NAME,
err,
)
})?;
let is_header_known =
target_client.is_header_known(source_header_id).await.map_err(|err| {
format!(
"Failed to check existence of header {}/{} on {} node: {:?}",
source_header_id.0,
source_header_id.1,
P::TARGET_NAME,
err,
)
})?;
match is_header_known {
true => {
log::info!(
@@ -382,8 +395,8 @@ async fn wait_header_imported<P: TransactionProofPipeline>(
P::TARGET_NAME,
);
return Ok(());
}
return Ok(())
},
false => {
log::info!(
target: "bridge",
@@ -394,7 +407,7 @@ async fn wait_header_imported<P: TransactionProofPipeline>(
);
target_client.tick().await;
}
},
}
}
}
@@ -405,10 +418,8 @@ async fn wait_header_finalized<P: TransactionProofPipeline>(
source_header_id: &HeaderId<P>,
) -> Result<(), String> {
loop {
let is_header_finalized = target_client
.is_header_finalized(source_header_id)
.await
.map_err(|err| {
let is_header_finalized =
target_client.is_header_finalized(source_header_id).await.map_err(|err| {
format!(
"Failed to check finality of header {}/{} on {} node: {:?}",
source_header_id.0,
@@ -427,8 +438,8 @@ async fn wait_header_finalized<P: TransactionProofPipeline>(
P::TARGET_NAME,
);
return Ok(());
}
return Ok(())
},
false => {
log::info!(
target: "bridge",
@@ -439,7 +450,7 @@ async fn wait_header_finalized<P: TransactionProofPipeline>(
);
target_client.tick().await;
}
},
}
}
}
@@ -582,15 +593,22 @@ pub(crate) mod tests {
self.data.lock().block.clone()
}
async fn transaction_block(&self, _: &TestTransactionHash) -> Result<Option<(TestHeaderId, usize)>, TestError> {
async fn transaction_block(
&self,
_: &TestTransactionHash,
) -> Result<Option<(TestHeaderId, usize)>, TestError> {
self.data.lock().transaction_block.clone()
}
async fn transaction_proof(&self, block: &TestBlock, index: usize) -> Result<TestTransactionProof, TestError> {
async fn transaction_proof(
&self,
block: &TestBlock,
index: usize,
) -> Result<TestTransactionProof, TestError> {
let tx_hash = block.1[index].hash();
let proof_error = self.data.lock().proofs_to_fail.get(&tx_hash).cloned();
if let Some(err) = proof_error {
return Err(err);
return Err(err)
}
Ok(TestTransactionProof(tx_hash))
@@ -653,19 +671,32 @@ pub(crate) mod tests {
self.data.lock().best_finalized_header_id.clone()
}
async fn filter_transaction_proof(&self, proof: &TestTransactionProof) -> Result<bool, TestError> {
async fn filter_transaction_proof(
&self,
proof: &TestTransactionProof,
) -> Result<bool, TestError> {
Ok(self.data.lock().transactions_to_accept.contains(&proof.0))
}
async fn submit_transaction_proof(&self, proof: TestTransactionProof) -> Result<(), TestError> {
async fn submit_transaction_proof(
&self,
proof: TestTransactionProof,
) -> Result<(), TestError> {
self.data.lock().submitted_proofs.push(proof);
Ok(())
}
}
fn ensure_relay_single_success(source: &TestTransactionsSource, target: &TestTransactionsTarget) {
fn ensure_relay_single_success(
source: &TestTransactionsSource,
target: &TestTransactionsTarget,
) {
assert_eq!(
async_std::task::block_on(relay_single_transaction_proof(source, target, test_transaction_hash(0),)),
async_std::task::block_on(relay_single_transaction_proof(
source,
target,
test_transaction_hash(0),
)),
Ok(()),
);
assert_eq!(
@@ -782,11 +813,7 @@ pub(crate) mod tests {
let source = TestTransactionsSource::new(Box::new(|_| unreachable!("no ticks allowed")));
let target = TestTransactionsTarget::new(Box::new(|_| unreachable!("no ticks allowed")));
target
.data
.lock()
.transactions_to_accept
.remove(&test_transaction_hash(0));
target.data.lock().transactions_to_accept.remove(&test_transaction_hash(0));
ensure_relay_single_success(&source, &target)
}
@@ -814,25 +841,14 @@ pub(crate) mod tests {
let target = TestTransactionsTarget::new(Box::new(|_| unreachable!("no ticks allowed")));
// let's only accept tx#1
target
.data
.lock()
.transactions_to_accept
.remove(&test_transaction_hash(0));
target
.data
.lock()
.transactions_to_accept
.insert(test_transaction_hash(1));
target.data.lock().transactions_to_accept.remove(&test_transaction_hash(0));
target.data.lock().transactions_to_accept.insert(test_transaction_hash(1));
let relayed_transactions = test_relay_block_transactions(&source, &target, Default::default());
let relayed_transactions =
test_relay_block_transactions(&source, &target, Default::default());
assert_eq!(
relayed_transactions,
Ok(RelayedBlockTransactions {
processed: 3,
relayed: 1,
failed: 0,
}),
Ok(RelayedBlockTransactions { processed: 3, relayed: 1, failed: 0 }),
);
assert_eq!(
target.data.lock().submitted_proofs,
@@ -852,14 +868,11 @@ pub(crate) mod tests {
.proofs_to_fail
.insert(test_transaction_hash(0), TestError(false));
let relayed_transactions = test_relay_block_transactions(&source, &target, Default::default());
let relayed_transactions =
test_relay_block_transactions(&source, &target, Default::default());
assert_eq!(
relayed_transactions,
Ok(RelayedBlockTransactions {
processed: 3,
relayed: 0,
failed: 1,
}),
Ok(RelayedBlockTransactions { processed: 3, relayed: 0, failed: 1 }),
);
assert_eq!(target.data.lock().submitted_proofs, vec![],);
}
@@ -876,14 +889,11 @@ pub(crate) mod tests {
.proofs_to_fail
.insert(test_transaction_hash(1), TestError(true));
let relayed_transactions = test_relay_block_transactions(&source, &target, Default::default());
let relayed_transactions =
test_relay_block_transactions(&source, &target, Default::default());
assert_eq!(
relayed_transactions,
Err(RelayedBlockTransactions {
processed: 1,
relayed: 1,
failed: 0,
}),
Err(RelayedBlockTransactions { processed: 1, relayed: 1, failed: 0 }),
);
assert_eq!(
target.data.lock().submitted_proofs,
@@ -893,20 +903,13 @@ pub(crate) mod tests {
// now do not fail on tx#2
source.data.lock().proofs_to_fail.clear();
// and also relay tx#3
target
.data
.lock()
.transactions_to_accept
.insert(test_transaction_hash(2));
target.data.lock().transactions_to_accept.insert(test_transaction_hash(2));
let relayed_transactions = test_relay_block_transactions(&source, &target, relayed_transactions.unwrap_err());
let relayed_transactions =
test_relay_block_transactions(&source, &target, relayed_transactions.unwrap_err());
assert_eq!(
relayed_transactions,
Ok(RelayedBlockTransactions {
processed: 3,
relayed: 2,
failed: 0,
}),
Ok(RelayedBlockTransactions { processed: 3, relayed: 2, failed: 0 }),
);
assert_eq!(
target.data.lock().submitted_proofs,
+40 -36
View File
@@ -16,11 +16,13 @@
//! Relaying proofs of exchange transactions.
use crate::exchange::{
relay_block_transactions, BlockNumberOf, RelayedBlockTransactions, SourceClient, TargetClient,
TransactionProofPipeline,
use crate::{
exchange::{
relay_block_transactions, BlockNumberOf, RelayedBlockTransactions, SourceClient,
TargetClient, TransactionProofPipeline,
},
exchange_loop_metrics::ExchangeLoopMetrics,
};
use crate::exchange_loop_metrics::ExchangeLoopMetrics;
use backoff::backoff::Backoff;
use futures::{future::FutureExt, select};
@@ -58,13 +60,13 @@ pub struct InMemoryStorage<BlockNumber> {
impl<BlockNumber> InMemoryStorage<BlockNumber> {
/// Created new in-memory storage with given best processed block number.
pub fn new(best_processed_header_number: BlockNumber) -> Self {
InMemoryStorage {
best_processed_header_number,
}
InMemoryStorage { best_processed_header_number }
}
}
impl<BlockNumber: 'static + Clone + Copy + Send + Sync> TransactionProofsRelayStorage for InMemoryStorage<BlockNumber> {
impl<BlockNumber: 'static + Clone + Copy + Send + Sync> TransactionProofsRelayStorage
for InMemoryStorage<BlockNumber>
{
type BlockNumber = BlockNumber;
fn state(&self) -> TransactionProofsRelayState<BlockNumber> {
@@ -140,12 +142,11 @@ async fn run_until_connection_lost<P: TransactionProofPipeline>(
if let Err((is_connection_error, failed_client)) = iteration_result {
if is_connection_error {
return Err(failed_client);
return Err(failed_client)
}
let retry_timeout = retry_backoff
.next_backoff()
.unwrap_or(relay_utils::relay_loop::RECONNECT_DELAY);
let retry_timeout =
retry_backoff.next_backoff().unwrap_or(relay_utils::relay_loop::RECONNECT_DELAY);
select! {
_ = async_std::task::sleep(retry_timeout).fuse() => {},
_ = exit_signal => return Ok(()),
@@ -181,7 +182,7 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
);
best_finalized_header_id
}
},
Err(err) => {
log::error!(
target: "bridge",
@@ -191,14 +192,20 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
err,
);
return Err((err.is_connection_error(), FailedClient::Target));
}
return Err((err.is_connection_error(), FailedClient::Target))
},
};
loop {
// if we already have some finalized block body, try to relay its transactions
if let Some((block, relayed_transactions)) = current_finalized_block.take() {
let result = relay_block_transactions(source_client, target_client, &block, relayed_transactions).await;
let result = relay_block_transactions(
source_client,
target_client,
&block,
relayed_transactions,
)
.await;
match result {
Ok(relayed_transactions) => {
@@ -212,7 +219,8 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
relayed_transactions.failed,
);
state.best_processed_header_number = state.best_processed_header_number + One::one();
state.best_processed_header_number =
state.best_processed_header_number + One::one();
storage.set_state(state);
if let Some(exchange_loop_metrics) = exchange_loop_metrics {
@@ -224,11 +232,11 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
}
// we have just updated state => proceed to next block retrieval
}
},
Err((failed_client, relayed_transactions)) => {
*current_finalized_block = Some((block, relayed_transactions));
return Err((true, failed_client));
}
return Err((true, failed_client))
},
}
}
@@ -242,8 +250,8 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
*current_finalized_block = Some((block, RelayedBlockTransactions::default()));
// we have received new finalized block => go back to relay its transactions
continue;
}
continue
},
Err(err) => {
log::error!(
target: "bridge",
@@ -253,13 +261,13 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
err,
);
return Err((err.is_connection_error(), FailedClient::Source));
}
return Err((err.is_connection_error(), FailedClient::Source))
},
}
}
// there are no any transactions we need to relay => wait for new data
return Ok(());
return Ok(())
}
}
@@ -267,17 +275,16 @@ async fn run_loop_iteration<P: TransactionProofPipeline>(
mod tests {
use super::*;
use crate::exchange::tests::{
test_next_block, test_next_block_id, test_transaction_hash, TestTransactionProof, TestTransactionsSource,
TestTransactionsTarget,
test_next_block, test_next_block_id, test_transaction_hash, TestTransactionProof,
TestTransactionsSource, TestTransactionsTarget,
};
use futures::{future::FutureExt, stream::StreamExt};
#[test]
fn exchange_loop_is_able_to_relay_proofs() {
let storage = InMemoryStorage {
best_processed_header_number: 0,
};
let target = TestTransactionsTarget::new(Box::new(|_| unreachable!("no target ticks allowed")));
let storage = InMemoryStorage { best_processed_header_number: 0 };
let target =
TestTransactionsTarget::new(Box::new(|_| unreachable!("no target ticks allowed")));
let target_data = target.data.clone();
let (exit_sender, exit_receiver) = futures::channel::mpsc::unbounded();
@@ -295,11 +302,8 @@ mod tests {
(true, false) => {
data.block = Ok(test_next_block());
target_data.lock().best_finalized_header_id = Ok(test_next_block_id());
target_data
.lock()
.transactions_to_accept
.insert(test_transaction_hash(1));
}
target_data.lock().transactions_to_accept.insert(test_transaction_hash(1));
},
_ => (),
}
}));