Skip to content

Commit 2c45102

Browse files
authored
Merge pull request #801 from IntersectMBO/improve-ipc-haddock
Improve IPC haddock
2 parents 283b2af + d6eda15 commit 2c45102

File tree

4 files changed

+319
-72
lines changed

4 files changed

+319
-72
lines changed

cardano-api/cardano-api.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ library
7373
Cardano.Api.Experimental
7474
Cardano.Api.Internal.Address
7575
Cardano.Api.Internal.Block
76+
Cardano.Api.Internal.Convenience.Query
7677
Cardano.Api.Internal.DRepMetadata
7778
Cardano.Api.Internal.Eon.ShelleyBasedEra
7879
Cardano.Api.Internal.Eras
@@ -82,6 +83,8 @@ library
8283
Cardano.Api.Internal.GenesisParameters
8384
Cardano.Api.Internal.Governance.Metadata.Validation
8485
Cardano.Api.Internal.IO
86+
Cardano.Api.Internal.IPC
87+
Cardano.Api.Internal.IPC.Monad
8588
Cardano.Api.Internal.LedgerState
8689
Cardano.Api.Internal.Modes
8790
Cardano.Api.Internal.Orphans
@@ -190,7 +193,6 @@ library
190193
Cardano.Api.Internal.Certificate
191194
Cardano.Api.Internal.Compatible.Tx
192195
Cardano.Api.Internal.Convenience.Construction
193-
Cardano.Api.Internal.Convenience.Query
194196
Cardano.Api.Internal.DeserialiseAnyOf
195197
Cardano.Api.Internal.Eon.AllegraEraOnwards
196198
Cardano.Api.Internal.Eon.AlonzoEraOnwards
@@ -228,8 +230,6 @@ library
228230
Cardano.Api.Internal.IO.Compat
229231
Cardano.Api.Internal.IO.Compat.Posix
230232
Cardano.Api.Internal.IO.Compat.Win32
231-
Cardano.Api.Internal.IPC
232-
Cardano.Api.Internal.IPC.Monad
233233
Cardano.Api.Internal.IPC.Version
234234
Cardano.Api.Internal.InMode
235235
Cardano.Api.Internal.Json

cardano-api/src/Cardano/Api/Internal/Fees.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ module Cardano.Api.Internal.Fees
7171
-- @
7272
--
7373
-- 2. The protocol parameters for the current era, which can be obtained using the 'QueryProtocolParameters'
74-
-- query defined in "Cardano.Api.Internal.Query". Let's assume they are stored in the @exampleProtocolParams@ variable.
74+
-- query defined in "Cardano.Api.Internal.Query". "Cardano.Api.Internal.IPC" documentation illustrates how
75+
-- to make a query using IPC protocol.
76+
-- Let's assume they are stored in the @exampleProtocolParams@ variable.
7577
--
7678
-- 3. The draft transaction body, which can be created using 'createTransactionBody' defined in "Cardano.Api.Internal.Tx.Body":
7779
--
@@ -128,7 +130,8 @@ module Cardano.Api.Internal.Fees
128130
-- in "Cardano.Api.Internal.Tx.Body". It is assumed to be stored in the @txBodyContent@ variable.
129131
--
130132
-- 3. The protocol parameters for the current era, which can be obtained using the 'QueryProtocolParameters'
131-
-- query defined in "Cardano.Api.Internal.Query". Let's assume they are stored in the @exampleProtocolParams@ variable.
133+
-- query defined in "Cardano.Api.Internal.Query". "Cardano.Api.Internal.IPC" documentation illustrates how
134+
-- to make a query using IPC protocol. Let's assume they are stored in the @exampleProtocolParams@ variable.
132135
--
133136
-- 4. For stake pool and governance actions, we will also need:
134137
--
@@ -225,7 +228,8 @@ module Cardano.Api.Internal.Fees
225228
-- @
226229
--
227230
-- 2. Network start time, obtainable using the 'QuerySystemStart' query defined in
228-
-- "Cardano.Api.Internal.Query". Assume we have it in the @exampleSystemStart@ variable.
231+
-- "Cardano.Api.Internal.Query". "Cardano.Api.Internal.IPC" documentation illustrates how
232+
-- to make a query using IPC protocol. Assume we have it in the @exampleSystemStart@ variable.
229233
--
230234
-- 3. Ledger epoch information, derivable by applying 'toLedgerEpochInfo' to the
231235
-- 'EraHistory', which can be retrieved using the 'QueryEraHistory' query defined in

cardano-api/src/Cardano/Api/Internal/IPC.hs

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,192 @@
1212
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
1313

1414
-- | Node IPC protocols
15+
--
16+
-- This module provides the client side of the node-to-client interprocess
17+
-- communication (IPC) for interacting with a local Cardano node. It supports
18+
-- querying the node for information, submitting transactions, monitoring
19+
-- the local mempool, and retrieving historical chain data using the
20+
-- @ChainSync@ protocol.
1521
module Cardano.Api.Internal.IPC
16-
( -- * Node interaction
22+
( -- * Examples
23+
24+
-- | This section provides two examples:
25+
--
26+
-- 1. Querying the node to obtain basic information
27+
-- 2. Submitting a transaction to the node.
28+
--
29+
-- For details on how to create a transaction, see the
30+
-- "Cardano.Api.Internal.Tx.Body" documentation.
31+
--
32+
-- The following qualified imports from @cardano-api@ are
33+
-- used in the examples below:
34+
--
35+
-- @
36+
-- import qualified Cardano.Api as Api
37+
-- import qualified Cardano.Api.Consensus as Consensus
38+
-- import qualified Cardano.Api.Network as Network
39+
-- import qualified Cardano.Api.Shelley as Shelley
40+
-- @
41+
--
42+
-- The following explicit import from @base@ is also required:
43+
--
44+
-- @
45+
-- import Control.Monad.Except (runExceptT)
46+
-- @
47+
--
48+
-- The examples assume the use of the @IO@ monad and unqualified
49+
-- access to the @Prelude@ module.
50+
51+
-- ** Constructing connection information
52+
53+
-- | Regardless of whether the goal is to query the node or submit transactions,
54+
-- the first step is to gather the connection information.
55+
--
56+
-- The following information is required:
57+
--
58+
-- * __The number of slots per epoch__. This value depends on the network the
59+
-- node is connected to. It can be obtained by inspecting the @epochLength@
60+
-- key in the @shelley-genesis.json@ file used by the node. On the preview
61+
-- network, the value is, at the time of writing, @86_400@, but it can
62+
-- change. This value and other genesis parameters can also be obtained
63+
-- using the 'QueryGenesisParameters' query.
64+
-- * __Network identifier__. When connecting to a testnet, the network identifier
65+
-- is also required. It can be obtained by looking for the @networkId@
66+
-- obtained by looking up the @networkMagic@ key in the @shelley-genesis.json@
67+
-- file that the node uses to connect to the network. For the preview
68+
-- network, the current identifier is @2@.
69+
-- * __Socket path__. The path to the node's socket file. It can be set using
70+
-- the @--socket-path@ parameter when starting the node. By default, it is
71+
-- typically located in the @db@ subfolder of the node's working directory.
72+
--
73+
-- Then, combine all the required information into a 'LocalNodeConnectInfo' value.
74+
--
75+
-- For example, assume the node is connected to the preview network and the socket
76+
-- file is located at @\/home\/user\/cardano-node\/db\/node.socket@. The
77+
-- 'LocalNodeConnectInfo' value can then be constructed as follows:
78+
--
79+
-- @
80+
-- let connectionInfo =
81+
-- Api.LocalNodeConnectInfo
82+
-- { Api.localConsensusModeParams = Api.CardanoModeParams (Api.EpochSlots 86_400)
83+
-- , Api.localNodeNetworkId = Api.Testnet (Api.NetworkMagic 2)
84+
-- , Api.localNodeSocketPath = Api.File "\/home\/user\/cardano-node\/db\/node.socket"
85+
-- }
86+
-- @
87+
88+
-- ** Querying the node for the UTXO set
89+
90+
-- | To obtain the set of unspent transaction outputs (UTXO set) from the network,
91+
-- the current era must first be determined.
92+
93+
-- *** Obtaining the current era
94+
95+
-- | Many queries require knowing the current era. This can be hardcoded using one of
96+
-- the 'ShelleyBasedEra' constructors, but it is also possible to retreive it from
97+
-- the node:
98+
--
99+
-- @
100+
-- eEra <- runExceptT $ Api.queryNodeLocalState connectionInfo Network.VolatileTip Api.QueryCurrentEra
101+
-- @
102+
--
103+
-- 'VolatileTip' requests information from the most recent block the node is aware of.
104+
-- It is important to note that this information is not guaranteed to remain valid, as
105+
-- it may be rolled back.
106+
--
107+
-- Alternatively, 'ImmutableTip' can be used to obtain information from the most recent
108+
-- block considered as final by the consensus algorithm. While this data is stable and will
109+
-- not be rolled back, it is less recent – on mainnet, it is typically about 36 hours
110+
-- behind the current time.
111+
--
112+
-- 'QueryCurrentEra' is the constructor of the query that retrieves the node's current
113+
-- era.
114+
--
115+
-- The query returns an 'ExceptT' monad, which can be run using the 'runExceptT'
116+
-- function. This yields an @eEra@ value of type @Either AcquiringFailure AnyCardanoEra@.
117+
--
118+
-- Below is an example of how to unwrap this value into a 'ShelleyBasedEra' based era, assuming the node
119+
-- is not running Byron:
120+
--
121+
-- @
122+
-- Api.AnyShelleyBasedEra sbe :: Api.AnyShelleyBasedEra <- case eEra of
123+
-- Right (Api.AnyCardanoEra era) ->
124+
-- Api.caseByronOrShelleyBasedEra
125+
-- (error "Error, we are in Byron era")
126+
-- (return . Api.AnyShelleyBasedEra)
127+
-- era
128+
-- Left Shelley.AFPointTooOld -> error "Error, point queried in the chain is too old!"
129+
-- Left Shelley.AFPointNotOnChain -> error "Error, point queried is not on chain!"
130+
-- @
131+
--
132+
-- 'AFPointTooOld' and 'AFPointNotOnChain' errors should not occur when querying with
133+
-- either 'VolatileTip' or 'ImmutableTip'.
134+
135+
-- *** Obtaining the UTXO set
136+
137+
-- | After determining the current era, the node can be queried for the UTXO set using
138+
-- the 'QueryUTxO' query as follows:
139+
--
140+
-- @
141+
-- eUtxo <-
142+
-- runExceptT $
143+
-- Api.queryNodeLocalState
144+
-- connectionInfo
145+
-- Network.VolatileTip
146+
-- (Api.QueryInEra (Api.QueryInShelleyBasedEra sbe (Api.QueryUTxO Api.QueryUTxOWhole)))
147+
-- @
148+
--
149+
-- This returns, a nested type of @Either AcquiringFailure (Either EraMismatch (UTXO era))@.
150+
-- You can unwrap it as follows:
151+
--
152+
-- @
153+
-- utxo <- case eUtxo of
154+
-- Right (Right (Api.UTxO utxo)) -> do
155+
-- return utxo
156+
-- Right (Left (Consensus.EraMismatch{Consensus.ledgerEraName, Consensus.otherEraName})) ->
157+
-- error
158+
-- ( "Error, we assumed era was "
159+
-- ++ show otherEraName
160+
-- ++ " but it was "
161+
-- ++ show ledgerEraName
162+
-- )
163+
-- Left Shelley.AFPointTooOld -> error "Error, point queried in the chain is too old!"
164+
-- Left Shelley.AFPointNotOnChain -> error "Error, point queried is not on chain!"
165+
-- @
166+
--
167+
-- Alternatively, to avoid nested result types, you can use convenience
168+
-- functions and types from "Cardano.Api.Internal.Convenience.Query".
169+
-- It is also posible to combine several queries into a single connection by using
170+
-- the monadic interface that can be found in the "Cardano.Api.Internal.IPC.Monad"
171+
-- documentation.
172+
--
173+
-- The obtained @utxo@ variable is a standard @Map@ of type @Map TxIn (TxOut CtxUTxO era)@.
174+
175+
-- ** Submitting a transaction
176+
177+
-- | Assume there is a signed transaction in the latest era that you would like to submit
178+
-- to the node. Assume it is stored in the variable @signedTx@ of type @Tx era@.
179+
--
180+
-- For details on how to create such a transaction, see the "Cardano.Api.Internal.Tx.Body"
181+
-- documentation.
182+
--
183+
-- To submit the transaction to the node, use the 'submitTxToNodeLocal' function as follows:
184+
--
185+
-- @
186+
-- result <- Api.submitTxToNodeLocal connectionInfo (Api.TxInMode sbe signedTx)
187+
-- @
188+
--
189+
-- The result is a 'SubmitResult' value, which can be inspected as follows:
190+
--
191+
-- @
192+
-- case result of
193+
-- Api.SubmitSuccess -> putStrLn "Transaction submitted successfully!"
194+
-- Api.SubmitFail reason -> error $ "Error submitting transaction: " ++ show reason
195+
-- @
196+
--
197+
-- If the command succeeds, the transaction gets into the node's mempool, ready
198+
-- to be included in a block.
199+
200+
-- * Node interaction
17201

18202
-- | Operations that involve talking to a local Cardano node.
19203
connectToLocalNode

0 commit comments

Comments
 (0)