Skip to content

feat(vm,consensus): harden resource calculations(TIP-833)#6721

Open
halibobo1205 wants to merge 4 commits intotronprotocol:developfrom
halibobo1205:feature/tip-833-harden-resource-calculation
Open

feat(vm,consensus): harden resource calculations(TIP-833)#6721
halibobo1205 wants to merge 4 commits intotronprotocol:developfrom
halibobo1205:feature/tip-833-harden-resource-calculation

Conversation

@halibobo1205
Copy link
Copy Markdown
Collaborator

Summary

Implements TIP-833 to harden resource calculations across native and VM paths:

  • Algorithm: BigInteger-based intermediate products in ResourceProcessor (increase/increaseV2/unDelegateIncreaseV2/getNewWindowSize/getUsage) and RepositoryImpl (increase/getUsage); longValueExact() performs an explicit range check before writing back to long, replacing silent overflow wrap with a deterministic ArithmeticException that reverts the transaction.
  • Resource limits: calculateGlobalEnergyLimit{,V2} and calculateGlobalNetLimit{,V2} (and the VM-side calculateGlobalEnergyLimit) drop (long)((double) weight * limit / totalWeight) for two BigInteger helpers — calculateGlobalLimitV1 (preserves "floor frozeBalance/TRX_PRECISION first") and calculateGlobalLimitV2 (single integer truncation, fractional weight preserved so frozeBalance < TRX_PRECISION still yields a proportional non-zero result).
  • Adaptive limit: EnergyProcessor.updateAdaptiveTotalEnergyLimit and RepositoryImpl.usageToBalance (the (long)((double) usage * weight/limit * TRX_PRECISION) reverse-calculation) move to BigInteger under the same gate.
  • Governance: New proposal ALLOW_HARDEN_RESOURCE_CALCULATION (code 97), requires fork VERSION_4_8_2, one-time activation; behaviour pre-activation is byte-identical to current mainnet.

Reference

TIP-833: Harden ResourceProcessor Resource Window Calculations

public long calculateGlobalEnergyLimit(AccountCapsule accountCapsule) {
long frozeBalance = accountCapsule.getAllFrozenBalanceForEnergy();
if (frozeBalance < 1_000_000L) {
if (frozeBalance < precision) {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

TRX_PRECISION

Copy link
Copy Markdown
Collaborator

@waynercheung waynercheung left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Collaborator

@yanghang8612 yanghang8612 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Collaborator

@CodeNinjaEvan CodeNinjaEvan left a comment

Choose a reason for hiding this comment

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

LGTM

@halibobo1205 halibobo1205 force-pushed the feature/tip-833-harden-resource-calculation branch from 2e97653 to 82d1639 Compare May 7, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:consensus topic:vm VM, smart contract

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants