Skip to content

Commit 996d3b7

Browse files
committed
refactor: use CBOR set tags when encoding TX body fields
This creates a generic CBOR set type wrapper that supports the optional encoding format for various transaction body attributes Fixes #1063 Fixes #1064 Fixes #1065 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 9ba5f28 commit 996d3b7

File tree

6 files changed

+167
-62
lines changed

6 files changed

+167
-62
lines changed

cbor/tags.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,54 @@ type Set []any
159159

160160
// Map corresponds to CBOR tag 259 and is used to represent a map with key/value operations
161161
type Map map[any]any
162+
163+
// SetType is a generic type for wrapping other types in an optional CBOR set tag
164+
type SetType[T any] struct {
165+
useTag bool
166+
items []T
167+
}
168+
169+
func NewSetType[T any](items []T, useTag bool) SetType[T] {
170+
return SetType[T]{
171+
items: items,
172+
useTag: useTag,
173+
}
174+
}
175+
176+
func (t *SetType[T]) UnmarshalCBOR(data []byte) error {
177+
// Check if the set is wrapped in a CBOR tag
178+
// This is mostly needed so we can remember whether it was Set-wrapped for CBOR encoding
179+
var tmpTag RawTag
180+
t.useTag = false
181+
if _, err := Decode(data, &tmpTag); err == nil {
182+
if tmpTag.Number != CborTagSet {
183+
return errors.New("unexpected tag type")
184+
}
185+
data = []byte(tmpTag.Content)
186+
t.useTag = true
187+
}
188+
var tmpData []T
189+
if _, err := Decode(data, &tmpData); err != nil {
190+
return err
191+
}
192+
t.items = tmpData
193+
return nil
194+
}
195+
196+
func (t *SetType[T]) MarshalCBOR() ([]byte, error) {
197+
tmpItems := make([]any, len(t.items))
198+
for i, item := range t.items {
199+
tmpItems[i] = item
200+
}
201+
var tmpData any = tmpItems
202+
if t.useTag {
203+
tmpData = Set(tmpItems)
204+
}
205+
return Encode(tmpData)
206+
}
207+
208+
func (t *SetType[T]) Items() []T {
209+
ret := make([]T, len(t.items))
210+
copy(ret, t.items)
211+
return ret
212+
}

ledger/alonzo/alonzo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ type AlonzoTransactionBody struct {
172172
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
173173
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
174174
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
175-
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
176-
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
175+
TxCollateral cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"13,keyasint,omitempty,omitzero"`
176+
TxRequiredSigners cbor.SetType[common.Blake2b224] `cbor:"14,keyasint,omitempty,omitzero"`
177177
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
178178
}
179179

@@ -246,14 +246,14 @@ func (b *AlonzoTransactionBody) AssetMint() *common.MultiAsset[common.MultiAsset
246246

247247
func (b *AlonzoTransactionBody) Collateral() []common.TransactionInput {
248248
ret := []common.TransactionInput{}
249-
for _, collateral := range b.TxCollateral {
249+
for _, collateral := range b.TxCollateral.Items() {
250250
ret = append(ret, collateral)
251251
}
252252
return ret
253253
}
254254

255255
func (b *AlonzoTransactionBody) RequiredSigners() []common.Blake2b224 {
256-
return b.TxRequiredSigners[:]
256+
return b.TxRequiredSigners.Items()
257257
}
258258

259259
func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 {

ledger/babbage/babbage.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,12 @@ type BabbageTransactionBody struct {
245245
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
246246
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
247247
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
248-
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
249-
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
248+
TxCollateral cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"13,keyasint,omitempty,omitzero"`
249+
TxRequiredSigners cbor.SetType[common.Blake2b224] `cbor:"14,keyasint,omitempty,omitzero"`
250250
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
251251
TxCollateralReturn *BabbageTransactionOutput `cbor:"16,keyasint,omitempty"`
252252
TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"`
253-
TxReferenceInputs []shelley.ShelleyTransactionInput `cbor:"18,keyasint,omitempty"`
253+
TxReferenceInputs cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"18,keyasint,omitempty,omitzero"`
254254
}
255255

256256
func (b *BabbageTransactionBody) UnmarshalCBOR(cborData []byte) error {
@@ -322,14 +322,14 @@ func (b *BabbageTransactionBody) AssetMint() *common.MultiAsset[common.MultiAsse
322322

323323
func (b *BabbageTransactionBody) Collateral() []common.TransactionInput {
324324
ret := []common.TransactionInput{}
325-
for _, collateral := range b.TxCollateral {
325+
for _, collateral := range b.TxCollateral.Items() {
326326
ret = append(ret, collateral)
327327
}
328328
return ret
329329
}
330330

331331
func (b *BabbageTransactionBody) RequiredSigners() []common.Blake2b224 {
332-
return b.TxRequiredSigners[:]
332+
return b.TxRequiredSigners.Items()
333333
}
334334

335335
func (b *BabbageTransactionBody) ScriptDataHash() *common.Blake2b256 {
@@ -338,7 +338,7 @@ func (b *BabbageTransactionBody) ScriptDataHash() *common.Blake2b256 {
338338

339339
func (b *BabbageTransactionBody) ReferenceInputs() []common.TransactionInput {
340340
ret := []common.TransactionInput{}
341-
for _, input := range b.TxReferenceInputs {
341+
for _, input := range b.TxReferenceInputs.Items() {
342342
ret = append(ret, &input)
343343
}
344344
return ret

ledger/conway/conway.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,12 @@ type ConwayTransactionBody struct {
350350
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
351351
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
352352
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
353-
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
354-
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
353+
TxCollateral cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"13,keyasint,omitempty,omitzero"`
354+
TxRequiredSigners cbor.SetType[common.Blake2b224] `cbor:"14,keyasint,omitempty,omitzero"`
355355
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
356356
TxCollateralReturn *babbage.BabbageTransactionOutput `cbor:"16,keyasint,omitempty"`
357357
TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"`
358-
TxReferenceInputs []shelley.ShelleyTransactionInput `cbor:"18,keyasint,omitempty"`
358+
TxReferenceInputs cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"18,keyasint,omitempty,omitzero"`
359359
TxVotingProcedures common.VotingProcedures `cbor:"19,keyasint,omitempty"`
360360
TxProposalProcedures []common.ProposalProcedure `cbor:"20,keyasint,omitempty"`
361361
TxCurrentTreasuryValue int64 `cbor:"21,keyasint,omitempty"`
@@ -431,14 +431,14 @@ func (b *ConwayTransactionBody) AssetMint() *common.MultiAsset[common.MultiAsset
431431

432432
func (b *ConwayTransactionBody) Collateral() []common.TransactionInput {
433433
ret := []common.TransactionInput{}
434-
for _, collateral := range b.TxCollateral {
434+
for _, collateral := range b.TxCollateral.Items() {
435435
ret = append(ret, collateral)
436436
}
437437
return ret
438438
}
439439

440440
func (b *ConwayTransactionBody) RequiredSigners() []common.Blake2b224 {
441-
return b.TxRequiredSigners[:]
441+
return b.TxRequiredSigners.Items()
442442
}
443443

444444
func (b *ConwayTransactionBody) ScriptDataHash() *common.Blake2b256 {
@@ -447,7 +447,7 @@ func (b *ConwayTransactionBody) ScriptDataHash() *common.Blake2b256 {
447447

448448
func (b *ConwayTransactionBody) ReferenceInputs() []common.TransactionInput {
449449
ret := []common.TransactionInput{}
450-
for _, input := range b.TxReferenceInputs {
450+
for _, input := range b.TxReferenceInputs.Items() {
451451
ret = append(ret, &input)
452452
}
453453
return ret

ledger/conway/pparams_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,23 @@ func TestConwayTransactionBody_Utxorpc(t *testing.T) {
619619
OutputAddress: address,
620620
OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000},
621621
}
622-
txCollateral := []shelley.ShelleyTransactionInput{input}
622+
txCollateral := cbor.NewSetType[shelley.ShelleyTransactionInput](
623+
[]shelley.ShelleyTransactionInput{input},
624+
false,
625+
)
623626
txTotalCollateral := uint64(200)
624-
txReferenceInputs := []shelley.ShelleyTransactionInput{input}
627+
txReferenceInputs := cbor.NewSetType[shelley.ShelleyTransactionInput](
628+
[]shelley.ShelleyTransactionInput{input},
629+
false,
630+
)
625631
txAuxDataHash := &common.Blake2b256{0xde, 0xad, 0xbe, 0xef}
626632
txValidityIntervalStart := uint64(4000)
627633
var signer common.Blake2b224
628634
copy(signer[:], []byte{0xab, 0xcd, 0xef})
629-
txRequiredSigners := []common.Blake2b224{signer}
635+
txRequiredSigners := cbor.NewSetType[common.Blake2b224](
636+
[]common.Blake2b224{signer},
637+
false,
638+
)
630639
txScriptDataHash := &common.Blake2b256{0xba, 0xad, 0xf0, 0x0d}
631640
txMint := &common.MultiAsset[common.MultiAssetTypeMint]{}
632641

@@ -682,14 +691,23 @@ func TestConwayTransaction_Utxorpc(t *testing.T) {
682691
OutputAmount: mary.MaryTransactionOutputValue{Amount: 5000},
683692
}
684693

685-
txCollateral := []shelley.ShelleyTransactionInput{input}
694+
txCollateral := cbor.NewSetType[shelley.ShelleyTransactionInput](
695+
[]shelley.ShelleyTransactionInput{input},
696+
false,
697+
)
686698
txTotalCollateral := uint64(200)
687-
txReferenceInputs := []shelley.ShelleyTransactionInput{input}
699+
txReferenceInputs := cbor.NewSetType[shelley.ShelleyTransactionInput](
700+
[]shelley.ShelleyTransactionInput{input},
701+
false,
702+
)
688703
txAuxDataHash := &common.Blake2b256{0xde, 0xad, 0xbe, 0xef}
689704
txValidityIntervalStart := uint64(4000)
690705
var signer common.Blake2b224
691706
copy(signer[:], []byte{0xab, 0xcd, 0xef})
692-
txRequiredSigners := []common.Blake2b224{signer}
707+
txRequiredSigners := cbor.NewSetType[common.Blake2b224](
708+
[]common.Blake2b224{signer},
709+
false,
710+
)
693711
txScriptDataHash := &common.Blake2b256{0xba, 0xad, 0xf0, 0x0d}
694712
txMint := &common.MultiAsset[common.MultiAssetTypeMint]{}
695713

0 commit comments

Comments
 (0)