Skip to content

Commit b61e1ae

Browse files
committed
add Exception using ValidateBalanceError
1 parent 30a9ea1 commit b61e1ae

File tree

2 files changed

+51
-19
lines changed

2 files changed

+51
-19
lines changed

cardano-db-tool/src/Cardano/DbTool/Validate/Balance.hs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE ScopedTypeVariables #-}
55
{-# LANGUAGE TypeFamilies #-}
66
{-# LANGUAGE NoImplicitPrelude #-}
7+
{-# LANGUAGE LambdaCase #-}
78

89
module Cardano.DbTool.Validate.Balance (
910
ledgerAddrBalance,
@@ -33,32 +34,63 @@ import qualified Cardano.Ledger.Shelley.UTxO as Shelley
3334
import Cardano.Ledger.Val
3435
import Cardano.Prelude
3536
import qualified Data.Map.Strict as Map
36-
import qualified Data.Text as Text
3737
import Ouroboros.Consensus.Byron.Ledger
3838
import Ouroboros.Consensus.Cardano.Block (CardanoBlock, LedgerState (..), StandardCrypto)
3939
import Ouroboros.Consensus.Shelley.Ledger.Block (ShelleyBlock)
4040
import Ouroboros.Consensus.Shelley.Ledger.Ledger
41+
import qualified GHC.Show as S
42+
import Data.String (String)
43+
44+
data ValidateBalanceError
45+
= VBErrByron String
46+
| VBErrShelley String
47+
| VBErrAllegra String
48+
| VBErrMary String
49+
| VBErrAlonzo String
50+
| VBErrBabbage String
51+
| VBErrConway String
52+
53+
instance Exception ValidateBalanceError
54+
55+
instance Show ValidateBalanceError where
56+
show =
57+
\case
58+
VBErrByron err -> vBErr <> "Byron: " <> err
59+
VBErrShelley err -> vBErr <> "Shelley: " <> err
60+
VBErrAllegra err -> vBErr <> "Allegra: " <> err
61+
VBErrMary err -> vBErr <> "Mary: " <> err
62+
VBErrAlonzo err -> vBErr <> "Alonzo: " <> err
63+
VBErrBabbage err -> vBErr <> "Babbage: " <> err
64+
VBErrConway err -> vBErr <> "Conway: " <> err
65+
66+
vBErr :: String
67+
vBErr = "Validation Balance Error - "
4168

4269
-- Given an address, return it's current UTxO balance.
43-
ledgerAddrBalance :: Text -> LedgerState (CardanoBlock StandardCrypto) -> Either Text Word64
70+
ledgerAddrBalance :: Text -> LedgerState (CardanoBlock StandardCrypto) -> Either ValidateBalanceError Word64
4471
ledgerAddrBalance addr lsc =
4572
case lsc of
4673
LedgerStateByron st -> getByronBalance addr $ Byron.cvsUtxo $ byronLedgerState st
4774
LedgerStateShelley st -> getShelleyBalance addr $ getUTxO st
4875
LedgerStateAllegra st -> getShelleyBalance addr $ getUTxO st
4976
LedgerStateMary st -> getShelleyBalance addr $ getUTxO st
5077
LedgerStateAlonzo st -> getAlonzoBalance addr $ getUTxO st
51-
LedgerStateBabbage _st -> panic "undefined Babbage ledgerAddrBalance"
52-
LedgerStateConway _st -> panic "undefined Conway ledgerAddrBalance"
78+
LedgerStateBabbage _st -> Left $ VBErrBabbage "undefined Babbage ledgerAddrBalance"
79+
LedgerStateConway _st -> Left $ VBErrConway "undefined Conway ledgerAddrBalance"
5380
where
5481
getUTxO :: LedgerState (ShelleyBlock p era) -> Shelley.UTxO era
5582
getUTxO = Shelley.utxosUtxo . Shelley.lsUTxOState . Shelley.esLState . Shelley.nesEs . shelleyLedgerState
5683

57-
getByronBalance :: Text -> Byron.UTxO -> Either Text Word64
84+
getByronBalance :: Text -> Byron.UTxO -> Either ValidateBalanceError Word64
5885
getByronBalance addrText utxo = do
5986
case toCompactAddress <$> decodeAddressBase58 addrText of
60-
Left err -> Left $ textShow err
61-
Right caddr -> bimap show unsafeGetLovelace . sumLovelace . mapMaybe (compactTxOutValue caddr) . Map.elems $ Byron.unUTxO utxo
87+
Left err -> Left $ VBErrByron $ show err
88+
Right caddr -> do
89+
let utxos = Map.elems $ Byron.unUTxO utxo
90+
lvlaces = mapMaybe (compactTxOutValue caddr) utxos
91+
case sumLovelace lvlaces of
92+
Left err -> Left $ VBErrByron $ show err
93+
Right sLvlace -> Right $ unsafeGetLovelace sLvlace
6294
where
6395
compactTxOutValue :: CompactAddress -> Byron.CompactTxOut -> Maybe Lovelace
6496
compactTxOutValue caddr (Byron.CompactTxOut bcaddr lovelace) =
@@ -72,21 +104,23 @@ getShelleyBalance ::
72104
Val (Ledger.Value era) =>
73105
Text ->
74106
Shelley.UTxO era ->
75-
Either Text Word64
107+
Either ValidateBalanceError Word64
76108
getShelleyBalance addrText utxo = do
77-
caddr <- covertToCompactAddress addrText
78-
Right . fromIntegral . sum $ unCoin <$> mapMaybe (compactTxOutValue caddr) (Map.elems $ Shelley.unUTxO utxo)
109+
case covertToCompactAddress addrText of
110+
Left err -> Left $ VBErrShelley err
111+
Right cmpAddr -> Right . fromIntegral . sum $ unCoin <$> mapMaybe (compactTxOutValue cmpAddr) (Map.elems $ Shelley.unUTxO utxo)
79112
where
80113
compactTxOutValue :: CompactAddr (EraCrypto era) -> Ledger.TxOut era -> Maybe Coin
81114
compactTxOutValue caddr (Shelley.TxOutCompact scaddr v) =
82115
if caddr == scaddr
83116
then Just $ coin (fromCompact v)
84117
else Nothing
85118

86-
getAlonzoBalance :: Text -> Shelley.UTxO (AlonzoEra StandardCrypto) -> Either Text Word64
119+
getAlonzoBalance :: Text -> Shelley.UTxO (AlonzoEra StandardCrypto) -> Either ValidateBalanceError Word64
87120
getAlonzoBalance addrText utxo = do
88-
caddr <- covertToCompactAddress addrText
89-
Right . fromIntegral . sum $ unCoin <$> mapMaybe (compactTxOutValue caddr) (Map.elems $ Shelley.unUTxO utxo)
121+
case covertToCompactAddress addrText of
122+
Left err -> Left $ VBErrAlonzo err
123+
Right cmpAddr -> Right . fromIntegral . sum $ unCoin <$> mapMaybe (compactTxOutValue cmpAddr) (Map.elems $ Shelley.unUTxO utxo)
90124
where
91125
compactTxOutValue ::
92126
CompactAddr StandardCrypto -> Alonzo.AlonzoTxOut (AlonzoEra StandardCrypto) -> Maybe Coin
@@ -98,15 +132,12 @@ getAlonzoBalance addrText utxo = do
98132
then Just $ coin (fromCompact val)
99133
else Nothing
100134

101-
covertToCompactAddress :: Text -> Either Text (CompactAddr StandardCrypto)
135+
covertToCompactAddress :: Text -> Either String (CompactAddr StandardCrypto)
102136
covertToCompactAddress addrText =
103137
case Api.deserialiseAddress (Api.AsAddress Api.AsShelleyAddr) addrText of
104138
Nothing ->
105139
case decodeAddressBase58 addrText of
106-
Left err -> Left $ textShow err
140+
Left err -> Left $ show err
107141
Right badrr -> Right $ compactAddr (AddrBootstrap $ BootstrapAddress badrr)
108142
Just (Api.ShelleyAddress n p s) ->
109143
Right $ compactAddr (Addr n p s)
110-
111-
textShow :: Show a => a -> Text
112-
textShow = Text.pack . show

cardano-db-tool/src/Cardano/DbTool/Validate/Ledger.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ validate params genCfg slotNo ledgerFiles =
5555
when logFailure . putStrLn $ redText "Ledger is newer than DB. Trying an older ledger."
5656
go rest False
5757

58+
-- TODO: Vince - Should this throwIO when there is an error as it just prints right now
5859
validateBalance :: SlotNo -> Text -> CardanoLedgerState -> IO ()
5960
validateBalance slotNo addr st = do
6061
balanceDB <- DB.runDbNoLoggingEnv $ DB.queryAddressBalanceAtSlot addr (unSlotNo slotNo)
6162
let eiBalanceLedger = DB.word64ToAda <$> ledgerAddrBalance addr (ledgerState $ clsState st)
6263
case eiBalanceLedger of
63-
Left str -> putStrLn $ redText (Text.unpack str)
64+
Left str -> putStrLn $ redText $ show str
6465
Right balanceLedger ->
6566
if balanceDB == balanceLedger
6667
then

0 commit comments

Comments
 (0)