Skip to content

Commit 60087de

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 <aurora@blinklabs.io>
1 parent 9f19260 commit 60087de

File tree

9 files changed

+438
-72
lines changed

9 files changed

+438
-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+
type tAllegraBlock AllegraBlock
57+
var tmp tAllegraBlock
58+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
59+
return err
60+
}
61+
*b = AllegraBlock(tmp)
62+
b.SetCbor(cborData)
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+
type tAllegraTransactionBody AllegraTransactionBody
163+
var tmp tAllegraTransactionBody
164+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
165+
return err
166+
}
167+
*b = AllegraTransactionBody(tmp)
168+
b.SetCbor(cborData)
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+
type tAllegraTransaction AllegraTransaction
238+
var tmp tAllegraTransaction
239+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
240+
return err
241+
}
242+
*t = AllegraTransaction(tmp)
243+
t.SetCbor(cborData)
244+
return nil
224245
}
225246

226247
func (AllegraTransaction) Type() int {

ledger/alonzo/alonzo.go

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

5858
func (b *AlonzoBlock) UnmarshalCBOR(cborData []byte) error {
59-
return b.UnmarshalCbor(cborData, b)
59+
type tAlonzoBlock AlonzoBlock
60+
var tmp tAlonzoBlock
61+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
62+
return err
63+
}
64+
*b = AlonzoBlock(tmp)
65+
b.SetCbor(cborData)
66+
fmt.Printf("AlonzoBlock b = %#v\n", b)
67+
return nil
6068
}
6169

6270
func (AlonzoBlock) Type() int {
@@ -166,7 +174,14 @@ type AlonzoTransactionBody struct {
166174
}
167175

168176
func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
169-
return b.UnmarshalCbor(cborData, b)
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+
b.SetCbor(cborData)
184+
return nil
170185
}
171186

172187
func (b *AlonzoTransactionBody) Inputs() []common.TransactionInput {
@@ -391,7 +406,14 @@ type AlonzoTransactionWitnessSet struct {
391406
}
392407

393408
func (w *AlonzoTransactionWitnessSet) UnmarshalCBOR(cborData []byte) error {
394-
return w.UnmarshalCbor(cborData, w)
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+
w.SetCbor(cborData)
416+
return nil
395417
}
396418

397419
func (w AlonzoTransactionWitnessSet) Vkey() []common.VkeyWitness {
@@ -437,8 +459,15 @@ type AlonzoTransaction struct {
437459
TxMetadata *cbor.LazyValue
438460
}
439461

440-
func (t *AlonzoTransaction) UnmarshalCBOR(data []byte) error {
441-
return t.UnmarshalCbor(data, t)
462+
func (t *AlonzoTransaction) UnmarshalCBOR(cborData []byte) error {
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+
t.SetCbor(cborData)
470+
return nil
442471
}
443472

444473
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+
type tBabbageBlock BabbageBlock
62+
var tmp tBabbageBlock
63+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
64+
return err
65+
}
66+
*b = BabbageBlock(tmp)
67+
b.SetCbor(cborData)
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+
type tBabbageBlockHeader BabbageBlockHeader
185+
var tmp tBabbageBlockHeader
186+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
187+
return err
188+
}
189+
*h = BabbageBlockHeader(tmp)
190+
h.SetCbor(cborData)
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+
type tBabbageTransactionBody BabbageTransactionBody
253+
var tmp tBabbageTransactionBody
254+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
255+
return err
256+
}
257+
*b = BabbageTransactionBody(tmp)
258+
b.SetCbor(cborData)
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+
type tBabbageTransactionWitnessSet BabbageTransactionWitnessSet
562+
var tmp tBabbageTransactionWitnessSet
563+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
564+
return err
565+
}
566+
*w = BabbageTransactionWitnessSet(tmp)
567+
w.SetCbor(cborData)
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+
type tBabbageTransaction BabbageTransaction
615+
var tmp tBabbageTransaction
616+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
617+
return err
618+
}
619+
*t = BabbageTransaction(tmp)
620+
t.SetCbor(cborData)
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+
type tByronMainBlockHeader ByronMainBlockHeader
84+
var tmp tByronMainBlockHeader
85+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
86+
return err
87+
}
88+
*h = ByronMainBlockHeader(tmp)
89+
h.SetCbor(cborData)
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+
type tByronTransaction ByronTransaction
148+
var tmp tByronTransaction
149+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
150+
return err
151+
}
152+
*t = ByronTransaction(tmp)
153+
t.SetCbor(cborData)
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+
type tByronUpdateProposal ByronUpdateProposal
468+
var tmp tByronUpdateProposal
469+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
470+
return err
471+
}
472+
*p = ByronUpdateProposal(tmp)
473+
p.SetCbor(cborData)
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+
type tByronMainBlockBody ByronMainBlockBody
512+
var tmp tByronMainBlockBody
513+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
514+
return err
515+
}
516+
*b = ByronMainBlockBody(tmp)
517+
b.SetCbor(cborData)
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+
type tByronEpochBoundaryBlockHeader ByronEpochBoundaryBlockHeader
541+
var tmp tByronEpochBoundaryBlockHeader
542+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
543+
return err
544+
}
545+
*h = ByronEpochBoundaryBlockHeader(tmp)
546+
h.SetCbor(cborData)
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+
type tByronMainBlock ByronMainBlock
602+
var tmp tByronMainBlock
603+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
604+
return err
605+
}
606+
*b = ByronMainBlock(tmp)
607+
b.SetCbor(cborData)
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+
type tByronEpochBoundaryBlock ByronEpochBoundaryBlock
669+
var tmp tByronEpochBoundaryBlock
670+
if _, err := cbor.Decode(cborData, &tmp); err != nil {
671+
return err
672+
}
673+
*b = ByronEpochBoundaryBlock(tmp)
674+
b.SetCbor(cborData)
675+
return nil
633676
}
634677

635678
func (ByronEpochBoundaryBlock) Type() int {

0 commit comments

Comments
 (0)
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