Skip to content

Commit 78ff0b5

Browse files
authored
Abstract over the chain backend in e2e tests (#2101)
fix #2100 --- <!-- Consider each and tick it off one way or the other --> * [x] CHANGELOG updated or not needed * [x] Documentation updated or not needed * [x] Haddocks updated or not needed * [x] No new TODOs introduced or explained herafter
2 parents 43d0a86 + 67e7260 commit 78ff0b5

File tree

3 files changed

+85
-46
lines changed

3 files changed

+85
-46
lines changed

hydra-cluster/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ 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+
38+
To run the e2e tests successfully using blockfrost backend there should be also
39+
a file named `blocfrost-project.txt` in the root of the repository with the
40+
appropriate api key for the network you want to run on.
41+
3442
## Smoke Testing
3543

3644
The `hydra-cluster` executable spins up a `cardano-node` as a network
3745
participant which synchronizes the block chain and then executes a
3846
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
47+
to the `config/credentials/faucet.sk` on that network.
48+
49+
Smoke tests can also run using _Blockfrost_ in which case there is no need to
50+
start `cardano-node`.
51+
52+
The Hydra nodes can reference pre-existing contracts living at some well-known
4153
transaction or can post a new transaction to use those contracts.
4254

4355
:warning: do not provide actual funds to this faucet address as the
@@ -153,7 +165,7 @@ P95: 19.81722345ms
153165
P50: 18.532922ms
154166
Invalid txs: 0
155167
Writing report to: out/end-to-end-benchmarks.md
156-
line 0: warning: Cannot find or open file "out/system.csv"
168+
line 0: warning: Cannot find or open file "out/system.csv"
157169
Created plot: out/results.png
158170
```
159171

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)