If I have a StorageValue which is for example a rust BTree, and my custom pallets interacts with that BTree which should an invariant - let's say it's a BTree of integers, and n can be added to that BTree if and only if n-1 and n+1 are not present in that BTree.

How does it deal with synchronization issues between nodes? For example, let's say I am the one who created the block, and I tried inserting the number 8 to it. Then, another node tries inserting the number 9 to it. I assume that since I am the one who created the block, 8 was added and 9 was not. But how does the other node verify that I altered the storage item according to protocol and that it still holds the invariant we assume that BTree should always hold? What if my node is a malicious node and added 8 and 9 together? How is it checked?

1 Answer 1



Let's start with consensus. Nodes do not directly agree on what storage values your custom pallet has but on what block should be included next in the chain. Block, in turn, consists of transactions, each of which may or may not modify the storage state. When a node receives a block, it executes sequentially all of its transactions thus actualizing the storage state as well, so all nodes will have the same storage state after executing the same set of blocks given that transactions are deterministic.

Regarding the maintenance of the B-tree invariant - that is actually the responsibility of the node's Runtime - the business logic compiled to wasm that all nodes currently agreed on to be active based on transactions from the blocks, as mentioned above. So here we have two options:

  • Either B-tree structure validates the invariant you mentioned
  • Or your business logic (aka your pallet code) should validate it each time access happens and emit an error if the invariant is violated
  • I want to make sure I understood it correctly: Let's say I create a function called add_number_to_btree(sender: T::AccountId, number: u32). What gets added to the actual block is the call to that function with the parameters, and every node executes that call? So if I check for that invariant in the function I've written, all nodes which use my code and are not malicious will have the same storage state, since every function I'm writing has to be deterministic?
    – Ron Michal
    Nov 6, 2022 at 23:58
  • Yes, you are right!
    – Valentun
    Nov 7, 2022 at 4:40

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.