Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit e127fd6

Browse files
authored
Merge pull request paritytech#286 from subspace/prepare-fraud-proof
Prepare the fraud proof implementation
2 parents 217b3e4 + 7c524dc commit e127fd6

File tree

10 files changed

+133
-59
lines changed

10 files changed

+133
-59
lines changed

cumulus/client/cirrus-executor/src/bundler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use subspace_runtime_primitives::Hash as PHash;
1717

1818
use super::{Executor, LOG_TARGET};
1919

20-
impl<Block, Client, TransactionPool, Backend, CIDP>
21-
Executor<Block, Client, TransactionPool, Backend, CIDP>
20+
impl<Block, Client, TransactionPool, Backend, CIDP, E>
21+
Executor<Block, Client, TransactionPool, Backend, CIDP, E>
2222
where
2323
Block: BlockT,
2424
Client: sp_blockchain::HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block>,

cumulus/client/cirrus-executor/src/lib.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use sc_utils::mpsc::TracingUnboundedSender;
2929
use sp_api::ProvideRuntimeApi;
3030
use sp_blockchain::HeaderBackend;
3131
use sp_consensus::BlockStatus;
32-
use sp_core::traits::SpawnNamed;
32+
use sp_core::traits::{CodeExecutor, SpawnNamed};
3333
use sp_inherents::CreateInherentDataProviders;
3434
use sp_runtime::{
3535
generic::BlockId,
@@ -62,22 +62,23 @@ use tracing::Instrument;
6262
const LOG_TARGET: &str = "cirrus::executor";
6363

6464
/// The implementation of the Cirrus `Executor`.
65-
pub struct Executor<Block: BlockT, Client, TransactionPool, Backend, CIDP> {
65+
pub struct Executor<Block: BlockT, Client, TransactionPool, Backend, CIDP, E> {
6666
// TODO: no longer used in executor, revisit this with ParachainBlockImport together.
6767
parachain_consensus: Box<dyn ParachainConsensus<Block>>,
6868
client: Arc<Client>,
69-
spawner: Arc<dyn SpawnNamed + Send + Sync>,
69+
spawner: Box<dyn SpawnNamed + Send + Sync>,
7070
overseer_handle: OverseerHandle,
7171
transaction_pool: Arc<TransactionPool>,
7272
bundle_sender: Arc<TracingUnboundedSender<Bundle<Block::Extrinsic>>>,
7373
execution_receipt_sender: Arc<TracingUnboundedSender<ExecutionReceipt<Block::Hash>>>,
7474
backend: Arc<Backend>,
7575
create_inherent_data_providers: Arc<CIDP>,
76+
code_executor: Arc<E>,
7677
is_authority: bool,
7778
}
7879

79-
impl<Block: BlockT, Client, TransactionPool, Backend, CIDP> Clone
80-
for Executor<Block, Client, TransactionPool, Backend, CIDP>
80+
impl<Block: BlockT, Client, TransactionPool, Backend, CIDP, E> Clone
81+
for Executor<Block, Client, TransactionPool, Backend, CIDP, E>
8182
{
8283
fn clone(&self) -> Self {
8384
Self {
@@ -90,13 +91,14 @@ impl<Block: BlockT, Client, TransactionPool, Backend, CIDP> Clone
9091
execution_receipt_sender: self.execution_receipt_sender.clone(),
9192
backend: self.backend.clone(),
9293
create_inherent_data_providers: self.create_inherent_data_providers.clone(),
94+
code_executor: self.code_executor.clone(),
9395
is_authority: self.is_authority,
9496
}
9597
}
9698
}
9799

98-
impl<Block, Client, TransactionPool, Backend, CIDP>
99-
Executor<Block, Client, TransactionPool, Backend, CIDP>
100+
impl<Block, Client, TransactionPool, Backend, CIDP, E>
101+
Executor<Block, Client, TransactionPool, Backend, CIDP, E>
100102
where
101103
Block: BlockT,
102104
Client: HeaderBackend<Block> + BlockBackend<Block> + AuxStore + ProvideRuntimeApi<Block>,
@@ -114,18 +116,20 @@ where
114116
Backend: sc_client_api::Backend<Block> + Send + Sync + 'static,
115117
TransactionPool: sc_transaction_pool_api::TransactionPool<Block = Block> + 'static,
116118
CIDP: CreateInherentDataProviders<Block, Hash> + 'static,
119+
E: CodeExecutor,
117120
{
118121
/// Create a new instance.
119122
fn new(
120123
parachain_consensus: Box<dyn ParachainConsensus<Block>>,
121124
client: Arc<Client>,
122-
spawner: Arc<dyn SpawnNamed + Send + Sync>,
125+
spawner: Box<dyn SpawnNamed + Send + Sync>,
123126
overseer_handle: OverseerHandle,
124127
transaction_pool: Arc<TransactionPool>,
125128
bundle_sender: Arc<TracingUnboundedSender<Bundle<Block::Extrinsic>>>,
126129
execution_receipt_sender: Arc<TracingUnboundedSender<ExecutionReceipt<Block::Hash>>>,
127130
backend: Arc<Backend>,
128131
create_inherent_data_providers: Arc<CIDP>,
132+
code_executor: Arc<E>,
129133
is_authority: bool,
130134
) -> Self {
131135
Self {
@@ -138,6 +142,7 @@ where
138142
execution_receipt_sender,
139143
backend,
140144
create_inherent_data_providers,
145+
code_executor,
141146
is_authority,
142147
}
143148
}
@@ -364,8 +369,8 @@ pub enum GossipMessageError {
364369
SendError,
365370
}
366371

367-
impl<Block, Client, TransactionPool, Backend, CIDP> GossipMessageHandler<Block>
368-
for Executor<Block, Client, TransactionPool, Backend, CIDP>
372+
impl<Block, Client, TransactionPool, Backend, CIDP, E> GossipMessageHandler<Block>
373+
for Executor<Block, Client, TransactionPool, Backend, CIDP, E>
369374
where
370375
Block: BlockT,
371376
Client: HeaderBackend<Block>
@@ -389,6 +394,7 @@ where
389394
Backend: sc_client_api::Backend<Block> + Send + Sync + 'static,
390395
TransactionPool: sc_transaction_pool_api::TransactionPool<Block = Block> + 'static,
391396
CIDP: CreateInherentDataProviders<Block, Hash> + 'static,
397+
E: CodeExecutor,
392398
{
393399
type Error = GossipMessageError;
394400

@@ -525,22 +531,23 @@ where
525531
}
526532

527533
/// Parameters for [`start_executor`].
528-
pub struct StartExecutorParams<Block: BlockT, Spawner, Client, TransactionPool, Backend, CIDP> {
534+
pub struct StartExecutorParams<Block: BlockT, Spawner, Client, TransactionPool, Backend, CIDP, E> {
529535
pub client: Arc<Client>,
530536
pub announce_block: Arc<dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync>,
531537
pub overseer_handle: OverseerHandle,
532-
pub spawner: Spawner,
538+
pub spawner: Box<Spawner>,
533539
pub parachain_consensus: Box<dyn ParachainConsensus<Block>>,
534540
pub transaction_pool: Arc<TransactionPool>,
535541
pub bundle_sender: TracingUnboundedSender<Bundle<Block::Extrinsic>>,
536542
pub execution_receipt_sender: TracingUnboundedSender<ExecutionReceipt<Block::Hash>>,
537543
pub backend: Arc<Backend>,
538544
pub create_inherent_data_providers: Arc<CIDP>,
545+
pub code_executor: Arc<E>,
539546
pub is_authority: bool,
540547
}
541548

542549
/// Start the executor.
543-
pub async fn start_executor<Block, Spawner, Client, TransactionPool, Backend, CIDP>(
550+
pub async fn start_executor<Block, Spawner, Client, TransactionPool, Backend, CIDP, E>(
544551
StartExecutorParams {
545552
client,
546553
announce_block: _,
@@ -552,9 +559,10 @@ pub async fn start_executor<Block, Spawner, Client, TransactionPool, Backend, CI
552559
execution_receipt_sender,
553560
backend,
554561
create_inherent_data_providers,
562+
code_executor,
555563
is_authority,
556-
}: StartExecutorParams<Block, Spawner, Client, TransactionPool, Backend, CIDP>,
557-
) -> Executor<Block, Client, TransactionPool, Backend, CIDP>
564+
}: StartExecutorParams<Block, Spawner, Client, TransactionPool, Backend, CIDP, E>,
565+
) -> Executor<Block, Client, TransactionPool, Backend, CIDP, E>
558566
where
559567
Block: BlockT,
560568
Backend: sc_client_api::Backend<Block> + Send + Sync + 'static,
@@ -580,17 +588,19 @@ where
580588
TransactionPool:
581589
sc_transaction_pool_api::TransactionPool<Block = Block> + Send + Sync + 'static,
582590
CIDP: CreateInherentDataProviders<Block, Hash> + 'static,
591+
E: CodeExecutor,
583592
{
584593
let executor = Executor::new(
585594
parachain_consensus,
586595
client,
587-
Arc::new(spawner),
596+
spawner,
588597
overseer_handle.clone(),
589598
transaction_pool,
590599
Arc::new(bundle_sender),
591600
Arc::new(execution_receipt_sender),
592601
backend,
593602
create_inherent_data_providers,
603+
code_executor,
594604
is_authority,
595605
);
596606

cumulus/client/cirrus-executor/src/processor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ fn shuffle_extrinsics<Extrinsic: Debug>(
6969
shuffled_extrinsics
7070
}
7171

72-
impl<Block, Client, TransactionPool, Backend, CIDP>
73-
Executor<Block, Client, TransactionPool, Backend, CIDP>
72+
impl<Block, Client, TransactionPool, Backend, CIDP, E>
73+
Executor<Block, Client, TransactionPool, Backend, CIDP, E>
7474
where
7575
Block: BlockT,
7676
Client: sp_blockchain::HeaderBackend<Block>

cumulus/client/cirrus-service/src/lib.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use sc_utils::mpsc::tracing_unbounded;
3636
use sp_api::ProvideRuntimeApi;
3737
use sp_blockchain::HeaderBackend;
3838
use sp_consensus::BlockOrigin;
39-
use sp_core::traits::SpawnNamed;
39+
use sp_core::traits::{CodeExecutor, SpawnNamed};
4040
use sp_inherents::CreateInherentDataProviders;
4141
use sp_runtime::{
4242
traits::{Block as BlockT, NumberFor},
@@ -49,10 +49,21 @@ use cumulus_client_consensus_common::RelaychainClient;
4949
pub mod genesis;
5050

5151
/// Parameters given to [`start_executor`].
52-
pub struct StartExecutorParams<'a, Block: BlockT, Client, Spawner, RClient, IQ, TP, Backend, CIDP> {
52+
pub struct StartExecutorParams<
53+
'a,
54+
Block: BlockT,
55+
Client,
56+
Spawner,
57+
RClient,
58+
IQ,
59+
TP,
60+
Backend,
61+
CIDP,
62+
E,
63+
> {
5364
pub client: Arc<Client>,
5465
pub announce_block: Arc<dyn Fn(Block::Hash, Option<Vec<u8>>) + Send + Sync>,
55-
pub spawner: Spawner,
66+
pub spawner: Box<Spawner>,
5667
pub primary_chain_full_node: subspace_service::NewFull<RClient>,
5768
pub task_manager: &'a mut TaskManager,
5869
pub parachain_consensus: Box<dyn ParachainConsensus<Block>>,
@@ -61,11 +72,12 @@ pub struct StartExecutorParams<'a, Block: BlockT, Client, Spawner, RClient, IQ,
6172
pub network: Arc<NetworkService<Block, Block::Hash>>,
6273
pub backend: Arc<Backend>,
6374
pub create_inherent_data_providers: Arc<CIDP>,
75+
pub code_executor: Arc<E>,
6476
pub is_authority: bool,
6577
}
6678

6779
/// Start an executor node.
68-
pub async fn start_executor<'a, Block, Client, Backend, Spawner, RClient, IQ, TP, CIDP>(
80+
pub async fn start_executor<'a, Block, Client, Backend, Spawner, RClient, IQ, TP, CIDP, E>(
6981
StartExecutorParams {
7082
client,
7183
announce_block,
@@ -78,8 +90,9 @@ pub async fn start_executor<'a, Block, Client, Backend, Spawner, RClient, IQ, TP
7890
network,
7991
backend,
8092
create_inherent_data_providers,
93+
code_executor,
8194
is_authority,
82-
}: StartExecutorParams<'a, Block, Client, Spawner, RClient, IQ, TP, Backend, CIDP>,
95+
}: StartExecutorParams<'a, Block, Client, Spawner, RClient, IQ, TP, Backend, CIDP, E>,
8396
) -> sc_service::error::Result<()>
8497
where
8598
Block: BlockT,
@@ -110,6 +123,7 @@ where
110123
IQ: ImportQueue<Block> + 'static,
111124
TP: TransactionPool<Block = Block> + 'static,
112125
CIDP: CreateInherentDataProviders<Block, cirrus_primitives::Hash> + 'static,
126+
E: CodeExecutor,
113127
{
114128
let consensus = cumulus_client_consensus_common::run_parachain_consensus(
115129
client.clone(),
@@ -141,6 +155,7 @@ where
141155
execution_receipt_sender,
142156
backend,
143157
create_inherent_data_providers,
158+
code_executor,
144159
is_authority,
145160
})
146161
.await;

cumulus/pallets/executive/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<
184184
UnsignedValidator: ValidateUnsigned<Call = CallOf<Block::Extrinsic, Context>>,
185185
{
186186
/// Returns the latest storage root.
187-
fn storage_root() -> Vec<u8> {
187+
pub fn storage_root() -> Vec<u8> {
188188
let version = System::Version::get().state_version();
189189
sp_io::storage::root(version)
190190
}
@@ -239,7 +239,6 @@ impl<
239239
AllPalletsWithSystem,
240240
COnRuntimeUpgrade,
241241
>::initialize_block(header);
242-
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());
243242
}
244243

245244
// TODO: https://github.com/paritytech/substrate/issues/10711
@@ -307,6 +306,7 @@ impl<
307306

308307
/// Wrapped `frame_executive::Executive::finalize_block`.
309308
pub fn finalize_block() -> System::Header {
309+
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());
310310
frame_executive::Executive::<
311311
System,
312312
Block,
@@ -347,6 +347,7 @@ impl<
347347
///
348348
/// Note the storage root in the end.
349349
pub fn apply_extrinsic(uxt: Block::Extrinsic) -> ApplyExtrinsicResult {
350+
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());
350351
let res = frame_executive::Executive::<
351352
System,
352353
Block,
@@ -355,8 +356,15 @@ impl<
355356
AllPalletsWithSystem,
356357
COnRuntimeUpgrade,
357358
>::apply_extrinsic(uxt);
358-
// TODO: when the extrinsic fails, the storage root does not change, thus skip it?
359-
Pallet::<ExecutiveConfig>::push_root(Self::storage_root());
359+
// TODO: Critical!!! https://github.com/paritytech/substrate/pull/10922#issuecomment-1068997467
360+
frame_support::log::info!(
361+
target: "cirrus::runtime::executive",
362+
"[apply_extrinsic] after: {:?}",
363+
{
364+
use codec::Decode;
365+
Block::Hash::decode(&mut Self::storage_root().as_slice()).unwrap()
366+
}
367+
);
360368
res
361369
}
362370

cumulus/parachain-template/node/src/service.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn new_partial<RuntimeApi, Executor, BIQ>(
5555
Block,
5656
TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<Executor>>,
5757
>,
58-
(Option<Telemetry>, Option<TelemetryWorkerHandle>),
58+
(Option<Telemetry>, Option<TelemetryWorkerHandle>, NativeElseWasmExecutor<Executor>),
5959
>,
6060
sc_service::Error,
6161
>
@@ -98,7 +98,7 @@ where
9898
})
9999
.transpose()?;
100100

101-
let executor = sc_executor::NativeElseWasmExecutor::<Executor>::new(
101+
let executor = NativeElseWasmExecutor::<Executor>::new(
102102
config.wasm_method,
103103
config.default_heap_pages,
104104
config.max_runtime_instances,
@@ -109,7 +109,7 @@ where
109109
sc_service::new_full_parts::<Block, RuntimeApi, _>(
110110
config,
111111
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
112-
executor,
112+
executor.clone(),
113113
)?;
114114
let client = Arc::new(client);
115115

@@ -143,7 +143,7 @@ where
143143
task_manager,
144144
transaction_pool,
145145
select_chain: (),
146-
other: (telemetry, telemetry_worker_handle),
146+
other: (telemetry, telemetry_worker_handle, executor),
147147
};
148148

149149
Ok(params)
@@ -211,7 +211,7 @@ where
211211

212212
let params = new_partial::<RuntimeApi, Executor, BIQ>(&parachain_config, build_import_queue)?;
213213

214-
let (mut telemetry, _telemetry_worker_handle) = params.other;
214+
let (mut telemetry, _telemetry_worker_handle, code_executor) = params.other;
215215

216216
let primary_chain_full_node = {
217217
let span = tracing::info_span!(sc_tracing::logging::PREFIX_LOG_SPAN, name = "Primarychain");
@@ -306,13 +306,14 @@ where
306306
client: client.clone(),
307307
task_manager: &mut task_manager,
308308
primary_chain_full_node,
309-
spawner,
309+
spawner: Box::new(spawner),
310310
parachain_consensus,
311311
import_queue,
312312
transaction_pool,
313313
network,
314314
backend,
315315
create_inherent_data_providers: Arc::new(move |_, _relay_parent| async move { Ok(()) }),
316+
code_executor: Arc::new(code_executor),
316317
is_authority: validator,
317318
};
318319

cumulus/parachain-template/runtime/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ impl_runtime_apis! {
432432
fn intermediate_roots() -> Vec<[u8; 32]> {
433433
ExecutivePallet::intermediate_roots()
434434
}
435+
436+
fn apply_extrinsic_with_post_state_root(extrinsic: <Block as BlockT>::Extrinsic) -> Vec<u8> {
437+
let _ = Executive::apply_extrinsic(extrinsic);
438+
Executive::storage_root()
439+
}
435440
}
436441

437442
#[cfg(feature = "runtime-benchmarks")]

cumulus/primitives/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,8 @@ sp_api::decl_runtime_apis! {
7979

8080
/// Returns the intermediate storage roots in an encoded form.
8181
fn intermediate_roots() -> Vec<[u8; 32]>;
82+
83+
/// Returns the storage root after applying the extrinsic.
84+
fn apply_extrinsic_with_post_state_root(extrinsic: <Block as BlockT>::Extrinsic) -> Vec<u8>;
8285
}
8386
}

0 commit comments

Comments
 (0)