{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Actions that record their name when they run, and the values they return,
-- for testing which queries and block reads a field fill runs.
module Test.Cardano.Rpc.RecordedQuery where

import Cardano.Api
  ( BlockHeader (..)
  , BlockInMode (..)
  , ChainPoint (..)
  , Hash (HeaderHash)
  , SlotNo
  , getBlockHeader
  , toConsensusBlock
  )
import Cardano.Api.Consensus qualified as Consensus
import Cardano.Rpc.Proto.Api.UtxoRpc.Query qualified as U5c
import Cardano.Rpc.Server.Internal.FieldMask (FieldFill, FieldPath, resolve, runFill)
import Cardano.Rpc.Server.Internal.UtxoRpc.Type (UtxoFields (..), mkChainPointMsg)

import Cardano.Slotting.Slot (WithOrigin (..))
import Ouroboros.Consensus.Block (getHeader)

import RIO
import RIO.Writer (Writer, runWriter, tell)

import Data.ProtoLens (Message, defMessage)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import Network.GRPC.Spec (getProto)

-- | Run a fill for the mask paths, recording the session and each query it runs.
runRecordedFill
  :: Message msg
  => [FieldPath]
  -- ^ Field mask paths, rooted at the response
  -> FieldFill (Writer [Text]) msg
  -- ^ Fills the response from recorded actions
  -> (msg, [Text])
runRecordedFill :: forall msg.
Message msg =>
[FieldPath]
-> FieldFill (Writer [FieldPath]) msg -> (msg, [FieldPath])
runRecordedFill [FieldPath]
paths = Writer [FieldPath] msg -> (msg, [FieldPath])
forall w a. Writer w a -> (a, w)
runWriter (Writer [FieldPath] msg -> (msg, [FieldPath]))
-> (FieldFill (Writer [FieldPath]) msg -> Writer [FieldPath] msg)
-> FieldFill (Writer [FieldPath]) msg
-> (msg, [FieldPath])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall x. Writer [FieldPath] x -> Writer [FieldPath] x)
-> FieldMaskTree msg
-> FieldFill (Writer [FieldPath]) msg
-> Writer [FieldPath] msg
forall (n :: * -> *) msg (m :: * -> *).
(Applicative n, Message msg) =>
(forall x. m x -> n x)
-> FieldMaskTree msg -> FieldFill m msg -> n msg
runFill Writer [FieldPath] x -> Writer [FieldPath] x
forall x. Writer [FieldPath] x -> Writer [FieldPath] x
session ([FieldPath] -> FieldMaskTree msg
forall msg. Message msg => [FieldPath] -> FieldMaskTree msg
resolve [FieldPath]
paths)

-- | Read block components from a stored block as ChainDB would, recording each
-- component read.
readRecordedComponent
  :: StoredBlock
  -- ^ The block to read from
  -> Consensus.BlockComponent (Consensus.CardanoBlock Consensus.StandardCrypto) a
  -- ^ The components to read
  -> Writer [Text] a
readRecordedComponent :: forall a.
StoredBlock
-> BlockComponent (CardanoBlock StandardCrypto) a
-> Writer [FieldPath] a
readRecordedComponent StoredBlock
stored = \case
  BlockComponent (CardanoBlock StandardCrypto) a
Consensus.GetRawBlock -> FieldPath -> a -> Writer [FieldPath] a
forall a. FieldPath -> a -> Writer [FieldPath] a
recorded FieldPath
"GetRawBlock" (a -> Writer [FieldPath] a)
-> (ByteString -> a) -> ByteString -> Writer [FieldPath] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> a
ByteString -> ByteString
fromStrictBytes (ByteString -> Writer [FieldPath] a)
-> ByteString -> Writer [FieldPath] a
forall a b. (a -> b) -> a -> b
$ StoredBlock -> ByteString
storedBlockRawBytes StoredBlock
stored
  BlockComponent (CardanoBlock StandardCrypto) a
Consensus.GetBlock -> FieldPath -> a -> Writer [FieldPath] a
forall a. FieldPath -> a -> Writer [FieldPath] a
recorded FieldPath
"GetBlock" a
CardanoBlock StandardCrypto
consensusBlock
  BlockComponent (CardanoBlock StandardCrypto) a
Consensus.GetHeader -> FieldPath -> a -> Writer [FieldPath] a
forall a. FieldPath -> a -> Writer [FieldPath] a
recorded FieldPath
"GetHeader" (a -> Writer [FieldPath] a) -> a -> Writer [FieldPath] a
forall a b. (a -> b) -> a -> b
$ CardanoBlock StandardCrypto -> Header (CardanoBlock StandardCrypto)
forall blk. GetHeader blk => blk -> Header blk
getHeader CardanoBlock StandardCrypto
consensusBlock
  BlockComponent (CardanoBlock StandardCrypto) a
Consensus.GetSlot -> FieldPath -> a -> Writer [FieldPath] a
forall a. FieldPath -> a -> Writer [FieldPath] a
recorded FieldPath
"GetSlot" (a -> Writer [FieldPath] a)
-> ((SlotNo, Hash BlockHeader) -> a)
-> (SlotNo, Hash BlockHeader)
-> Writer [FieldPath] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SlotNo, Hash BlockHeader) -> a
(SlotNo, Hash BlockHeader) -> SlotNo
forall a b. (a, b) -> a
fst ((SlotNo, Hash BlockHeader) -> Writer [FieldPath] a)
-> (SlotNo, Hash BlockHeader) -> Writer [FieldPath] a
forall a b. (a -> b) -> a -> b
$ StoredBlock -> (SlotNo, Hash BlockHeader)
storedBlockPoint StoredBlock
stored
  BlockComponent (CardanoBlock StandardCrypto) a
Consensus.GetHash -> FieldPath -> a -> Writer [FieldPath] a
forall a. FieldPath -> a -> Writer [FieldPath] a
recorded FieldPath
"GetHash" (a -> Writer [FieldPath] a)
-> ((SlotNo, Hash BlockHeader) -> a)
-> (SlotNo, Hash BlockHeader)
-> Writer [FieldPath] a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash BlockHeader -> a
Hash BlockHeader
-> OneEraHash (ByronBlock : CardanoShelleyEras StandardCrypto)
forall {xs :: [*]}. Hash BlockHeader -> OneEraHash xs
toOneEraHash (Hash BlockHeader -> a)
-> ((SlotNo, Hash BlockHeader) -> Hash BlockHeader)
-> (SlotNo, Hash BlockHeader)
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SlotNo, Hash BlockHeader) -> Hash BlockHeader
forall a b. (a, b) -> b
snd ((SlotNo, Hash BlockHeader) -> Writer [FieldPath] a)
-> (SlotNo, Hash BlockHeader) -> Writer [FieldPath] a
forall a b. (a -> b) -> a -> b
$ StoredBlock -> (SlotNo, Hash BlockHeader)
storedBlockPoint StoredBlock
stored
  Consensus.GetPure a
value -> a -> Writer [FieldPath] a
forall a. a -> WriterT [FieldPath] Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value
  Consensus.GetApply BlockComponent (CardanoBlock StandardCrypto) (a1 -> a)
doRead BlockComponent (CardanoBlock StandardCrypto) a1
component ->
    StoredBlock
-> BlockComponent (CardanoBlock StandardCrypto) (a1 -> a)
-> Writer [FieldPath] (a1 -> a)
forall a.
StoredBlock
-> BlockComponent (CardanoBlock StandardCrypto) a
-> Writer [FieldPath] a
readRecordedComponent StoredBlock
stored BlockComponent (CardanoBlock StandardCrypto) (a1 -> a)
doRead Writer [FieldPath] (a1 -> a)
-> WriterT [FieldPath] Identity a1 -> Writer [FieldPath] a
forall a b.
WriterT [FieldPath] Identity (a -> b)
-> WriterT [FieldPath] Identity a -> WriterT [FieldPath] Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> StoredBlock
-> BlockComponent (CardanoBlock StandardCrypto) a1
-> WriterT [FieldPath] Identity a1
forall a.
StoredBlock
-> BlockComponent (CardanoBlock StandardCrypto) a
-> Writer [FieldPath] a
readRecordedComponent StoredBlock
stored BlockComponent (CardanoBlock StandardCrypto) a1
component
  BlockComponent (CardanoBlock StandardCrypto) a
_ -> String -> Writer [FieldPath] a
forall a. HasCallStack => String -> a
error String
"readRecordedComponent: no handler reads this block component"
 where
  consensusBlock :: CardanoBlock StandardCrypto
consensusBlock = BlockInMode -> CardanoBlock StandardCrypto
forall block.
(CardanoBlock StandardCrypto ~ block) =>
BlockInMode -> block
toConsensusBlock (BlockInMode -> CardanoBlock StandardCrypto)
-> BlockInMode -> CardanoBlock StandardCrypto
forall a b. (a -> b) -> a -> b
$ StoredBlock -> BlockInMode
storedBlockInMode StoredBlock
stored
  toOneEraHash :: Hash BlockHeader -> OneEraHash xs
toOneEraHash (HeaderHash ShortByteString
bytes) = ShortByteString -> OneEraHash xs
forall k (xs :: [k]). ShortByteString -> OneEraHash xs
Consensus.OneEraHash ShortByteString
bytes

-- | Run recorded actions in 'IO', appending what they record to the log.
logRecorded
  :: IORef [Text]
  -- ^ The log
  -> Writer [Text] a
  -- ^ The recorded actions
  -> IO a
logRecorded :: forall a. IORef [FieldPath] -> Writer [FieldPath] a -> IO a
logRecorded IORef [FieldPath]
logRef Writer [FieldPath] a
doRecorded = do
  let (a
value, [FieldPath]
names) = Writer [FieldPath] a -> (a, [FieldPath])
forall w a. Writer w a -> (a, w)
runWriter Writer [FieldPath] a
doRecorded
  IORef [FieldPath] -> ([FieldPath] -> [FieldPath]) -> IO ()
forall (m :: * -> *) a. MonadIO m => IORef a -> (a -> a) -> m ()
modifyIORef' IORef [FieldPath]
logRef ([FieldPath] -> [FieldPath] -> [FieldPath]
forall a. Semigroup a => a -> a -> a
<> [FieldPath]
names)
  a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
value

-- | An action that records its name when it runs.
recorded
  :: Text
  -- ^ The name to record
  -> a
  -- ^ The result of the action
  -> Writer [Text] a
recorded :: forall a. FieldPath -> a -> Writer [FieldPath] a
recorded FieldPath
name a
value = [FieldPath] -> WriterT [FieldPath] Identity ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [FieldPath
name] WriterT [FieldPath] Identity ()
-> a -> WriterT [FieldPath] Identity a
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> a
value

-- | A runner that records each session it opens.
session
  :: Writer [Text] a
  -- ^ The actions to run in the session
  -> Writer [Text] a
session :: forall x. Writer [FieldPath] x -> Writer [FieldPath] x
session = ([FieldPath] -> WriterT [FieldPath] Identity ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [FieldPath
"session"] WriterT [FieldPath] Identity ()
-> WriterT [FieldPath] Identity a -> WriterT [FieldPath] Identity a
forall a b.
WriterT [FieldPath] Identity a
-> WriterT [FieldPath] Identity b -> WriterT [FieldPath] Identity b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*>)

-- | A block as ChainDB stores it: its point, which ChainDB keeps in its index,
-- its raw bytes and the parsed block. The fields are lazy, so a test can leave
-- out a part that must not be read.
data StoredBlock = StoredBlock
  { StoredBlock -> (SlotNo, Hash BlockHeader)
storedBlockPoint :: (SlotNo, Hash BlockHeader)
  , StoredBlock -> ByteString
storedBlockRawBytes :: ByteString
  , StoredBlock -> BlockInMode
storedBlockInMode :: BlockInMode
  }

-- | Store a raw block and its parsed form at the point its header names.
toStoredBlock
  :: (ByteString, BlockInMode)
  -- ^ The raw block and the same block parsed
  -> StoredBlock
toStoredBlock :: (ByteString, BlockInMode) -> StoredBlock
toStoredBlock (ByteString
rawBytes, blockInMode :: BlockInMode
blockInMode@(BlockInMode CardanoEra era
_ Block era
block)) = do
  let BlockHeader SlotNo
slot Hash BlockHeader
headerHash BlockNo
_ = Block era -> BlockHeader
forall era. Block era -> BlockHeader
getBlockHeader Block era
block
  (SlotNo, Hash BlockHeader)
-> ByteString -> BlockInMode -> StoredBlock
StoredBlock (SlotNo
slot, Hash BlockHeader
headerHash) ByteString
rawBytes BlockInMode
blockInMode

-- | The chain tip that the recorded tip queries return.
realTip :: U5c.ChainPoint
realTip :: ChainPoint
realTip = Proto ChainPoint -> ChainPoint
forall msg. Proto msg -> msg
getProto (Proto ChainPoint -> ChainPoint) -> Proto ChainPoint -> ChainPoint
forall a b. (a -> b) -> a -> b
$ ChainPoint -> WithOrigin BlockNo -> UTCTime -> Proto ChainPoint
mkChainPointMsg ChainPoint
ChainPointAtGenesis WithOrigin BlockNo
forall t. WithOrigin t
Origin (NominalDiffTime -> UTCTime
posixSecondsToUTCTime NominalDiffTime
1)

-- | The UTxO entries that the recorded UTxO queries return.
realFields :: [UtxoFields]
realFields :: [UtxoFields]
realFields =
  [ UtxoFields
      { utxoFieldsRef :: TxoRef
utxoFieldsRef = TxoRef
forall msg. Message msg => msg
defMessage TxoRef -> (TxoRef -> TxoRef) -> TxoRef
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxoRef ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "hash" a) =>
LensLike' f s a
U5c.hash LensLike' Identity TxoRef ByteString
-> ByteString -> TxoRef -> TxoRef
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString
"tx-1" TxoRef -> (TxoRef -> TxoRef) -> TxoRef
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxoRef Word32
forall (f :: * -> *) s a.
(Functor f, HasField s "index" a) =>
LensLike' f s a
U5c.index LensLike' Identity TxoRef Word32 -> Word32 -> TxoRef -> TxoRef
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Word32
0
      , utxoFieldsNativeBytes :: ByteString
utxoFieldsNativeBytes = ByteString
"native-1"
      , utxoFieldsCardano :: TxOutput
utxoFieldsCardano =
          TxOutput
forall msg. Message msg => msg
defMessage
            TxOutput -> (TxOutput -> TxOutput) -> TxOutput
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxOutput ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "address" a) =>
LensLike' f s a
U5c.address LensLike' Identity TxOutput ByteString
-> ByteString -> TxOutput -> TxOutput
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString
"address-1"
            TxOutput -> (TxOutput -> TxOutput) -> TxOutput
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxOutput BigInt
forall (f :: * -> *) s a.
(Functor f, HasField s "coin" a) =>
LensLike' f s a
U5c.coin LensLike' Identity TxOutput BigInt
-> ((Int64 -> Identity Int64) -> BigInt -> Identity BigInt)
-> (Int64 -> Identity Int64)
-> TxOutput
-> Identity TxOutput
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int64 -> Identity Int64) -> BigInt -> Identity BigInt
forall (f :: * -> *) s a.
(Functor f, HasField s "int" a) =>
LensLike' f s a
U5c.int ((Int64 -> Identity Int64) -> TxOutput -> Identity TxOutput)
-> Int64 -> TxOutput -> TxOutput
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int64
100
      }
  , UtxoFields
      { utxoFieldsRef :: TxoRef
utxoFieldsRef = TxoRef
forall msg. Message msg => msg
defMessage TxoRef -> (TxoRef -> TxoRef) -> TxoRef
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxoRef ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "hash" a) =>
LensLike' f s a
U5c.hash LensLike' Identity TxoRef ByteString
-> ByteString -> TxoRef -> TxoRef
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString
"tx-2" TxoRef -> (TxoRef -> TxoRef) -> TxoRef
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxoRef Word32
forall (f :: * -> *) s a.
(Functor f, HasField s "index" a) =>
LensLike' f s a
U5c.index LensLike' Identity TxoRef Word32 -> Word32 -> TxoRef -> TxoRef
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Word32
1
      , utxoFieldsNativeBytes :: ByteString
utxoFieldsNativeBytes = ByteString
"native-2"
      , utxoFieldsCardano :: TxOutput
utxoFieldsCardano =
          TxOutput
forall msg. Message msg => msg
defMessage
            TxOutput -> (TxOutput -> TxOutput) -> TxOutput
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxOutput ByteString
forall (f :: * -> *) s a.
(Functor f, HasField s "address" a) =>
LensLike' f s a
U5c.address LensLike' Identity TxOutput ByteString
-> ByteString -> TxOutput -> TxOutput
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString
"address-2"
            TxOutput -> (TxOutput -> TxOutput) -> TxOutput
forall a b. a -> (a -> b) -> b
& LensLike' Identity TxOutput BigInt
forall (f :: * -> *) s a.
(Functor f, HasField s "coin" a) =>
LensLike' f s a
U5c.coin LensLike' Identity TxOutput BigInt
-> ((Int64 -> Identity Int64) -> BigInt -> Identity BigInt)
-> (Int64 -> Identity Int64)
-> TxOutput
-> Identity TxOutput
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int64 -> Identity Int64) -> BigInt -> Identity BigInt
forall (f :: * -> *) s a.
(Functor f, HasField s "int" a) =>
LensLike' f s a
U5c.int ((Int64 -> Identity Int64) -> TxOutput -> Identity TxOutput)
-> Int64 -> TxOutput -> TxOutput
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int64
200
      }
  ]