Skip to content

Commit ad654fe

Browse files
authored
Merge pull request #215 from cloudstruct/fix/ntc-chainsync-decode-error
fix: store block type/CBOR in MsgRollForwardNtC
2 parents 24b3312 + 83cb63d commit ad654fe

File tree

9 files changed

+94
-73
lines changed

9 files changed

+94
-73
lines changed

cbor/cbor.go

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package cbor
22

33
import (
4-
"fmt"
5-
"reflect"
6-
74
_cbor "github.com/fxamacker/cbor/v2"
8-
"github.com/jinzhu/copier"
95
)
106

117
const (
@@ -46,32 +42,9 @@ func (d *DecodeStoreCbor) Cbor() []byte {
4642
return d.cborData
4743
}
4844

49-
// UnmarshalCborGeneric decodes the specified CBOR into the destination object without using the
50-
// destination object's UnmarshalCBOR() function
51-
func (d *DecodeStoreCbor) UnmarshalCborGeneric(cborData []byte, dest DecodeStoreCborInterface) error {
52-
// Create a duplicate(-ish) struct from the destination
53-
// We do this so that we can bypass any custom UnmarshalCBOR() function on the
54-
// destination object
55-
valueDest := reflect.ValueOf(dest)
56-
if valueDest.Kind() != reflect.Pointer || valueDest.Elem().Kind() != reflect.Struct {
57-
return fmt.Errorf("destination must be a pointer to a struct")
58-
}
59-
typeDestElem := valueDest.Elem().Type()
60-
destTypeFields := []reflect.StructField{}
61-
for i := 0; i < typeDestElem.NumField(); i++ {
62-
tmpField := typeDestElem.Field(i)
63-
if tmpField.IsExported() && tmpField.Name != "DecodeStoreCbor" {
64-
destTypeFields = append(destTypeFields, tmpField)
65-
}
66-
}
67-
// Create temporary object with the type created above
68-
tmpDest := reflect.New(reflect.StructOf(destTypeFields))
69-
// Decode CBOR into temporary object
70-
if _, err := Decode(cborData, tmpDest.Interface()); err != nil {
71-
return err
72-
}
73-
// Copy values from temporary object into destination object
74-
if err := copier.Copy(dest, tmpDest.Interface()); err != nil {
45+
// UnmarshalCbor decodes the specified CBOR into the destination object and saves the original CBOR
46+
func (d *DecodeStoreCbor) UnmarshalCbor(cborData []byte, dest DecodeStoreCborInterface) error {
47+
if err := DecodeGeneric(cborData, dest); err != nil {
7548
return err
7649
}
7750
// Store a copy of the original CBOR data

cbor/decode.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package cbor
33
import (
44
"bytes"
55
"fmt"
6+
"reflect"
67

78
_cbor "github.com/fxamacker/cbor/v2"
9+
"github.com/jinzhu/copier"
810
)
911

1012
func Decode(dataBytes []byte, dest interface{}) (int, error) {
@@ -78,3 +80,34 @@ func DecodeById(cborData []byte, idMap map[int]interface{}) (interface{}, error)
7880
}
7981
return ret, nil
8082
}
83+
84+
// DecodeGeneric decodes the specified CBOR into the destination object without using the
85+
// destination object's UnmarshalCBOR() function
86+
func DecodeGeneric(cborData []byte, dest interface{}) error {
87+
// Create a duplicate(-ish) struct from the destination
88+
// We do this so that we can bypass any custom UnmarshalCBOR() function on the
89+
// destination object
90+
valueDest := reflect.ValueOf(dest)
91+
if valueDest.Kind() != reflect.Pointer || valueDest.Elem().Kind() != reflect.Struct {
92+
return fmt.Errorf("destination must be a pointer to a struct")
93+
}
94+
typeDestElem := valueDest.Elem().Type()
95+
destTypeFields := []reflect.StructField{}
96+
for i := 0; i < typeDestElem.NumField(); i++ {
97+
tmpField := typeDestElem.Field(i)
98+
if tmpField.IsExported() && tmpField.Name != "DecodeStoreCbor" {
99+
destTypeFields = append(destTypeFields, tmpField)
100+
}
101+
}
102+
// Create temporary object with the type created above
103+
tmpDest := reflect.New(reflect.StructOf(destTypeFields))
104+
// Decode CBOR into temporary object
105+
if _, err := Decode(cborData, tmpDest.Interface()); err != nil {
106+
return err
107+
}
108+
// Copy values from temporary object into destination object
109+
if err := copier.Copy(dest, tmpDest.Interface()); err != nil {
110+
return err
111+
}
112+
return nil
113+
}

ledger/allegra.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type AllegraBlock struct {
2626
}
2727

2828
func (b *AllegraBlock) UnmarshalCBOR(cborData []byte) error {
29-
return b.UnmarshalCborGeneric(cborData, b)
29+
return b.UnmarshalCbor(cborData, b)
3030
}
3131

3232
func (b *AllegraBlock) Hash() string {
@@ -69,7 +69,7 @@ type AllegraTransactionBody struct {
6969
}
7070

7171
func (b *AllegraTransactionBody) UnmarshalCBOR(cborData []byte) error {
72-
return b.UnmarshalCborGeneric(cborData, b)
72+
return b.UnmarshalCbor(cborData, b)
7373
}
7474

7575
type AllegraTransaction struct {
@@ -82,23 +82,23 @@ type AllegraTransaction struct {
8282
func NewAllegraBlockFromCbor(data []byte) (*AllegraBlock, error) {
8383
var allegraBlock AllegraBlock
8484
if _, err := cbor.Decode(data, &allegraBlock); err != nil {
85-
return nil, fmt.Errorf("decode error: %s", err)
85+
return nil, fmt.Errorf("Allegra block decode error: %s", err)
8686
}
8787
return &allegraBlock, nil
8888
}
8989

9090
func NewAllegraTransactionBodyFromCbor(data []byte) (*AllegraTransactionBody, error) {
9191
var allegraTx AllegraTransactionBody
9292
if _, err := cbor.Decode(data, &allegraTx); err != nil {
93-
return nil, fmt.Errorf("decode error: %s", err)
93+
return nil, fmt.Errorf("Allegra transaction body decode error: %s", err)
9494
}
9595
return &allegraTx, nil
9696
}
9797

9898
func NewAllegraTransactionFromCbor(data []byte) (*AllegraTransaction, error) {
9999
var allegraTx AllegraTransaction
100100
if _, err := cbor.Decode(data, &allegraTx); err != nil {
101-
return nil, fmt.Errorf("decode error: %s", err)
101+
return nil, fmt.Errorf("Allegra transaction decode error: %s", err)
102102
}
103103
return &allegraTx, nil
104104
}

ledger/alonzo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type AlonzoBlock struct {
2727
}
2828

2929
func (b *AlonzoBlock) UnmarshalCBOR(cborData []byte) error {
30-
return b.UnmarshalCborGeneric(cborData, b)
30+
return b.UnmarshalCbor(cborData, b)
3131
}
3232

3333
func (b *AlonzoBlock) Hash() string {
@@ -74,7 +74,7 @@ type AlonzoTransactionBody struct {
7474
}
7575

7676
func (b *AlonzoTransactionBody) UnmarshalCBOR(cborData []byte) error {
77-
return b.UnmarshalCborGeneric(cborData, b)
77+
return b.UnmarshalCbor(cborData, b)
7878
}
7979

8080
type AlonzoTransactionOutput struct {
@@ -93,7 +93,7 @@ func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
9393
o.Address = tmpOutput.Address
9494
o.Amount = tmpOutput.Amount
9595
} else {
96-
return o.UnmarshalCborGeneric(cborData, o)
96+
return o.UnmarshalCbor(cborData, o)
9797
}
9898
return nil
9999
}
@@ -116,23 +116,23 @@ type AlonzoTransaction struct {
116116
func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) {
117117
var alonzoBlock AlonzoBlock
118118
if _, err := cbor.Decode(data, &alonzoBlock); err != nil {
119-
return nil, fmt.Errorf("decode error: %s", err)
119+
return nil, fmt.Errorf("Alonzo block decode error: %s", err)
120120
}
121121
return &alonzoBlock, nil
122122
}
123123

124124
func NewAlonzoTransactionBodyFromCbor(data []byte) (*AlonzoTransactionBody, error) {
125125
var alonzoTx AlonzoTransactionBody
126126
if _, err := cbor.Decode(data, &alonzoTx); err != nil {
127-
return nil, fmt.Errorf("decode error: %s", err)
127+
return nil, fmt.Errorf("Alonzo transaction body decode error: %s", err)
128128
}
129129
return &alonzoTx, nil
130130
}
131131

132132
func NewAlonzoTransactionFromCbor(data []byte) (*AlonzoTransaction, error) {
133133
var alonzoTx AlonzoTransaction
134134
if _, err := cbor.Decode(data, &alonzoTx); err != nil {
135-
return nil, fmt.Errorf("decode error: %s", err)
135+
return nil, fmt.Errorf("Alonzo transaction decode error: %s", err)
136136
}
137137
return &alonzoTx, nil
138138
}

ledger/babbage.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type BabbageBlock struct {
2727
}
2828

2929
func (b *BabbageBlock) UnmarshalCBOR(cborData []byte) error {
30-
return b.UnmarshalCborGeneric(cborData, b)
30+
return b.UnmarshalCbor(cborData, b)
3131
}
3232

3333
func (b *BabbageBlock) Hash() string {
@@ -87,7 +87,7 @@ type BabbageBlockHeader struct {
8787
}
8888

8989
func (h *BabbageBlockHeader) UnmarshalCBOR(cborData []byte) error {
90-
return h.UnmarshalCborGeneric(cborData, h)
90+
return h.UnmarshalCbor(cborData, h)
9191
}
9292

9393
func (h *BabbageBlockHeader) Hash() string {
@@ -118,11 +118,10 @@ type BabbageTransactionBody struct {
118118
}
119119

120120
func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
121-
return b.UnmarshalCborGeneric(cborData, b)
121+
return b.UnmarshalCbor(cborData, b)
122122
}
123123

124124
type BabbageTransactionOutput struct {
125-
cbor.DecodeStoreCbor
126125
Address Blake2b256 `cbor:"0,keyasint,omitempty"`
127126
Amount cbor.Value `cbor:"1,keyasint,omitempty"`
128127
DatumOption []cbor.RawMessage `cbor:"2,keyasint,omitempty"`
@@ -139,7 +138,7 @@ func (o *BabbageTransactionOutput) UnmarshalCBOR(cborData []byte) error {
139138
o.Amount = tmpOutput.Amount
140139
o.legacyOutput = true
141140
} else {
142-
return o.UnmarshalCborGeneric(cborData, o)
141+
return cbor.DecodeGeneric(cborData, o)
143142
}
144143
return nil
145144
}
@@ -160,31 +159,31 @@ type BabbageTransaction struct {
160159
func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {
161160
var babbageBlock BabbageBlock
162161
if _, err := cbor.Decode(data, &babbageBlock); err != nil {
163-
return nil, fmt.Errorf("decode error: %s", err)
162+
return nil, fmt.Errorf("Babbage block decode error: %s", err)
164163
}
165164
return &babbageBlock, nil
166165
}
167166

168167
func NewBabbageBlockHeaderFromCbor(data []byte) (*BabbageBlockHeader, error) {
169168
var babbageBlockHeader BabbageBlockHeader
170169
if _, err := cbor.Decode(data, &babbageBlockHeader); err != nil {
171-
return nil, fmt.Errorf("decode error: %s", err)
170+
return nil, fmt.Errorf("Babbage block header decode error: %s", err)
172171
}
173172
return &babbageBlockHeader, nil
174173
}
175174

176175
func NewBabbageTransactionBodyFromCbor(data []byte) (*BabbageTransactionBody, error) {
177176
var babbageTx BabbageTransactionBody
178177
if _, err := cbor.Decode(data, &babbageTx); err != nil {
179-
return nil, fmt.Errorf("decode error: %s", err)
178+
return nil, fmt.Errorf("Babbage transaction body decode error: %s", err)
180179
}
181180
return &babbageTx, nil
182181
}
183182

184183
func NewBabbageTransactionFromCbor(data []byte) (*BabbageTransaction, error) {
185184
var babbageTx BabbageTransaction
186185
if _, err := cbor.Decode(data, &babbageTx); err != nil {
187-
return nil, fmt.Errorf("decode error: %s", err)
186+
return nil, fmt.Errorf("Babbage transaction decode error: %s", err)
188187
}
189188
return &babbageTx, nil
190189
}

ledger/byron.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type ByronMainBlockHeader struct {
5858
}
5959

6060
func (h *ByronMainBlockHeader) UnmarshalCBOR(cborData []byte) error {
61-
return h.UnmarshalCborGeneric(cborData, h)
61+
return h.UnmarshalCbor(cborData, h)
6262
}
6363

6464
func (h *ByronMainBlockHeader) Hash() string {
@@ -117,7 +117,7 @@ type ByronEpochBoundaryBlockHeader struct {
117117
}
118118

119119
func (h *ByronEpochBoundaryBlockHeader) UnmarshalCBOR(cborData []byte) error {
120-
return h.UnmarshalCborGeneric(cborData, h)
120+
return h.UnmarshalCbor(cborData, h)
121121
}
122122

123123
func (h *ByronEpochBoundaryBlockHeader) Hash() string {
@@ -153,7 +153,7 @@ type ByronMainBlock struct {
153153
}
154154

155155
func (b *ByronMainBlock) UnmarshalCBOR(cborData []byte) error {
156-
return b.UnmarshalCborGeneric(cborData, b)
156+
return b.UnmarshalCbor(cborData, b)
157157
}
158158

159159
func (b *ByronMainBlock) Hash() string {
@@ -186,7 +186,7 @@ type ByronEpochBoundaryBlock struct {
186186
}
187187

188188
func (b *ByronEpochBoundaryBlock) UnmarshalCBOR(cborData []byte) error {
189-
return b.UnmarshalCborGeneric(cborData, b)
189+
return b.UnmarshalCbor(cborData, b)
190190
}
191191

192192
func (b *ByronEpochBoundaryBlock) Hash() string {
@@ -213,47 +213,47 @@ func (b *ByronEpochBoundaryBlock) Transactions() []TransactionBody {
213213
func NewByronEpochBoundaryBlockFromCbor(data []byte) (*ByronEpochBoundaryBlock, error) {
214214
var byronEbbBlock ByronEpochBoundaryBlock
215215
if _, err := cbor.Decode(data, &byronEbbBlock); err != nil {
216-
return nil, fmt.Errorf("decode error: %s", err)
216+
return nil, fmt.Errorf("Byron EBB block decode error: %s", err)
217217
}
218218
return &byronEbbBlock, nil
219219
}
220220

221221
func NewByronEpochBoundaryBlockHeaderFromCbor(data []byte) (*ByronEpochBoundaryBlockHeader, error) {
222222
var byronEbbBlockHeader ByronEpochBoundaryBlockHeader
223223
if _, err := cbor.Decode(data, &byronEbbBlockHeader); err != nil {
224-
return nil, fmt.Errorf("decode error: %s", err)
224+
return nil, fmt.Errorf("Byron EBB block header decode error: %s", err)
225225
}
226226
return &byronEbbBlockHeader, nil
227227
}
228228

229229
func NewByronMainBlockFromCbor(data []byte) (*ByronMainBlock, error) {
230230
var byronMainBlock ByronMainBlock
231231
if _, err := cbor.Decode(data, &byronMainBlock); err != nil {
232-
return nil, fmt.Errorf("decode error: %s", err)
232+
return nil, fmt.Errorf("Byron main block decode error: %s", err)
233233
}
234234
return &byronMainBlock, nil
235235
}
236236

237237
func NewByronMainBlockHeaderFromCbor(data []byte) (*ByronMainBlockHeader, error) {
238238
var byronMainBlockHeader ByronMainBlockHeader
239239
if _, err := cbor.Decode(data, &byronMainBlockHeader); err != nil {
240-
return nil, fmt.Errorf("decode error: %s", err)
240+
return nil, fmt.Errorf("Byron main block header decode error: %s", err)
241241
}
242242
return &byronMainBlockHeader, nil
243243
}
244244

245245
func NewByronTransactionBodyFromCbor(data []byte) (*ByronTransactionBody, error) {
246246
var byronTx ByronTransactionBody
247247
if _, err := cbor.Decode(data, &byronTx); err != nil {
248-
return nil, fmt.Errorf("decode error: %s", err)
248+
return nil, fmt.Errorf("Byron transaction body decode error: %s", err)
249249
}
250250
return &byronTx, nil
251251
}
252252

253253
func NewByronTransactionFromCbor(data []byte) (*ByronTransaction, error) {
254254
var byronTx ByronTransaction
255255
if _, err := cbor.Decode(data, &byronTx); err != nil {
256-
return nil, fmt.Errorf("decode error: %s", err)
256+
return nil, fmt.Errorf("Byron transaction decode error: %s", err)
257257
}
258258
return &byronTx, nil
259259
}

0 commit comments

Comments
 (0)