|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE OverloadedStrings #-} |
| 4 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 5 | +{-# LANGUAGE TypeApplications #-} |
| 6 | + |
| 7 | +module Cardano.Testnet.Test.Rpc.FetchBlock ( |
| 8 | + hprop_rpc_fetch_block, |
| 9 | +) |
| 10 | +where |
| 11 | + |
| 12 | +import Cardano.Api |
| 13 | +import qualified Cardano.Api.Experimental as Exp |
| 14 | + |
| 15 | +import Cardano.CLI.Type.Output (QueryTipLocalStateOutput (..)) |
| 16 | +import qualified Cardano.Rpc.Client as Rpc |
| 17 | +import qualified Cardano.Rpc.Proto.Api.UtxoRpc.Sync as U5c |
| 18 | +import Cardano.Testnet |
| 19 | + |
| 20 | +import Prelude |
| 21 | + |
| 22 | +import qualified Data.ByteString as BS |
| 23 | +import qualified Data.ByteString.Short as SBS |
| 24 | +import Data.Default.Class |
| 25 | +import Data.Word (Word64) |
| 26 | +import Lens.Micro |
| 27 | + |
| 28 | +import Testnet.Process.Run |
| 29 | +import Testnet.Property.Util (integrationRetryWorkspace) |
| 30 | +import Testnet.Start.Types |
| 31 | + |
| 32 | +import qualified Hedgehog as H |
| 33 | +import qualified Hedgehog.Extras as H |
| 34 | + |
| 35 | +-- | Run with: |
| 36 | +-- @TASTY_PATTERN='/RPC FetchBlock/' cabal test cardano-testnet-test@ |
| 37 | +-- |
| 38 | +hprop_rpc_fetch_block :: H.Property |
| 39 | +hprop_rpc_fetch_block = integrationRetryWorkspace 2 "rpc-fetch-block" $ \tempAbsBasePath' -> H.runWithDefaultWatchdog_ $ do |
| 40 | + conf@Conf{tempAbsPath} <- mkConf tempAbsBasePath' |
| 41 | + let tempAbsPath' = unTmpAbsPath tempAbsPath |
| 42 | + |
| 43 | + let era = Exp.ConwayEra |
| 44 | + sbe = convert era |
| 45 | + eraName = eraToString sbe |
| 46 | + creationOptions = def{creationEra = AnyShelleyBasedEra sbe} |
| 47 | + runtimeOptions = def{runtimeEnableRpc = RpcEnabled} |
| 48 | + |
| 49 | + TestnetRuntime |
| 50 | + { testnetMagic |
| 51 | + , testnetNodes = node0@TestnetNode{nodeSprocket} : _ |
| 52 | + } <- |
| 53 | + createAndRunTestnet creationOptions runtimeOptions conf |
| 54 | + |
| 55 | + execConfig <- mkExecConfig tempAbsPath' nodeSprocket testnetMagic |
| 56 | + rpcSocket <- H.note . unFile $ nodeRpcSocketPath node0 |
| 57 | + |
| 58 | + -- Get chain tip via CLI |
| 59 | + QueryTipLocalStateOutput{localStateChainTip} <- |
| 60 | + H.noteShowM $ execCliStdoutToJson execConfig [eraName, "query", "tip"] |
| 61 | + (slot, blockHash, blockNo) <- case localStateChainTip of |
| 62 | + ChainTipAtGenesis -> H.failure |
| 63 | + ChainTip (SlotNo tipSlot) (HeaderHash hash) (BlockNo bn) -> pure (tipSlot, SBS.fromShort hash, bn) |
| 64 | + |
| 65 | + H.note_ $ "Tip slot: " <> show slot |
| 66 | + H.note_ $ "Tip block number: " <> show blockNo |
| 67 | + H.note_ $ "Tip hash: " <> show (BS.length blockHash) <> " bytes" |
| 68 | + |
| 69 | + -- Call FetchBlock via gRPC |
| 70 | + let rpcServer = Rpc.ServerUnix rpcSocket |
| 71 | + blockRef = def & U5c.slot .~ slot & U5c.hash .~ blockHash |
| 72 | + request = def & U5c.ref .~ [blockRef] |
| 73 | + |
| 74 | + response <- H.evalIO . Rpc.withConnection def rpcServer $ \conn -> |
| 75 | + Rpc.nonStreaming conn (Rpc.rpc @(Rpc.Protobuf U5c.SyncService "fetchBlock")) request |
| 76 | + |
| 77 | + -- Verify response contains exactly one block |
| 78 | + let blocks = response ^. U5c.block |
| 79 | + length blocks H.=== 1 |
| 80 | + |
| 81 | + let block = head blocks |
| 82 | + |
| 83 | + -- Verify nativeBytes is non-empty |
| 84 | + let rawBytes = block ^. U5c.nativeBytes |
| 85 | + H.note_ $ "Block CBOR: " <> show (BS.length rawBytes) <> " bytes" |
| 86 | + H.assertWith rawBytes $ not . BS.null |
| 87 | + |
| 88 | + -- Verify cardano block header matches the requested tip |
| 89 | + block ^. U5c.cardano . U5c.header . U5c.slot H.=== slot |
| 90 | + block ^. U5c.cardano . U5c.header . U5c.hash H.=== blockHash |
| 91 | + |
| 92 | + -- height and timestamp are not set by the handler (not available from ChainDB without deserialising) |
| 93 | + -- so they default to 0 |
| 94 | + block ^. U5c.cardano . U5c.header . U5c.height H.=== 0 |
| 95 | + block ^. U5c.cardano . U5c.timestamp H.=== 0 |
0 commit comments