Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 31c633c

Browse files
authored
Revert "Header-only sync for old forks (#3942)" (#4022)
This reverts commit ac78c90.
1 parent ac78c90 commit 31c633c

File tree

11 files changed

+81
-179
lines changed

11 files changed

+81
-179
lines changed

core/client/db/src/lib.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,6 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
894894
inmem
895895
}
896896

897-
/// Returns total numbet of blocks (headers) in the block DB.
898-
#[cfg(feature = "test-helpers")]
899-
pub fn blocks_count(&self) -> u64 {
900-
self.blockchain.db.iter(columns::HEADER).count() as u64
901-
}
902-
903897
/// Read (from storage or cache) changes trie config.
904898
///
905899
/// Currently changes tries configuration is set up once (at genesis) and could not
@@ -1121,7 +1115,7 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
11211115
);
11221116

11231117
transaction.put(columns::HEADER, &lookup_key, &pending_block.header.encode());
1124-
if let Some(body) = &pending_block.body {
1118+
if let Some(body) = pending_block.body {
11251119
transaction.put(columns::BODY, &lookup_key, &body.encode());
11261120
}
11271121
if let Some(justification) = pending_block.justification {
@@ -1133,26 +1127,21 @@ impl<Block: BlockT<Hash=H256>> Backend<Block> {
11331127
transaction.put(columns::META, meta_keys::GENESIS_HASH, hash.as_ref());
11341128
}
11351129

1136-
let finalized = if pending_block.body.is_some() {
1137-
let mut changeset: state_db::ChangeSet<Vec<u8>> = state_db::ChangeSet::default();
1138-
for (key, (val, rc)) in operation.db_updates.drain() {
1139-
if rc > 0 {
1140-
changeset.inserted.push((key, val.to_vec()));
1141-
} else if rc < 0 {
1142-
changeset.deleted.push(key);
1143-
}
1130+
let mut changeset: state_db::ChangeSet<Vec<u8>> = state_db::ChangeSet::default();
1131+
for (key, (val, rc)) in operation.db_updates.drain() {
1132+
if rc > 0 {
1133+
changeset.inserted.push((key, val.to_vec()));
1134+
} else if rc < 0 {
1135+
changeset.deleted.push(key);
11441136
}
1145-
let number_u64 = number.saturated_into::<u64>();
1146-
let commit = self.storage.state_db.insert_block(&hash, number_u64, &pending_block.header.parent_hash(), changeset)
1147-
.map_err(|e: state_db::Error<io::Error>| client::error::Error::from(format!("State database error: {:?}", e)))?;
1148-
apply_state_commit(&mut transaction, commit);
1149-
1150-
// Check if need to finalize. Genesis is always finalized instantly.
1151-
let finalized = number_u64 == 0 || pending_block.leaf_state.is_final();
1152-
finalized
1153-
} else {
1154-
false
1155-
};
1137+
}
1138+
let number_u64 = number.saturated_into::<u64>();
1139+
let commit = self.storage.state_db.insert_block(&hash, number_u64, &pending_block.header.parent_hash(), changeset)
1140+
.map_err(|e: state_db::Error<io::Error>| client::error::Error::from(format!("State database error: {:?}", e)))?;
1141+
apply_state_commit(&mut transaction, commit);
1142+
1143+
// Check if need to finalize. Genesis is always finalized instantly.
1144+
let finalized = number_u64 == 0 || pending_block.leaf_state.is_final();
11561145

11571146
let header = &pending_block.header;
11581147
let is_best = pending_block.leaf_state.is_best();
@@ -1592,7 +1581,7 @@ mod tests {
15921581
};
15931582
let mut op = backend.begin_operation().unwrap();
15941583
backend.begin_state_operation(&mut op, block_id).unwrap();
1595-
op.set_block_data(header, Some(Vec::new()), None, NewBlockState::Best).unwrap();
1584+
op.set_block_data(header, None, None, NewBlockState::Best).unwrap();
15961585
op.update_changes_trie((changes_trie_update, ChangesTrieCacheAction::Clear)).unwrap();
15971586
backend.commit_operation(op).unwrap();
15981587

core/client/src/client.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -927,39 +927,22 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
927927
BlockOrigin::Genesis | BlockOrigin::NetworkInitialSync | BlockOrigin::File => false,
928928
};
929929

930-
let storage_changes = match &body {
931-
Some(body) => {
932-
self.backend.begin_state_operation(&mut operation.op, BlockId::Hash(parent_hash))?;
933-
934-
// ensure parent block is finalized to maintain invariant that
935-
// finality is called sequentially.
936-
if finalized {
937-
self.apply_finality_with_block_hash(operation, parent_hash, None, info.best_hash, make_notifications)?;
938-
}
930+
self.backend.begin_state_operation(&mut operation.op, BlockId::Hash(parent_hash))?;
939931

940-
// FIXME #1232: correct path logic for when to execute this function
941-
let (storage_update, changes_update, storage_changes) = self.block_execution(
942-
&operation.op,
943-
&import_headers,
944-
origin,
945-
hash,
946-
&body,
947-
)?;
932+
// ensure parent block is finalized to maintain invariant that
933+
// finality is called sequentially.
934+
if finalized {
935+
self.apply_finality_with_block_hash(operation, parent_hash, None, info.best_hash, make_notifications)?;
936+
}
948937

949-
operation.op.update_cache(new_cache);
950-
if let Some(storage_update) = storage_update {
951-
operation.op.update_db_storage(storage_update)?;
952-
}
953-
if let Some(storage_changes) = storage_changes.clone() {
954-
operation.op.update_storage(storage_changes.0, storage_changes.1)?;
955-
}
956-
if let Some(Some(changes_update)) = changes_update {
957-
operation.op.update_changes_trie(changes_update)?;
958-
}
959-
storage_changes
960-
},
961-
None => Default::default()
962-
};
938+
// FIXME #1232: correct path logic for when to execute this function
939+
let (storage_update, changes_update, storage_changes) = self.block_execution(
940+
&operation.op,
941+
&import_headers,
942+
origin,
943+
hash,
944+
body.clone(),
945+
)?;
963946

964947
let is_new_best = finalized || match fork_choice {
965948
ForkChoiceStrategy::LongestChain => import_headers.post().number() > &info.best_number,
@@ -994,6 +977,17 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
994977
leaf_state,
995978
)?;
996979

980+
operation.op.update_cache(new_cache);
981+
if let Some(storage_update) = storage_update {
982+
operation.op.update_db_storage(storage_update)?;
983+
}
984+
if let Some(storage_changes) = storage_changes.clone() {
985+
operation.op.update_storage(storage_changes.0, storage_changes.1)?;
986+
}
987+
if let Some(Some(changes_update)) = changes_update {
988+
operation.op.update_changes_trie(changes_update)?;
989+
}
990+
997991
operation.op.insert_aux(aux)?;
998992

999993
if make_notifications {
@@ -1020,7 +1014,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
10201014
import_headers: &PrePostHeader<Block::Header>,
10211015
origin: BlockOrigin,
10221016
hash: Block::Hash,
1023-
body: &[Block::Extrinsic],
1017+
body: Option<Vec<Block::Extrinsic>>,
10241018
) -> error::Result<(
10251019
Option<StorageUpdate<B, Block>>,
10261020
Option<Option<ChangesUpdate<Block>>>,
@@ -1058,7 +1052,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where
10581052

10591053
let encoded_block = <Block as BlockT>::encode_from(
10601054
import_headers.pre(),
1061-
body,
1055+
&body.unwrap_or_default()
10621056
);
10631057

10641058
let (_, storage_update, changes_update) = self.executor
@@ -1529,7 +1523,7 @@ impl<'a, B, E, Block, RA> consensus::BlockImport<Block> for &'a Client<B, E, Blo
15291523
&mut self,
15301524
block: BlockCheckParams<Block>,
15311525
) -> Result<ImportResult, Self::Error> {
1532-
let BlockCheckParams { hash, number, parent_hash, header_only } = block;
1526+
let BlockCheckParams { hash, number, parent_hash } = block;
15331527

15341528
if let Some(h) = self.fork_blocks.as_ref().and_then(|x| x.get(&number)) {
15351529
if &hash != h {
@@ -1547,9 +1541,7 @@ impl<'a, B, E, Block, RA> consensus::BlockImport<Block> for &'a Client<B, E, Blo
15471541
.map_err(|e| ConsensusError::ClientImport(e.to_string()))?
15481542
{
15491543
BlockStatus::InChainWithState | BlockStatus::Queued => {},
1550-
BlockStatus::Unknown => return Ok(ImportResult::UnknownParent),
1551-
BlockStatus::InChainPruned if header_only => {},
1552-
BlockStatus::InChainPruned => return Ok(ImportResult::MissingState),
1544+
BlockStatus::Unknown | BlockStatus::InChainPruned => return Ok(ImportResult::UnknownParent),
15531545
BlockStatus::KnownBad => return Ok(ImportResult::KnownBad),
15541546
}
15551547

@@ -1561,6 +1553,7 @@ impl<'a, B, E, Block, RA> consensus::BlockImport<Block> for &'a Client<B, E, Blo
15611553
BlockStatus::KnownBad => return Ok(ImportResult::KnownBad),
15621554
}
15631555

1556+
15641557
Ok(ImportResult::imported(false))
15651558
}
15661559
}

core/consensus/common/src/block_import.rs

-6
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ pub enum ImportResult {
3535
KnownBad,
3636
/// Block parent is not in the chain.
3737
UnknownParent,
38-
/// Parent state is missing.
39-
MissingState,
4038
}
4139

4240
/// Auxiliary data associated with an imported block result.
4341
#[derive(Debug, Default, PartialEq, Eq)]
4442
pub struct ImportedAux {
45-
/// Only the header has been imported. Block body verification was skipped.
46-
pub header_only: bool,
4743
/// Clear all pending justification requests.
4844
pub clear_justification_requests: bool,
4945
/// Request a justification for the given block.
@@ -102,8 +98,6 @@ pub struct BlockCheckParams<Block: BlockT> {
10298
pub number: NumberFor<Block>,
10399
/// Parent hash of the block that we verify.
104100
pub parent_hash: Block::Hash,
105-
/// Don't check state availability
106-
pub header_only: bool,
107101
}
108102

109103
/// Data required to import a Block.

core/consensus/common/src/import_queue.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
203203
Ok(BlockImportResult::ImportedKnown(number))
204204
},
205205
Ok(ImportResult::Imported(aux)) => Ok(BlockImportResult::ImportedUnknown(number, aux, peer.clone())),
206-
Ok(ImportResult::MissingState) => {
207-
debug!(target: "sync", "Parent state is missing for {}: {:?}, parent: {:?}", number, hash, parent_hash);
208-
Err(BlockImportError::UnknownParent)
209-
},
210206
Ok(ImportResult::UnknownParent) => {
211207
debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", number, hash, parent_hash);
212208
Err(BlockImportError::UnknownParent)
@@ -221,12 +217,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
221217
}
222218
}
223219
};
224-
match import_error(import_handle.check_block(BlockCheckParams {
225-
hash,
226-
number,
227-
parent_hash,
228-
header_only: block.body.is_none(),
229-
}))? {
220+
match import_error(import_handle.check_block(BlockCheckParams { hash, number, parent_hash }))? {
230221
BlockImportResult::ImportedUnknown { .. } => (),
231222
r => return Ok(r), // Any other successful result means that the block is already imported.
232223
}

core/finality-grandpa/src/light_import.rs

-4
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,6 @@ pub mod tests {
680680
bad_justification: false,
681681
needs_finality_proof: false,
682682
is_new_best: true,
683-
header_only: false,
684683
}));
685684
}
686685

@@ -693,7 +692,6 @@ pub mod tests {
693692
bad_justification: false,
694693
needs_finality_proof: false,
695694
is_new_best: true,
696-
header_only: false,
697695
}));
698696
}
699697

@@ -707,7 +705,6 @@ pub mod tests {
707705
bad_justification: false,
708706
needs_finality_proof: true,
709707
is_new_best: true,
710-
header_only: false,
711708
}));
712709
}
713710

@@ -724,7 +721,6 @@ pub mod tests {
724721
bad_justification: false,
725722
needs_finality_proof: true,
726723
is_new_best: false,
727-
header_only: false,
728724
},
729725
));
730726
}

core/finality-grandpa/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,6 @@ fn allows_reimporting_change_blocks() {
984984
bad_justification: false,
985985
needs_finality_proof: false,
986986
is_new_best: true,
987-
header_only: false,
988987
}),
989988
);
990989

0 commit comments

Comments
 (0)