congestionPenaltyRate function
ewma::penalty_fee's firing test, as a per-unit RATE: penalty_rate when
the transaction's gas price is a high outlier against the market's
pre-trade EWMA, else zero. It is a rate and not an amount because the
surcharge is rate ยท quantity, and a budget search has to reprice it at
every candidate quantity. gasPrice is the raw reference gas price (not
1e9-scaled), as tx_context::gas_price reports it.
Implementation
BigInt congestionPenaltyRate(
CongestionPolicy policy, CongestionState state, BigInt gasPrice) {
_assertUint(policy.penaltyRate, 'penaltyRate', maximum: FLOAT_SCALING);
_assertUint(policy.zScoreThreshold, 'zScoreThreshold');
_assertUint(state.mean, 'mean');
_assertUint(state.variance, 'variance');
_assertUint(gasPrice, 'gasPrice', maximum: U64_MAX ~/ FLOAT_SCALING);
if (!policy.enabled || state.variance == BigInt.zero) return BigInt.zero;
final scaled = gasPrice * FLOAT_SCALING;
if (scaled <= state.mean) return BigInt.zero;
final zScore = _divDown(scaled - state.mean, sqrtDown(state.variance));
if (zScore <= policy.zScoreThreshold) return BigInt.zero;
return policy.penaltyRate;
}