Skip to content

Commit

Permalink
txgen-mvar: remove dependency on iproute
Browse files Browse the repository at this point in the history
It was introduced as part of an intermediate debugging measure. This can
likely be folded back into the commit that introduced the dependency.
  • Loading branch information
NadiaYvette committed May 7, 2024
1 parent b3a7618 commit 4be81ee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
4 changes: 3 additions & 1 deletion bench/tx-generator/src/Cardano/Benchmarking/GeneratorTx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ walletBenchmark
= liftIO $ do
traceDebug "******* Tx generator, phase 2: pay to recipients *******"

remoteAddresses <- forM targets (\(NodeDescription {..}) -> secondM lookupNodeAddress (ndName, NodeAddress { naHostAddress = NodeHostIPv4Address ndAddr, naPort = toEnum ndPort }))
let numTargets :: Natural = fromIntegral $ NE.length targets
lookupTarget :: NodeDescription -> IO (String, AddrInfo)
lookupTarget NodeDescription {..} = secondM lookupNodeAddress (ndName, ndAddr)
remoteAddresses <- forM targets lookupTarget

traceDebug $ "******* Tx generator, launching Tx peers: " ++ show (NE.length remoteAddresses) ++ " of them"

Expand Down
38 changes: 20 additions & 18 deletions bench/tx-generator/src/Cardano/TxGenerator/Setup/NixService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Cardano.TxGenerator.Setup.NixService
, NodeDescription (..)
, getKeepaliveTimeout
, getNodeAlias
, setNodeAlias
, getNodeConfigFile
, setNodeConfigFile
, txGenTxParams
Expand All @@ -25,15 +24,16 @@ import Cardano.Api (AnyCardanoEra, mapFile)

import Cardano.CLI.Types.Common (FileDirection (..), SigningKeyFile)
import qualified Cardano.Ledger.Coin as L
import Cardano.Node.Configuration.NodeAddress (NodeAddress' (..), NodeHostIPv4Address (..), NodeIPv4Address)
import Cardano.Node.Types (AdjustFilePaths (..))
import Cardano.TxGenerator.Internal.Orphans ()
import Cardano.TxGenerator.Types

import Data.Aeson as Aeson
import Data.Aeson.Types as Aeson
import Data.Foldable (find)
import Data.IP as IP
import Data.List.NonEmpty (NonEmpty (..), partition)
import Data.Function (on)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe (fromMaybe)
import qualified Data.Time.Clock as Clock (DiffTime, secondsToDiffTime)
import GHC.Generics (Generic)
Expand Down Expand Up @@ -64,40 +64,42 @@ deriving instance Generic NixServiceOptions
-- only works on JSON Object types
data NodeDescription =
NodeDescription {
ndAddr :: IPv4
-- NodeIPAddress would be agnostic to IPv4 vs. IPv6 and likely
-- a small investment here.
ndAddr :: NodeIPv4Address
, ndName :: String
, ndPort :: Int
} deriving (Eq, Show, Generic)

-- { "alias": "foo", "addr": ..., "port": ... }
instance FromJSON NodeDescription where
parseJSON = withObject "NodeDescription" \v -> do
ndAddr <- v .: "addr" <?> Key "addr"
ndPort <- v .: "port" <?> Key "port"
ndName <- v .:? "name" <?> Key "name" .!= show ndAddr
unNodeHostIPv4Address
<- v .: "addr" <?> Key "addr"
naPort <- fmap toEnum $
v .: "port" <?> Key "port"
let naHostAddress = NodeHostIPv4Address {..}
ndAddr = NodeAddress {..}
ndName <- v .:? "name" <?> Key "name" .!= show ndAddr
pure $ NodeDescription {..}

instance ToJSON NodeDescription where
toJSON NodeDescription {..} = object
[ "name" .= ndName
, "addr" .= ndAddr
, "port" .= ndPort ]
, "addr" .= unNodeHostIPv4Address
, "port" .= fromEnum naPort ] where
_addr@(NodeAddress {..}) = ndAddr
_hostAddr@(NodeHostIPv4Address {..}) = naHostAddress


-- Long GC pauses on target nodes can trigger spurious MVar deadlock
-- detection. Increasing this timeout can help mitigate those errors.
getKeepaliveTimeout :: NixServiceOptions -> Clock.DiffTime
getKeepaliveTimeout = maybe 10 Clock.secondsToDiffTime . _nix_keepalive

getNodeAlias :: NixServiceOptions -> IPv4 -> Maybe String
getNodeAlias :: NixServiceOptions -> NodeIPv4Address -> Maybe String
getNodeAlias NixServiceOptions {..} ip = fmap ndName $
flip find _nix_targetNodes \(NodeDescription {..}) -> ndAddr == ip

setNodeAlias :: NixServiceOptions -> IPv4 -> String -> Maybe NixServiceOptions
setNodeAlias opts@(NixServiceOptions { _nix_targetNodes = targets }) ip name
| ([match], nonMatches) <- flip partition targets \(NodeDescription {..}) -> ndAddr == ip
= Just $ opts { _nix_targetNodes = match { ndName = name } :| nonMatches }
| otherwise = Nothing
find ((=:=:= ip) . ndAddr) _nix_targetNodes where
(=:=:=) = (==) `on` naHostAddress

getNodeConfigFile :: NixServiceOptions -> Maybe FilePath
getNodeConfigFile = _nix_nodeConfigFile
Expand Down
1 change: 0 additions & 1 deletion bench/tx-generator/tx-generator.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ library
, generic-monoid
, ghc-prim
, io-classes
, iproute
, mtl
, network
, network-mux
Expand Down

0 comments on commit 4be81ee

Please sign in to comment.