Skip to content

Commit

Permalink
Feature adjustable account price (#119)
Browse files Browse the repository at this point in the history
* wip

* PoC

* review

* review

* add tests

Co-authored-by: Mark <[email protected]>
Co-authored-by: Vlad Pavlichek <[email protected]>
  • Loading branch information
3 people authored and roman-tik committed Jan 8, 2020
1 parent 456dab5 commit 68fb56b
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ namespace eosiosystem {
static constexpr uint32_t blocks_per_day = 2 * seconds_per_day; // half seconds per day

static constexpr int64_t min_activated_stake = 150'000'000'0000;
static constexpr int64_t ram_gift_bytes = 1400;
static constexpr int64_t min_pervote_daily_pay = 100'0000;
static constexpr uint32_t refund_delay_sec = 3 * seconds_per_day;

Expand All @@ -76,6 +75,10 @@ namespace eosiosystem {
static constexpr int64_t default_inflation_pay_factor = 50000; // producers pay share = 10000 / 50000 = 20% of the inflation
static constexpr int64_t default_votepay_factor = 40000; // per-block pay share = 10000 / 40000 = 25% of the producer pay

// 3742 bytes - is size of the new user account for current version
static constexpr int64_t min_account_ram = 3800;


/**
*
* @defgroup eosiosystem rem.system
Expand Down Expand Up @@ -1527,6 +1530,9 @@ namespace eosiosystem {
// to keep Guardian status, account should reassert its vote every eosio_global_rem_state::reassertion_period
bool vote_is_reasserted( eosio::time_point last_reassertion_time ) const;

// calculates amount of free bytes for current stake
int64_t ram_gift_bytes( int64_t stake ) const;

//defined in rotation.cpp
std::vector<eosio::producer_authority> get_rotated_schedule();

Expand Down
12 changes: 10 additions & 2 deletions contracts/contracts/rem.system/src/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ namespace eosiosystem {
int64_t cpu = 0;
get_resource_limits( receiver, ram_bytes, net, cpu );

const auto system_token_max_supply = eosio::token::get_max_supply(token_account, system_contract::get_core_symbol().code() );
const auto system_token_max_supply = eosio::token::get_max_supply(token_account, system_contract::get_core_symbol().code() );
const double bytes_per_token = (double)_gstate.max_ram_size / (double)system_token_max_supply.amount;
int64_t bytes_for_stake = bytes_per_token * (tot_itr->own_stake_amount + tot_itr->free_stake_amount);
const int64_t staked = tot_itr->own_stake_amount + tot_itr->free_stake_amount;
const int64_t bytes_for_stake = bytes_per_token * staked + ram_gift_bytes( staked );
set_resource_limits( receiver,
ram_managed ? ram_bytes : bytes_for_stake,
net_managed ? net : tot_itr->net_weight.amount + tot_itr->free_stake_amount,
Expand Down Expand Up @@ -319,4 +320,11 @@ namespace eosiosystem {
refunds_tbl.erase( req );
}
}

int64_t system_contract::ram_gift_bytes( int64_t stake ) const {
const auto system_token_max_supply = eosio::token::get_max_supply(token_account, system_contract::get_core_symbol().code() );
const double bytes_per_token = (double)_gstate.max_ram_size / (double)system_token_max_supply.amount;

return std::max( int64_t{0}, min_account_ram - static_cast< int64_t >(stake * bytes_per_token) );
}
} //namespace eosiosystem
152 changes: 152 additions & 0 deletions unittests/rem_account_price_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <boost/test/unit_test.hpp>
#pragma GCC diagnostic pop

#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/testing/tester.hpp>

#include <fc/exception/exception.hpp>
#include <fc/variant_object.hpp>

#include <contracts.hpp>

#include "eosio_system_tester.hpp"

BOOST_AUTO_TEST_SUITE(rem_account_price_tests)
BOOST_FIXTURE_TEST_CASE(rem_account_price_test, rem_system::eosio_system_tester) {
try {
cross_15_percent_threshold();
produce_blocks(10);

// everyone should be able to create account with given price
{
const int64_t account_price = 50'0000;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser111),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}

// everyone should be able to create account with given price
{
const int64_t account_price = 10'0000;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser222),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}

// everyone should be able to create account with given price
{
const int64_t account_price = 1'0000;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser333),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}

// everyone should be able to create account with given price
{
const int64_t account_price = 1;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser335),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}

// ram_gift_bytes should be equal 0 in this tests
// everyone should be able to create account with given price
{
const int64_t account_price = 100'0000;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser444),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}

// everyone should be able to create account with given price
{
const int64_t account_price = 1000'0000;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser555),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}

// everyone should be able to create account with given price
{
const int64_t account_price = 10000'0000;
push_action( config::system_account_name, N(setminstake), mvo()("min_account_stake", account_price) );

const auto min_account_stake = get_global_state()["min_account_stake"].as<int64_t>();
BOOST_REQUIRE_EQUAL(min_account_stake, account_price);

create_account_with_resources(
N(testuser511),
config::system_account_name,
false,
asset{ account_price }
);

produce_blocks(1);
}
} FC_LOG_AND_RETHROW()
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 68fb56b

Please sign in to comment.