(params bmetypes.Params, cr int64)
| 946 | } |
| 947 | |
| 948 | func calculateBlocksDiff(params bmetypes.Params, cr int64) int64 { |
| 949 | warnThreshold := int64(params.CircuitBreakerWarnThreshold) |
| 950 | |
| 951 | if cr >= warnThreshold { |
| 952 | return params.MinEpochBlocks |
| 953 | } |
| 954 | |
| 955 | if params.EpochBlocksBackoffPercent == 0 { |
| 956 | return params.MinEpochBlocks |
| 957 | } |
| 958 | |
| 959 | // The number of steps CR has dropped below warn threshold. |
| 960 | // Each step = 10 BPS = 0.001 in the stored format (10000 = 1.0). |
| 961 | steps := uint64((warnThreshold - cr) / 10) |
| 962 | |
| 963 | if steps == 0 { |
| 964 | return params.MinEpochBlocks |
| 965 | } |
| 966 | |
| 967 | // MinEpochBlocks * (1 + EpochBlocksBackoff/100) ^ steps. |
| 968 | // EpochBlocksBackoff is in percent (e.g., 10 = 10%). |
| 969 | // Each step grows the backoff by EpochBlocksBackoff% of the current value. |
| 970 | // Uses sdkmath.LegacyDec for deterministic arbitrary-precision arithmetic. |
| 971 | base := sdkmath.LegacyNewDec(100 + int64(params.EpochBlocksBackoffPercent)).Quo(sdkmath.LegacyNewDec(100)) |
| 972 | |
| 973 | // Cap at ~1 day of blocks (assuming 6s per block) to prevent excessively long epochs. |
| 974 | // Cap the Dec before TruncateInt64 to avoid int64 overflow with aggressive params. |
| 975 | maxEpochBlocks := sdkmath.LegacyNewDec(14400) |
| 976 | |
| 977 | resDec := sdkmath.LegacyNewDec(params.MinEpochBlocks).Mul(base.Power(steps)) |
| 978 | if resDec.GT(maxEpochBlocks) { |
| 979 | resDec = maxEpochBlocks |
| 980 | } |
| 981 | |
| 982 | res := resDec.TruncateInt64() |
| 983 | if res < params.MinEpochBlocks { |
| 984 | return params.MinEpochBlocks |
| 985 | } |
| 986 | |
| 987 | return res |
| 988 | } |
no outgoing calls