Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change serialization of OneEraGenTxId to treat it as though it's simply a ShortByteString #1311

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ package ouroboros-network
if(os(windows))
constraints:
bitvec -simd

source-repository-package
type: git
location: https://github.com/input-output-hk/ouroboros-network
-- branch: fraser-iohk/one-era-gen-tx-id-protocol-version-bump
tag: 320dcf4d260761484f8cf3114c340b66635f4985
subdir: ouroboros-network-api
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

{-# OPTIONS_GHC -Wno-orphans #-}

Expand All @@ -16,6 +18,10 @@ module Ouroboros.Consensus.Byron.Ledger.Mempool (
GenTx (..)
, TxId (..)
, Validated (..)
, pattern ByronDlgId
, pattern ByronTxId
, pattern ByronUpdateProposalId
, pattern ByronUpdateVoteId
-- * Transaction IDs
, byronIdDlg
, byronIdProp
Expand Down Expand Up @@ -44,20 +50,17 @@ import qualified Cardano.Chain.UTxO as Utxo
import qualified Cardano.Chain.ValidationMode as CC
import Cardano.Crypto (hashDecoded)
import qualified Cardano.Crypto as CC
import Cardano.Ledger.Binary (ByteSpan, DecoderError (..),
byronProtVer, fromByronCBOR, serialize, slice, toByronCBOR,
unsafeDeserialize)
import Cardano.Ledger.Binary.Plain (enforceSize)
import Cardano.Prelude (Natural, cborError)
import Cardano.Ledger.Binary (ByteSpan, byronProtVer, fromByronCBOR,
serialize, slice, toByronCBOR, unsafeDeserialize)
import Cardano.Prelude (Natural)
import Codec.CBOR.Decoding (Decoder)
import qualified Codec.CBOR.Decoding as CBOR
import Codec.CBOR.Encoding (Encoding)
import qualified Codec.CBOR.Encoding as CBOR
import Control.Monad (void)
import Control.Monad.Except (Except, throwError)
import Data.ByteString (ByteString)
import qualified Data.ByteString as Strict
import qualified Data.ByteString.Lazy as Lazy
import Data.Coerce
import Data.Maybe (maybeToList)
import Data.Word
import GHC.Generics (Generic)
Expand All @@ -83,10 +86,10 @@ import Ouroboros.Consensus.Util.Condense
-- This is effectively the same as 'CC.AMempoolPayload' but we cache the
-- transaction ID (a hash).
data instance GenTx ByronBlock
= ByronTx !Utxo.TxId !(Utxo.ATxAux ByteString)
| ByronDlg !Delegation.CertificateId !(Delegation.ACertificate ByteString)
| ByronUpdateProposal !Update.UpId !(Update.AProposal ByteString)
| ByronUpdateVote !Update.VoteId !(Update.AVote ByteString)
= ByronTx !(GenTxId ByronBlock) !(Utxo.ATxAux ByteString)
| ByronDlg !(GenTxId ByronBlock) !(Delegation.ACertificate ByteString)
| ByronUpdateProposal !(GenTxId ByronBlock) !(Update.AProposal ByteString)
| ByronUpdateVote !(GenTxId ByronBlock) !(Update.AVote ByteString)
deriving (Eq, Generic)
deriving NoThunks via InspectHeapNamed "GenTx ByronBlock" (GenTx ByronBlock)

Expand Down Expand Up @@ -157,26 +160,47 @@ instance TxLimits ByronBlock where
$ Utxo.TxValidationTxTooLarge txszNat maxTxSize

data instance TxId (GenTx ByronBlock)
= ByronTxId !Utxo.TxId
| ByronDlgId !Delegation.CertificateId
| ByronUpdateProposalId !Update.UpId
| ByronUpdateVoteId !Update.VoteId
= ByronGenTxId !(CC.Hash (GenTx ByronBlock))
deriving (Eq, Ord)
deriving NoThunks via InspectHeapNamed "TxId (GenTx ByronBlock)" (TxId (GenTx ByronBlock))

instance ShowProxy (TxId (GenTx ByronBlock)) where

instance HasTxId (GenTx ByronBlock) where
txId (ByronTx i _) = ByronTxId i
txId (ByronDlg i _) = ByronDlgId i
txId (ByronUpdateProposal i _) = ByronUpdateProposalId i
txId (ByronUpdateVote i _) = ByronUpdateVoteId i
txId (ByronTx i _) = i
txId (ByronDlg i _) = i
txId (ByronUpdateProposal i _) = i
txId (ByronUpdateVote i _) = i

instance ConvertRawTxId (GenTx ByronBlock) where
toRawTxIdHash (ByronTxId i) = CC.abstractHashToShort i
toRawTxIdHash (ByronDlgId i) = CC.abstractHashToShort i
toRawTxIdHash (ByronUpdateProposalId i) = CC.abstractHashToShort i
toRawTxIdHash (ByronUpdateVoteId i) = CC.abstractHashToShort i
toRawTxIdHash (ByronGenTxId i) = CC.abstractHashToShort i

castAbstractHash :: CC.Hash a -> CC.Hash b
castAbstractHash = coerce

pattern ByronTxId :: Utxo.TxId -> TxId (GenTx ByronBlock)
pattern ByronTxId i <- ByronGenTxId (castAbstractHash -> i)
where ByronTxId i = ByronGenTxId (castAbstractHash i)

{-# COMPLETE ByronTxId #-}

pattern ByronDlgId :: Delegation.CertificateId -> TxId (GenTx ByronBlock)
pattern ByronDlgId i <- ByronGenTxId (castAbstractHash -> i)
where ByronDlgId i = ByronGenTxId (castAbstractHash i)

{-# COMPLETE ByronDlgId #-}

pattern ByronUpdateProposalId :: Update.UpId -> TxId (GenTx ByronBlock)
pattern ByronUpdateProposalId i <- ByronGenTxId (castAbstractHash -> i)
where ByronUpdateProposalId i = ByronGenTxId (castAbstractHash i)

{-# COMPLETE ByronUpdateProposalId #-}

pattern ByronUpdateVoteId :: Update.VoteId -> TxId (GenTx ByronBlock)
pattern ByronUpdateVoteId i <- ByronGenTxId (castAbstractHash -> i)
where ByronUpdateVoteId i = ByronGenTxId (castAbstractHash i)

{-# COMPLETE ByronUpdateVoteId #-}

instance HasTxs ByronBlock where
extractTxs blk = case byronBlockRaw blk of
Expand Down Expand Up @@ -221,17 +245,17 @@ fromMempoolPayload = go
-------------------------------------------------------------------------------}

-- TODO: move to cardano-ledger-byron (cardano-ledger-byron#581)
byronIdTx :: Utxo.ATxAux ByteString -> Utxo.TxId
byronIdTx = hashDecoded . Utxo.aTaTx
byronIdTx :: Utxo.ATxAux ByteString -> GenTxId ByronBlock
byronIdTx = ByronTxId . hashDecoded . Utxo.aTaTx

byronIdDlg :: Delegation.ACertificate ByteString -> Delegation.CertificateId
byronIdDlg = Delegation.recoverCertificateId
byronIdDlg :: Delegation.ACertificate ByteString -> GenTxId ByronBlock
byronIdDlg = ByronDlgId . Delegation.recoverCertificateId

byronIdProp :: Update.AProposal ByteString -> Update.UpId
byronIdProp = Update.recoverUpId
byronIdProp :: Update.AProposal ByteString -> GenTxId ByronBlock
byronIdProp = ByronUpdateProposalId . Update.recoverUpId

byronIdVote :: Update.AVote ByteString -> Update.VoteId
byronIdVote = Update.recoverVoteId
byronIdVote :: Update.AVote ByteString -> GenTxId ByronBlock
byronIdVote = ByronUpdateVoteId . Update.recoverVoteId

{-------------------------------------------------------------------------------
Pretty-printing
Expand All @@ -241,10 +265,7 @@ instance Condense (GenTx ByronBlock) where
condense = condense . toMempoolPayload

instance Condense (GenTxId ByronBlock) where
condense (ByronTxId i) = condense i
condense (ByronDlgId i) = condense i
condense (ByronUpdateProposalId i) = condense i
condense (ByronUpdateVoteId i) = condense i
condense (ByronGenTxId i) = show $ CC.abstractHashToShort i

instance Show (GenTx ByronBlock) where
show = condense
Expand Down Expand Up @@ -313,24 +334,10 @@ decodeByronGenTx = fromMempoolPayload . canonicalise <$> fromByronCBOR
mp' = unsafeDeserialize byronProtVer canonicalBytes

encodeByronGenTxId :: GenTxId ByronBlock -> Encoding
encodeByronGenTxId genTxId = mconcat [
CBOR.encodeListLen 2
, case genTxId of
ByronTxId i -> toByronCBOR (0 :: Word8) <> toByronCBOR i
ByronDlgId i -> toByronCBOR (1 :: Word8) <> toByronCBOR i
ByronUpdateProposalId i -> toByronCBOR (2 :: Word8) <> toByronCBOR i
ByronUpdateVoteId i -> toByronCBOR (3 :: Word8) <> toByronCBOR i
]
encodeByronGenTxId (ByronGenTxId i) = toByronCBOR i

decodeByronGenTxId :: Decoder s (GenTxId ByronBlock)
decodeByronGenTxId = do
enforceSize "GenTxId (ByronBlock cfg)" 2
CBOR.decodeWord8 >>= \case
0 -> ByronTxId <$> fromByronCBOR
1 -> ByronDlgId <$> fromByronCBOR
2 -> ByronUpdateProposalId <$> fromByronCBOR
3 -> ByronUpdateVoteId <$> fromByronCBOR
tag -> cborError $ DecoderErrorUnknownTag "GenTxId (ByronBlock cfg)" tag
decodeByronGenTxId = ByronGenTxId <$> fromByronCBOR

encodeByronApplyTxError :: ApplyTxErr ByronBlock -> Encoding
encodeByronApplyTxError = toByronCBOR
Expand Down
Loading
Loading