Skip to content

Commit

Permalink
Split JSON parsing in three alternatives
Browse files Browse the repository at this point in the history
  Better captures what's going on in the implementation, makes it easier to follow.
  • Loading branch information
KtorZ committed May 13, 2022
1 parent 7a180ed commit 89192d8
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions hydra-node/src/Hydra/Ledger/Cardano/Json.hs
Original file line number Diff line number Diff line change
Expand Up @@ -492,22 +492,10 @@ instance
--
-- (c) As base16 string representing a CBOR-serialized transaction, since
-- this is the most common medium of exchange used for transactions.
parseAsJSONObject value <|> parseAsBase16CBOR value
parseAsBase16CBOR value
<|> parseAsEnvelopedBase16CBOR value
<|> parseAsAdHocJSONObject value
where
parseAsJSONObject =
withObject "Tx" $ \o -> do
o .:? "cborHex" >>= \case
Nothing ->
Ledger.Alonzo.ValidatedTx
<$> o .: "body"
<*> o .: "witnesses"
<*> o .:? "isValid" .!= Ledger.Alonzo.IsValid True
<*> o .:? "auxiliaryData" .!= SNothing
Just str -> do
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx))
guard . (== envelopeType) =<< (o .: "type")
parseAsBase16CBOR (String str)

parseAsBase16CBOR =
withText "Tx" $ \t ->
case Base16.decode $ encodeUtf8 t of
Expand All @@ -518,6 +506,21 @@ instance
Left cborError -> fail $ show cborError
Right tx -> pure tx

parseAsEnvelopedBase16CBOR =
withObject "Tx" $ \o -> do
let TextEnvelopeType envelopeType = textEnvelopeType (proxyToAsType (Proxy @Tx))
str <- o .: "cborHex"
guard . (== envelopeType) =<< (o .: "type")
parseAsBase16CBOR (String str)

parseAsAdHocJSONObject =
withObject "Tx" $ \o -> do
Ledger.Alonzo.ValidatedTx
<$> o .: "body"
<*> o .: "witnesses"
<*> o .:? "isValid" .!= Ledger.Alonzo.IsValid True
<*> o .:? "auxiliaryData" .!= SNothing

--
-- ValidityInterval
--
Expand Down

0 comments on commit 89192d8

Please sign in to comment.