/
Translation.hs
371 lines (321 loc) · 13 KB
/
Translation.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{- We disable warnings for name shadowing because of
https://gitlab.haskell.org/ghc/ghc/-/issues/14630, which means that we get
shadowing warnings for the named field puns when used with a pattern synonym.
-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Cardano.Ledger.Mary.Translation where
import Cardano.Ledger.Allegra (AllegraEra)
import Cardano.Ledger.Compactible
import Cardano.Ledger.Crypto (Crypto)
import Cardano.Ledger.Era hiding (Crypto)
import Cardano.Ledger.Mary (MaryEra)
import Cardano.Ledger.Mary.Value
import Cardano.Ledger.ShelleyMA.Metadata (Metadata (..), pattern Metadata)
import Cardano.Ledger.ShelleyMA.Scripts (Timelock)
import Cardano.Ledger.ShelleyMA.TxBody
import qualified Cardano.Ledger.Val as Val
import Control.Iterate.SetAlgebra (biMapFromList, lifo)
import Data.Coerce (coerce)
import Data.Foldable (Foldable (toList))
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Shelley.Spec.Ledger.API hiding (TxBody)
import qualified Shelley.Spec.Ledger.EpochBoundary as EB
import qualified Shelley.Spec.Ledger.LedgerState as LS
import Shelley.Spec.Ledger.Rewards (NonMyopic (NonMyopic), likelihoodsNM, rewardPotNM)
import Shelley.Spec.Ledger.Tx
( WitnessSetHKD (WitnessSet, addrWits, bootWits, scriptWits),
)
--------------------------------------------------------------------------------
-- Translation from Allegra to Mary
--
-- The instances below are needed by the consensus layer. Do not remove any of
-- them without coordinating with consensus.
--
-- Please add auxiliary instances and other declarations at the bottom of this
-- module, not in the list below so that it remains clear which instances the
-- consensus layer needs.
--
-- WARNING: when a translation instance currently uses the default
-- 'TranslationError', i.e., 'Void', it means the consensus layer relies on it
-- being total. Do not change it!
--------------------------------------------------------------------------------
type instance PreviousEra (MaryEra c) = AllegraEra c
-- | Currently no context is needed to translate from Allegra to Mary.
--
-- Note: if context is needed, please coordinate with consensus, who will have
-- to provide the context in the right place.
type instance TranslationContext (MaryEra c) = ()
instance Crypto c => TranslateEra (MaryEra c) NewEpochState where
translateEra ctxt nes =
return $
NewEpochState
{ nesEL = nesEL nes,
nesBprev = translateEra' ctxt $ nesBprev nes,
nesBcur = translateEra' ctxt $ nesBcur nes,
nesEs = translateEra' ctxt $ nesEs nes,
nesRu = translateEra' ctxt <$> nesRu nes,
nesPd = nesPd nes
}
instance Crypto c => TranslateEra (MaryEra c) Tx where
translateEra ctx (Tx body witness md) =
pure $
Tx
{ _body = translateEra' ctx body,
_witnessSet = translateEra' ctx witness,
_metadata = translateEra' ctx <$> md
}
-- TODO when a genesis has been introduced for Mary, this instance can be
-- removed.
instance Crypto c => TranslateEra (MaryEra c) ShelleyGenesis where
translateEra ctxt genesis =
return
ShelleyGenesis
{ sgSystemStart = sgSystemStart genesis,
sgNetworkMagic = sgNetworkMagic genesis,
sgNetworkId = sgNetworkId genesis,
sgActiveSlotsCoeff = sgActiveSlotsCoeff genesis,
sgSecurityParam = sgSecurityParam genesis,
sgEpochLength = sgEpochLength genesis,
sgSlotsPerKESPeriod = sgSlotsPerKESPeriod genesis,
sgMaxKESEvolutions = sgMaxKESEvolutions genesis,
sgSlotLength = sgSlotLength genesis,
sgUpdateQuorum = sgUpdateQuorum genesis,
sgMaxLovelaceSupply = sgMaxLovelaceSupply genesis,
sgProtocolParams = translateEra' ctxt (sgProtocolParams genesis),
sgGenDelegs = sgGenDelegs genesis,
sgInitialFunds = Map.mapKeys (translateEra' ctxt) $ sgInitialFunds genesis,
sgStaking = translateEra' ctxt $ sgStaking genesis
}
--------------------------------------------------------------------------------
-- Auxiliary instances and functions
--------------------------------------------------------------------------------
instance (Crypto c, Functor f) => TranslateEra (MaryEra c) (PParams' f)
instance Crypto c => TranslateEra (MaryEra c) RewardAcnt
instance Crypto c => TranslateEra (MaryEra c) PoolParams where
translateEra ctxt poolParams =
return $
PoolParams
{ _poolId = _poolId poolParams,
_poolVrf = _poolVrf poolParams,
_poolPledge = _poolPledge poolParams,
_poolCost = _poolCost poolParams,
_poolMargin = _poolMargin poolParams,
_poolRAcnt = translateEra' ctxt (_poolRAcnt poolParams),
_poolOwners = _poolOwners poolParams,
_poolRelays = _poolRelays poolParams,
_poolMD = _poolMD poolParams
}
instance Crypto c => TranslateEra (MaryEra c) ShelleyGenesisStaking where
translateEra ctxt sgs =
return $
ShelleyGenesisStaking
{ sgsPools = Map.map (translateEra' ctxt) (sgsPools sgs),
sgsStake = sgsStake sgs
}
instance Crypto c => TranslateEra (MaryEra c) Addr
instance Crypto c => TranslateEra (MaryEra c) EB.BlocksMade
instance Crypto c => TranslateEra (MaryEra c) SnapShot where
translateEra ctxt snap =
return
SnapShot
{ _stake = Stake $ Map.mapKeys (translateEra' ctxt) $ unStake . _stake $ snap,
EB._delegations = Map.mapKeys (translateEra' ctxt) $ EB._delegations snap,
_poolParams = Map.map (translateEra' ctxt) $ _poolParams snap
}
instance Crypto c => TranslateEra (MaryEra c) SnapShots where
translateEra ctxt snaps =
return
SnapShots
{ _pstakeMark = translateEra' ctxt $ _pstakeMark snaps,
_pstakeSet = translateEra' ctxt $ _pstakeSet snaps,
_pstakeGo = translateEra' ctxt $ _pstakeGo snaps,
_feeSS = _feeSS snaps
}
instance Crypto c => TranslateEra (MaryEra c) EpochState where
translateEra ctxt es =
return
EpochState
{ esAccountState = esAccountState es,
esSnapshots = translateEra' ctxt $ esSnapshots es,
esLState = translateEra' ctxt $ esLState es,
esPrevPp = translateEra' ctxt $ esPrevPp es,
esPp = translateEra' ctxt $ esPp es,
esNonMyopic = translateEra' ctxt $ esNonMyopic es
}
instance Crypto c => TranslateEra (MaryEra c) NonMyopic where
translateEra _ nm =
return
NonMyopic
{ likelihoodsNM = likelihoodsNM nm,
rewardPotNM = rewardPotNM nm
}
instance Crypto c => TranslateEra (MaryEra c) (Credential kr) where
translateEra ctxt (ScriptHashObj h) = return (ScriptHashObj (translateEra' ctxt h))
translateEra _ (KeyHashObj h) = return (KeyHashObj h)
instance Crypto c => TranslateEra (MaryEra c) ScriptHash
instance Crypto c => TranslateEra (MaryEra c) InstantaneousRewards where
translateEra ctxt ir =
return
InstantaneousRewards
{ iRReserves = Map.mapKeys (translateEra' ctxt) (iRReserves ir),
iRTreasury = Map.mapKeys (translateEra' ctxt) (iRTreasury ir)
}
instance Crypto c => TranslateEra (MaryEra c) DState where
translateEra ctxt ds =
return
DState
{ _rewards = Map.mapKeys (translateEra' ctxt) (_rewards ds),
LS._delegations = Map.mapKeys (translateEra' ctxt) (LS._delegations ds),
_ptrs =
biMapFromList const $
toList $
fmap
(\(ptr, cred) -> (ptr, translateEra' ctxt cred))
(lifo (_ptrs ds)),
_fGenDelegs = _fGenDelegs ds,
_genDelegs = _genDelegs ds,
_irwd = translateEra' ctxt $ _irwd ds
}
instance Crypto c => TranslateEra (MaryEra c) PState where
translateEra ctxt ps =
return
PState
{ _pParams = Map.map (translateEra' ctxt) (_pParams ps),
_fPParams = Map.map (translateEra' ctxt) (_fPParams ps),
_retiring = _retiring ps
}
instance Crypto c => TranslateEra (MaryEra c) DPState where
translateEra ctxt dp =
return
DPState
{ _dstate = translateEra' ctxt $ _dstate dp,
_pstate = translateEra' ctxt $ _pstate dp
}
instance Crypto c => TranslateEra (MaryEra c) LedgerState where
translateEra ctxt ls =
return
LedgerState
{ _utxoState = translateEra' ctxt $ _utxoState ls,
_delegationState = translateEra' ctxt $ _delegationState ls
}
instance Crypto c => TranslateEra (MaryEra c) ProposedPPUpdates where
translateEra ctxt (ProposedPPUpdates ppup) =
return $ ProposedPPUpdates $ Map.map (translateEra' ctxt) ppup
instance Crypto c => TranslateEra (MaryEra c) PPUPState where
translateEra ctxt ps =
return
PPUPState
{ proposals = translateEra' ctxt $ proposals ps,
futureProposals = translateEra' ctxt $ futureProposals ps
}
instance Crypto c => TranslateEra (MaryEra c) UTxOState where
translateEra ctxt us =
return
UTxOState
{ _utxo = translateEra' ctxt $ _utxo us,
_deposited = _deposited us,
_fees = _fees us,
_ppups = translateEra' ctxt $ _ppups us
}
instance Crypto c => TranslateEra (MaryEra c) RewardUpdate where
translateEra ctxt ru =
return
RewardUpdate
{ deltaT = deltaT ru,
deltaR = deltaR ru,
rs = Map.mapKeys (translateEra' ctxt) $ rs ru,
deltaF = deltaF ru,
nonMyopic = translateEra' ctxt $ nonMyopic ru
}
instance Crypto c => TranslateEra (MaryEra c) TxId
instance Crypto c => TranslateEra (MaryEra c) TxIn where
translateEra ctxt (TxIn txid ix) = return $ TxIn (translateEra' ctxt txid) ix
instance Crypto c => TranslateEra (MaryEra c) TxOut where
translateEra () (TxOutCompact addr cfval) =
pure $ TxOutCompact (coerce addr) (translateCompactValue cfval)
instance Crypto c => TranslateEra (MaryEra c) UTxO where
translateEra ctxt utxo =
return $
UTxO $
Map.mapKeys (translateEra' ctxt) $
Map.map (translateEra' ctxt) $
unUTxO utxo
instance Crypto c => TranslateEra (MaryEra c) WitnessSet where
translateEra ctxt WitnessSet {addrWits, scriptWits, bootWits} =
pure $
WitnessSet
{ addrWits = Set.map (translateEra' @(MaryEra c) ctxt) addrWits,
scriptWits =
Map.map coerce
. Map.mapKeysMonotonic coerce
$ scriptWits,
bootWits = Set.map (translateEra' @(MaryEra c) ctxt) bootWits
}
instance Crypto c => TranslateEra (MaryEra c) BootstrapWitness where
translateEra _ BootstrapWitness {bwKey, bwSig, bwChainCode, bwAttributes} =
pure
BootstrapWitness
{ bwKey = bwKey,
bwSig = coerce bwSig,
bwChainCode,
bwAttributes
}
instance
(Crypto c, Typeable kr) =>
TranslateEra (MaryEra c) (WitVKey kr)
where
translateEra _ (WitVKey k s) =
pure $
WitVKey k s
instance Crypto c => TranslateEra (MaryEra c) Wdrl where
translateEra _ (Wdrl w) = pure . Wdrl $ Map.mapKeysMonotonic coerce w
instance Crypto c => TranslateEra (MaryEra c) DCert where
translateEra _ (DCertDeleg c) = pure . DCertDeleg $ coerce c
translateEra ctx (DCertPool c) = DCertPool <$> translateEra ctx c
translateEra _ (DCertGenesis c) = pure . DCertGenesis $ coerce c
translateEra ctx (DCertMir c) = DCertMir <$> translateEra ctx c
instance Crypto c => TranslateEra (MaryEra c) PoolCert where
translateEra ctx (RegPool pp) = pure . RegPool $ translateEra' ctx pp
translateEra _ (RetirePool kh e) = pure $ RetirePool (coerce kh) e
instance Crypto c => TranslateEra (MaryEra c) MIRCert where
translateEra _ (MIRCert pot rewards) =
pure $
MIRCert pot (Map.mapKeysMonotonic coerce rewards)
instance Crypto c => TranslateEra (MaryEra c) Update where
translateEra _ (Update pp en) = pure $ Update (coerce pp) en
instance Crypto c => TranslateEra (MaryEra c) TxBody where
translateEra ctx (TxBody i o d w fee vi u m forge) =
pure $
TxBody
(Set.map (translateEra' ctx) i)
(translateEra' ctx <$> o)
(translateEra' ctx <$> d)
(translateEra' ctx w)
fee
vi
(translateEra' ctx <$> u)
(coerce m)
(translateValue forge)
instance Crypto c => TranslateEra (MaryEra c) Metadata where
translateEra ctx (Metadata blob sp) =
pure $
Metadata blob (translateEra' ctx <$> sp)
instance Crypto c => TranslateEra (MaryEra c) Timelock
translateValue :: Era era => Coin -> Value era
translateValue = Val.inject
translateCompactValue :: Era era => CompactForm Coin -> CompactForm (Value era)
translateCompactValue = assume . toCompact . translateValue . fromCompact
where
assume Nothing = error "impossible error: compact coin is out of range"
assume (Just x) = x