README.md (3277B)
1 # mssmt-lmdb 2 3 Merkle-Sum Sparse Merkle Trees (MS-SMTs) held in [LMDB][lmdb], via 4 mssmt and [ppad-lmdb][plmdb]. 5 6 A store holds any number of named trees in an LMDB environment: each 7 tree's nodes, keyed by the hashes of their commitments, and its root. 8 Each operation on a tree runs in its own transaction, loading only the 9 paths it touches and writing only the nodes it creates. There's a store 10 for each kind of tree that mssmt provides: 11 12 * `Data.MSSMT.LMDB.Split`, re-exported by `Data.MSSMT.LMDB`: for trees 13 whose branches commit to the hash and sum of each of their children. 14 15 * `Data.MSSMT.LMDB.Aggregate`: for trees whose branches commit to their 16 children's hashes and the sum of their sums. 17 18 ## Usage 19 20 A sample GHCi session: 21 22 ``` 23 > :set -XOverloadedStrings 24 > 25 > import qualified Data.ByteString as BS 26 > import qualified Data.MSSMT.LMDB as M 27 > 28 > -- open a store, and a tree in it 29 > st <- M.open "trees.mdb" M.defaultEnvFlags { M.envNoSubdir = True } 30 > let t = M.tree st "assets" 31 > 32 > -- keys are 32 bytes 33 > let Just alice = M.key (BS.replicate 32 0xa1) 34 > let Just bob = M.key (BS.replicate 32 0xb0) 35 > 36 > -- insert a value and sum at a key, in one transaction 37 > _ <- M.insert t alice "alice's leaf" 100 38 > M.lookup t alice 39 Just ("alice's leaf",100) 40 > 41 > -- the root commits to every leaf, and to the total of their sums 42 > M.Commitment _ total <- M.root t 43 > total 44 100 45 > 46 > -- prove a key's inclusion, or, via an empty value with zero sum, 47 > -- its exclusion 48 > r <- M.root t 49 > p <- M.prove t alice 50 > M.verify alice "alice's leaf" 100 p r 51 True 52 > q <- M.prove t bob 53 > M.verify bob mempty 0 q r 54 True 55 > 56 > -- trees persist 57 > M.close st 58 > st' <- M.open "trees.mdb" M.defaultEnvFlags { M.envNoSubdir = True } 59 > M.lookup (M.tree st' "assets") alice 60 Just ("alice's leaf",100) 61 ``` 62 63 ## Performance 64 65 On an Apple M4, inserting a batch of 1000 leaves into a fresh store, in 66 one transaction, takes roughly 1.8 ms for a split tree, and 15 ms for 67 an aggregate one. A proof, in its own read transaction, takes roughly 68 5 μs in a store of 1000 leaves, and 12 μs in one of 100000. 69 70 The benchmark suite follows that of the [Taproot Assets][tapd] 71 SQL-backed tree store, against which aggregate trees here are about 3 72 to 15 times faster at inserting batches, and 35 to 55 times faster at 73 proving. Note that on macOS, LMDB syncs commits with `fsync`, rather 74 than the `F_FULLFSYNC` that Taproot Assets configures SQLite to use. 75 76 Use `cabal bench` to run the benchmark suite. You should compile with 77 the 'llvm' flag for maximum performance. 78 79 ## Limitations 80 81 Nodes that a tree no longer refers to aren't yet pruned, so a store 82 grows with every change to its trees. LMDB permits one open environment 83 per path per process. 84 85 ## Development 86 87 You'll require [Nix][nixos] with [flake][flake] support enabled. Enter a 88 development shell with: 89 90 ``` 91 $ nix develop 92 ``` 93 94 Then do e.g.: 95 96 ``` 97 $ cabal repl mssmt-lmdb 98 ``` 99 100 to get a REPL for the main library, or `cabal test` to run the test 101 suite. 102 103 [tapd]: https://github.com/lightninglabs/taproot-assets 104 [lmdb]: https://www.symas.com/lmdb 105 [plmdb]: https://github.com/ppad-tech/lmdb 106 [nixos]: https://nixos.org/ 107 [flake]: https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html