Skip to content

Commit

Permalink
Merge pull request #1715 from subspace/fix-genesis-block-encoding-on-…
Browse files Browse the repository at this point in the history
…restart

Fix genesis block encoding on restart
  • Loading branch information
nazar-pc authored Jul 29, 2023
2 parents d2b3944 + fb51e6a commit 0eb416e
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions crates/sc-consensus-subspace/src/archiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,35 @@ where
best_archived_block: (Block::Hash, NumberFor<Block>),
}

fn encode_genesis_block<Block>(block: &SignedBlock<Block>) -> Vec<u8>
where
Block: BlockT,
{
let mut encoded_block = block.encode();
let encoded_block_length = encoded_block.len();

// We extend encoding of genesis block with extra data such that the very first
// archived segment can be produced right away, bootstrapping the farming
// process.
//
// Note: we add it to the end of the encoded block, so during decoding it'll
// actually be ignored (unless `DecodeAll::decode_all()` is used) even though it
// is technically present in encoded form.
encoded_block.resize(RecordedHistorySegment::SIZE, 0);
let mut rng = ChaCha8Rng::from_seed(
block
.block
.header()
.state_root()
.as_ref()
.try_into()
.expect("State root in Subspace must be 32 bytes, panic otherwise; qed"),
);
rng.fill(&mut encoded_block[encoded_block_length..]);

encoded_block
}

fn initialize_archiver<Block, Client, AS>(
best_block_hash: Block::Hash,
best_block_number: NumberFor<Block>,
Expand Down Expand Up @@ -337,10 +366,17 @@ where
*last_archived_block.block.header().number(),
));

let last_archived_block_encoded =
if last_archived_block.block.header().number().is_zero() {
encode_genesis_block(&last_archived_block)
} else {
last_archived_block.encode()
};

Archiver::with_initial_state(
kzg,
last_segment_header,
&last_archived_block.encode(),
&last_archived_block_encoded,
block_object_mappings,
)
.expect("Incorrect parameters for archiver")
Expand Down Expand Up @@ -412,31 +448,7 @@ where
.unwrap_or_default();

let encoded_block = if block_number_to_archive.is_zero() {
let mut encoded_block = block.encode();
let encoded_block_length = encoded_block.len();

// We extend encoding of genesis block with extra data such that the very first
// archived segment can be produced right away, bootstrapping the farming
// process.
//
// Note: we add it to the end of the encoded block, so during decoding it'll
// actually be ignored (unless `DecodeAll::decode_all()` is used) even though it
// is technically present in encoded form.
encoded_block.resize(RecordedHistorySegment::SIZE, 0);
let mut rng = ChaCha8Rng::from_seed(
block
.block
.header()
.state_root()
.as_ref()
.try_into()
.expect(
"State root in Subspace must be 32 bytes, panic otherwise; qed",
),
);
rng.fill(&mut encoded_block[encoded_block_length..]);

encoded_block
encode_genesis_block(&block)
} else {
block.encode()
};
Expand Down

0 comments on commit 0eb416e

Please sign in to comment.