提交 ceb04d10 编写于 作者: B Brian Johnson

Changed to report different exception for each type of rate limiting. Fixed...

Changed to report different exception for each type of rate limiting.  Fixed name for authorization account parameters.
上级 eac0424f
......@@ -878,7 +878,7 @@ void chain_controller::validate_expiration(const Transaction& trx) const
} FC_CAPTURE_AND_RETHROW((trx)) }
uint32_t chain_controller::_transaction_message_rate(uint32_t now, uint32_t last_update_sec, uint32_t rate_limit_time_frame_sec,
uint32_t rate_limit, uint32_t previous_rate, const char* type, const AccountName& name)
uint32_t rate_limit, uint32_t previous_rate, rate_limit_type type, const AccountName& name)
{
const auto delta_time = now - last_update_sec;
uint32_t message_count = 1;
......@@ -886,13 +886,23 @@ uint32_t chain_controller::_transaction_message_rate(uint32_t now, uint32_t last
{
message_count += ( ( ( rate_limit_time_frame_sec - delta_time ) * fc::uint128( previous_rate ) )
/ rate_limit_time_frame_sec ).to_uint64();
EOS_ASSERT(message_count <= rate_limit, tx_msgs_exceeded,
"Rate limiting ${type}=${name} messages sent, ${count} exceeds ${max} messages limit per ${sec} seconds. Wait 1 second and try again",
("type",type)
("name",name)
("count",message_count)
("max",rate_limit)
("sec", rate_limit_time_frame_sec));
#define RATE_LIMIT_ASSERT(tx_msgs_exceeded, type_str) \
EOS_ASSERT(message_count <= rate_limit, tx_msgs_exceeded, \
"Rate limiting ${type} account=${name} messages sent, ${count} exceeds ${max} messages limit per ${sec} seconds. Wait 1 second and try again", \
("type",type_str) \
("name",name) \
("count",message_count) \
("max",rate_limit) \
("sec", rate_limit_time_frame_sec))
switch (type)
{
case authorization_account:
RATE_LIMIT_ASSERT(tx_msgs_auth_exceeded, "authorization");
break;
case code_account:
RATE_LIMIT_ASSERT(tx_msgs_code_exceeded, "code");
break;
}
}
return message_count;
......@@ -910,7 +920,7 @@ void chain_controller::rate_limit_message(const Message& message)
{
_db.create<rate_limiting_object>([&](rate_limiting_object& rlo) {
rlo.name = permission.account;
rlo.per_auth_code_trans_msg_rate = 1;
rlo.per_auth_account_trans_msg_rate = 1;
rlo.per_auth_account_last_update_sec = now;
});
}
......@@ -918,9 +928,9 @@ void chain_controller::rate_limit_message(const Message& message)
{
const auto message_rate =
_transaction_message_rate(now, rate_limiting->per_auth_account_last_update_sec, _per_auth_account_trans_msg_rate_limit_time_frame_sec,
_per_auth_account_trans_msg_rate_limit, rate_limiting->per_auth_code_trans_msg_rate, "account", permission.account);
_per_auth_account_trans_msg_rate_limit, rate_limiting->per_auth_account_trans_msg_rate, authorization_account, permission.account);
_db.modify(*rate_limiting, [&] (rate_limiting_object& rlo) {
rlo.per_auth_code_trans_msg_rate = message_rate;
rlo.per_auth_account_trans_msg_rate = message_rate;
rlo.per_auth_account_last_update_sec = now;
});
}
......@@ -940,7 +950,7 @@ void chain_controller::rate_limit_message(const Message& message)
{
const auto message_rate =
_transaction_message_rate(now, rate_limiting->per_code_account_last_update_sec, _per_code_account_trans_msg_rate_limit_time_frame_sec,
_per_code_account_trans_msg_rate_limit, rate_limiting->per_code_account_trans_msg_rate, "account", message.code);
_per_code_account_trans_msg_rate_limit, rate_limiting->per_code_account_trans_msg_rate, code_account, message.code);
_db.modify(*rate_limiting, [&] (rate_limiting_object& rlo) {
rlo.per_code_account_trans_msg_rate = message_rate;
rlo.per_code_account_last_update_sec = now;
......
......@@ -259,6 +259,15 @@ namespace eos { namespace chain {
const deque<SignedTransaction>& pending()const { return _pending_transactions; }
/**
* Enum to indicate what type of rate limiting is being performed.
*/
enum rate_limit_type
{
authorization_account,
code_account
};
/**
* Determine what the current message rate is.
* @param now The current block time seconds
......@@ -266,13 +275,14 @@ namespace eos { namespace chain {
* @param rate_limit_time_frame_sec The time frame, in seconds, that the rate limit is over
* @param rate_limit The rate that is not allowed to be exceeded
* @param previous_rate The rate at the last_update_sec
* @param type The string type description (for logging errors)
* @param type The type of the rate limit
* @param name The account name associated with this rate (for logging errors)
* @return the calculated rate at this time
* @throws tx_msgs_exceeded if current message rate exceeds the passed in rate_limit
* @throws tx_msgs_auth_exceeded if current message rate exceeds the passed in rate_limit, and type is authorization_account
* @throws tx_msgs_code_exceeded if current message rate exceeds the passed in rate_limit, and type is code_account
*/
static uint32_t _transaction_message_rate(uint32_t now, uint32_t last_update_sec, uint32_t rate_limit_time_frame_sec,
uint32_t rate_limit, uint32_t previous_rate, const char* type, const AccountName& name);
uint32_t rate_limit, uint32_t previous_rate, rate_limit_type type, const AccountName& name);
struct trans_msg_rate_limits {
static const uint32_t default_per_auth_account_time_frame_seconds;
......@@ -361,7 +371,8 @@ namespace eos { namespace chain {
/**
* Calculate all rates associated with the given message and enforce rate limiting.
* @param message The message to calculate
* @throws tx_msgs_exceeded if any of the calculated message rates exceed the configured rate limit
* @throws tx_msgs_auth_exceeded if any of the calculated message rates exceed the configured authorization account rate limit
* @throws tx_msgs_code_exceeded if the calculated message rate exceed the configured code account rate limit
*/
void rate_limit_message(const Message& message);
......
......@@ -24,7 +24,7 @@ namespace eos { namespace chain {
id_type id;
AccountName name;
uint32_t per_auth_account_last_update_sec = 0;
uint32_t per_auth_code_trans_msg_rate = 0;
uint32_t per_auth_account_trans_msg_rate = 0;
uint32_t per_code_account_last_update_sec = 0;
uint32_t per_code_account_trans_msg_rate = 0;
};
......@@ -45,4 +45,4 @@ CHAINBASE_SET_INDEX_TYPE(eos::chain::rate_limiting_object, eos::chain::rate_limi
FC_REFLECT(chainbase::oid<eos::chain::rate_limiting_object>, (_id))
FC_REFLECT(eos::chain::rate_limiting_object, (id)(name)(per_auth_account_last_update_sec)(per_auth_code_trans_msg_rate)(per_code_account_last_update_sec)(per_code_account_trans_msg_rate))
FC_REFLECT(eos::chain::rate_limiting_object, (id)(name)(per_auth_account_last_update_sec)(per_auth_account_trans_msg_rate)(per_code_account_last_update_sec)(per_code_account_trans_msg_rate))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册