Skip to content

Commit

Permalink
Merge branch 'main' into domain-instance-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
NingLin-P authored Jul 31, 2023
2 parents 8e373c4 + f1aaba5 commit 3c68abe
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
19 changes: 11 additions & 8 deletions crates/sc-consensus-subspace/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use sp_objects::ObjectsApi;
use sp_runtime::generic::SignedBlock;
use sp_runtime::traits::{Block as BlockT, CheckedSub, Header, NumberFor, One, Zero};
use std::future::Future;
use std::slice;
use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::Arc;
use subspace_archiving::archiver::{Archiver, NewArchivedSegment};
Expand Down Expand Up @@ -687,6 +688,16 @@ where
for archived_segment in archiver.add_block(encoded_block, block_object_mappings) {
let segment_header = archived_segment.segment_header;

if let Err(error) =
segment_headers_store.add_segment_headers(slice::from_ref(&segment_header))
{
error!(
target: "subspace",
"Failed to store segment headers: {error}"
);
return;
}

send_archived_segment_notification(
&archived_segment_notification_sender,
archived_segment,
Expand All @@ -697,14 +708,6 @@ where
}

if !new_segment_headers.is_empty() {
if let Err(error) = segment_headers_store.add_segment_headers(&new_segment_headers)
{
error!(
target: "subspace",
"Failed to store segment headers: {error}"
);
return;
}
let maybe_block_number_to_finalize = {
let mut segment_headers = segment_headers.lock();
segment_headers.put(block_number + One::one(), new_segment_headers);
Expand Down
10 changes: 9 additions & 1 deletion crates/sc-consensus-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub enum Error<Header: HeaderT> {
/// Stored segment header extrinsic was not found
#[error("Stored segment header extrinsic was not found: {0:?}")]
SegmentHeadersExtrinsicNotFound(Vec<SegmentHeader>),
/// Duplicated segment commitment
/// Different segment commitment found
#[error(
"Different segment commitment for segment index {0} was found in storage, likely fork \
below archiving point"
Expand Down Expand Up @@ -1161,6 +1161,14 @@ where
.segment_commitment();

if &found_segment_commitment != segment_commitment {
warn!(
target: "subspace",
"Different segment commitment for segment index {} was found in storage, \
likely fork below archiving point. expected {:?}, found {:?}",
segment_index,
segment_commitment,
found_segment_commitment
);
return Err(ConsensusError::ClientImport(
Error::<Block::Header>::DifferentSegmentCommitment(segment_index).to_string(),
));
Expand Down
50 changes: 48 additions & 2 deletions crates/subspace-farmer/src/single_disk_plot/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ where

while let Some(()) = archived_segments_receiver.next().await {
let archived_segment_header = last_archived_segment.load(Ordering::SeqCst);
trace!(
segment_index = %archived_segment_header.segment_index(),
"New archived segment received",
);

// It is fine to take a synchronous read lock here because the only time
// write lock is taken is during plotting, which we know doesn't happen
Expand All @@ -460,7 +464,23 @@ where
.collect_into(&mut sectors_to_check);
for (sector_index, history_size) in sectors_to_check.drain(..) {
if let Some(sector_expire_at) = sectors_expire_at.get(&sector_index) {
if *sector_expire_at >= archived_segment_header.segment_index() {
trace!(
%sector_index,
%history_size,
%sector_expire_at,
"Checking sector for expiration"
);
// +1 means we will start replotting a bit before it actually expires to avoid
// storing expired sectors
if *sector_expire_at
<= (archived_segment_header.segment_index() + SegmentIndex::ONE)
{
debug!(
%sector_index,
%history_size,
%sector_expire_at,
"Sector expires soon #1, scheduling replotting"
);
// Time to replot
sector_indices_to_replot.push(sector_index);
}
Expand All @@ -471,6 +491,12 @@ where
.sector_expiration_check(min_sector_lifetime)
.map(|expiration_check_history_size| expiration_check_history_size.segment_index())
{
trace!(
%sector_index,
%history_size,
%expiration_check_segment_index,
"Determined sector expiration check segment index"
);
let maybe_sector_expiration_check_segment_commitment =
if let Some(segment_commitment) =
archived_segment_commitments_cache.get(&expiration_check_segment_index)
Expand Down Expand Up @@ -508,12 +534,32 @@ where
metadata; qed",
);

trace!(
%sector_index,
%history_size,
sector_expire_at = %expiration_history_size.segment_index(),
"Determined sector expiration segment index"
);
// +1 means we will start replotting a bit before it actually expires to avoid
// storing expired sectors
if expiration_history_size.segment_index()
>= archived_segment_header.segment_index()
<= (archived_segment_header.segment_index() + SegmentIndex::ONE)
{
debug!(
%sector_index,
%history_size,
sector_expire_at = %expiration_history_size.segment_index(),
"Sector expires soon #2, scheduling replotting"
);
// Time to replot
sector_indices_to_replot.push(sector_index);
} else {
trace!(
%sector_index,
%history_size,
sector_expire_at = %expiration_history_size.segment_index(),
"Sector expires later, remembering sector expiration"
);
// Store expiration so we don't have to recalculate it later
sectors_expire_at
.insert(sector_index, expiration_history_size.segment_index());
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-verification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ where
}
};

if expiration_history_size >= *current_history_size {
if expiration_history_size <= *current_history_size {
return Err(Error::SectorExpired {
expiration_history_size,
current_history_size: *current_history_size,
Expand Down

0 comments on commit 3c68abe

Please sign in to comment.