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

Split.hs (3128B)


      1 {-# OPTIONS_HADDOCK prune #-}
      2 
      3 -- |
      4 -- Module: Data.MSSMT.LMDB.Split
      5 -- Copyright: (c) 2026 Jared Tobin
      6 -- License: MIT
      7 -- Maintainer: Jared Tobin <jared@jtobin.io>
      8 --
      9 -- A merkle-sum sparse merkle tree (MS-SMT) whose branches commit to
     10 -- the hash and sum of each of their children, held in LMDB; see
     11 -- "Data.MSSMT.Split".
     12 --
     13 -- A store holds any number of named trees in an LMDB environment. Each
     14 -- operation on a tree runs in its own transaction, loading only the
     15 -- paths it touches.
     16 
     17 module Data.MSSMT.LMDB.Split (
     18   -- * Stores
     19     Store
     20   , open
     21   , close
     22   , L.EnvFlags(..)
     23   , L.defaultEnvFlags
     24   , StoreException(..)
     25 
     26   -- * Trees
     27   , Tree
     28   , tree
     29   , root
     30   , insert
     31   , insert_many
     32   , delete
     33   , lookup
     34   , prove
     35 
     36   -- * Keys, commitments, and proofs
     37   , M.Key
     38   , M.key
     39   , M.Hash
     40   , M.parse_hash
     41   , M.serialize_hash
     42   , M.Commitment(..)
     43   , M.Proof(..)
     44   , M.Neighbor(..)
     45   , M.verify
     46   ) where
     47 
     48 import qualified Data.ByteString as BS
     49 import Data.MSSMT.Internal (Scheme(..))
     50 import Data.MSSMT.LMDB.Internal (StoreException(..))
     51 import qualified Data.MSSMT.LMDB.Internal as I
     52 import qualified Data.MSSMT.Split as M
     53 import Data.Word (Word64)
     54 import qualified Database.LMDB as L
     55 import Prelude hiding (lookup)
     56 
     57 -- | A store of split trees, held in an LMDB environment.
     58 newtype Store = Store I.Store
     59 
     60 -- | A named tree in a store.
     61 data Tree = Tree !I.Store !BS.ByteString
     62 
     63 -- | Open a store at a path.
     64 --
     65 --   LMDB permits one open environment per path per process.
     66 open :: FilePath -> L.EnvFlags -> IO Store
     67 open path flags = Store <$> I.open Split path flags
     68 
     69 -- | Close a store.
     70 close :: Store -> IO ()
     71 close (Store st) = I.close st
     72 
     73 -- | The tree with a name in a store; empty, if there's none yet.
     74 tree :: Store -> BS.ByteString -> Tree
     75 tree (Store st) = Tree st
     76 
     77 -- | The tree's root commitment.
     78 root :: Tree -> IO M.Commitment
     79 root (Tree st n) = I.root st n
     80 
     81 -- | Insert a value and sum at a key, replacing any existing leaf, and
     82 --   return the new root.
     83 --
     84 --   Returns 'Nothing', changing nothing, if the tree's sum would
     85 --   overflow.
     86 insert
     87   :: Tree -> M.Key -> BS.ByteString -> Word64 -> IO (Maybe M.Commitment)
     88 insert (Tree st n) = I.insert st n
     89 
     90 -- | Insert many values and sums, in one transaction, and return the new
     91 --   root.
     92 --
     93 --   Where a key appears more than once, its last appearance wins, and
     94 --   an empty value with zero sum deletes whatever is at its key.
     95 --   Returns 'Nothing', changing nothing, if the resulting tree's sum
     96 --   would overflow.
     97 insert_many
     98   :: Tree -> [(M.Key, BS.ByteString, Word64)] -> IO (Maybe M.Commitment)
     99 insert_many (Tree st n) = I.insert_many st n
    100 
    101 -- | Delete the leaf at a key, and return the new root.
    102 delete :: Tree -> M.Key -> IO M.Commitment
    103 delete (Tree st n) = I.delete st n
    104 
    105 -- | Look up the value and sum at a key.
    106 lookup :: Tree -> M.Key -> IO (Maybe (BS.ByteString, Word64))
    107 lookup (Tree st n) = I.lookup st n
    108 
    109 -- | Produce a proof for the leaf at a key.
    110 --
    111 --   If no leaf exists at the key, this proves its exclusion.
    112 prove :: Tree -> M.Key -> IO M.Proof
    113 prove (Tree st n) k = do
    114   (nb, ps) <- I.prove_split st n k
    115   pure (M.Proof nb ps)