Skip to content

refactor: remove additional uses of reflection for CBOR decoding #986

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

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ type AlonzoTransactionOutput struct {
}

func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
// Save original CBOR
o.SetCbor(cborData)
// Try to parse as legacy mary.Mary output first
var tmpOutput mary.MaryTransactionOutput
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
Expand All @@ -279,8 +277,15 @@ func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
o.OutputAmount = tmpOutput.OutputAmount
o.legacyOutput = true
} else {
return cbor.DecodeGeneric(cborData, o)
type tAlonzoTransactionOutput AlonzoTransactionOutput
var tmp tAlonzoTransactionOutput
if _, err := cbor.Decode(cborData, &tmp); err != nil {
return err
}
*o = AlonzoTransactionOutput(tmp)
}
// Save original CBOR
o.SetCbor(cborData)
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,12 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
o.OutputAmount = tmpOutput.OutputAmount
o.legacyOutput = true
} else {
return cbor.DecodeGeneric(cborData, o)
type tBabbageTransactionOutput BabbageTransactionOutput
var tmp tBabbageTransactionOutput
if _, err := cbor.Decode(cborData, &tmp); err != nil {
return err
}
*o = BabbageTransactionOutput(tmp)
}
return nil
}
Expand Down
7 changes: 6 additions & 1 deletion ledger/byron/byron.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ func (i *ByronTransactionInput) UnmarshalCBOR(data []byte) error {
}
switch id {
case 0:
// Decode outer data
var tmpData struct {
cbor.StructAsArray
Id int
Expand All @@ -345,9 +346,13 @@ func (i *ByronTransactionInput) UnmarshalCBOR(data []byte) error {
if _, err := cbor.Decode(data, &tmpData); err != nil {
return err
}
if err := cbor.DecodeGeneric(tmpData.Cbor, i); err != nil {
// Decode inner data
type tByronTransactionInput ByronTransactionInput
var tmp tByronTransactionInput
if _, err := cbor.Decode(tmpData.Cbor, &tmp); err != nil {
return err
}
*i = ByronTransactionInput(tmp)
default:
// [u8 .ne 0, encoded-cbor]
return errors.New("can't parse yet")
Expand Down
5 changes: 4 additions & 1 deletion ledger/common/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ func (n *Nonce) UnmarshalCBOR(data []byte) error {
case NonceTypeNeutral:
// Value uses default value
case NonceTypeNonce:
if err := cbor.DecodeGeneric(data, n); err != nil {
type tNonce Nonce
var tmp tNonce
if _, err := cbor.Decode(data, &tmp); err != nil {
return err
}
*n = Nonce(tmp)
default:
return fmt.Errorf("unsupported nonce type %d", nonceType)
}
Expand Down
6 changes: 5 additions & 1 deletion ledger/mary/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,16 @@ type MaryTransactionOutputValue struct {
}

func (v *MaryTransactionOutputValue) UnmarshalCBOR(data []byte) error {
// Try to decode as simple amount first
if _, err := cbor.Decode(data, &(v.Amount)); err == nil {
return nil
}
if err := cbor.DecodeGeneric(data, v); err != nil {
type tMaryTransactionOutputValue MaryTransactionOutputValue
var tmp tMaryTransactionOutputValue
if _, err := cbor.Decode(data, &tmp); err != nil {
return err
}
*v = MaryTransactionOutputValue(tmp)
return nil
}

Expand Down
7 changes: 6 additions & 1 deletion protocol/chainsync/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ func NewMsgRollForwardNtC(
}

func (m *MsgRollForwardNtC) UnmarshalCBOR(data []byte) error {
if err := cbor.DecodeGeneric(data, m); err != nil {
// Decode message
type tMsgRollForwardNtC MsgRollForwardNtC
var tmp tMsgRollForwardNtC
if _, err := cbor.Decode(data, &tmp); err != nil {
return err
}
*m = MsgRollForwardNtC(tmp)
// Decode wrapped block
var wb WrappedBlock
if _, err := cbor.Decode(m.WrappedBlock.Content.([]byte), &wb); err != nil {
return err
Expand Down
7 changes: 6 additions & 1 deletion protocol/localstatequery/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,12 @@ func (u *UtxoId) UnmarshalCBOR(data []byte) error {
u.Hash = tmpData.Hash
u.Idx = tmpData.Idx
case 3:
return cbor.DecodeGeneric(data, u)
type tUtxoId UtxoId
var tmp tUtxoId
if _, err := cbor.Decode(data, &tmp); err != nil {
return err
}
*u = UtxoId(tmp)
default:
return fmt.Errorf("invalid list length: %d", listLen)
}
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy