Skip to content

Commit

Permalink
Add setinfo action to cyber.stake #329
Browse files Browse the repository at this point in the history
  • Loading branch information
zxcat committed Mar 23, 2020
1 parent 70226ec commit 753ac8f
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 173 deletions.
9 changes: 9 additions & 0 deletions cyber.stake/cyber.stake.abi
Expand Up @@ -130,6 +130,14 @@
{"name": "break_fee", "type": "int16"},
{"name": "break_min_own_staked", "type": "int64"}
]
}, {
"name": "setinfo", "base": "",
"fields": [
{"name": "account", "type": "name"},
{"name": "token_code", "type": "symbol_code"},
{"name": "url", "type": "string"},
{"name": "info", "type": "string$"}
]
}, {
"name": "setkey", "base": "",
"fields": [
Expand Down Expand Up @@ -202,6 +210,7 @@
{"name": "setautorc", "type": "setautorc"},
{"name": "setautorcmode", "type": "setautorcmode"},
{"name": "setgrntterms", "type": "setgrntterms"},
{"name": "setinfo", "type": "setinfo"},
{"name": "setkey", "type": "setkey"},
{"name": "setminstaked", "type": "setminstaked"},
{"name": "setproxyfee", "type": "setproxyfee"},
Expand Down
2 changes: 2 additions & 0 deletions cyber.stake/include/cyber.stake/config.hpp
Expand Up @@ -8,6 +8,8 @@ static const auto proxylvl_recovery_delay = 60 * 60 * 24 * 7;
static const auto max_no_pick_period = 60 * 60 * 24 * 30;
static const auto reward_memo = "$reward";

constexpr size_t max_validator_meta_size = 2048;

}

} // cyber::config
24 changes: 13 additions & 11 deletions cyber.stake/include/cyber.stake/cyber.stake.hpp
Expand Up @@ -93,16 +93,16 @@ struct structures {
}
void check_own_staked(name privileged, int64_t min_own_staked_for_election) const {
if (!has_auth(privileged)) {
//there are accounts with inconsistent balance and min_own_staked values,
//there are accounts with inconsistent balance and min_own_staked values,
//to test their deactivation we allow _self to set such values

eosio::check(proxy_level || min_own_staked >= min_own_staked_for_election,
"min_own_staked can't be less than min_own_staked_for_election for users with an ultimate level");
eosio::check(get_own_funds() >= min_own_staked, "own staked funds can't be less than min_own_staked");
}
}
};
};

struct auto_recall {
uint64_t id;
symbol_code token_code;
Expand Down Expand Up @@ -228,7 +228,7 @@ struct structures {
using prov_payouts [[eosio::order("id")]] =
eosio::multi_index<"provpayout"_n, structures::prov_payout_struct, prov_payout_acc_index>;

using susp_key_index [[using eosio: order("token_code"), order("account"), order("action_name")]] =
using susp_key_index [[using eosio: order("token_code"), order("account"), order("action_name")]] =
eosio::indexed_by<"bykey"_n, eosio::const_mem_fun<structures::suspense, structures::suspense::key_t, &structures::suspense::by_key> >;
using susps [[eosio::order("id")]] =
eosio::multi_index<"suspense"_n, structures::suspense, susp_key_index>;
Expand Down Expand Up @@ -278,7 +278,7 @@ struct structures {
void set_votes(symbol_code token_code, const std::map<name, int64_t>& votes_changes);

void update_provided(name grantor_name, name recipient_name, asset quantity);

void check_suspense(susps& susps_table, susps_idx_t& susps_idx, symbol_code token_code, name account, name action_name);
void set_suspense(name ram_payer, susps& susps_table, susps_idx_t& susps_idx, symbol_code token_code, name account, name action_name, int delay);

Expand All @@ -289,7 +289,7 @@ struct structures {
int64_t votes = 0;
public_key signing_key = {};
};

static inline uint8_t get_max_level(symbol_code token_code) {
params params_table(table_owner, table_owner.value);
return params_table.get(token_code.raw(), "no staking for token").max_proxies.size();
Expand Down Expand Up @@ -369,10 +369,10 @@ struct structures {
}
return ret;
}

static inline bool candidate_exists(name account, symbol_code token_code) {
staking_exists(token_code);

candidates candidates_table(table_owner, table_owner.value);
auto cands_idx = candidates_table.get_index<"bykey"_n>();
return cands_idx.find(std::make_tuple(token_code, account)) != cands_idx.end();
Expand Down Expand Up @@ -407,7 +407,7 @@ struct structures {
[[eosio::action]] void setproxyfee(name account, symbol_code token_code, int16_t fee);
[[eosio::action]] void setminstaked(name account, symbol_code token_code, int64_t min_own_staked);
[[eosio::action]] void setkey(name account, symbol_code token_code, std::optional<public_key> signing_key);

[[eosio::action]] void updatefunds(name account, symbol_code token_code);
[[eosio::action]] void suspendcand(name account, symbol_code token_code);

Expand All @@ -428,8 +428,10 @@ struct structures {
[[eosio::action]] void recalluse(name grantor_name, name recipient_name, asset quantity);

[[eosio::action]] void claim(name grantor_name, name recipient_name, symbol_code token_code);

[[eosio::action]] void setautorc(name account, symbol_code token_code, bool break_fee_enabled, bool break_min_stake_enabled);
[[eosio::action]] void setautorcmode(symbol_code token_code, bool enabled);

[[eosio::action]] void setinfo(name account, symbol_code token_code, string url, eosio::binary_extension<string> info);
};
} /// namespace cyber
48 changes: 29 additions & 19 deletions cyber.stake/src/cyber.stake.cpp
Expand Up @@ -328,7 +328,7 @@ void stake::set_suspense(name ram_payer, susps& susps_table, susps_idx_t& susps_
auto susps_itr = susps_idx.find(std::make_tuple(token_code, account, action_name));
if (susps_itr != susps_idx.end()) {
susps_idx.modify(susps_itr, name(), [&](auto& s) {
s.expiration_time = std::max(s.expiration_time, time_point_sec(eosio::current_time_point() + seconds(delay)));
s.expiration_time = std::max(s.expiration_time, time_point_sec(eosio::current_time_point() + seconds(delay)));
});
}
else {
Expand All @@ -353,19 +353,19 @@ void stake::setkey(name account, symbol_code token_code, std::optional<public_ke
auto agents_idx = agents_table.get_index<"bykey"_n>();
auto agent = get_agent_itr(token_code, agents_idx, account);
eosio::check(!agent->proxy_level, account.to_string() + " is not a candidate");

auto issuer = eosio::token::get_issuer(config::token_name, token_code);
bool has_issuer_auth = has_auth(issuer);

candidates candidates_table(table_owner, table_owner.value);
auto cands_idx = candidates_table.get_index<"bykey"_n>();
auto cand = cands_idx.find(std::make_tuple(token_code, account));
eosio::check(cand != cands_idx.end(), ("SYSTEM: candidate " + account.to_string() + " doesn't exist").c_str());

susps susps_table(_self, _self.value);
auto susps_idx = susps_table.get_index<"bykey"_n>();
auto action_name = "setkey"_n;

if (actual_signing_key != public_key{}) {
require_auth(account);
check_suspense(susps_table, susps_idx, token_code, account, action_name);
Expand All @@ -374,12 +374,12 @@ void stake::setkey(name account, symbol_code token_code, std::optional<public_ke
else if (!has_issuer_auth && agent->min_own_staked >= min_own_staked_for_election && agent->get_own_funds() >= agent->min_own_staked) {
require_auth(account);
}

cands_idx.modify(cand, name(), [actual_signing_key](auto& a) {
a.signing_key = actual_signing_key;
a.enabled = actual_signing_key != public_key{};
});

if (has_issuer_auth && !cand->enabled) {
set_suspense(issuer, susps_table, susps_idx, token_code, account, action_name, config::key_recovery_delay);
}
Expand All @@ -388,16 +388,16 @@ void stake::setkey(name account, symbol_code token_code, std::optional<public_ke
void stake::setproxylvl(name account, symbol_code token_code, uint8_t level) {
auto issuer = eosio::token::get_issuer(config::token_name, token_code);
bool has_issuer_auth = has_auth(issuer);

susps susps_table(_self, _self.value);
auto susps_idx = susps_table.get_index<"bykey"_n>();
auto action_name = "setproxylvl"_n;

if (!has_issuer_auth) {
require_auth(account);
check_suspense(susps_table, susps_idx, token_code, account, action_name);
}

params params_table(table_owner, table_owner.value);
const auto& param = params_table.get(token_code.raw(), "no staking for token");

Expand Down Expand Up @@ -445,12 +445,12 @@ void stake::setproxylvl(name account, symbol_code token_code, uint8_t level) {
a.proxy_level = level;
});
agent->check_own_staked(_self, param.min_own_staked_for_election);

if (has_issuer_auth) {
set_suspense(issuer, susps_table, susps_idx, token_code, account, action_name, config::proxylvl_recovery_delay);
}
}

void stake::create(symbol token_symbol, std::vector<uint8_t> max_proxies, int64_t depriving_window, int64_t min_own_staked_for_election)
{
auto token_code = token_symbol.code();
Expand Down Expand Up @@ -536,13 +536,13 @@ void stake::updatefunds(name account, symbol_code token_code) {
void stake::suspendcand(name account, symbol_code token_code) {
//require_auth(anyone);
auto issuer = eosio::token::get_issuer(config::token_name, token_code);

candidates candidates_table(table_owner, table_owner.value);
auto cands_idx = candidates_table.get_index<"bykey"_n>();
auto cand = cands_idx.find(std::make_tuple(token_code, account));
eosio::check(cand != cands_idx.end(), "candidate doesn't exist");
eosio::check(cand->latest_pick < eosio::current_time_point() - eosio::seconds(config::max_no_pick_period), "candidate is active");

INLINE_ACTION_SENDER(cyber::stake, setproxylvl)(config::stake_name, {issuer, config::active_name},
{account, token_code, get_max_level(token_code)});
}
Expand Down Expand Up @@ -742,15 +742,15 @@ void stake::claim(name grantor_name, name recipient_name, symbol_code token_code
void stake::setautorc(name account, symbol_code token_code, bool break_fee_enabled, bool break_min_stake_enabled) {
require_auth(account);
staking_exists(token_code);

agents agents_table(table_owner, table_owner.value);
auto agents_idx = agents_table.get_index<"bykey"_n>();
get_agent_itr(token_code, agents_idx, account); //checking that the agent exists

autorcs autorcs_table(table_owner, table_owner.value);
auto autorcs_idx = autorcs_table.get_index<"bykey"_n>();
autorcs_idx.get(std::make_tuple(token_code, name()), "custom auto recall mode is disabled for this token");

if (!break_fee_enabled && !break_min_stake_enabled) {
auto autorc_itr = autorcs_idx.find(std::make_tuple(token_code, account));
eosio::check(autorc_itr != autorcs_idx.end(), "no params changed");
Expand Down Expand Up @@ -781,10 +781,10 @@ void stake::setautorcmode(symbol_code token_code, bool enabled) {
auto issuer = eosio::token::get_issuer(config::token_name, token_code);
require_auth(issuer);
staking_exists(token_code);

autorcs autorcs_table(table_owner, table_owner.value);
auto autorcs_idx = autorcs_table.get_index<"bykey"_n>();
auto autorc_itr = autorcs_idx.find(std::make_tuple(token_code, name()));
auto autorc_itr = autorcs_idx.find(std::make_tuple(token_code, name()));
if (enabled) {
eosio::check(autorc_itr == autorcs_idx.end(), "custom auto recall mode is already enabled for this token");
autorcs_table.emplace(issuer, [&](auto& p) { p = {
Expand All @@ -799,6 +799,16 @@ void stake::setautorcmode(symbol_code token_code, bool enabled) {
}
}

void stake::setinfo(name account, symbol_code token_code, string url, eosio::binary_extension<string> info) {
require_auth(account);
eosio::check(url.size() <= config::max_validator_meta_size, "url is too long");
eosio::check(!info.has_value() || info->size() <= config::max_validator_meta_size, "info is too long");
agents tbl(table_owner, table_owner.value);
auto idx = tbl.get_index<"bykey"_n>();
auto agent = get_agent_itr(token_code, idx, account); // only syste
eosio::check(!agent->proxy_level, "only validator can set info");
}

void stake::returnlosses() {
auto losses_state = losses_singleton(_self, _self.value);
eosio::check(!losses_state.exists(), "losses already have been returned");
Expand Down
38 changes: 24 additions & 14 deletions tests/cyber.stake_test_api.hpp
Expand Up @@ -27,11 +27,11 @@ struct cyber_stake_api: base_contract_api {
}
return 0;
}

action_result push_maybe_msig(account_name act, account_name actor, mvo a, bool self_signed) {
return self_signed ?
push_msig(act, {{actor, config::active_name}, {_code, config::active_name}},
{actor, _code}, a) :
push_msig(act, {{actor, config::active_name}, {_code, config::active_name}},
{actor, _code}, a) :
push(act, actor, a);
}

Expand Down Expand Up @@ -123,7 +123,7 @@ struct cyber_stake_api: base_contract_api {
("quantity", quantity)
);
}

action_result setproxylvl(account_name account, symbol_code token_code, uint8_t level, bool mssg = true, bool self_signed = false) {
if (mssg && verbose) {
BOOST_TEST_MESSAGE("--- " << account << " sets proxy level");
Expand All @@ -135,7 +135,7 @@ struct cyber_stake_api: base_contract_api {
return push_maybe_msig(N(setproxylvl), account, a, self_signed);
}
action_result setproxyfee(account_name account, symbol_code token_code, int16_t fee, bool self_signed = false) {
auto a = args()
auto a = args()
("account", account)
("token_code", token_code)
("fee", fee);
Expand All @@ -148,8 +148,8 @@ struct cyber_stake_api: base_contract_api {
("min_own_staked", min_own_staked);
return push_maybe_msig(N(setminstaked), account, a, self_signed);
}
action_result setkey(account_name account, symbol_code token_code, bool empty,

action_result setkey(account_name account, symbol_code token_code, bool empty,
std::optional<account_name> signer = std::nullopt, bool self_signed = false) {
auto a = args()
("account", account)
Expand All @@ -166,7 +166,7 @@ struct cyber_stake_api: base_contract_api {
("token_code", token_code)
);
}

action_result suspendcand(account_name account, symbol_code token_code) {
return push(N(suspendcand), account, args()
("account", account)
Expand Down Expand Up @@ -210,16 +210,26 @@ struct cyber_stake_api: base_contract_api {
("token_code", token_code)
);
}

action_result setautorc(account_name account, symbol_code token_code, bool break_fee_enabled, bool break_min_stake_enabled) {
return push(N(setautorc), account, args()("account", account)("token_code", token_code)
("break_fee_enabled", break_fee_enabled)("break_min_stake_enabled", break_min_stake_enabled));
}

action_result setautorcmode(account_name issuer, symbol_code token_code, bool enabled) {
return push(N(setautorcmode), issuer, args()("enabled", enabled)("token_code", token_code));
}

action_result setinfo(account_name account, symbol_code token_code, string url, optional<string> info = {}) {
auto a = args()
("account", account)
("token_code", token_code)
("url", url);
if (info)
a("info", info);
return push(N(setinfo), account, a);
}

action_result register_candidate(account_name account, symbol_code token_code, bool need_to_open = true) {
if (need_to_open) {
auto ret = open(account, token_code);
Expand All @@ -232,7 +242,7 @@ struct cyber_stake_api: base_contract_api {
if (verbose) {
BOOST_TEST_MESSAGE("--- " << account << " sets key");
}
return push(N(setkey), account, args()
return push(N(setkey), account, args()
("account", account)
("token_code", token_code)
("signing_key", base_tester::get_public_key(account, "active"))
Expand Down Expand Up @@ -260,15 +270,15 @@ struct cyber_stake_api: base_contract_api {
auto all = _tester->get_all_chaindb_rows(name(), 0, N(stake.cand), false);
for(auto& v : all) {
auto o = mvo(v);
if (v["account"].as<account_name>() == account &&
v["token_code"].as<symbol_code>() == token_symbol.to_symbol_code())
if (v["account"].as<account_name>() == account &&
v["token_code"].as<symbol_code>() == token_symbol.to_symbol_code())
{
return v;
}
}
return variant();
}

int64_t get_payout(symbol_code token_code, name grantor_name, name recipient_name) {
return get_amount(token_code, grantor_name, recipient_name, _tester->get_all_chaindb_rows(_code, _code.value, N(provpayout), false));
}
Expand Down

0 comments on commit 753ac8f

Please sign in to comment.