github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/iavl/docs/node/key_format.md (about)

     1  # Key Format
     2  
     3  Nodes, orphans, and roots are stored under the database with different key formats to ensure there are no key collisions and a structured key from which we can extract useful information.
     4  
     5  ### Nodes
     6  
     7  Node KeyFormat: `n|<node.hash>`
     8  
     9  Nodes are marshalled and stored under nodekey with prefix `n` to prevent collisions and then appended with the node's hash.
    10  
    11  ### Orphans
    12  
    13  Orphan KeyFormat: `o|toVersion|fromVersion|hash`
    14  
    15  Orphans are marshalled nodes stored with prefix `o` to prevent collisions. You can extract the toVersion, fromVersion and hash from the orphan key by using:
    16  
    17  ```golang
    18  // orphanKey: o|50|30|0xABCD
    19  var toVersion, fromVersion int64
    20  var hash []byte
    21  orphanKeyFormat.Scan(orphanKey, &toVersion, &fromVersion, hash)
    22  
    23  /*
    24  toVersion = 50
    25  fromVersion = 30
    26  hash = 0xABCD
    27  */
    28  ```
    29  
    30  The order of the orphan KeyFormat matters. Since deleting a version `v` will delete all orphans whose `toVersion = v`, we can easily retrieve all orphans from nodeDb by iterating over the key prefix: `o|v`.
    31  
    32  ### Roots
    33  
    34  Root KeyFormat: `r|<version>`
    35  
    36  Root hash of the IAVL tree at version `v` is stored under the key `r|v` (prefixed with `r` to avoid collision).