Skip to content

Commit dbc91aa

Browse files
committed
Update to mempack-0.2
1 parent 34b4660 commit dbc91aa

File tree

4 files changed

+56
-25
lines changed

4 files changed

+56
-25
lines changed

cabal.project

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,22 @@ if impl (ghc >= 9.10)
5555
-- https://github.com/phadej/regression-simple/pull/14
5656
, regression-simple:base
5757

58-
-- source-repository-package
59-
-- type: git
60-
-- location: https://github.com/lehins/mempack
61-
-- tag: 2f2528780c715afd2f270447359c7cde632f2c49
58+
source-repository-package
59+
type: git
60+
location: https://github.com/lehins/mempack
61+
tag: 2f2528780c715afd2f270447359c7cde632f2c49
62+
--sha256: sha256-OtcXXetYvECE6iofJxVu/Tz+Vk0OGmbS8lTyOJXWmQ0=
63+
64+
source-repository-package
65+
type: git
66+
location: https://github.com/IntersectMBO/cardano-base
67+
tag: 4b2337529c6ae12677461f6fa373e47020455850
68+
--sha256: sha256-RDOM3SGVCfi4eeblh9a+7mtA6Rk7262HiDKsYmaXeBU=
69+
subdir: cardano-crypto-class
70+
71+
source-repository-package
72+
type: git
73+
location: https://github.com/IntersectMBO/cardano-ledger
74+
tag: 8b139be8ffcc1a84d4352beed44ef27a18e179f7
75+
--sha256: sha256-2Srt5YUdRRUhN/TZBShcG9JlldvWywN0DpjjjRHkzaU=
76+
subdir: libs/cardano-ledger-core

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V1/BackingStore/Impl/LMDB/Bridge.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import Database.LMDB.Simple.Cursor (CursorM)
4141
import qualified Database.LMDB.Simple.Cursor as Cursor
4242
import qualified Database.LMDB.Simple.Internal as Internal
4343
import Foreign (Storable (peek, poke), castPtr)
44-
import GHC.Ptr (Ptr (..))
44+
import GHC.Exts
4545
import Ouroboros.Consensus.Util.IndexedMemPack
4646

4747
instance Buffer MDB_val where
@@ -51,6 +51,15 @@ instance Buffer MDB_val where
5151
buffer (MDB_val _ (Ptr addr#)) _ f = f addr#
5252
{-# INLINE buffer #-}
5353

54+
mkBuffer ba# =
55+
MDB_val
56+
(fromIntegral (I# (sizeofByteArray# ba#)))
57+
(Ptr (byteArrayContents# ba#))
58+
{-# INLINE mkBuffer #-}
59+
60+
bufferHasToBePinned = True
61+
{-# INLINE bufferHasToBePinned #-}
62+
5463
{-------------------------------------------------------------------------------
5564
Internal: peek and poke
5665
-------------------------------------------------------------------------------}

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2/LSM.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ toTxOutBytes st txout =
111111
in TxOutBytes $ LSM.RawBytes (VP.Vector 0 (PBA.sizeofByteArray barr) barr)
112112

113113
fromTxOutBytes :: IndexedMemPack (l EmptyMK) (TxOut l) => l EmptyMK -> TxOutBytes -> TxOut l
114-
fromTxOutBytes st (TxOutBytes (LSM.RawBytes (VP.force -> (VP.Vector _ _ barr)))) =
115-
case indexedUnpackLeftOver' st barr of
114+
fromTxOutBytes st (TxOutBytes (LSM.RawBytes vec)) =
115+
case indexedUnpackEither st vec of
116116
Left err ->
117117
error $
118118
unlines
119119
[ "There was an error deserializing a TxOut from the LSM backend."
120120
, "This will likely result in a restart-crash loop."
121121
, "The error: " <> show err
122122
]
123-
Right (v, _) -> v
123+
Right v -> v
124124

125125
instance LSM.SerialiseValue TxOutBytes where
126126
serialiseValue = unTxOutBytes
@@ -140,16 +140,16 @@ toTxInBytes _ txin =
140140
in TxInBytes $ LSM.RawBytes (VP.Vector 0 (PBA.sizeofByteArray barr) barr)
141141

142142
fromTxInBytes :: MemPack (TxIn l) => Proxy l -> TxInBytes -> TxIn l
143-
fromTxInBytes _ (TxInBytes (LSM.RawBytes (VP.force -> (VP.Vector _ _ barr)))) =
144-
case unpackLeftOver' barr of
143+
fromTxInBytes _ (TxInBytes (LSM.RawBytes vec)) =
144+
case unpackEither vec of
145145
Left err ->
146146
error $
147147
unlines
148148
[ "There was an error deserializing a TxIn from the LSM backend."
149149
, "This will likely result in a restart-crash loop."
150150
, "The error: " <> show err
151151
]
152-
Right (v, _) -> v
152+
Right v -> v
153153

154154
instance LSM.SerialiseKey TxInBytes where
155155
serialiseKey = unTxInBytes

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Util/IndexedMemPack.hs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ module Ouroboros.Consensus.Util.IndexedMemPack
1717
, indexedPackByteArray
1818
, indexedUnpackError
1919
, indexedUnpack
20-
, indexedUnpackLeftOver'
21-
, unpackLeftOver'
20+
, indexedUnpackEither
21+
, unpackEither
2222
) where
2323

2424
import qualified Control.Monad as Monad
25-
import Control.Monad.Trans.Fail (Fail, errorFail, failT, runFailAgg)
25+
import Control.Monad.ST
26+
import Control.Monad.Trans.Fail
2627
import Data.Array.Byte (ByteArray (..))
2728
import Data.Bifunctor (first)
2829
import Data.ByteString
@@ -35,7 +36,7 @@ import GHC.Stack
3536
class IndexedMemPack idx a where
3637
indexedPackedByteCount :: idx -> a -> Int
3738
indexedPackM :: idx -> a -> Pack s ()
38-
indexedUnpackM :: Buffer b => idx -> Unpack b a
39+
indexedUnpackM :: Buffer b => forall s. idx -> Unpack s b a
3940
indexedTypeName :: idx -> String
4041

4142
indexedPackByteString ::
@@ -82,24 +83,30 @@ indexedUnpackFail idx b = do
8283
indexedUnpackLeftOver ::
8384
forall idx a b.
8485
(IndexedMemPack idx a, Buffer b, HasCallStack) => idx -> b -> Fail SomeError (a, Int)
85-
indexedUnpackLeftOver idx b = do
86+
indexedUnpackLeftOver idx b = FailT $ pure $ runST $ runFailAggT $ indexedUnpackLeftOverST idx b
87+
{-# INLINEABLE indexedUnpackLeftOver #-}
88+
89+
indexedUnpackLeftOverST ::
90+
forall idx a b s.
91+
(IndexedMemPack idx a, Buffer b, HasCallStack) => idx -> b -> FailT SomeError (ST s) (a, Int)
92+
indexedUnpackLeftOverST idx b = do
8693
let len = bufferByteCount b
8794
res@(_, consumedBytes) <- runStateT (runUnpack (indexedUnpackM idx) b) 0
8895
Monad.when (consumedBytes > len) $ errorLeftOver (indexedTypeName @idx @a idx) consumedBytes len
8996
pure res
90-
{-# INLINEABLE indexedUnpackLeftOver #-}
97+
{-# INLINEABLE indexedUnpackLeftOverST #-}
9198

92-
indexedUnpackLeftOver' ::
99+
indexedUnpackEither ::
93100
forall idx a b.
94-
(IndexedMemPack idx a, Buffer b, HasCallStack) => idx -> b -> Either SomeError (a, Int)
95-
indexedUnpackLeftOver' idx = first fromMultipleErrors . runFailAgg . indexedUnpackLeftOver idx
96-
{-# INLINEABLE indexedUnpackLeftOver' #-}
101+
(IndexedMemPack idx a, Buffer b, HasCallStack) => idx -> b -> Either SomeError a
102+
indexedUnpackEither idx = first fromMultipleErrors . runFailAgg . indexedUnpackFail idx
103+
{-# INLINEABLE indexedUnpackEither #-}
97104

98-
unpackLeftOver' ::
105+
unpackEither ::
99106
forall a b.
100-
(MemPack a, Buffer b, HasCallStack) => b -> Either SomeError (a, Int)
101-
unpackLeftOver' = first fromMultipleErrors . runFailAgg . unpackLeftOver
102-
{-# INLINEABLE unpackLeftOver' #-}
107+
(MemPack a, Buffer b, HasCallStack) => b -> Either SomeError a
108+
unpackEither = first fromMultipleErrors . runFailAgg . unpackFail
109+
{-# INLINEABLE unpackEither #-}
103110

104111
errorLeftOver :: HasCallStack => String -> Int -> Int -> a
105112
errorLeftOver name consumedBytes len =

0 commit comments

Comments
 (0)