Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP chain observer #1815

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ changes.

- Bump docusaurus version

- Add blockfrost support to `hydra-chain-observer`, to follow the chain via Blockfrost API.
- Changes to `hydra-chain-observer`:
- Add blockfrost support to follow the chain via Blockfrost API.
- Submit observations to a `hydra-explorer` via optional `--explorer` option.

- Fix `bench-e2e single` benchmarks and only use `--output-directory` to keep
the whole benchmark state.
Expand Down
1 change: 1 addition & 0 deletions hydra-cardano-api/hydra-cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ library
Hydra.Cardano.Api.Hash
Hydra.Cardano.Api.Network
Hydra.Cardano.Api.NetworkId
Hydra.Cardano.Api.NetworkMagic
Hydra.Cardano.Api.PolicyId
Hydra.Cardano.Api.Prelude
Hydra.Cardano.Api.Pretty
Expand Down
1 change: 1 addition & 0 deletions hydra-cardano-api/src/Hydra/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import Hydra.Cardano.Api.CtxTx as Extras
import Hydra.Cardano.Api.ExecutionUnits as Extras
import Hydra.Cardano.Api.Hash as Extras
import Hydra.Cardano.Api.NetworkId ()
import Hydra.Cardano.Api.NetworkMagic ()
import Hydra.Cardano.Api.PolicyId as Extras
import Hydra.Cardano.Api.ReferenceScript as Extras
import Hydra.Cardano.Api.ScriptData as Extras
Expand Down
14 changes: 11 additions & 3 deletions hydra-cardano-api/src/Hydra/Cardano/Api/NetworkId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module Hydra.Cardano.Api.NetworkId where

import Hydra.Cardano.Api.Prelude

import Data.Aeson (Value (Number, String), object, withObject, (.:), (.=))
import Data.Aeson (Value (String), object, withObject, (.:), (.=))
import Hydra.Cardano.Api.NetworkMagic ()
import Test.QuickCheck (oneof)

-- * Orphans

Expand All @@ -14,13 +16,19 @@ instance ToJSON NetworkId where
Testnet magic ->
object
[ "tag" .= String "Testnet"
, "magic" .= Number (fromIntegral $ unNetworkMagic magic)
, "magic" .= toJSON magic
]

instance FromJSON NetworkId where
parseJSON = withObject "NetworkId" $ \o -> do
tag <- o .: "tag"
case tag :: Text of
"Mainnet" -> pure Mainnet
"Testnet" -> Testnet . NetworkMagic <$> o .: "magic"
"Testnet" -> Testnet <$> o .: "magic"
_ -> fail "Expected tag to be Mainnet | Testnet"

instance Arbitrary NetworkId where
arbitrary = oneof [pure Mainnet, Testnet <$> arbitrary]
shrink = \case
Mainnet -> []
Testnet magic -> Testnet <$> shrink magic
19 changes: 19 additions & 0 deletions hydra-cardano-api/src/Hydra/Cardano/Api/NetworkMagic.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{-# OPTIONS_GHC -Wno-orphans #-}

module Hydra.Cardano.Api.NetworkMagic where

import Cardano.Api (NetworkMagic (..))
import Data.Aeson (FromJSON (..), ToJSON (..))
import Test.QuickCheck (Arbitrary (..))

-- * Orphans

instance ToJSON NetworkMagic where
toJSON (NetworkMagic magic) = toJSON magic

instance FromJSON NetworkMagic where
parseJSON = fmap NetworkMagic . parseJSON

instance Arbitrary NetworkMagic where
arbitrary = NetworkMagic <$> arbitrary
shrink (NetworkMagic x) = NetworkMagic <$> shrink x
33 changes: 18 additions & 15 deletions hydra-chain-observer/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
# Hydra Chain Observer

A lightweight executable designed to connect to a blockchain, such as the `hydra-node`, and streams chain observations as traces to `stdout`.

It supports two modes of operation: **Direct** connection to a node via socket, and connection through **Blockfrost** API.

## Direct Mode
In both modes, reporting observations to a [`hydra-explorer`](https://github.com/cardano-scaling/hydra-explorer) can be enabled.

To run the observer in Direct Mode, provide the following arguments:
- `--node-socket`: path to the node socket file.
- network id: `--testnet-magic` (with magic number) for the testnet or `--mainnet` for the mainnet.
- (optional) `--start-chain-from`: specify a chain point (SLOT.HEADER_HASH) to start observing from.
## Direct Mode

For example:
To run the observer directly connected to a `cardano-node`, use the `--node-socket` option and specify the network id via `--mainnet` or `--testnet-magic`. Optionally, you can specify a starting point to observe usin `--start-chain-from`:

``` shell
hydra-chain-observer direct \
hydra-chain-observer \
--node-socket testnets/preprod/node.socket \
--testnet-magic 1 \
--start-chain-from "41948777.5d34af0f42be9823ebd35c2d83d5d879c5615ac17f7158bb9aa4ef89072455a7"
```


## Blockfrost Mode

To run the observer in Blockfrost Mode, provide the following arguments:
- `--project-path`: file path to your Blockfrost project API token hash.
> expected to be prefixed with environment (e.g. testnetA3C2E...)
- (optional) `--start-chain-from`: specify a chain point (SLOT.HEADER_HASH) to start observing from.

For example:
To run a chain observer using [blockfrost](blockfrost.io), use the `--blockfrost-project-path` option to point to a file containing your Blockfrost project API token (e.g. testnetA3C2E...). Optionally, you can specify a starting point to observe usin `--start-chain-from`:

``` shell
hydra-chain-observer blockfrost \
--project-path $PROJECT_TOKEN_HASH_PATH \
--blockfrost-project-path $PROJECT_TOKEN_PATH \
--start-chain-from "41948777.5d34af0f42be9823ebd35c2d83d5d879c5615ac17f7158bb9aa4ef89072455a7"
```

## Report to hydra-explorer

Using the `--explorer` argument we can specify a hostname / port for a `hydra-explorer` instance to report observations to. For example using a `direct` observer:

``` shell
hydra-chain-observer \
--node-socket testnets/preview/node.socket \
--testnet-magic 2 \
--start-chain-from "49533501.e364500a42220ea47314215679b7e42e9bbb81fa69d1366fe738d8aef900f7ee" \
--explorer http://0.0.0.0:8080
```
6 changes: 3 additions & 3 deletions hydra-chain-observer/exe/Main.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Main where

import Hydra.ChainObserver qualified
import Hydra.ChainObserver.NodeClient (defaultObserverHandler)
import Hydra.Prelude

import Hydra.ChainObserver qualified

main :: IO ()
main = Hydra.ChainObserver.main defaultObserverHandler
main = Hydra.ChainObserver.main
9 changes: 9 additions & 0 deletions hydra-chain-observer/golden/OnChainTx/OnAbortTx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"samples": [
{
"headId": "c7e3d62268804fc24729407e02a939c7",
"tag": "OnAbortTx"
}
],
"seed": -827095779
}
11 changes: 11 additions & 0 deletions hydra-chain-observer/golden/OnChainTx/OnCloseTx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"samples": [
{
"contestationDeadline": "1864-05-11T13:14:04.480927225587Z",
"headId": "c8dbcee16850146d996f4f05dbbd1a4c",
"snapshotNumber": 10,
"tag": "OnCloseTx"
}
],
"seed": -1105831259
}
9 changes: 9 additions & 0 deletions hydra-chain-observer/golden/OnChainTx/OnCollectComTx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"samples": [
{
"headId": "18c22cf884f4d8ff5ed450bdb188639d",
"tag": "OnCollectComTx"
}
],
"seed": 629191208
}
Loading
Loading