Skip to content

Commit 6b7f00c

Browse files
committed
Further integration
1 parent 2f269eb commit 6b7f00c

File tree

33 files changed

+364
-146
lines changed

33 files changed

+364
-146
lines changed

cabal.project

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ packages:
2525
ouroboros-consensus-diffusion
2626
sop-extras
2727
strict-sop-core
28-
../lsm-tree
2928

3029
-- We want to always build the test-suites and benchmarks
3130
tests: true
@@ -119,7 +118,7 @@ source-repository-package
119118
source-repository-package
120119
type: git
121120
location: https://github.com/jasagredo/lsm-tree
122-
tag: fa0d862b7a609473538a0007bd0df314cd2fa635
121+
tag: 55c4b99e3e569c168de38f6d351507236d027f49
123122
subdir:
124-
123+
.
125124
blockio

ouroboros-consensus-cardano/app/snapshot-converter.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ load config@Config{inpath = pathToDiskSnapshot -> Just (fs@(SomeHasFS hasFS), pa
200200
checkSnapshotFileStructure Mem path fs
201201
(ls, _) <- withExceptT SnapshotError $ V2.loadSnapshot nullTracer rr ccfg fs ds
202202
let h = V2.currentHandle ls
203-
(V2.state h,) <$> Trans.lift (V2.readAll (V2.tables h))
203+
(V2.state h,) <$> Trans.lift (V2.readAll (V2.tables h) (V2.state h))
204204
LMDB -> do
205205
checkSnapshotFileStructure LMDB path fs
206206
((dbch, k, bstore), _) <-

ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ library
175175
sop-extras ^>=0.4,
176176
strict-sop-core ^>=0.1,
177177
primitive,
178-
FailT,
179178
text,
180179
these ^>=1.2,
181180
validation,
@@ -403,6 +402,7 @@ library unstable-cardano-testlib
403402

404403
build-depends:
405404
QuickCheck,
405+
lsm-tree,
406406
base,
407407
cardano-crypto-class,
408408
cardano-crypto-wrapper,

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/ByronHFC.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{-# LANGUAGE StandaloneDeriving #-}
1212
{-# LANGUAGE TypeApplications #-}
1313
{-# LANGUAGE TypeFamilies #-}
14+
{-# LANGUAGE UndecidableInstances #-}
1415
{-# OPTIONS_GHC -Wno-orphans #-}
1516

1617
module Ouroboros.Consensus.Byron.ByronHFC
@@ -294,7 +295,7 @@ instance HasCanonicalTxIn '[ByronBlock] where
294295
{ getByronHFCTxIn :: Void
295296
}
296297
deriving stock (Show, Eq, Ord)
297-
deriving newtype (NoThunks, MemPack, LSM.SerialiseKey, LSM.SerialiseValue, LSM.ResolveValue)
298+
deriving newtype (NoThunks, MemPack, LSM.SerialiseKey)
298299

299300
injectCanonicalTxIn IZ key = absurd key
300301
injectCanonicalTxIn (IS idx') _ = case idx' of {}
@@ -313,9 +314,13 @@ deriving via
313314
instance
314315
IndexedMemPack (LedgerState (HardForkBlock '[ByronBlock]) EmptyMK) Void
315316

316-
instance LSMOrder (LedgerState (HardForkBlock '[ByronBlock])) where
317-
toLSMOrder _ [] = []
318-
toLSMOrder _ (x : _) = absurd . getByronHFCTxIn $ x
317+
type instance
318+
LSMTxOut (LedgerState (HardForkBlock '[ByronBlock])) =
319+
TxOut (LedgerState (HardForkBlock '[ByronBlock]))
320+
321+
instance ToLSMTxOut (LedgerState (HardForkBlock '[ByronBlock])) where
322+
toLSMTxOut _ = id
323+
fromLSMTxOut _ = id
319324

320325
instance BlockSupportsHFLedgerQuery '[ByronBlock] where
321326
answerBlockQueryHFLookup IZ _cfg (q :: BlockQuery ByronBlock QFLookupTables result) _dlv = case q of {}

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Ledger.hs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
{-# LANGUAGE ScopedTypeVariables #-}
1515
{-# LANGUAGE StandaloneDeriving #-}
1616
{-# LANGUAGE TypeFamilies #-}
17+
{-# LANGUAGE UndecidableInstances #-}
1718
{-# OPTIONS_GHC -Wno-orphans #-}
1819

1920
-- | Instances requires for consensus/ledger integration
@@ -70,8 +71,7 @@ import qualified Control.State.Transition.Extended as STS
7071
import Data.ByteString (ByteString)
7172
import Data.Map.Strict (Map)
7273
import qualified Data.Map.Strict as Map
73-
import Data.Void (Void, absurd)
74-
import qualified Database.LSMTree as LSM
74+
import Data.Void (Void)
7575
import GHC.Generics (Generic)
7676
import NoThunks.Class (NoThunks)
7777
import Ouroboros.Consensus.Block
@@ -204,16 +204,11 @@ instance IsLedger (LedgerState ByronBlock) where
204204

205205
type instance TxIn (LedgerState ByronBlock) = Void
206206
type instance TxOut (LedgerState ByronBlock) = Void
207+
type instance LSMTxOut (LedgerState ByronBlock) = TxOut (LedgerState ByronBlock)
207208

208-
instance LSM.SerialiseKey Void where
209-
serialiseKey = absurd
210-
deserialiseKey = error "deserialiseKey: Void"
211-
212-
deriving via LSM.ResolveViaSemigroup Void instance LSM.ResolveValue Void
213-
214-
instance LSMOrder (LedgerState ByronBlock) where
215-
toLSMOrder _ [] = []
216-
toLSMOrder _ (x : _) = absurd x
209+
instance ToLSMTxOut (LedgerState ByronBlock) where
210+
toLSMTxOut _ = id
211+
fromLSMTxOut _ = id
217212

218213
instance LedgerTablesAreTrivial (LedgerState ByronBlock) where
219214
convertMapKind (ByronLedgerState x y z) = ByronLedgerState x y z

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/Ledger.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE DeriveAnyClass #-}
55
{-# LANGUAGE DeriveGeneric #-}
66
{-# LANGUAGE DerivingStrategies #-}
7+
{-# LANGUAGE DerivingVia #-}
78
{-# LANGUAGE EmptyCase #-}
89
{-# LANGUAGE FlexibleContexts #-}
910
{-# LANGUAGE FlexibleInstances #-}
@@ -13,6 +14,7 @@
1314
{-# LANGUAGE MultiParamTypeClasses #-}
1415
{-# LANGUAGE RankNTypes #-}
1516
{-# LANGUAGE ScopedTypeVariables #-}
17+
{-# LANGUAGE StandaloneDeriving #-}
1618
{-# LANGUAGE TypeApplications #-}
1719
{-# LANGUAGE TypeFamilies #-}
1820
{-# LANGUAGE UndecidableInstances #-}
@@ -116,23 +118,20 @@ data CardanoTxOut c
116118
deriving stock (Show, Eq, Generic)
117119
deriving anyclass NoThunks
118120

119-
-- TODO
120-
instance CardanoHardForkConstraints c => LSM.SerialiseValue (CardanoTxOut c) where
121-
serialiseValue txout =
122-
let barr =
123-
indexedPackByteArray
124-
False
125-
(undefined :: (LedgerState (HardForkBlock (CardanoEras c)) EmptyMK)) -- unused
126-
txout
127-
in LSM.RawBytes (Vector 0 (PBA.sizeofByteArray barr) barr)
128-
deserialiseValue (LSM.RawBytes (Vector _ _ barr)) =
129-
indexedUnpackError (undefined :: (LedgerState (HardForkBlock (CardanoEras c)) EmptyMK)) barr
121+
type instance LSMTxOut (LedgerState (CardanoBlock c)) = LSM.RawBytes
122+
123+
instance LSM.SerialiseValue LSM.RawBytes where
124+
serialiseValue = id
125+
deserialiseValue = id
130126

131-
instance CardanoHardForkConstraints c => LSM.ResolveValue (CardanoTxOut c) where
132-
resolve = undefined
127+
deriving via LSM.ResolveAsFirst LSM.RawBytes instance LSM.ResolveValue LSM.RawBytes
133128

134-
instance LSMOrder (LedgerState (HardForkBlock (CardanoEras c))) where
135-
toLSMOrder _ = undefined
129+
instance CardanoHardForkConstraints c => ToLSMTxOut (LedgerState (CardanoBlock c)) where
130+
toLSMTxOut _ txout =
131+
let barr = eliminateCardanoTxOut (const pack) txout
132+
in LSM.RawBytes (Vector 0 (PBA.sizeofByteArray barr) barr)
133+
fromLSMTxOut st (LSM.RawBytes (Vector _ _ barr)) =
134+
indexedUnpackError st barr
136135

137136
-- | Eliminate the wrapping of CardanoTxOut with the provided function. Similar
138137
-- to 'hcimap' on an 'NS'.

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Eras.hs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
22
{-# LANGUAGE DataKinds #-}
3+
{-# LANGUAGE DerivingVia #-}
34
{-# LANGUAGE FlexibleContexts #-}
45
{-# LANGUAGE FlexibleInstances #-}
56
{-# LANGUAGE GADTs #-}
67
{-# LANGUAGE LambdaCase #-}
78
{-# LANGUAGE MultiParamTypeClasses #-}
89
{-# LANGUAGE ScopedTypeVariables #-}
10+
{-# LANGUAGE StandaloneDeriving #-}
911
{-# LANGUAGE TypeApplications #-}
1012
{-# LANGUAGE TypeFamilies #-}
1113
{-# LANGUAGE TypeOperators #-}
@@ -77,11 +79,13 @@ import Control.Monad.Except
7779
import Control.State.Transition (PredicateFailure)
7880
import Data.Data (Proxy (Proxy))
7981
import Data.List.NonEmpty (NonEmpty ((:|)))
82+
import qualified Database.LSMTree as LSM
8083
import NoThunks.Class (NoThunks)
8184
import Ouroboros.Consensus.Ledger.SupportsMempool
8285
( WhetherToIntervene (..)
8386
)
8487
import Ouroboros.Consensus.Protocol.TPraos (StandardCrypto)
88+
import Ouroboros.Consensus.Storage.LedgerDB.V2.LSM
8589

8690
{-------------------------------------------------------------------------------
8791
Eras instantiated with standard crypto
@@ -404,3 +408,34 @@ instance Core.TranslateEra ConwayEra WrapTx where
404408
. Core.translateEra @ConwayEra ctxt
405409
. Conway.Tx
406410
. unwrapTx
411+
412+
{-------------------------------------------------------------------------------
413+
SerialiseValue
414+
-------------------------------------------------------------------------------}
415+
416+
instance LSM.SerialiseValue (SL.ShelleyTxOut ShelleyEra) where
417+
serialiseValue = serialiseLSMViaMemPack
418+
deserialiseValue = deserialiseLSMViaMemPack
419+
420+
deriving via
421+
LSM.ResolveAsFirst (SL.ShelleyTxOut ShelleyEra)
422+
instance
423+
LSM.ResolveValue (SL.ShelleyTxOut ShelleyEra)
424+
425+
-- instance LSM.SerialiseValue (SL.ShelleyTxOut AllegraEra) where
426+
-- serialiseValue = serialiseLSMViaMemPack
427+
-- deserialiseValue = deserialiseLSMViaMemPack
428+
429+
-- deriving via
430+
-- LSM.ResolveAsFirst (SL.ShelleyTxOut AllegraEra)
431+
-- instance
432+
-- LSM.ResolveValue (SL.ShelleyTxOut AllegraEra)
433+
434+
-- instance LSM.SerialiseValue (SL.ShelleyTxOut MaryEra) where
435+
-- serialiseValue = serialiseLSMViaMemPack
436+
-- deserialiseValue = deserialiseLSMViaMemPack
437+
438+
-- deriving via
439+
-- LSM.ResolveAsFirst (SL.ShelleyTxOut MaryEra)
440+
-- instance
441+
-- LSM.ResolveValue (SL.ShelleyTxOut MaryEra)

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Ledger.hs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{-# LANGUAGE DeriveAnyClass #-}
44
{-# LANGUAGE DeriveGeneric #-}
55
{-# LANGUAGE DerivingStrategies #-}
6+
{-# LANGUAGE DerivingVia #-}
67
{-# LANGUAGE DisambiguateRecordFields #-}
78
{-# LANGUAGE FlexibleContexts #-}
89
{-# LANGUAGE FlexibleInstances #-}
@@ -96,12 +97,9 @@ import Codec.Serialise (decode, encode)
9697
import Control.Arrow (left, second)
9798
import qualified Control.Exception as Exception
9899
import Control.Monad.Except
99-
import Control.Monad.Trans.Fail
100100
import qualified Control.State.Transition.Extended as STS
101-
import qualified Data.Array.Byte as BA
102101
import Data.Coerce (coerce)
103102
import Data.Functor.Identity
104-
import qualified Data.List as L
105103
import Data.MemPack
106104
import Data.Primitive.ByteArray as PBA
107105
import qualified Data.Text as T
@@ -338,25 +336,16 @@ instance MemPack LSMTxIn where
338336
pure . LSMTxIn $ SL.TxIn txid txix
339337

340338
instance LSM.SerialiseKey SL.TxIn where
341-
serialiseKey txin =
342-
let barr = pack $ LSMTxIn txin
343-
in LSM.RawBytes (Vector 0 (PBA.sizeofByteArray barr) barr)
344-
deserialiseKey (LSM.RawBytes (Vector _ _ barr)) = lsmTxIn . unpackError $ barr
345-
346-
instance LSMOrder (LedgerState (ShelleyBlock proto era)) where
347-
toLSMOrder _ =
348-
L.sortBy
349-
( \(SL.TxIn id1 ix1) (SL.TxIn id2 ix2) ->
350-
case compare ix1 ix2 of
351-
EQ -> compare id1 id2
352-
x -> x
353-
)
339+
serialiseKey = serialiseLSMViaMemPack . LSMTxIn
340+
deserialiseKey = lsmTxIn . deserialiseLSMViaMemPack
341+
342+
type instance
343+
LSMTxOut (LedgerState (ShelleyBlock proto era)) =
344+
TxOut (LedgerState (ShelleyBlock proto era))
354345

355-
-- instance (txout ~ Core.TxOut era, MemPack txout) => LSM.SerialiseValue txout where
356-
-- serialiseValue txout =
357-
-- let barr = pack txout
358-
-- in LSM.RawBytes (Vector 0 (PBA.sizeofByteArray barr) barr)
359-
-- deserialiseValue (LSM.RawBytes (Vector _ _ barr)) = unpackError barr
346+
instance ToLSMTxOut (LedgerState (ShelleyBlock proto era)) where
347+
toLSMTxOut _ = id
348+
fromLSMTxOut _ = id
360349

361350
instance
362351
(txout ~ Core.TxOut era, MemPack txout) =>

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/ShelleyHFC.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,13 @@ instance
441441
ejectCanonicalTxIn IZ txIn = getShelleyBlockHFCTxIn txIn
442442
ejectCanonicalTxIn (IS idx') _ = case idx' of {}
443443

444-
instance LSMOrder (LedgerState (HardForkBlock '[ShelleyBlock proto era])) where
445-
toLSMOrder _ =
446-
map ShelleyBlockHFCTxIn
447-
. toLSMOrder (Proxy @(LedgerState (ShelleyBlock proto era)))
448-
. map getShelleyBlockHFCTxIn
444+
type instance
445+
LSMTxOut (LedgerState (HardForkBlock '[ShelleyBlock proto era])) =
446+
TxOut (LedgerState (HardForkBlock '[ShelleyBlock proto era]))
447+
448+
instance ToLSMTxOut (LedgerState (HardForkBlock '[ShelleyBlock proto era])) where
449+
toLSMTxOut _ = id
450+
fromLSMTxOut _ = id
449451

450452
{-------------------------------------------------------------------------------
451453
HardForkTxOut

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/Infra/ShelleyBasedHardFork.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import Data.SOP.Strict
6767
import qualified Data.SOP.Tails as Tails
6868
import qualified Data.SOP.Telescope as Telescope
6969
import Data.Void (Void)
70+
import qualified Database.LSMTree as LSM
7071
import Lens.Micro ((^.))
7172
import NoThunks.Class (NoThunks)
7273
import Ouroboros.Consensus.Block.Forging (BlockForging)
@@ -95,6 +96,7 @@ import Ouroboros.Consensus.Shelley.Ledger
9596
import Ouroboros.Consensus.Shelley.Node
9697
import Ouroboros.Consensus.Shelley.Protocol.Abstract (ProtoCrypto)
9798
import Ouroboros.Consensus.Storage.LedgerDB
99+
import Ouroboros.Consensus.Storage.LedgerDB.V2.LSM
98100
import Ouroboros.Consensus.TypeFamilyWrappers
99101
import Ouroboros.Consensus.Util (eitherToMaybe)
100102
import Ouroboros.Consensus.Util.IOLike (IOLike)
@@ -513,6 +515,21 @@ deriving newtype instance
513515
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
514516
MemPack (CanonicalTxIn (ShelleyBasedHardForkEras proto1 era1 proto2 era2))
515517

518+
type instance
519+
LSMTxOut (LedgerState (HardForkBlock (ShelleyBasedHardForkEras proto1 era1 proto2 era2))) =
520+
TxOut (LedgerState (HardForkBlock (ShelleyBasedHardForkEras proto1 era1 proto2 era2)))
521+
522+
instance ToLSMTxOut (LedgerState (HardForkBlock (ShelleyBasedHardForkEras proto1 era1 proto2 era2))) where
523+
toLSMTxOut _ = id
524+
fromLSMTxOut _ = id
525+
526+
instance
527+
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
528+
LSM.SerialiseKey (CanonicalTxIn (ShelleyBasedHardForkEras proto1 era1 proto2 era2))
529+
where
530+
serialiseKey = serialiseLSMViaMemPack
531+
deserialiseKey = deserialiseLSMViaMemPack
532+
516533
instance
517534
ShelleyBasedHardForkConstraints proto1 era1 proto2 era2 =>
518535
HasHardForkTxOut (ShelleyBasedHardForkEras proto1 era1 proto2 era2)

0 commit comments

Comments
 (0)