Skip to content

Commit

Permalink
Fix decoding of Hydra keys and ignore problematic head init
Browse files Browse the repository at this point in the history
We were not correctly decoding Hydra verification keys (but crashing via
error) because of a missing data constructor.

This also makes the observeInitTx sort out heads that would have such
problematic party datums.

An example transaction with problematic data is:
b860a236a7e77577628bf705286d449188831cf90d714934f1cc06369c6e3953 on preprod
  • Loading branch information
ch1bo committed Feb 14, 2025
1 parent 696a4a0 commit 23af241
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 10 additions & 1 deletion hydra-node/src/Hydra/Chain/Direct/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ instance Arbitrary HeadObservation where
-- | Observe any Hydra head transaction.
observeHeadTx :: NetworkId -> UTxO -> Tx -> HeadObservation
observeHeadTx networkId utxo tx =
-- XXX: This is throwing away valuable information! We should be collecting
-- all "not an XX" reasons here in case we fall through and want that
-- diagnostic information in the call site of this function. Collecting errors
-- could be done with 'validation' or a similar package.
fromMaybe NoHeadTx $
either (const Nothing) (Just . Init) (observeInitTx tx)
<|> Abort <$> observeAbortTx utxo tx
Expand Down Expand Up @@ -149,6 +153,7 @@ data NotAnInitReason
| NotAHeadDatum
| NoSTFound
| NotAHeadPolicy
| InvalidPartyInDatum
deriving stock (Show, Eq, Generic)
deriving anyclass (ToJSON, FromJSON)

Expand Down Expand Up @@ -183,14 +188,18 @@ observeInitTx tx = do
unless (pid == HeadTokens.headPolicyId seedTxIn) $
Left NotAHeadPolicy

parties <-
maybe (Left InvalidPartyInDatum) Right $
traverse partyFromChain onChainParties

pure $
InitObservation
{ headId = mkHeadId pid
, seedTxIn
, initialThreadUTxO = (mkTxIn tx ix, toCtxUTxOTxOut headOut)
, initials
, contestationPeriod
, parties = mapMaybe partyFromChain onChainParties
, parties
, participants = assetNameToOnChainId <$> mintedTokenNames pid
}
where
Expand Down
16 changes: 13 additions & 3 deletions hydra-tx/src/Hydra/Tx/Crypto.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import Hydra.Cardano.Api (
Key (..),
SerialiseAsCBOR,
SerialiseAsRawBytes (..),
SerialiseAsRawBytesError (..),
serialiseToRawBytesHexText,
)
import Hydra.Contract.HeadState qualified as OnChain
Expand Down Expand Up @@ -85,7 +86,10 @@ instance SerialiseAsRawBytes (Hash HydraKey) where
serialiseToRawBytes (HydraKeyHash vkh) = hashToBytes vkh

deserialiseFromRawBytes (AsHash AsHydraKey) bs =
maybe (error "TODO: SerialiseAsRawBytesError, but constructor not exported") (Right . HydraKeyHash) (hashFromBytes bs)
maybe
(Left $ SerialiseAsRawBytesError "invalid length when deserializing Hash HydraKey")
(Right . HydraKeyHash)
(hashFromBytes bs)

instance Key HydraKey where
-- Hydra verification key, which can be used to 'verify' signed messages.
Expand Down Expand Up @@ -134,7 +138,10 @@ instance SerialiseAsRawBytes (SigningKey HydraKey) where
rawSerialiseSignKeyDSIGN sk

deserialiseFromRawBytes (AsSigningKey AsHydraKey) bs =
maybe (error "TODO: SerialiseAsRawBytesError, but constructor not exported") (Right . HydraSigningKey) (rawDeserialiseSignKeyDSIGN bs)
maybe
(Left $ SerialiseAsRawBytesError "invalid length when deserializing SigningKey HydraKey")
(Right . HydraSigningKey)
(rawDeserialiseSignKeyDSIGN bs)

instance HasTextEnvelope (SigningKey HydraKey) where
textEnvelopeType _ =
Expand All @@ -149,7 +156,10 @@ instance SerialiseAsRawBytes (VerificationKey HydraKey) where
rawSerialiseVerKeyDSIGN vk

deserialiseFromRawBytes (AsVerificationKey AsHydraKey) bs =
maybe (error "TODO: SerialiseAsRawBytesError, but constructor not exported") (Right . HydraVerificationKey) (rawDeserialiseVerKeyDSIGN bs)
maybe
(Left $ SerialiseAsRawBytesError "invalid length when deserializing VerificationKey HydraKey")
(Right . HydraVerificationKey)
(rawDeserialiseVerKeyDSIGN bs)

instance ToJSON (VerificationKey HydraKey) where
toJSON = toJSON . serialiseToRawBytesHexText
Expand Down

0 comments on commit 23af241

Please sign in to comment.