Hold up to max_voters voters in a vote cache entry (#5290) * Rewrite the saturating arithmetic helpers The old add_sat and sub_sat checked against the limits with expressions that overflow for a negative signed operand, never clamped a sum at the minimum, accepted any type with numeric_limits, floating point and bool included, and could not run in constant expressions. add_sat, sub_sat, inc_sat and dec_sat now give the exact result clamped to the range of the operand type. They take the built-in integer types other than bool and the character types, and the fixed-width unsigned multiprecision integers the node uses, recognised through numeric_limits so the header needs no Boost include. Signed built-ins compute in the unsigned type and detect overflow from the sign bits, so no expression overflows. All four are constexpr and noexcept, and the operands of the binary ones share one type. The tests move from the numbers suite to their own saturate suite, with exhaustive 8-bit checks and compile-time checks of the accepted types. * Check the position a vote result is recorded at vote_results::set wrote the code for a position before anything compared the position with the hashes of the vote. The bound check of the vector holding the codes is compiled out of release builds, where a position past the last hash went into unused capacity and set a routed bit that entries () never visits, and one past the capacity wrote outside the buffer before the bitset threw. The router only passes positions of its own loops over the hashes, so the check holds the setter to its contract for any other caller. * Add a switch to turn the vote cache off The vote cache had no setting of its own. hinted_scheduler.enable only stops hinted elections: the router keeps filling the cache, and new elections and the bounded backlog keep reading it. A max_size of zero left the cache empty only because every insert was evicted at once. vote_cache.enable now keeps every vote out of the cache. * Hold up to max_voters voters in a vote cache entry A new voter was added first and the lightest voter dropped as soon as the list reached max_voters, so an entry never held more than max_voters - 1 voters, and none at a limit of one. The comparison against the lightest voter of a full entry was only reached at a limit of zero, where the list was empty and its assert stopped the node on the first cached vote. A full entry now turns away a voter lighter than all it holds, and otherwise drops its lightest voter, the earliest among equals, before adding the new one. An entry holds up to max_voters voters, never grows past the room it reserved for them, and keeps none at a limit of zero. A voter turned away no longer counts as a tally change, so it neither recounts the tally nor keeps the entry from aging out. * Saturate the tallies of a vote cache entry instead of wrapping Each cached weight is the one read when its vote arrived, so delegation moving between two votes counts the moved weight twice and the sum can pass the largest uint128 value. A wrapped tally comes out small and can fall below the threshold the hinted scheduler looks for. Both tallies now stop at the largest value instead.
__________
Type: commit
Hash: 921e7815879c1ab56c7f4737baa01bc7415ef0d6
Created: yesterday
__________
gr0v1ty/nano-node:921e7815879c1ab56c7f4737baa01bc7415ef0d6
| Test Case | Status | Duration | Median Deviation | Log | Graph |
|---|---|---|---|---|---|
| 5n4pr_conf_10k_bintree | PASS | 110 s | 1.0 s | GitHub job logs | Show |
| 5n4pr_conf_10k_change | PASS | 117 s | -11.0 s | GitHub job logs | Show |
| 5n4pr_conf_change_dependant | PASS | 106 s | -8.0 s | GitHub job logs | - |
| 5n4pr_conf_change_independant | PASS | 109 s | 0.0 s | GitHub job logs | - |
| 5n4pr_conf_send_dependant | PASS | 100 s | -12.0 s | GitHub job logs | - |
| 5n4pr_conf_send_independant | PASS | 122 s | 8.0 s | GitHub job logs | - |
| 5n4pr_rocks_10k_bintree | PASS | 98 s | -13.0 s | GitHub job logs | - |
| 5n4pr_rocks_10k_change | PASS | 170 s | 14.0 s | GitHub job logs | - |