Commit

The Commit operation is an essential step in the MerklePatriciaTree that ensures the data's persistence and updates the root hash. This operation makes sure that the latest trie's state gets saved securely and consistently.

🪜 Steps to Execute a Commit:

  1. Prepare for Commit: Ensure all recent changes are finalized and are ready for a persistent store.

  2. Traverse the Trie: Go through every node starting from the root, capturing their current state and respective hash values.

  3. Leaf Node Handling: For leaf nodes:

    • Compute the hash of the leaf node's key and value.

    • Store the leaf node in the persistent storage (like PebbleDB or MPTMemoryStorage).

  4. Branch Node Handling: For each branch node:

    • Compute the hash of the branch node, considering all its child references.

    • Persist the branch node in the storage.

  5. Extension Node Handling: For extension nodes:

    • Compute the hash considering the shared path and the next node reference.

    • Save the extension node in the storage.

  6. HashNode Encountered: Upon encountering a HashNode:

    • Fetch the actual node it points to.

    • Compute its hash and continue the commit process for this node.

  7. Update the Root Hash: Once the entire trie has been traversed and all nodes have been stored:

    • Update the root hash in the storage under the key rootHashKey.

  8. Finalize Commit: Conclude the commit process by ensuring that all nodes are now safely stored and that the new root hash correctly reflects the trie's current state.

Last updated