Aggregate.hs (3138B)
1 {-# OPTIONS_HADDOCK prune #-} 2 3 -- | 4 -- Module: Data.MSSMT.LMDB.Aggregate 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 -- their children's hashes and the sum of their sums, as in Taproot 11 -- Assets, held in LMDB; see "Data.MSSMT.Aggregate". 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.Aggregate ( 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.verify 45 ) where 46 47 import qualified Data.ByteString as BS 48 import Data.MSSMT.Internal (Scheme(..)) 49 import Data.MSSMT.LMDB.Internal (StoreException(..)) 50 import qualified Data.MSSMT.LMDB.Internal as I 51 import qualified Data.MSSMT.Aggregate as M 52 import Data.Word (Word64) 53 import qualified Database.LMDB as L 54 import Prelude hiding (lookup) 55 56 -- | A store of aggregate trees, held in an LMDB environment. 57 newtype Store = Store I.Store 58 59 -- | A named tree in a store. 60 data Tree = Tree !I.Store !BS.ByteString 61 62 -- | Open a store at a path. 63 -- 64 -- LMDB permits one open environment per path per process. 65 open :: FilePath -> L.EnvFlags -> IO Store 66 open path flags = Store <$> I.open Aggregate path flags 67 68 -- | Close a store. 69 close :: Store -> IO () 70 close (Store st) = I.close st 71 72 -- | The tree with a name in a store; empty, if there's none yet. 73 tree :: Store -> BS.ByteString -> Tree 74 tree (Store st) = Tree st 75 76 -- | The tree's root commitment. 77 root :: Tree -> IO M.Commitment 78 root (Tree st n) = I.root st n 79 80 -- | Insert a value and sum at a key, replacing any existing leaf, and 81 -- return the new root. 82 -- 83 -- Returns 'Nothing', changing nothing, if the tree's sum would 84 -- overflow. 85 insert 86 :: Tree -> M.Key -> BS.ByteString -> Word64 -> IO (Maybe M.Commitment) 87 insert (Tree st n) = I.insert st n 88 89 -- | Insert many values and sums, in one transaction, and return the new 90 -- root. 91 -- 92 -- Where a key appears more than once, its last appearance wins, and 93 -- an empty value with zero sum deletes whatever is at its key. 94 -- Returns 'Nothing', changing nothing, if the resulting tree's sum 95 -- would overflow. 96 insert_many 97 :: Tree -> [(M.Key, BS.ByteString, Word64)] -> IO (Maybe M.Commitment) 98 insert_many (Tree st n) = I.insert_many st n 99 100 -- | Delete the leaf at a key, and return the new root. 101 delete :: Tree -> M.Key -> IO M.Commitment 102 delete (Tree st n) = I.delete st n 103 104 -- | Look up the value and sum at a key. 105 lookup :: Tree -> M.Key -> IO (Maybe (BS.ByteString, Word64)) 106 lookup (Tree st n) = I.lookup st n 107 108 -- | Produce a proof for the leaf at a key. 109 -- 110 -- If no leaf exists at the key, this proves its exclusion. 111 prove :: Tree -> M.Key -> IO M.Proof 112 prove (Tree st n) k = M.Proof <$> I.prove_aggregate st n k