Skip to content

Commit

Permalink
Merge pull request #5 from terradacs/mt-stake-problem
Browse files Browse the repository at this point in the history
Problem with account limit - fixed
  • Loading branch information
vogel76 authored Jul 31, 2019
2 parents bf60876 + 6d79919 commit 31e1917
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 63 deletions.
20 changes: 2 additions & 18 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,9 @@ static inline void print_debug(account_name receiver, const action_trace& ar) {
inline void apply_context::change_any_resource_limits_impl( const account_name& acc, int64_t ram, int64_t net, int64_t cpu, bool is_distribution )const
{
auto& resource_limit_mgr = control.get_mutable_resource_limits_manager();
int64_t ram_bytes = 0;
int64_t net_weight = 0;
int64_t cpu_weight = 0;

resource_limit_mgr.get_account_limits( acc, ram_bytes, net_weight, cpu_weight );
ram_bytes += ram;
net_weight += net;
cpu_weight += cpu;

if( is_distribution )
{
if (resource_limit_mgr.set_distribution_account_limits( acc, ram_bytes, net_weight, cpu_weight ))
trx_context.validate_ram_usage.insert( acc );
}
else
{
if (resource_limit_mgr.set_account_limits( acc, ram_bytes, net_weight, cpu_weight ))
trx_context.validate_ram_usage.insert( acc );
}
if( resource_limit_mgr.change_account_limits( acc, ram, net, cpu, is_distribution ) )
trx_context.validate_ram_usage.insert( acc );
}

inline void apply_context::change_distribution_resource_limits( const account_name& acc, int64_t ram, int64_t net, int64_t cpu )const {
Expand Down
7 changes: 5 additions & 2 deletions libraries/chain/include/eosio/chain/resource_limits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace eosio { namespace chain { namespace resource_limits {
void verify_account_ram_usage( const account_name accunt )const;

/// set_account_limits returns true if new ram_bytes limit is more restrictive than the previously set one
bool set_distribution_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight);
bool change_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight, bool is_distribution );
bool set_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight);
void enable_unstake_mode_distribution_resource_rewards( account_name account );
/** Allows to retrieve amount of resources associated to given account by rewarding process performed during distribution period.
Expand Down Expand Up @@ -109,7 +109,10 @@ namespace eosio { namespace chain { namespace resource_limits {
chainbase::database& _db;

bool get_any_account_data( const account_name& account, int64_t& ram_bytes, int64_t& net_weight, int64_t& cpu_weight, bool is_distribution ) const;
bool set_any_account_limits_impl( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight, bool is_distribution );

const resource_limits_object& find_or_create_pending_limits( const account_name& account );
bool change_any_account_limits_impl( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight, bool is_distribution );
bool set_any_account_limits_impl( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight );

template <typename TObjectConverter, typename TProcessor>
void process_userres(const account_name& lowerBound, const account_name& upperBound, TProcessor processor) const;
Expand Down
27 changes: 15 additions & 12 deletions libraries/chain/include/eosio/chain/resource_limits_private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,27 @@ namespace eosio { namespace chain { namespace resource_limits {
int64_t distribution_cpu_weight = 0;
int64_t distribution_ram_bytes = 0;

void set_resource_limits( int64_t _ram_bytes, int64_t _net_weight, int64_t _cpu_weight, bool is_distribution )
void change_resource_limits( int64_t _ram_bytes, int64_t _net_weight, int64_t _cpu_weight, bool is_distribution )
{
ram_bytes = _ram_bytes;
net_weight = _net_weight;
cpu_weight = _cpu_weight;
ram_bytes += _ram_bytes;
net_weight += _net_weight;
cpu_weight += _cpu_weight;

if( is_distribution )
{
if( _ram_bytes > distribution_ram_bytes )
distribution_ram_bytes = _ram_bytes;

if( _net_weight > distribution_net_weight )
distribution_net_weight = _net_weight;

if( _cpu_weight > distribution_cpu_weight )
distribution_cpu_weight = _cpu_weight;
EOS_ASSERT( _ram_bytes >= 0 && _net_weight >= 0 && _cpu_weight >= 0, resource_limit_exception, "during distribution only positive rewards are allowed" );
distribution_ram_bytes += _ram_bytes;
distribution_net_weight += _net_weight;
distribution_cpu_weight += _cpu_weight;
}
}

void set_resource_limits( int64_t _ram_bytes, int64_t _net_weight, int64_t _cpu_weight )
{
ram_bytes = _ram_bytes;
net_weight = _net_weight;
cpu_weight = _cpu_weight;
}
};

struct by_owner;
Expand Down
79 changes: 48 additions & 31 deletions libraries/chain/resource_limits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,40 +301,55 @@ fc::mutable_variant_object resource_limits_manager::convert_to_public( const res
( "ram_bytes", object.ram_bytes );
}

bool resource_limits_manager::set_any_account_limits_impl( const account_name& account,
int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight,
bool is_distribution ) {
const resource_limits_object& resource_limits_manager::find_or_create_pending_limits( const account_name& account )
{
//const auto& usage = _db.get<resource_usage_object,by_owner>( account );
/*
* Since we need to delay these until the next resource limiting boundary, these are created in a "pending"
* state or adjusted in an existing "pending" state. The chain controller will collapse "pending" state into
* the actual state at the next appropriate boundary.
*/
auto find_or_create_pending_limits = [&]() -> const resource_limits_object& {
const auto* pending_limits = _db.find<resource_limits_object, by_owner>( boost::make_tuple(true, account) );
if (pending_limits == nullptr) {
const auto& limits = _db.get<resource_limits_object, by_owner>( boost::make_tuple(false, account));
return _db.create<resource_limits_object>([&](resource_limits_object& pending_limits){
pending_limits.owner = limits.owner;

pending_limits.ram_bytes = limits.ram_bytes;
pending_limits.net_weight = limits.net_weight;
pending_limits.cpu_weight = limits.cpu_weight;

pending_limits.distribution_ram_bytes = limits.distribution_ram_bytes;
pending_limits.distribution_net_weight = limits.distribution_net_weight;
pending_limits.distribution_cpu_weight = limits.distribution_cpu_weight;

pending_limits.pending = true;
pending_limits.unstaked_mode = limits.unstaked_mode;
});
} else {
return *pending_limits;
}
};
const auto* pending_limits = _db.find<resource_limits_object, by_owner>( boost::make_tuple(true, account) );
if (pending_limits == nullptr) {
const auto& limits = _db.get<resource_limits_object, by_owner>( boost::make_tuple(false, account));
return _db.create<resource_limits_object>([&](resource_limits_object& pending_limits){
pending_limits.owner = limits.owner;

pending_limits.ram_bytes = limits.ram_bytes;
pending_limits.net_weight = limits.net_weight;
pending_limits.cpu_weight = limits.cpu_weight;

pending_limits.distribution_ram_bytes = limits.distribution_ram_bytes;
pending_limits.distribution_net_weight = limits.distribution_net_weight;
pending_limits.distribution_cpu_weight = limits.distribution_cpu_weight;

pending_limits.pending = true;
pending_limits.unstaked_mode = limits.unstaked_mode;
});
} else {
return *pending_limits;
}
}

bool resource_limits_manager::change_any_account_limits_impl( const account_name& account,
int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight,
bool is_distribution )
{
// update the users weights directly
auto& limits = find_or_create_pending_limits( account );

_db.modify( limits, [&]( resource_limits_object& pending_limits ){
pending_limits.change_resource_limits( ram_bytes, net_weight, cpu_weight, is_distribution );
});

return ram_bytes < 0;
}

bool resource_limits_manager::set_any_account_limits_impl( const account_name& account,
int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight )
{
// update the users weights directly
auto& limits = find_or_create_pending_limits();
auto& limits = find_or_create_pending_limits( account );

bool decreased_limit = false;

Expand All @@ -352,18 +367,20 @@ bool resource_limits_manager::set_any_account_limits_impl( const account_name& a
}

_db.modify( limits, [&]( resource_limits_object& pending_limits ){
pending_limits.set_resource_limits( ram_bytes, net_weight, cpu_weight, is_distribution );
pending_limits.set_resource_limits( ram_bytes, net_weight, cpu_weight );
});

return decreased_limit;
}

bool resource_limits_manager::set_distribution_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) {
return set_any_account_limits_impl( account, ram_bytes, net_weight, cpu_weight, true/*is_distribution*/ );
bool resource_limits_manager::change_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight, bool is_distribution )
{
return change_any_account_limits_impl( account, ram_bytes, net_weight, cpu_weight, is_distribution );
}

bool resource_limits_manager::set_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight) {
return set_any_account_limits_impl( account, ram_bytes, net_weight, cpu_weight, false/*is_distribution*/ );
bool resource_limits_manager::set_account_limits( const account_name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight)
{
return set_any_account_limits_impl( account, ram_bytes, net_weight, cpu_weight );
}

bool resource_limits_manager::get_any_account_data( const account_name& account, int64_t& ram_bytes, int64_t& net_weight, int64_t& cpu_weight, bool is_distribution ) const
Expand Down

0 comments on commit 31e1917

Please sign in to comment.