{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Cardano.Rpc.Server.Internal.UtxoRpc.Query
( readParamsMethod
, readParamsFill
, readUtxosMethod
, searchUtxosMethod
, readGenesisMethod
, readEraSummaryMethod
, paginateByTxIn
)
where
import Cardano.Api
import Cardano.Api.Experimental.Era
import Cardano.Api.Parser.Text qualified as P
import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as U5c
import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as UtxoRpc
import Cardano.Rpc.Server.Internal.Error
import Cardano.Rpc.Server.Internal.FieldMask
import Cardano.Rpc.Server.Internal.Monad
import Cardano.Rpc.Server.Internal.Orphans ()
import Cardano.Rpc.Server.Internal.TimedCache (readThroughCache)
import Cardano.Rpc.Server.Internal.UtxoRpc.Predicate
import Cardano.Rpc.Server.Internal.UtxoRpc.Type
import Cardano.Rpc.Server.NodeKernelAccess
import Cardano.Crypto.Hash.Class qualified as Crypto (hashToBytes)
import Cardano.Ledger.Shelley.Genesis qualified as L (ShelleyGenesis, sgNetworkMagic)
import RIO hiding (toList)
import Control.Error.Util (hush)
import Data.Map.Strict qualified as Map
import Data.ProtoLens (defMessage)
import Data.Text qualified as Text (pack)
import Data.Time.Clock (UTCTime)
import GHC.IsList
import Network.GRPC.Spec
import System.FS.API (MountPoint (..), SomeHasFS (..))
import System.FS.IO (ioHasFS)
import System.FilePath (takeDirectory)
import Proto.Google.Protobuf.FieldMask_Fields qualified as FieldMask (paths)
readParamsMethod
:: MonadRpc e m
=> Proto UtxoRpc.ReadParamsRequest
-> m (Proto UtxoRpc.ReadParamsResponse)
readParamsMethod :: forall e (m :: * -> *).
MonadRpc e m =>
Proto ReadParamsRequest -> m (Proto ReadParamsResponse)
readParamsMethod Proto ReadParamsRequest
req = do
nodeConnInfo <- m LocalNodeConnectInfo
forall field env (m :: * -> *).
(Has field env, MonadReader env m) =>
m field
grab
let fieldMask = [Text] -> FieldMaskTree ReadParamsResponse
forall msg. Message msg => [Text] -> FieldMaskTree msg
resolve ([Text] -> FieldMaskTree ReadParamsResponse)
-> [Text] -> FieldMaskTree ReadParamsResponse
forall a b. (a -> b) -> a -> b
$ Proto ReadParamsRequest -> ReadParamsRequest
forall msg. Proto msg -> msg
getProto Proto ReadParamsRequest
req ReadParamsRequest
-> Getting [Text] ReadParamsRequest [Text] -> [Text]
forall s a. s -> Getting a s a -> a
^. LensLike' (Const [Text]) ReadParamsRequest FieldMask
forall (f :: * -> *) s a.
(Functor f, HasField s "fieldMask" a) =>
LensLike' f s a
U5c.fieldMask LensLike' (Const [Text]) ReadParamsRequest FieldMask
-> (([Text] -> Const [Text] [Text])
-> FieldMask -> Const [Text] FieldMask)
-> Getting [Text] ReadParamsRequest [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Text] -> Const [Text] [Text])
-> FieldMask -> Const [Text] FieldMask
forall (f :: * -> *) s a.
(Functor f, HasField s "paths" a) =>
LensLike' f s a
FieldMask.paths
fmap Proto . runFill (runLocalQuery nodeConnInfo) fieldMask $
readParamsFill doQueryPParams doQueryTip
where
doQueryPParams :: LocalQuery UtxoRpc.PParams
doQueryPParams :: LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams
doQueryPParams = (forall era.
Era era
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams)
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams
forall a. (forall era. Era era -> LocalQuery a) -> LocalQuery a
queryInCurrentEra ((forall era.
Era era
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams)
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams)
-> (forall era.
Era era
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams)
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO PParams
forall a b. (a -> b) -> a -> b
$ \Era era
eon -> do
pparams <- Either EraMismatch (PParams (ShelleyLedgerEra era))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(PParams (ShelleyLedgerEra era))
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either EraMismatch (PParams (ShelleyLedgerEra era))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(PParams (ShelleyLedgerEra era)))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either EraMismatch (PParams (ShelleyLedgerEra era)))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(PParams (ShelleyLedgerEra era))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Either
UnsupportedNtcVersionError
(Either EraMismatch (PParams (ShelleyLedgerEra era)))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either EraMismatch (PParams (ShelleyLedgerEra era)))
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either
UnsupportedNtcVersionError
(Either EraMismatch (PParams (ShelleyLedgerEra era)))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either EraMismatch (PParams (ShelleyLedgerEra era))))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either
UnsupportedNtcVersionError
(Either EraMismatch (PParams (ShelleyLedgerEra era))))
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either EraMismatch (PParams (ShelleyLedgerEra era)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ShelleyBasedEra era
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either
UnsupportedNtcVersionError
(Either EraMismatch (PParams (ShelleyLedgerEra era))))
forall era block point r.
ShelleyBasedEra era
-> LocalStateQueryExpr
block
point
QueryInMode
r
IO
(Either
UnsupportedNtcVersionError
(Either EraMismatch (PParams (ShelleyLedgerEra era))))
queryProtocolParameters (Era era -> ShelleyBasedEra era
forall era. Era era -> ShelleyBasedEra era
forall a (f :: a -> *) (g :: a -> *) (era :: a).
Convert f g =>
f era -> g era
convert Era era
eon)
pure . getProto $ obtainCommonConstraints eon (protocolParamsToUtxoRpcPParams eon pparams)
doQueryTip :: LocalQuery UtxoRpc.ChainPoint
doQueryTip :: LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO ChainPoint
doQueryTip = do
chainPoint <- Either UnsupportedNtcVersionError ChainPoint
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO ChainPoint
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either UnsupportedNtcVersionError ChainPoint
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO ChainPoint)
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either UnsupportedNtcVersionError ChainPoint)
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO ChainPoint
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either UnsupportedNtcVersionError ChainPoint)
forall block point r.
LocalStateQueryExpr
block
point
QueryInMode
r
IO
(Either UnsupportedNtcVersionError ChainPoint)
queryChainPoint
chainBlockNo <- throwEither =<< queryChainBlockNo
systemStart <- throwEither =<< querySystemStart
eraHistory <- throwEither =<< queryEraHistory
timestamp <- slotToTimestamp systemStart eraHistory chainPoint
pure . getProto $ mkChainPointMsg chainPoint chainBlockNo timestamp
readParamsFill
:: Applicative m
=> m UtxoRpc.PParams
-> m UtxoRpc.ChainPoint
-> FieldFill m UtxoRpc.ReadParamsResponse
readParamsFill :: forall (m :: * -> *).
Applicative m =>
m PParams -> m ChainPoint -> FieldFill m ReadParamsResponse
readParamsFill m PParams
doQueryPParams m ChainPoint
doQueryTip =
forall (label :: Symbol) msg sub (m :: * -> *).
(HasField msg label sub, KnownSymbol label, Message msg,
Message sub, Applicative m) =>
FieldFill m sub -> FieldFill m msg
fillSub @"values" (forall (label :: Symbol) msg a (m :: * -> *).
(HasField msg label a, KnownSymbol label, Message msg,
Functor m) =>
m a -> FieldFill m msg
fill @"cardano" m PParams
doQueryPParams) FieldFill m ReadParamsResponse
-> FieldFill m ReadParamsResponse -> FieldFill m ReadParamsResponse
forall a. Semigroup a => a -> a -> a
<> forall (label :: Symbol) msg a (m :: * -> *).
(HasField msg label a, KnownSymbol label, Message msg,
Functor m) =>
m a -> FieldFill m msg
fill @"ledgerTip" m ChainPoint
doQueryTip
readUtxosMethod
:: MonadRpc e m
=> Proto UtxoRpc.ReadUtxosRequest
-> m (Proto UtxoRpc.ReadUtxosResponse)
readUtxosMethod :: forall e (m :: * -> *).
MonadRpc e m =>
Proto ReadUtxosRequest -> m (Proto ReadUtxosResponse)
readUtxosMethod Proto ReadUtxosRequest
req
| [Proto TxoRef] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Proto TxoRef] -> Bool) -> [Proto TxoRef] -> Bool
forall a b. (a -> b) -> a -> b
$ Proto ReadUtxosRequest
req Proto ReadUtxosRequest
-> Getting [Proto TxoRef] (Proto ReadUtxosRequest) [Proto TxoRef]
-> [Proto TxoRef]
forall s a. s -> Getting a s a -> a
^. Getting [Proto TxoRef] (Proto ReadUtxosRequest) [Proto TxoRef]
forall (f :: * -> *) s a.
(Functor f, HasField s "keys" a) =>
LensLike' f s a
U5c.keys = Proto ReadUtxosResponse -> m (Proto ReadUtxosResponse)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Proto ReadUtxosResponse
forall msg. Message msg => msg
defMessage
| Bool
otherwise = do
let keyCount :: Int
keyCount = [Proto TxoRef] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Proto TxoRef] -> Int) -> [Proto TxoRef] -> Int
forall a b. (a -> b) -> a -> b
$ Proto ReadUtxosRequest
req Proto ReadUtxosRequest
-> Getting [Proto TxoRef] (Proto ReadUtxosRequest) [Proto TxoRef]
-> [Proto TxoRef]
forall s a. s -> Getting a s a -> a
^. Getting [Proto TxoRef] (Proto ReadUtxosRequest) [Proto TxoRef]
forall (f :: * -> *) s a.
(Functor f, HasField s "keys" a) =>
LensLike' f s a
U5c.keys
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
keyCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxUtxoQueryKeys)
(m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ GrpcError -> Text -> m ()
forall (m :: * -> *) a. MonadIO m => GrpcError -> Text -> m a
throwGrpcErrorWithMessage GrpcError
GrpcInvalidArgument
(Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$ Text
"too many keys: "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
keyCount
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", maximum "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
maxUtxoQueryKeys
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"; batch your requests"
utxoFilter <- Set TxIn -> QueryUTxOFilter
QueryUTxOByTxIn (Set TxIn -> QueryUTxOFilter)
-> ([TxIn] -> Set TxIn) -> [TxIn] -> QueryUTxOFilter
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Item (Set TxIn)] -> Set TxIn
[TxIn] -> Set TxIn
forall l. IsList l => [Item l] -> l
fromList ([TxIn] -> QueryUTxOFilter) -> m [TxIn] -> m QueryUTxOFilter
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Proto TxoRef -> m TxIn) -> [Proto TxoRef] -> m [TxIn]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Proto TxoRef -> m TxIn
forall e (m :: * -> *). MonadRpc e m => Proto TxoRef -> m TxIn
txoRefToTxIn (Proto ReadUtxosRequest
req Proto ReadUtxosRequest
-> Getting [Proto TxoRef] (Proto ReadUtxosRequest) [Proto TxoRef]
-> [Proto TxoRef]
forall s a. s -> Getting a s a -> a
^. Getting [Proto TxoRef] (Proto ReadUtxosRequest) [Proto TxoRef]
forall (f :: * -> *) s a.
(Functor f, HasField s "keys" a) =>
LensLike' f s a
U5c.keys)
nodeConnInfo <- grab
AnyCardanoEra era <- liftIO . throwExceptT $ determineEra nodeConnInfo
eon <- forEraInEon @Era era (error "Minimum Conway era required") pure
let target = Target point
forall point. Target point
VolatileTip
(utxo, chainPoint, chainBlockNo, systemStart, eraHistory) <- liftIO . (throwEither =<<) $ executeLocalStateQueryExpr nodeConnInfo target $ do
utxo <- throwEither =<< throwEither =<< queryUtxo (convert eon) utxoFilter
chainPoint <- throwEither =<< queryChainPoint
chainBlockNo <- throwEither =<< queryChainBlockNo
systemStart <- throwEither =<< querySystemStart
eraHistory <- throwEither =<< queryEraHistory
pure (utxo, chainPoint, chainBlockNo, systemStart, eraHistory)
timestamp <- slotToTimestamp systemStart eraHistory chainPoint
pure $
defMessage
& U5c.ledgerTip .~ mkChainPointMsg chainPoint chainBlockNo timestamp
& U5c.items .~ obtainCommonConstraints eon (utxoToUtxoRpcAnyUtxoData utxo)
where
txoRefToTxIn :: MonadRpc e m => Proto UtxoRpc.TxoRef -> m TxIn
txoRefToTxIn :: forall e (m :: * -> *). MonadRpc e m => Proto TxoRef -> m TxIn
txoRefToTxIn Proto TxoRef
r = do
txId' <- Either SerialiseAsRawBytesError TxId -> m TxId
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either SerialiseAsRawBytesError TxId -> m TxId)
-> Either SerialiseAsRawBytesError TxId -> m TxId
forall a b. (a -> b) -> a -> b
$ AsType TxId -> ByteString -> Either SerialiseAsRawBytesError TxId
forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either SerialiseAsRawBytesError a
deserialiseFromRawBytes AsType TxId
AsTxId (ByteString -> Either SerialiseAsRawBytesError TxId)
-> ByteString -> Either SerialiseAsRawBytesError TxId
forall a b. (a -> b) -> a -> b
$ Proto TxoRef
r Proto TxoRef
-> Getting ByteString (Proto TxoRef) ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString (Proto TxoRef) ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "hash" a) =>
LensLike' f s a
U5c.hash
pure $ TxIn txId' (TxIx . fromIntegral $ r ^. U5c.index)
maxUtxoQueryKeys :: Int
maxUtxoQueryKeys :: Int
maxUtxoQueryKeys = Int
20_000
searchUtxosMethod
:: MonadRpc e m
=> Proto UtxoRpc.SearchUtxosRequest
-> m (Proto UtxoRpc.SearchUtxosResponse)
searchUtxosMethod :: forall e (m :: * -> *).
MonadRpc e m =>
Proto SearchUtxosRequest -> m (Proto SearchUtxosResponse)
searchUtxosMethod Proto SearchUtxosRequest
req = do
let maxItems :: Int32
maxItems = Proto SearchUtxosRequest
req Proto SearchUtxosRequest
-> Getting Int32 (Proto SearchUtxosRequest) Int32 -> Int32
forall s a. s -> Getting a s a -> a
^. Getting Int32 (Proto SearchUtxosRequest) Int32
forall (f :: * -> *) s a.
(Functor f, HasField s "maxItems" a) =>
LensLike' f s a
U5c.maxItems
startToken :: Maybe Text
startToken = Proto SearchUtxosRequest
req Proto SearchUtxosRequest
-> Getting (Maybe Text) (Proto SearchUtxosRequest) (Maybe Text)
-> Maybe Text
forall s a. s -> Getting a s a -> a
^. Getting (Maybe Text) (Proto SearchUtxosRequest) (Maybe Text)
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'startToken" a) =>
LensLike' f s a
U5c.maybe'startToken
(compiledPredicate, utxoFilter) <- case Proto SearchUtxosRequest
req Proto SearchUtxosRequest
-> Getting
(Maybe (Proto UtxoPredicate))
(Proto SearchUtxosRequest)
(Maybe (Proto UtxoPredicate))
-> Maybe (Proto UtxoPredicate)
forall s a. s -> Getting a s a -> a
^. Getting
(Maybe (Proto UtxoPredicate))
(Proto SearchUtxosRequest)
(Maybe (Proto UtxoPredicate))
forall (f :: * -> *) s a.
(Functor f, HasField s "maybe'predicate" a) =>
LensLike' f s a
U5c.maybe'predicate of
Just Proto UtxoPredicate
p
| Bool -> Bool
not (Bool -> Bool)
-> ([Proto AnyUtxoPattern] -> Bool)
-> [Proto AnyUtxoPattern]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Proto AnyUtxoPattern] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Proto AnyUtxoPattern] -> Bool)
-> ([Proto AnyUtxoPattern] -> [Proto AnyUtxoPattern])
-> [Proto AnyUtxoPattern]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [Proto AnyUtxoPattern] -> [Proto AnyUtxoPattern]
forall a. Int -> [a] -> [a]
drop Int
maxUtxoQueryKeys ([Proto AnyUtxoPattern] -> Bool) -> [Proto AnyUtxoPattern] -> Bool
forall a b. (a -> b) -> a -> b
$ Proto UtxoPredicate -> [Proto AnyUtxoPattern]
predicateLeaves Proto UtxoPredicate
p ->
GrpcError -> Text -> m (CompiledUtxoPredicate, QueryUTxOFilter)
forall (m :: * -> *) a. MonadIO m => GrpcError -> Text -> m a
throwGrpcErrorWithMessage GrpcError
GrpcInvalidArgument (Text -> m (CompiledUtxoPredicate, QueryUTxOFilter))
-> Text -> m (CompiledUtxoPredicate, QueryUTxOFilter)
forall a b. (a -> b) -> a -> b
$
Text
"predicate names more than "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
maxUtxoQueryKeys
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" address terms (repeated addresses count); batch your requests"
| Just CompiledUtxoPredicate
compiled <- Proto UtxoPredicate -> Maybe CompiledUtxoPredicate
compileUtxoPredicate Proto UtxoPredicate
p ->
(CompiledUtxoPredicate, QueryUTxOFilter)
-> m (CompiledUtxoPredicate, QueryUTxOFilter)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CompiledUtxoPredicate
compiled, Set AddressAny -> QueryUTxOFilter
QueryUTxOByAddress (Set AddressAny -> QueryUTxOFilter)
-> Set AddressAny -> QueryUTxOFilter
forall a b. (a -> b) -> a -> b
$ CompiledUtxoPredicate -> Set AddressAny
compiledPredicateAddresses CompiledUtxoPredicate
compiled)
Maybe (Proto UtxoPredicate)
_ ->
GrpcError -> Text -> m (CompiledUtxoPredicate, QueryUTxOFilter)
forall (m :: * -> *) a. MonadIO m => GrpcError -> Text -> m a
throwGrpcErrorWithMessage
GrpcError
GrpcInvalidArgument
Text
"predicate too broad: must contain exact address match to avoid fetching the entire UTxO set"
nodeConnInfo <- grab
AnyCardanoEra era <- liftIO . throwExceptT $ determineEra nodeConnInfo
eon <- forEraInEon @Era era (error "Minimum Conway era required") pure
let target = Target point
forall point. Target point
VolatileTip
(utxo, chainPoint, chainBlockNo, systemStart, eraHistory) <- liftIO . (throwEither =<<) $ executeLocalStateQueryExpr nodeConnInfo target $ do
utxo <- throwEither =<< throwEither =<< queryUtxo (convert eon) utxoFilter
chainPoint <- throwEither =<< queryChainPoint
chainBlockNo <- throwEither =<< queryChainBlockNo
systemStart <- throwEither =<< querySystemStart
eraHistory <- throwEither =<< queryEraHistory
pure (utxo, chainPoint, chainBlockNo, systemStart, eraHistory)
timestamp <- slotToTimestamp systemStart eraHistory chainPoint
obtainCommonConstraints eon $ do
let (page, nextTok) =
paginateByTxIn (matchesCompiledPredicate compiledPredicate) (unUTxO utxo) startToken maxItems
pure $
defMessage
& U5c.ledgerTip .~ mkChainPointMsg chainPoint chainBlockNo timestamp
& U5c.items .~ map (uncurry txInTxOutToAnyUtxoData) page
& U5c.maybe'nextToken .~ nextTok
readGenesisMethod
:: MonadRpc e m
=> Proto UtxoRpc.ReadGenesisRequest
-> m (Proto UtxoRpc.ReadGenesisResponse)
readGenesisMethod :: forall e (m :: * -> *).
MonadRpc e m =>
Proto ReadGenesisRequest -> m (Proto ReadGenesisResponse)
readGenesisMethod Proto ReadGenesisRequest
_req = do
nodeKernelAccess <- m NodeKernelAccess
forall e (m :: * -> *). MonadRpc e m => m NodeKernelAccess
grabNodeKernelAccess
let genesisBundle@GenesisBundle
{ shelleyGenesisHash
, shelleyGenesis = (shelleyGenesisFile, shelleyGenesisCache)
} = genesisConfig nodeKernelAccess
shelleyGenesis <-
readThroughCache shelleyGenesisCache $
readShelleyGenesisWithInitialFunds shelleyGenesisFile shelleyGenesisHash
pure $
defMessage
& U5c.genesis .~ Crypto.hashToBytes (unGenesisHashShelley shelleyGenesisHash)
& U5c.caip2 .~ networkMagicToCaip2 (L.sgNetworkMagic shelleyGenesis)
& U5c.cardano .~ genesisBundleToProto genesisBundle shelleyGenesis
readShelleyGenesisWithInitialFunds
:: forall e m
. MonadRpc e m
=> ShelleyGenesisFile In
-> GenesisHashShelley
-> m L.ShelleyGenesis
readShelleyGenesisWithInitialFunds :: forall e (m :: * -> *).
MonadRpc e m =>
ShelleyGenesisFile 'In -> GenesisHashShelley -> m ShelleyGenesis
readShelleyGenesisWithInitialFunds shelleyGenesisFile :: ShelleyGenesisFile 'In
shelleyGenesisFile@(File String
path) GenesisHashShelley
bootGenesisHash = do
ShelleyConfig bootGenesis _ <-
(ShelleyGenesisError -> m ShelleyConfig)
-> (ShelleyConfig -> m ShelleyConfig)
-> Either ShelleyGenesisError ShelleyConfig
-> m ShelleyConfig
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ShelleyGenesisError -> m ShelleyConfig
forall a. ShelleyGenesisError -> m a
rejectGenesisFile ShelleyConfig -> m ShelleyConfig
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(Either ShelleyGenesisError ShelleyConfig -> m ShelleyConfig)
-> m (Either ShelleyGenesisError ShelleyConfig) -> m ShelleyConfig
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO (Either ShelleyGenesisError ShelleyConfig)
-> m (Either ShelleyGenesisError ShelleyConfig)
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ExceptT ShelleyGenesisError IO ShelleyConfig
-> IO (Either ShelleyGenesisError ShelleyConfig)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ShelleyGenesisFile 'In
-> Maybe GenesisHashShelley
-> ExceptT ShelleyGenesisError IO ShelleyConfig
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
MonadIOTransError ShelleyGenesisError t m =>
ShelleyGenesisFile 'In
-> Maybe GenesisHashShelley -> t m ShelleyConfig
readShelleyGenesis ShelleyGenesisFile 'In
shelleyGenesisFile (GenesisHashShelley -> Maybe GenesisHashShelley
forall a. a -> Maybe a
Just GenesisHashShelley
bootGenesisHash)))
let genesisDirectory = HasFS IO HandleIO -> SomeHasFS IO
forall h (m :: * -> *). Eq h => HasFS m h -> SomeHasFS m
SomeHasFS (HasFS IO HandleIO -> SomeHasFS IO)
-> (String -> HasFS IO HandleIO) -> String -> SomeHasFS IO
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MountPoint -> HasFS IO HandleIO
forall (m :: * -> *).
(MonadIO m, PrimState IO ~ PrimState m) =>
MountPoint -> HasFS m HandleIO
ioHasFS (MountPoint -> HasFS IO HandleIO)
-> (String -> MountPoint) -> String -> HasFS IO HandleIO
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> MountPoint
MountPoint (String -> SomeHasFS IO) -> String -> SomeHasFS IO
forall a b. (a -> b) -> a -> b
$ String -> String
takeDirectory String
path
either (rejectInitialFunds . displayException) pure
=<< tryAny (liftIO $ resolveShelleyInitialFunds genesisDirectory bootGenesis)
where
rejectGenesisFile :: ShelleyGenesisError -> m a
rejectGenesisFile :: forall a. ShelleyGenesisError -> m a
rejectGenesisFile = \case
ShelleyGenesisHashMismatch{} ->
GrpcError -> Text -> m a
forall (m :: * -> *) a. MonadIO m => GrpcError -> Text -> m a
throwGrpcErrorWithMessage GrpcError
GrpcFailedPrecondition (Text -> m a) -> Text -> m a
forall a b. (a -> b) -> a -> b
$
Text
"The Shelley genesis file "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. Show a => a -> Text
tshow String
path
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" has changed since the node started, so it no longer describes the genesis the node is running on."
ShelleyGenesisError
err -> GrpcError -> Text -> m a
forall (m :: * -> *) a. MonadIO m => GrpcError -> Text -> m a
throwGrpcErrorWithMessage GrpcError
GrpcInternal (Text -> m a) -> Text -> m a
forall a b. (a -> b) -> a -> b
$ ShelleyGenesisError -> Text
renderShelleyGenesisError ShelleyGenesisError
err
rejectInitialFunds :: String -> m a
rejectInitialFunds :: forall a. String -> m a
rejectInitialFunds String
reason =
GrpcError -> Text -> m a
forall (m :: * -> *) a. MonadIO m => GrpcError -> Text -> m a
throwGrpcErrorWithMessage GrpcError
GrpcInternal (Text -> m a) -> Text -> m a
forall a b. (a -> b) -> a -> b
$
Text
"Cannot resolve the initial funds of the Shelley genesis file "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. Show a => a -> Text
tshow String
path
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
reason
readEraSummaryMethod
:: MonadRpc e m
=> Proto UtxoRpc.ReadEraSummaryRequest
-> m (Proto UtxoRpc.ReadEraSummaryResponse)
readEraSummaryMethod :: forall e (m :: * -> *).
MonadRpc e m =>
Proto ReadEraSummaryRequest -> m (Proto ReadEraSummaryResponse)
readEraSummaryMethod Proto ReadEraSummaryRequest
_req = do
nodeKernelAccess <- m NodeKernelAccess
forall e (m :: * -> *). MonadRpc e m => m NodeKernelAccess
grabNodeKernelAccess
summary <- readHardForkSummary nodeKernelAccess
pure $
defMessage & U5c.cardano .~ eraSummariesToProto (nodeKernelSystemStart nodeKernelAccess) summary
networkMagicToCaip2 :: Word32 -> Text
networkMagicToCaip2 :: Word32 -> Text
networkMagicToCaip2 = \case
Word32
764824073 -> Text
"cardano:mainnet"
Word32
1 -> Text
"cardano:preprod"
Word32
2 -> Text
"cardano:preview"
Word32
magic -> Text
"cardano:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Word32 -> Text
forall a. Show a => a -> Text
tshow Word32
magic
paginateByTxIn
:: (a -> Bool)
-> Map TxIn a
-> Maybe Text
-> Int32
-> ([(TxIn, a)], Maybe Text)
paginateByTxIn :: forall a.
(a -> Bool)
-> Map TxIn a -> Maybe Text -> Int32 -> ([(TxIn, a)], Maybe Text)
paginateByTxIn a -> Bool
keep Map TxIn a
items Maybe Text
startToken Int32
maxItems = ([(TxIn, a)]
page, Maybe Text
nextToken)
where
afterCursor :: Map TxIn a
afterCursor =
Map TxIn a -> (TxIn -> Map TxIn a) -> Maybe TxIn -> Map TxIn a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Map TxIn a
items (\TxIn
cursor -> (Map TxIn a, Map TxIn a) -> Map TxIn a
forall a b. (a, b) -> b
snd ((Map TxIn a, Map TxIn a) -> Map TxIn a)
-> (Map TxIn a, Map TxIn a) -> Map TxIn a
forall a b. (a -> b) -> a -> b
$ TxIn -> Map TxIn a -> (Map TxIn a, Map TxIn a)
forall k a. Ord k => k -> Map k a -> (Map k a, Map k a)
Map.split TxIn
cursor Map TxIn a
items) (Maybe TxIn -> Map TxIn a) -> Maybe TxIn -> Map TxIn a
forall a b. (a -> b) -> a -> b
$
Either String TxIn -> Maybe TxIn
forall a b. Either a b -> Maybe b
hush (Either String TxIn -> Maybe TxIn)
-> (Text -> Either String TxIn) -> Text -> Maybe TxIn
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser TxIn -> Text -> Either String TxIn
forall a. Parser a -> Text -> Either String a
P.runParser Parser TxIn
parseTxIn (Text -> Maybe TxIn) -> Maybe Text -> Maybe TxIn
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe Text
startToken
matching :: [(TxIn, a)]
matching = ((TxIn, a) -> Bool) -> [(TxIn, a)] -> [(TxIn, a)]
forall a. (a -> Bool) -> [a] -> [a]
filter (a -> Bool
keep (a -> Bool) -> ((TxIn, a) -> a) -> (TxIn, a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxIn, a) -> a
forall a b. (a, b) -> b
snd) ([(TxIn, a)] -> [(TxIn, a)]) -> [(TxIn, a)] -> [(TxIn, a)]
forall a b. (a -> b) -> a -> b
$ Map TxIn a -> [(TxIn, a)]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map TxIn a
afterCursor
limit :: Int
limit = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (if Int32
maxItems Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
0 then Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
maxItems else Int
defaultPageSize) Int
maxPageSize
page :: [(TxIn, a)]
page = Int -> [(TxIn, a)] -> [(TxIn, a)]
forall a. Int -> [a] -> [a]
take Int
limit [(TxIn, a)]
matching
hasMore :: Bool
hasMore = Bool -> Bool
not (Bool -> Bool) -> ([(TxIn, a)] -> Bool) -> [(TxIn, a)] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(TxIn, a)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([(TxIn, a)] -> Bool) -> [(TxIn, a)] -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> [(TxIn, a)] -> [(TxIn, a)]
forall a. Int -> [a] -> [a]
drop Int
limit [(TxIn, a)]
matching
nextToken :: Maybe Text
nextToken = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard Bool
hasMore
Text -> Maybe Text
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Maybe Text)
-> ((TxIn, a) -> Text) -> (TxIn, a) -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxIn -> Text
renderTxIn (TxIn -> Text) -> ((TxIn, a) -> TxIn) -> (TxIn, a) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxIn, a) -> TxIn
forall a b. (a, b) -> a
fst ((TxIn, a) -> Maybe Text) -> (TxIn, a) -> Maybe Text
forall a b. (a -> b) -> a -> b
$ [(TxIn, a)] -> (TxIn, a)
forall a. HasCallStack => [a] -> a
last [(TxIn, a)]
page
defaultPageSize :: Int
defaultPageSize = Int
100
maxPageSize :: Int
maxPageSize = Int
10_000
type LocalQuery = LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO
runLocalQuery
:: MonadIO m
=> LocalNodeConnectInfo
-> LocalQuery a
-> m a
runLocalQuery :: forall (m :: * -> *) a.
MonadIO m =>
LocalNodeConnectInfo -> LocalQuery a -> m a
runLocalQuery LocalNodeConnectInfo
nodeConnInfo =
IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a)
-> (LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a
-> IO a)
-> LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a
-> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Either AcquiringFailure a -> IO a
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either AcquiringFailure a -> IO a)
-> (LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a
-> IO (Either AcquiringFailure a))
-> LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a
-> IO a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< LocalNodeConnectInfo
-> Target ChainPoint
-> LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a
-> IO (Either AcquiringFailure a)
forall a.
LocalNodeConnectInfo
-> Target ChainPoint
-> LocalStateQueryExpr BlockInMode ChainPoint QueryInMode () IO a
-> IO (Either AcquiringFailure a)
executeLocalStateQueryExpr LocalNodeConnectInfo
nodeConnInfo Target ChainPoint
forall point. Target point
VolatileTip)
queryInCurrentEra
:: (forall era. Era era -> LocalQuery a)
-> LocalQuery a
queryInCurrentEra :: forall a. (forall era. Era era -> LocalQuery a) -> LocalQuery a
queryInCurrentEra forall era. Era era -> LocalQuery a
query = do
AnyCardanoEra era <- Either UnsupportedNtcVersionError AnyCardanoEra
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO AnyCardanoEra
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either UnsupportedNtcVersionError AnyCardanoEra
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO AnyCardanoEra)
-> LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either UnsupportedNtcVersionError AnyCardanoEra)
-> LocalStateQueryExpr
BlockInMode ChainPoint QueryInMode () IO AnyCardanoEra
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LocalStateQueryExpr
BlockInMode
ChainPoint
QueryInMode
()
IO
(Either UnsupportedNtcVersionError AnyCardanoEra)
forall block point r.
LocalStateQueryExpr
block
point
QueryInMode
r
IO
(Either UnsupportedNtcVersionError AnyCardanoEra)
queryCurrentEra
eon <- forEraInEon @Era era (error "Minimum Conway era required") pure
query eon
slotToTimestamp
:: HasCallStack
=> MonadIO m
=> SystemStart -> EraHistory -> ChainPoint -> m UTCTime
slotToTimestamp :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
SystemStart -> EraHistory -> ChainPoint -> m UTCTime
slotToTimestamp SystemStart
systemStart EraHistory
eraHistory = \case
ChainPoint
ChainPointAtGenesis -> do
let SystemStart UTCTime
t = SystemStart
systemStart
UTCTime -> m UTCTime
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure UTCTime
t
ChainPoint SlotNo
slotNo Hash BlockHeader
_ ->
Either PastHorizonException UTCTime -> m UTCTime
forall e (m :: * -> *) a.
(Error e, HasCallStack, MonadIO m, Show e, Typeable e) =>
Either e a -> m a
throwEither (Either PastHorizonException UTCTime -> m UTCTime)
-> Either PastHorizonException UTCTime -> m UTCTime
forall a b. (a -> b) -> a -> b
$ SystemStart
-> EraHistory -> SlotNo -> Either PastHorizonException UTCTime
slotToUTCTime SystemStart
systemStart EraHistory
eraHistory SlotNo
slotNo