Skip to content

Commit 46c6894

Browse files
committed
Abstract over the chain backend in e2e tests
Signed-off-by: Sasha Bogicevic <[email protected]>
1 parent 43d0a86 commit 46c6894

File tree

3 files changed

+81
-46
lines changed

3 files changed

+81
-46
lines changed

hydra-cluster/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ The `HYDRA_CONFIG_DIR` environment variable is used to control where the executa
3131
for its configuration files. By default those files are resolved using the cabal package's
3232
data structure which is not always convenient.
3333

34+
The `HYDRA_BACKEND` environment variable is used to choose over which backend we will run our
35+
end-to-end tests. Possible values are _direct_ and _blockfrost_ (eg. export HYDRA_BACKEND="blockfrost").
36+
If this env variable is not set, the tests will default to _direct_ backend.
37+
3438
## Smoke Testing
3539

3640
The `hydra-cluster` executable spins up a `cardano-node` as a network
3741
participant which synchronizes the block chain and then executes a
3842
single scenario (single party, full life cycle) using funds available
39-
to the `config/credentials/faucet.sk` on that network. The Hydra nodes
40-
can reference pre-existing contracts living at some well-known
43+
to the `config/credentials/faucet.sk` on that network.
44+
45+
Smoke tests can also run using _Blockfrost_ in which case there is no need to
46+
start `cardano-node`.
47+
48+
The Hydra nodes can reference pre-existing contracts living at some well-known
4149
transaction or can post a new transaction to use those contracts.
4250

4351
:warning: do not provide actual funds to this faucet address as the
@@ -153,7 +161,7 @@ P95: 19.81722345ms
153161
P50: 18.532922ms
154162
Invalid txs: 0
155163
Writing report to: out/end-to-end-benchmarks.md
156-
line 0: warning: Cannot find or open file "out/system.csv"
164+
line 0: warning: Cannot find or open file "out/system.csv"
157165
Created plot: out/results.png
158166
```
159167

hydra-cluster/src/CardanoNode.hs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ import Hydra.Cardano.Api (
2424
import Hydra.Cardano.Api qualified as Api
2525
import Hydra.Chain.Backend (ChainBackend)
2626
import Hydra.Chain.Backend qualified as Backend
27+
import Hydra.Chain.Blockfrost (BlockfrostBackend (..))
2728
import Hydra.Chain.Direct (DirectBackend (..))
2829
import Hydra.Cluster.Fixture (KnownNetwork (..), toNetworkId)
2930
import Hydra.Cluster.Util (readConfigFile)
30-
import Hydra.Options (DirectOptions (..))
31+
import Hydra.Options (BlockfrostOptions (..), DirectOptions (..))
3132
import Network.HTTP.Simple (getResponseBody, httpBS, parseRequestThrow)
3233
import System.Directory (createDirectoryIfMissing, doesFileExist, removeFile)
3334
import System.Exit (ExitCode (..))
@@ -159,6 +160,31 @@ withCardanoNodeDevnet tracer stateDirectory action = do
159160
args <- setupCardanoDevnet stateDirectory
160161
withCardanoNode tracer stateDirectory args action
161162

163+
withBlockfrostBackend ::
164+
Tracer IO NodeLog ->
165+
-- | State directory in which credentials, db & logs are persisted.
166+
FilePath ->
167+
(NominalDiffTime -> BlockfrostBackend -> IO a) ->
168+
IO a
169+
withBlockfrostBackend _tracer stateDirectory action = do
170+
args <- setupCardanoDevnet stateDirectory
171+
shelleyGenesis <- readFileBS >=> unsafeDecodeJson $ stateDirectory </> nodeShelleyGenesisFile args
172+
let backend = BlockfrostBackend $ BlockfrostOptions{projectPath = ".." </> Backend.blockfrostProjectPath}
173+
action (getShelleyGenesisBlockTime shelleyGenesis) backend
174+
175+
withBackend ::
176+
forall a.
177+
Tracer IO NodeLog ->
178+
FilePath ->
179+
(forall backend. ChainBackend backend => NominalDiffTime -> backend -> IO a) ->
180+
IO a
181+
withBackend tracer stateDirectory action = do
182+
backend <- lookupEnv "HYDRA_BACKEND"
183+
case backend of
184+
Just "direct" -> withCardanoNodeDevnet tracer stateDirectory action
185+
Just "blockfrost" -> withBlockfrostBackend tracer stateDirectory action
186+
_ -> withCardanoNodeDevnet tracer stateDirectory action
187+
162188
-- | Run a cardano-node as normal network participant on a known network.
163189
withCardanoNodeOnKnownNetwork ::
164190
Tracer IO NodeLog ->
@@ -320,12 +346,13 @@ withCardanoNode tr stateDirectory args action = do
320346
else do
321347
let magic = json ^?! key "networkMagic" . _Number
322348
Api.Testnet (Api.NetworkMagic $ truncate magic)
323-
-- Read expected time between blocks from shelley genesis
324-
getShelleyGenesisBlockTime :: Value -> NominalDiffTime
325-
getShelleyGenesisBlockTime json = do
326-
let slotLength = json ^?! key "slotLength" . _Number
327-
let activeSlotsCoeff = json ^?! key "activeSlotsCoeff" . _Number
328-
computeBlockTime (realToFrac slotLength) (toRational activeSlotsCoeff)
349+
350+
-- Read expected time between blocks from shelley genesis
351+
getShelleyGenesisBlockTime :: Value -> NominalDiffTime
352+
getShelleyGenesisBlockTime json = do
353+
let slotLength = json ^?! key "slotLength" . _Number
354+
let activeSlotsCoeff = json ^?! key "activeSlotsCoeff" . _Number
355+
computeBlockTime (realToFrac slotLength) (toRational activeSlotsCoeff)
329356

330357
-- | Compute the block time (expected time between blocks) given a slot length
331358
-- as diff time and active slot coefficient.

0 commit comments

Comments
 (0)