MCPcopy Index your code
hub / github.com/cosmos/iavl

github.com/cosmos/iavl @v1.3.6

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.3.6 ↗ · + Follow
1,300 symbols 6,308 edges 102 files 529 documented · 41% 1 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

IAVL+ Tree

version license API Reference Lint Test Discord chat

Note: Requires Go 1.18+

A versioned, snapshottable (immutable) AVL+ tree for persistent data.

Benchmarks

The purpose of this data structure is to provide persistent storage for key-value pairs (say to store account balances) such that a deterministic merkle root hash can be computed. The tree is balanced using a variant of the AVL algorithm so all operations are O(log(n)).

Nodes of this tree are immutable and indexed by their hash. Thus any node serves as an immutable snapshot which lets us stage uncommitted transactions from the mempool cheaply, and we can instantly roll back to the last committed state to process transactions of a newly committed block (which may not be the same set of transactions as those from the mempool).

In an AVL tree, the heights of the two child subtrees of any node differ by at most one. Whenever this condition is violated upon an update, the tree is rebalanced by creating O(log(n)) new nodes that point to unmodified nodes of the old tree. In the original AVL algorithm, inner nodes can also hold key-value pairs. The AVL+ algorithm (note the plus) modifies the AVL algorithm to keep all values on leaf nodes, while only using branch-nodes to store keys. This simplifies the algorithm while keeping the merkle hash trail short.

In Ethereum, the analog is Patricia tries. There are tradeoffs. Keys do not need to be hashed prior to insertion in IAVL+ trees, so this provides faster iteration in the key space which may benefit some applications. The logic is simpler to implement, requiring only two types of nodes -- inner nodes and leaf nodes. On the other hand, while IAVL+ trees provide a deterministic merkle root hash, it depends on the order of transactions. In practice this shouldn't be a problem, since you can efficiently encode the tree structure when serializing the tree contents.

IAVL x Cosmos SDK

IAVL DB Interface Cosmos SDK
v0.19.x tm-db v0.45.x, v0.46.x
v0.20.x cometbft-db v0.47.x
v1.0.3 cosmos-db v0.50.0-5
v1.1.2,4 iavl-db v0.50.6
v1.2.x iavl-db v0.50.7+

NOTE: In the past, a v0.21.x release was published, but never used in production. It was retracted to avoid confusion.

Extension points exported contracts — how you extend this code

Node (Interface)
Node represents a node eligible for caching. [5 implementers]
cache/cache.go
DB (Interface)
DB is the main interface for all database backends. DBs are concurrency-safe. Callers must call Close on the database wh [5 …
db/types.go
NodeExporter (Interface)
(no doc) [14 implementers]
compress.go
Iterator (Interface)
(no doc) [10 implementers]
v2/iterator.go
Logger (Interface)
Logger defines basic logger that IAVL expects. It is a subset of the cosmossdk.io/core/log.Logger interface. cosmossdk.i [1 …
logger.go
KVPairReceiver (FuncType)
KVPairReceiver is callback parameter of method `extractStateChanges` to receive stream of `KVPair`s.
diff.go
NodeEncoder (FuncType)
NodeEncoder will take an id (hash, or key for leaf nodes), the depth of the node, and whether or not this is a leaf node
immutable_tree.go
Proxy (Interface)
(no doc) [1 implementers]
v2/metrics/metrics.go

Core symbols most depended-on inside this repo

Set
called by 157
v2/tree.go
NewNopLogger
called by 144
logger.go
Set
called by 137
mutable_tree.go
NewMutableTree
called by 123
mutable_tree.go
SaveVersion
called by 123
mutable_tree.go
Error
called by 100
logger.go
SaveVersion
called by 98
v2/tree.go
Get
called by 84
db/types.go

Shape

Method 681
Function 500
Struct 99
TypeAlias 9
Interface 8
FuncType 3

Languages

Go100%

Modules by API surface

nodedb.go68 symbols
mock/db_mock.go52 symbols
mutable_tree.go49 symbols
tree_test.go48 symbols
mutable_tree_test.go47 symbols
db/memdb.go44 symbols
proto/changeset.pb.go39 symbols
db/goleveldb.go39 symbols
v2/sqlite.go36 symbols
v2/tree.go34 symbols
internal/rand/random.go33 symbols
db/prefixdb.go33 symbols

Used by 1 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add iavl \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact