Skip to content

Commit edebbb2

Browse files
committed
[alonzo] - AlonzoTxSeq
1 parent c87184c commit edebbb2

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxSeq/Internal.hs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ import qualified Cardano.Crypto.Hash as Hash
3232
import Cardano.Ledger.Alonzo.Era
3333
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (..), IsValid (..), alonzoSegwitTx)
3434
import Cardano.Ledger.Binary (
35+
Annotated (..),
3536
Annotator,
3637
DecCBOR (..),
3738
EncCBORGroup (..),
39+
decodeAnnotated,
3840
encCBOR,
3941
encodeFoldableEncoder,
4042
encodeFoldableMapEncoder,
@@ -44,14 +46,14 @@ import Cardano.Ledger.Binary (
4446
withSlice,
4547
)
4648
import Cardano.Ledger.Core
47-
import Cardano.Ledger.Shelley.BlockChain (constructMetadata)
49+
import Cardano.Ledger.Shelley.BlockChain (constructMetadata, indexLookupSeq)
4850
import Control.Monad (unless)
4951
import Data.ByteString (ByteString)
5052
import Data.ByteString.Builder (shortByteString, toLazyByteString)
5153
import qualified Data.ByteString.Lazy as BSL
5254
import Data.Coerce (coerce)
5355
import qualified Data.Map.Strict as Map
54-
import Data.Maybe.Strict (strictMaybeToMaybe)
56+
import Data.Maybe.Strict (maybeToStrictMaybe, strictMaybeToMaybe)
5557
import Data.Proxy (Proxy (..))
5658
import qualified Data.Sequence as Seq
5759
import Data.Sequence.Strict (StrictSeq)
@@ -230,6 +232,65 @@ instance AlonzoEraTx era => DecCBOR (Annotator (AlonzoTxSeq era)) where
230232
<*> auxDataAnn
231233
<*> isValAnn
232234

235+
instance
236+
( AlonzoEraTx era
237+
, DecCBOR (TxBody era)
238+
, DecCBOR (TxWits era)
239+
, DecCBOR (TxAuxData era)
240+
) =>
241+
DecCBOR (AlonzoTxSeq era)
242+
where
243+
decCBOR = do
244+
Annotated bodies bodiesBs <- decodeAnnotated decCBOR
245+
Annotated wits witsBs <- decodeAnnotated decCBOR
246+
Annotated auxDataMap auxDataBs <- decodeAnnotated decCBOR
247+
let b = length bodies
248+
inRange x = (0 <= x) && (x <= (b - 1))
249+
w = length wits
250+
unless
251+
(all inRange (Map.keysSet auxDataMap))
252+
( fail
253+
( "Some Auxiliarydata index is not in the range: 0 .. "
254+
++ show (b - 1)
255+
)
256+
)
257+
let auxData = maybeToStrictMaybe <$> indexLookupSeq b auxDataMap
258+
Annotated isValidIdxs isValidBs <- decodeAnnotated decCBOR
259+
let vs = alignedValidFlags b isValidIdxs
260+
unless
261+
(b == w)
262+
( fail $
263+
"different number of transaction bodies ("
264+
<> show b
265+
<> ") and witness sets ("
266+
<> show w
267+
<> ")"
268+
)
269+
unless
270+
(all inRange isValidIdxs)
271+
( fail
272+
( "Some IsValid index is not in the range: 0 .. "
273+
++ show (b - 1)
274+
++ ", "
275+
++ show isValidIdxs
276+
)
277+
)
278+
let mkTx body wt isValid ad =
279+
mkBasicTx body
280+
& witsTxL .~ wt
281+
& auxDataTxL .~ ad
282+
& isValidTxL .~ isValid
283+
let txs =
284+
StrictSeq.forceToStrict $
285+
Seq.zipWith4 mkTx bodies wits vs auxData
286+
pure $
287+
AlonzoTxSeqRaw
288+
txs
289+
bodiesBs
290+
witsBs
291+
auxDataBs
292+
isValidBs
293+
233294
--------------------------------------------------------------------------------
234295
-- Internal utility functions
235296
--------------------------------------------------------------------------------

eras/shelley/impl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.16.0.0
44

5+
* Add `indexLookupSeq`
56
* Add `segWitTx`
67
* Rename `segwitTx` to `segWitAnnTx`
78
* Made the fields of predicate failures and environments lazy

eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
module Cardano.Ledger.Shelley.BlockChain (
2020
ShelleyTxSeq (ShelleyTxSeq, txSeqTxns', TxSeq'),
2121
constructMetadata,
22+
indexLookupSeq,
2223
txSeqTxns,
2324
bbHash,
2425
bBodySize,

0 commit comments

Comments
 (0)