Skip to content

Commit 320d214

Browse files
authored
test(babbage): block CBOR round-trip (#1091)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent 74e2028 commit 320d214

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

ledger/babbage/babbage.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,12 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
456456
// Copy from temp legacy object to Babbage format
457457
o.OutputAddress = tmpOutput.OutputAddress
458458
o.OutputAmount = tmpOutput.OutputAmount
459+
// Copy datum hash if present in legacy Alonzo output
460+
if tmpOutput.OutputDatumHash != nil {
461+
o.DatumOption = &BabbageTransactionOutputDatumOption{
462+
hash: tmpOutput.OutputDatumHash,
463+
}
464+
}
459465
o.legacyOutput = true
460466
} else {
461467
type tBabbageTransactionOutput BabbageTransactionOutput
@@ -476,6 +482,10 @@ func (o *BabbageTransactionOutput) MarshalCBOR() ([]byte, error) {
476482
OutputAddress: o.OutputAddress,
477483
OutputAmount: o.OutputAmount,
478484
}
485+
// Copy datum hash if present
486+
if o.DatumOption != nil && o.DatumOption.hash != nil {
487+
tmpOutput.OutputDatumHash = o.DatumOption.hash
488+
}
479489
return cbor.Encode(&tmpOutput)
480490
}
481491
return cbor.EncodeGeneric(o)
@@ -693,6 +703,49 @@ func (w *BabbageTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
693703
return nil
694704
}
695705

706+
func (w *BabbageTransactionWitnessSet) MarshalCBOR() ([]byte, error) {
707+
// Return stored CBOR if available
708+
if w.Cbor() != nil {
709+
return w.Cbor(), nil
710+
}
711+
712+
// When encoding from scratch, we need to use indefinite-length encoding
713+
// for WsPlutusData to match the original on-chain format
714+
715+
// Create a temporary witness set for encoding
716+
type tempWitnessSet struct {
717+
cbor.DecodeStoreCbor
718+
VkeyWitnesses []common.VkeyWitness `cbor:"0,keyasint,omitempty"`
719+
WsNativeScripts []common.NativeScript `cbor:"1,keyasint,omitempty"`
720+
BootstrapWitnesses []common.BootstrapWitness `cbor:"2,keyasint,omitempty"`
721+
WsPlutusV1Scripts []common.PlutusV1Script `cbor:"3,keyasint,omitempty"`
722+
WsPlutusData cbor.IndefLengthList `cbor:"4,keyasint,omitempty"` // Use indefinite-length for Plutus data
723+
WsRedeemers alonzo.AlonzoRedeemers `cbor:"5,keyasint,omitempty"`
724+
WsPlutusV2Scripts []common.PlutusV2Script `cbor:"6,keyasint,omitempty"`
725+
}
726+
727+
// Convert WsPlutusData to IndefLengthList
728+
var plutusDataIndefList cbor.IndefLengthList
729+
if w.WsPlutusData != nil {
730+
plutusDataIndefList = make(cbor.IndefLengthList, len(w.WsPlutusData))
731+
for i, datum := range w.WsPlutusData {
732+
plutusDataIndefList[i] = datum
733+
}
734+
}
735+
736+
temp := tempWitnessSet{
737+
VkeyWitnesses: w.VkeyWitnesses,
738+
WsNativeScripts: w.WsNativeScripts,
739+
BootstrapWitnesses: w.BootstrapWitnesses,
740+
WsPlutusV1Scripts: w.WsPlutusV1Scripts,
741+
WsPlutusData: plutusDataIndefList,
742+
WsRedeemers: w.WsRedeemers,
743+
WsPlutusV2Scripts: w.WsPlutusV2Scripts,
744+
}
745+
746+
return cbor.Encode(&temp)
747+
}
748+
696749
func (w BabbageTransactionWitnessSet) Vkey() []common.VkeyWitness {
697750
return w.VkeyWitnesses
698751
}

0 commit comments

Comments
 (0)