Skip to content

Commit ee88b07

Browse files
committed
Remove code duplication
1 parent 6f3464c commit ee88b07

File tree

5 files changed

+149
-112
lines changed

5 files changed

+149
-112
lines changed

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Allegra.hs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,17 @@ import Cardano.Prelude
1717
import Cardano.Slotting.Slot (SlotNo (..))
1818

1919
import qualified Cardano.Ledger.Allegra as Allegra
20-
import Cardano.Ledger.BaseTypes
20+
import Cardano.Ledger.BaseTypes (StrictMaybe, strictMaybeToMaybe)
2121
import qualified Cardano.Ledger.Core as Core
2222
import Cardano.Ledger.Shelley.Scripts (ScriptHash)
23-
import qualified Cardano.Ledger.Shelley.Tx as Shelley
24-
import qualified Cardano.Ledger.Shelley.TxBody as Shelley
25-
import qualified Cardano.Ledger.ShelleyMA.AuxiliaryData as ShelleyMa
23+
import Cardano.Ledger.ShelleyMA.AuxiliaryData (MAAuxiliaryData (AuxiliaryData'))
2624
import qualified Cardano.Ledger.ShelleyMA.TxBody as ShelleyMa
27-
import qualified Cardano.Ledger.ShelleyMA.Timelocks as ShelleyMa
25+
import Cardano.Ledger.ShelleyMA.Timelocks (Timelock)
2826

2927
import qualified Data.Aeson as Aeson
3028
import qualified Data.ByteString.Lazy.Char8 as LBS
3129
import qualified Data.Map.Strict as Map
32-
import Lens.Micro
30+
import Lens.Micro ((^.))
3331

3432
import Ouroboros.Consensus.Cardano.Block (StandardAllegra, StandardCrypto)
3533

@@ -38,20 +36,19 @@ import qualified Cardano.Api.Shelley as Api
3836
import Cardano.Db (ScriptType (..))
3937

4038
import Cardano.DbSync.Era.Shelley.Generic.Metadata
41-
import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
4239
import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley
4340
import Cardano.DbSync.Era.Shelley.Generic.Tx.Types
4441
import Cardano.DbSync.Era.Shelley.Generic.Util
4542
import Cardano.DbSync.Era.Shelley.Generic.Witness
4643

47-
fromAllegraTx :: (Word64, Shelley.ShelleyTx StandardAllegra) -> Tx
44+
fromAllegraTx :: (Word64, Core.Tx StandardAllegra) -> Tx
4845
fromAllegraTx (blkIndex, tx) =
4946
Tx
5047
{ txHash = txHashId tx
5148
, txBlockIndex = blkIndex
52-
, txSize = fromIntegral $ tx ^. Core.sizeTxF
49+
, txSize = getTxSize tx
5350
, txValidContract = True
54-
, txInputs = map fromTxIn (toList $ ShelleyMa.inputs' txBody)
51+
, txInputs = mkTxIn txBody
5552
, txCollateralInputs = [] -- Allegra does not have collateral inputs
5653
, txReferenceInputs = [] -- Allegra does not have reference inputs
5754
, txOutputs = outputs
@@ -60,11 +57,11 @@ fromAllegraTx (blkIndex, tx) =
6057
, txOutSum = sumTxOutCoin outputs
6158
, txInvalidBefore = invalidBefore
6259
, txInvalidHereafter = invalidAfter
63-
, txWithdrawalSum = calcWithdrawalSum $ ShelleyMa.wdrls' txBody
64-
, txMetadata = fromAllegraMetadata <$> strictMaybeToMaybe (tx ^. Core.auxDataTxL)
65-
, txCertificates = zipWith mkTxCertificate [0..] (toList $ ShelleyMa.certs' txBody)
66-
, txWithdrawals = map mkTxWithdrawal (Map.toList . Shelley.unWdrl $ ShelleyMa.wdrls' txBody)
67-
, txParamProposal = maybe [] (convertParamProposal (Allegra Standard)) $ strictMaybeToMaybe (ShelleyMa.update' txBody)
60+
, txWithdrawalSum = calcWithdrawalSum txBody
61+
, txMetadata = fromAllegraMetadata <$> getTxMetadata tx
62+
, txCertificates = mkTxCertificates txBody
63+
, txWithdrawals = mkTxWithdrawals txBody
64+
, txParamProposal = mkTxParamProposal (Allegra Standard) txBody
6865
, txMint = mempty -- Allegra does not support Multi-Assets
6966
, txRedeemer = [] -- Allegra does not support redeemers
7067
, txData = []
@@ -73,11 +70,11 @@ fromAllegraTx (blkIndex, tx) =
7370
, txExtraKeyWitnesses = []
7471
}
7572
where
76-
txBody :: ShelleyMa.MATxBody StandardAllegra
73+
txBody :: Core.TxBody StandardAllegra
7774
txBody = tx ^. Core.bodyTxL
7875

7976
outputs :: [TxOut]
80-
outputs = zipWith fromTxOut [0 .. ] $ toList (ShelleyMa.outputs' txBody)
77+
outputs = mkTxOut txBody
8178

8279
scripts :: [TxScript]
8380
scripts =
@@ -88,15 +85,15 @@ fromAllegraTx (blkIndex, tx) =
8885
(invalidBefore, invalidAfter) = getInterval $ ShelleyMa.vldt' txBody
8986

9087
getAuxScripts
91-
:: ShelleyMa.StrictMaybe (ShelleyMa.MAAuxiliaryData (Allegra.AllegraEra StandardCrypto))
92-
-> [(ScriptHash StandardCrypto, ShelleyMa.Timelock StandardCrypto)]
88+
:: StrictMaybe (MAAuxiliaryData (Allegra.AllegraEra StandardCrypto))
89+
-> [(ScriptHash StandardCrypto, Timelock StandardCrypto)]
9390
getAuxScripts maux =
9491
case strictMaybeToMaybe maux of
9592
Nothing -> []
96-
Just (ShelleyMa.AuxiliaryData' _ scrs) ->
93+
Just (AuxiliaryData' _ scrs) ->
9794
map (\scr -> (Core.hashScript @(Allegra.AllegraEra StandardCrypto) scr, scr)) $ toList scrs
9895

99-
mkTxScript :: (ScriptHash StandardCrypto, ShelleyMa.Timelock StandardCrypto) -> TxScript
96+
mkTxScript :: (ScriptHash StandardCrypto, Timelock StandardCrypto) -> TxScript
10097
mkTxScript (hsh, script) = TxScript
10198
{ txScriptHash = unScriptHash hsh
10299
, txScriptType = Timelock

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Alonzo.hs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
module Cardano.DbSync.Era.Shelley.Generic.Tx.Alonzo
1010
( fromAlonzoTx
1111
, dataHashToBytes
12+
, mkCollTxIn
1213
, mkTxData
1314
, mkTxScript
1415
, resolveRedeemers
@@ -30,20 +31,19 @@ import qualified Cardano.Crypto.Hash as Crypto
3031
import Cardano.Db (ScriptType (..))
3132

3233
import Cardano.DbSync.Era.Shelley.Generic.Metadata
33-
import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
3434
import Cardano.DbSync.Era.Shelley.Generic.Tx.Allegra (getInterval)
35-
import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley (calcWithdrawalSum, fromTxIn,
36-
mkTxCertificate, mkTxWithdrawal)
35+
import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley
3736
import Cardano.DbSync.Era.Shelley.Generic.Tx.Types
3837
import Cardano.DbSync.Era.Shelley.Generic.Util
3938
import Cardano.DbSync.Era.Shelley.Generic.Witness
4039

4140
import qualified Cardano.Ledger.Address as Ledger
42-
import qualified Cardano.Ledger.Alonzo.Data as Alonzo
41+
import Cardano.Ledger.Alonzo.Data (AlonzoAuxiliaryData (..))
4342
import qualified Cardano.Ledger.Alonzo.Language as Alonzo
4443
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), txscriptfee)
4544
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
4645
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
46+
import Cardano.Ledger.Alonzo.TxBody (AlonzoEraTxBody, AlonzoTxOut)
4747
import qualified Cardano.Ledger.Alonzo.TxBody as Alonzo
4848
import qualified Cardano.Ledger.Alonzo.TxWitness as Alonzo
4949
import Cardano.Ledger.BaseTypes
@@ -73,15 +73,15 @@ import Ouroboros.Consensus.Cardano.Block (StandardAlonzo, StandardCryp
7373
fromAlonzoTx :: Maybe Alonzo.Prices -> (Word64, Core.Tx StandardAlonzo) -> Tx
7474
fromAlonzoTx mprices (blkIndex, tx) =
7575
Tx
76-
{ txHash = Crypto.hashToBytes . Ledger.extractHash $ Ledger.hashAnnotated txBody
76+
{ txHash = txHashId tx
7777
, txBlockIndex = blkIndex
7878
, txSize = fromIntegral $ tx ^. Core.sizeTxF
7979
, txValidContract = isValid2
8080
, txInputs =
8181
if not isValid2
82-
then map fromTxIn . toList $ Alonzo.collateral' txBody
82+
then collInputs
8383
else Map.elems $ rmInps finalMaps
84-
, txCollateralInputs = map fromTxIn . toList $ Alonzo.collateral' txBody
84+
, txCollateralInputs = collInputs
8585
, txReferenceInputs = [] -- Alonzo does not have reference inputs
8686
, txOutputs =
8787
if not isValid2
@@ -98,11 +98,11 @@ fromAlonzoTx mprices (blkIndex, tx) =
9898
else sumTxOutCoin outputs
9999
, txInvalidBefore = invalidBefore
100100
, txInvalidHereafter = invalidAfter
101-
, txWithdrawalSum = calcWithdrawalSum $ Alonzo.wdrls' txBody
102-
, txMetadata = fromAlonzoMetadata <$> strictMaybeToMaybe (getField @"auxiliaryData" tx)
101+
, txWithdrawalSum = calcWithdrawalSum txBody
102+
, txMetadata = fromAlonzoMetadata <$> getTxMetadata tx
103103
, txCertificates = snd <$> rmCerts finalMaps
104104
, txWithdrawals = Map.elems $ rmWdrl finalMaps
105-
, txParamProposal = maybe [] (convertParamProposal (Alonzo Standard)) $ strictMaybeToMaybe (Alonzo.update' txBody)
105+
, txParamProposal = mkTxParamProposal (Alonzo Standard) txBody
106106
, txMint = Alonzo.mint' txBody
107107
, txRedeemer = redeemers
108108
, txData = txDataWitness tx
@@ -117,7 +117,7 @@ fromAlonzoTx mprices (blkIndex, tx) =
117117
outputs :: [TxOut]
118118
outputs = zipWith fromTxOut [0 .. ] $ toList (Alonzo.outputs' txBody)
119119

120-
fromTxOut :: Word64 -> Alonzo.AlonzoTxOut StandardAlonzo -> TxOut
120+
fromTxOut :: Word64 -> AlonzoTxOut StandardAlonzo -> TxOut
121121
fromTxOut index txOut =
122122
TxOut
123123
{ txOutIndex = index
@@ -143,11 +143,16 @@ fromAlonzoTx mprices (blkIndex, tx) =
143143

144144
(invalidBefore, invalidAfter) = getInterval $ Alonzo.vldt' txBody
145145

146+
collInputs = mkCollTxIn txBody
147+
148+
mkCollTxIn :: (Ledger.Crypto era ~ StandardCrypto, AlonzoEraTxBody era) => Core.TxBody era -> [TxIn]
149+
mkCollTxIn txBody = map fromTxIn . toList $ txBody ^. Alonzo.collateralInputsTxBodyL
150+
146151
getScripts ::
147152
forall era.
148153
( Ledger.Crypto era ~ StandardCrypto
149154
, Core.Script era ~ Alonzo.AlonzoScript era
150-
, Core.AuxiliaryData era ~ Alonzo.AlonzoAuxiliaryData era
155+
, Core.AuxiliaryData era ~ AlonzoAuxiliaryData era
151156
, Core.EraWitnesses era
152157
, Core.EraTx era
153158
) => Core.Tx era -> [TxScript]
@@ -157,12 +162,12 @@ getScripts tx =
157162
++ getAuxScripts (tx ^. Core.auxDataTxL))
158163
where
159164
getAuxScripts
160-
:: ShelleyMa.StrictMaybe (Alonzo.AlonzoAuxiliaryData era)
165+
:: ShelleyMa.StrictMaybe (AlonzoAuxiliaryData era)
161166
-> [(ScriptHash StandardCrypto, Alonzo.AlonzoScript era)]
162167
getAuxScripts maux =
163168
case strictMaybeToMaybe maux of
164169
Nothing -> []
165-
Just (Alonzo.AlonzoAuxiliaryData' _ scrs) ->
170+
Just (AlonzoAuxiliaryData' _ scrs) ->
166171
map (\scr -> (Core.hashScript @era scr, scr)) $ toList scrs
167172

168173
resolveRedeemers ::
@@ -302,10 +307,10 @@ getPlutusScriptSize script =
302307
Alonzo.PlutusScript _lang sbs -> Just $ fromIntegral (SBS.length sbs)
303308

304309
txDataWitness ::
305-
(HasField "wits" tx (Alonzo.TxWitness era), Ledger.Crypto era ~ StandardCrypto)
306-
=> tx -> [PlutusData]
310+
(Core.Witnesses era ~ Alonzo.TxWitness era, Core.EraTx era, Ledger.Crypto era ~ StandardCrypto)
311+
=> Core.Tx era -> [PlutusData]
307312
txDataWitness tx =
308-
mkTxData <$> Map.toList (Alonzo.unTxDats $ Alonzo.txdats' (getField @"wits" tx))
313+
mkTxData <$> Map.toList (Alonzo.unTxDats $ Alonzo.txdats' (tx ^. Core.witsTxL))
309314

310315
mkTxData :: (Ledger.DataHash StandardCrypto, Alonzo.Data era) -> PlutusData
311316
mkTxData (dataHash, dt) = PlutusData (dataHashToBytes dataHash) (jsonData dt) (Ledger.originalBytes dt)
@@ -317,7 +322,7 @@ mkTxData (dataHash, dt) = PlutusData (dataHashToBytes dataHash) (jsonData dt) (L
317322
. Api.scriptDataToJson Api.ScriptDataJsonDetailedSchema
318323
. Api.fromAlonzoData
319324

320-
extraKeyWits :: Alonzo.AlonzoEraTxBody era
325+
extraKeyWits :: AlonzoEraTxBody era
321326
=> Core.TxBody era -> [ByteString]
322327
extraKeyWits txBody = Set.toList $
323328
Set.map (\(Ledger.KeyHash h) -> Crypto.hashToBytes h) $

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Generic/Tx/Babbage.hs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ module Cardano.DbSync.Era.Shelley.Generic.Tx.Babbage
1111

1212
import Cardano.Prelude
1313

14-
import qualified Cardano.Crypto.Hash as Crypto
15-
1614
import qualified Cardano.Ledger.Alonzo.Data as Alonzo
1715
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
1816
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
17+
import Cardano.Ledger.Babbage.TxBody (BabbageTxOut)
1918
import qualified Cardano.Ledger.Babbage.TxBody as Babbage
2019
import Cardano.Ledger.BaseTypes
2120
import Cardano.Ledger.Coin (Coin (..))
2221
import qualified Cardano.Ledger.CompactAddress as Ledger
2322
import qualified Cardano.Ledger.Core as Core
2423
import qualified Cardano.Ledger.Era as Ledger
2524
import Cardano.Ledger.Mary.Value (MaryValue (..))
26-
import qualified Cardano.Ledger.SafeHash as Ledger
2725

2826
import qualified Data.ByteString.Short as SBS
2927
import qualified Data.Map.Strict as Map
@@ -32,25 +30,24 @@ import Lens.Micro
3230
import Ouroboros.Consensus.Shelley.Eras (StandardBabbage, StandardCrypto)
3331

3432
import Cardano.DbSync.Era.Shelley.Generic.Metadata
35-
import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
3633
import Cardano.DbSync.Era.Shelley.Generic.Tx.Allegra (getInterval)
3734
import Cardano.DbSync.Era.Shelley.Generic.Tx.Alonzo
38-
import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley (calcWithdrawalSum, fromTxIn)
35+
import Cardano.DbSync.Era.Shelley.Generic.Tx.Shelley
3936
import Cardano.DbSync.Era.Shelley.Generic.Tx.Types
4037
import Cardano.DbSync.Era.Shelley.Generic.Witness
4138

4239
fromBabbageTx :: Maybe Alonzo.Prices -> (Word64, Core.Tx StandardBabbage) -> Tx
4340
fromBabbageTx mprices (blkIndex, tx) =
4441
Tx
45-
{ txHash = Crypto.hashToBytes . Ledger.extractHash $ Ledger.hashAnnotated txBody
42+
{ txHash = txHashId tx
4643
, txBlockIndex = blkIndex
47-
, txSize = fromIntegral $ tx ^. Core.sizeTxF
44+
, txSize = getTxSize tx
4845
, txValidContract = isValid2
4946
, txInputs =
5047
if not isValid2
51-
then map fromTxIn . toList $ Babbage.collateralInputs' txBody
48+
then collInputs
5249
else Map.elems $ rmInps finalMaps
53-
, txCollateralInputs = map fromTxIn . toList $ Babbage.collateralInputs' txBody
50+
, txCollateralInputs = collInputs
5451
, txReferenceInputs = map fromTxIn . toList $ Babbage.referenceInputs' txBody
5552
, txOutputs =
5653
if not isValid2
@@ -68,11 +65,11 @@ fromBabbageTx mprices (blkIndex, tx) =
6865
else sumTxOutCoin outputs
6966
, txInvalidBefore = invalidBefore
7067
, txInvalidHereafter = invalidAfter
71-
, txWithdrawalSum = calcWithdrawalSum $ Babbage.wdrls' txBody
72-
, txMetadata = fromAlonzoMetadata <$> strictMaybeToMaybe (getField @"auxiliaryData" tx)
68+
, txWithdrawalSum = calcWithdrawalSum txBody
69+
, txMetadata = fromAlonzoMetadata <$> getTxMetadata tx
7370
, txCertificates = snd <$> rmCerts finalMaps
7471
, txWithdrawals = Map.elems $ rmWdrl finalMaps
75-
, txParamProposal = maybe [] (convertParamProposal (Babbage Standard)) $ strictMaybeToMaybe (Babbage.update' txBody)
72+
, txParamProposal = mkTxParamProposal (Babbage Standard) txBody
7673
, txMint = Babbage.mint' txBody
7774
, txRedeemer = redeemers
7875
, txData = txDataWitness tx
@@ -87,7 +84,7 @@ fromBabbageTx mprices (blkIndex, tx) =
8784
outputs :: [TxOut]
8885
outputs = zipWith fromTxOut [0 .. ] $ toList (Babbage.outputs' txBody)
8986

90-
fromTxOut :: Word64 -> Babbage.BabbageTxOut StandardBabbage -> TxOut
87+
fromTxOut :: Word64 -> BabbageTxOut StandardBabbage -> TxOut
9188
fromTxOut index txOut =
9289
TxOut
9390
{ txOutIndex = index
@@ -124,6 +121,7 @@ fromBabbageTx mprices (blkIndex, tx) =
124121
(finalMaps, redeemers) = resolveRedeemers mprices tx
125122
(invalidBefore, invalidAfter) = getInterval $ Babbage.vldt' txBody
126123

124+
collInputs = mkCollTxIn txBody
127125

128126
fromScript
129127
:: forall era.

0 commit comments

Comments
 (0)