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
12 changes: 10 additions & 2 deletions ledger/babbage/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,19 @@ func UtxoValidateCollateralEqBalance(
}
collBalance += utxo.Output.Amount()
}
// Subtract collateral return amount

// Skip validation if no valid collateral UTxOs were found
// This avoids subtracting from zero and prevents uint underflow
if collBalance == 0 {
return nil
}

// Subtract collateral return amount with underflow protection
collReturn := tx.CollateralReturn()
if collReturn != nil {
if collReturn != nil && collBalance >= collReturn.Amount() {
collBalance -= collReturn.Amount()
}

if totalCollateral == collBalance {
return nil
}
Expand Down
21 changes: 21 additions & 0 deletions ledger/babbage/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,27 @@ func TestUtxoValidateCollateralEqBalance(t *testing.T) {
}
},
)
// no valid collateral UTxO, should skip and not underflow
t.Run("no valid collateral UTxO, should skip and not underflow", func(t *testing.T) {
// Ledger state with NO matching UTxO
missingUtxoLedgerState := test.MockLedgerState{
MockUtxos: []common.Utxo{}, // empty
}
testTx.Body.TxCollateralReturn = &babbage.BabbageTransactionOutput{
OutputAmount: mary.MaryTransactionOutputValue{
Amount: testCollateralReturnAmountBad,
},
}
err := babbage.UtxoValidateCollateralEqBalance(
testTx,
testSlot,
missingUtxoLedgerState,
testProtocolParams,
)
if err != nil {
t.Errorf("Should skip collateral return validation if collBalance == 0. Got error: %v", err)
}
})
}

func TestUtxoValidateTooManyCollateralInputs(t *testing.T) {
Expand Down
Loading