Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #182 from EOSIO/rex-en
Browse files Browse the repository at this point in the history
REX enhancements and bug fixes
  • Loading branch information
zorba80 committed Feb 7, 2019
2 parents e3a3332 + c9648b8 commit 5087727
Show file tree
Hide file tree
Showing 10 changed files with 1,267 additions and 212 deletions.
23 changes: 23 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- PLEASE FILL OUT THE FOLLOWING MARKDOWN TEMPLATE -->
<!-- PR title alone should be sufficient to understand changes. -->

## Change Description
<!-- Describe your changes, their justification, AND their impact. Reference issues or pull requests where possible (use '#XX' or 'GH-XX' where XX is the issue or pull request number). -->


## Deployment Changes
- [ ] Deployment Changes
<!-- checked [x] = Deployment changes; unchecked [ ] = no changes, ignore this section -->
<!-- If this PR introduces a change to the contracts that causes deployment to change, please describe the impact. -->


## API Changes
- [ ] API Changes
<!-- checked [x] = API changes; unchecked [ ] = no changes, ignore this section -->
<!-- If this PR introduces API changes, please describe the changes here. What will developers need to know before upgrading to this version? -->


## Documentation Additions
- [ ] Documentation Additions
<!-- checked [x] = Documentation changes; unchecked [ ] = no changes, ignore this section -->
<!-- Describe what must be added to the documentation after merge. -->
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(eosio_contracts)
set(VERSION_MAJOR 1)
set(VERSION_MINOR 6)
set(VERSION_PATCH 0)
set(VERSION_SUFFIX rc1)
set(VERSION_SUFFIX rc2)

if (VERSION_SUFFIX)
set(VERSION_FULL "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_SUFFIX}")
Expand Down
93 changes: 91 additions & 2 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <string>
#include <deque>
#include <type_traits>
#include <optional>

#ifdef CHANNEL_RAM_AND_NAMEBID_FEES_TO_REX
#undef CHANNEL_RAM_AND_NAMEBID_FEES_TO_REX
Expand All @@ -37,6 +39,25 @@ namespace eosiosystem {
using eosio::datastream;
using eosio::check;

template<typename E, typename F>
static inline auto has_field( F flags, E field )
-> std::enable_if_t< std::is_integral_v<F> && std::is_unsigned_v<F> &&
std::is_enum_v<E> && std::is_same_v< F, std::underlying_type_t<E> >, bool>
{
return ( (flags & static_cast<F>(field)) != 0 );
}

template<typename E, typename F>
static inline auto set_field( F flags, E field, bool value = true )
-> std::enable_if_t< std::is_integral_v<F> && std::is_unsigned_v<F> &&
std::is_enum_v<E> && std::is_same_v< F, std::underlying_type_t<E> >, F >
{
if( value )
return ( flags | static_cast<F>(field) );
else
return ( flags & ~static_cast<F>(field) );
}

struct [[eosio::table, eosio::contract("eosio.system")]] name_bid {
name newname;
name high_bidder;
Expand Down Expand Up @@ -162,14 +183,20 @@ namespace eosiosystem {
bool is_proxy = 0; /// whether the voter is a proxy for others


uint32_t reserved1 = 0;
uint32_t flags1 = 0;
uint32_t reserved2 = 0;
eosio::asset reserved3;

uint64_t primary_key()const { return owner.value; }

enum class flags1_fields : uint32_t {
ram_managed = 1,
net_managed = 2,
cpu_managed = 4
};

// explicit serialization macro is not necessary, used here only to improve compilation time
EOSLIB_SERIALIZE( voter_info, (owner)(proxy)(producers)(staked)(last_vote_weight)(proxied_vote_weight)(is_proxy)(reserved1)(reserved2)(reserved3) )
EOSLIB_SERIALIZE( voter_info, (owner)(proxy)(producers)(staked)(last_vote_weight)(proxied_vote_weight)(is_proxy)(flags1)(reserved2)(reserved3) )
};

typedef eosio::multi_index< "voters"_n, voter_info > voters_table;
Expand Down Expand Up @@ -301,6 +328,7 @@ namespace eosiosystem {
static constexpr eosio::name names_account{"eosio.names"_n};
static constexpr eosio::name saving_account{"eosio.saving"_n};
static constexpr eosio::name rex_account{"eosio.rex"_n};
static constexpr eosio::name null_account{"eosio.null"_n};
static constexpr symbol ramcore_symbol = symbol(symbol_code("RAMCORE"), 4);
static constexpr symbol ram_symbol = symbol(symbol_code("RAM"), 0);
static constexpr symbol rex_symbol = symbol(symbol_code("REX"), 4);
Expand All @@ -322,6 +350,16 @@ namespace eosiosystem {

[[eosio::action]]
void setalimits( name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight );

[[eosio::action]]
void setacctram( name account, std::optional<int64_t> ram_bytes );

[[eosio::action]]
void setacctnet( name account, std::optional<int64_t> net_weight );

[[eosio::action]]
void setacctcpu( name account, std::optional<int64_t> cpu_weight );

// functions defined in delegate_bandwidth.cpp

/**
Expand Down Expand Up @@ -429,6 +467,22 @@ namespace eosiosystem {
[[eosio::action]]
void consolidate( const name& owner );

/**
* Moves a specified amount of REX into savings bucket. REX savings bucket
* never matures. In order for it to be sold, it has to be moved explicitly
* out of that bucket. Then the moved amount will have the regular maturity
* period of 4 days starting from the end of the day.
*/
[[eosio::action]]
void mvtosavings( const name& owner, const asset& rex );

/**
* Moves a specified amount of REX out of savings bucket. The moved amount
* will have the regular REX maturity period of 4 days.
*/
[[eosio::action]]
void mvfrsavings( const name& owner, const asset& rex );

/**
* Deletes owner records from REX tables and frees used RAM.
* Owner must not have an outstanding REX balance.
Expand Down Expand Up @@ -566,6 +620,9 @@ namespace eosiosystem {
void process_rex_maturities( const rex_balance_table::const_iterator& bitr );
void consolidate_rex_balance( const rex_balance_table::const_iterator& bitr,
const asset& rex_in_sell_order );
int64_t read_rex_savings( const rex_balance_table::const_iterator& bitr );
void put_rex_savings( const rex_balance_table::const_iterator& bitr, int64_t rex );
void update_rex_stake( const name& voter );

// defined in delegate_bandwidth.cpp
void changebw( name from, name receiver,
Expand All @@ -581,6 +638,38 @@ namespace eosiosystem {
double shares_rate, bool reset_to_zero = false );
double update_total_votepay_share( time_point ct,
double additional_shares_delta = 0.0, double shares_rate_delta = 0.0 );

template <auto system_contract::*...Ptrs>
class registration {
public:
template <auto system_contract::*P, auto system_contract::*...Ps>
struct for_each {
template <typename... Args>
static constexpr void call( system_contract* this_contract, Args&&... args )
{
std::invoke( P, this_contract, std::forward<Args>(args)... );
for_each<Ps...>::call( this_contract, std::forward<Args>(args)... );
}
};
template <auto system_contract::*P>
struct for_each<P> {
template <typename... Args>
static constexpr void call( system_contract* this_contract, Args&&... args )
{
std::invoke( P, this_contract, std::forward<Args>(args)... );
}
};

template <typename... Args>
constexpr void operator() ( Args&&... args )
{
for_each<Ptrs...>::call( this_contract, std::forward<Args>(args)... );
}

system_contract* this_contract;
};

registration<&system_contract::update_rex_stake> vote_stake_updater{ this };
};

} /// eosiosystem
55 changes: 42 additions & 13 deletions contracts/eosio.system/src/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <eosio.system/eosio.system.hpp>

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/datastream.hpp>
#include <eosiolib/serialize.hpp>
#include <eosiolib/multi_index.hpp>
Expand All @@ -22,7 +21,6 @@ namespace eosiosystem {
using eosio::asset;
using eosio::indexed_by;
using eosio::const_mem_fun;
using eosio::print;
using eosio::permission_level;
using eosio::time_point_sec;
using std::map;
Expand Down Expand Up @@ -163,7 +161,13 @@ namespace eosiosystem {
res.ram_bytes += bytes_out;
});
}
set_resource_limits( res_itr->owner.value, res_itr->ram_bytes + ram_gift_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );

auto voter_itr = _voters.find( res_itr->owner.value );
if( voter_itr == _voters.end() || !has_field( voter_itr->flags1, voter_info::flags1_fields::ram_managed ) ) {
int64_t ram_bytes, net, cpu;
get_resource_limits( res_itr->owner.value, &ram_bytes, &net, &cpu );
set_resource_limits( res_itr->owner.value, res_itr->ram_bytes + ram_gift_bytes, net, cpu );
}
}

/**
Expand Down Expand Up @@ -201,7 +205,13 @@ namespace eosiosystem {
userres.modify( res_itr, account, [&]( auto& res ) {
res.ram_bytes -= bytes;
});
set_resource_limits( res_itr->owner.value, res_itr->ram_bytes + ram_gift_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );

auto voter_itr = _voters.find( res_itr->owner.value );
if( voter_itr == _voters.end() || !has_field( voter_itr->flags1, voter_info::flags1_fields::ram_managed ) ) {
int64_t ram_bytes, net, cpu;
get_resource_limits( res_itr->owner.value, &ram_bytes, &net, &cpu );
set_resource_limits( res_itr->owner.value, res_itr->ram_bytes + ram_gift_bytes, net, cpu );
}

INLINE_ACTION_SENDER(eosio::token, transfer)(
token_account, { {ram_account, active_permission}, {account, active_permission} },
Expand Down Expand Up @@ -233,8 +243,8 @@ namespace eosiosystem {
require_auth( from );
check( stake_net_delta.amount != 0 || stake_cpu_delta.amount != 0, "should stake non-zero amount" );
check( std::abs( (stake_net_delta + stake_cpu_delta).amount )
>= std::max( std::abs( stake_net_delta.amount ), std::abs( stake_cpu_delta.amount ) ),
"net and cpu deltas cannot be opposite signs" );
>= std::max( std::abs( stake_net_delta.amount ), std::abs( stake_cpu_delta.amount ) ),
"net and cpu deltas cannot be opposite signs" );

name source_stake_from = from;
if ( transfer ) {
Expand Down Expand Up @@ -285,10 +295,28 @@ namespace eosiosystem {
check( 0 <= tot_itr->net_weight.amount, "insufficient staked total net bandwidth" );
check( 0 <= tot_itr->cpu_weight.amount, "insufficient staked total cpu bandwidth" );

int64_t ram_bytes, net, cpu;
get_resource_limits( receiver.value, &ram_bytes, &net, &cpu );

set_resource_limits( receiver.value, std::max( tot_itr->ram_bytes + ram_gift_bytes, ram_bytes ), tot_itr->net_weight.amount, tot_itr->cpu_weight.amount );
{
bool ram_managed = false;
bool net_managed = false;
bool cpu_managed = false;

auto voter_itr = _voters.find( receiver.value );
if( voter_itr != _voters.end() ) {
ram_managed = has_field( voter_itr->flags1, voter_info::flags1_fields::ram_managed );
net_managed = has_field( voter_itr->flags1, voter_info::flags1_fields::net_managed );
cpu_managed = has_field( voter_itr->flags1, voter_info::flags1_fields::cpu_managed );
}

if( !(net_managed && cpu_managed) ) {
int64_t ram_bytes, net, cpu;
get_resource_limits( receiver.value, &ram_bytes, &net, &cpu );

set_resource_limits( receiver.value,
ram_managed ? ram_bytes : std::max( tot_itr->ram_bytes + ram_gift_bytes, ram_bytes ),
net_managed ? net : tot_itr->net_weight.amount,
cpu_managed ? cpu : tot_itr->cpu_weight.amount );
}
}

if ( tot_itr->is_empty() ) {
totals_tbl.erase( tot_itr );
Expand Down Expand Up @@ -385,6 +413,7 @@ namespace eosiosystem {
}
}

vote_stake_updater( from );
update_voting_power( from, stake_net_delta + stake_cpu_delta );
}

Expand All @@ -402,7 +431,7 @@ namespace eosiosystem {
});
}

check( 0 <= voter_itr->staked, "stake for voting cannot be negative");
check( 0 <= voter_itr->staked, "stake for voting cannot be negative" );

if( voter == "b1"_n ) {
validate_b1_vesting( voter_itr->staked );
Expand Down Expand Up @@ -434,7 +463,7 @@ namespace eosiosystem {
check( unstake_net_quantity >= zero_asset, "must unstake a positive amount" );
check( unstake_cpu_quantity.amount + unstake_net_quantity.amount > 0, "must unstake a positive amount" );
check( _gstate.total_activated_stake >= min_activated_stake,
"cannot undelegate bandwidth until the chain is activated (at least 15% of all tokens participate in voting)" );
"cannot undelegate bandwidth until the chain is activated (at least 15% of all tokens participate in voting)" );

changebw( from, receiver, -unstake_net_quantity, -unstake_cpu_quantity, false);
} // undelegatebw
Expand All @@ -447,7 +476,7 @@ namespace eosiosystem {
auto req = refunds_tbl.find( owner.value );
check( req != refunds_tbl.end(), "refund request not found" );
check( req->request_time + seconds(refund_delay_sec) <= current_time_point(),
"refund is not available yet" );
"refund is not available yet" );

INLINE_ACTION_SENDER(eosio::token, transfer)(
token_account, { {stake_account, active_permission}, {req->owner, active_permission} },
Expand Down
Loading

0 comments on commit 5087727

Please sign in to comment.