Skip to content

Commit

Permalink
Several minor refactorings
Browse files Browse the repository at this point in the history
No logical changes.

- Make `verify_vrf_proof` a method of ProofOfElection.
- Remove resolved TODO in {core,system}_gossip_message_validator.
- Rename `extract_stored_bundles_at` to `successfully_submitted_bundles_at`
  • Loading branch information
liuchengxu committed May 15, 2023
1 parent 89b9272 commit ceab1f8
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 25 deletions.
12 changes: 4 additions & 8 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use frame_support::traits::Get;
use frame_system::offchain::SubmitTransaction;
pub use pallet::*;
use sp_core::H256;
use sp_domains::bundle_election::{verify_system_bundle_solution, verify_vrf_proof};
use sp_domains::bundle_election::verify_system_bundle_solution;
use sp_domains::fraud_proof::FraudProof;
use sp_domains::merkle_tree::Witness;
use sp_domains::transaction::InvalidTransactionCode;
Expand Down Expand Up @@ -539,13 +539,9 @@ impl<T: Config> Pallet<T> {
return Err(BundleError::BadSignature);
}

verify_vrf_proof(
&proof_of_election.executor_public_key,
&proof_of_election.vrf_output,
&proof_of_election.vrf_proof,
&proof_of_election.global_challenge,
)
.map_err(|_| BundleError::BadVrfProof)?;
proof_of_election
.verify_vrf_proof()
.map_err(|_| BundleError::BadVrfProof)?;

Self::validate_execution_receipts(&bundle.receipts).map_err(BundleError::Receipt)?;

Expand Down
2 changes: 1 addition & 1 deletion crates/sp-domains/src/bundle_election.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub enum VrfProofError {
}

/// Verify the vrf proof generated in the bundle election.
pub fn verify_vrf_proof(
pub(crate) fn verify_vrf_proof(
public_key: &ExecutorPublicKey,
vrf_output: &[u8],
vrf_proof: &[u8],
Expand Down
16 changes: 14 additions & 2 deletions crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod merkle_tree;
pub mod transaction;

use crate::fraud_proof::FraudProof;
use bundle_election::VrfProofError;
use merkle_tree::Witness;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
Expand Down Expand Up @@ -217,6 +218,17 @@ pub struct ProofOfElection<DomainHash> {
pub system_block_hash: DomainHash,
}

impl<DomainHash> ProofOfElection<DomainHash> {
pub fn verify_vrf_proof(&self) -> Result<(), VrfProofError> {
bundle_election::verify_vrf_proof(
&self.executor_public_key,
&self.vrf_output,
&self.vrf_proof,
&self.global_challenge,
)
}
}

impl<DomainHash: Default> ProofOfElection<DomainHash> {
#[cfg(feature = "std")]
pub fn dummy(domain_id: DomainId, executor_public_key: ExecutorPublicKey) -> Self {
Expand Down Expand Up @@ -440,8 +452,8 @@ sp_api::decl_runtime_apis! {
domain_id: DomainId,
) -> OpaqueBundles<Block, DomainHash>;

/// Extract the hashes of bundles stored in the block
fn extract_stored_bundle_hashes() -> Vec<H256>;
/// Returns the hash of successfully submitted bundles.
fn successful_bundle_hashes() -> Vec<H256>;

/// Extract the receipts from the given extrinsics.
fn extract_receipts(
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ impl_runtime_apis! {
crate::domains::extract_core_bundles(extrinsics, domain_id)
}

fn extract_stored_bundle_hashes() -> Vec<H256> {
fn successful_bundle_hashes() -> Vec<H256> {
Domains::successful_bundles()
}

Expand Down
8 changes: 4 additions & 4 deletions crates/subspace-transaction-pool/src/bundle_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ where
}
}

fn extract_stored_bundles_at(
fn successfully_submitted_bundles_at(
&self,
block_hash: Block::Hash,
) -> sp_blockchain::Result<HashSet<Hash>> {
let bundle_hashes: HashSet<_> = self
.client
.runtime_api()
.extract_stored_bundle_hashes(block_hash)?
.successful_bundle_hashes(block_hash)?
.into_iter()
.collect();
Ok(bundle_hashes)
Expand Down Expand Up @@ -96,7 +96,7 @@ where
}
}
for (hash, number) in blocks {
let bundles = self.extract_stored_bundles_at(hash)?;
let bundles = self.successfully_submitted_bundles_at(hash)?;
bundle_stored_in_last_k.push_front(BlockBundle::new(hash, number, bundles));
}
Ok(())
Expand Down Expand Up @@ -142,7 +142,7 @@ where

// Add bundles from the new block of the best fork
for enacted_block in enacted {
let bundles = self.extract_stored_bundles_at(enacted_block.hash)?;
let bundles = self.successfully_submitted_bundles_at(enacted_block.hash)?;
bundle_stored_in_last_k.push_front(BlockBundle::new(
enacted_block.hash,
enacted_block.number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,18 @@ where
if bundle_exists {
Ok(Action::Empty)
} else {
let executor_public_key = &bundle_solution.proof_of_election().executor_public_key;
let proof_of_election = bundle_solution.proof_of_election();

if !executor_public_key.verify(&bundle.hash(), signature) {
if !proof_of_election
.executor_public_key
.verify(&bundle.hash(), signature)
{
return Err(Self::Error::BadBundleSignature);
}

// TODO: validate the bundle election.

// TODO: Validate the receipts correctly when the bundle gossip is re-enabled.
let domain_id = bundle_solution.proof_of_election().domain_id;
let domain_id = proof_of_election.domain_id;

self.gossip_message_validator
.validate_bundle_receipts(&bundle.receipts, domain_id)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,18 @@ where
if bundle_exists {
Ok(Action::Empty)
} else {
let executor_public_key = &bundle_solution.proof_of_election().executor_public_key;
let proof_of_election = bundle_solution.proof_of_election();

if !executor_public_key.verify(&bundle.hash(), signature) {
if !proof_of_election
.executor_public_key
.verify(&bundle.hash(), signature)
{
return Err(GossipMessageError::BadBundleSignature);
}

// TODO: validate the bundle election.

// TODO: Validate the receipts correctly when the bundle gossip is re-enabled.
let domain_id = bundle_solution.proof_of_election().domain_id;
let domain_id = proof_of_election.domain_id;

self.gossip_message_validator
.validate_bundle_receipts(&bundle.receipts, domain_id)?;
Expand Down
2 changes: 1 addition & 1 deletion test/subspace-test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ impl_runtime_apis! {
extract_core_bundles(extrinsics, domain_id)
}

fn extract_stored_bundle_hashes() -> Vec<H256> {
fn successful_bundle_hashes() -> Vec<H256> {
Domains::successful_bundles()
}

Expand Down

0 comments on commit ceab1f8

Please sign in to comment.