Skip to content

feat(ledger/babbage): Integer underflow issue in babbage.UtxoValidateCollateralEqBalance #1024

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

Merged
merged 2 commits into from
Jun 4, 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
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