|
12 | 12 | {-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-} |
13 | 13 |
|
14 | 14 | -- | 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. |
15 | 21 | 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 |
17 | 201 |
|
18 | 202 | -- | Operations that involve talking to a local Cardano node. |
19 | 203 | connectToLocalNode |
|
0 commit comments