Skip to content

fix(lottery): reject buy amount overflow - #1343

Open
vipwzw wants to merge 1 commit into
masterfrom
fix-lottery-amount-overflow
Open

vipwzw wants to merge 1 commit into
masterfrom
fix-lottery-amount-overflow

Conversation

@vipwzw

@vipwzw vipwzw commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

问题

LotteryBuy 只检查 amount > 0,随后用 amount*coinPrecision 做转账/冻结:

  • 取 Amount=184467440738 时乘积回绕成 90448384(0.90448384 枚),但 Fund/BuyAmount 记成 184467440738;
  • 开奖时 factor = precision*Fund/2/totalFund 再次 int64 溢出,factor=0,随后 0 金额转账返回 ErrAmount,本轮无法开奖,资金锁到关奖。

实测(v1.76.3 执行器):溢出买入成功、开奖失败、关奖按回绕额退款。属资金锁定/DoS,同时暴露出资金会计被绕过。

修复

  • 新增 ForkLotteryFixOverflow:LotteryBuy 拒绝 amount*precision 溢出及 amount >= MaxCoin。
  • 默认高度 0 生效;老链可配置为未来高度保持旧行为。
  • 新增回归测试:溢出买入拒绝、正常买入/开奖、正常关奖。

验证

  • go build ./plugin/dapp/lottery/... 通过
  • go test ./plugin/dapp/lottery/executor/ -count=1 通过

LotteryBuy only checked amount > 0. amount*coinPrecision is then used for the
transfer and freeze and can wrap past int64: e.g. amount=184467440738 pays
0.90448384 coin while Fund/BuyAmount record 184467440738. The draw later
computes factor from precision*Fund, which overflows as well and leaves
factor=0, so the round can never be drawn and the funds stay locked until
close.

Reject amounts that would overflow amount*precision or exceed MaxCoin behind
ForkLotteryFixOverflow (default height 0). Add regression tests for the
rejection, the normal buy/draw path and the close path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant