4

Currently in substrate there is a possibility to set state pruning to "archive-canonical" or "number". In docs it states, that these values will cause to:

  • archive-canonical - Keep only the state of finalized blocks.
  • number - Keep the state of the last number of finalized blocks.

That said, when digging deeper there is a fundamental difference between canonical and finalized. It is described in https://github.com/paritytech/substrate/blob/master/client/db/src/lib.rs

//! # Canonicality vs. Finality
//!
//! Finality indicates that a block will not be reverted, according to the consensus algorithm,
//! while canonicality indicates that the block may be reverted, but we will be unable to do so,
//! having discarded heavy state that will allow a chain reorganization.
//!
//! Finality implies canonicality but not vice-versa.

And this functionality works in such a way, that if best block is over 4096 (constant value named CANONICALIZATION_DELAY) blocks on top of best finalized block, state of yet unfinalized blocks will be pruned by function force_delayed_canonicalize (https://github.com/paritytech/substrate/blob/129fee774a6d185d117a57fd1e81b3d0d05ad747/client/db/src/lib.rs#L1399).

This has proven problematic for me, when using pruning. I could not call runtime_api for the state of best finalized block (it was already pruned because of finalization stall).

Because of that I have three questions:

  1. Are there any plans to add something like archive-finalized? So in this setting unfinalized blocks would not be prunned.
  2. Why is force_delayed_canonicalize function needed? Are there any dangers of it not being called?
  3. Why is CANONICALIZATION_DELAY set to 4096 blocks? Can it be bigger?

1 Answer 1

1

Are there any plans to add something like archive-finalized? So in this setting unfinalized blocks would not be prunned.

https://github.com/paritytech/substrate/pull/13157 after this pr blocks will stay pinned until the pinning handle is dropped. This should solve your issue.

Why is force_delayed_canonicalize function needed? Are there any dangers of it not being called?

The database is growing and gets slower. In a future iteration of the Substrate database implementation we hopefully can get rid of this.

Why is CANONICALIZATION_DELAY set to 4096 blocks? Can it be bigger?

You can try it. It should work, but it could happen that the node gets slower on a bigger finality stall.

This has proven problematic for me, when using pruning. I could not call runtime_api for the state of best finalized block (it was already pruned because of finalization stall).

But with archive-canonical it should not had happened that the state was removed as long as the block was part of the finalized chain.

6
  • So for now if I just remove call to force_delayed_canonicalize, in case of finalization stall db would become slower, but not slower then archive-all db? And when the finalization catches up, states will be pruned and it will once again be as fast as before?
    – mnems
    Jan 31, 2023 at 15:37
  • Yes, this removal happened when using number, not archive-canonical. I should have made it clear. Still, theoretically if there is a fork of size 4096, and another branch is finalized, this might happen in archive-canonical too.
    – mnems
    Jan 31, 2023 at 15:41
  • If you use the pin logic I posted above, the block would be pruned but you could still access it as long as it is pinned. But be aware that this may increase the memory usage when the block is pinned and then pruned. Regarding the the removal of the forced canonicalization, I would need to check the code again to tell you about the exact impact.
    – bkchr
    Jan 31, 2023 at 15:50
  • I'm not sure that pin logic solves my problem. If the node is restarted, memory would be lost. What I need is to access state of last finalized block, even if the node crashed/exited. This is needed for our finalization gadget. Solution I found thus far was somehow changing pruning logic. Alternatively maybe we could use AuxStore similarly to Grandpa, but this seems like a lot of work.
    – mnems
    Jan 31, 2023 at 16:00
  • Can you create some issue for Substrate about the forced canonicalization. Maybe we can remove it. But I get what you problem is.
    – bkchr
    Jan 31, 2023 at 18:50

Your Answer

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

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