Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cbor/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,54 @@ type Set []any

// Map corresponds to CBOR tag 259 and is used to represent a map with key/value operations
type Map map[any]any

// SetType is a generic type for wrapping other types in an optional CBOR set tag
type SetType[T any] struct {
useTag bool
items []T
}

func NewSetType[T any](items []T, useTag bool) SetType[T] {
return SetType[T]{
items: items,
useTag: useTag,
}
}

func (t *SetType[T]) UnmarshalCBOR(data []byte) error {
// Check if the set is wrapped in a CBOR tag
// This is mostly needed so we can remember whether it was Set-wrapped for CBOR encoding
var tmpTag RawTag
t.useTag = false
if _, err := Decode(data, &tmpTag); err == nil {
if tmpTag.Number != CborTagSet {
return errors.New("unexpected tag type")
}
data = []byte(tmpTag.Content)
t.useTag = true
}
var tmpData []T
if _, err := Decode(data, &tmpData); err != nil {
return err
}
t.items = tmpData
return nil
}

func (t *SetType[T]) MarshalCBOR() ([]byte, error) {
tmpItems := make([]any, len(t.items))
for i, item := range t.items {
tmpItems[i] = item
}
var tmpData any = tmpItems
if t.useTag {
tmpData = Set(tmpItems)
}
return Encode(tmpData)
}

func (t *SetType[T]) Items() []T {
ret := make([]T, len(t.items))
copy(ret, t.items)
return ret
}
8 changes: 4 additions & 4 deletions ledger/alonzo/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ type AlonzoTransactionBody struct {
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
TxCollateral cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"13,keyasint,omitempty,omitzero"`
TxRequiredSigners cbor.SetType[common.Blake2b224] `cbor:"14,keyasint,omitempty,omitzero"`
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
}

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

func (b *AlonzoTransactionBody) Collateral() []common.TransactionInput {
ret := []common.TransactionInput{}
for _, collateral := range b.TxCollateral {
for _, collateral := range b.TxCollateral.Items() {
ret = append(ret, collateral)
}
return ret
}

func (b *AlonzoTransactionBody) RequiredSigners() []common.Blake2b224 {
return b.TxRequiredSigners[:]
return b.TxRequiredSigners.Items()
}

func (b *AlonzoTransactionBody) ScriptDataHash() *common.Blake2b256 {
Expand Down
49 changes: 32 additions & 17 deletions ledger/alonzo/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,9 +997,12 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) {
t.Run(
"insufficient collateral",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := alonzo.UtxoValidateInsufficientCollateral(
testTx,
testSlot,
Expand Down Expand Up @@ -1027,10 +1030,13 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) {
t.Run(
"sufficient collateral",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
},
false,
)
err := alonzo.UtxoValidateInsufficientCollateral(
testTx,
testSlot,
Expand Down Expand Up @@ -1087,10 +1093,13 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
t.Run(
"coin and assets",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
},
false,
)
err := alonzo.UtxoValidateCollateralContainsNonAda(
testTx,
testSlot,
Expand Down Expand Up @@ -1118,9 +1127,12 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
t.Run(
"coin only",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := alonzo.UtxoValidateCollateralContainsNonAda(
testTx,
testSlot,
Expand Down Expand Up @@ -1192,9 +1204,12 @@ func TestUtxoValidateNoCollateralInputs(t *testing.T) {
t.Run(
"collateral",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := alonzo.UtxoValidateNoCollateralInputs(
testTx,
testSlot,
Expand Down
12 changes: 6 additions & 6 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ type BabbageTransactionBody struct {
TxValidityIntervalStart uint64 `cbor:"8,keyasint,omitempty"`
TxMint *common.MultiAsset[common.MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
TxScriptDataHash *common.Blake2b256 `cbor:"11,keyasint,omitempty"`
TxCollateral []shelley.ShelleyTransactionInput `cbor:"13,keyasint,omitempty"`
TxRequiredSigners []common.Blake2b224 `cbor:"14,keyasint,omitempty"`
TxCollateral cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"13,keyasint,omitempty,omitzero"`
TxRequiredSigners cbor.SetType[common.Blake2b224] `cbor:"14,keyasint,omitempty,omitzero"`
NetworkId uint8 `cbor:"15,keyasint,omitempty"`
TxCollateralReturn *BabbageTransactionOutput `cbor:"16,keyasint,omitempty"`
TxTotalCollateral uint64 `cbor:"17,keyasint,omitempty"`
TxReferenceInputs []shelley.ShelleyTransactionInput `cbor:"18,keyasint,omitempty"`
TxReferenceInputs cbor.SetType[shelley.ShelleyTransactionInput] `cbor:"18,keyasint,omitempty,omitzero"`
}

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

func (b *BabbageTransactionBody) Collateral() []common.TransactionInput {
ret := []common.TransactionInput{}
for _, collateral := range b.TxCollateral {
for _, collateral := range b.TxCollateral.Items() {
ret = append(ret, collateral)
}
return ret
}

func (b *BabbageTransactionBody) RequiredSigners() []common.Blake2b224 {
return b.TxRequiredSigners[:]
return b.TxRequiredSigners.Items()
}

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

func (b *BabbageTransactionBody) ReferenceInputs() []common.TransactionInput {
ret := []common.TransactionInput{}
for _, input := range b.TxReferenceInputs {
for _, input := range b.TxReferenceInputs.Items() {
ret = append(ret, &input)
}
return ret
Expand Down
98 changes: 64 additions & 34 deletions ledger/babbage/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,12 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) {
t.Run(
"insufficient collateral",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := babbage.UtxoValidateInsufficientCollateral(
testTx,
testSlot,
Expand Down Expand Up @@ -1028,10 +1031,13 @@ func TestUtxoValidateInsufficientCollateral(t *testing.T) {
t.Run(
"sufficient collateral",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
},
false,
)
err := babbage.UtxoValidateInsufficientCollateral(
testTx,
testSlot,
Expand Down Expand Up @@ -1110,10 +1116,13 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
t.Run(
"coin and assets",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
},
false,
)
err := babbage.UtxoValidateCollateralContainsNonAda(
testTx,
testSlot,
Expand Down Expand Up @@ -1141,9 +1150,12 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
t.Run(
"coin only",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := babbage.UtxoValidateCollateralContainsNonAda(
testTx,
testSlot,
Expand All @@ -1162,10 +1174,13 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
t.Run(
"coin and assets with return",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
},
false,
)
testTx.Body.TxCollateralReturn = &babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralAmount,
Expand All @@ -1190,9 +1205,12 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
t.Run(
"coin and zero assets with return",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 2),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 2),
},
false,
)
testTx.Body.TxCollateralReturn = &babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralAmount,
Expand Down Expand Up @@ -1269,9 +1287,12 @@ func TestUtxoValidateNoCollateralInputs(t *testing.T) {
t.Run(
"collateral",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := babbage.UtxoValidateNoCollateralInputs(
testTx,
testSlot,
Expand Down Expand Up @@ -1374,9 +1395,12 @@ func TestUtxoValidateCollateralEqBalance(t *testing.T) {
testTx := &babbage.BabbageTransaction{
Body: babbage.BabbageTransactionBody{
TxTotalCollateral: testTotalCollateral,
TxCollateral: []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
TxCollateral: cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
),
},
}
testLedgerState := test.MockLedgerState{
Expand Down Expand Up @@ -1492,10 +1516,13 @@ func TestUtxoValidateTooManyCollateralInputs(t *testing.T) {
t.Run(
"too many collateral inputs",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
shelley.NewShelleyTransactionInput(testInputTxId, 1),
},
false,
)
err := babbage.UtxoValidateTooManyCollateralInputs(
testTx,
testSlot,
Expand Down Expand Up @@ -1523,9 +1550,12 @@ func TestUtxoValidateTooManyCollateralInputs(t *testing.T) {
t.Run(
"single collateral input",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
}
testTx.Body.TxCollateral = cbor.NewSetType[shelley.ShelleyTransactionInput](
[]shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 0),
},
false,
)
err := babbage.UtxoValidateTooManyCollateralInputs(
testTx,
testSlot,
Expand Down
Loading
Loading