Skip to content

Commit a300b14

Browse files
authored
Merge pull request #941 from subspace/core-domain-prototype-refactoring-part-2
Core domain prototype refactoring (part 2)
2 parents 6e76133 + 2c8d66b commit a300b14

23 files changed

Lines changed: 492 additions & 503 deletions

File tree

crates/pallet-domains/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ mod pallet {
492492

493493
impl<T: Config> Pallet<T> {
494494
/// Returns the block number of the latest receipt.
495-
pub fn best_execution_chain_number() -> T::BlockNumber {
495+
pub fn head_receipt_number() -> T::BlockNumber {
496496
let (_, best_number) = <ReceiptHead<T>>::get();
497497
best_number
498498
}

crates/pallet-domains/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn submit_bundle_with_many_reeipts_should_work() {
328328
assert!(!frame_system::BlockHash::<Test>::contains_key(1));
329329
assert!(!frame_system::BlockHash::<Test>::contains_key(255));
330330
assert_ok!(Domains::submit_bundle(RuntimeOrigin::none(), bundle1));
331-
assert_eq!(Domains::best_execution_chain_number(), 255);
331+
assert_eq!(Domains::head_receipt_number(), 255);
332332

333333
// Reaching the receipts pruning depth, block hash mapping will be pruned as well.
334334
assert!(BlockHash::<Test>::contains_key(0));
@@ -346,7 +346,7 @@ fn submit_bundle_with_many_reeipts_should_work() {
346346
assert!(!BlockHash::<Test>::contains_key(2));
347347
assert_eq!(OldestReceiptNumber::<Test>::get(), 3);
348348
assert_eq!(Domains::finalized_receipt_number(), 2);
349-
assert_eq!(Domains::best_execution_chain_number(), 258);
349+
assert_eq!(Domains::head_receipt_number(), 258);
350350
});
351351
}
352352

@@ -390,7 +390,7 @@ fn submit_fraud_proof_should_work() {
390390
RuntimeOrigin::none(),
391391
dummy_proof
392392
));
393-
assert_eq!(Domains::best_execution_chain_number(), 99);
393+
assert_eq!(Domains::head_receipt_number(), 99);
394394
let receipt_hash = dummy_bundles[98].clone().bundle.receipts[0].hash();
395395
assert!(Receipts::<Test>::get(receipt_hash).is_some());
396396
// Receipts for block [100, 256] should be removed as being invalid.

crates/sp-domains/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ sp_api::decl_runtime_apis! {
409409
fn execution_wasm_bundle() -> Cow<'static, [u8]>;
410410

411411
/// Returns the best execution chain number.
412-
fn best_execution_chain_number() -> NumberFor<Block>;
412+
fn head_receipt_number() -> NumberFor<Block>;
413413

414414
/// Returns the block number of oldest execution receipt.
415415
fn oldest_receipt_number() -> NumberFor<Block>;

crates/subspace-runtime/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,8 @@ impl_runtime_apis! {
863863
EXECUTION_WASM_BUNDLE.into()
864864
}
865865

866-
fn best_execution_chain_number() -> NumberFor<Block> {
867-
Domains::best_execution_chain_number()
866+
fn head_receipt_number() -> NumberFor<Block> {
867+
Domains::head_receipt_number()
868868
}
869869

870870
fn oldest_receipt_number() -> NumberFor<Block> {

domains/client/domain-executor/src/aux_schema.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn load_decode<Backend: AuxStore, T: Decode>(
5555
pub(super) fn write_execution_receipt<Backend: AuxStore, Block: BlockT, PBlock: BlockT>(
5656
backend: &Backend,
5757
(block_hash, block_number): (Block::Hash, NumberFor<Block>),
58-
best_execution_chain_number: NumberFor<Block>,
58+
head_receipt_number: NumberFor<Block>,
5959
execution_receipt: &ExecutionReceipt<NumberFor<PBlock>, PBlock::Hash, Block::Hash>,
6060
) -> Result<(), sp_blockchain::Error> {
6161
let block_number_key = (EXECUTION_RECEIPT_BLOCK_NUMBER, block_number).encode();
@@ -71,7 +71,7 @@ pub(super) fn write_execution_receipt<Backend: AuxStore, Block: BlockT, PBlock:
7171

7272
let mut keys_to_delete = vec![];
7373

74-
if let Some(delete_receipts_to) = best_execution_chain_number
74+
if let Some(delete_receipts_to) = head_receipt_number
7575
.saturated_into::<BlockNumber>()
7676
.checked_sub(PRUNING_DEPTH)
7777
{
@@ -130,10 +130,10 @@ where
130130
}
131131

132132
pub(super) fn target_receipt_is_pruned(
133-
best_execution_chain_number: BlockNumber,
133+
head_receipt_number: BlockNumber,
134134
target_block: BlockNumber,
135135
) -> bool {
136-
best_execution_chain_number.saturating_sub(target_block) >= PRUNING_DEPTH
136+
head_receipt_number.saturating_sub(target_block) >= PRUNING_DEPTH
137137
}
138138

139139
/// Writes a bad execution receipt to aux storage.
@@ -464,7 +464,7 @@ mod tests {
464464

465465
assert!(!target_receipt_is_pruned(PRUNING_DEPTH, 1));
466466

467-
// Create PRUNING_DEPTH + 1 receipt, best_execution_chain_number is PRUNING_DEPTH.
467+
// Create PRUNING_DEPTH + 1 receipt, head_receipt_number is PRUNING_DEPTH.
468468
let block_hash = Hash::random();
469469
assert!(receipt_at(block_hash).is_none());
470470
write_receipt_at(
@@ -474,7 +474,7 @@ mod tests {
474474
);
475475
assert!(receipt_at(block_hash).is_some());
476476

477-
// Create PRUNING_DEPTH + 2 receipt, best_execution_chain_number is PRUNING_DEPTH + 1.
477+
// Create PRUNING_DEPTH + 2 receipt, head_receipt_number is PRUNING_DEPTH + 1.
478478
let block_hash = Hash::random();
479479
write_receipt_at(
480480
block_hash,
@@ -490,7 +490,7 @@ mod tests {
490490
assert!(target_receipt_is_pruned(PRUNING_DEPTH + 1, 1));
491491
assert_eq!(receipt_start(), Some(2));
492492

493-
// Create PRUNING_DEPTH + 3 receipt, best_execution_chain_number is PRUNING_DEPTH + 2.
493+
// Create PRUNING_DEPTH + 3 receipt, head_receipt_number is PRUNING_DEPTH + 2.
494494
let block_hash = Hash::random();
495495
write_receipt_at(
496496
block_hash,
@@ -519,7 +519,7 @@ mod tests {
519519
}
520520

521521
#[test]
522-
fn execution_receipts_should_be_kept_against_best_execution_chain_number() {
522+
fn execution_receipts_should_be_kept_against_head_receipt_number() {
523523
let client = substrate_test_runtime_client::new();
524524

525525
let receipt_start = || {
@@ -538,20 +538,20 @@ mod tests {
538538
let receipt_at = |block_hash: Hash| load_execution_receipt(&client, block_hash).unwrap();
539539

540540
let write_receipt_at = |(hash, number): (Hash, BlockNumber),
541-
best_execution_chain_number: BlockNumber,
541+
head_receipt_number: BlockNumber,
542542
receipt: &ExecutionReceipt| {
543543
write_execution_receipt::<_, Block, PBlock>(
544544
&client,
545545
(hash, number),
546-
best_execution_chain_number,
546+
head_receipt_number,
547547
receipt,
548548
)
549549
.unwrap()
550550
};
551551

552552
assert_eq!(receipt_start(), None);
553553

554-
// Create PRUNING_DEPTH receipts, best_execution_chain_number is 0, i.e., no receipt
554+
// Create PRUNING_DEPTH receipts, head_receipt_number is 0, i.e., no receipt
555555
// has ever been included on primary chain.
556556
let block_hash_list = (1..=PRUNING_DEPTH)
557557
.map(|block_number| {
@@ -567,7 +567,7 @@ mod tests {
567567

568568
assert!(!target_receipt_is_pruned(PRUNING_DEPTH, 1));
569569

570-
// Create PRUNING_DEPTH + 1 receipt, best_execution_chain_number is 0.
570+
// Create PRUNING_DEPTH + 1 receipt, head_receipt_number is 0.
571571
let block_hash = Hash::random();
572572
assert!(receipt_at(block_hash).is_none());
573573
write_receipt_at(
@@ -576,7 +576,7 @@ mod tests {
576576
&create_execution_receipt(PRUNING_DEPTH + 1),
577577
);
578578

579-
// Create PRUNING_DEPTH + 2 receipt, best_execution_chain_number is 0.
579+
// Create PRUNING_DEPTH + 2 receipt, head_receipt_number is 0.
580580
let block_hash = Hash::random();
581581
write_receipt_at(
582582
(block_hash, PRUNING_DEPTH + 2),
@@ -591,15 +591,15 @@ mod tests {
591591
assert!(!target_receipt_is_pruned(0, 1));
592592
assert_eq!(receipt_start(), Some(1));
593593

594-
// Create PRUNING_DEPTH + 3 receipt, best_execution_chain_number is 0.
594+
// Create PRUNING_DEPTH + 3 receipt, head_receipt_number is 0.
595595
let block_hash = Hash::random();
596596
write_receipt_at(
597597
(block_hash, PRUNING_DEPTH + 3),
598598
0,
599599
&create_execution_receipt(PRUNING_DEPTH + 3),
600600
);
601601

602-
// Create PRUNING_DEPTH + 4 receipt, best_execution_chain_number is PRUNING_DEPTH + 3.
602+
// Create PRUNING_DEPTH + 4 receipt, head_receipt_number is PRUNING_DEPTH + 3.
603603
let block_hash = Hash::random();
604604
write_receipt_at(
605605
(block_hash, PRUNING_DEPTH + 4),

domains/client/domain-executor/src/bundle_election_solver.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,56 +16,56 @@ use std::sync::Arc;
1616
use subspace_core_primitives::{Blake2b256Hash, BlockNumber};
1717
use system_runtime_primitives::SystemDomainApi;
1818

19-
pub(super) struct BundleElectionSolver<Block, PBlock, Client> {
20-
client: Arc<Client>,
19+
pub(super) struct BundleElectionSolver<SBlock, PBlock, SClient> {
20+
system_domain_client: Arc<SClient>,
2121
keystore: SyncCryptoStorePtr,
22-
_phantom_data: PhantomData<(Block, PBlock)>,
22+
_phantom_data: PhantomData<(SBlock, PBlock)>,
2323
}
2424

25-
impl<Block, PBlock, Client> Clone for BundleElectionSolver<Block, PBlock, Client>
25+
impl<SBlock, PBlock, SClient> Clone for BundleElectionSolver<SBlock, PBlock, SClient>
2626
where
27-
Block: BlockT,
27+
SBlock: BlockT,
2828
PBlock: BlockT,
2929
{
3030
fn clone(&self) -> Self {
3131
Self {
32-
client: self.client.clone(),
32+
system_domain_client: self.system_domain_client.clone(),
3333
keystore: self.keystore.clone(),
3434
_phantom_data: self._phantom_data,
3535
}
3636
}
3737
}
3838

39-
impl<Block, PBlock, Client> BundleElectionSolver<Block, PBlock, Client>
39+
impl<SBlock, PBlock, SClient> BundleElectionSolver<SBlock, PBlock, SClient>
4040
where
41-
Block: BlockT,
41+
SBlock: BlockT,
4242
PBlock: BlockT,
43-
Client: HeaderBackend<Block> + ProvideRuntimeApi<Block> + ProofProvider<Block>,
44-
Client::Api: SystemDomainApi<Block, NumberFor<PBlock>, PBlock::Hash>,
43+
SClient: HeaderBackend<SBlock> + ProvideRuntimeApi<SBlock> + ProofProvider<SBlock>,
44+
SClient::Api: SystemDomainApi<SBlock, NumberFor<PBlock>, PBlock::Hash>,
4545
{
46-
pub(super) fn new(client: Arc<Client>, keystore: SyncCryptoStorePtr) -> Self {
46+
pub(super) fn new(system_domain_client: Arc<SClient>, keystore: SyncCryptoStorePtr) -> Self {
4747
Self {
48-
client,
48+
system_domain_client,
4949
keystore,
5050
_phantom_data: PhantomData::default(),
5151
}
5252
}
5353

5454
pub(super) fn solve_bundle_election_challenge(
5555
&self,
56-
best_hash: Block::Hash,
57-
best_number: NumberFor<Block>,
56+
best_hash: SBlock::Hash,
57+
best_number: NumberFor<SBlock>,
5858
domain_id: DomainId,
5959
global_challenge: Blake2b256Hash,
60-
) -> sp_blockchain::Result<Option<ProofOfElection<Block::Hash>>> {
60+
) -> sp_blockchain::Result<Option<ProofOfElection<SBlock::Hash>>> {
6161
let best_block_id = BlockId::Hash(best_hash);
6262

6363
let BundleElectionParams {
6464
authorities,
6565
total_stake_weight,
6666
slot_probability,
6767
} = self
68-
.client
68+
.system_domain_client
6969
.runtime_api()
7070
.bundle_elections_params(&best_block_id, domain_id)?;
7171

@@ -110,11 +110,11 @@ where
110110
// electioned executor storage instead of the whole authority set.
111111
let storage_proof = if domain_id.is_system() {
112112
let storage_keys = well_known_keys::bundle_election_storage_keys(domain_id);
113-
self.client
113+
self.system_domain_client
114114
.read_proof(best_hash, &mut storage_keys.iter().map(|s| s.as_slice()))?
115115
} else if domain_id.is_core() {
116116
let storage_keys = self
117-
.client
117+
.system_domain_client
118118
.runtime_api()
119119
.core_bundle_election_storage_keys(
120120
&best_block_id,
@@ -126,7 +126,7 @@ where
126126
"Empty core bundle election storage keys".to_string(),
127127
)
128128
})?;
129-
self.client
129+
self.system_domain_client
130130
.read_proof(best_hash, &mut storage_keys.iter().map(|s| s.as_slice()))?
131131
} else {
132132
return Err(sp_blockchain::Error::Application(Box::from(
@@ -135,7 +135,7 @@ where
135135
};
136136

137137
let state_root = *self
138-
.client
138+
.system_domain_client
139139
.header(best_block_id)?
140140
.expect("Best block header must exist; qed")
141141
.state_root();

domains/client/domain-executor/src/core_bundle_processor.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,15 @@ where
165165
// TODO: just make it compile for now, likely wrong, rethink about it.
166166
let system_domain_hash = self.system_domain_client.info().best_hash;
167167

168-
let best_execution_chain_number = self
168+
let head_receipt_number = self
169169
.system_domain_client
170170
.runtime_api()
171-
.best_execution_chain_number(&BlockId::Hash(system_domain_hash), self.domain_id)?;
172-
let best_execution_chain_number = translate_number_type::<
173-
NumberFor<SBlock>,
174-
NumberFor<Block>,
175-
>(best_execution_chain_number);
171+
.head_receipt_number(&BlockId::Hash(system_domain_hash), self.domain_id)?;
172+
let head_receipt_number =
173+
translate_number_type::<NumberFor<SBlock>, NumberFor<Block>>(head_receipt_number);
176174

177175
assert!(
178-
domain_block_result.header_number > best_execution_chain_number,
176+
domain_block_result.header_number > head_receipt_number,
179177
"Consensus chain number must larger than execution chain number by at least 1"
180178
);
181179

@@ -189,7 +187,7 @@ where
189187
if let Some(fraud_proof) = self.domain_block_processor.on_domain_block_processed(
190188
primary_hash,
191189
domain_block_result,
192-
best_execution_chain_number,
190+
head_receipt_number,
193191
oldest_receipt_number,
194192
)? {
195193
// TODO: self.system_domain_client.runtime_api().submit_fraud_proof_unsigned()

0 commit comments

Comments
 (0)