Skip to content

Commit

Permalink
Merge pull request #15 from EOSLaoMao/hotfix/creditor-rotation
Browse files Browse the repository at this point in the history
Hotfix/creditor rotation
  • Loading branch information
datudou authored Oct 21, 2018
2 parents cb5e0e9 + b8d8f8b commit fc74205
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 56 deletions.
4 changes: 2 additions & 2 deletions include/bankofstaked/bankofstaked.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file bankofmemory.hpp
* @file bankofstaked.hpp
*/
#include <eosiolib/asset.hpp>
#include <eosiolib/eosio.hpp>
Expand All @@ -22,7 +22,7 @@ static const uint64_t TRUE = 1;
static const uint64_t FALSE = 0;
static const uint64_t CHECK_MAX_DEPTH = 3;
static const uint64_t MAX_EOS_BALANCE = 500 * 10000; // 500 EOS at most
static const uint64_t MIN_CREDITOR_BALANCE = 10 * 10000; // 10 EOS at least
static const uint64_t MIN_FREE_CREDITOR_BALANCE = 10 * 10000; // 10 EOS at least

// To protect your table, you can specify different scope as random numbers
static const uint64_t SCOPE_ORDER = 1842919517374;
Expand Down
37 changes: 1 addition & 36 deletions src/bankofstaked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,42 +255,7 @@ class bankofstaked : contract
void activate(account_name account)
{
require_auth(code_account);
creditor_table c(code_account, SCOPE_CREDITOR>>1);

auto creditor = c.find(account);
//make sure specified creditor exists
eosio_assert(creditor != c.end(), "account not found in creditor table");

//activate creditor, deactivate others
auto itr = c.end();
while (itr != c.begin())
{
itr--;
if (itr->for_free != creditor->for_free)
{
continue;
}

if(itr->account==creditor->account) {
c.modify(itr, ram_payer, [&](auto &i) {
i.is_active = TRUE;
i.balance = get_balance(itr->account);
i.updated_at = now();
});
}
else
{
if(itr->is_active == FALSE)
{
continue;
}
c.modify(itr, ram_payer, [&](auto &i) {
i.is_active = FALSE;
i.balance = get_balance(itr->account);
i.updated_at = now();
});
}
}
activate_creditor(account);
}


Expand Down
90 changes: 72 additions & 18 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,49 +59,103 @@ namespace utils
return balance;
}

void activate_creditor(account_name account)
{
creditor_table c(code_account, SCOPE_CREDITOR>>1);

auto creditor = c.find(account);
//make sure specified creditor exists
eosio_assert(creditor != c.end(), "account not found in creditor table");

//activate creditor, deactivate others
auto itr = c.end();
while (itr != c.begin())
{
itr--;
if (itr->for_free != creditor->for_free)
{
continue;
}

if(itr->account==creditor->account) {
c.modify(itr, ram_payer, [&](auto &i) {
i.is_active = TRUE;
i.balance = get_balance(itr->account);
i.updated_at = now();
});
}
else
{
if(itr->is_active == FALSE)
{
continue;
}
c.modify(itr, ram_payer, [&](auto &i) {
i.is_active = FALSE;
i.balance = get_balance(itr->account);
i.updated_at = now();
});
}
}
}

//get min paid creditor balance
uint64_t get_min_paid_creditor_balance()
{

uint64_t balance = 10000 * 10000; // 10000 EOS
plan_table p(code_account, code_account);
eosio_assert(p.begin() != p.end(), "plan table is empty!");
auto itr = p.begin();
while (itr != p.end())
{
auto required = itr->cpu.amount + itr->net.amount;
if (itr->is_free == false && itr->is_active && required < balance) {
balance = required;
}
itr++;
}
return balance;
}

//rotate active creditor
void rotate_creditor()
{
creditor_table c(code_account, SCOPE_CREDITOR>>1);
auto free_creditor = get_active_creditor(TRUE);
auto paid_creditor = get_active_creditor(FALSE);

auto idx = c.get_index<N(updated_at)>();
auto itr = idx.begin();
asset free_balance = get_balance(free_creditor);
asset paid_balance = get_balance(paid_creditor);
auto free_rotated = free_balance.amount > MIN_CREDITOR_BALANCE ?TRUE:FALSE;
auto paid_rotated = paid_balance.amount > MIN_CREDITOR_BALANCE ?TRUE:FALSE;

uint64_t min_paid_creditor_balance = get_min_paid_creditor_balance();
auto free_rotated = free_balance.amount > MIN_FREE_CREDITOR_BALANCE ?TRUE:FALSE;
auto paid_rotated = paid_balance.amount > min_paid_creditor_balance ?TRUE:FALSE;
auto idx = c.get_index<N(updated_at)>();
auto itr = idx.begin();
while (itr != idx.end())
{
if(itr->for_free == TRUE)
{
if(free_rotated == TRUE){itr++;continue;}
if (itr->account != free_creditor)
auto balance = get_balance(itr->account);
if (itr->account != free_creditor && balance.amount > MIN_FREE_CREDITOR_BALANCE)
{
idx.modify(itr, ram_payer, [&](auto &i) {
i.is_active = TRUE;
i.balance = get_balance(itr->account);
i.updated_at = now();
});
activate_creditor(itr->account);
free_rotated = TRUE;
}
itr++;
}
else
{
if(paid_rotated == TRUE){itr++;continue;}
if (itr->account != paid_creditor)
auto balance = get_balance(itr->account);
if (itr->account != paid_creditor && balance.amount > min_paid_creditor_balance)
{
idx.modify(itr, ram_payer, [&](auto &i) {
i.is_active = TRUE;
i.balance = get_balance(itr->account);
i.updated_at = now();
});
activate_creditor(itr->account);
paid_rotated = TRUE;
}
itr++;
}

}
}
}

0 comments on commit fc74205

Please sign in to comment.