Skip to content

Commit

Permalink
RustFmt the repo (sorry not sorry)
Browse files Browse the repository at this point in the history
  • Loading branch information
HCastano authored and bkchr committed Apr 10, 2024
1 parent ffdb987 commit 987888e
Show file tree
Hide file tree
Showing 8 changed files with 439 additions and 418 deletions.
23 changes: 12 additions & 11 deletions bridges/modules/ethereum/src/finality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>.

use sp_std::prelude::*;
use crate::error::Error;
use crate::{ancestry, Storage};
use primitives::{public_to_address, Address, Header, SealedEmptyStep, H256};
use sp_io::crypto::secp256k1_ecdsa_recover;
use sp_std::collections::{
btree_map::{BTreeMap, Entry},
btree_set::BTreeSet,
vec_deque::VecDeque,
};
use sp_io::crypto::secp256k1_ecdsa_recover;
use primitives::{Address, H256, Header, SealedEmptyStep, public_to_address};
use crate::{Storage, ancestry};
use crate::error::Error;
use sp_std::prelude::*;

/// Tries to finalize blocks when given block is imported.
///
Expand Down Expand Up @@ -68,8 +68,8 @@ fn is_finalized(
votes: &BTreeMap<Address, u64>,
requires_two_thirds_majority: bool,
) -> bool {
(!requires_two_thirds_majority && votes.len() * 2 > validators.len()) ||
(requires_two_thirds_majority && votes.len() * 3 > validators.len() * 2)
(!requires_two_thirds_majority && votes.len() * 2 > validators.len())
|| (requires_two_thirds_majority && votes.len() * 3 > validators.len() * 2)
}

/// Prepare 'votes' of header and its ancestors' signers.
Expand Down Expand Up @@ -157,15 +157,16 @@ fn remove_signers_votes(signers_to_remove: &BTreeSet<Address>, votes: &mut BTree
} else {
*entry.get_mut() -= 1;
}
},
}
Entry::Vacant(_) => unreachable!("we only remove signers that have been added; qed"),
}
}
}

/// Returns unique set of empty steps signers.
fn empty_steps_signers(header: &Header) -> BTreeSet<Address> {
header.empty_steps()
header
.empty_steps()
.into_iter()
.flat_map(|steps| steps)
.filter_map(|step| empty_step_signer(&step, &header.parent_hash))
Expand All @@ -182,9 +183,9 @@ fn empty_step_signer(empty_step: &SealedEmptyStep, parent_hash: &H256) -> Option

#[cfg(test)]
mod tests {
use crate::HeaderToImport;
use crate::tests::{InMemoryStorage, genesis, validator, validators_addresses};
use super::*;
use crate::tests::{genesis, validator, validators_addresses, InMemoryStorage};
use crate::HeaderToImport;

#[test]
fn verifies_header_author() {
Expand Down
107 changes: 40 additions & 67 deletions bridges/modules/ethereum/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Parity-Bridge. If not, see <http://www.gnu.org/licenses/>.

use sp_std::prelude::*;
use primitives::{H256, Header, Receipt};
use crate::{AuraConfiguration, Storage};
use crate::error::Error;
use crate::finality::finalize_blocks;
use crate::validators::{Validators, ValidatorsConfiguration};
use crate::verification::verify_aura_header;
use crate::{AuraConfiguration, Storage};
use primitives::{Header, Receipt, H256};
use sp_std::prelude::*;

/// Maximal number of headers behind best blocks that we are aiming to store. When there
/// are too many unfinalized headers, it slows down finalization tracking significantly.
Expand Down Expand Up @@ -50,14 +50,7 @@ pub fn import_headers<S: Storage>(
let mut useful = 0;
let mut useless = 0;
for (header, receipts) in headers {
let import_result = import_header(
storage,
aura_config,
validators_config,
prune_depth,
header,
receipts,
);
let import_result = import_header(storage, aura_config, validators_config, prune_depth, header, receipts);

match import_result {
Ok(_) => useful += 1,
Expand Down Expand Up @@ -85,16 +78,11 @@ pub fn import_header<S: Storage>(
let (hash, prev_finalized_hash) = is_importable_header(storage, &header)?;

// verify header
let import_context = verify_aura_header(
storage,
aura_config,
&header,
)?;
let import_context = verify_aura_header(storage, aura_config, &header)?;

// check if block schedules new validators
let validators = Validators::new(validators_config);
let (scheduled_change, enacted_change) =
validators.extract_validators_change(&header, receipts)?;
let (scheduled_change, enacted_change) = validators.extract_validators_change(&header, receipts)?;

// check if block finalizes some other blocks and corresponding scheduled validators
let finalized_blocks = finalize_blocks(
Expand All @@ -105,8 +93,7 @@ pub fn import_header<S: Storage>(
&header,
aura_config.two_thirds_majority_transition,
)?;
let enacted_change = enacted_change
.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks));
let enacted_change = enacted_change.or_else(|| validators.finalize_validators_change(storage, &finalized_blocks));

// NOTE: we can't return Err() from anywhere below this line
// (because otherwise we'll have inconsistent storage if transaction will fail)
Expand Down Expand Up @@ -169,14 +156,12 @@ fn is_importable_header<S: Storage>(storage: &S, header: &Header) -> Result<(H25

#[cfg(test)]
mod tests {
use crate::{kovan_aura_config, kovan_validators_config};
use super::*;
use crate::tests::{
InMemoryStorage,
block_i, custom_block_i, signed_header, genesis,
validator, validators_addresses,
block_i, custom_block_i, genesis, signed_header, validator, validators_addresses, InMemoryStorage,
};
use crate::validators::ValidatorsSource;
use super::*;
use crate::{kovan_aura_config, kovan_validators_config};

#[test]
fn rejects_finalized_block_competitors() {
Expand Down Expand Up @@ -208,7 +193,8 @@ mod tests {
PRUNE_DEPTH,
block.clone(),
None,
).map(|_| ()),
)
.map(|_| ()),
Ok(()),
);
assert_eq!(
Expand All @@ -219,7 +205,8 @@ mod tests {
PRUNE_DEPTH,
block,
None,
).map(|_| ()),
)
.map(|_| ()),
Err(Error::KnownHeader),
);
}
Expand All @@ -235,8 +222,15 @@ mod tests {
let header = block_i(&storage, 1, &validators);
let hash = header.hash();
assert_eq!(
import_header(&mut storage, &kovan_aura_config(), &validators_config, PRUNE_DEPTH, header, None)
.map(|_| ()),
import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
PRUNE_DEPTH,
header,
None
)
.map(|_| ()),
Ok(()),
);

Expand All @@ -250,9 +244,8 @@ mod tests {

#[test]
fn headers_are_pruned() {
let validators_config = ValidatorsConfiguration::Single(
ValidatorsSource::Contract([3; 20].into(), validators_addresses(3)),
);
let validators_config =
ValidatorsConfiguration::Single(ValidatorsSource::Contract([3; 20].into(), validators_addresses(3)));
let validators = vec![validator(0), validator(1), validator(2)];
let mut storage = InMemoryStorage::new(genesis(), validators_addresses(3));

Expand All @@ -261,31 +254,30 @@ mod tests {
let mut last_block_hash = Default::default();
for i in 1..11 {
let header = block_i(&storage, i, &validators);
last_block_hash = import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
last_block_hash =
import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
}
assert!(storage.header(&genesis().hash()).is_some());

// header 11 finalizes headers [10] AND schedules change
// => we prune header#0
let header = custom_block_i(&storage, 11, &validators, |header| {
header.log_bloom = (&[0xff; 256]).into();
header.receipts_root = "2e60346495092587026484e868a5b3063749032b2ea3843844509a6320d7f951".parse().unwrap();
header.receipts_root = "2e60346495092587026484e868a5b3063749032b2ea3843844509a6320d7f951"
.parse()
.unwrap();
});
last_block_hash = import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
Some(vec![crate::validators::tests::validators_change_recept(last_block_hash)]),
).unwrap();
Some(vec![crate::validators::tests::validators_change_recept(
last_block_hash,
)]),
)
.unwrap();
assert!(storage.header(&genesis().hash()).is_none());

// and now let's say validators 1 && 2 went offline
Expand All @@ -298,22 +290,13 @@ mod tests {
parent_hash: last_block_hash,
gas_limit: 0x2000.into(),
author: validator(2).address().to_fixed_bytes().into(),
seal: vec![
vec![step].into(),
vec![].into(),
],
seal: vec![vec![step].into(), vec![].into()],
difficulty: i.into(),
..Default::default()
};
let header = signed_header(&validators, header, step as _);
last_block_hash = import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
last_block_hash =
import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
step += 3;
}
assert_eq!(storage.oldest_unpruned_block(), 11);
Expand All @@ -326,22 +309,12 @@ mod tests {
parent_hash: last_block_hash,
gas_limit: 0x2000.into(),
author: validator(0).address().to_fixed_bytes().into(),
seal: vec![
vec![step].into(),
vec![].into(),
],
seal: vec![vec![step].into(), vec![].into()],
difficulty: 25.into(),
..Default::default()
};
let header = signed_header(&validators, header, step as _);
import_header(
&mut storage,
&kovan_aura_config(),
&validators_config,
10,
header,
None,
).unwrap();
import_header(&mut storage, &kovan_aura_config(), &validators_config, 10, header, None).unwrap();
assert_eq!(storage.oldest_unpruned_block(), 15);
}
}
Loading

0 comments on commit 987888e

Please sign in to comment.