|
| 1 | +use crate::{Block, BlockNumber, Hash, RuntimeCall, UncheckedExtrinsic}; |
| 2 | +use sp_consensus_subspace::digests::CompatibleDigestItem; |
| 3 | +use sp_consensus_subspace::FarmerPublicKey; |
| 4 | +use sp_domains::fraud_proof::FraudProof; |
| 5 | +use sp_domains::transaction::PreValidationObject; |
| 6 | +use sp_domains::{DomainId, ExecutionReceipt}; |
| 7 | +use sp_runtime::traits::{BlakeTwo256, Block as BlockT, Hash as HashT, Header as HeaderT, Zero}; |
| 8 | +use sp_std::vec::Vec; |
| 9 | +use subspace_core_primitives::{PublicKey, Randomness}; |
| 10 | +use subspace_verification::derive_randomness; |
| 11 | + |
| 12 | +pub(crate) fn extract_system_bundles( |
| 13 | + extrinsics: Vec<UncheckedExtrinsic>, |
| 14 | +) -> ( |
| 15 | + sp_domains::OpaqueBundles<Block, domain_runtime_primitives::Hash>, |
| 16 | + sp_domains::SignedOpaqueBundles<Block, domain_runtime_primitives::Hash>, |
| 17 | +) { |
| 18 | + let (system_bundles, core_bundles): (Vec<_>, Vec<_>) = extrinsics |
| 19 | + .into_iter() |
| 20 | + .filter_map(|uxt| { |
| 21 | + if let RuntimeCall::Domains(pallet_domains::Call::submit_bundle { |
| 22 | + signed_opaque_bundle, |
| 23 | + }) = uxt.function |
| 24 | + { |
| 25 | + if signed_opaque_bundle.domain_id().is_system() { |
| 26 | + Some((Some(signed_opaque_bundle.bundle), None)) |
| 27 | + } else { |
| 28 | + Some((None, Some(signed_opaque_bundle))) |
| 29 | + } |
| 30 | + } else { |
| 31 | + None |
| 32 | + } |
| 33 | + }) |
| 34 | + .unzip(); |
| 35 | + ( |
| 36 | + system_bundles.into_iter().flatten().collect(), |
| 37 | + core_bundles.into_iter().flatten().collect(), |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +pub(crate) fn extract_core_bundles( |
| 42 | + extrinsics: Vec<UncheckedExtrinsic>, |
| 43 | + domain_id: DomainId, |
| 44 | +) -> sp_domains::OpaqueBundles<Block, domain_runtime_primitives::Hash> { |
| 45 | + extrinsics |
| 46 | + .into_iter() |
| 47 | + .filter_map(|uxt| match uxt.function { |
| 48 | + RuntimeCall::Domains(pallet_domains::Call::submit_bundle { |
| 49 | + signed_opaque_bundle, |
| 50 | + }) if signed_opaque_bundle.domain_id() == domain_id => { |
| 51 | + Some(signed_opaque_bundle.bundle) |
| 52 | + } |
| 53 | + _ => None, |
| 54 | + }) |
| 55 | + .collect() |
| 56 | +} |
| 57 | + |
| 58 | +pub(crate) fn extract_receipts( |
| 59 | + extrinsics: Vec<UncheckedExtrinsic>, |
| 60 | + domain_id: DomainId, |
| 61 | +) -> Vec<ExecutionReceipt<BlockNumber, Hash, domain_runtime_primitives::Hash>> { |
| 62 | + extrinsics |
| 63 | + .into_iter() |
| 64 | + .filter_map(|uxt| match uxt.function { |
| 65 | + RuntimeCall::Domains(pallet_domains::Call::submit_bundle { |
| 66 | + signed_opaque_bundle, |
| 67 | + }) if signed_opaque_bundle.domain_id() == domain_id => { |
| 68 | + Some(signed_opaque_bundle.bundle.receipts) |
| 69 | + } |
| 70 | + _ => None, |
| 71 | + }) |
| 72 | + .flatten() |
| 73 | + .collect() |
| 74 | +} |
| 75 | + |
| 76 | +pub(crate) fn extract_fraud_proofs(extrinsics: Vec<UncheckedExtrinsic>) -> Vec<FraudProof> { |
| 77 | + extrinsics |
| 78 | + .into_iter() |
| 79 | + .filter_map(|uxt| { |
| 80 | + if let RuntimeCall::Domains(pallet_domains::Call::submit_fraud_proof { fraud_proof }) = |
| 81 | + uxt.function |
| 82 | + { |
| 83 | + Some(fraud_proof) |
| 84 | + } else { |
| 85 | + None |
| 86 | + } |
| 87 | + }) |
| 88 | + .collect() |
| 89 | +} |
| 90 | + |
| 91 | +pub(crate) fn extract_pre_validation_object( |
| 92 | + extrinsic: UncheckedExtrinsic, |
| 93 | +) -> PreValidationObject<Block, domain_runtime_primitives::Hash> { |
| 94 | + match extrinsic.function { |
| 95 | + RuntimeCall::Domains(pallet_domains::Call::submit_fraud_proof { fraud_proof }) => { |
| 96 | + PreValidationObject::FraudProof(fraud_proof) |
| 97 | + } |
| 98 | + RuntimeCall::Domains(pallet_domains::Call::submit_bundle { |
| 99 | + signed_opaque_bundle, |
| 100 | + }) => PreValidationObject::Receipts(signed_opaque_bundle.bundle.receipts), |
| 101 | + _ => PreValidationObject::Null, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +pub(crate) fn extrinsics_shuffling_seed<Block: BlockT>(header: Block::Header) -> Randomness { |
| 106 | + if header.number().is_zero() { |
| 107 | + Randomness::default() |
| 108 | + } else { |
| 109 | + let mut pre_digest: Option<_> = None; |
| 110 | + for log in header.digest().logs() { |
| 111 | + match ( |
| 112 | + log.as_subspace_pre_digest::<FarmerPublicKey>(), |
| 113 | + pre_digest.is_some(), |
| 114 | + ) { |
| 115 | + (Some(_), true) => panic!("Multiple Subspace pre-runtime digests in a header"), |
| 116 | + (None, _) => {} |
| 117 | + (s, false) => pre_digest = s, |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + let pre_digest = pre_digest.expect("Header must contain one pre-runtime digest; qed"); |
| 122 | + |
| 123 | + let seed: &[u8] = b"extrinsics-shuffling-seed"; |
| 124 | + let randomness = derive_randomness( |
| 125 | + &Into::<PublicKey>::into(&pre_digest.solution.public_key), |
| 126 | + &pre_digest.solution.chunk.to_bytes(), |
| 127 | + &pre_digest.solution.chunk_signature, |
| 128 | + ) |
| 129 | + .expect("Tag signature is verified by the client and must always be valid; qed"); |
| 130 | + let mut data = Vec::with_capacity(seed.len() + randomness.len()); |
| 131 | + data.extend_from_slice(seed); |
| 132 | + data.extend_from_slice(&randomness); |
| 133 | + |
| 134 | + BlakeTwo256::hash_of(&data).into() |
| 135 | + } |
| 136 | +} |
0 commit comments