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 1 commit
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
15 changes: 13 additions & 2 deletions ledger/babbage/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,21 @@ 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 {
collBalance -= collReturn.Amount()
returnAmt := collReturn.Amount()
if collBalance < returnAmt {
return errors.New("collateral return amount exceeds collateral input balance")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to return the specific errors defined in the ledger for each era, and generally only at the end of the function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the changes and please review it once

}
collBalance -= returnAmt
}
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