Skip to content

fix(pm): compute avg_resolution_time running mean in uint128 - #137

Merged
On1x merged 1 commit into
VIZ-Blockchain:pmfrom
web3blind:fix/pm-avg-resolution-time-uint128
Aug 6, 2026
Merged

fix(pm): compute avg_resolution_time running mean in uint128#137
On1x merged 1 commit into
VIZ-Blockchain:pmfrom
web3blind:fix/pm-avg-resolution-time-uint128

Conversation

@web3blind

Copy link
Copy Markdown

Summary

Small follow-up to P5 (oracle-metrics) on pm (#124): the running-mean formula for pm_oracle_object::avg_resolution_time accumulates in uint64, but avg_resolution_time (uint32) * (n-1) + rt can reach the uint64 boundary once n approaches the uint32 max — the product of two uint32 values sits within a hair of 2^64, and adding rt can tip it over.

Change

libraries/chain/pm_evaluator.cpp, in pm_resolve_market_evaluator::do_apply:

const uint64_t sum =
    ((fc::uint128_t((uint64_t)ora.avg_resolution_time) * (uint64_t)(n - 1)
      + fc::uint128_t(rt)) / n).lo;
ora.avg_resolution_time = (uint32_t)sum;

fc::uint128_t is already used elsewhere in the same file (time-penalty math), so no new includes.

Verification

  • Formula equivalence checked against the original integer math on boundary cases (avg=0..UINT32_MAX, n=1..4e9, rt=0..UINT32_MAX) — matches on all cases, and confirms the original uint64 path lands within 1 bit of overflow on the extreme (bit_length()==64).
  • git diff --check clean; single file, 6 insertions / 2 deletions.
  • Full C++ build not run here (no local Boost toolchain); change is a pure arithmetic widening with identical semantics.

avg_resolution_time (uint32) * (n-1) + rt can reach the uint64 boundary
once n approaches the uint32 max (product of two uint32 values is within
a hair of 2^64, and adding rt can tip it over). Accumulate the running
mean in fc::uint128_t to keep the counter exact.

Mathematically equivalent to the previous integer formula; verified with
a quick script across normal and boundary cases (matches on all).
@On1x
On1x merged commit 104090f into VIZ-Blockchain:pm Aug 6, 2026
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.

2 participants