Skip to content

Commit

Permalink
Draft a cliQueryProtocolParameters in withHydraNode'
Browse files Browse the repository at this point in the history
This deliberately uses the cardano-cli to query protocol parameters as
JSON.
  • Loading branch information
ch1bo committed Jan 3, 2024
1 parent 3def981 commit 5ac2048
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
36 changes: 35 additions & 1 deletion hydra-cluster/src/CardanoNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ import Data.Aeson qualified as Aeson
import Data.Aeson.Lens (atKey, key, _Number)
import Data.Text qualified as Text
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
import Hydra.Cardano.Api (AsType (AsPaymentKey), File (..), NetworkId, PaymentKey, SigningKey, SocketPath, VerificationKey, generateSigningKey, getVerificationKey)
import Hydra.Cardano.Api (
AsType (AsPaymentKey),
File (..),
NetworkId,
PaymentKey,
SigningKey,
SocketPath,
VerificationKey,
generateSigningKey,
getVerificationKey,
)
import Hydra.Cardano.Api qualified as Api
import Hydra.Chain.CardanoClient (QueryPoint (QueryTip), queryProtocolParameters)
import Hydra.Cluster.Fixture (
Expand All @@ -29,6 +39,7 @@ import System.Process (
CreateProcess (..),
StdStream (UseHandle),
proc,
readCreateProcess,
readProcess,
withCreateProcess,
)
Expand Down Expand Up @@ -414,6 +425,29 @@ data ProcessHasExited = ProcessHasExited Text ExitCode

instance Exception ProcessHasExited

-- | Cardano-cli wrapper to query protocol parameters. While we have also client
-- functions in Hydra.Chain.CardanoClient and Hydra.Cluster.CardanoClient,
-- sometimes we deliberately want to use the cardano-cli to ensure
-- compatibility.
cliQueryProtocolParameters :: RunningNode -> IO Value
cliQueryProtocolParameters RunningNode{nodeSocket, networkId} = do
out <- readCreateProcess cmd ""
unsafeDecodeJson $ fromString out
where
cmd =
proc "cardano-cli" $
[ "query"
, "protocol-parameters"
, "--socket-path"
, unFile nodeSocket
]
<> case networkId of
Api.Mainnet -> ["--mainnet"]
Api.Testnet magic -> ["--testnet-magic", show magic]
<> [ "--out-file"
, "/dev/stdout"
]

--
-- Helpers
--
Expand Down
39 changes: 15 additions & 24 deletions hydra-cluster/src/HydraNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Hydra.Cardano.Api
import Hydra.Prelude hiding (delete)

import Cardano.BM.Tracing (ToObject)
import Cardano.Ledger.Babbage.PParams (BabbagePParams (..))
import Cardano.Ledger.Core (PParams (..))
import CardanoNode (cliQueryProtocolParameters)
import Control.Concurrent.Async (forConcurrently_)
import Control.Concurrent.Class.MonadSTM (modifyTVar', newTVarIO, readTVarIO)
import Control.Exception (IOException)
Expand Down Expand Up @@ -305,17 +305,21 @@ withHydraNode' ::
IO a
withHydraNode' chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNodeIds pparams mGivenStdOut action = do
withSystemTempDirectory "hydra-node" $ \dir -> do
let cardanoLedgerProtocolParametersFile = dir </> "pparams.json"
writeFileBS cardanoLedgerProtocolParametersFile (writeZeroedPParams pparams)
-- NOTE: This implicitly tests of cardano-cli with hydra-node
let cardanoLedgerProtocolParametersFile = dir </> "protocol-parameters.json"
protocolParameters <- cliQueryProtocolParameters
Aeson.encodeFile cardanoLedgerProtocolParametersFile $
protocolParameters
& atKey "txFeeFixed" ?~ toJSON (Number 0)
& atKey "txFeePerByte" ?~ toJSON (Number 0)
& key "executionUnitPrices" . atKey "priceMemory" ?~ toJSON (Number 0)
& key "executionUnitPrices" . atKey "priceSteps" ?~ toJSON (Number 0)

let hydraSigningKey = dir </> (show hydraNodeId <> ".sk")
void $ writeFileTextEnvelope (File hydraSigningKey) Nothing hydraSKey
hydraVerificationKeys <- forM (zip [1 ..] hydraVKeys) $ \(i :: Int, vKey) -> do
let filepath = dir </> (show i <> ".vk")
filepath <$ writeFileTextEnvelope (File filepath) Nothing vKey
let ledgerConfig =
CardanoLedgerConfig
{ cardanoLedgerProtocolParametersFile
}
let p =
( hydraNodeProcess $
RunOptions
Expand All @@ -331,7 +335,10 @@ withHydraNode' chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNodeIds p
, hydraVerificationKeys
, persistenceDir = workDir </> "state-" <> show hydraNodeId
, chainConfig
, ledgerConfig
, ledgerConfig =
CardanoLedgerConfig
{ cardanoLedgerProtocolParametersFile
}
}
)
{ std_out = maybe CreatePipe UseHandle mGivenStdOut
Expand All @@ -344,22 +351,6 @@ withHydraNode' chainConfig workDir hydraNodeId hydraSKey hydraVKeys allNodeIds p
(Nothing, _) -> error "Should not happen™"
(_, Nothing) -> error "Should not happen™"
where
-- NOTE: We want to have zeroed fees in the Head.
writeZeroedPParams (PParams BabbagePParams{bppProtocolVersion}) =
toStrict $
( Aeson.encode (toJSON pparams)
-- FIXME: this is a hack because cardano-ledger has a bug
-- (https://github.com/IntersectMBO/cardano-ledger/issues/3943) in the
-- BabbagePParams ToJSON instance where 'protocolVersion' is missing.
& atKey "protocolVersion" ?~ toJSON bppProtocolVersion
& atKey "minFeeA" ?~ toJSON (Number 0)
& atKey "minFeeB" ?~ toJSON (Number 0)
& atKey "txFeeFixed" ?~ toJSON (Number 0)
& atKey "txFeePerByte" ?~ toJSON (Number 0)
& key "executionUnitPrices" . atKey "priceMemory" ?~ toJSON (Number 0)
& key "executionUnitPrices" . atKey "priceSteps" ?~ toJSON (Number 0)
)

peers =
[ Host
{ Network.hostname = "127.0.0.1"
Expand Down

0 comments on commit 5ac2048

Please sign in to comment.