Files
pezkuwi-subxt/polkadot/modules/ethereum/src/import.rs
T
antonio-dropulic 392447f5c8 Squashed 'bridges/' changes from 23dda62482..407bf44a8a
407bf44a8a add missing license header (#1204)
9babb19810  Custom relay strategy (#1198)
c287872a11 fix clippy things (#1200)
3a40e62789 Expose some const value and type (#1186)
32b61476d1 increase sleep before connectingMillau (#1195)
aabe7041fa revert messages transactions mortality (#1194)
3651f4f909 Message transactions mortality (#1191)
364d6e155d Bump dependencies (#1180)
f0389acc08 cargo +nightly fmt --all (#1192)
b270b6a016 Unify error enums in substrate and ethereum clients with `thiserror` (#1094)
58c4946f74 Limit max call size of Rialto/Millau runtimes (#1187)
fd56a8cd56 Add UI to the deployment (#1047)
16f01dc736 Westend -> Millau alerts are pending before notifications are sent (#1184)
5628c11ece replace collective flip with babe randomness in Rialto (#1188)
1094a63b00 ignore another (pretty bad) RUSTSEC (#1185)
379fe323ea fix/ignore cargo deny issues (#1183)
92af5e6e64 additional log in finality relay + rephrase "failed" (#1182)
b996a3b681 Rialto parachain in test deployments (#1178)
28d9332b44 Resubmit transactions strategy for Polkadot/Kusama (#1175)
d0172c6847 Playing with CI (#1179)
fb6f42456d fix checks order when registering parachain (#1177)
ee828c005a Register-parachain subcommand of substrate-relay (#1170)
8cd2b1a112 Token swap pallet benchmarks (#1174)
bb811accb1 fix collision with westend bridge (#1172)
8d2fba70ed add token swaps to test deployments (#1169)
b6d1bdfe2c publish rialto parachain collator image (#1171)
834ae4a10a Fix OutboundLaneData types (#1159)
5ee0ea1626 copypasted -> copied (#1168)
c3bb835f18 fix spelling (#1167)
f90d041dc9 Upgrade `jsonrpsee` to v0.3 (#1051)
598c9b6d0d add some basic tests for swap tokens (#1164)
05e88c61f5 publish images when tag of specific format(e.g. v2021-09-27 + v2021-09-27-1) is published (#1166)
7f3f94a6e0 Fix CI again (#1165)
ff37de332f Move calculation relayer reward into `MessageDeliveryAndDispatchPayment` (#1153)
36fbba839b fix clippy warning (#1163)
16da44d018 explicit wasm build (#1158)
c9c8226449 Match substrate's fmt (#1148)
2fdd7f3e5e Fix/ignore clippy warnings (#1157)
43dfcc2686 Adding LookupAddress (#1156)
951eaa5582 Add rialto-parachain runtime and node (#1142)
803d266d61 Rename MessageId -> BridgeMessageId (#1152)
5f234484fc Box large arguments of GRANDPA pallet (#1154)
cf9abc1011 Fix spelling (#1150)
ab83ba2e58 Relay subcommand that performs token RLT <> MLAU token swap (#1141)
832536caf0 Polkadot <> Kusama relayers (#1122)
6d0daa8975 Add `OnMessageAccepted` callback (#1134)
5d03a20b3e Integrate token swap pallet into Millau runtime (#1099)
ea4cfa833e Adding MultiAddress type and ValidationCodeHash (#1139)
c20325a784 Add tests for `Raw` and `BridgeSendMessage` enum `Call` variants (#1125)
6d802416e2 increase pause before pining Rialto nodes (#1137)
b54fa56b62 calculate fee using full message payload (#1132)
ca5d8178f5 Add parachain pallets to rialto runtime (#1053)
9eaae4142e fix transaction resubmitter limits for Millau -> Rialto transactions (#1135)
9d4e17783c add --mandatory-headers-only cli option to complex relay (#1129)
1c5e0ec1cb Add local CI info to README (#1131)
a8e0929e14 chore: spellchecker fixes (#1130)
3b8e2118e3 set fee for importing mandatory headers to zero (#1127)
49bba9aa52 another bunch of words for spellchecker (#1128)
8a72eafef6 Increase pause before messages generation start (#1126)
1f0ba9a191 Move some associated types from relay_substrate_client::Chain to bp_runtime::Chain (#1087)
74bc1a5b54 Transactions resubmitter (#1083)
21ba001f26 log max balance drop when sending message (#1117)
638a7ddffa Code Cleaning (#1124)
be6555c51b Fix buildah logout (#1120)
87539c4a98 Format code work (#1116)
526fe7fdd7 fix spelling (#1119)
bd4ce7f241 Fix spelling (#1118)
3c1147858e added missing constants to Kusama/Polkadot primitives (#1114)
52093b22ab Fix delivery transaction estimation used by rational relayer (#1109)
77a2f2fbed Remove fund account checks from upgrade. (#1111)
824334802b Rename param and update comment (#1108)
d7784bfe06 Fix spellcheck (#1110)
0b18f5906a Refactor substrate messages source and substrate messages target (#1105)
b27240bbff fix compilation (#1107)
9697da4fe8 Emit mortal transactions from relay (#1073)
b29396c077 Change vault vars type to env vars (#1084)
35e0bbdc0c Make clippy mandatory. (#1103)
a517e8541f Remove unused deps (#1102)
873dae608a Remove unnessary deps (#1101)
13450b74ee Stored conversion rate updater (#1005)
74389829f3 [BREAKING] Migrate messages pallet to frame v2 (#1088)
424da938dd README fix (#1100)
865744c909 upgrade currency exchange pallet to frame v2 (#1097)
b5038148b3 Add missing docs (#1095)
0791e911c1 Common crate for substrate-relay (#1082)
3834c9d880 Update high-level-overview.md (#1093)
c93553face Increase the time window for messaging alerts. (#1092)
8b9cc3cecd migrate pallet-shift-session-manager to frame v2 (#1090)
dc91813c22 migrate eth PoA pallet to frame v2 (#1091)
f16bb098cc Migrate dispatch pallet  to frame v2 (#1089)
19f4325348 Bridge/This Chain Ids should be exposed as constants on pallet level. (#1085)
6381122df7 Change ChainSpec::from_genesis for Rialto and Millau chains to reflect the chain names. (#1079)
0f1d33e973 Make CI happy again (#1086)
238e65d96f fix typo (#1080)
fc008457b6 Token-swap-over-bridge pallet (#944)
3fb97fa5ef Fix full spellcheck (#1076)
eae4ed7170 fixed wrong trace (#1075)
219a0fad04 merge two weight-related loops in messages pallet (#1071)
fc85632fdb increase_message_fee depends on stored mesage size (#1066)
530f37a23b companion for https://github.com/paritytech/polkadot/pull/3507 (#1067)
53b8cba683 sc_basic_authorship=trace for millau nodes (#1074)
9874e05e98 Improve traces of message generator scripts (#1069)
7b5ee84fbb extract message_details impl into runtime common (#1070)
5a4aed5a8b refund weight for mot pruning messages (#1062)
90e3d1e111 Fix Westend -> Millau sync (#1064)
427d30ddfc When restarting client, also "restart" tokio runtime (#1065)
d47c05eeef Change get pipeline sensitive variables from Vault instead of GitLab settings (#1063)
d775a85415 use tokio reactor to execute jsonrpsee futures (#1061)
15c8cd61cb Use BABE to author blocks on Rialto (previously: Aura) (#1050)
5186293500 Allow reading suri && password override from file (#1059)
b506298262 Update jsonrpsee reference (#1049)
1734d00517 enable weight fee adjustent in Rialto/Millau (#1044)
607265afae Pay dispatch fee at target chain cli option (#1043)
ce79ef91be bump dependencies before start referencing polkadot repo (#1048)
924fa24f6d Cli option for greedy relayer + run no-losses relayer by default (#1042)
e21eba7b59 Yrong README Fixup + M1 Fixes (#1045)
20d08204a2 Confirm delivery detects when more than expected messages are confirmed (#1039)
994b846b52 pre and post dispatch weights of OnDeliveryConfirmed callback (#1040)
1dd5297e84 give real value to Rialto and Millau tokens (#1038)
035bee8715 Use real conversion rate in greedy relayer strategy (#1035)
9cfaecd0f7 fixed metrics prefix (#1037)
1d8d224937 Use kebab-case for bridge arguments (#1036)
f30a4c79a6 Shared reference to conversion rate metric value (#1034)
c34d7a5cbb estimate transaction fee (#1015)
93404b18bb change alert period from 2m to 10m for Westend -> Millau (GRANDPA or public node itself is lagging sometimes) (#1032)

git-subtree-dir: bridges
git-subtree-split: 407bf44a8a5f4e60aceef2dc755cd9ff09929ac3
2021-12-01 09:24:53 +01:00

601 lines
18 KiB
Rust

// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
use crate::{
error::Error,
finality::finalize_blocks,
validators::{Validators, ValidatorsConfiguration},
verification::{is_importable_header, verify_aura_header},
AuraConfiguration, ChainTime, ChangeToEnact, PruningStrategy, Storage,
};
use bp_eth_poa::{AuraHeader, HeaderId, Receipt};
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
/// Imports a bunch of headers and updates blocks finality.
///
/// Transactions receipts must be provided if `header_import_requires_receipts()`
/// has returned true.
/// If successful, returns tuple where first element is the number of useful headers
/// we have imported and the second element is the number of useless headers (duplicate)
/// we have NOT imported.
/// Returns error if fatal error has occurred during import. Some valid headers may be
/// imported in this case.
/// TODO: update me (https://github.com/paritytech/parity-bridges-common/issues/415)
#[allow(clippy::too_many_arguments)]
pub fn import_headers<S: Storage, PS: PruningStrategy, CT: ChainTime>(
storage: &mut S,
pruning_strategy: &mut PS,
aura_config: &AuraConfiguration,
validators_config: &ValidatorsConfiguration,
submitter: Option<S::Submitter>,
headers: Vec<(AuraHeader, Option<Vec<Receipt>>)>,
chain_time: &CT,
finalized_headers: &mut BTreeMap<S::Submitter, u64>,
) -> Result<(u64, u64), Error> {
let mut useful = 0;
let mut useless = 0;
for (header, receipts) in headers {
let import_result = import_header(
storage,
pruning_strategy,
aura_config,
validators_config,
submitter.clone(),
header,
chain_time,
receipts,
);
match import_result {
Ok((_, finalized)) => {
for (_, submitter) in finalized {
if let Some(submitter) = submitter {
*finalized_headers.entry(submitter).or_default() += 1;
}
}
useful += 1;
},
Err(Error::AncientHeader) | Err(Error::KnownHeader) => useless += 1,
Err(error) => return Err(error),
}
}
Ok((useful, useless))
}
/// A vector of finalized headers and their submitters.
pub type FinalizedHeaders<S> = Vec<(HeaderId, Option<<S as Storage>::Submitter>)>;
/// Imports given header and updates blocks finality (if required).
///
/// Transactions receipts must be provided if `header_import_requires_receipts()`
/// has returned true.
///
/// Returns imported block id and list of all finalized headers.
/// TODO: update me (https://github.com/paritytech/parity-bridges-common/issues/415)
#[allow(clippy::too_many_arguments)]
pub fn import_header<S: Storage, PS: PruningStrategy, CT: ChainTime>(
storage: &mut S,
pruning_strategy: &mut PS,
aura_config: &AuraConfiguration,
validators_config: &ValidatorsConfiguration,
submitter: Option<S::Submitter>,
header: AuraHeader,
chain_time: &CT,
receipts: Option<Vec<Receipt>>,
) -> Result<(HeaderId, FinalizedHeaders<S>), Error> {
// first check that we are able to import this header at all
let (header_id, finalized_id) = is_importable_header(storage, &header)?;
// verify header
let import_context = verify_aura_header(storage, aura_config, submitter, &header, chain_time)?;
// check if block schedules new validators
let validators = Validators::new(validators_config);
let (scheduled_change, enacted_change) =
validators.extract_validators_change(&header, receipts)?;
// check if block finalizes some other blocks and corresponding scheduled validators
let validators_set = import_context.validators_set();
let finalized_blocks = finalize_blocks(
storage,
finalized_id,
(validators_set.enact_block, &validators_set.validators),
header_id,
import_context.submitter(),
&header,
aura_config.two_thirds_majority_transition,
)?;
let enacted_change = enacted_change
.map(|validators| ChangeToEnact { signal_block: None, validators })
.or_else(|| {
validators.finalize_validators_change(storage, &finalized_blocks.finalized_headers)
});
// NOTE: we can't return Err() from anywhere below this line
// (because otherwise we'll have inconsistent storage if transaction will fail)
// and finally insert the block
let (best_id, best_total_difficulty) = storage.best_block();
let total_difficulty = import_context.total_difficulty() + header.difficulty;
let is_best = total_difficulty > best_total_difficulty;
storage.insert_header(import_context.into_import_header(
is_best,
header_id,
header,
total_difficulty,
enacted_change,
scheduled_change,
finalized_blocks.votes,
));
// compute upper border of updated pruning range
let new_best_block_id = if is_best { header_id } else { best_id };
let new_best_finalized_block_id = finalized_blocks.finalized_headers.last().map(|(id, _)| *id);
let pruning_upper_bound = pruning_strategy.pruning_upper_bound(
new_best_block_id.number,
new_best_finalized_block_id.map(|id| id.number).unwrap_or(finalized_id.number),
);
// now mark finalized headers && prune old headers
storage.finalize_and_prune_headers(new_best_finalized_block_id, pruning_upper_bound);
Ok((header_id, finalized_blocks.finalized_headers))
}
/// Returns true if transactions receipts are required to import given header.
pub fn header_import_requires_receipts<S: Storage>(
storage: &S,
validators_config: &ValidatorsConfiguration,
header: &AuraHeader,
) -> bool {
is_importable_header(storage, header)
.map(|_| Validators::new(validators_config))
.map(|validators| validators.maybe_signals_validators_change(header))
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
mock::{
run_test, secret_to_address, test_aura_config, test_validators_config, validator,
validators_addresses, validators_change_receipt, HeaderBuilder,
KeepSomeHeadersBehindBest, TestRuntime, GAS_LIMIT,
},
validators::ValidatorsSource,
BlocksToPrune, BridgeStorage, Headers, PruningRange,
};
use libsecp256k1::SecretKey;
const TOTAL_VALIDATORS: usize = 3;
#[test]
fn rejects_finalized_block_competitors() {
run_test(TOTAL_VALIDATORS, |_| {
let mut storage = BridgeStorage::<TestRuntime>::new();
storage.finalize_and_prune_headers(
Some(HeaderId { number: 100, ..Default::default() }),
0,
);
assert_eq!(
import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&test_validators_config(),
None,
Default::default(),
&(),
None,
),
Err(Error::AncientHeader),
);
});
}
#[test]
fn rejects_known_header() {
run_test(TOTAL_VALIDATORS, |ctx| {
let mut storage = BridgeStorage::<TestRuntime>::new();
let header = HeaderBuilder::with_parent(&ctx.genesis).sign_by(&validator(1));
assert_eq!(
import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&test_validators_config(),
None,
header.clone(),
&(),
None,
)
.map(|_| ()),
Ok(()),
);
assert_eq!(
import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&test_validators_config(),
None,
header,
&(),
None,
)
.map(|_| ()),
Err(Error::KnownHeader),
);
});
}
#[test]
fn import_header_works() {
run_test(TOTAL_VALIDATORS, |ctx| {
let validators_config = ValidatorsConfiguration::Multi(vec![
(0, ValidatorsSource::List(ctx.addresses.clone())),
(1, ValidatorsSource::List(validators_addresses(2))),
]);
let mut storage = BridgeStorage::<TestRuntime>::new();
let header = HeaderBuilder::with_parent(&ctx.genesis).sign_by(&validator(1));
let hash = header.compute_hash();
assert_eq!(
import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&validators_config,
None,
header,
&(),
None
)
.map(|_| ()),
Ok(()),
);
// check that new validators will be used for next header
let imported_header = Headers::<TestRuntime>::get(&hash).unwrap();
assert_eq!(
imported_header.next_validators_set_id,
1, // new set is enacted from config
);
});
}
#[test]
fn headers_are_pruned_during_import() {
run_test(TOTAL_VALIDATORS, |ctx| {
let validators_config = ValidatorsConfiguration::Single(ValidatorsSource::Contract(
[3; 20].into(),
ctx.addresses.clone(),
));
let validators = vec![validator(0), validator(1), validator(2)];
let mut storage = BridgeStorage::<TestRuntime>::new();
// header [0..11] are finalizing blocks [0; 9]
// => since we want to keep 10 finalized blocks, we aren't pruning anything
let mut latest_block_id = Default::default();
for i in 1..11 {
let header = HeaderBuilder::with_parent_number(i - 1).sign_by_set(&validators);
let parent_id = header.parent_id().unwrap();
let (rolling_last_block_id, finalized_blocks) = import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&validators_config,
Some(100),
header,
&(),
None,
)
.unwrap();
match i {
2..=10 => {
assert_eq!(finalized_blocks, vec![(parent_id, Some(100))], "At {}", i,)
},
_ => assert_eq!(finalized_blocks, vec![], "At {}", i),
}
latest_block_id = rolling_last_block_id;
}
assert!(storage.header(&ctx.genesis.compute_hash()).is_some());
// header 11 finalizes headers [10] AND schedules change
// => we prune header#0
let header11 = HeaderBuilder::with_parent_number(10)
.log_bloom((&[0xff; 256]).into())
.receipts_root(
"ead6c772ba0083bbff497ba0f4efe47c199a2655401096c21ab7450b6c466d97"
.parse()
.unwrap(),
)
.sign_by_set(&validators);
let parent_id = header11.parent_id().unwrap();
let (rolling_last_block_id, finalized_blocks) = import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&validators_config,
Some(101),
header11.clone(),
&(),
Some(vec![validators_change_receipt(latest_block_id.hash)]),
)
.unwrap();
assert_eq!(finalized_blocks, vec![(parent_id, Some(100))],);
assert!(storage.header(&ctx.genesis.compute_hash()).is_none());
latest_block_id = rolling_last_block_id;
// and now let's say validators 1 && 2 went offline
// => in the range 12-25 no blocks are finalized, but we still continue to prune old
// headers until header#11 is met. we can't prune #11, because it schedules change
let mut step = 56u64;
let mut expected_blocks = vec![(header11.compute_id(), Some(101))];
for i in 12..25 {
let header = HeaderBuilder::with_parent_hash(latest_block_id.hash)
.difficulty(i.into())
.step(step)
.sign_by_set(&validators);
expected_blocks.push((header.compute_id(), Some(102)));
let (rolling_last_block_id, finalized_blocks) = import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&validators_config,
Some(102),
header,
&(),
None,
)
.unwrap();
assert_eq!(finalized_blocks, vec![],);
latest_block_id = rolling_last_block_id;
step += 3;
}
assert_eq!(
BlocksToPrune::<TestRuntime, ()>::get(),
PruningRange { oldest_unpruned_block: 11, oldest_block_to_keep: 14 },
);
// now let's insert block signed by validator 1
// => blocks 11..24 are finalized and blocks 11..14 are pruned
step -= 2;
let header = HeaderBuilder::with_parent_hash(latest_block_id.hash)
.difficulty(25.into())
.step(step)
.sign_by_set(&validators);
let (_, finalized_blocks) = import_header(
&mut storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&validators_config,
Some(103),
header,
&(),
None,
)
.unwrap();
assert_eq!(finalized_blocks, expected_blocks);
assert_eq!(
BlocksToPrune::<TestRuntime, ()>::get(),
PruningRange { oldest_unpruned_block: 15, oldest_block_to_keep: 15 },
);
});
}
fn import_custom_block<S: Storage>(
storage: &mut S,
validators: &[SecretKey],
header: AuraHeader,
) -> Result<HeaderId, Error> {
let id = header.compute_id();
import_header(
storage,
&mut KeepSomeHeadersBehindBest::default(),
&test_aura_config(),
&ValidatorsConfiguration::Single(ValidatorsSource::Contract(
[0; 20].into(),
validators.iter().map(secret_to_address).collect(),
)),
None,
header,
&(),
None,
)
.map(|_| id)
}
#[test]
fn import_of_non_best_block_may_finalize_blocks() {
run_test(TOTAL_VALIDATORS, |ctx| {
let mut storage = BridgeStorage::<TestRuntime>::new();
// insert headers (H1, validator1), (H2, validator1), (H3, validator1)
// making H3 the best header, without finalizing anything (we need 2 signatures)
let mut expected_best_block = Default::default();
for i in 1..4 {
let step = 1 + i * TOTAL_VALIDATORS as u64;
expected_best_block = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(i - 1)
.step(step)
.sign_by_set(&ctx.validators),
)
.unwrap();
}
let (best_block, best_difficulty) = storage.best_block();
assert_eq!(best_block, expected_best_block);
assert_eq!(storage.finalized_block(), ctx.genesis.compute_id());
// insert headers (H1', validator1), (H2', validator2), finalizing H2, even though H3
// has better difficulty than H2' (because there are more steps involved)
let mut expected_finalized_block = Default::default();
let mut parent_hash = ctx.genesis.compute_hash();
for i in 1..3 {
let step = i;
let id = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_hash(parent_hash)
.step(step)
.gas_limit((GAS_LIMIT + 1).into())
.sign_by_set(&ctx.validators),
)
.unwrap();
parent_hash = id.hash;
if i == 1 {
expected_finalized_block = id;
}
}
let (new_best_block, new_best_difficulty) = storage.best_block();
assert_eq!(new_best_block, expected_best_block);
assert_eq!(new_best_difficulty, best_difficulty);
assert_eq!(storage.finalized_block(), expected_finalized_block);
});
}
#[test]
fn append_to_unfinalized_fork_fails() {
const VALIDATORS: u64 = 5;
run_test(VALIDATORS as usize, |ctx| {
let mut storage = BridgeStorage::<TestRuntime>::new();
// header1, authored by validator[2] is best common block between two competing forks
let header1 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(0).step(2).sign_by_set(&ctx.validators),
)
.unwrap();
assert_eq!(storage.best_block().0, header1);
assert_eq!(storage.finalized_block().number, 0);
// validator[3] has authored header2 (nothing is finalized yet)
let header2 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(1).step(3).sign_by_set(&ctx.validators),
)
.unwrap();
assert_eq!(storage.best_block().0, header2);
assert_eq!(storage.finalized_block().number, 0);
// validator[4] has authored header3 (header1 is finalized)
let header3 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(2).step(4).sign_by_set(&ctx.validators),
)
.unwrap();
assert_eq!(storage.best_block().0, header3);
assert_eq!(storage.finalized_block(), header1);
// validator[4] has authored 4 blocks: header2'...header5' (header1 is still finalized)
let header2_1 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(1)
.gas_limit((GAS_LIMIT + 1).into())
.step(4)
.sign_by_set(&ctx.validators),
)
.unwrap();
let header3_1 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_hash(header2_1.hash)
.step(4 + VALIDATORS)
.sign_by_set(&ctx.validators),
)
.unwrap();
let header4_1 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_hash(header3_1.hash)
.step(4 + VALIDATORS * 2)
.sign_by_set(&ctx.validators),
)
.unwrap();
let header5_1 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_hash(header4_1.hash)
.step(4 + VALIDATORS * 3)
.sign_by_set(&ctx.validators),
)
.unwrap();
assert_eq!(storage.best_block().0, header5_1);
assert_eq!(storage.finalized_block(), header1);
// when we import header4 { parent = header3 }, authored by validator[0], header2 is
// finalized
let header4 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(3).step(5).sign_by_set(&ctx.validators),
)
.unwrap();
assert_eq!(storage.best_block().0, header5_1);
assert_eq!(storage.finalized_block(), header2);
// when we import header5 { parent = header4 }, authored by validator[1], header3 is
// finalized
let header5 = import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_hash(header4.hash)
.step(6)
.sign_by_set(&ctx.validators),
)
.unwrap();
assert_eq!(storage.best_block().0, header5);
assert_eq!(storage.finalized_block(), header3);
// import of header2'' { parent = header1 } fails, because it has number <
// best_finalized
assert_eq!(
import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(1)
.gas_limit((GAS_LIMIT + 1).into())
.step(3)
.sign_by_set(&ctx.validators)
),
Err(Error::AncientHeader),
);
// import of header6' should also fail because we're trying to append to fork thas
// has forked before finalized block
assert_eq!(
import_custom_block(
&mut storage,
&ctx.validators,
HeaderBuilder::with_parent_number(5)
.gas_limit((GAS_LIMIT + 1).into())
.step(5 + VALIDATORS * 4)
.sign_by_set(&ctx.validators),
),
Err(Error::TryingToFinalizeSibling),
);
});
}
}