Skip to content

Commit

Permalink
[alonzo] - AlonzoTxSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Feb 11, 2025
1 parent 30282f8 commit 50817c5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
65 changes: 63 additions & 2 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxSeq/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ import qualified Cardano.Crypto.Hash as Hash
import Cardano.Ledger.Alonzo.Era
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (..), IsValid (..), alonzoSegwitTx)
import Cardano.Ledger.Binary (
Annotated (..),
Annotator,
DecCBOR (..),
EncCBORGroup (..),
decodeAnnotated,
encCBOR,
encodeFoldableEncoder,
encodeFoldableMapEncoder,
Expand All @@ -44,14 +46,14 @@ import Cardano.Ledger.Binary (
withSlice,
)
import Cardano.Ledger.Core
import Cardano.Ledger.Shelley.BlockChain (constructMetadata)
import Cardano.Ledger.Shelley.BlockChain (constructMetadata, indexLookupSeq)
import Control.Monad (unless)
import Data.ByteString (ByteString)
import Data.ByteString.Builder (shortByteString, toLazyByteString)
import qualified Data.ByteString.Lazy as BSL
import Data.Coerce (coerce)
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (strictMaybeToMaybe)
import Data.Maybe.Strict (maybeToStrictMaybe, strictMaybeToMaybe)
import Data.Proxy (Proxy (..))
import qualified Data.Sequence as Seq
import Data.Sequence.Strict (StrictSeq)
Expand Down Expand Up @@ -230,6 +232,65 @@ instance AlonzoEraTx era => DecCBOR (Annotator (AlonzoTxSeq era)) where
<*> auxDataAnn
<*> isValAnn

instance
( AlonzoEraTx era
, DecCBOR (TxBody era)
, DecCBOR (TxWits era)
, DecCBOR (TxAuxData era)
) =>
DecCBOR (AlonzoTxSeq era)
where
decCBOR = do
Annotated bodies bodiesBs <- decodeAnnotated decCBOR
Annotated wits witsBs <- decodeAnnotated decCBOR
Annotated auxDataMap auxDataBs <- decodeAnnotated decCBOR
let b = length bodies
inRange x = (0 <= x) && (x <= (b - 1))
w = length wits
unless
(all inRange (Map.keysSet auxDataMap))
( fail
( "Some Auxiliarydata index is not in the range: 0 .. "
++ show (b - 1)
)
)
let auxData = maybeToStrictMaybe <$> indexLookupSeq b auxDataMap
Annotated isValidIdxs isValidBs <- decodeAnnotated decCBOR
let vs = alignedValidFlags b isValidIdxs
unless
(b == w)
( fail $
"different number of transaction bodies ("
<> show b
<> ") and witness sets ("
<> show w
<> ")"
)
unless
(all inRange isValidIdxs)
( fail
( "Some IsValid index is not in the range: 0 .. "
++ show (b - 1)
++ ", "
++ show isValidIdxs
)
)
let mkTx body wt isValid ad =
mkBasicTx body
& witsTxL .~ wt
& auxDataTxL .~ ad
& isValidTxL .~ isValid
let txs =
StrictSeq.forceToStrict $
Seq.zipWith4 mkTx bodies wits vs auxData
pure $
AlonzoTxSeqRaw
txs
bodiesBs
witsBs
auxDataBs
isValidBs

--------------------------------------------------------------------------------
-- Internal utility functions
--------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.16.0.0

* Add `indexLookupSeq`
* Add `segWitTx`
* Rename `segwitTx` to `segWitAnnTx`
* Move `AccountState` to `Cardano.Ledger.State`
Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
module Cardano.Ledger.Shelley.BlockChain (
ShelleyTxSeq (ShelleyTxSeq, txSeqTxns', TxSeq'),
constructMetadata,
indexLookupSeq,
txSeqTxns,
bbHash,
bBodySize,
Expand Down

0 comments on commit 50817c5

Please sign in to comment.