In substrate-node-template, are there any available ways to execute a task or provide a callback to be executed just before the block gets gossiped to other validators, right after it has produced the block?
1 Answer
If you want to do some work before announcing a block to the network, you can do it the following way. You will need to change this code to something like this:
// Don't let the internals announce the block automatically
config.announce_block = false;
// Start the network etc.
let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue,
block_announce_validator_builder: None,
warp_sync_params: Some(WarpSyncParams::WithProvider(warp_sync)),
})?;
let block_import = client.import_notification_stream();
let custom_block_announce = async move {
while let Some(import) = block_import.next().await {
// If the local node produced the block
if import.origin == BlockOrigin::Own {
// Insert your special logic here
}
// Announce the block to the network
sync_service.announce_block(import.hash, None);
}
};
task_manager.spawn_handle().spawn(
"custom-block-announce",
None,
custom_block_announce.boxed(),
);
Vec<Vec<u8>>
in the dht, (i raised some concerns about the dht here