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

REX enhancements and bug fixes #182

Merged
merged 27 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6d2f5c4
Created Pull Request Template (#161)
kj4ezj Jan 3, 2019
20ce75f
Add inline dummy actions to sell and buy rex for bookkeeping
zorba80 Jan 10, 2019
e44c5a1
Remove unnecessary permissions from inline dummy actions
zorba80 Jan 11, 2019
38db481
Other: Added checkbox to pull request template (#164)
kj4ezj Jan 15, 2019
69d7a6c
add functionality to set explicit resources
arhag Jan 15, 2019
8eef0fc
unit tests for new actions to set explicit resources
arhag Jan 15, 2019
9f60bee
update version and expand eosio version dependency range
arhag Jan 15, 2019
060a9f8
Fix sellrex bug
zorba80 Jan 15, 2019
50b235c
Fix bug in updating REX vote stake
zorba80 Jan 22, 2019
fa5936f
fix bug found from @zorba80's review
arhag Jan 22, 2019
fc55d76
Merge pull request #172 from EOSIO/managed-resources-2
arhag Jan 23, 2019
d078c9f
Merge branch 'develop' into rex-en
zorba80 Jan 23, 2019
7a6b114
REX rounding errors
zorba80 Jan 25, 2019
e067647
Add comment
zorba80 Jan 29, 2019
a4fd33e
Change initial REX connector balance, unlent REX pool lower bound
zorba80 Jan 30, 2019
c99ac02
Small change
zorba80 Jan 31, 2019
a3b6ce3
Small comment changes
zorba80 Jan 31, 2019
8dc8e1c
fix setabi_bios test
arhag Jan 31, 2019
50ccb32
Merge pull request #183 from EOSIO/fix-setabi_bios-test
arhag Jan 31, 2019
1bb9f72
Merge branch 'develop' into rex-en
zorba80 Feb 1, 2019
5a2e3b0
Create REX savings
zorba80 Feb 5, 2019
c8010b3
Create REX savings - 2
zorba80 Feb 5, 2019
c40bac8
Create REX savings - testing
zorba80 Feb 5, 2019
a4b8808
Comments, small changes
zorba80 Feb 5, 2019
04b43db
Fixed bug found by @larryk85 code review, more testing
zorba80 Feb 5, 2019
e24442d
Small optimization
zorba80 Feb 6, 2019
c9648b8
Small change
zorba80 Feb 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 87 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,18 @@ namespace eosiosystem {
[[eosio::action]]
void consolidate( const name& owner );

/**
*
*/
[[eosio::action]]
void mvtosavings( const name& owner, const asset& rex );

/**
*
*/
[[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 +616,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 +634,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>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, who ever wrote this is amazing!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's VT PhD level coding. :)

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