10namespace sysio {
namespace chain {
namespace resource_limits {
20 SYS_ASSERT(
r.numerator ==
T(0) || std::numeric_limits<T>::max() /
r.numerator >=
value, rate_limiting_state_inconsistent,
"Usage exceeds maximum value representable after extending for precision");
21 return (
value *
r.numerator) /
r.denominator;
24 template<
typename Un
signedIntType>
26 return (num / den) + ((num % den) > 0 ? 1 : 0);
30 template<
typename LesserIntType,
typename GreaterIntType>
32 std::is_integral<LesserIntType>::value &&
33 std::is_integral<GreaterIntType>::value &&
34 (std::numeric_limits<LesserIntType>::max() <= std::numeric_limits<GreaterIntType>::max());
39 template<
typename LesserIntType,
typename GreaterIntType>
41 std::enable_if_t<is_valid_downgrade_cast<LesserIntType,GreaterIntType> && std::is_signed<LesserIntType>::value == std::is_signed<GreaterIntType>::value, LesserIntType>
43 const GreaterIntType max = std::numeric_limits<LesserIntType>::max();
44 const GreaterIntType
min = std::numeric_limits<LesserIntType>::min();
45 SYS_ASSERT( val >=
min && val <= max, rate_limiting_state_inconsistent,
"Casting a higher bit integer value ${v} to a lower bit integer value which cannot contain the value, valid range is [${min}, ${max}]", (
"v", val)(
"min",
min)(
"max",max) );
46 return LesserIntType(val);
52 template<
typename LesserIntType,
typename GreaterIntType>
54 std::enable_if_t<is_valid_downgrade_cast<LesserIntType,GreaterIntType> && std::is_signed<LesserIntType>::value != std::is_signed<GreaterIntType>::value, LesserIntType>
56 const GreaterIntType max = std::numeric_limits<LesserIntType>::max();
57 const GreaterIntType
min = 0;
58 SYS_ASSERT( val >=
min && val <= max, rate_limiting_state_inconsistent,
"Casting a higher bit integer value ${v} to a lower bit integer value which cannot contain the value, valid range is [${min}, ${max}]", (
"v", val)(
"min",
min)(
"max",max) );
59 return LesserIntType(val);
68 template<u
int64_t Precision = config::rate_limiting_precision>
71 static_assert( Precision > 0,
"Precision must be positive" );
95 SYS_ASSERT(units <=
max_raw_value, rate_limiting_state_inconsistent,
"Usage exceeds maximum value representable after extending for precision");
96 SYS_ASSERT(std::numeric_limits<
decltype(
consumed)>::max() -
consumed >= units, rate_limiting_state_inconsistent,
"Overflow in tracked usage when adding usage!");
99 SYS_ASSERT(std::numeric_limits<
decltype(
value_ex)>::max() -
value_ex >= value_ex_contrib, rate_limiting_state_inconsistent,
"Overflow in accumulated value when adding usage!");
102 SYS_ASSERT( ordinal >
last_ordinal, resource_limit_exception,
"new ordinal cannot be less than the previous ordinal" );
130 template<u
int64_t Precision = config::rate_limiting_precision>
133 static_assert( Precision > 0,
"Precision must be positive" );
176 SYS_ASSERT(units <=
max_raw_value, rate_limiting_state_inconsistent,
"Usage exceeds maximum value representable after extending for precision");
218 ordered_unique<tag<by_owner>,
251 static_assert( config::block_interval_ms > 0,
"config::block_interval_ms must be positive" );
252 static_assert( config::block_cpu_usage_average_window_ms >= config::block_interval_ms,
253 "config::block_cpu_usage_average_window_ms cannot be less than config::block_interval_ms" );
254 static_assert( config::block_size_average_window_ms >= config::block_interval_ms,
255 "config::block_size_average_window_ms cannot be less than config::block_interval_ms" );
258 elastic_limit_parameters cpu_limit_parameters = {
SYS_PERCENT(config::default_max_block_cpu_usage, config::default_target_block_cpu_usage_pct), config::default_max_block_cpu_usage, config::block_cpu_usage_average_window_ms / config::block_interval_ms, 1000, {99, 100}, {1000, 999}};
259 elastic_limit_parameters net_limit_parameters = {
SYS_PERCENT(config::default_max_block_net_usage, config::default_target_block_net_usage_pct), config::default_max_block_net_usage, config::block_size_average_window_ms / config::block_interval_ms, 1000, {99, 100}, {1000, 999}};
261 uint32_t account_cpu_usage_average_window = config::account_cpu_usage_average_window_ms / config::block_interval_ms;
262 uint32_t account_net_usage_average_window = config::account_net_usage_average_window_ms / config::block_interval_ms;
268 ordered_unique<tag<by_id>, member<resource_limits_config_object, resource_limits_config_object::id_type, &resource_limits_config_object::id>>
323 ordered_unique<tag<by_id>, member<resource_limits_state_object, resource_limits_state_object::id_type, &resource_limits_state_object::id>>
constexpr uint64_t SYS_PERCENT(uint64_t value, uint32_t percentage)
#define SYS_ASSERT(expr, exc_type, FORMAT,...)
#define CHAINBASE_SET_INDEX_TYPE(OBJECT_TYPE, INDEX_TYPE)
boost::multi_index_container< Object, Args..., chainbase::node_allocator< Object > > shared_multi_index_container
const T & min(const T &a, const T &b)
constexpr UnsignedIntType integer_divide_ceil(UnsignedIntType num, UnsignedIntType den)
ratio< T > make_ratio(T n, T d)
T operator*(T value, const ratio< T > &r)
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 >
constexpr bool is_valid_downgrade_cast
chainbase::shared_multi_index_container< resource_usage_object, indexed_by< ordered_unique< tag< by_id >, member< resource_usage_object, resource_usage_object::id_type, &resource_usage_object::id > >, ordered_unique< tag< by_owner >, member< resource_usage_object, account_name, &resource_usage_object::owner > > > > resource_usage_index
chainbase::shared_multi_index_container< resource_limits_object, indexed_by< ordered_unique< tag< by_id >, member< resource_limits_object, resource_limits_object::id_type, &resource_limits_object::id > >, ordered_unique< tag< by_owner >, composite_key< resource_limits_object, BOOST_MULTI_INDEX_MEMBER(resource_limits_object, bool, pending), > > > > resource_limits_index
chainbase::shared_multi_index_container< resource_limits_state_object, indexed_by< ordered_unique< tag< by_id >, member< resource_limits_state_object, resource_limits_state_object::id_type, &resource_limits_state_object::id > > > > resource_limits_state_index
chainbase::shared_multi_index_container< resource_limits_config_object, indexed_by< ordered_unique< tag< by_id >, member< resource_limits_config_object, resource_limits_config_object::id_type, &resource_limits_config_object::id > > > > resource_limits_config_index
@ resource_usage_object_type
@ resource_limits_config_object_type
unsigned __int128 uint128_t
#define T(meth, val, expected)
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
unsigned __int64 uint64_t
oid< resource_limits_object > id_type
Immutable except for fc::from_variant.
static constexpr uint64_t max_raw_value
void add(uint64_t units, uint32_t ordinal, uint32_t window_size)
uint64_t value_at(uint32_t ordinal, uint32_t window_size) const
uint64_t value_ex
The current accumulated value pre-multiplied by Precision.
uint64_t value_ex_at(uint32_t ordinal, uint32_t window_size) const
uint32_t last_ordinal
The ordinal of the last period which has contributed to the accumulator.
exponential_decay_accumulator()
uint64_t consumed
The last periods average + the current periods contribution so far.
exponential_moving_average_accumulator()
uint64_t value_ex
The current average pre-multiplied by Precision.
static constexpr uint64_t max_raw_value
uint32_t last_ordinal
The ordinal of the last period which has contributed to the average.
void add(uint64_t units, uint32_t ordinal, uint32_t window_size)