2

I am trying to execute some logic whenever a block is received, specifically i want to create custom transactions validation of a received block.

can someone provide a clear example (simple code) on how to provide extra verification of a received block in aura?

ps: i noticed that aura use grandpa when it creates its BlockImport, i assume this has something to with verifying the block if so why aura is using grandpa validation logic of having its own, and is there a way to simply extend it by plugging extra verification logic, if that is indeed the correct place for verification.

1 Answer 1

1

You could write your own BlockImport:

struct BlockImport<I>(I);

impl<I: sc_consensus::BlockImport> sc_consensus::BlockImport for BlockImport<I> {
    type Error = I::Error;
    type Transaction = I::Transaction;

    async fn check_block(
        &mut self,
        block: BlockCheckParams<B>,
    ) -> Result<ImportResult, Self::Error> {
        self.0.check_block(block).await
    }

    async fn import_block(
        &mut self,
        block: BlockImportParams<B, Self::Transaction>,
        cache: HashMap<CacheKeyId, Vec<u8>>,
    ) -> Result<ImportResult, Self::Error> {
        if let Some(transactions) = &block.body {
             // Do whatever check you need with the transactions
        }

        self.0.import_block(block, cache).await
    }
}

Then in your service you stack BlockImport(AuraBlockImport(GrandpaBlockImport(Client))) and each external block will be checked with your custom logic.

5
  • when i put my CustomBlockImport in here , the code compiles and it executes with no errors by it does not execute mu custom BlockImport (i put prinln!() everywhere just to be sure and nothing was printed), where is the right place to place it to work ?
    – dadzerlaze
    Oct 24, 2022 at 17:32
  • Be aware that this code is only called when the block is imported from the network. If you are just running with --dev for example, you also need to the block import here: github.com/substrate-developer-hub/substrate-node-template/blob/…. However, I think you don't need to check the extrinsic on your local node after building the block?
    – bkchr
    Oct 24, 2022 at 17:50
  • i guess im going to write another BlockImport and use it in there, however i dont understand what are those two functions (new_partial and new_full) and why in the first we provide new_partial and in the second we dont, and which BlockImport does it gets executed, is it the first or the second?
    – dadzerlaze
    Oct 24, 2022 at 19:06
  • These are different code paths, one is called when a block is being produced by the local node and the other is when importing a block from the network into the local node.
    – bkchr
    Oct 24, 2022 at 21:49
  • It is initialized here: github.com/paritytech/substrate/blob/…
    – bkchr
    Oct 25, 2022 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.