JoshOrndorff / recipes

A Hands-On Cookbook for Aspiring Blockchain Chefs
GNU General Public License v3.0
376 stars 186 forks source link

Properly writeup and code up instant-and-manual seal consensus #364

Open JoshOrndorff opened 3 years ago

JoshOrndorff commented 3 years ago

@lsaether added an enhancement to the kitchen node that allows it to both instantly ad manually seal blocks. The code is in an [alternate service.rs file]() which was not tested and not discoverable. The proper way to host this topic is in a consensus crate alongside the sha3pow.

The original code is here https://github.com/substrate-developer-hub/recipes/blob/v2.0.0-rc3/nodes/manual-seal/src/combined_service.rs#L85-L124

    // 1) We create the pool here because we need it for both the `pool_stream`
    // and later in the consensus builder.
    let pool = service.transaction_pool().pool().clone();

    // 2) We create a pool_stream for notifications of blocks that are imported into
    // the transaction pool. This code was cribbed from the implementation of instant seal
    // and is how instant seal works internally.
    let pool_stream = pool
        .validated_pool()
        .import_notification_stream()
        .map(|_| {
            // Every new block create an `EngineCommand` that will seal a new block.
            rpc::EngineCommand::SealNewBlock {
                create_empty: false,
                finalize: false,
                parent_hash: None,
                sender: None,
            }
        });

    // 3) Use select to take events produced by both the `commands_stream` and the
    // `pool_stream` together.
    let combined_stream = futures::stream::select(commands_stream, pool_stream);

    if is_authority {
        // Proposer object for block authorship.
        let proposer = sc_basic_authorship::ProposerFactory::new(
            service.client(),
            service.transaction_pool(),
            service.prometheus_registry().as_ref(),
        );

        // Background authorship future.
        let authorship_future = manual_seal::run_manual_seal(
            Box::new(service.client()),
            proposer,
            service.client(), // 4) vvvvv
            pool,             // <- Use the same pool that we used to get `pool_stream`.
            combined_stream,  // <- Here we place the combined streams. 
jimmychu0807 commented 3 years ago

pending on paritytech/substrate/pull/6951

@bkchr @seunlanlege since Substrate v2 is launched, do you have a little bit of time to review that PR? If yes, I can merge with v2 first, and then notify you guys.