Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

n2n-pg: init #1160

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions ouroboros-consensus-cardano/app/n2n-pg.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE NamedFieldPuns #-}

module Main (main) where

import Cardano.Crypto.Init (cryptoInit)
import Cardano.Tools.N2NPG.Run (Opts (..), StartFrom (..))
import qualified Cardano.Tools.N2NPG.Run as N2NPG
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Char8 as BC8
import Main.Utf8 (withStdTerminalHandles)
import Options.Applicative
import Ouroboros.Consensus.Block
import Text.Read (readEither)

main :: IO ()
main = withStdTerminalHandles $ do
cryptoInit
N2NPG.run =<< execParser optsParser

optsParser :: ParserInfo Opts
optsParser =
info (helper <*> parse) $ fullDesc <> progDesc desc
where
desc = "Download headers and blocks via the N2N protocol"

parse = do
configFile <- strOption $ mconcat
[ long "config"
, help "Path to config file, in the same format as for the node or db-analyser"
, metavar "PATH"
]
immutableDBDir <- optional $ strOption $ mconcat
[ long "db"
, help "Path to the ImmutableDB, only used for hash lookups"
, metavar "PATH"
]
let readHostPort = do
hostPort <- str
case break (== ':') hostPort of
(host, ':' : port) -> pure (host, port)
_ -> fail $ "Could not read host and port: " ++ show hostPort
serverAddr <- option readHostPort $ mconcat
[ long "server"
, help "Server address"
, metavar "HOST:PORT"
]
let readStartFrom = eitherReader $ \sf -> case break (== '@') sf of
(h, '@' : s) -> do
hash <- B16.decode $ BC8.pack h
slot <- readEither s
pure $ StartFromPoint (SlotNo slot) hash
(s, _) -> StartFromSlot . SlotNo <$> readEither s
startFrom <- some $ option readStartFrom $ mconcat
[ long "start-from"
, metavar "SLOT_NUMBER or HASH@SLOT_NUMBER"
, help $ "Start downloading from this slot (must be in the ImmutableDB) "
<> "or the given point (hash and slot)"
]
numBlocks <- option auto $ mconcat
[ long "num-blocks"
, metavar "NUM_BLOCKS"
, help "Number of blocks to download"
]
pure Opts {
configFile
, immutableDBDir
, serverAddr
, startFrom
, numBlocks
}
17 changes: 17 additions & 0 deletions ouroboros-consensus-cardano/ouroboros-consensus-cardano.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ library unstable-cardano-tools
Cardano.Tools.GitRev
Cardano.Tools.ImmDBServer.Diffusion
Cardano.Tools.ImmDBServer.MiniProtocols
Cardano.Tools.N2NPG.Run

other-modules:
Cardano.Api.Key
Expand Down Expand Up @@ -542,6 +543,7 @@ library unstable-cardano-tools
filepath,
fs-api ^>=0.2.0.1,
githash,
io-classes,
microlens,
mtl,
network,
Expand All @@ -563,6 +565,7 @@ library unstable-cardano-tools
text-builder,
transformers,
transformers-except,
typed-protocols,

executable db-analyser
import: common-lib
Expand Down Expand Up @@ -652,6 +655,20 @@ executable immdb-server
ouroboros-consensus-cardano:unstable-cardano-tools,
with-utf8,

executable n2n-pg
import: common-exe
hs-source-dirs: app
main-is: n2n-pg.hs
build-depends:
base,
base16-bytestring,
bytestring,
cardano-crypto-class,
optparse-applicative,
ouroboros-consensus,
ouroboros-consensus-cardano:unstable-cardano-tools,
with-utf8,

test-suite tools-test
import: common-test
type: exitcode-stdio-1.0
Expand Down
Loading
Loading