Skip to content

Commit 62b227b

Browse files
authored
refactor: remove additional uses of reflection for CBOR decoding (#986)
Signed-off-by: Aurora Gaffney <[email protected]>
1 parent f428ca1 commit 62b227b

File tree

7 files changed

+41
-9
lines changed

7 files changed

+41
-9
lines changed

ledger/alonzo/alonzo.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ type AlonzoTransactionOutput struct {
269269
}
270270

271271
func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
272-
// Save original CBOR
273-
o.SetCbor(cborData)
274272
// Try to parse as legacy mary.Mary output first
275273
var tmpOutput mary.MaryTransactionOutput
276274
if _, err := cbor.Decode(cborData, &tmpOutput); err == nil {
@@ -279,8 +277,15 @@ func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
279277
o.OutputAmount = tmpOutput.OutputAmount
280278
o.legacyOutput = true
281279
} else {
282-
return cbor.DecodeGeneric(cborData, o)
280+
type tAlonzoTransactionOutput AlonzoTransactionOutput
281+
var tmp tAlonzoTransactionOutput
282+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
283+
return err
284+
}
285+
*o = AlonzoTransactionOutput(tmp)
283286
}
287+
// Save original CBOR
288+
o.SetCbor(cborData)
284289
return nil
285290
}
286291

ledger/babbage/babbage.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,12 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
435435
o.OutputAmount = tmpOutput.OutputAmount
436436
o.legacyOutput = true
437437
} else {
438-
return cbor.DecodeGeneric(cborData, o)
438+
type tBabbageTransactionOutput BabbageTransactionOutput
439+
var tmp tBabbageTransactionOutput
440+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
441+
return err
442+
}
443+
*o = BabbageTransactionOutput(tmp)
439444
}
440445
return nil
441446
}

ledger/byron/byron.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ func (i *ByronTransactionInput) UnmarshalCBOR(data []byte) error {
337337
}
338338
switch id {
339339
case 0:
340+
// Decode outer data
340341
var tmpData struct {
341342
cbor.StructAsArray
342343
Id int
@@ -345,9 +346,13 @@ func (i *ByronTransactionInput) UnmarshalCBOR(data []byte) error {
345346
if _, err := cbor.Decode(data, &tmpData); err != nil {
346347
return err
347348
}
348-
if err := cbor.DecodeGeneric(tmpData.Cbor, i); err != nil {
349+
// Decode inner data
350+
type tByronTransactionInput ByronTransactionInput
351+
var tmp tByronTransactionInput
352+
if _, err := cbor.Decode(tmpData.Cbor, &tmp); err != nil {
349353
return err
350354
}
355+
*i = ByronTransactionInput(tmp)
351356
default:
352357
// [u8 .ne 0, encoded-cbor]
353358
return errors.New("can't parse yet")

ledger/common/nonce.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ func (n *Nonce) UnmarshalCBOR(data []byte) error {
4545
case NonceTypeNeutral:
4646
// Value uses default value
4747
case NonceTypeNonce:
48-
if err := cbor.DecodeGeneric(data, n); err != nil {
48+
type tNonce Nonce
49+
var tmp tNonce
50+
if _, err := cbor.Decode(data, &tmp); err != nil {
4951
return err
5052
}
53+
*n = Nonce(tmp)
5154
default:
5255
return fmt.Errorf("unsupported nonce type %d", nonceType)
5356
}

ledger/mary/mary.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,16 @@ type MaryTransactionOutputValue struct {
471471
}
472472

473473
func (v *MaryTransactionOutputValue) UnmarshalCBOR(data []byte) error {
474+
// Try to decode as simple amount first
474475
if _, err := cbor.Decode(data, &(v.Amount)); err == nil {
475476
return nil
476477
}
477-
if err := cbor.DecodeGeneric(data, v); err != nil {
478+
type tMaryTransactionOutputValue MaryTransactionOutputValue
479+
var tmp tMaryTransactionOutputValue
480+
if _, err := cbor.Decode(data, &tmp); err != nil {
478481
return err
479482
}
483+
*v = MaryTransactionOutputValue(tmp)
480484
return nil
481485
}
482486

protocol/chainsync/messages.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,14 @@ func NewMsgRollForwardNtC(
144144
}
145145

146146
func (m *MsgRollForwardNtC) UnmarshalCBOR(data []byte) error {
147-
if err := cbor.DecodeGeneric(data, m); err != nil {
147+
// Decode message
148+
type tMsgRollForwardNtC MsgRollForwardNtC
149+
var tmp tMsgRollForwardNtC
150+
if _, err := cbor.Decode(data, &tmp); err != nil {
148151
return err
149152
}
153+
*m = MsgRollForwardNtC(tmp)
154+
// Decode wrapped block
150155
var wb WrappedBlock
151156
if _, err := cbor.Decode(m.WrappedBlock.Content.([]byte), &wb); err != nil {
152157
return err

protocol/localstatequery/queries.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,12 @@ func (u *UtxoId) UnmarshalCBOR(data []byte) error {
525525
u.Hash = tmpData.Hash
526526
u.Idx = tmpData.Idx
527527
case 3:
528-
return cbor.DecodeGeneric(data, u)
528+
type tUtxoId UtxoId
529+
var tmp tUtxoId
530+
if _, err := cbor.Decode(data, &tmp); err != nil {
531+
return err
532+
}
533+
*u = UtxoId(tmp)
529534
default:
530535
return fmt.Errorf("invalid list length: %d", listLen)
531536
}

0 commit comments

Comments
 (0)