Skip to content

Commit 9069e51

Browse files
committed
refactor: remove reflection when storing CBOR on decode
This removes multiple uses of reflection during decoding by creating a static type of the same shape in UnmarshalCBOR. This works because the UnmarshalCBOR function from the original type does not follow to the new type, so generic decoding behavior is used instead. This was only possible after removing embedding in any of the types that we want to capture the original CBOR on. The promoted UnmarshalCBOR function from the embedded type(s) were being called otherwise, which did not produce the desired result. Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 9f19260 commit 9069e51

File tree

9 files changed

+437
-72
lines changed

9 files changed

+437
-72
lines changed

ledger/allegra/allegra.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ type AllegraBlock struct {
5353
}
5454

5555
func (b *AllegraBlock) UnmarshalCBOR(cborData []byte) error {
56-
return b.UnmarshalCbor(cborData, b)
56+
b.SetCbor(cborData)
57+
type tAllegraBlock AllegraBlock
58+
var tmp tAllegraBlock
59+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
60+
return err
61+
}
62+
*b = AllegraBlock(tmp)
63+
return nil
5764
}
5865

5966
func (AllegraBlock) Type() int {
@@ -152,7 +159,14 @@ type AllegraTransactionBody struct {
152159
}
153160

154161
func (b *AllegraTransactionBody) UnmarshalCBOR(cborData []byte) error {
155-
return b.UnmarshalCbor(cborData, b)
162+
b.SetCbor(cborData)
163+
type tAllegraTransactionBody AllegraTransactionBody
164+
var tmp tAllegraTransactionBody
165+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
166+
return err
167+
}
168+
*b = AllegraTransactionBody(tmp)
169+
return nil
156170
}
157171

158172
func (b *AllegraTransactionBody) Inputs() []common.TransactionInput {
@@ -219,8 +233,15 @@ type AllegraTransaction struct {
219233
TxMetadata *cbor.LazyValue
220234
}
221235

222-
func (t *AllegraTransaction) UnmarshalCBOR(data []byte) error {
223-
return t.UnmarshalCbor(data, t)
236+
func (t *AllegraTransaction) UnmarshalCBOR(cborData []byte) error {
237+
t.SetCbor(cborData)
238+
type tAllegraTransaction AllegraTransaction
239+
var tmp tAllegraTransaction
240+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
241+
return err
242+
}
243+
*t = AllegraTransaction(tmp)
244+
return nil
224245
}
225246

226247
func (AllegraTransaction) Type() int {

ledger/alonzo/alonzo.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ type AlonzoBlock struct {
5656
}
5757

5858
func (b *AlonzoBlock) UnmarshalCBOR(cborData []byte) error {
59-
return b.UnmarshalCbor(cborData, b)
59+
b.SetCbor(cborData)
60+
type tAlonzoBlock AlonzoBlock
61+
var tmp tAlonzoBlock
62+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
63+
return err
64+
}
65+
*b = AlonzoBlock(tmp)
66+
return nil
6067
}
6168

6269
func (AlonzoBlock) Type() int {
@@ -166,7 +173,14 @@ type AlonzoTransactionBody struct {
166173
}
167174

168175
func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
169-
return b.UnmarshalCbor(cborData, b)
176+
b.SetCbor(cborData)
177+
type tAlonzoTransactionBody AlonzoTransactionBody
178+
var tmp tAlonzoTransactionBody
179+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
180+
return err
181+
}
182+
*b = AlonzoTransactionBody(tmp)
183+
return nil
170184
}
171185

172186
func (b *AlonzoTransactionBody) Inputs() []common.TransactionInput {
@@ -391,7 +405,14 @@ type AlonzoTransactionWitnessSet struct {
391405
}
392406

393407
func (w *AlonzoTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
394-
return w.UnmarshalCbor(cborData, w)
408+
w.SetCbor(cborData)
409+
type tAlonzoTransactionWitnessSet AlonzoTransactionWitnessSet
410+
var tmp tAlonzoTransactionWitnessSet
411+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
412+
return err
413+
}
414+
*w = AlonzoTransactionWitnessSet(tmp)
415+
return nil
395416
}
396417

397418
func (w AlonzoTransactionWitnessSet) Vkey() []common.VkeyWitness {
@@ -437,8 +458,15 @@ type AlonzoTransaction struct {
437458
TxMetadata *cbor.LazyValue
438459
}
439460

440-
func (t *AlonzoTransaction) UnmarshalCBOR(data []byte) error {
441-
return t.UnmarshalCbor(data, t)
461+
func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
462+
t.SetCbor(cborData)
463+
type tAlonzoTransaction AlonzoTransaction
464+
var tmp tAlonzoTransaction
465+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
466+
return err
467+
}
468+
*t = AlonzoTransaction(tmp)
469+
return nil
442470
}
443471

444472
func (AlonzoTransaction) Type() int {

ledger/babbage/babbage.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ type BabbageBlock struct {
5858
}
5959

6060
func (b *BabbageBlock) UnmarshalCBOR(cborData []byte) error {
61-
return b.UnmarshalCbor(cborData, b)
61+
b.SetCbor(cborData)
62+
type tBabbageBlock BabbageBlock
63+
var tmp tBabbageBlock
64+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
65+
return err
66+
}
67+
*b = BabbageBlock(tmp)
68+
return nil
6269
}
6370

6471
func (BabbageBlock) Type() int {
@@ -174,7 +181,14 @@ type BabbageProtoVersion struct {
174181
}
175182

176183
func (h *BabbageBlockHeader) UnmarshalCBOR(cborData []byte) error {
177-
return h.UnmarshalCbor(cborData, h)
184+
h.SetCbor(cborData)
185+
type tBabbageBlockHeader BabbageBlockHeader
186+
var tmp tBabbageBlockHeader
187+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
188+
return err
189+
}
190+
*h = BabbageBlockHeader(tmp)
191+
return nil
178192
}
179193

180194
func (h *BabbageBlockHeader) Hash() common.Blake2b256 {
@@ -235,7 +249,14 @@ type BabbageTransactionBody struct {
235249
}
236250

237251
func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
238-
return b.UnmarshalCbor(cborData, b)
252+
b.SetCbor(cborData)
253+
type tBabbageTransactionBody BabbageTransactionBody
254+
var tmp tBabbageTransactionBody
255+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
256+
return err
257+
}
258+
*b = BabbageTransactionBody(tmp)
259+
return nil
239260
}
240261

241262
func (b *BabbageTransactionBody) Inputs() []common.TransactionInput {
@@ -537,7 +558,14 @@ type BabbageTransactionWitnessSet struct {
537558
}
538559

539560
func (w *BabbageTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
540-
return w.UnmarshalCbor(cborData, w)
561+
w.SetCbor(cborData)
562+
type tBabbageTransactionWitnessSet BabbageTransactionWitnessSet
563+
var tmp tBabbageTransactionWitnessSet
564+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
565+
return err
566+
}
567+
*w = BabbageTransactionWitnessSet(tmp)
568+
return nil
541569
}
542570

543571
func (w BabbageTransactionWitnessSet) Vkey() []common.VkeyWitness {
@@ -582,8 +610,15 @@ type BabbageTransaction struct {
582610
TxMetadata *cbor.LazyValue
583611
}
584612

585-
func (t *BabbageTransaction) UnmarshalCBOR(data []byte) error {
586-
return t.UnmarshalCbor(data, t)
613+
func (t *BabbageTransaction) UnmarshalCBOR(cborData []byte) error {
614+
t.SetCbor(cborData)
615+
type tBabbageTransaction BabbageTransaction
616+
var tmp tBabbageTransaction
617+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
618+
return err
619+
}
620+
*t = BabbageTransaction(tmp)
621+
return nil
587622
}
588623

589624
func (BabbageTransaction) Type() int {

ledger/byron/byron.go

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,14 @@ type ByronMainBlockHeader struct {
8080
}
8181

8282
func (h *ByronMainBlockHeader) UnmarshalCBOR(cborData []byte) error {
83-
// Decode generically and store original CBOR
84-
return h.UnmarshalCbor(cborData, h)
83+
h.SetCbor(cborData)
84+
type tByronMainBlockHeader ByronMainBlockHeader
85+
var tmp tByronMainBlockHeader
86+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
87+
return err
88+
}
89+
*h = ByronMainBlockHeader(tmp)
90+
return nil
8591
}
8692

8793
func (h *ByronMainBlockHeader) Hash() common.Blake2b256 {
@@ -137,9 +143,15 @@ type ByronTransaction struct {
137143
Attributes *cbor.LazyValue
138144
}
139145

140-
func (t *ByronTransaction) UnmarshalCBOR(data []byte) error {
141-
// Decode generically and store original CBOR
142-
return t.UnmarshalCbor(data, t)
146+
func (t *ByronTransaction) UnmarshalCBOR(cborData []byte) error {
147+
t.SetCbor(cborData)
148+
type tByronTransaction ByronTransaction
149+
var tmp tByronTransaction
150+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
151+
return err
152+
}
153+
*t = ByronTransaction(tmp)
154+
return nil
143155
}
144156

145157
func (ByronTransaction) Type() int {
@@ -451,8 +463,15 @@ type ByronUpdateProposal struct {
451463
Signature []byte
452464
}
453465

454-
func (p *ByronUpdateProposal) UnmarshalCBOR(data []byte) error {
455-
return p.UnmarshalCbor(data, p)
466+
func (p *ByronUpdateProposal) UnmarshalCBOR(cborData []byte) error {
467+
p.SetCbor(cborData)
468+
type tByronUpdateProposal ByronUpdateProposal
469+
var tmp tByronUpdateProposal
470+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
471+
return err
472+
}
473+
*p = ByronUpdateProposal(tmp)
474+
return nil
456475
}
457476

458477
type ByronUpdateProposalBlockVersionMod struct {
@@ -488,9 +507,15 @@ type ByronMainBlockBody struct {
488507
UpdPayload ByronUpdatePayload
489508
}
490509

491-
func (b *ByronMainBlockBody) UnmarshalCBOR(data []byte) error {
492-
// Decode generically and store original CBOR
493-
return b.UnmarshalCbor(data, b)
510+
func (b *ByronMainBlockBody) UnmarshalCBOR(cborData []byte) error {
511+
b.SetCbor(cborData)
512+
type tByronMainBlockBody ByronMainBlockBody
513+
var tmp tByronMainBlockBody
514+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
515+
return err
516+
}
517+
*b = ByronMainBlockBody(tmp)
518+
return nil
494519
}
495520

496521
type ByronEpochBoundaryBlockHeader struct {
@@ -512,8 +537,14 @@ type ByronEpochBoundaryBlockHeader struct {
512537
}
513538

514539
func (h *ByronEpochBoundaryBlockHeader) UnmarshalCBOR(cborData []byte) error {
515-
// Decode generically and store original CBOR
516-
return h.UnmarshalCbor(cborData, h)
540+
h.SetCbor(cborData)
541+
type tByronEpochBoundaryBlockHeader ByronEpochBoundaryBlockHeader
542+
var tmp tByronEpochBoundaryBlockHeader
543+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
544+
return err
545+
}
546+
*h = ByronEpochBoundaryBlockHeader(tmp)
547+
return nil
517548
}
518549

519550
func (h *ByronEpochBoundaryBlockHeader) Hash() common.Blake2b256 {
@@ -567,8 +598,14 @@ type ByronMainBlock struct {
567598
}
568599

569600
func (b *ByronMainBlock) UnmarshalCBOR(cborData []byte) error {
570-
// Decode generically and store original CBOR
571-
return b.UnmarshalCbor(cborData, b)
601+
b.SetCbor(cborData)
602+
type tByronMainBlock ByronMainBlock
603+
var tmp tByronMainBlock
604+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
605+
return err
606+
}
607+
*b = ByronMainBlock(tmp)
608+
return nil
572609
}
573610

574611
func (ByronMainBlock) Type() int {
@@ -628,8 +665,14 @@ type ByronEpochBoundaryBlock struct {
628665
}
629666

630667
func (b *ByronEpochBoundaryBlock) UnmarshalCBOR(cborData []byte) error {
631-
// Decode generically and store original CBOR
632-
return b.UnmarshalCbor(cborData, b)
668+
b.SetCbor(cborData)
669+
type tByronEpochBoundaryBlock ByronEpochBoundaryBlock
670+
var tmp tByronEpochBoundaryBlock
671+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
672+
return err
673+
}
674+
*b = ByronEpochBoundaryBlock(tmp)
675+
return nil
633676
}
634677

635678
func (ByronEpochBoundaryBlock) Type() int {

0 commit comments

Comments
 (0)