1
1
{-# LANGUAGE DataKinds #-}
2
+ {-# LANGUAGE DerivingStrategies #-}
2
3
{-# LANGUAGE FlexibleContexts #-}
3
4
{-# LANGUAGE GADTs #-}
4
5
{-# LANGUAGE LambdaCase #-}
7
8
{-# LANGUAGE ScopedTypeVariables #-}
8
9
{-# LANGUAGE TypeApplications #-}
9
10
11
+ {-# OPTIONS_GHC -Wwarn #-}
12
+
10
13
module Cardano.Tools.N2NPG.Run (
11
14
Opts (.. )
15
+ , StartFrom (.. )
12
16
, run
13
17
) where
14
18
@@ -19,6 +23,7 @@ import Control.Monad.Class.MonadSay (MonadSay (..))
19
23
import Control.Monad.Cont
20
24
import Control.Monad.Trans (MonadTrans (.. ))
21
25
import Control.Tracer (nullTracer , stdoutTracer )
26
+ import Data.ByteString (ByteString )
22
27
import qualified Data.ByteString.Lazy as BL
23
28
import Data.Functor ((<&>) )
24
29
import Data.Functor.Contravariant ((>$<) )
@@ -31,6 +36,7 @@ import Network.TypedProtocol (N (..), Nat (..), PeerHasAgency (..),
31
36
PeerPipelined (.. ), PeerReceiver (.. ), PeerRole (.. ),
32
37
PeerSender (.. ), natToInt )
33
38
import Ouroboros.Consensus.Block
39
+ import Ouroboros.Consensus.Cardano.Block
34
40
import Ouroboros.Consensus.Config
35
41
import Ouroboros.Consensus.Config.SupportsNode
36
42
(ConfigSupportsNode (.. ))
@@ -84,29 +90,45 @@ import System.FS.IO (ioHasFS)
84
90
85
91
data Opts = Opts {
86
92
configFile :: FilePath
87
- , immutableDBDir :: FilePath
93
+ , immutableDBDir :: Maybe FilePath
88
94
, serverAddr :: (Socket. HostName , Socket. ServiceName )
89
- , startSlots :: [SlotNo ]
95
+ , startFrom :: [StartFrom ]
90
96
, numBlocks :: Word64
91
97
}
98
+ deriving stock (Show )
99
+
100
+ data StartFrom =
101
+ -- | Start from a specific slot number. We will use the ImmutableDB to find
102
+ -- the corresponding hash.
103
+ StartFromSlot SlotNo
104
+ -- | Start from a specific point, ie a pair of slot number and hash.
105
+ | StartFromPoint SlotNo ByteString
106
+ deriving stock (Show )
92
107
93
108
run :: Opts -> IO ()
94
109
run opts = evalContT $ do
95
- let immDBFS = SomeHasFS $ ioHasFS $ MountPoint immutableDBDir
110
+ let mImmDBFS = SomeHasFS . ioHasFS . MountPoint <$> immutableDBDir
96
111
args = Cardano. CardanoBlockArgs configFile Nothing
97
112
ProtocolInfo {pInfoConfig = cfg} <- lift $ mkProtocolInfo args
98
113
registry <- ContT withRegistry
99
- internalImmDB <- ContT $ withImmutableDBInternal cfg registry immDBFS
114
+ mInternalImmDB <-
115
+ traverse (ContT . withImmutableDBInternal cfg registry) mImmDBFS
100
116
snocket <- Snocket. socketSnocket <$> ContT withIOManager
101
117
lift $ do
102
118
ptQueue <- newTQueueIO
103
119
varNumDequeued <- newTVarIO (0 :: Word64 )
104
120
blockFetchDone <- newEmptyTMVarIO
105
121
106
- startPoints <- for startSlots $ \ s ->
107
- ImmutableDB. getHashForSlot internalImmDB s >>= \ case
108
- Just h -> pure $ BlockPoint s h
109
- Nothing -> fail $ " Slot not in ImmutableDB: " <> show s
122
+ startPoints <- for startFrom $ \ case
123
+ StartFromSlot s -> case mInternalImmDB of
124
+ Just internalImmDB ->
125
+ ImmutableDB. getHashForSlot internalImmDB s >>= \ case
126
+ Just h -> pure $ BlockPoint s h
127
+ Nothing -> fail $ " Slot not in ImmutableDB: " <> show s
128
+ Nothing -> fail " Need to specify the path to an ImmutableDB"
129
+ StartFromPoint s h -> pure $ BlockPoint s (fromRawHash p h)
130
+ where
131
+ p = Proxy @ (CardanoBlock StandardCrypto )
110
132
111
133
let totalBlocks = numBlocks * fromIntegral (length startPoints)
112
134
@@ -138,7 +160,7 @@ run opts = evalContT $ do
138
160
configFile
139
161
, immutableDBDir
140
162
, serverAddr = (serverHostName, serverPort)
141
- , startSlots
163
+ , startFrom
142
164
, numBlocks
143
165
} = opts
144
166
0 commit comments