domain_client_operator/
bundle_processor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use crate::domain_block_processor::{
    DomainBlockProcessor, PendingConsensusBlocks, ReceiptsChecker,
};
use crate::ExecutionReceiptFor;
use domain_block_preprocessor::DomainBlockPreprocessor;
use sc_client_api::{AuxStore, BlockBackend, ExecutorProvider, Finalizer, ProofProvider};
use sc_consensus::{BlockImportParams, ForkChoiceStrategy, StateAction};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{HeaderBackend, HeaderMetadata};
use sp_consensus::BlockOrigin;
use sp_core::traits::CodeExecutor;
use sp_core::H256;
use sp_domain_digests::AsPredigest;
use sp_domains::core_api::DomainCoreApi;
use sp_domains::{DomainId, DomainsApi, ReceiptValidity};
use sp_domains_fraud_proof::FraudProofApi;
use sp_messenger::MessengerApi;
use sp_mmr_primitives::MmrApi;
use sp_runtime::traits::{Block as BlockT, CheckedSub, NumberFor, Zero};
use sp_runtime::{Digest, DigestItem};
use sp_weights::constants::WEIGHT_REF_TIME_PER_MILLIS;
use std::sync::Arc;
use std::time::Instant;
use tracing::info;

// The slow log threshold for consensus block preprocessing
const SLOW_PREPROCESS_MILLIS: u64 = 500;

// The slow log threshold for domain block execution: `reference_duration_ms * 1.2 + 200ms`,
// where `reference_duration_ms * 0.2` as buffer of the slow extrinsic execution (i.e. slower
// machine than the reference machine) and 200ms as buffer of the rest of the processing.
fn slow_domain_block_execution_threshold(reference_duration_ms: u64) -> u64 {
    reference_duration_ms + (reference_duration_ms / 5) + 200
}

// The slow log threshold for post domain block execution
const SLOW_POST_DOMAIN_BLOCK_EXECUTION_MILLIS: u64 = 250;

type DomainReceiptsChecker<Block, CBlock, Client, CClient, Backend, E> =
    ReceiptsChecker<Block, Client, CBlock, CClient, Backend, E>;

pub(crate) struct BundleProcessor<Block, CBlock, Client, CClient, Backend, E>
where
    Block: BlockT,
    CBlock: BlockT,
{
    domain_id: DomainId,
    consensus_client: Arc<CClient>,
    client: Arc<Client>,
    backend: Arc<Backend>,
    domain_receipts_checker: DomainReceiptsChecker<Block, CBlock, Client, CClient, Backend, E>,
    domain_block_preprocessor:
        DomainBlockPreprocessor<Block, CBlock, Client, CClient, ReceiptValidator<Client>>,
    domain_block_processor: DomainBlockProcessor<Block, CBlock, Client, CClient, Backend, E>,
    confirmation_depth_k: NumberFor<CBlock>,
}

impl<Block, CBlock, Client, CClient, Backend, E> Clone
    for BundleProcessor<Block, CBlock, Client, CClient, Backend, E>
where
    Block: BlockT,
    CBlock: BlockT,
{
    fn clone(&self) -> Self {
        Self {
            domain_id: self.domain_id,
            consensus_client: self.consensus_client.clone(),
            client: self.client.clone(),
            backend: self.backend.clone(),
            domain_receipts_checker: self.domain_receipts_checker.clone(),
            domain_block_preprocessor: self.domain_block_preprocessor.clone(),
            domain_block_processor: self.domain_block_processor.clone(),
            confirmation_depth_k: self.confirmation_depth_k,
        }
    }
}

struct ReceiptValidator<Client> {
    client: Arc<Client>,
}

impl<Client> Clone for ReceiptValidator<Client> {
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
        }
    }
}

impl<Client> ReceiptValidator<Client> {
    pub fn new(client: Arc<Client>) -> Self {
        Self { client }
    }
}

impl<Block, CBlock, Client> domain_block_preprocessor::ValidateReceipt<Block, CBlock>
    for ReceiptValidator<Client>
where
    Block: BlockT,
    CBlock: BlockT,
    Client: AuxStore,
{
    fn validate_receipt(
        &self,
        receipt: &ExecutionReceiptFor<Block, CBlock>,
    ) -> sp_blockchain::Result<ReceiptValidity> {
        // Skip genesis receipt as it has been already verified by the consensus chain.
        if receipt.domain_block_number.is_zero() {
            return Ok(ReceiptValidity::Valid);
        }

        let consensus_block_hash = receipt.consensus_block_hash;
        let _local_receipt = crate::aux_schema::load_execution_receipt::<_, Block, CBlock>(
            &*self.client,
            consensus_block_hash,
        )?
        .ok_or_else(|| {
            sp_blockchain::Error::Backend(format!(
                "Receipt for consensus block {consensus_block_hash} not found"
            ))
        })?;

        Ok(ReceiptValidity::Valid)
    }
}

impl<Block, CBlock, Client, CClient, Backend, E>
    BundleProcessor<Block, CBlock, Client, CClient, Backend, E>
where
    Block: BlockT,
    Block::Hash: Into<H256>,
    CBlock: BlockT,
    NumberFor<CBlock>: From<NumberFor<Block>> + Into<NumberFor<Block>>,
    CBlock::Hash: From<Block::Hash>,
    Client: HeaderBackend<Block>
        + BlockBackend<Block>
        + AuxStore
        + ProvideRuntimeApi<Block>
        + ProofProvider<Block>
        + ExecutorProvider<Block>
        + Finalizer<Block, Backend>
        + 'static,
    Client::Api: DomainCoreApi<Block>
        + MessengerApi<Block, NumberFor<CBlock>, CBlock::Hash>
        + sp_block_builder::BlockBuilder<Block>
        + sp_api::ApiExt<Block>,
    CClient: HeaderBackend<CBlock>
        + HeaderMetadata<CBlock, Error = sp_blockchain::Error>
        + BlockBackend<CBlock>
        + ProofProvider<CBlock>
        + ProvideRuntimeApi<CBlock>
        + 'static,
    CClient::Api: DomainsApi<CBlock, Block::Header>
        + MessengerApi<CBlock, NumberFor<CBlock>, CBlock::Hash>
        + FraudProofApi<CBlock, Block::Header>
        + MmrApi<CBlock, H256, NumberFor<CBlock>>
        + 'static,
    Backend: sc_client_api::Backend<Block> + 'static,
    E: CodeExecutor,
{
    pub(crate) fn new(
        domain_id: DomainId,
        consensus_client: Arc<CClient>,
        client: Arc<Client>,
        backend: Arc<Backend>,
        domain_receipts_checker: DomainReceiptsChecker<Block, CBlock, Client, CClient, Backend, E>,
        domain_block_processor: DomainBlockProcessor<Block, CBlock, Client, CClient, Backend, E>,
        confirmation_depth_k: NumberFor<CBlock>,
    ) -> Self {
        let domain_block_preprocessor = DomainBlockPreprocessor::new(
            domain_id,
            client.clone(),
            consensus_client.clone(),
            ReceiptValidator::new(client.clone()),
        );
        Self {
            domain_id,
            consensus_client,
            client,
            backend,
            domain_receipts_checker,
            domain_block_preprocessor,
            domain_block_processor,
            confirmation_depth_k,
        }
    }

    // TODO: Handle the returned error properly, ref to https://github.com/autonomys/subspace/pull/695#discussion_r926721185
    pub(crate) async fn process_bundles(
        self,
        consensus_block_info: (CBlock::Hash, NumberFor<CBlock>, bool),
    ) -> sp_blockchain::Result<()> {
        let (consensus_block_hash, consensus_block_number, is_new_best) = consensus_block_info;

        // Skip processing the blocks of the non-canonical chain, these blocks will be processed if
        // the chain becomes canonical later
        if !is_new_best {
            return Ok(());
        }

        tracing::debug!(
            "Processing consensus block #{consensus_block_number},{consensus_block_hash}"
        );

        let maybe_pending_consensus_blocks = self
            .domain_block_processor
            .pending_imported_consensus_blocks(consensus_block_hash, consensus_block_number)?;

        if let Some(PendingConsensusBlocks {
            initial_parent,
            consensus_imports,
        }) = maybe_pending_consensus_blocks
        {
            tracing::trace!(
                ?initial_parent,
                ?consensus_imports,
                "Pending consensus blocks to process"
            );

            let mut domain_parent = initial_parent;

            for consensus_info in consensus_imports {
                if let Some(next_domain_parent) = self
                    .process_bundles_at((consensus_info.hash, consensus_info.number), domain_parent)
                    .await?
                {
                    domain_parent = next_domain_parent;
                }
            }

            // The domain branch driving from the best consensus branch should also be the best domain branch even
            // if it is no the longest domain branch. Thus re-import the tip of the best domain branch to make it
            // the new best block if it isn't.
            //
            // Note: this may cause the best domain fork switch to a shorter fork or in some case the best domain
            // block become the ancestor block of the current best block.
            let domain_tip = domain_parent.0;
            if self.client.info().best_hash != domain_tip {
                let header = self.client.header(domain_tip)?.ok_or_else(|| {
                    sp_blockchain::Error::Backend(format!("Header for #{:?} not found", domain_tip))
                })?;
                let block_origin = if self
                    .domain_block_processor
                    .domain_sync_oracle
                    .is_major_syncing()
                {
                    // The domain block is derived from the consensus block, if the consensus chain is
                    // in major sync then we should also consider the domain block is `NetworkInitialSync`
                    BlockOrigin::NetworkInitialSync
                } else {
                    BlockOrigin::Own
                };
                let block_import_params = {
                    let mut import_block = BlockImportParams::new(block_origin, header);
                    import_block.import_existing = true;
                    import_block.fork_choice = Some(ForkChoiceStrategy::Custom(true));
                    import_block.state_action = StateAction::Skip;
                    import_block
                };
                self.domain_block_processor
                    .import_domain_block(block_import_params)
                    .await?;
                assert_eq!(domain_tip, self.client.info().best_hash);

                // finalize the latest confirmed domain block in the finalized Consensus block
                self.finalize_domain_block((consensus_block_hash, consensus_block_number))?;
            }

            // Check the ER submitted to consensus chain and submit fraud proof if there is bad ER
            // NOTE: this have to be done after the recorrect of the best domain fork happen above
            self.domain_receipts_checker
                .maybe_submit_fraud_proof(consensus_block_hash)?;
        }

        Ok(())
    }

    // finalize the domain block which is confirmed in the finalized consensus block.
    fn finalize_domain_block(
        &self,
        consensus_block_info: (CBlock::Hash, NumberFor<CBlock>),
    ) -> sp_blockchain::Result<()> {
        if let Some(confirmed_consensus_block) = consensus_block_info
            .1
            .checked_sub(&self.confirmation_depth_k)
        {
            let runtime_api = self.consensus_client.runtime_api();
            let confirmed_consensus_block_hash = self
                .consensus_client
                .hash(confirmed_consensus_block)?
                .ok_or(sp_blockchain::Error::MissingHeader(format!(
                    "Block Number: {}",
                    confirmed_consensus_block
                )))?;

            let finalized_domain_block_number = self.client.info().finalized_number;
            if let Some(confirmed_domain_block) = runtime_api
                .latest_confirmed_domain_block(confirmed_consensus_block_hash, self.domain_id)?
                && confirmed_domain_block.0 > finalized_domain_block_number
            {
                self.client
                    .finalize_block(confirmed_domain_block.1, None, true)?;
                info!("🔒 Finalized block: {:?}", confirmed_domain_block);
            }
        }
        Ok(())
    }

    async fn process_bundles_at(
        &self,
        consensus_block_info: (CBlock::Hash, NumberFor<CBlock>),
        parent_info: (Block::Hash, NumberFor<Block>),
    ) -> 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;
        let start = Instant::now();

        tracing::debug!(
            "Building a new domain block from consensus block #{consensus_block_number},{consensus_block_hash} \
            on top of parent block #{parent_number},{parent_hash}"
        );

        let maybe_preprocess_result = self
            .domain_block_preprocessor
            .preprocess_consensus_block(consensus_block_hash, (parent_hash, parent_number))?;

        let preprocess_took = start.elapsed().as_millis();
        if preprocess_took >= SLOW_PREPROCESS_MILLIS.into() {
            tracing::warn!(
                ?consensus_block_info,
                "Slow consensus block preprocessing, took {preprocess_took}ms"
            );
        }

        let Some(preprocess_result) = maybe_preprocess_result else {
            tracing::debug!(
                "Skip building new domain block, no bundles and runtime upgrade for this domain \
                    in consensus block #{consensus_block_number:?},{consensus_block_hash}"
            );
            self.domain_block_processor
                .on_consensus_block_processed(consensus_block_hash, None)?;
            return Ok(None);
        };

        let inherent_digests = Digest {
            logs: vec![DigestItem::consensus_block_info(consensus_block_hash)],
        };

        let domain_block_result = self
            .domain_block_processor
            .process_domain_block(
                (consensus_block_hash, consensus_block_number),
                (parent_hash, parent_number),
                preprocess_result,
                inherent_digests,
            )
            .await?;

        let head_receipt_number = self
            .consensus_client
            .runtime_api()
            .head_receipt_number(consensus_block_hash, self.domain_id)?;
        assert!(
            domain_block_result.header_number > head_receipt_number,
            "Domain chain number must larger than the head number of the receipt chain \
            (which is maintained on the consensus chain) by at least 1"
        );

        let built_block_info = (
            domain_block_result.header_hash,
            domain_block_result.header_number,
        );

        let block_execution_took = start.elapsed().as_millis().saturating_sub(preprocess_took);

        let reference_block_execution_duration_ms = self
            .client
            .runtime_api()
            .block_weight(domain_block_result.header_hash)?
            .ref_time()
            / WEIGHT_REF_TIME_PER_MILLIS;
        if block_execution_took
            >= slow_domain_block_execution_threshold(reference_block_execution_duration_ms).into()
        {
            tracing::warn!(
                ?consensus_block_info,
                ?built_block_info,
                ?reference_block_execution_duration_ms,
                "Slow domain block execution, took {block_execution_took}ms"
            );
        }

        self.domain_block_processor
            .on_consensus_block_processed(consensus_block_hash, Some(domain_block_result))?;

        let post_block_execution_took = start
            .elapsed()
            .as_millis()
            .saturating_sub(preprocess_took + block_execution_took);
        if post_block_execution_took >= SLOW_POST_DOMAIN_BLOCK_EXECUTION_MILLIS.into() {
            tracing::warn!(
                ?consensus_block_info,
                ?built_block_info,
                "Slow post domain block execution, took {post_block_execution_took}ms"
            );
        }

        Ok(Some(built_block_info))
    }
}