Skip to content

Commit 1c03a40

Browse files
committed
Allow to specify points (does not require an ImmutableDB)
1 parent a381b12 commit 1c03a40

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

ouroboros-consensus-cardano/app/n2n-pg.hs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
module Main (main) where
55

66
import Cardano.Crypto.Init (cryptoInit)
7-
import Cardano.Tools.N2NPG.Run (Opts (..))
7+
import Cardano.Tools.N2NPG.Run (Opts (..), StartFrom (..))
88
import qualified Cardano.Tools.N2NPG.Run as N2NPG
9+
import qualified Data.ByteString.Base16 as B16
10+
import qualified Data.ByteString.Char8 as BC8
911
import Main.Utf8 (withStdTerminalHandles)
1012
import Options.Applicative
1113
import Ouroboros.Consensus.Block
14+
import Text.Read (readEither)
1215

1316
main :: IO ()
1417
main = withStdTerminalHandles $ do
@@ -27,7 +30,7 @@ optsParser =
2730
, help "Path to config file, in the same format as for the node or db-analyser"
2831
, metavar "PATH"
2932
]
30-
immutableDBDir <- strOption $ mconcat
33+
immutableDBDir <- optional $ strOption $ mconcat
3134
[ long "db"
3235
, help "Path to the ImmutableDB, only used for hash lookups"
3336
, metavar "PATH"
@@ -42,10 +45,17 @@ optsParser =
4245
, help "Server address"
4346
, metavar "HOST:PORT"
4447
]
45-
startSlots <- some $ option (SlotNo <$> auto) $ mconcat
48+
let readStartFrom = eitherReader $ \sf -> case break (== '@') sf of
49+
(h, '@' : s) -> do
50+
hash <- B16.decode $ BC8.pack h
51+
slot <- readEither s
52+
pure $ StartFromPoint (SlotNo slot) hash
53+
(s, _) -> StartFromSlot . SlotNo <$> readEither s
54+
startFrom <- some $ option readStartFrom $ mconcat
4655
[ long "start-from"
47-
, metavar "SLOT_NUMBER"
48-
, help "Start downloading from this slot (must be in the ImmutableDB)"
56+
, metavar "SLOT_NUMBER or HASH@SLOT_NUMBER"
57+
, help $ "Start downloading from this slot (must be in the ImmutableDB) "
58+
<> "or the given point (hash and slot)"
4959
]
5060
numBlocks <- option auto $ mconcat
5161
[ long "num-blocks"
@@ -56,6 +66,6 @@ optsParser =
5666
configFile
5767
, immutableDBDir
5868
, serverAddr
59-
, startSlots
69+
, startFrom
6070
, numBlocks
6171
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,8 @@ executable n2n-pg
661661
main-is: n2n-pg.hs
662662
build-depends:
663663
base,
664+
base16-bytestring,
665+
bytestring,
664666
cardano-crypto-class,
665667
optparse-applicative,
666668
ouroboros-consensus,

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/N2NPG/Run.hs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DerivingStrategies #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
{-# LANGUAGE GADTs #-}
45
{-# LANGUAGE LambdaCase #-}
@@ -7,8 +8,11 @@
78
{-# LANGUAGE ScopedTypeVariables #-}
89
{-# LANGUAGE TypeApplications #-}
910

11+
{-# OPTIONS_GHC -Wwarn #-}
12+
1013
module Cardano.Tools.N2NPG.Run (
1114
Opts (..)
15+
, StartFrom (..)
1216
, run
1317
) where
1418

@@ -19,6 +23,7 @@ import Control.Monad.Class.MonadSay (MonadSay (..))
1923
import Control.Monad.Cont
2024
import Control.Monad.Trans (MonadTrans (..))
2125
import Control.Tracer (nullTracer, stdoutTracer)
26+
import Data.ByteString (ByteString)
2227
import qualified Data.ByteString.Lazy as BL
2328
import Data.Functor ((<&>))
2429
import Data.Functor.Contravariant ((>$<))
@@ -31,6 +36,7 @@ import Network.TypedProtocol (N (..), Nat (..), PeerHasAgency (..),
3136
PeerPipelined (..), PeerReceiver (..), PeerRole (..),
3237
PeerSender (..), natToInt)
3338
import Ouroboros.Consensus.Block
39+
import Ouroboros.Consensus.Cardano.Block
3440
import Ouroboros.Consensus.Config
3541
import Ouroboros.Consensus.Config.SupportsNode
3642
(ConfigSupportsNode (..))
@@ -84,29 +90,45 @@ import System.FS.IO (ioHasFS)
8490

8591
data Opts = Opts {
8692
configFile :: FilePath
87-
, immutableDBDir :: FilePath
93+
, immutableDBDir :: Maybe FilePath
8894
, serverAddr :: (Socket.HostName, Socket.ServiceName)
89-
, startSlots :: [SlotNo]
95+
, startFrom :: [StartFrom]
9096
, numBlocks :: Word64
9197
}
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)
92107

93108
run :: Opts -> IO ()
94109
run opts = evalContT $ do
95-
let immDBFS = SomeHasFS $ ioHasFS $ MountPoint immutableDBDir
110+
let mImmDBFS = SomeHasFS . ioHasFS . MountPoint <$> immutableDBDir
96111
args = Cardano.CardanoBlockArgs configFile Nothing
97112
ProtocolInfo{pInfoConfig = cfg} <- lift $ mkProtocolInfo args
98113
registry <- ContT withRegistry
99-
internalImmDB <- ContT $ withImmutableDBInternal cfg registry immDBFS
114+
mInternalImmDB <-
115+
traverse (ContT . withImmutableDBInternal cfg registry) mImmDBFS
100116
snocket <- Snocket.socketSnocket <$> ContT withIOManager
101117
lift $ do
102118
ptQueue <- newTQueueIO
103119
varNumDequeued <- newTVarIO (0 :: Word64)
104120
blockFetchDone <- newEmptyTMVarIO
105121

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)
110132

111133
let totalBlocks = numBlocks * fromIntegral (length startPoints)
112134

@@ -138,7 +160,7 @@ run opts = evalContT $ do
138160
configFile
139161
, immutableDBDir
140162
, serverAddr = (serverHostName, serverPort)
141-
, startSlots
163+
, startFrom
142164
, numBlocks
143165
} = opts
144166

0 commit comments

Comments
 (0)