Skip to content

Commit eed2c70

Browse files
authored
Merge pull request #3347 from autonomys/optimize-block-finalization
Optimize block finalization
2 parents 6aa60d7 + e5a510a commit eed2c70

File tree

1 file changed

+54
-35
lines changed

1 file changed

+54
-35
lines changed

crates/sc-consensus-subspace/src/archiver.rs

Lines changed: 54 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ use rand::prelude::*;
5858
use rand_chacha::ChaCha8Rng;
5959
use rayon::prelude::*;
6060
use rayon::ThreadPoolBuilder;
61-
use sc_client_api::{AuxStore, Backend as BackendT, BlockBackend, Finalizer, LockImportRun};
61+
use sc_client_api::{
62+
AuxStore, Backend as BackendT, BlockBackend, BlockchainEvents, Finalizer, LockImportRun,
63+
};
6264
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_INFO};
6365
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedSender};
6466
use sp_api::ProvideRuntimeApi;
@@ -830,7 +832,7 @@ where
830832

831833
fn finalize_block<Block, Backend, Client>(
832834
client: &Client,
833-
telemetry: Option<TelemetryHandle>,
835+
telemetry: Option<&TelemetryHandle>,
834836
hash: Block::Hash,
835837
number: NumberFor<Block>,
836838
) where
@@ -916,6 +918,7 @@ where
916918
+ HeaderBackend<Block>
917919
+ LockImportRun<Block, Backend>
918920
+ Finalizer<Block, Backend>
921+
+ BlockchainEvents<Block>
919922
+ AuxStore
920923
+ Send
921924
+ Sync
@@ -1055,19 +1058,67 @@ where
10551058
}
10561059
}
10571060

1061+
let max_segment_index_before = segment_headers_store.max_segment_index();
10581062
(best_archived_block_hash, best_archived_block_number) = archive_block(
10591063
&mut archiver,
10601064
segment_headers_store.clone(),
10611065
&*client,
10621066
&sync_oracle,
1063-
telemetry.clone(),
10641067
subspace_link.object_mapping_notification_sender.clone(),
10651068
subspace_link.archived_segment_notification_sender.clone(),
10661069
best_archived_block_hash,
10671070
block_number_to_archive,
10681071
create_object_mappings,
10691072
)
10701073
.await?;
1074+
1075+
let max_segment_index = segment_headers_store.max_segment_index();
1076+
if max_segment_index_before != max_segment_index {
1077+
let maybe_block_number_to_finalize = max_segment_index
1078+
// Skip last `FINALIZATION_DEPTH_IN_SEGMENTS` archived segments
1079+
.and_then(|max_segment_index| {
1080+
max_segment_index.checked_sub(FINALIZATION_DEPTH_IN_SEGMENTS)
1081+
})
1082+
.and_then(|segment_index| {
1083+
segment_headers_store.get_segment_header(segment_index)
1084+
})
1085+
.map(|segment_header| segment_header.last_archived_block().number)
1086+
// Make sure not to finalize block number that does not yet exist (segment
1087+
// headers store may contain future blocks during initial sync)
1088+
.map(|block_number| block_number_to_archive.min(block_number.into()))
1089+
// Do not finalize blocks twice
1090+
.filter(|block_number| *block_number > client.info().finalized_number);
1091+
1092+
if let Some(block_number_to_finalize) = maybe_block_number_to_finalize {
1093+
{
1094+
let mut import_notification = client.every_import_notification_stream();
1095+
1096+
// Drop notification to drop acknowledgement and allow block import to
1097+
// proceed
1098+
drop(block_importing_notification);
1099+
1100+
while let Some(notification) = import_notification.next().await {
1101+
// Wait for importing block to finish importing
1102+
if notification.header.number() == &importing_block_number {
1103+
break;
1104+
}
1105+
}
1106+
}
1107+
1108+
// Block is not guaranteed to be present this deep if we have only synced recent
1109+
// blocks
1110+
if let Some(block_hash_to_finalize) =
1111+
client.block_hash(block_number_to_finalize)?
1112+
{
1113+
finalize_block(
1114+
&*client,
1115+
telemetry.as_ref(),
1116+
block_hash_to_finalize,
1117+
block_number_to_finalize,
1118+
);
1119+
}
1120+
}
1121+
}
10711122
}
10721123

10731124
Ok(())
@@ -1081,7 +1132,6 @@ async fn archive_block<Block, Backend, Client, AS, SO>(
10811132
segment_headers_store: SegmentHeadersStore<AS>,
10821133
client: &Client,
10831134
sync_oracle: &SubspaceSyncOracle<SO>,
1084-
telemetry: Option<TelemetryHandle>,
10851135
object_mapping_notification_sender: SubspaceNotificationSender<ObjectMappingNotification>,
10861136
archived_segment_notification_sender: SubspaceNotificationSender<ArchivedSegmentNotification>,
10871137
best_archived_block_hash: Block::Hash,
@@ -1157,7 +1207,6 @@ where
11571207
encoded_block.len() as f32 / 1024.0
11581208
);
11591209

1160-
let mut new_segment_headers = Vec::new();
11611210
let block_outcome = archiver.add_block(
11621211
encoded_block,
11631212
block_object_mappings,
@@ -1175,36 +1224,6 @@ where
11751224

11761225
send_archived_segment_notification(&archived_segment_notification_sender, archived_segment)
11771226
.await;
1178-
1179-
new_segment_headers.push(segment_header);
1180-
}
1181-
1182-
if !new_segment_headers.is_empty() {
1183-
let maybe_block_number_to_finalize = segment_headers_store
1184-
.max_segment_index()
1185-
// Skip last `FINALIZATION_DEPTH_IN_SEGMENTS` archived segments
1186-
.and_then(|max_segment_index| {
1187-
max_segment_index.checked_sub(FINALIZATION_DEPTH_IN_SEGMENTS)
1188-
})
1189-
.and_then(|segment_index| segment_headers_store.get_segment_header(segment_index))
1190-
.map(|segment_header| segment_header.last_archived_block().number)
1191-
// Make sure not to finalize block number that does not yet exist (segment
1192-
// headers store may contain future blocks during initial sync)
1193-
.map(|block_number| block_number_to_archive.min(block_number.into()))
1194-
// Do not finalize blocks twice
1195-
.filter(|block_number| *block_number > client.info().finalized_number);
1196-
1197-
if let Some(block_number_to_finalize) = maybe_block_number_to_finalize {
1198-
// Block is not guaranteed to be present this deep if we have only synced recent blocks
1199-
if let Some(block_hash_to_finalize) = client.block_hash(block_number_to_finalize)? {
1200-
finalize_block(
1201-
client,
1202-
telemetry.clone(),
1203-
block_hash_to_finalize,
1204-
block_number_to_finalize,
1205-
);
1206-
}
1207-
}
12081227
}
12091228

12101229
Ok((block_hash_to_archive, block_number_to_archive))

0 commit comments

Comments
 (0)