packages feed

mssmt-lmdb-0.1.0: lib/Data/MSSMT/LMDB/Aggregate.hs

{-# OPTIONS_HADDOCK prune #-}

-- |
-- Module: Data.MSSMT.LMDB.Aggregate
-- Copyright: (c) 2026 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@jtobin.io>
--
-- A merkle-sum sparse merkle tree (MS-SMT) whose branches commit to
-- their children's hashes and the sum of their sums, as in Taproot
-- Assets, held in LMDB; see "Data.MSSMT.Aggregate".
--
-- A store holds any number of named trees in an LMDB environment. Each
-- operation on a tree runs in its own transaction, loading only the
-- paths it touches.

module Data.MSSMT.LMDB.Aggregate (
  -- * Stores
    Store
  , open
  , close
  , L.EnvFlags(..)
  , L.defaultEnvFlags
  , StoreException(..)

  -- * Trees
  , Tree
  , tree
  , root
  , insert
  , insert_many
  , delete
  , lookup
  , prove

  -- * Keys, commitments, and proofs
  , M.Key
  , M.key
  , M.Hash
  , M.parse_hash
  , M.serialize_hash
  , M.Commitment(..)
  , M.Proof(..)
  , M.verify
  ) where

import qualified Data.ByteString as BS
import Data.MSSMT.Internal (Scheme(..))
import Data.MSSMT.LMDB.Internal (StoreException(..))
import qualified Data.MSSMT.LMDB.Internal as I
import qualified Data.MSSMT.Aggregate as M
import Data.Word (Word64)
import qualified Database.LMDB as L
import Prelude hiding (lookup)

-- | A store of aggregate trees, held in an LMDB environment.
newtype Store = Store I.Store

-- | A named tree in a store.
data Tree = Tree !I.Store !BS.ByteString

-- | Open a store at a path.
--
--   LMDB permits one open environment per path per process.
open :: FilePath -> L.EnvFlags -> IO Store
open path flags = Store <$> I.open Aggregate path flags

-- | Close a store.
close :: Store -> IO ()
close (Store st) = I.close st

-- | The tree with a name in a store; empty, if there's none yet.
tree :: Store -> BS.ByteString -> Tree
tree (Store st) = Tree st

-- | The tree's root commitment.
root :: Tree -> IO M.Commitment
root (Tree st n) = I.root st n

-- | Insert a value and sum at a key, replacing any existing leaf, and
--   return the new root.
--
--   Returns 'Nothing', changing nothing, if the tree's sum would
--   overflow.
insert
  :: Tree -> M.Key -> BS.ByteString -> Word64 -> IO (Maybe M.Commitment)
insert (Tree st n) = I.insert st n

-- | Insert many values and sums, in one transaction, and return the new
--   root.
--
--   Where a key appears more than once, its last appearance wins, and
--   an empty value with zero sum deletes whatever is at its key.
--   Returns 'Nothing', changing nothing, if the resulting tree's sum
--   would overflow.
insert_many
  :: Tree -> [(M.Key, BS.ByteString, Word64)] -> IO (Maybe M.Commitment)
insert_many (Tree st n) = I.insert_many st n

-- | Delete the leaf at a key, and return the new root.
delete :: Tree -> M.Key -> IO M.Commitment
delete (Tree st n) = I.delete st n

-- | Look up the value and sum at a key.
lookup :: Tree -> M.Key -> IO (Maybe (BS.ByteString, Word64))
lookup (Tree st n) = I.lookup st n

-- | Produce a proof for the leaf at a key.
--
--   If no leaf exists at the key, this proves its exclusion.
prove :: Tree -> M.Key -> IO M.Proof
prove (Tree st n) k = M.Proof <$> I.prove_aggregate st n k