Skip to content

feat(actuator,consensus): harden exchange calculations (TIP-836)#6710

Open
halibobo1205 wants to merge 6 commits intotronprotocol:developfrom
halibobo1205:feat/tip-836-harden-exchange-transaction
Open

feat(actuator,consensus): harden exchange calculations (TIP-836)#6710
halibobo1205 wants to merge 6 commits intotronprotocol:developfrom
halibobo1205:feat/tip-836-harden-exchange-transaction

Conversation

@halibobo1205
Copy link
Copy Markdown
Collaborator

Summary

Implements TIP-836 to harden exchange transaction calculations:

  • Algorithm: New SafeExchangeProcessor replaces double arithmetic in the Bancor formula with BigDecimal, eliminating silent Infinity/NaN from division-by-zero.
  • Invariants: ExchangeCapsule.transaction() rejects negative post-trade pool balances via StrictMathWrapper.addExact/subtractExact.
  • Actuators: All four exchange actuators (Create/Inject/Transaction/Withdraw) inherit AbstractExchangeActuator for unified overflow-checked arithmetic gated by the new proposal.
  • Governance: New proposal ALLOW_HARDEN_EXCHANGE_CALCULATION (code 98), requires fork VERSION_4_8_2, one-time activation.

Reference

TIP-836: Harden Exchange Transaction Calculations

…IP-836)

Add SafeExchangeProcessor that replaces double arithmetic with
BigDecimal in the Bancor-formula calculation:

1. BigDecimal divide throws ArithmeticException on zero divisor
2. ExchangeCapsule.transaction() rejects negative post-trade balances
3. longValueExact() guards against BigDecimal-to-long saturation

Introduce AbstractExchangeActuator that overrides addExact/subtractExact
to route through allowHardenExchangeCalculation() flag, applied to all
four exchange actuators (Create, Inject, Transaction, Withdraw) for
consistent overflow detection.

ExchangeWithdrawActuator.validate() also gains a pure BigDecimal
precision-loss check when the proposal is active (previously used
double comparison).

Pre-activation behavior is byte-for-byte identical to legacy code.
Activation gated by ALLOW_HARDEN_EXCHANGE_CALCULATION (proposal 98).
@halibobo1205 halibobo1205 force-pushed the feat/tip-836-harden-exchange-transaction branch from 6c92146 to 03ecf09 Compare April 28, 2026 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants