Skip to content

Commit

Permalink
Merge pull request #2290 from subspace/fraud-proof-fixes
Browse files Browse the repository at this point in the history
Several minor fraud proof fixes
  • Loading branch information
NingLin-P authored Dec 6, 2023
2 parents b7b01c1 + 4d8e011 commit 7004b4b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
8 changes: 6 additions & 2 deletions crates/sp-domains-fraud-proof/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ where
.map(|resp| resp.into_domain_set_code_extrinsic())
.ok_or(VerificationError::FailedToDeriveDomainSetCodeExtrinsic)?;

let bad_receipt_valid_bundle_digests = bad_receipt.valid_bundle_digests();
if valid_bundle_digests.len() != bad_receipt_valid_bundle_digests.len() {
return Err(VerificationError::InvalidBundleDigest);
}

let mut bundle_extrinsics_digests = Vec::new();
for (bad_receipt_valid_bundle_digest, bundle_digest) in bad_receipt
.valid_bundle_digests()
for (bad_receipt_valid_bundle_digest, bundle_digest) in bad_receipt_valid_bundle_digests
.into_iter()
.zip(valid_bundle_digests)
{
Expand Down
5 changes: 5 additions & 0 deletions domains/client/domain-operator/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ where
let bad_receipt_hashes_key = (BAD_RECEIPT_HASHES, bad_receipt_number).encode();
let mut bad_receipt_hashes: Vec<Block::Hash> =
load_decode(backend, bad_receipt_hashes_key.as_slice())?.unwrap_or_default();
// Return early if the bad ER is already tracked
if bad_receipt_hashes.contains(&bad_receipt_hash) {
return Ok(());
}

bad_receipt_hashes.push(bad_receipt_hash);

let mut to_insert = vec![
Expand Down
31 changes: 22 additions & 9 deletions domains/client/domain-operator/src/bundle_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ where
) -> sp_blockchain::Result<Option<(Block::Hash, NumberFor<Block>)>> {
let (consensus_block_hash, consensus_block_number) = consensus_block_info;
let (parent_hash, parent_number) = parent_info;
// TODO: this is used to keep compatible with the gemini-3g network, because some necessary
// runtime API are introduced in `#[api_version(2)]`, remove this before the next network
let domains_api_version = self
.consensus_client
.runtime_api()
.api_version::<dyn DomainsApi<CBlock, Block::Header>>(consensus_block_hash)?
// safe to return default version as 1 since there will always be version 1.
.unwrap_or(1);
let start = Instant::now();

tracing::debug!(
Expand Down Expand Up @@ -289,6 +297,20 @@ where
head_receipt_number,
)?;

// Check the consensus runtime version before submitting fraud proof.
if domains_api_version >= 2 {
// Even the consensus block doesn't contains bundle it may still contains
// fraud proof, thus we need to call `check_state_transition` to remove the
// bad ER info that targetted by the potential fraud proof
self.domain_receipts_checker
.check_state_transition(consensus_block_hash)?;

// Try submit fraud proof for the previous detected bad ER
self.domain_receipts_checker
.submit_fraud_proof(consensus_block_hash)
.await?;
}

return Ok(None);
};

Expand Down Expand Up @@ -354,15 +376,6 @@ where
)?;

// Check the consensus runtime version before checking bad ER and submit fraud proof.
//
// TODO: this is used to keep compatible with the gemini-3g network, because some necessary
// runtime API are introduced in `#[api_version(2)]`, remove this before the next network
let domains_api_version = self
.consensus_client
.runtime_api()
.api_version::<dyn DomainsApi<CBlock, Block::Header>>(consensus_block_hash)?
// safe to return default version as 1 since there will always be version 1.
.unwrap_or(1);
if domains_api_version >= 2 {
// TODO: Remove as ReceiptsChecker has been superseded by ReceiptValidator in block-preprocessor.
self.domain_receipts_checker
Expand Down
20 changes: 13 additions & 7 deletions domains/client/domain-operator/src/domain_block_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_domains_fraud_proof::FraudProofApi;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, One, Zero};
use sp_runtime::Digest;
use std::cmp::Ordering;
use std::collections::VecDeque;
use std::collections::{HashSet, VecDeque};
use std::sync::Arc;

struct DomainBlockBuildResult<Block>
Expand Down Expand Up @@ -781,12 +781,18 @@ where
receipts: Vec<ExecutionReceiptFor<Block, CBlock>>,
fraud_proofs: Vec<FraudProof<NumberFor<CBlock>, CBlock::Hash, Block::Header>>,
) -> Result<(), sp_blockchain::Error> {
let mut checked_receipt = HashSet::new();
let mut bad_receipts_to_write = vec![];

for execution_receipt in receipts.iter() {
let receipt_hash = execution_receipt.hash::<HeaderHashingFor<Block::Header>>();

// Skip check for genesis receipt as it is generated on the domain instantiation by
// the consensus chain.
if execution_receipt.domain_block_number.is_zero() {
if execution_receipt.domain_block_number.is_zero()
// Skip check if the same receipt is already checked
|| !checked_receipt.insert(receipt_hash)
{
continue;
}

Expand All @@ -806,7 +812,7 @@ where
{
bad_receipts_to_write.push((
execution_receipt.consensus_block_number,
execution_receipt.hash::<HeaderHashingFor<Block::Header>>(),
receipt_hash,
receipt_mismatch_info,
));

Expand All @@ -818,7 +824,7 @@ where
{
bad_receipts_to_write.push((
execution_receipt.consensus_block_number,
execution_receipt.hash::<HeaderHashingFor<Block::Header>>(),
receipt_hash,
ReceiptMismatchInfo::DomainExtrinsicsRoot {
consensus_block_hash,
},
Expand All @@ -832,7 +838,7 @@ where
) {
bad_receipts_to_write.push((
execution_receipt.consensus_block_number,
execution_receipt.hash::<HeaderHashingFor<Block::Header>>(),
receipt_hash,
(trace_mismatch_index, consensus_block_hash).into(),
));
continue;
Expand All @@ -841,7 +847,7 @@ where
if execution_receipt.total_rewards != local_receipt.total_rewards {
bad_receipts_to_write.push((
execution_receipt.consensus_block_number,
execution_receipt.hash::<HeaderHashingFor<Block::Header>>(),
receipt_hash,
ReceiptMismatchInfo::TotalRewards {
consensus_block_hash,
},
Expand All @@ -851,7 +857,7 @@ where
if execution_receipt.domain_block_hash != local_receipt.domain_block_hash {
bad_receipts_to_write.push((
execution_receipt.consensus_block_number,
execution_receipt.hash::<HeaderHashingFor<Block::Header>>(),
receipt_hash,
ReceiptMismatchInfo::DomainBlockHash {
consensus_block_hash,
},
Expand Down

0 comments on commit 7004b4b

Please sign in to comment.