I have written a simple block
import that wraps grandpa block import, it does tiny verification and calls grandpa block import after that, it looks like this:
pub struct CustomBlockImport<I> {
grandpa_block_import: I,
}
#[async_trait::async_trait]
impl<I: sc_consensus::BlockImport<Block>> sc_consensus::BlockImport<Block> for WitnessBlockImport<I>
where
I: Send,
{
type Error = ConsensusError;
type Transaction = I::Transaction;
async fn check_block(
&mut self,
block: BlockCheckParams<Block>,
) -> Result<ImportResult, Self::Error> {
return self
.grandpa_block_import
.import_block(block, cache)
.await
.map_err(|e| ConsensusError::ClientImport(format!("{}", e)))
}
async fn import_block(
&mut self,
block: BlockImportParams<Block, Self::Transaction>,
cache: HashMap<well_known_cache_keys::Id, Vec<u8>>,
) -> Result<ImportResult, Self::Error> {
// if block is already finalized no need for my custom verification, just run grandpa block import
if block.finalized {
log::info!("block already finalized");
return self
.grandpa_block_import
.import_block(block, cache)
.await
.map_err(|e| ConsensusError::ClientImport(format!("{}", e)))
}
// do some verification
return self
.grandpa_block_import
.import_block(block, cache)
.await
.map_err(|e| ConsensusError::ClientImport(format!("{}", e)))
}
}
I tried using block.finalized
field to detect whether its already finalized or not however in my simple network, I start three authorities (Alice, Bob, Charlie) and delay running the final authority (Dave) after quite some time (so that the first three get to finalize some blocks); when I start the Dave node, it does not log block already finalized
as it should, instead it goes and do my custom verification logic.
why is this happening and how to solve this?