mssmt

A high-performance Merkle-Sum Sparse Merkle Tree.
git clone git://git.jtobin.io/mssmt.git
Log | Files | Refs | README | LICENSE | git.jtobin.io

Main.hs (20380B)


      1 {-# LANGUAGE OverloadedStrings #-}
      2 {-# LANGUAGE RecordWildCards #-}
      3 
      4 module Main where
      5 
      6 import Control.Monad (foldM)
      7 import qualified Data.Aeson as A
      8 import Data.Bits (testBit)
      9 import qualified Data.ByteString as BS
     10 import qualified Data.Map.Strict as M
     11 import Data.Maybe (isJust)
     12 import Data.MSSMT
     13 import qualified Data.MSSMT.Aggregate as Agg
     14 import qualified Data.MSSMT.Hash as H
     15 import qualified Data.MSSMT.Internal as I
     16 import Data.Void (Void)
     17 import Data.Word (Word32, Word64)
     18 import Prelude hiding (lookup)
     19 import Test.Tasty
     20 import Test.Tasty.HUnit
     21 import qualified Test.Tasty.QuickCheck as Q
     22 import qualified Reference as R
     23 import qualified Vectors as V
     24 
     25 main :: IO ()
     26 main = do
     27   vs <- traverse load files
     28   defaultMain $ testGroup "mssmt" [
     29       units
     30     , hashing
     31     , properties "split" split_ops
     32     , properties "aggregate" aggregate_ops
     33     , split_proofs
     34     , partial "split" I.Split I.prove_split
     35         (\k t -> I.complete (I.prove_split k t))
     36     , partial "aggregate" I.Aggregate I.prove_aggregate
     37         (\k t -> I.complete (I.prove_aggregate k t))
     38     , structure
     39     , testGroup "taproot-assets vectors" (zipWith V.execute files vs)
     40     ]
     41   where
     42     files = [
     43         "mssmt_tree_proofs.json"
     44       , "mssmt_tree_deletion.json"
     45       , "mssmt_tree_replacement.json"
     46       , "mssmt_tree_error_cases.json"
     47       ]
     48     load :: FilePath -> IO V.Vectors
     49     load f = A.eitherDecodeFileStrict' ("etc/" <> f)
     50       >>= either (\e -> fail (f <> ": " <> e)) pure
     51 
     52 -- units ----------------------------------------------------------------------
     53 
     54 units :: TestTree
     55 units = testGroup "units" [
     56     testCase "keys are 32 bytes" $ do
     57       assertBool "31" (not (isJust (key (BS.replicate 31 0))))
     58       assertBool "32" (isJust (key (BS.replicate 32 0)))
     59       assertBool "33" (not (isJust (key (BS.replicate 33 0))))
     60   , testCase "hashes are 32 bytes" $ do
     61       assertBool "31" (not (isJust (parse_hash (BS.replicate 31 0))))
     62       assertBool "33" (not (isJust (parse_hash (BS.replicate 33 0))))
     63   , Q.testProperty "serialize_hash . parse_hash ~ id" $
     64       Q.forAll (BS.pack <$> Q.vector 32) $ \bs ->
     65         fmap serialize_hash (parse_hash bs) Q.=== Just bs
     66   , testCase "an empty value with zero sum is the empty leaf" $
     67       case key (BS.replicate 32 0) of
     68         Nothing -> assertFailure "key"
     69         Just k -> assertEqual mempty (Just empty) (insert k mempty 0 empty)
     70   , testCase "empty roots agree with the reference" $ do
     71       let Commitment h s = root empty
     72           Commitment g n = Agg.root Agg.empty
     73       assertEqual "split" (R.root M.empty) (serialize_hash h, s)
     74       assertEqual "aggregate" (R.root_aggregate M.empty) (serialize_hash g, n)
     75   , testCase "aggregate proofs have 256 siblings" $
     76       case key (BS.replicate 32 0) of
     77         Nothing -> assertFailure "key"
     78         Just k -> case Agg.insert k "a" 1 Agg.empty of
     79           Nothing -> assertFailure "insert"
     80           Just t -> do
     81             let Agg.Proof ps = Agg.prove k t
     82                 r = Agg.root t
     83             assertEqual "length" 256 (length ps)
     84             assertBool "short" $
     85               not (Agg.verify k "a" 1 (Agg.Proof (drop 1 ps)) r)
     86             assertBool "long" $
     87               not (Agg.verify k "a" 1 (Agg.Proof (ps <> take 1 ps)) r)
     88   , testCase "split proofs run only as deep as their key's slot" $
     89       case key (BS.replicate 32 0) of
     90         Nothing -> assertFailure "key"
     91         Just k -> case insert k "a" 1 empty of
     92           Nothing -> assertFailure "insert"
     93           Just t -> do
     94             let p@(Proof nb ps) = prove k t
     95                 r = root t
     96                 e = root empty
     97             assertEqual "siblings" 0 (length ps)
     98             assertEqual "neighbor" Nothing nb
     99             assertBool "verify" (verify k "a" 1 p r)
    100             assertBool "long" $ not (verify k "a" 1 (Proof nb [e]) r)
    101             assertBool "too long" $
    102               not (verify k "a" 1 (Proof nb (replicate 257 e)) r)
    103   , testCase "proofs whose sums overflow are rejected" $
    104       case key z of
    105         Nothing -> assertFailure "key"
    106         Just k -> do
    107           -- roots over the leaf ("a", 1) at the all-zero key, whose path
    108           -- runs left at every level, and siblings of hash g, the
    109           -- deepest of the given sum and the rest of zero sum, with
    110           -- sums added as words, which wrap
    111           let g = H.unsafe_hash (BS.replicate 32 7)
    112               gb = serialize_hash g
    113               split_root n =
    114                 let h = R.hash_node_split (R.hash_leaf_split z "a" 1) 1 gb n
    115                 in  Commitment (H.unsafe_hash h) (1 + n)
    116               aggregate_root n =
    117                 let up (h, t) gs =
    118                       (R.hash_node_aggregate h gb (t + gs), t + gs)
    119                     (rh, rt) = foldl up (R.hash_leaf_aggregate "a" 1, 1)
    120                       (n : replicate 255 0)
    121                 in  Commitment (H.unsafe_hash rh) rt
    122               split_proof n = Proof Nothing [Commitment g n]
    123               aggregate_proof n =
    124                 Agg.Proof (Commitment g n : replicate 255 (Commitment g 0))
    125               fits = maxBound - 1
    126           assertBool "split, fits" $
    127             verify k "a" 1 (split_proof fits) (split_root fits)
    128           assertBool "split, overflows" . not $
    129             verify k "a" 1 (split_proof maxBound) (split_root maxBound)
    130           assertBool "aggregate, fits" $
    131             Agg.verify k "a" 1 (aggregate_proof fits) (aggregate_root fits)
    132           assertBool "aggregate, overflows" . not $
    133             Agg.verify k "a" 1 (aggregate_proof maxBound)
    134               (aggregate_root maxBound)
    135   ]
    136   where
    137     z = BS.replicate 32 0
    138 
    139 -- hashing --------------------------------------------------------------------
    140 
    141 gen_hash :: Q.Gen Hash
    142 gen_hash = fmap BS.pack (Q.vector 32) `Q.suchThatMap` parse_hash
    143 
    144 -- each hash agrees with the reference; the native paths are checked
    145 -- only where hardware SHA-256 is available
    146 hashing :: TestTree
    147 hashing = testGroup "hashing" $ [
    148       Q.testProperty "hash_leaf_split ~ reference" $
    149         Q.forAll gen_hash $ \k v -> Q.forAll big $ \s ->
    150           serialize_hash (I.hash_leaf_split k (I.hash_value (BS.pack v)) s)
    151             Q.=== R.hash_leaf_split (serialize_hash k) (BS.pack v) s
    152     , Q.testProperty "hash_leaf_aggregate ~ reference" $
    153         \v -> Q.forAll big $ \s ->
    154           serialize_hash (I.hash_leaf_aggregate (BS.pack v) s)
    155             Q.=== R.hash_leaf_aggregate (BS.pack v) s
    156     , split "hash_node_split_pure" I.hash_node_split_pure
    157     , aggregate "hash_node_aggregate_pure" I.hash_node_aggregate_pure
    158     ]
    159   <> [ split "hash_node_split_native" I.hash_node_split_native | I.native ]
    160   <> [ aggregate "hash_node_aggregate_native" I.hash_node_aggregate_native
    161      | I.native
    162      ]
    163   where
    164     split name f = Q.testProperty (name <> " ~ reference") $
    165       Q.forAll gen_hash $ \l -> Q.forAll big $ \sl ->
    166       Q.forAll gen_hash $ \r -> Q.forAll big $ \sr ->
    167         serialize_hash (f l sl r sr)
    168           Q.=== R.hash_node_split (serialize_hash l) sl (serialize_hash r) sr
    169     aggregate name f = Q.testProperty (name <> " ~ reference") $
    170       Q.forAll gen_hash $ \l -> Q.forAll gen_hash $ \r ->
    171       Q.forAll big $ \s ->
    172         serialize_hash (f l r s)
    173           Q.=== R.hash_node_aggregate (serialize_hash l) (serialize_hash r) s
    174 
    175 -- properties -----------------------------------------------------------------
    176 
    177 -- a key with the given prefix
    178 gen_key :: BS.ByteString -> Q.Gen Key
    179 gen_key pre =
    180   fmap ((pre <>) . BS.pack) (Q.vector (32 - BS.length pre))
    181     `Q.suchThatMap` key
    182 
    183 -- a nonempty leaf, with a sum small enough that trees of leaves don't
    184 -- overflow
    185 data E = E !Key !BS.ByteString !Word64
    186   deriving Show
    187 
    188 -- leaves whose keys share a random prefix, so that their paths share
    189 -- up to 256 levels (and keys may collide)
    190 data Es = Es !BS.ByteString [E]
    191   deriving Show
    192 
    193 instance Q.Arbitrary Es where
    194   arbitrary = do
    195     pre <- BS.pack <$> (Q.chooseInt (0, 32) >>= Q.vector)
    196     Es pre <$> Q.listOf (E
    197       <$> gen_key pre
    198       <*> fmap BS.pack (Q.listOf1 Q.arbitrary)
    199       <*> fmap fromIntegral (Q.arbitrary :: Q.Gen Word32))
    200   shrink (Es pre es) = Es pre <$> Q.shrinkList (const []) es
    201 
    202 -- a tree module's operations, and its reference root
    203 data Ops t p = Ops {
    204     o_empty  :: t
    205   , o_insert :: Key -> BS.ByteString -> Word64 -> t -> Maybe t
    206   , o_insert_many :: [(Key, BS.ByteString, Word64)] -> t -> Maybe t
    207   , o_delete :: Key -> t -> t
    208   , o_lookup :: Key -> t -> Maybe (BS.ByteString, Word64)
    209   , o_root   :: t -> Commitment
    210   , o_prove  :: Key -> t -> p
    211   , o_verify :: Key -> BS.ByteString -> Word64 -> p -> Commitment -> Bool
    212   , o_ref    :: R.Leaves -> (BS.ByteString, Word64)
    213   }
    214 
    215 split_ops :: Ops Tree Proof
    216 split_ops = Ops empty insert insert_many delete lookup root prove verify R.root
    217 
    218 aggregate_ops :: Ops Agg.Tree Agg.Proof
    219 aggregate_ops = Ops
    220   Agg.empty Agg.insert Agg.insert_many Agg.delete Agg.lookup Agg.root
    221   Agg.prove Agg.verify R.root_aggregate
    222 
    223 build :: Ops t p -> [E] -> Maybe t
    224 build Ops {..} = foldM (\t (E k v s) -> o_insert k v s t) o_empty
    225 
    226 -- later leaves replace earlier ones
    227 model :: [E] -> M.Map Key (BS.ByteString, Word64)
    228 model es = M.fromList [(k, (v, s)) | E k v s <- es]
    229 
    230 -- a key from the leaves, or a fresh one under their prefix
    231 pick :: Es -> Q.Gen Key
    232 pick (Es pre es) = Q.oneof (gen_key pre : [Q.elements ks | not (null ks)])
    233   where
    234     ks = [k | E k _ _ <- es]
    235 
    236 -- a batch of puts and deletions, at keys from the leaves or fresh ones
    237 -- under their prefix, with sums from the given generator
    238 gen_batch :: Q.Gen Word64 -> Es -> Q.Gen [(Key, BS.ByteString, Word64)]
    239 gen_batch sums x = Q.listOf $ do
    240   k <- pick x
    241   Q.frequency [
    242       (3, (,,) k <$> fmap BS.pack (Q.listOf1 Q.arbitrary) <*> sums)
    243     , (1, pure (k, mempty, 0))
    244     ]
    245 
    246 -- a sum small enough that batches of them don't overflow
    247 small :: Q.Gen Word64
    248 small = fromIntegral <$> (Q.arbitrary :: Q.Gen Word32)
    249 
    250 -- a sum, often near maxBound
    251 big :: Q.Gen Word64
    252 big = Q.oneof [
    253     Q.arbitrary
    254   , (maxBound -) . fromIntegral <$> (Q.arbitrary :: Q.Gen Word32)
    255   ]
    256 
    257 -- a key's raw bytes
    258 raw_key :: Key -> BS.ByteString
    259 raw_key (I.Key h) = I.serialize_hash h
    260 
    261 -- key the model by raw bytes
    262 raw :: M.Map Key a -> M.Map BS.ByteString a
    263 raw = M.mapKeysMonotonic raw_key
    264 
    265 -- path bit at the given depth; LSB-first within each byte
    266 kbit :: Key -> Int -> Bool
    267 kbit k d = testBit (BS.index (raw_key k) (d `quot` 8)) (d `rem` 8)
    268 
    269 -- every branch holds two or more leaves, and every leaf's key follows
    270 -- its path
    271 compact :: I.Tree Void -> Bool
    272 compact = isJust . go 0 [] where
    273   go :: Int -> [Bool] -> I.Tree Void -> Maybe Int
    274   go _ _ I.Empty = Just 0
    275   go d path (I.Leaf _ _ k _)
    276     | reverse path == fmap (kbit k) [0 .. d - 1] = Just 1
    277     | otherwise = Nothing
    278   go d path (I.Node l _ r) = do
    279     a <- go (d + 1) (False : path) l
    280     b <- go (d + 1) (True : path) r
    281     if a + b >= 2 then Just (a + b) else Nothing
    282 
    283 -- whether a key's path ends at another key's leaf
    284 at_leaf :: Key -> I.Tree Void -> Bool
    285 at_leaf k = go 0 where
    286   go :: Int -> I.Tree Void -> Bool
    287   go d t = case t of
    288     I.Empty -> False
    289     I.Leaf _ _ k' _ -> k' /= k
    290     I.Node l _ r -> go (d + 1) (if kbit k d then r else l)
    291 
    292 -- the internal tree underlying a split tree
    293 build_raw :: [E] -> Maybe (I.Tree Void)
    294 build_raw = build_with I.Split
    295 
    296 build_with :: I.Scheme -> [E] -> Maybe (I.Tree Void)
    297 build_with sc =
    298   foldM (\t (E k v s) -> I.complete (I.insert_with sc k v s t)) I.empty
    299 
    300 -- a tree held entirely elsewhere: its root as a stub, and a store of its
    301 -- nodes by commitment, each with its subtrees as stubs
    302 held
    303   :: I.Tree Void
    304   -> (I.Tree I.Kind, M.Map (Hash, Word64) (I.Tree I.Kind))
    305 held t0 = (stub t0, go t0 M.empty) where
    306   stub :: I.Tree Void -> I.Tree I.Kind
    307   stub t = case t of
    308     I.Empty -> I.Empty
    309     I.Leaf c _ _ _ -> I.Stub I.Single c
    310     I.Node _ c _ -> I.Stub I.Branch c
    311   go
    312     :: I.Tree Void -> M.Map (Hash, Word64) (I.Tree I.Kind)
    313     -> M.Map (Hash, Word64) (I.Tree I.Kind)
    314   go t m = case t of
    315     I.Empty -> m
    316     I.Leaf c lh k v -> M.insert (ckey c) (I.Leaf c lh k v) m
    317     I.Node l c r ->
    318       go l (go r (M.insert (ckey c) (I.Node (stub l) c (stub r)) m))
    319 
    320 ckey :: Commitment -> (Hash, Word64)
    321 ckey (Commitment h s) = (h, s)
    322 
    323 -- load a stub's contents from a store
    324 from_store
    325   :: M.Map (Hash, Word64) (I.Tree I.Kind) -> I.Kind -> Commitment
    326   -> Maybe (I.Tree I.Kind)
    327 from_store m _ c = M.lookup (ckey c) m
    328 
    329 with_tree :: Ops t p -> [E] -> (t -> Q.Property) -> Q.Property
    330 with_tree o es f = case build o es of
    331   Nothing -> Q.counterexample "overflow" False
    332   Just t -> f t
    333 
    334 properties :: (Eq t, Show t) => String -> Ops t p -> TestTree
    335 properties name o@Ops {..} = testGroup ("properties (" <> name <> ")") [
    336     Q.testProperty "lookup agrees with a map model" $ \(Es _ es) ->
    337       with_tree o es $ \t ->
    338         Q.property $
    339           all (\(k, l) -> o_lookup k t == Just l) (M.toList (model es))
    340 
    341   , Q.testProperty "root sum is the sum of leaf sums" $ \(Es _ es) ->
    342       with_tree o es $ \t ->
    343         let Commitment _ s = o_root t
    344         in  toInteger s Q.=== sum (fmap (toInteger . snd) (model es))
    345 
    346   , Q.testProperty "root agrees with the reference" $ \(Es _ es) ->
    347       with_tree o es $ \t ->
    348         let Commitment h s = o_root t
    349         in  (serialize_hash h, s) Q.=== o_ref (raw (model es))
    350 
    351   , Q.testProperty "trees are independent of insertion history" $
    352       \(Es _ es) ->
    353         let canonical = [E k v s | (k, (v, s)) <- M.toList (model es)]
    354         in  build o es Q.=== build o canonical
    355 
    356   , Q.testProperty "delete k . insert k v s == delete k" $ \x@(Es _ es) ->
    357       Q.forAll (pick x) $ \k ->
    358         with_tree o es $ \t ->
    359           fmap (o_delete k) (o_insert k "v" 1 t) Q.=== Just (o_delete k t)
    360 
    361   , Q.testProperty "deleting every leaf yields the empty tree" $ \(Es _ es) ->
    362       with_tree o es $ \t ->
    363         foldr o_delete t (M.keys (model es)) Q.=== o_empty
    364 
    365   , Q.testProperty "inclusion proofs verify" $ \(Es _ es) ->
    366       with_tree o es $ \t ->
    367         Q.property $ all
    368           (\(k, (v, s)) -> o_verify k v s (o_prove k t) (o_root t))
    369           (M.toList (model es))
    370 
    371   , Q.testProperty "exclusion proofs verify" $ \x@(Es _ es) ->
    372       Q.forAll (pick x) $ \k ->
    373         M.notMember k (model es) Q.==>
    374           with_tree o es $ \t ->
    375             Q.classify (maybe False (at_leaf k) (build_raw es))
    376               "ends at another leaf" $
    377               o_verify k mempty 0 (o_prove k t) (o_root t)
    378 
    379   , Q.testProperty "proofs reject a wrong value or sum" $ \(Es _ es) ->
    380       with_tree o es $ \t ->
    381         let r = o_root t
    382         in  Q.property $ all
    383               (\(k, (v, s)) ->
    384                    not (o_verify k (v <> "x") s (o_prove k t) r)
    385                 && not (o_verify k v (s + 1) (o_prove k t) r)
    386                 && not (o_verify k mempty 0 (o_prove k t) r))
    387               (M.toList (model es))
    388 
    389   , Q.testProperty "insert_many agrees with successive inserts" $
    390       \x@(Es _ es) -> Q.forAll (gen_batch small x) $ \b ->
    391         with_tree o es $ \t ->
    392           o_insert_many b t
    393             Q.=== foldM (\u (k, v, s) -> o_insert k v s u) t b
    394 
    395   , Q.testProperty "insert_many fails iff the root sum would overflow" $
    396       \x@(Es _ es) -> Q.forAll (gen_batch (Q.oneof [small, big]) x) $ \b ->
    397         with_tree o es $ \t ->
    398           let apply m (k, v, s)
    399                 | BS.null v && s == 0 = M.delete k m
    400                 | otherwise = M.insert k (v, s) m
    401               final = foldl apply (model es) b
    402               fits = sum (fmap (toInteger . snd) final)
    403                   <= toInteger (maxBound :: Word64)
    404           in  Q.classify fits "fits" $ isJust (o_insert_many b t) Q.=== fits
    405 
    406   , Q.testProperty "insert fails iff the root sum would overflow" $
    407       \x@(Es _ es) -> Q.forAll (pick x) $ \k -> Q.forAll big $ \s ->
    408         with_tree o es $ \t ->
    409           let m    = model es
    410               rest = sum (fmap (toInteger . snd) (M.delete k m))
    411               fits = rest + toInteger s <= toInteger (maxBound :: Word64)
    412           in  Q.classify fits "fits" $ isJust (o_insert k "v" s t) Q.=== fits
    413   ]
    414 
    415 -- split proofs, which run only as deep as their key's slot
    416 split_proofs :: TestTree
    417 split_proofs = testGroup "split proofs" [
    418     Q.testProperty "proofs end at their key's slot" $ \x@(Es _ es) ->
    419       Q.forAll (pick x) $ \k ->
    420         with_tree split_ops es $ \t ->
    421           let Proof nb ps = prove k t
    422               neighbor (Neighbor k' _ _) = raw_key k'
    423           in  (length ps, fmap neighbor nb)
    424                 Q.=== R.slot (raw (model es)) (raw_key k)
    425 
    426   , Q.testProperty "a present key's own leaf proves nothing" $
    427       \(Es _ es) ->
    428         with_tree split_ops es $ \t ->
    429           Q.property $ all
    430             (\(k, (v, s)) ->
    431                let Proof _ ps = prove k t
    432                    own = Proof (Just (Neighbor k (I.hash_value v) s)) ps
    433                in     not (verify k mempty 0 own (root t))
    434                    && not (verify k v s own (root t)))
    435             (M.toList (model es))
    436 
    437   , Q.testProperty "exclusion proofs reject a forged neighbor" $
    438       \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
    439         with_tree split_ops es $ \t -> case prove k t of
    440           Proof (Just (Neighbor k' h s)) ps ->
    441             let r = root t
    442                 with n = verify k mempty 0 (Proof (Just n) ps) r
    443             in  Q.property $
    444                      with (Neighbor k' h s)
    445                   && not (with (Neighbor k' h (s + 1)))
    446                   && not (with (Neighbor k h s))
    447           Proof Nothing _ -> Q.discard
    448   ]
    449 
    450 -- operations on a tree held entirely elsewhere, loaded along the paths
    451 -- they touch, agree with those on the tree itself; and report what they
    452 -- need, where they find it held elsewhere
    453 partial
    454   :: (Eq p, Show p)
    455   => String
    456   -> I.Scheme
    457   -> (Key -> I.Tree I.Kind -> Either (I.Missing I.Kind) p)
    458   -> (Key -> I.Tree Void -> p)
    459   -> TestTree
    460 partial name sc pf pf' = testGroup ("partial trees (" <> name <> ")") [
    461     Q.testProperty "lookup" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
    462       with es $ \t (h, m) ->
    463         fmap (I.lookup k) (I.expand (from_store m) k h)
    464           Q.=== Just (Right (I.complete (I.lookup k t)))
    465 
    466   , Q.testProperty "prove" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
    467       with es $ \t (h, m) ->
    468         fmap (pf k) (I.expand (from_store m) k h) Q.=== Just (Right (pf' k t))
    469 
    470   , Q.testProperty "insert" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
    471       with es $ \t (h, m) ->
    472         let ins u = I.insert_with sc k "v" 1 u
    473         in  fmap (fmap (fmap root_of) . ins) (I.expand (from_store m) k h)
    474               Q.=== Just (Right (fmap root_of (I.complete (ins t))))
    475 
    476   , Q.testProperty "delete" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
    477       with es $ \t (h, m) ->
    478         let del u = I.delete_with sc k u
    479         in  fmap (fmap root_of . del) (I.expand (from_store m) k h)
    480               Q.=== Just (Right (root_of (I.complete (del t))))
    481 
    482   , Q.testProperty "insert_many" $ \x@(Es _ es) ->
    483       Q.forAll (gen_batch small x) $ \b ->
    484         with es $ \t (h, m) ->
    485           let ins u = I.insert_many_with sc b u
    486               loaded = foldM (\u (k, _, _) -> I.expand (from_store m) k u) h b
    487           in  fmap (fmap (fmap root_of) . ins) loaded
    488                 Q.=== Just (Right (fmap root_of (I.complete (ins t))))
    489 
    490   , Q.testProperty "unloaded paths report what they need" $
    491       \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
    492         with es $ \_ (h, _) -> case h of
    493           I.Stub p c ->
    494             let needed = Left (I.Missing p c)
    495             in      I.lookup k h Q.=== needed
    496               Q..&&. fmap (const ()) (pf k h) Q.=== needed
    497               Q..&&. fmap (const ()) (I.insert_with sc k "v" 1 h) Q.=== needed
    498               Q..&&. fmap (const ()) (I.delete_with sc k h) Q.=== needed
    499           _ -> Q.property True
    500   ]
    501   where
    502     root_of = I.root_with sc
    503     with es f = case build_with sc es of
    504       Nothing -> Q.counterexample "overflow" False
    505       Just t -> f t (held t)
    506 
    507 -- the shape of the underlying tree, which doesn't depend on the scheme
    508 structure :: TestTree
    509 structure = testGroup "structure" [
    510     Q.testProperty "trees are compact" $ \(Es _ es) ->
    511       Q.forAll (Q.sublistOf (M.keys (model es))) $ \ks ->
    512         case build_raw es of
    513           Nothing -> Q.counterexample "overflow" False
    514           Just t ->
    515             let del k u = I.complete (I.delete_with I.Split k u)
    516             in  compact t Q..&&. compact (foldr del t ks)
    517   ]