Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
resource_limits.hpp
Go to the documentation of this file.
1#pragma once
8#include <set>
9
10namespace sysio { namespace chain {
11
12 class deep_mind_handler;
13
14 namespace resource_limits {
15 namespace impl {
16 template<typename T>
17 struct ratio {
18 static_assert(std::is_integral<T>::value, "ratios must have integral types");
21
22 friend inline bool operator ==( const ratio& lhs, const ratio& rhs ) {
23 return std::tie(lhs.numerator, lhs.denominator) == std::tie(rhs.numerator, rhs.denominator);
24 }
25
26 friend inline bool operator !=( const ratio& lhs, const ratio& rhs ) {
27 return !(lhs == rhs);
28 }
29 };
30 }
31
33
35 uint64_t target; // the desired usage
36 uint64_t max; // the maximum usage
37 uint32_t periods; // the number of aggregation periods that contribute to the average usage
38
39 uint32_t max_multiplier; // the multiplier by which virtual space can oversell usage when uncongested
40 ratio contract_rate; // the rate at which a congested resource contracts its limit
41 ratio expand_rate; // the rate at which an uncongested resource expands its limits
42
43 void validate()const; // throws if the parameters do not satisfy basic sanity checks
44
45 friend inline bool operator ==( const elastic_limit_parameters& lhs, const elastic_limit_parameters& rhs ) {
46 return std::tie(lhs.target, lhs.max, lhs.periods, lhs.max_multiplier, lhs.contract_rate, lhs.expand_rate)
47 == std::tie(rhs.target, rhs.max, rhs.periods, rhs.max_multiplier, rhs.contract_rate, rhs.expand_rate);
48 }
49
50 friend inline bool operator !=( const elastic_limit_parameters& lhs, const elastic_limit_parameters& rhs ) {
51 return !(lhs == rhs);
52 }
53 };
54
57 int64_t available = 0;
58 int64_t max = 0;
59 };
60
62 public:
63
64 explicit resource_limits_manager(chainbase::database& db, std::function<deep_mind_handler*()> get_deep_mind_logger)
65 :_db(db),_get_deep_mind_logger(get_deep_mind_logger)
66 {
67 }
68
69 void add_indices();
71 void add_to_snapshot( const snapshot_writer_ptr& snapshot ) const;
72 void read_from_snapshot( const snapshot_reader_ptr& snapshot );
73
74 void initialize_account( const account_name& account );
75 void set_block_parameters( const elastic_limit_parameters& cpu_limit_parameters, const elastic_limit_parameters& net_limit_parameters );
76
77 void update_account_usage( const flat_set<account_name>& accounts, uint32_t ordinal );
78 void add_transaction_usage( const flat_set<account_name>& accounts, uint64_t cpu_usage, uint64_t net_usage, uint32_t ordinal );
79
80 void add_pending_ram_usage( const account_name account, int64_t ram_delta );
81 void verify_account_ram_usage( const account_name accunt )const;
82
84 bool set_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight);
85 void get_account_limits( const account_name& account, int64_t& ram_bytes, int64_t& net_weight, int64_t& cpu_weight) const;
86
87 bool is_unlimited_cpu( const account_name& account ) const;
88
90 void process_block_usage( uint32_t block_num );
91
92 // accessors
95
98
101
102 std::pair<int64_t, bool> get_account_cpu_limit( const account_name& name, uint32_t greylist_limit = config::maximum_elastic_resource_multiplier ) const;
103 std::pair<int64_t, bool> get_account_net_limit( const account_name& name, uint32_t greylist_limit = config::maximum_elastic_resource_multiplier ) const;
104
105 std::pair<account_resource_limit, bool> get_account_cpu_limit_ex( const account_name& name, uint32_t greylist_limit = config::maximum_elastic_resource_multiplier ) const;
106 std::pair<account_resource_limit, bool> get_account_net_limit_ex( const account_name& name, uint32_t greylist_limit = config::maximum_elastic_resource_multiplier ) const;
107
109
110 private:
112 std::function<deep_mind_handler*()> _get_deep_mind_logger;
113 };
114} } }
115
117FC_REFLECT( sysio::chain::resource_limits::ratio, (numerator)(denominator))
118FC_REFLECT( sysio::chain::resource_limits::elastic_limit_parameters, (target)(max)(periods)(max_multiplier)(contract_rate)(expand_rate))
bool is_unlimited_cpu(const account_name &account) const
void get_account_limits(const account_name &account, int64_t &ram_bytes, int64_t &net_weight, int64_t &cpu_weight) const
std::pair< account_resource_limit, bool > get_account_cpu_limit_ex(const account_name &name, uint32_t greylist_limit=config::maximum_elastic_resource_multiplier) const
void read_from_snapshot(const snapshot_reader_ptr &snapshot)
bool set_account_limits(const account_name &account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight)
set_account_limits returns true if new ram_bytes limit is more restrictive than the previously set on...
void set_block_parameters(const elastic_limit_parameters &cpu_limit_parameters, const elastic_limit_parameters &net_limit_parameters)
std::pair< int64_t, bool > get_account_cpu_limit(const account_name &name, uint32_t greylist_limit=config::maximum_elastic_resource_multiplier) const
std::pair< int64_t, bool > get_account_net_limit(const account_name &name, uint32_t greylist_limit=config::maximum_elastic_resource_multiplier) const
void add_to_snapshot(const snapshot_writer_ptr &snapshot) const
void add_pending_ram_usage(const account_name account, int64_t ram_delta)
void update_account_usage(const flat_set< account_name > &accounts, uint32_t ordinal)
void add_transaction_usage(const flat_set< account_name > &accounts, uint64_t cpu_usage, uint64_t net_usage, uint32_t ordinal)
int64_t get_account_ram_usage(const account_name &name) const
void verify_account_ram_usage(const account_name accunt) const
std::pair< account_resource_limit, bool > get_account_net_limit_ex(const account_name &name, uint32_t greylist_limit=config::maximum_elastic_resource_multiplier) const
resource_limits_manager(chainbase::database &db, std::function< deep_mind_handler *()> get_deep_mind_logger)
std::shared_ptr< snapshot_writer > snapshot_writer_ptr
Definition snapshot.hpp:155
std::shared_ptr< snapshot_reader > snapshot_reader_ptr
Definition snapshot.hpp:294
#define T(meth, val, expected)
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition reflect.hpp:311
signed __int64 int64_t
Definition stdint.h:135
unsigned int uint32_t
Definition stdint.h:126
unsigned __int64 uint64_t
Definition stdint.h:136
Immutable except for fc::from_variant.
Definition name.hpp:43
int64_t used
quantity used in current window
friend bool operator!=(const elastic_limit_parameters &lhs, const elastic_limit_parameters &rhs)
friend bool operator==(const elastic_limit_parameters &lhs, const elastic_limit_parameters &rhs)
friend bool operator==(const ratio &lhs, const ratio &rhs)
friend bool operator!=(const ratio &lhs, const ratio &rhs)