Skip to content

Commit

Permalink
Remove OS-dependent code and simplify dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Feb 8, 2023
1 parent cac6382 commit 7e1ed4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 63 deletions.
36 changes: 6 additions & 30 deletions ouroboros-consensus-cardano-tools/app/block-tool.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{-# LANGUAGE NamedFieldPuns #-}

import Cardano.Tools.Block (BlockOptions (..), readChainPoint, run)
import Control.Concurrent.MVar (newMVar)
import Options.Applicative (
Parser,
execParser,
Expand All @@ -19,29 +16,9 @@ import Options.Applicative (
(<**>),
(<|>),
)
import Ouroboros.Consensus.Storage.FS.API.Types (Handle (..), MountPoint (..), mkFsPath)
import Ouroboros.Consensus.Storage.FS.Handle (HandleOS (..))
import Ouroboros.Consensus.Storage.FS.IO (HandleIO, ioHasFS)
import System.Posix (Fd, stdInput, stdOutput)

main :: IO ()
main = do
stdinIO <- mkHandle "<stdin>" stdInput
stdoutIO <- mkHandle "<stdout>" stdOutput
parseOptions >>= run (ioHasFS $ MountPoint ".") stdinIO stdoutIO

mkHandle :: [Char] -> Fd -> IO (Handle HandleIO)
mkHandle filePath fd = do
handle <- newMVar (Just fd)
pure $
Handle
{ handlePath = mkFsPath [filePath]
, handleRaw =
HandleOS
{ filePath
, handle
}
}
main = parseOptions >>= run

parseOptions :: IO BlockOptions
parseOptions = execParser opts
Expand All @@ -62,12 +39,11 @@ parseBlockOptions =
viewBlockOptions =
ViewBlock
<$> optional
( mkFsPath . (: [])
<$> strOption
( long "file-in"
<> metavar "FILE"
<> help "Path to file containing hex-encoded CBOR-encoded block"
)
( strOption
( long "file-in"
<> metavar "FILE"
<> help "Path to file containing hex-encoded CBOR-encoded block"
)
)

extractBlockOptions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,9 @@ executable block-tool
hs-source-dirs: app
main-is: block-tool.hs
build-depends: base
, base16-bytestring
, bytestring
, cardano-crypto-wrapper
, cardano-ledger-byron
, cborg
, optparse-applicative
, ouroboros-consensus
, ouroboros-consensus-byron
, ouroboros-consensus-cardano
, ouroboros-consensus-cardano-tools
, ouroboros-consensus-diffusion
, ouroboros-consensus-shelley
, text
, zlib
-- does not work on Windows because of fancy stuff juggling with File descriptors
-- too provide stdout/stdin with `HasFS` interface
-- FIXME: this should probably disappear has there's not much point in having this
-- level of indirection
if !os(windows)
build-depends: unix


default-language: Haskell2010
ghc-options: -Wall
-Wcompat
Expand Down
25 changes: 11 additions & 14 deletions ouroboros-consensus-cardano-tools/src/Cardano/Tools/Block.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,32 @@ import Ouroboros.Consensus.Protocol.Praos.Translate ()
import qualified Ouroboros.Consensus.Shelley.Ledger as Shelley
import Ouroboros.Consensus.Shelley.Ledger.SupportsProtocol ()
import Ouroboros.Consensus.Storage.Common (BlockComponent (GetBlock))
import Ouroboros.Consensus.Storage.FS.API (Handle, HasFS, hGetAll, hPutAll, withFile)
import Ouroboros.Consensus.Storage.FS.API.Types (FsPath, OpenMode (ReadMode))
import Ouroboros.Consensus.Storage.ImmutableDB.API (getBlockComponent)
import Ouroboros.Consensus.Storage.Serialisation (decodeDisk)
import Ouroboros.Consensus.Util.IOLike (Exception, MonadThrow (throwIO))
import System.IO (Handle, IOMode (ReadMode), stdin, stdout, withFile)
import Text.Read (readMaybe)

data BlockOptions
= ViewBlock
{ blockFile :: Maybe FsPath
{ blockFile :: Maybe FilePath
}
| ExtractBlock
{ dbDirectory :: FilePath
, cardanoConfigPath :: FilePath
, point :: Point CBlock
}

run :: HasFS IO h -> Handle h -> Handle h -> BlockOptions -> IO ()
run hasFS stdinIO stdoutIO options = do
run :: BlockOptions -> IO ()
run options = do
block <- case options of
ViewBlock{blockFile} -> do
case blockFile of
Just file -> withFile hasFS file ReadMode $ viewBlock hasFS
Nothing -> viewBlock hasFS stdinIO
Just file -> withFile file ReadMode $ viewBlock
Nothing -> viewBlock stdin
ExtractBlock{dbDirectory, cardanoConfigPath, point} ->
Aeson.encode <$> readBlockFromDB dbDirectory cardanoConfigPath point
void $ hPutAll hasFS stdoutIO block
void $ LBS.hPut stdout block

readBlockFromDB :: FilePath -> FilePath -> Point CBlock -> IO CBlock
readBlockFromDB dbDirectory configFilePath point =
Expand All @@ -88,12 +87,10 @@ type CBlock = CardanoBlock StandardCrypto
`stdout`.
-}
viewBlock ::
(MonadThrow m) =>
HasFS m h ->
Handle h ->
m LBS.ByteString
viewBlock hasFS hdl = do
bytes <- hGetAll hasFS hdl
Handle ->
IO LBS.ByteString
viewBlock hdl = do
bytes <- LBS.hGetContents hdl
cbor <- either (throwIO . BadHexEncoding) pure $ Hex.decode bytes
Aeson.encode <$> parseBlock cbor

Expand Down

0 comments on commit 7e1ed4a

Please sign in to comment.