Skip to content

Add SerialiseAsRawBytes instance to UnsignedTx ConwayEra #880

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

palas
Copy link
Contributor

@palas palas commented Jun 26, 2025

Changelog

- description: |
    Add `SerialiseAsRawBytes` instance to `UnsignedTx ConwayEra`
  type:
  - feature

Context

The standard way of serialising transactions is using CBOR, following the CDDL, and in the case of CIP-30, encoding it as base16. SerialiseAsRawBytes makes this easy. This PR adds SerialiseAsRawBytes instance to UnsignedTx.

Idea comes from this review in a different PR: #852 (comment)

How to trust this PR

It is mainly format changes. Make sure the new instances are implemented correctly and code design is good.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated. See Running tests for more details
  • Self-reviewed the diff

@palas palas force-pushed the serialise-raw-bytes-for-unsigned-tx branch from 9933152 to 4c1e0b0 Compare June 30, 2025 14:59
Copy link
Contributor

@carbolymer carbolymer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @Jimbo4350

Co-authored-by: Mateusz Galazyn <[email protected]>
@palas palas force-pushed the serialise-raw-bytes-for-unsigned-tx branch from cd55c85 to 75b3755 Compare July 1, 2025 14:32
@palas palas requested review from Jimbo4350 and carbolymer July 1, 2025 16:26
Comment on lines 193 to 194
, Ledger.EncCBOR (Ledger.Tx (LedgerEra era))
, Ledger.DecCBOR (Ledger.Annotator (Ledger.Tx (LedgerEra era)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to have those constraints in UnsignedTx. I mean to write:

data UnsignedTx
 = L.EraTx (LedgerEra era) => UnsignedTx (Ledger.Tx (LedgerEra era))

instead of adding them to instances.

Rationale: we'll always be dealing with L.EraTx instances for transactions. Having L.EraTx is easier to understand and satisfy, rather than dealing with more specialised constraints like EncCBOR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still, that would work for serialiseToRawBytes, but I don't think it is possible to implement deserialiseFromRawBytes without the assumption that era is a valid era

Copy link
Contributor

@carbolymer carbolymer Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think we can simplify constraints further:

diff --git c/cardano-api/src/Cardano/Api/Experimental/Tx.hs i/cardano-api/src/Cardano/Api/Experimental/Tx.hs
index 208a65777..1978d1efd 100644
--- c/cardano-api/src/Cardano/Api/Experimental/Tx.hs
+++ i/cardano-api/src/Cardano/Api/Experimental/Tx.hs
@@ -165,7 +165,6 @@ import Cardano.Crypto.Hash qualified as Hash
 import Cardano.Ledger.Alonzo.TxBody qualified as L
 import Cardano.Ledger.Api qualified as L
 import Cardano.Ledger.Binary qualified as Ledger
-import Cardano.Ledger.Conway qualified as Ledger
 import Cardano.Ledger.Conway.TxBody qualified as L
 import Cardano.Ledger.Core qualified as Ledger
 import Cardano.Ledger.Hashes qualified as L hiding (Hash)
@@ -180,8 +179,8 @@ import Lens.Micro
 
 -- | A transaction that can contain everything
 -- except key witnesses.
-newtype UnsignedTx era
-  = UnsignedTx (Ledger.Tx (LedgerEra era))
+data UnsignedTx era
+  = L.EraTx (LedgerEra era) => UnsignedTx (Ledger.Tx (LedgerEra era))
 
 instance HasTypeProxy era => HasTypeProxy (UnsignedTx era) where
   data AsType (UnsignedTx era) = AsUnsignedTx (AsType era)
@@ -189,10 +188,8 @@ instance HasTypeProxy era => HasTypeProxy (UnsignedTx era) where
   proxyToAsType _ = AsUnsignedTx (asType @era)
 
 instance
-  ( L.Era (LedgerEra era)
-  , HasTypeProxy era
-  , Ledger.EncCBOR (Ledger.Tx (LedgerEra era))
-  , Ledger.DecCBOR (Ledger.Annotator (Ledger.Tx (LedgerEra era)))
+  ( HasTypeProxy era
+  , L.EraTx (LedgerEra era)
   )
   => SerialiseAsRawBytes (UnsignedTx era)
   where
@@ -210,9 +207,8 @@ instance
       :: Ledger.DecoderError -> SerialiseAsRawBytesError
     wrapError = SerialiseAsRawBytesError . displayException
 
-instance IsEra era => Show (UnsignedTx era) where
-  showsPrec p (UnsignedTx tx) = case useEra @era of
-    ConwayEra -> showsPrec p (tx :: Ledger.Tx Ledger.ConwayEra)
+instance Show (UnsignedTx era) where
+  showsPrec p (UnsignedTx tx) = showsPrec p tx
 
 newtype UnsignedTxError
   = UnsignedTxError TxBodyError
@@ -366,5 +362,5 @@ convertTxBodyToUnsignedTx sbe txbody =
     (error $ "convertTxBodyToUnsignedTx: Error - unsupported era " <> docToString (pretty sbe))
     ( \w -> do
         let ShelleyTx _ unsignedLedgerTx = makeSignedTransaction [] txbody
-        UnsignedTx $ obtainCommonConstraints w unsignedLedgerTx
+        obtainCommonConstraints w $ UnsignedTx unsignedLedgerTx
     )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, so these were not necessary at all:

Ledger.EncCBOR (Ledger.Tx (LedgerEra era))
Ledger.DecCBOR (Ledger.Annotator (Ledger.Tx (LedgerEra era)))

But adding L.EraTx (LedgerEra era) => does simplify the Show instance.

I've applied your patch in: 4bf61df

Copy link
Contributor

@Jimbo4350 Jimbo4350 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Squash all the commits before merging.

@palas palas requested a review from carbolymer July 2, 2025 22:03
Copy link
Contributor

@carbolymer carbolymer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants