Skip to content

Commit b5dff26

Browse files
committed
Fix compilation of several Hydra modules
Some instances are incomplete as it is missing the SerialiseAsRawBytesError from cardano-api to produce the proper Left values. See IntersectMBO/cardano-node#5085
1 parent 1617d72 commit b5dff26

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

hydra-node/src/Hydra/Chain.hs

+9-9
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ newtype HeadId = HeadId ByteString
8080

8181
instance SerialiseAsRawBytes HeadId where
8282
serialiseToRawBytes (HeadId bytes) = bytes
83-
deserialiseFromRawBytes _ = Just . HeadId
83+
deserialiseFromRawBytes _ = Right . HeadId
8484

8585
instance HasTypeProxy HeadId where
8686
data AsType HeadId = AsHeadId
@@ -189,14 +189,14 @@ instance Arbitrary ChainSlot where
189189

190190
-- | Handle to interface with the main chain network
191191
newtype Chain tx m = Chain
192-
{ -- | Construct and send a transaction to the main chain corresponding to the
193-
-- given 'PostChainTx' description and the current 'ChainState'. This
194-
-- function is not expected to block, so it is only responsible for
195-
-- submitting, but it should validate the created transaction against a
196-
-- reasonable local view of the chain and throw an exception when invalid.
197-
--
198-
-- Does at least throw 'PostTxError'.
199-
postTx :: (IsChainState tx, MonadThrow m) => ChainStateType tx -> PostChainTx tx -> m ()
192+
{ postTx :: (IsChainState tx, MonadThrow m) => ChainStateType tx -> PostChainTx tx -> m ()
193+
-- ^ Construct and send a transaction to the main chain corresponding to the
194+
-- given 'PostChainTx' description and the current 'ChainState'. This
195+
-- function is not expected to block, so it is only responsible for
196+
-- submitting, but it should validate the created transaction against a
197+
-- reasonable local view of the chain and throw an exception when invalid.
198+
--
199+
-- Does at least throw 'PostTxError'.
200200
}
201201

202202
data ChainEvent tx

hydra-node/src/Hydra/Crypto.hs

+7-4
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import Hydra.Cardano.Api (
6161
serialiseToRawBytesHexText,
6262
)
6363
import qualified Hydra.Contract.HeadState as OnChain
64-
import qualified Plutus.V2.Ledger.Api as Plutus
64+
import qualified PlutusLedgerApi.V2 as Plutus
6565
import Test.QuickCheck (vectorOf)
6666
import Test.QuickCheck.Instances.ByteString ()
6767
import Text.Show (Show (..))
@@ -85,7 +85,8 @@ instance SerialiseAsRawBytes (Hash HydraKey) where
8585
serialiseToRawBytes (HydraKeyHash vkh) = hashToBytes vkh
8686

8787
deserialiseFromRawBytes (AsHash AsHydraKey) bs =
88-
HydraKeyHash <$> hashFromBytes bs
88+
maybe (error "TODO: SerialiseAsRawBytesError, but constructor not exported") Right $
89+
HydraKeyHash <$> hashFromBytes bs
8990

9091
instance Key HydraKey where
9192
-- Hydra verification key, which can be used to 'verify' signed messages.
@@ -134,7 +135,8 @@ instance SerialiseAsRawBytes (SigningKey HydraKey) where
134135
rawSerialiseSignKeyDSIGN sk
135136

136137
deserialiseFromRawBytes (AsSigningKey AsHydraKey) bs =
137-
HydraSigningKey <$> rawDeserialiseSignKeyDSIGN bs
138+
maybe (error "TODO: SerialiseAsRawBytesError, but constructor not exported") Right $
139+
HydraSigningKey <$> rawDeserialiseSignKeyDSIGN bs
138140

139141
instance HasTextEnvelope (SigningKey HydraKey) where
140142
textEnvelopeType _ =
@@ -149,7 +151,8 @@ instance SerialiseAsRawBytes (VerificationKey HydraKey) where
149151
rawSerialiseVerKeyDSIGN vk
150152

151153
deserialiseFromRawBytes (AsVerificationKey AsHydraKey) bs =
152-
HydraVerificationKey <$> rawDeserialiseVerKeyDSIGN bs
154+
maybe (error "TODO: SerialiseAsRawBytesError, but constructor not exported") Right $
155+
HydraVerificationKey <$> rawDeserialiseVerKeyDSIGN bs
153156

154157
instance ToJSON (VerificationKey HydraKey) where
155158
toJSON = toJSON . serialiseToRawBytesHexText

hydra-node/src/Hydra/Options.hs

+15-19
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import Hydra.Cardano.Api (
2222
NetworkMagic (..),
2323
SlotNo (..),
2424
TxId (..),
25-
UsingRawBytesHex (..),
2625
deserialiseFromRawBytes,
27-
deserialiseFromRawBytesBase16,
2826
deserialiseFromRawBytesHex,
2927
proxyToAsType,
3028
serialiseToRawBytesHexText,
@@ -266,16 +264,16 @@ cardanoLedgerProtocolParametersParser =
266264
)
267265

268266
data ChainConfig = DirectChainConfig
269-
{ -- | Network identifer to which we expect to connect.
270-
networkId :: NetworkId
271-
, -- | Path to a domain socket used to connect to the server.
272-
nodeSocket :: FilePath
273-
, -- | Path to the cardano signing key of the internal wallet.
274-
cardanoSigningKey :: FilePath
275-
, -- | Paths to other node's verification keys.
276-
cardanoVerificationKeys :: [FilePath]
277-
, -- | Point at which to start following the chain.
278-
startChainFrom :: Maybe ChainPoint
267+
{ networkId :: NetworkId
268+
-- ^ Network identifer to which we expect to connect.
269+
, nodeSocket :: FilePath
270+
-- ^ Path to a domain socket used to connect to the server.
271+
, cardanoSigningKey :: FilePath
272+
-- ^ Path to the cardano signing key of the internal wallet.
273+
, cardanoVerificationKeys :: [FilePath]
274+
-- ^ Paths to other node's verification keys.
275+
, startChainFrom :: Maybe ChainPoint
276+
-- ^ Point at which to start following the chain.
279277
, contestationPeriod :: ContestationPeriod
280278
}
281279
deriving (Eq, Show, Generic, ToJSON, FromJSON)
@@ -525,11 +523,9 @@ startChainFromParser =
525523
case T.splitOn "." (toText chainPointStr) of
526524
[slotNoTxt, headerHashTxt] -> do
527525
slotNo <- SlotNo <$> readMaybe (toString slotNoTxt)
528-
UsingRawBytesHex headerHash <-
529-
either
530-
(const Nothing)
531-
Just
532-
(deserialiseFromRawBytesBase16 (encodeUtf8 headerHashTxt))
526+
headerHash <-
527+
either (const Nothing) Just $
528+
deserialiseFromRawBytesHex (proxyToAsType Proxy) (encodeUtf8 headerHashTxt)
533529
pure $ ChainPoint slotNo headerHash
534530
_emptyOrSingularList ->
535531
Nothing
@@ -630,7 +626,7 @@ validateRunOptions :: RunOptions -> Either InvalidOptions ()
630626
validateRunOptions RunOptions{hydraVerificationKeys, chainConfig}
631627
| numberOfOtherParties + 1 > maximumNumberOfParties = Left MaximumNumberOfPartiesExceeded
632628
| length (cardanoVerificationKeys chainConfig) /= length hydraVerificationKeys =
633-
Left CardanoAndHydraKeysMissmatch
629+
Left CardanoAndHydraKeysMissmatch
634630
| otherwise = Right ()
635631
where
636632
-- let's take the higher number of loaded cardano/hydra keys
@@ -766,5 +762,5 @@ genChainPoint = ChainPoint <$> (SlotNo <$> arbitrary) <*> someHeaderHash
766762
where
767763
someHeaderHash = do
768764
bytes <- vectorOf 32 arbitrary
769-
let hash = fromMaybe (error "invalid bytes") $ deserialiseFromRawBytes (proxyToAsType Proxy) . BS.pack $ bytes
765+
let hash = either (error "invalid bytes") id $ deserialiseFromRawBytes (proxyToAsType Proxy) . BS.pack $ bytes
770766
pure hash

hydra-node/src/Hydra/Party.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- Hydra protocol.
44
module Hydra.Party where
55

6-
import Hydra.Prelude hiding (show)
6+
import Hydra.Prelude
77

88
import Data.Aeson (ToJSONKey)
99
import Data.Aeson.Types (FromJSONKey)
@@ -48,6 +48,6 @@ partyToChain Party{vkey} =
4848
-- for an explanation why this is a distinct type.
4949
partyFromChain :: MonadFail m => OnChain.Party -> m Party
5050
partyFromChain =
51-
maybe (fail "partyFromChain got Nothing") (pure . Party)
51+
either (\e -> fail $ "partyFromChain failed: " <> show e) (pure . Party)
5252
. deserialiseFromRawBytes (AsVerificationKey AsHydraKey)
5353
. OnChain.partyToVerficationKeyBytes

hydra-node/src/Hydra/Snapshot.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Hydra.Cardano.Api (SigningKey)
1212
import qualified Hydra.Contract.HeadState as Onchain
1313
import Hydra.Crypto (HydraKey, MultiSignature, aggregate, sign)
1414
import Hydra.Ledger (IsTx (..))
15-
import Plutus.V2.Ledger.Api (toBuiltin, toData)
15+
import PlutusLedgerApi.V2 (toBuiltin, toData)
1616
import Test.QuickCheck (frequency, suchThat)
1717
import Test.QuickCheck.Instances.Natural ()
1818

@@ -24,8 +24,8 @@ newtype SnapshotNumber
2424
data Snapshot tx = Snapshot
2525
{ number :: SnapshotNumber
2626
, utxo :: UTxOType tx
27-
, -- | The set of transactions that lead to 'utxo'
28-
confirmed :: [tx]
27+
, confirmed :: [tx]
28+
-- ^ The set of transactions that lead to 'utxo'
2929
}
3030
deriving (Generic)
3131

@@ -127,10 +127,10 @@ genConfirmedSnapshot ::
127127
genConfirmedSnapshot minSn utxo sks
128128
| minSn > 0 = confirmedSnapshot
129129
| otherwise =
130-
frequency
131-
[ (1, initialSnapshot)
132-
, (9, confirmedSnapshot)
133-
]
130+
frequency
131+
[ (1, initialSnapshot)
132+
, (9, confirmedSnapshot)
133+
]
134134
where
135135
initialSnapshot =
136136
pure $ InitialSnapshot utxo

0 commit comments

Comments
 (0)