Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
3208b11
temporarily move back comment to avoid cherry-pick conflicts
arhag Dec 17, 2019
822271a
resource31
tbfleming Nov 27, 2019
1f4110f
buybandwidth
tbfleming Nov 27, 2019
4a3502b
buybandwidth
tbfleming Nov 27, 2019
d6f34f0
buybandwidth
tbfleming Nov 27, 2019
eed8e08
buybandwidth
tbfleming Nov 27, 2019
753594d
buybandwidth
tbfleming Dec 2, 2019
0641c09
rentbw
tbfleming Dec 2, 2019
e4b6caf
rentbw
tbfleming Dec 2, 2019
fbe2a69
rentbw
tbfleming Dec 3, 2019
9e4e1c8
rentbw
tbfleming Dec 3, 2019
418012c
testing
tbfleming Dec 4, 2019
25c8dfe
testing
tbfleming Dec 4, 2019
650dc4c
tests
tbfleming Dec 5, 2019
9950ad5
tests
tbfleming Dec 5, 2019
65ca28b
tests
tbfleming Dec 6, 2019
5142946
tests
tbfleming Dec 6, 2019
7238a7c
Fix eosio.reserve
tbfleming Dec 6, 2019
9e3d51d
fix utilization update order
tbfleming Dec 9, 2019
eba1aad
Fix adjusted_utilization
tbfleming Dec 9, 2019
954e7ce
tests
tbfleming Dec 10, 2019
33ee7b8
tests
tbfleming Dec 10, 2019
913a26a
prevent weight from getting too large
tbfleming Dec 10, 2019
9cb59e9
tests
tbfleming Dec 11, 2019
0010cf4
tests
tbfleming Dec 11, 2019
280c80a
Missing action wrapper
tbfleming Dec 11, 2019
be84f4d
Adjust min_rent_price description
tbfleming Dec 13, 2019
4aaac71
minor changes to rentbw system
arhag Dec 17, 2019
24fd5d8
move comment (with new rentbw addition) back to its proper place
arhag Dec 17, 2019
14d29b3
also change description of decay_secs in rentbw_state_resource to ref…
arhag Dec 17, 2019
5aacf5d
fix typo in previous commit
arhag Dec 17, 2019
6ed4424
update dependencies in pipeline.jsonc
arhag Dec 17, 2019
4aa8d94
replace calc_rentbw_price with calc_rentbw_fee
arhag Dec 18, 2019
07b37a7
prefer rounding in a way that ensures fee calculation does not give a…
arhag Dec 18, 2019
a4ee27b
fix name of configrentbw action wrapper type
arhag Dec 18, 2019
86e6fff
add default values for rent_days, decay_secs, and exponent
arhag Dec 18, 2019
f0ac157
add default values for min_rent_price and target_price as well
arhag Dec 18, 2019
a785971
fix wrong default value for rent_days
arhag Dec 18, 2019
defb54c
fix comment
arhag Dec 19, 2019
1f38b5e
fix comment description of some configuration options
arhag Dec 19, 2019
1e6f2b8
remove defaults for min_rent_price and target_price since values that…
arhag Dec 19, 2019
8ac98ee
modify fee calculation to not give advantage to very small rentals ev…
arhag Dec 19, 2019
61bce43
rename min_rent_price to min_rent_fee
arhag Dec 19, 2019
764667f
use optionals rather than 0 for config parameters to signal that it s…
arhag Dec 19, 2019
7f75eef
replace target_price with the more meaningful max_price; also change …
arhag Dec 20, 2019
948de82
remove unnecessary precondition comment
arhag Dec 20, 2019
718f3d5
small tweaks to comments
arhag Dec 20, 2019
cc24aed
quick fix to comment
arhag Dec 20, 2019
c272470
Merge branch 'develop' into rentbw-develop
arhag Dec 20, 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
1 change: 1 addition & 0 deletions contracts/eosio.system/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_contract(eosio.system eosio.system
${CMAKE_CURRENT_SOURCE_DIR}/src/exchange_state.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/native.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/producer_pay.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rentbw.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/rex.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/voting.cpp
)
Expand Down
196 changes: 179 additions & 17 deletions contracts/eosio.system/include/eosio.system/eosio.system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace eosiosystem {
using eosio::time_point_sec;
using eosio::unsigned_int;

inline constexpr int64_t rentbw_frac = 1'000'000'000'000'000ll; // 1.0 = 10^15

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> &&
Expand Down Expand Up @@ -332,8 +334,8 @@ namespace eosiosystem {
// - `version` defaulted to zero,
// - `last_dist_time` the last time proceeds from renting, ram fees, and name bids were added to the rex pool,
// - `pending_bucket_time` timestamp of the pending 12-hour return bucket,
// - `oldest_bucket_time` cached timestamp of the oldest 12-hour return bucket,
// - `pending_bucket_proceeds` proceeds in the pending 12-hour return bucket,
// - `oldest_bucket_time` cached timestamp of the oldest 12-hour return bucket,
// - `pending_bucket_proceeds` proceeds in the pending 12-hour return bucket,
// - `current_rate_of_increase` the current rate per dist_interval at which proceeds are added to the rex pool,
// - `proceeds` the maximum amount of proceeds that can be added to the rex pool at any given time
struct [[eosio::table,eosio::contract("eosio.system")]] rex_return_pool {
Expand All @@ -357,7 +359,7 @@ namespace eosiosystem {

// `rex_return_buckets` structure underlying the rex return buckets table. A rex return buckets table is defined by:
// - `version` defaulted to zero,
// - `return_buckets` buckets of proceeds accumulated in 12-hour intervals
// - `return_buckets` buckets of proceeds accumulated in 12-hour intervals
struct [[eosio::table,eosio::contract("eosio.system")]] rex_return_buckets {
uint8_t version = 0;
std::map<time_point_sec, int64_t> return_buckets;
Expand Down Expand Up @@ -458,19 +460,138 @@ namespace eosiosystem {
asset stake_change;
};

/**
* The `eosio.system` smart contract is provided by `block.one` as a sample system contract, and it defines the structures and actions needed for blockchain's core functionality.
*
* Just like in the `eosio.bios` sample contract implementation, there are a few actions which are not implemented at the contract level (`newaccount`, `updateauth`, `deleteauth`, `linkauth`, `unlinkauth`, `canceldelay`, `onerror`, `setabi`, `setcode`), they are just declared in the contract so they will show in the contract's ABI and users will be able to push those actions to the chain via the account holding the `eosio.system` contract, but the implementation is at the EOSIO core level. They are referred to as EOSIO native actions.
*
* - Users can stake tokens for CPU and Network bandwidth, and then vote for producers or
* delegate their vote to a proxy.
* - Producers register in order to be voted for, and can claim per-block and per-vote rewards.
* - Users can buy and sell RAM at a market-determined price.
* - Users can bid on premium names.
* - A resource exchange system (REX) allows token holders to lend their tokens,
* and users to rent CPU and Network resources in return for a market-determined fee.
*/
struct rentbw_config_resource {
std::optional<int64_t> current_weight_ratio; // Immediately set weight_ratio to this amount. 1x = 10^15. 0.01x = 10^13.
// Do not specify to preserve the existing setting or use the default;
// this avoids sudden price jumps. For new chains which don't need
// to gradually phase out staking and REX, 0.01x (10^13) is a good
// value for both current_weight_ratio and target_weight_ratio.
std::optional<int64_t> target_weight_ratio; // Linearly shrink weight_ratio to this amount. 1x = 10^15. 0.01x = 10^13.
// Do not specify to preserve the existing setting or use the default.
std::optional<int64_t> assumed_stake_weight; // Assumed stake weight for ratio calculations. Use the sum of total
// staked and total rented by REX at the time the rentbw market
// is first activated. Do not specify to preserve the existing
// setting (no default exists); this avoids sudden price jumps.
// For new chains which don't need to phase out staking and REX,
// 10^12 is probably a good value.
std::optional<time_point_sec> target_timestamp; // Stop automatic weight_ratio shrinkage at this time. Once this
// time hits, weight_ratio will be target_weight_ratio. Ignored
// if current_weight_ratio == target_weight_ratio. Do not specify
// this to preserve the existing setting (no default exists).
std::optional<double> exponent; // Exponent of resource price curve. Must be >= 1. Do not specify
// to preserve the existing setting or use the default.
std::optional<uint32_t> decay_secs; // Number of seconds for the gap between adjusted resource
// utilization and instantaneous resource utilization to shrink
// by 63%. Do not specify to preserve the existing setting or
// use the default.
std::optional<asset> min_price; // Fee needed to rent the entire resource market weight at the
// minimum price. For example, this could be set to 0.005% of
// total token supply. Do not specify to preserve the existing
// setting or use the default.
std::optional<asset> max_price; // Fee needed to rent the entire resource market weight at the
// maximum price. For example, this could be set to 10% of total
// token supply. Do not specify to preserve the existing
// setting (no default exists).

EOSLIB_SERIALIZE( rentbw_config_resource, (current_weight_ratio)(target_weight_ratio)(assumed_stake_weight)
(target_timestamp)(exponent)(decay_secs)(min_price)(max_price) )
};

struct rentbw_config {
rentbw_config_resource net; // NET market configuration
rentbw_config_resource cpu; // CPU market configuration
std::optional<uint32_t> rent_days; // `rentbw` `days` argument must match this. Do not specify to preserve the
// existing setting or use the default.
std::optional<asset> min_rent_fee; // Rental fees below this amount are rejected. Do not specify to preserve the
// existing setting (no default exists).

EOSLIB_SERIALIZE( rentbw_config, (net)(cpu)(rent_days)(min_rent_fee) )
};

struct rentbw_state_resource {
static constexpr double default_exponent = 2.0; // Exponent of 2.0 means that the price to rent a
// tiny amount of resources increases linearly
// with utilization.
static constexpr uint32_t default_decay_secs = 1 * seconds_per_day; // 1 day; if 100% of bandwidth resources are in a
// single loan, then, assuming no further renting,
// 1 day after it expires the adjusted utilization
// will be at approximately 37% and after 3 days
// the adjusted utilization will be less than 5%.

uint8_t version = 0;
int64_t weight = 0; // resource market weight. calculated; varies over time.
// 1 represents the same amount of resources as 1
// satoshi of SYS staked.
int64_t weight_ratio = 0; // resource market weight ratio:
// assumed_stake_weight / (assumed_stake_weight + weight).
// calculated; varies over time. 1x = 10^15. 0.01x = 10^13.
int64_t assumed_stake_weight = 0; // Assumed stake weight for ratio calculations.
int64_t initial_weight_ratio = rentbw_frac; // Initial weight_ratio used for linear shrinkage.
int64_t target_weight_ratio = rentbw_frac / 100; // Linearly shrink the weight_ratio to this amount.
time_point_sec initial_timestamp = {}; // When weight_ratio shrinkage started
time_point_sec target_timestamp = {}; // Stop automatic weight_ratio shrinkage at this time. Once this
// time hits, weight_ratio will be target_weight_ratio.
double exponent = default_exponent; // Exponent of resource price curve.
uint32_t decay_secs = default_decay_secs; // Number of seconds for the gap between adjusted resource
// utilization and instantaneous utilization to shrink by 63%.
asset min_price = {}; // Fee needed to rent the entire resource market weight at
// the minimum price (defaults to 0).
asset max_price = {}; // Fee needed to rent the entire resource market weight at
// the maximum price.
int64_t utilization = 0; // Instantaneous resource utilization. This is the current
// amount sold. utilization <= weight.
int64_t adjusted_utilization = 0; // Adjusted resource utilization. This is >= utilization and
// <= weight. It grows instantly but decays exponentially.
time_point_sec utilization_timestamp = {}; // When adjusted_utilization was last updated
};

struct [[eosio::table("rent.state"),eosio::contract("eosio.system")]] rentbw_state {
static constexpr uint32_t default_rent_days = 30; // 30 day resource rentals

uint8_t version = 0;
rentbw_state_resource net = {}; // NET market state
rentbw_state_resource cpu = {}; // CPU market state
uint32_t rent_days = default_rent_days; // `rentbw` `days` argument must match this.
asset min_rent_fee = {}; // Rental fees below this amount are rejected

uint64_t primary_key()const { return 0; }
};

typedef eosio::singleton<"rent.state"_n, rentbw_state> rentbw_state_singleton;

struct [[eosio::table("rentbw.order"),eosio::contract("eosio.system")]] rentbw_order {
uint8_t version = 0;
uint64_t id;
name owner;
int64_t net_weight;
int64_t cpu_weight;
time_point_sec expires;

uint64_t primary_key()const { return id; }
uint64_t by_owner()const { return owner.value; }
uint64_t by_expires()const { return expires.utc_seconds; }
};

typedef eosio::multi_index< "rentbw.order"_n, rentbw_order,
indexed_by<"byowner"_n, const_mem_fun<rentbw_order, uint64_t, &rentbw_order::by_owner>>,
indexed_by<"byexpires"_n, const_mem_fun<rentbw_order, uint64_t, &rentbw_order::by_expires>>
> rentbw_order_table;


/**
* The `eosio.system` smart contract is provided by `block.one` as a sample system contract, and it defines the structures and actions needed for blockchain's core functionality.
*
* Just like in the `eosio.bios` sample contract implementation, there are a few actions which are not implemented at the contract level (`newaccount`, `updateauth`, `deleteauth`, `linkauth`, `unlinkauth`, `canceldelay`, `onerror`, `setabi`, `setcode`), they are just declared in the contract so they will show in the contract's ABI and users will be able to push those actions to the chain via the account holding the `eosio.system` contract, but the implementation is at the EOSIO core level. They are referred to as EOSIO native actions.
*
* - Users can stake tokens for CPU and Network bandwidth, and then vote for producers or
* delegate their vote to a proxy.
* - Producers register in order to be voted for, and can claim per-block and per-vote rewards.
* - Users can buy and sell RAM at a market-determined price.
* - Users can bid on premium names.
* - A resource exchange system (REX) allows token holders to lend their tokens,
* and users to rent CPU and Network resources in return for a market-determined fee.
* - A resource market separate from REX: `rentbw`.
*/
class [[eosio::contract("eosio.system")]] system_contract : public native {

private:
Expand Down Expand Up @@ -504,6 +625,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 reserv_account{"eosio.reserv"_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);
Expand Down Expand Up @@ -1121,6 +1243,36 @@ namespace eosiosystem {
[[eosio::action]]
void setinflation( int64_t annual_rate, int64_t inflation_pay_factor, int64_t votepay_factor );

/**
* Configure the `rentbw` market. The market becomes available the first time this
* action is invoked.
*/
[[eosio::action]]
void configrentbw( rentbw_config& args );

/**
* Process rentbw queue and update state. Action does not execute anything related to a specific user.
*
* @param user - any account can execute this action
* @param max - number of queue items to process
*/
[[eosio::action]]
void rentbwexec( const name& user, uint16_t max );

/**
* Rent NET and CPU
*
* @param payer - the resource buyer
* @param receiver - the resource receiver
* @param days - number of days of resource availability. Must match market configuration.
* @param net_frac - fraction of net (100% = 10^15) managed by this market
* @param cpu_frac - fraction of cpu (100% = 10^15) managed by this market
* @param max_payment - the maximum amount `payer` is willing to pay. Tokens are withdrawn from
* `payer`'s token balance.
*/
[[eosio::action]]
void rentbw( const name& payer, const name& receiver, uint32_t days, int64_t net_frac, int64_t cpu_frac, const asset& max_payment );

using init_action = eosio::action_wrapper<"init"_n, &system_contract::init>;
using setacctram_action = eosio::action_wrapper<"setacctram"_n, &system_contract::setacctram>;
using setacctnet_action = eosio::action_wrapper<"setacctnet"_n, &system_contract::setacctnet>;
Expand Down Expand Up @@ -1167,6 +1319,9 @@ namespace eosiosystem {
using setalimits_action = eosio::action_wrapper<"setalimits"_n, &system_contract::setalimits>;
using setparams_action = eosio::action_wrapper<"setparams"_n, &system_contract::setparams>;
using setinflation_action = eosio::action_wrapper<"setinflation"_n, &system_contract::setinflation>;
using configrentbw_action = eosio::action_wrapper<"configrentbw"_n, &system_contract::configrentbw>;
using rentbwexec_action = eosio::action_wrapper<"rentbwexec"_n, &system_contract::rentbwexec>;
using rentbw_action = eosio::action_wrapper<"rentbw"_n, &system_contract::rentbw>;

private:
// Implementation details:
Expand All @@ -1191,7 +1346,7 @@ namespace eosiosystem {
const char* error_msg = "must vote for at least 21 producers or for a proxy before buying REX" )const;
rex_order_outcome fill_rex_order( const rex_balance_table::const_iterator& bitr, const asset& rex );
asset update_rex_account( const name& owner, const asset& proceeds, const asset& unstake_quant, bool force_vote_update = false );
void channel_to_rex( const name& from, const asset& amount );
void channel_to_rex( const name& from, const asset& amount, bool required = false );
void channel_namebid_to_rex( const int64_t highest_bid );
template <typename T>
int64_t rent_rex( T& table, const name& from, const name& receiver, const asset& loan_payment, const asset& loan_fund );
Expand Down Expand Up @@ -1267,6 +1422,13 @@ namespace eosiosystem {
};

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

// defined in rentbw.cpp
void adjust_resources(name payer, name account, symbol core_symbol, int64_t net_delta, int64_t cpu_delta, bool must_not_be_managed = false);
void process_rentbw_queue(
time_point_sec now, symbol core_symbol, rentbw_state& state,
rentbw_order_table& orders, uint32_t max_items, int64_t& net_delta_available,
int64_t& cpu_delta_available);
};

}
Loading