Skip to content

Commit

Permalink
improve initial utxo parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
cardenaso11 committed May 8, 2024
1 parent 107ba90 commit ea79cc7
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions hydra-node/src/Hydra/Chain/Offline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Hydra.Chain.Offline where

import Hydra.Prelude

import Data.Aeson qualified as Json
import Data.Aeson.Types qualified as Json

import Cardano.Api.Genesis (shelleyGenesisDefaults)
import Cardano.Api.GenesisParameters (fromShelleyGenesis)
import Cardano.Ledger.Slot (unSlotNo)
Expand Down Expand Up @@ -36,6 +39,15 @@ offlineHeadId = UnsafeHeadId "offline"
offlineHeadSeed :: HeadSeed
offlineHeadSeed = UnsafeHeadSeed "offline"

data InitialUTxOParseException = InitialUTxOParseException String
deriving stock (Show)

instance Exception InitialUTxOParseException where
displayException (InitialUTxOParseException err)
= "Failed to parse initial UTXO: " <> err <> ". Example UTXO: "
<> "{\"1541287c2598ffc682742c961a96343ac64e9b9030e6b03a476bb18c8c50134d#0\":{\"address\":\"addr_test1vqg9ywrpx6e50uam03nlu0ewunh3yrscxmjayurmkp52lfskgkq5k\",\"datum\":null,\"datumhash\":null,\"inlineDatum \":null,\"referenceScript\":null,\"value\":{\"lovelace\":100000000}},\"39786f186d94d8dd0b4fcf05d1458b18cd5fd8c6823364612f4a3c11b77e7cc7#0\":{\"address\":\"addr_test1vru2drx33ev6dt8gfq245r5k0tmy7ngqe79va69de9dxkrg09c7d3\",\"datum\":null,\"datumhash\":null,\"inlineDatum\":null,\"referenceScript\":null,\"value\":{\"lovelace\":100000000}}}"


-- | Load the given genesis file or use defaults specific to the offline mode.
loadGenesisFile :: Maybe FilePath -> IO (GenesisParameters ShelleyEra)
loadGenesisFile ledgerGenesisFile =
Expand All @@ -46,14 +58,11 @@ loadGenesisFile ledgerGenesisFile =
now <- getCurrentTime
-- TODO: uses internal cardano-api lib
pure shelleyGenesisDefaults{sgSystemStart = now}
Just fp ->
readJsonFileThrow (parseJSON @(ShelleyGenesis StandardCrypto)) fp
`catch` \(e :: IOException) -> do
putStrLn $ "Failed to parse initial UTXO" <> fp
putStrLn $ "Example UTXO: " <> "
{\"1541287c2598ffc682742c961a96343ac64e9b9030e6b03a476bb18c8c50134d#0\":{\"address\":\"addr_test1vqg9ywrpx6e50uam03nlu0ewunh3yrscxmjayurmkp52lfskgkq5k\",\"datum\":null,\"datumhash\":null,\"inlineDatum \":null,\"referenceScript\":null,\"value\":{\"lovelace\":100000000}},\"39786f186d94d8dd0b4fcf05d1458b18cd5fd8c6823364612f4a3c11b77e7cc7#0\":{\"address\":\"addr_test1vru2drx33ev6dt8gfq245r5k0tmy7ngqe79va69de9dxkrg09c7d3\",\"datum\":null,\"datumhash\":null,\"inlineDatum\":null,\"referenceScript\":null,\"value\":{\"lovelace\":100000000}}}
"
throwIO e
Just fp -> do
jsonVal <- Json.eitherDecodeFileStrict fp >>= either fail pure -- just crash if we can't read the file
case Json.parseEither (parseJSON @(ShelleyGenesis StandardCrypto)) jsonVal of
Right a -> pure a
Left e -> throwIO $ InitialUTxOParseException e

withOfflineChain ::
OfflineChainConfig ->
Expand Down

0 comments on commit ea79cc7

Please sign in to comment.