{"id":27021194,"url":"https://github.com/jonas089/jonas089-trie","last_synced_at":"2025-04-04T19:43:04.005Z","repository":{"id":244697708,"uuid":"815993343","full_name":"jonas089/jonas089-trie","owner":"jonas089","description":"Rust Merkle Trie for Blockchain Systems","archived":false,"fork":false,"pushed_at":"2025-02-08T22:17:06.000Z","size":222,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-08T22:27:39.008Z","etag":null,"topics":["blockchain","merkle-trie","portfolio","rust","rust-blockchain","rust-crypto"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonas089.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-16T18:54:36.000Z","updated_at":"2025-02-08T22:17:09.000Z","dependencies_parsed_at":"2024-06-16T20:25:41.400Z","dependency_job_id":"f6428bae-1d1f-4ff1-b99f-e497617b16c0","html_url":"https://github.com/jonas089/jonas089-trie","commit_stats":null,"previous_names":["jonas089/jonas089-trie"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonas089%2Fjonas089-trie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonas089%2Fjonas089-trie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonas089%2Fjonas089-trie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonas089%2Fjonas089-trie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonas089","download_url":"https://codeload.github.com/jonas089/jonas089-trie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247239045,"owners_count":20906556,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","merkle-trie","portfolio","rust","rust-blockchain","rust-crypto"],"created_at":"2025-04-04T19:43:03.456Z","updated_at":"2025-04-04T19:43:03.989Z","avatar_url":"https://github.com/jonas089.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Merkle Binary Patricia Trie for immutable Blockchain State\n![benchmark](https://github.com/jonas089/jonas089-trie/blob/master/resources/simple-bench.png)\n\n\u003e [!WARNING]  \n\u003e This is a research project and hasn't been audited. Use in production at your own risk.\n\n## Implementation Details\n\nHistorical state is preserved for each `root hash`, one can query the `db` for a Root and generate `Merkle Proofs` for `Leaf`s in the `Trie`.\nEach `Merkle Proof` is verified against a `root` to verify that a `Leaf` was present in the `Trie` at some point in time.\n\nAn example of constructing the in-memory `db`, inserting a `Leafs` and verifying `Merkle Proof` can be found [here](https://github.com/jonas089/jonas089-trie/blob/master/src/merkle.rs)\n\n## Experimental SQLite Support\nNew Feature! The InMemoryDB is now optional and can be replaced with an SQLite table.\n\nTo enable this feature, include the `sqlite` flag:\n\n```rust\ncargo test --features sqlite test_sql_db\n```\n\n\n## API\n\nThis library primarily exposes two entry points, one to insert a new `Leaf` into a `Trie`:\n\n```rust\npub fn insert_leaf(db: \u0026mut dyn Database, new_leaf: \u0026mut Leaf, root_node: Node) -\u003e Root {\n    assert_eq!(new_leaf.key.len(), 256);\n    let modified_nodes = traverse_trie(db, new_leaf, root_node.clone(), false);\n    let mut new_root = update_modified_leafs(db, modified_nodes, root_node.unwrap_as_root());\n    new_root.hash_and_store(db);\n    new_root\n}\n```\n\n\nAdditionally, there are two public functions to generate and verify `Merkle Proofs`:\n\n```rust\npub fn merkle_proof(db: \u0026mut dyn Database, key: Vec\u003cu8\u003e, trie_root: Node) -\u003e Option\u003cMerkleProof\u003e {\n    assert_eq!(key.len(), 256);\n    let mut proof: MerkleProof = MerkleProof { nodes: Vec::new() };\n    let mut current_node = trie_root.clone();\n    loop {\n        match \u0026mut current_node {\n            Node::Root(root) =\u003e {\n                proof.nodes.push((false, Node::Root(root.clone())));\n                if key[0] == 0 {\n                    let left_child = db.get(\u0026root.left.clone().unwrap()).unwrap();\n                    current_node = left_child.clone();\n                    proof.nodes.push((false, left_child.clone()));\n                } else {\n                    let right_child = db.get(\u0026root.right.clone().unwrap()).unwrap();\n                    current_node = right_child.clone();\n                    proof.nodes.push((true, right_child.clone()));\n                }\n            }\n            Node::Branch(branch) =\u003e {\n                let digit = key[branch.key[0] as usize];\n                if digit == 0 {\n                    current_node = db.get(\u0026branch.left.clone().unwrap()).unwrap().clone();\n                    proof.nodes.push((false, current_node.clone()));\n                } else {\n                    current_node = db.get(\u0026branch.right.clone().unwrap()).unwrap().clone();\n                    proof.nodes.push((true, current_node.clone()));\n                }\n            }\n            Node::Leaf(_) =\u003e return Some(proof),\n        }\n    }\n}\n\n```\n\nand\n\n```rust\npub fn verify_merkle_proof(mut inner_proof: Vec\u003c(bool, Node)\u003e, state_root_hash: RootHash) {\n    inner_proof.reverse();\n    let mut current_hash: Option\u003c(bool, NodeHash)\u003e = None;\n    let mut root_hash: Option\u003cRootHash\u003e = None;\n    for (idx, node) in inner_proof.into_iter().enumerate() {\n        if idx == 0 {\n            let leaf = node.1.unwrap_as_leaf();\n            current_hash = Some((node.0, leaf.hash.unwrap()));\n        } else {\n            match node.1 {\n                Node::Root(mut root) =\u003e {\n                    if !current_hash.clone().unwrap().0 {\n                        root.left = Some(current_hash.clone().unwrap().1);\n                    } else {\n                        root.right = Some(current_hash.clone().unwrap().1);\n                    }\n                    root.hash();\n                    root_hash = root.hash;\n                }\n                Node::Branch(mut branch) =\u003e {\n                    if !current_hash.clone().unwrap().0 {\n                        branch.left = Some(current_hash.clone().unwrap().1);\n                    } else {\n                        branch.right = Some(current_hash.clone().unwrap().1);\n                    }\n                    branch.hash();\n                    current_hash = Some((node.0, branch.hash.unwrap()));\n                }\n                Node::Leaf(_) =\u003e panic!(\"Invalid Node variant in Merkle Proof\"),\n            }\n        }\n    }\n    // if this assertion passes, the merkle proof is valid\n    // for the given root hash\n    assert_eq!(\u0026state_root_hash, \u0026root_hash.unwrap());\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonas089%2Fjonas089-trie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonas089%2Fjonas089-trie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonas089%2Fjonas089-trie/lists"}