Skip to content

Commit

Permalink
Merge branch 'issue_5' into 'beos-initial-release'
Browse files Browse the repository at this point in the history
Issue 5

Closes #5

See merge request blocktrades/beos-core!24
  • Loading branch information
vogel76 committed Nov 9, 2018
2 parents 2eda4a6 + 61d3786 commit 5fb303a
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 247 deletions.
2 changes: 1 addition & 1 deletion contracts/beoslib/beos_privileged.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" {
/**
* Defined in distribution_api
*/
void reward_all(uint32_t, const void*, int, bool);
void reward_all(uint32_t, uint64_t, const void*, int, bool);
void reward_done(const void*, int, bool);

#ifdef __cplusplus
Expand Down
73 changes: 8 additions & 65 deletions contracts/eosio.distribution/eosio.distribution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ distribution::~distribution()
{
}

void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution,
uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode )
void distribution::execute( uint32_t block_nr, asset proxy_asset,
uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution,
uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode )
{
//Only during a distribution period, an action can be called.
if( block_nr >= starting_block_for_any_distribution && block_nr <= ending_block_for_any_distribution )
Expand All @@ -25,8 +26,10 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t start
if( ( ( block_nr - starting_block_for_any_distribution ) % distribution_payment_block_interval_for_any_distribution ) == 0 )
{
//Rewarding all accounts.
rewardall( nr_items, proxy_asset, is_beos_mode );
//reward_all( nr_items, &proxy_asset, sizeof(asset), is_beos_mode );
uint64_t gathered_amount = get_sum();

if (gathered_amount != 0)
reward_all( nr_items, gathered_amount, &proxy_asset, sizeof(asset), is_beos_mode );

//Total end of distribution period. Transferring from staked BEOS/RAM to liquid BEOS/RAM.
//It depends on `is_beos_mode` variable.
Expand All @@ -35,8 +38,7 @@ void distribution::execute( uint32_t block_nr, asset proxy_asset, uint32_t start
( block_nr + distribution_payment_block_interval_for_any_distribution > ending_block_for_any_distribution )
)
{
rewarddone( proxy_asset, is_beos_mode );
//reward_done( &proxy_asset, sizeof(asset), is_beos_mode );
reward_done( &proxy_asset, sizeof(asset), is_beos_mode );
}
}
}
Expand Down Expand Up @@ -68,65 +70,6 @@ uint64_t distribution::get_sum()
return issued - withdrawn;
}

void distribution::rewardall( uint32_t total_amount, asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode )
{
//Retrieve total sum of balances for every account.
uint64_t gathered_amount = get_sum();

if( gathered_amount == 0 )
return;

auto callable = [&]( const init_data& obj )
{
auto balance = eosio::token( N(eosio.token) ).check_balance( obj.owner, symbol.symbol );
//Calculation ratio for given account.
long double ratio = static_cast< long double >( balance.amount ) / gathered_amount;
int64_t val = static_cast< int64_t >( total_amount * ratio );

if( val > 0 )
{
if( is_beos_mode )
{
//Staking BEOS for user `obj.owner` always during every reward-time.
auto from = N(beos.distrib);

auto stake_net = asset( val / 2 );
auto stake_cpu = asset( val - stake_net.amount );

INLINE_ACTION_SENDER(eosiosystem::system_contract, reward)( N(eosio), {N(eosio),N(active)},
{ obj.owner, 0/*ram_bytes*/, stake_net/*net_weight*/, stake_cpu/*cpu_weight*/ } );

}
else
{
INLINE_ACTION_SENDER(eosiosystem::system_contract, reward)( N(eosio), {N(eosio),N(active)},
{ obj.owner, val/*ram_bytes*/, asset()/*net_weight*/, asset()/*cpu_weight*/ } );
}
}

};

//Review all accounts
review( callable );
}

void distribution::rewarddone( asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode )
{
}

void distribution::review( ConstModifier&& mod )
{
inits _init( N(beos.gateway), N(beos.gateway) );
auto itr = _init.begin();

//Go through all accounts and do specific action `mod`.
while( itr != _init.end() )
{
mod( *itr );
++itr;
}
}

} /// namespace eosio

EOSIO_ABI( eosio::distribution, (onblock) )
4 changes: 0 additions & 4 deletions contracts/eosio.distribution/eosio.distribution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ namespace eosio {
using ConstModifier = std::function< void( const init_data& a )>;

uint64_t get_sum();
void review( ConstModifier&& mod );

void execute( uint32_t block_nr, asset proxy_asset, uint32_t starting_block_for_any_distribution, uint32_t ending_block_for_any_distribution,
uint32_t distribution_payment_block_interval_for_any_distribution, uint32_t nr_items, bool is_beos_mode );

void rewardall( uint32_t total_amount, asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode );
void rewarddone( asset symbol/*correct symbol of BEOS coin, for example: `0.0000 BEOS`*/, bool is_beos_mode );

public:

distribution( account_name self );
Expand Down
99 changes: 6 additions & 93 deletions contracts/eosio.system/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ namespace eosiosystem {
* These tables are designed to be constructed in the scope of the relevant user, this
* facilitates simpler API for per-user queries
*/
typedef eosio::multi_index< N(userres), user_resources> user_resources_table;
typedef eosio::multi_index< N(delband), delegated_bandwidth> del_bandwidth_table;
typedef eosio::multi_index< N(refunds), refund_request> refunds_table;

Expand Down Expand Up @@ -149,81 +148,26 @@ namespace eosiosystem {
_gstate.total_ram_bytes_reserved += uint64_t(bytes_out);
_gstate.total_ram_stake += quant_after_fee.amount;

user_resources_table userres( _self, receiver );
auto res_itr = userres.find( receiver );
if( res_itr == userres.end() ) {
res_itr = userres.emplace( receiver, [&]( auto& res ) {
res.owner = receiver;
res.ram_bytes = bytes_out;
});
} else {
userres.modify( res_itr, receiver, [&]( auto& res ) {
res.ram_bytes += bytes_out;
});
}
set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );
change_resource_limits( receiver, bytes_out, 0, 0 );
}

void system_contract::initresource( account_name receiver, int64_t bytes, asset stake_net_quantity, asset stake_cpu_quantity )
{
require_auth( _self );

//Delegate RAM from payer to receiver
user_resources_table userres( _self, receiver );

auto res_itr = userres.find( receiver );

if( res_itr == userres.end() ) {
res_itr = userres.emplace( receiver, [&]( auto& res ) {
res.owner = receiver;
res.ram_bytes = bytes;
res.net_weight = stake_net_quantity;
res.cpu_weight = stake_cpu_quantity;
});
} else {
userres.modify( res_itr, receiver, [&]( auto& res ) {
res.ram_bytes += bytes;
res.net_weight += stake_net_quantity;
res.cpu_weight += stake_cpu_quantity;
});
}

set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );
change_resource_limits( receiver, bytes, stake_net_quantity.amount, stake_cpu_quantity.amount );
}

void system_contract::delegateram( account_name payer, account_name receiver, int64_t bytes )
{
require_auth( payer );

//Check amount of RAM for payer
user_resources_table userres_payer( _self, payer );
auto res_itr = userres_payer.find( payer );
eosio_assert( res_itr != userres_payer.end(), "payer must exist" );

eosio_assert( res_itr->ram_bytes >= bytes, "not enough RAM" );

//Decrease amount of RAM for payer
userres_payer.modify( res_itr, payer, [&]( auto& res ) {
res.ram_bytes -= bytes;
});
change_resource_limits( payer, -bytes, 0, 0 );

//Delegate RAM from payer to receiver
user_resources_table userres( _self, receiver );

res_itr = userres.find( receiver );

if( res_itr == userres.end() ) {
res_itr = userres.emplace( receiver, [&]( auto& res ) {
res.owner = receiver;
res.ram_bytes = bytes;
});
} else {
userres.modify( res_itr, receiver, [&]( auto& res ) {
res.ram_bytes += bytes;
});
}

set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );
change_resource_limits( receiver, bytes, 0, 0 );
}

/**
Expand All @@ -239,11 +183,6 @@ namespace eosiosystem {
require_auth( account );
eosio_assert( bytes > 0, "cannot sell negative byte" );

user_resources_table userres( _self, account );
auto res_itr = userres.find( account );
eosio_assert( res_itr != userres.end(), "no resource row" );
eosio_assert( res_itr->ram_bytes >= bytes, "insufficient quota" );

asset tokens_out;
auto itr = _rammarket.find(S(4,RAMCORE));
_rammarket.modify( itr, 0, [&]( auto& es ) {
Expand All @@ -259,10 +198,7 @@ namespace eosiosystem {
//// this shouldn't happen, but just in case it does we should prevent it
eosio_assert( _gstate.total_ram_stake >= 0, "error, attempt to unstake more tokens than previously staked" );

userres.modify( res_itr, account, [&]( auto& res ) {
res.ram_bytes -= bytes;
});
set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );
change_resource_limits( account, -bytes, 0, 0 );

INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {N(eosio.ram),N(active)},
{ N(eosio.ram), account, asset(tokens_out), std::string("sell ram") } );
Expand Down Expand Up @@ -324,30 +260,7 @@ namespace eosiosystem {
} // itr can be invalid, should go out of scope

// update totals of "receiver"
{
user_resources_table totals_tbl( _self, receiver );
auto tot_itr = totals_tbl.find( receiver );
if( tot_itr == totals_tbl.end() ) {
tot_itr = totals_tbl.emplace( from, [&]( auto& tot ) {
tot.owner = receiver;
tot.net_weight = stake_net_delta;
tot.cpu_weight = stake_cpu_delta;
});
} else {
totals_tbl.modify( tot_itr, from == receiver ? from : 0, [&]( auto& tot ) {
tot.net_weight += stake_net_delta;
tot.cpu_weight += stake_cpu_delta;
});
}
eosio_assert( asset(0) <= tot_itr->net_weight, "insufficient staked total net bandwidth" );
eosio_assert( asset(0) <= tot_itr->cpu_weight, "insufficient staked total cpu bandwidth" );

set_resource_limits( receiver, tot_itr->ram_bytes, tot_itr->net_weight.amount, tot_itr->cpu_weight.amount );

if ( tot_itr->net_weight == asset(0) && tot_itr->cpu_weight == asset(0) && tot_itr->ram_bytes == 0 ) {
totals_tbl.erase( tot_itr );
}
} // tot_itr can be invalid, should go out of scope
change_resource_limits( receiver, 0, stake_net_delta.amount, stake_cpu_delta.amount );

// create refund or update from existing refund
if ( N(eosio.stake) != source_stake_from ) { //for eosio both transfer and refund make no sense
Expand Down
6 changes: 0 additions & 6 deletions contracts/eosio.system/eosio.system.abi
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,6 @@
"index_type": "i64",
"key_names" : ["owner"],
"key_types" : ["account_name"]
},{
"name": "userres",
"type": "user_resources",
"index_type": "i64",
"key_names" : ["owner"],
"key_types" : ["uint64"]
},{
"name": "delband",
"type": "delegated_bandwidth",
Expand Down
27 changes: 1 addition & 26 deletions contracts/eosio.system/eosio.system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,7 @@ namespace eosiosystem {

void system_contract::reward( account_name receiver, int64_t ram_bytes, asset net_weight, asset cpu_weight )
{
user_resources_table userres( _self, receiver );

auto res_itr = userres.find( receiver );

if( res_itr == userres.end() ) {
res_itr = userres.emplace( receiver, [&]( auto& res ) {
res.owner = receiver;
res.ram_bytes = ram_bytes;
res.net_weight = net_weight;
res.cpu_weight = cpu_weight;
});
} else {
userres.modify( res_itr, receiver, [&]( auto& res ) {
res.ram_bytes += ram_bytes;
res.net_weight += net_weight;
res.cpu_weight += cpu_weight;
});
}

set_resource_limits( res_itr->owner, res_itr->ram_bytes, res_itr->net_weight.amount, res_itr->cpu_weight.amount );
change_resource_limits( receiver, ram_bytes, net_weight.amount, cpu_weight.amount );
}

/**
Expand Down Expand Up @@ -196,12 +177,6 @@ namespace eosiosystem {
}
}

user_resources_table userres( _self, newact);

userres.emplace( newact, [&]( auto& res ) {
res.owner = newact;
});

set_resource_limits( newact, 0, 0, 0 );
}

Expand Down
10 changes: 10 additions & 0 deletions contracts/eosiolib/privileged.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ extern "C" {
*/
void set_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight );

/**
* @brief Change the resource limits of an account
* Change the resource limits of an account
* @param account - name of the account whose resource limit to be change
* @param ram_bytes - ram delta limit in absolute bytes
* @param net_weight - fractionally proportionate net delta limit of available resources based on (weight / total_weight_of_all_accounts)
* @param cpu_weight - fractionally proportionate cpu delta limit of available resources based on (weight / total_weight_of_all_accounts)
*/
void change_resource_limits( account_name account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight );

/**
* Proposes a schedule change, once the block that contains the proposal becomes irreversible, the schedule is promoted to "pending" automatically. Once the block that promotes the schedule is irreversible, the schedule will become "active"
* @param producer_data - packed data of produce_keys in the appropriate producer schedule order
Expand Down
Loading

0 comments on commit 5fb303a

Please sign in to comment.