Skip to content

Commit

Permalink
Merge pull request #43 from EOSLaoMao/hotfix/reduce-table-write
Browse files Browse the repository at this point in the history
get_balance optimized to reduce cpu cost
  • Loading branch information
JohnnyZhao authored Dec 18, 2018
2 parents 891be0f + aa20df2 commit 4539bf6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 5 additions & 9 deletions src/bankofstaked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class bankofstaked : contract
undelegate(order_ids, 0);
expire_freelock();
rotate_creditor();
get_balance(creditor);
update_balance(creditor);
}

// @abi action forcexpire
Expand Down Expand Up @@ -192,13 +192,12 @@ class bankofstaked : contract
// updated cpu_staked/net_staked/cpu_unstaked/net_unstaked of creditor entry
creditor_table c(CODE_ACCOUNT, SCOPE);
auto creditor_itr = c.find(order->creditor);
asset balance = get_balance(order->creditor);
c.modify(creditor_itr, RAM_PAYER, [&](auto &i) {
i.cpu_staked -= order->cpu_staked;
i.net_staked -= order->net_staked;
i.cpu_unstaked += order->cpu_staked;
i.net_unstaked += order->net_staked;
i.balance = balance;
i.balance = get_balance(order->creditor);
i.updated_at = now();
});

Expand Down Expand Up @@ -254,13 +253,12 @@ class bankofstaked : contract
auto itr = c.find(account);
eosio_assert(itr == c.end(), "account already exist in creditor table");

asset balance = get_balance(account);
c.emplace(RAM_PAYER, [&](auto &i) {
i.is_active = FALSE;
i.for_free = for_free?TRUE:FALSE;
i.free_memo = for_free?free_memo:"";
i.account = account;
i.balance = balance;
i.balance = get_balance(account);
i.created_at = now();
i.updated_at = 0; // set to 0 for creditor auto rotation
});
Expand Down Expand Up @@ -552,8 +550,7 @@ class bankofstaked : contract
if(plan->is_free == FALSE)
{
asset to_delegate = plan->cpu + plan->net;
asset balance = get_balance(creditor);
if(balance < to_delegate) {
if(get_balance(creditor) < to_delegate) {
creditor = get_qualified_paid_creditor(to_delegate);
}
}
Expand Down Expand Up @@ -587,13 +584,12 @@ class bankofstaked : contract
(CODE_ACCOUNT, {{CODE_ACCOUNT, N(bankperm)}}, {creditor});

// add cpu_staked&net_staked to creditor entry
asset balance = get_balance(creditor);
creditor_table c(CODE_ACCOUNT, SCOPE);
auto creditor_itr = c.find(creditor);
c.modify(creditor_itr, RAM_PAYER, [&](auto &i) {
i.cpu_staked += plan->cpu;
i.net_staked += plan->net;
i.balance = balance;
i.balance = get_balance(creditor);
i.updated_at = now();
});

Expand Down
11 changes: 10 additions & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ namespace utils
auto symbol = symbol_type(system_token_symbol);
eosio::token t(N(eosio.token));
auto balance = t.get_balance(owner, symbol.name());
// update creditor if balance is outdated
return balance;
}

//get account EOS balance
asset update_balance(account_name owner)
{
auto balance = get_balance(owner);
// update creditor if update is true
creditor_table c(CODE_ACCOUNT, SCOPE);
auto creditor_itr = c.find(owner);
if(creditor_itr != c.end() && creditor_itr->balance != balance) {
Expand All @@ -59,6 +66,8 @@ namespace utils
return balance;
}



//get creditor with balance >= to_delegate
account_name get_qualified_paid_creditor(asset to_delegate)
{
Expand Down

0 comments on commit 4539bf6

Please sign in to comment.