mssmt-lmdb

An LMDB-backed Merkle-Sum Sparse Merkle Tree.
git clone git://git.jtobin.io/mssmt-lmdb.git
Log | Files | Refs | README | LICENSE | git.jtobin.io

commit 30a4baff7b00a7b97511b27ea14577b58bbeb7cd
parent ee5d011966e3d2dd4f3f5740b39bb9878f24e343
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 23:17:18 +0400

readme: init

Diffstat:
AREADME.md | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 106 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,106 @@ +# mssmt-lmdb + +Merkle-sum sparse merkle trees (MS-SMTs) held in [LMDB][lmdb], via +mssmt and [ppad-lmdb][plmdb]. + +A store holds any number of named trees in an LMDB environment: each +tree's nodes, keyed by the hashes of their commitments, and its root. +Each operation on a tree runs in its own transaction, loading only the +paths it touches and writing only the nodes it creates. There's a store +for each kind of tree that mssmt provides: + +* `Data.MSSMT.LMDB.Split`, re-exported by `Data.MSSMT.LMDB`: for trees + whose branches commit to the hash and sum of each of their children. + +* `Data.MSSMT.LMDB.Aggregate`: for trees whose branches commit to their + children's hashes and the sum of their sums, as in Taproot Assets. + +## Usage + +A sample GHCi session: + +``` + > :set -XOverloadedStrings + > + > import qualified Data.ByteString as BS + > import qualified Data.MSSMT.LMDB as M + > + > -- open a store, and a tree in it + > st <- M.open "trees.mdb" M.defaultEnvFlags { M.envNoSubdir = True } + > let t = M.tree st "assets" + > + > -- keys are 32 bytes + > let Just alice = M.key (BS.replicate 32 0xa1) + > let Just bob = M.key (BS.replicate 32 0xb0) + > + > -- insert a value and sum at a key, in one transaction + > _ <- M.insert t alice "alice's leaf" 100 + > M.lookup t alice + Just ("alice's leaf",100) + > + > -- the root commits to every leaf, and to the total of their sums + > M.Commitment _ total <- M.root t + > total + 100 + > + > -- prove a key's inclusion, or, via an empty value with zero sum, + > -- its exclusion + > r <- M.root t + > p <- M.prove t alice + > M.verify alice "alice's leaf" 100 p r + True + > q <- M.prove t bob + > M.verify bob mempty 0 q r + True + > + > -- trees persist + > M.close st + > st' <- M.open "trees.mdb" M.defaultEnvFlags { M.envNoSubdir = True } + > M.lookup (M.tree st' "assets") alice + Just ("alice's leaf",100) +``` + +## Performance + +On an Apple M4, inserting a batch of 1000 leaves into a fresh store, in +one transaction, takes roughly 1.8 ms for a split tree, and 15 ms for +an aggregate one. A proof, in its own read transaction, takes roughly +5 μs in a store of 1000 leaves, and 12 μs in one of 100000. + +The benchmark suite follows that of the Taproot Assets SQL-backed tree +store, against which aggregate trees here are about 3 to 15 times +faster at inserting batches, and 35 to 55 times faster at proving. +Note that on macOS, LMDB syncs commits with `fsync`, rather than the +`F_FULLFSYNC` that Taproot Assets configures SQLite to use. + +Use `cabal bench` to run the benchmark suite. You should compile with +the 'llvm' flag for maximum performance. + +## Limitations + +Nodes that a tree no longer refers to aren't yet pruned, so a store +grows with every change to its trees. LMDB permits one open environment +per path per process. + +## Development + +You'll require [Nix][nixos] with [flake][flake] support enabled. Enter a +development shell with: + +``` +$ nix develop +``` + +Then do e.g.: + +``` +$ cabal repl mssmt-lmdb +``` + +to get a REPL for the main library, or `cabal test` to run the test +suite. + +[lmdb]: https://www.symas.com/lmdb +[plmdb]: https://github.com/ppad-tech/lmdb +[nixos]: https://nixos.org/ +[flake]: https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html