Skip to content

Commit

Permalink
Create a first pab-based chain client
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1bo committed Jun 14, 2021
1 parent e74eebc commit 3f34dcb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
5 changes: 4 additions & 1 deletion hydra-node/hydra-node.cabal
Expand Up @@ -55,6 +55,7 @@ common project-config
ScopedTypeVariables
StandaloneDeriving
TupleSections
TypeApplications
TypeFamilies
TypeSynonymInstances
ViewPatterns
Expand All @@ -77,6 +78,7 @@ library
exposed-modules:
Hydra.API.Server
Hydra.Chain
Hydra.Chain.ExternalPAB
Hydra.Chain.ZeroMQ
Hydra.Network
Hydra.Network.BroadcastToSelf
Expand Down Expand Up @@ -121,6 +123,7 @@ library
, optparse-applicative
, ouroboros-network-framework
, prometheus
, req
, shelley-spec-ledger
, shelley-spec-ledger-test
, stm
Expand Down Expand Up @@ -163,6 +166,7 @@ test-suite tests
import: project-config
hs-source-dirs: test
other-modules:
Hydra.Chain.ExternalPABSpec
Hydra.Chain.ZeroMQSpec
Hydra.FireForgetSpec
Hydra.BehaviorSpec
Expand Down Expand Up @@ -192,7 +196,6 @@ test-suite tests
, QuickCheck
, quickcheck-instances
, typed-protocols-examples
, HUnit
build-tool-depends:
hspec-discover:hspec-discover
ghc-options:
Expand Down
40 changes: 40 additions & 0 deletions hydra-node/src/Hydra/Chain/ExternalPAB.hs
@@ -0,0 +1,40 @@
module Hydra.Chain.ExternalPAB where

import Cardano.Prelude
import Hydra.Chain (Chain (Chain, postTx))
import Hydra.HeadLogic (OnChainTx (InitTx))
import Hydra.Ledger (Tx)
import Hydra.Logging (Tracer)
import Network.HTTP.Req (POST (..), ReqBodyJson (..), defaultHttpConfig, http, jsonResponse, port, req, responseBody, responseStatusCode, runReq, (/:))

data ExternalPABLog

withExternalPAB ::
Tx tx =>
Tracer IO ExternalPABLog ->
(OnChainTx tx -> IO ()) ->
(Chain tx IO -> IO a) ->
IO a
withExternalPAB _tracer _callback action = do
action $ Chain{postTx}
where
postTx = \case
InitTx _ -> loadCid >>= postInitTx
tx -> panic $ "should post " <> show tx

loadCid = readFile "/tmp/W1.cid"

-- TODO(SN): use MonadHttp, but clashes with MonadThrow
postInitTx :: Text -> IO ()
postInitTx cid = do
runReq defaultHttpConfig $ do
res <-
req
POST
(http "127.0.0.1" /: "api" /: "new" /: "contract" /: "instance" /: cid /: "endpoint" /: "init")
(ReqBodyJson ())
jsonResponse
(port 8080)
when (responseStatusCode res /= 200) $
panic "failed to postInitTx"
pure $ responseBody res
17 changes: 17 additions & 0 deletions hydra-node/test/Hydra/Chain/ExternalPABSpec.hs
@@ -0,0 +1,17 @@
module Hydra.Chain.ExternalPABSpec where

import Cardano.Prelude
import Hydra.Chain (Chain (..))
import Hydra.Chain.ExternalPAB (withExternalPAB)
import Hydra.HeadLogic (OnChainTx (..))
import Hydra.Ledger.Mock (MockTx)
import Hydra.Logging (nullTracer)
import Test.Hspec.Core.Spec (Spec, describe, it)

spec :: Spec
spec =
describe "ExternalPAB" $ do
it "publishes init tx using wallet 1" $ do
-- TODO(SN): launch hydra-pab as process
withExternalPAB nullTracer (panic "called back") $ \Chain{postTx} ->
postTx $ InitTx @MockTx (panic "unused")
2 changes: 1 addition & 1 deletion hydra-plutus/exe/hydra-pab/Main.hs
Expand Up @@ -54,7 +54,7 @@ main = void $

activateWallets = forM [alice, bob] $ \w -> do
(ContractInstanceId cid) <- Simulator.activateContract w HydraContract
let fn = 'W' : show (getWallet w) <> ".cid"
let fn = "/tmp/W" <> show (getWallet w) <> ".cid"
liftIO $ writeFile fn $ show cid
pure fn

Expand Down

0 comments on commit 3f34dcb

Please sign in to comment.