The `trie` library is a Python implementation of the Ethereum Trie (specifically, the Hexary Trie) data structure. It provides tools for interacting with the Merkelized Patricia Trie, which is fundamental to Ethereum's state, transaction, and receipt storage. As of its current version 3.1.0, it remains an active project maintained by the Ethereum Foundation, with a release cadence tied to ongoing Ethereum development and needs.
pip install trieVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the basic usage of `HexaryTrie` by initializing it with an in-memory dictionary acting as a database, inserting and retrieving byte string key-value pairs, and checking for key existence. In a real-world Ethereum context, the `db` parameter would typically be a more robust, persistent database backend.
Provide a persistent and performant database object (implementing dict-like interface) to the `HexaryTrie` constructor for production environments.
For navigating large tries, utilize `trie.TrieFrontierCache` and `HexaryTrie.traverse_from()` methods to minimize database interactions by caching and optimizing exploration paths.
Familiarize yourself with Ethereum's Hex Prefix encoding and RLP encoding specifications for keys and values to ensure correct data interaction and interpretation. The library handles much of this internally, but understanding the underlying mechanics is crucial for debugging and advanced usage.
Update your import statements from `from py_trie import ...` to `from trie import ...`.
Ensure all keys used for `set()`, `get()`, `delete()`, and `in` operations are byte strings (`b'your_key'`). Verify that the key you are searching for genuinely exists or has been properly inserted into the trie.
Catch the `TraversedPartialPath` exception. The exception object contains `exc.simulated_node.sub_segments` which provides the relevant sub-segments to continue exploration into the children of the extension node. Alternatively, use `HexaryTrieFog` for more controlled and stateful traversal of the trie.