Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision > Struct Template Reference

#include <resource_limits_private.hpp>

Public Member Functions

 exponential_decay_accumulator ()
 
uint64_t value_ex_at (uint32_t ordinal, uint32_t window_size) const
 
uint64_t value_at (uint32_t ordinal, uint32_t window_size) const
 
void add (uint64_t units, uint32_t ordinal, uint32_t window_size)
 

Public Attributes

uint32_t last_ordinal
 The ordinal of the last period which has contributed to the accumulator.
 
uint64_t value_ex
 The current accumulated value pre-multiplied by Precision.
 

Static Public Attributes

static constexpr uint64_t max_raw_value = std::numeric_limits<uint64_t>::max() / Precision
 

Detailed Description

template<uint64_t Precision = config::rate_limiting_precision>
struct sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >

This class accumulates a value that decays over quantums based on inputs The decay is linear between updates and exponential if the set of inputs has no gaps

The value stored is Precision times the sum of the inputs.

Definition at line 131 of file resource_limits_private.hpp.

Constructor & Destructor Documentation

◆ exponential_decay_accumulator()

template<uint64_t Precision = config::rate_limiting_precision>
sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::exponential_decay_accumulator ( )
inline

Definition at line 136 of file resource_limits_private.hpp.

137 : last_ordinal(0)
138 , value_ex(0)
139 {
140 }
uint64_t value_ex
The current accumulated value pre-multiplied by Precision.
uint32_t last_ordinal
The ordinal of the last period which has contributed to the accumulator.

Member Function Documentation

◆ add()

template<uint64_t Precision = config::rate_limiting_precision>
void sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::add ( uint64_t units,
uint32_t ordinal,
uint32_t window_size )
inline

Definition at line 173 of file resource_limits_private.hpp.

174 {
175 // check for some numerical limits before doing any state mutations
176 SYS_ASSERT(units <= max_raw_value, rate_limiting_state_inconsistent, "Usage exceeds maximum value representable after extending for precision");
177
178 uint128_t units_ex = (uint128_t)units * Precision;
179 if (last_ordinal < ordinal) {
180 value_ex = value_ex_at(ordinal, window_size);
181 last_ordinal = ordinal;
182 }
183
184 // saturate the value
185 uint128_t new_value_ex = std::min<uint128_t>(units_ex + (uint128_t)value_ex, std::numeric_limits<uint64_t>::max());
186 value_ex = downgrade_cast<uint64_t>(new_value_ex);
187 }
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
Definition exceptions.hpp:7
constexpr auto downgrade_cast(GreaterIntType val) -> std::enable_if_t< is_valid_downgrade_cast< LesserIntType, GreaterIntType > &&std::is_signed< LesserIntType >::value==std::is_signed< GreaterIntType >::value, LesserIntType >
unsigned __int128 uint128_t
Definition types.hpp:242
uint64_t value_ex_at(uint32_t ordinal, uint32_t window_size) const
Here is the call graph for this function:

◆ value_at()

template<uint64_t Precision = config::rate_limiting_precision>
uint64_t sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::value_at ( uint32_t ordinal,
uint32_t window_size ) const
inline

return the value at a current or future ordinal

Definition at line 169 of file resource_limits_private.hpp.

169 {
170 return integer_divide_ceil(value_ex_at(ordinal, window_size), Precision);
171 }
constexpr UnsignedIntType integer_divide_ceil(UnsignedIntType num, UnsignedIntType den)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ value_ex_at()

template<uint64_t Precision = config::rate_limiting_precision>
uint64_t sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::value_ex_at ( uint32_t ordinal,
uint32_t window_size ) const
inline

return the extended value at a current or future ordinal

Definition at line 148 of file resource_limits_private.hpp.

148 {
149 if( last_ordinal < ordinal ) {
150 if( (uint64_t)last_ordinal + window_size > (uint64_t)ordinal ) {
151 const auto delta = ordinal - last_ordinal; // clearly 0 < delta < window_size
152 const auto decay = make_ratio(
153 (uint128_t)window_size - delta,
154 (uint128_t)window_size
155 );
156
158 } else {
159 return 0;
160 }
161 } else {
162 return value_ex;
163 }
164 }
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ last_ordinal

template<uint64_t Precision = config::rate_limiting_precision>
uint32_t sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::last_ordinal

Definition at line 142 of file resource_limits_private.hpp.

◆ max_raw_value

template<uint64_t Precision = config::rate_limiting_precision>
uint64_t sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::max_raw_value = std::numeric_limits<uint64_t>::max() / Precision
staticconstexpr

Definition at line 134 of file resource_limits_private.hpp.

◆ value_ex

template<uint64_t Precision = config::rate_limiting_precision>
uint64_t sysio::chain::resource_limits::impl::exponential_decay_accumulator< Precision >::value_ex

Definition at line 143 of file resource_limits_private.hpp.


The documentation for this struct was generated from the following file: