Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better comparison for MultiAsset #945

Merged
merged 1 commit into from
Mar 10, 2025
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
6 changes: 2 additions & 4 deletions ledger/babbage/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,8 @@ func UtxoValidateCollateralContainsNonAda(
collReturn := tx.CollateralReturn()
if collReturn != nil {
collReturnAssets := collReturn.Assets()
if collReturnAssets != nil {
if collReturnAssets.Compare(&totalAssets) {
return nil
}
if (&totalAssets).Compare(collReturnAssets) {
return nil
}
}
return alonzo.CollateralContainsNonAdaError{
Expand Down
42 changes: 42 additions & 0 deletions ledger/babbage/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,13 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
},
},
)
tmpZeroMultiAsset := common.NewMultiAsset[common.MultiAssetTypeOutput](
map[common.Blake2b224]map[cbor.ByteString]uint64{
common.Blake2b224Hash([]byte("abcd")): map[cbor.ByteString]uint64{
cbor.NewByteString([]byte("efgh")): 0,
},
},
)
testLedgerState := test.MockLedgerState{
MockUtxos: []common.Utxo{
{
Expand All @@ -1135,6 +1142,15 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
},
},
},
{
Id: shelley.NewShelleyTransactionInput(testInputTxId, 2),
Output: babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralAmount,
Assets: &tmpZeroMultiAsset,
},
},
},
},
}
testSlot := uint64(0)
Expand Down Expand Up @@ -1219,6 +1235,32 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
}
},
)
// Coin and zero assets with return
t.Run(
"coin and zero assets with return",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 2),
}
testTx.Body.TxCollateralReturn = &babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralAmount,
},
}
err := babbage.UtxoValidateCollateralContainsNonAda(
testTx,
testSlot,
testLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf(
"UtxoValidateCollateralContainsNonAda should succeed when collateral with only coin is provided\n got error: %v",
err,
)
}
},
)
}

func TestUtxoValidateNoCollateralInputs(t *testing.T) {
Expand Down
33 changes: 27 additions & 6 deletions ledger/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,20 @@ func (m *MultiAsset[T]) Add(assets *MultiAsset[T]) {
}

func (m *MultiAsset[T]) Compare(assets *MultiAsset[T]) bool {
if assets == nil {
return false
}
if len(assets.data) != len(m.data) {
// Normalize data for easier comparison
tmpData := m.normalize()
otherData := assets.normalize()
// Compare policy counts
if len(otherData) != len(tmpData) {
return false
}
for policy, assets := range assets.data {
if len(assets) != len(m.data[policy]) {
for policy, assets := range otherData {
// Compare asset counts for policy
if len(assets) != len(tmpData[policy]) {
return false
}
for asset, amount := range assets {
// Compare quantity of specific asset
if amount != m.Asset(policy, asset.Bytes()) {
return false
}
Expand All @@ -248,6 +251,24 @@ func (m *MultiAsset[T]) Compare(assets *MultiAsset[T]) bool {
return true
}

func (m *MultiAsset[T]) normalize() map[Blake2b224]map[cbor.ByteString]T {
ret := map[Blake2b224]map[cbor.ByteString]T{}
if m == nil || m.data == nil {
return ret
}
for policy, assets := range m.data {
for asset, amount := range assets {
if amount != 0 {
if _, ok := ret[policy]; !ok {
ret[policy] = make(map[cbor.ByteString]T)
}
ret[policy][asset] = amount
}
}
}
return ret
}

type AssetFingerprint struct {
policyId []byte
assetName []byte
Expand Down
78 changes: 78 additions & 0 deletions ledger/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,84 @@ func TestMultiAssetJson(t *testing.T) {
}
}

func TestMultiAssetCompare(t *testing.T) {
testDefs := []struct {
asset1 *MultiAsset[MultiAssetTypeOutput]
asset2 *MultiAsset[MultiAssetTypeOutput]
expectedResult bool
}{
{
asset1: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 123,
},
},
},
asset2: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 123,
},
},
},
expectedResult: true,
},
{
asset1: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 123,
},
},
},
asset2: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 124,
},
},
},
expectedResult: false,
},
{
asset1: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 0,
},
},
},
asset2: nil,
expectedResult: true,
},
{
asset1: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 123,
},
},
},
asset2: &MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224([]byte("abcd")): {
cbor.NewByteString([]byte("cdef")): 123,
cbor.NewByteString([]byte("efgh")): 123,
},
},
},
expectedResult: false,
},
}
for _, testDef := range testDefs {
tmpResult := testDef.asset1.Compare(testDef.asset2)
if tmpResult != testDef.expectedResult {
t.Errorf("did not get expected result: got %v, wanted %v", tmpResult, testDef.expectedResult)
}
}
}

// Test the MarshalJSON method for Blake2b224 to ensure it properly converts to JSON.
func TestBlake2b224_MarshalJSON(t *testing.T) {
// Example data to represent Blake2b224 hash
Expand Down
6 changes: 2 additions & 4 deletions ledger/conway/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,8 @@ func UtxoValidateCollateralContainsNonAda(
collReturn := tx.CollateralReturn()
if collReturn != nil {
collReturnAssets := collReturn.Assets()
if collReturnAssets != nil {
if collReturnAssets.Compare(&totalAssets) {
return nil
}
if (&totalAssets).Compare(collReturnAssets) {
return nil
}
}
return alonzo.CollateralContainsNonAdaError{
Expand Down
42 changes: 42 additions & 0 deletions ledger/conway/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,13 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
},
},
)
tmpZeroMultiAsset := common.NewMultiAsset[common.MultiAssetTypeOutput](
map[common.Blake2b224]map[cbor.ByteString]uint64{
common.Blake2b224Hash([]byte("abcd")): map[cbor.ByteString]uint64{
cbor.NewByteString([]byte("efgh")): 0,
},
},
)
testLedgerState := test.MockLedgerState{
MockUtxos: []common.Utxo{
{
Expand All @@ -1145,6 +1152,15 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
},
},
},
{
Id: shelley.NewShelleyTransactionInput(testInputTxId, 2),
Output: babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralAmount,
Assets: &tmpZeroMultiAsset,
},
},
},
},
}
testSlot := uint64(0)
Expand Down Expand Up @@ -1229,6 +1245,32 @@ func TestUtxoValidateCollateralContainsNonAda(t *testing.T) {
}
},
)
// Coin and zero assets with return
t.Run(
"coin and zero assets with return",
func(t *testing.T) {
testTx.Body.TxCollateral = []shelley.ShelleyTransactionInput{
shelley.NewShelleyTransactionInput(testInputTxId, 2),
}
testTx.Body.TxCollateralReturn = &babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralAmount,
},
}
err := conway.UtxoValidateCollateralContainsNonAda(
testTx,
testSlot,
testLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf(
"UtxoValidateCollateralContainsNonAda should succeed when collateral with only coin is provided\n got error: %v",
err,
)
}
},
)
}

func TestUtxoValidateNoCollateralInputs(t *testing.T) {
Expand Down