4
4
{-# LANGUAGE ScopedTypeVariables #-}
5
5
{-# LANGUAGE TypeFamilies #-}
6
6
{-# LANGUAGE NoImplicitPrelude #-}
7
+ {-# LANGUAGE LambdaCase #-}
7
8
8
9
module Cardano.DbTool.Validate.Balance (
9
10
ledgerAddrBalance ,
@@ -33,32 +34,63 @@ import qualified Cardano.Ledger.Shelley.UTxO as Shelley
33
34
import Cardano.Ledger.Val
34
35
import Cardano.Prelude
35
36
import qualified Data.Map.Strict as Map
36
- import qualified Data.Text as Text
37
37
import Ouroboros.Consensus.Byron.Ledger
38
38
import Ouroboros.Consensus.Cardano.Block (CardanoBlock , LedgerState (.. ), StandardCrypto )
39
39
import Ouroboros.Consensus.Shelley.Ledger.Block (ShelleyBlock )
40
40
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 - "
41
68
42
69
-- 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
44
71
ledgerAddrBalance addr lsc =
45
72
case lsc of
46
73
LedgerStateByron st -> getByronBalance addr $ Byron. cvsUtxo $ byronLedgerState st
47
74
LedgerStateShelley st -> getShelleyBalance addr $ getUTxO st
48
75
LedgerStateAllegra st -> getShelleyBalance addr $ getUTxO st
49
76
LedgerStateMary st -> getShelleyBalance addr $ getUTxO st
50
77
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"
53
80
where
54
81
getUTxO :: LedgerState (ShelleyBlock p era ) -> Shelley. UTxO era
55
82
getUTxO = Shelley. utxosUtxo . Shelley. lsUTxOState . Shelley. esLState . Shelley. nesEs . shelleyLedgerState
56
83
57
- getByronBalance :: Text -> Byron. UTxO -> Either Text Word64
84
+ getByronBalance :: Text -> Byron. UTxO -> Either ValidateBalanceError Word64
58
85
getByronBalance addrText utxo = do
59
86
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
62
94
where
63
95
compactTxOutValue :: CompactAddress -> Byron. CompactTxOut -> Maybe Lovelace
64
96
compactTxOutValue caddr (Byron. CompactTxOut bcaddr lovelace) =
@@ -72,21 +104,23 @@ getShelleyBalance ::
72
104
Val (Ledger. Value era ) =>
73
105
Text ->
74
106
Shelley. UTxO era ->
75
- Either Text Word64
107
+ Either ValidateBalanceError Word64
76
108
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)
79
112
where
80
113
compactTxOutValue :: CompactAddr (EraCrypto era ) -> Ledger. TxOut era -> Maybe Coin
81
114
compactTxOutValue caddr (Shelley. TxOutCompact scaddr v) =
82
115
if caddr == scaddr
83
116
then Just $ coin (fromCompact v)
84
117
else Nothing
85
118
86
- getAlonzoBalance :: Text -> Shelley. UTxO (AlonzoEra StandardCrypto ) -> Either Text Word64
119
+ getAlonzoBalance :: Text -> Shelley. UTxO (AlonzoEra StandardCrypto ) -> Either ValidateBalanceError Word64
87
120
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)
90
124
where
91
125
compactTxOutValue ::
92
126
CompactAddr StandardCrypto -> Alonzo. AlonzoTxOut (AlonzoEra StandardCrypto ) -> Maybe Coin
@@ -98,15 +132,12 @@ getAlonzoBalance addrText utxo = do
98
132
then Just $ coin (fromCompact val)
99
133
else Nothing
100
134
101
- covertToCompactAddress :: Text -> Either Text (CompactAddr StandardCrypto )
135
+ covertToCompactAddress :: Text -> Either String (CompactAddr StandardCrypto )
102
136
covertToCompactAddress addrText =
103
137
case Api. deserialiseAddress (Api. AsAddress Api. AsShelleyAddr ) addrText of
104
138
Nothing ->
105
139
case decodeAddressBase58 addrText of
106
- Left err -> Left $ textShow err
140
+ Left err -> Left $ show err
107
141
Right badrr -> Right $ compactAddr (AddrBootstrap $ BootstrapAddress badrr)
108
142
Just (Api. ShelleyAddress n p s) ->
109
143
Right $ compactAddr (Addr n p s)
110
-
111
- textShow :: Show a => a -> Text
112
- textShow = Text. pack . show
0 commit comments