commit ee5d011966e3d2dd4f3f5740b39bb9878f24e343
parent a44d12458da04e27cfa4f45ec38107171e1c6220
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 4 Nov 2026 23:15:22 +0400
lib: adapt to mssmt's Tree s
Stores hold Tree Kind, and load only operations' own paths, mssmt no
longer requiring the single leaves beside a deletion's path to be
loaded. Operations' Missing results, which loading their paths first
rules out, are raised as an Unloaded StoreException.
Diffstat:
2 files changed, 42 insertions(+), 38 deletions(-)
diff --git a/flake.lock b/flake.lock
@@ -41,11 +41,11 @@
]
},
"locked": {
- "lastModified": 1793817010,
- "narHash": "sha256-4yU/PgQwloSN/zePgY2+FRhBt+uoDet+ewGbdoanXaU=",
+ "lastModified": 1793819486,
+ "narHash": "sha256-rL7DwlwfsKpPUQRpaIf0egXkQurNUxxCtdWzalL5PGo=",
"ref": "refs/heads/master",
- "rev": "485b72aab2e5246dc778ae04f0dbd024ba65f36a",
- "revCount": 22,
+ "rev": "2cd79d2aeea095a10ddd11de572309ee2b3f377a",
+ "revCount": 24,
"type": "git",
"url": "file:///Users/jtobin/src/mssmt"
},
diff --git a/lib/Data/MSSMT/LMDB/Internal.hs b/lib/Data/MSSMT/LMDB/Internal.hs
@@ -85,6 +85,9 @@ data StoreException =
| BadNode !Hash
-- | A tree's root record is malformed.
| BadRoot !BS.ByteString
+ -- | An operation met a node it hadn't loaded. Operations load their
+ -- paths before running, so this indicates a bug.
+ | Unloaded !Hash
deriving (Eq, Show)
instance Exception StoreException
@@ -128,16 +131,16 @@ leaf_record (Commitment _ s) lh (Key k) v =
copyBytes (p `plusPtr` 73) (castPtr q) (BS.length v)
-- | A branch's record, given its children.
-branch_record :: I.Tree -> I.Tree -> BS.ByteString
+branch_record :: I.Tree Kind -> I.Tree Kind -> BS.ByteString
branch_record l r = BI.unsafeCreate 83 $ \p -> do
pokeByteOff p 0 (1 :: Word8)
poke_child p 1 l
poke_child p 42 r
-root_record :: I.Tree -> BS.ByteString
+root_record :: I.Tree Kind -> BS.ByteString
root_record t = BI.unsafeCreate 41 $ \p -> poke_child p 0 t
-poke_child :: Ptr Word8 -> Int -> I.Tree -> IO ()
+poke_child :: Ptr Word8 -> Int -> I.Tree Kind -> IO ()
poke_child p i t = case t of
I.Empty -> do
pokeByteOff p i (0 :: Word8)
@@ -186,7 +189,7 @@ commitment_at bs i =
Commitment (unsafe_hash (BU.unsafeDrop i bs)) (be64_at bs (i + 32))
-- a child at an offset, as a stub; callers check the length
-child_at :: BS.ByteString -> Int -> Maybe I.Tree
+child_at :: BS.ByteString -> Int -> Maybe (I.Tree Kind)
child_at bs i = case BU.unsafeIndex bs i of
0 -> Just I.Empty
1 -> Just (I.Stub Single (commitment_at bs (i + 1)))
@@ -195,7 +198,7 @@ child_at bs i = case BU.unsafeIndex bs i of
-- | A node from its record, given its commitment, with its subtrees as
-- stubs.
-decode :: Commitment -> BS.ByteString -> Maybe I.Tree
+decode :: Commitment -> BS.ByteString -> Maybe (I.Tree Kind)
decode c bs
| n == 83 && tag == 1 = do
l <- child_at bs 1
@@ -213,7 +216,7 @@ decode c bs
n = BS.length bs
tag = if n == 0 then 255 else BU.unsafeIndex bs 0
-decode_root :: BS.ByteString -> Maybe I.Tree
+decode_root :: BS.ByteString -> Maybe (I.Tree Kind)
decode_root bs
| BS.length bs == 41 = case child_at bs 0 of
Just I.Empty -> Nothing
@@ -225,7 +228,7 @@ decode_root bs
-- load a stub's node from a store, noting its hash
loader
:: Store -> L.Txn t -> IORef (S.Set Hash) -> Kind -> Commitment
- -> IO I.Tree
+ -> IO (I.Tree Kind)
loader st txn seen _ c@(Commitment h _) = do
mr <- L.get txn (store_nodes st) (I.serialize_hash h)
case mr of
@@ -237,7 +240,7 @@ loader st txn seen _ c@(Commitment h _) = do
pure t
-- a tree's root, as a stub
-stored_root :: Store -> L.Txn t -> BS.ByteString -> IO I.Tree
+stored_root :: Store -> L.Txn t -> BS.ByteString -> IO (I.Tree Kind)
stored_root st txn name = do
mr <- L.get txn (store_roots st) name
case mr of
@@ -245,7 +248,7 @@ stored_root st txn name = do
Just r -> maybe (throwIO (BadRoot name)) pure (decode_root r)
-- write the nodes of a tree that weren't loaded from the store
-persist :: Store -> L.Txn L.RW -> S.Set Hash -> I.Tree -> IO ()
+persist :: Store -> L.Txn L.RW -> S.Set Hash -> I.Tree Kind -> IO ()
persist st txn seen = go where
go t = case t of
I.Empty -> pure ()
@@ -264,25 +267,27 @@ persist st txn seen = go where
-- the tree at a name, with the paths to some keys loaded
loaded
- :: Store -> L.Txn t -> IORef (S.Set Hash) -> Bool -> BS.ByteString
- -> [Key] -> IO I.Tree
-loaded st txn seen deleting name ks = do
+ :: Store -> L.Txn t -> IORef (S.Set Hash) -> BS.ByteString -> [Key]
+ -> IO (I.Tree Kind)
+loaded st txn seen name ks = do
r <- stored_root st txn name
- foldM (\t k -> expander (loader st txn seen) k t) r ks
- where
- expander
- | deleting && store_scheme st == Aggregate = I.expand_deleting
- | otherwise = I.expand
+ foldM (\t k -> I.expand (loader st txn seen) k t) r ks
+
+-- the result of an operation on a tree with its paths loaded
+settled :: Either (I.Missing Kind) a -> IO a
+settled = either (\(I.Missing _ (Commitment h _)) -> throwIO (Unloaded h)) pure
-- apply an update to the tree at a name, loading the paths to some keys
-- first, and writing what it creates
update_in
- :: Store -> L.Txn L.RW -> BS.ByteString -> Bool -> [Key]
- -> (I.Tree -> Maybe I.Tree) -> IO (Maybe Commitment)
-update_in st txn name deleting ks f = do
+ :: Store -> L.Txn L.RW -> BS.ByteString -> [Key]
+ -> (I.Tree Kind -> Either (I.Missing Kind) (Maybe (I.Tree Kind)))
+ -> IO (Maybe Commitment)
+update_in st txn name ks f = do
seen <- newIORef S.empty
- t <- loaded st txn seen deleting name ks
- case f t of
+ t <- loaded st txn seen name ks
+ r <- settled (f t)
+ case r of
Nothing -> pure Nothing
Just t' -> do
s <- readIORef seen
@@ -303,22 +308,21 @@ insert_in
:: Store -> L.Txn L.RW -> BS.ByteString -> Key -> BS.ByteString -> Word64
-> IO (Maybe Commitment)
insert_in st txn name k v s =
- update_in st txn name (vacant v s) [k]
- (I.insert_with (store_scheme st) k v s)
+ update_in st txn name [k] (I.insert_with (store_scheme st) k v s)
-- | 'insert_many', within a transaction.
insert_many_in
:: Store -> L.Txn L.RW -> BS.ByteString -> [(Key, BS.ByteString, Word64)]
-> IO (Maybe Commitment)
insert_many_in st txn name kvs =
- update_in st txn name (any (\(_, v, s) -> vacant v s) kvs)
- [k | (k, _, _) <- kvs] (I.insert_many_with (store_scheme st) kvs)
+ update_in st txn name [k | (k, _, _) <- kvs]
+ (I.insert_many_with (store_scheme st) kvs)
-- | 'delete', within a transaction.
delete_in :: Store -> L.Txn L.RW -> BS.ByteString -> Key -> IO Commitment
delete_in st txn name k = do
- mr <- update_in st txn name True [k]
- (Just . I.delete_with (store_scheme st) k)
+ mr <- update_in st txn name [k]
+ (fmap Just . I.delete_with (store_scheme st) k)
maybe (root_in st txn name) pure mr
-- | 'lookup', within a transaction.
@@ -327,7 +331,8 @@ lookup_in
-> IO (Maybe (BS.ByteString, Word64))
lookup_in st txn name k = do
seen <- newIORef S.empty
- I.lookup k <$> loaded st txn seen False name [k]
+ t <- loaded st txn seen name [k]
+ settled (I.lookup k t)
-- | 'prove_split', within a transaction.
prove_split_in
@@ -335,17 +340,16 @@ prove_split_in
-> IO (Maybe Neighbor, [Commitment])
prove_split_in st txn name k = do
seen <- newIORef S.empty
- I.prove_split k <$> loaded st txn seen False name [k]
+ t <- loaded st txn seen name [k]
+ settled (I.prove_split k t)
-- | 'prove_aggregate', within a transaction.
prove_aggregate_in
:: Store -> L.Txn t -> BS.ByteString -> Key -> IO [Commitment]
prove_aggregate_in st txn name k = do
seen <- newIORef S.empty
- I.prove_aggregate k <$> loaded st txn seen False name [k]
-
-vacant :: BS.ByteString -> Word64 -> Bool
-vacant v s = BS.null v && s == 0
+ t <- loaded st txn seen name [k]
+ settled (I.prove_aggregate k t)
-- trees ----------------------------------------------------------------------