diff --git a/contract-shared-headers/daccustodian_shared.hpp b/contract-shared-headers/daccustodian_shared.hpp index ca73a577..813105b1 100644 --- a/contract-shared-headers/daccustodian_shared.hpp +++ b/contract-shared-headers/daccustodian_shared.hpp @@ -22,39 +22,6 @@ using namespace std; * simple getter/setter **/ #define PROPERTY(type, name) \ - type get_##name() const { \ - return get(state_keys::name); \ - } \ - void set_##name(const type value) { \ - set(state_keys::name, value); \ - } - -/** - * A slightly more complicated getter/setter macro that allows optional values. - * If value is not set, it returns a null optional. To unset a previously set - * value, it has an unset function. - * Since our variant can only hold certain number types, we sometimes need to - * convert our desired type to a storage_type. Before storing, it will convert - * the value to the storage_type and before returning, the getter will - * automatically convert back to type. - **/ -#define PROPERTY_OPTIONAL_TYPECASTING(type, storage_type, name) \ - std::optional get_##name() const { \ - return get_maybe(state_keys::name); \ - } \ - void set_##name(const type value) { \ - set(state_keys::name, storage_type(value)); \ - } \ - void unset_##name() { \ - const auto search = data.find(state_keys::name); \ - check(search != data.end(), "Cannot unset " #name ", no value set"); \ - data.erase(state_keys::name); \ - } - -/** - * simple getter/setter - **/ -#define PROPERTY2(type, name) \ type get_##name() const { \ return get(#name); \ } \ @@ -71,7 +38,7 @@ using namespace std; * the value to the storage_type and before returning, the getter will * automatically convert back to type. **/ -#define PROPERTY_OPTIONAL_TYPECASTING2(type, storage_type, name) \ +#define PROPERTY_OPTIONAL_TYPECASTING(type, storage_type, name) \ std::optional maybe_get_##name() const { \ return get_maybe(#name); \ } \ @@ -95,14 +62,12 @@ namespace eosdac { #define TRANSFER_DELAY 60 * 60 #endif struct [[eosio::table("custodians"), eosio::contract("daccustodian")]] custodian { - eosio::name cust_name; - eosio::asset requestedpay; - uint64_t total_vote_power; -#ifdef MIGRATION_STAGE_2 + eosio::name cust_name; + eosio::asset requestedpay; + uint64_t total_vote_power; uint64_t rank; uint32_t number_voters; eosio::time_point_sec avg_vote_time_stamp; -#endif uint64_t primary_key() const { return cust_name.value; @@ -112,11 +77,9 @@ namespace eosdac { return std::numeric_limits::max() - total_vote_power; } -#ifdef MIGRATION_STAGE_2 uint64_t by_decayed_votes() const { return std::numeric_limits::max() - rank; } -#endif uint64_t by_requested_pay() const { return S{requestedpay.amount}.to(); @@ -125,36 +88,9 @@ namespace eosdac { using custodians_table = eosio::multi_index<"custodians"_n, custodian, eosio::indexed_by<"byvotesrank"_n, eosio::const_mem_fun>, -#ifdef MIGRATION_STAGE_2 eosio::indexed_by<"bydecayed"_n, eosio::const_mem_fun>, -#endif eosio::indexed_by<"byreqpay"_n, eosio::const_mem_fun>>; - struct [[eosio::table("custodians2"), eosio::contract("daccustodian")]] custodian2 { - eosio::name cust_name; - eosio::asset requestedpay; - uint64_t rank; - uint64_t total_vote_power; - uint32_t number_voters; - eosio::time_point_sec avg_vote_time_stamp; - - uint64_t primary_key() const { - return cust_name.value; - } - - uint64_t by_decayed_votes() const { - return std::numeric_limits::max() - rank; - } - - uint64_t by_requested_pay() const { - return S{requestedpay.amount}.to(); - } - }; - - using custodians2_table = eosio::multi_index<"custodians2"_n, custodian2, - eosio::indexed_by<"bydecayed"_n, eosio::const_mem_fun>, - eosio::indexed_by<"byreqpay"_n, eosio::const_mem_fun>>; - struct [[eosio::table("candidates"), eosio::contract("daccustodian")]] candidate { eosio::name candidate_name; eosio::asset requestedpay; @@ -218,10 +154,7 @@ namespace eosdac { }; using weights = eosio::multi_index<"weights"_n, vote_weight>; - struct contr_config; - using configscontainer = eosio::singleton<"config2"_n, contr_config>; - - struct [[eosio::table("config2"), eosio::contract("daccustodian")]] contr_config { + struct contr_config { // The amount of assets that are locked up by each candidate applying for election. eosio::extended_asset lockupasset; // The maximum number of votes that each member can make for a candidate. @@ -254,75 +187,6 @@ namespace eosdac { uint32_t lockup_release_time_delay; eosio::extended_asset requested_pay_max; - - static contr_config get_current_configs(eosio::name account, eosio::name scope) { - return configscontainer(account, scope.value).get_or_default(contr_config()); - } - - void save(eosio::name account, eosio::name scope, eosio::name payer = same_payer) { - configscontainer(account, scope.value).set(*this, payer); - } - }; - - struct contr_state2; - using statecontainer2 = eosio::singleton<"state2"_n, contr_state2>; - - enum state_keys : uint8_t { - total_weight_of_votes = 1, - total_votes_on_candidates = 2, - number_active_candidates = 3, - met_initial_votes_threshold = 4, - lastclaimbudgettime = 5, - budget_percentage = 6 - }; - using state_value_variant = - std::variant, name, std::string, eosio::time_point_sec>; - - struct [[eosio::table("state2"), eosio::contract("daccustodian")]] contr_state2 { - eosio::time_point_sec lastperiodtime = time_point_sec(0); - std::map data = {{state_keys::total_weight_of_votes, int64_t(0)}, - {state_keys::total_votes_on_candidates, int64_t(0)}, {state_keys::number_active_candidates, uint32_t(0)}, - {state_keys::met_initial_votes_threshold, false}, {state_keys::lastclaimbudgettime, time_point_sec(0)}}; - - static contr_state2 get_current_state(const eosio::name account, const eosio::name scope) { - return statecontainer2(account, scope.value).get_or_default(contr_state2{}); - } - - void save(const eosio::name account, const eosio::name scope) { - statecontainer2(account, scope.value).set(*this, account); - } - - void set(const state_keys key, const state_value_variant &value) { - data[key] = value; - } - - template - T get(const state_keys key) const { - const auto search = data.find(key); - check(search != data.end(), "Key %s not found in state data", std::to_string(key)); - return std::get(search->second); - } - - template - std::optional get_maybe(const state_keys key) const { - const auto search = data.find(key); - if (search != data.end()) { - return std::get(search->second); - } else { - return {}; - } - } - - /** - * What follows are type-safe getters/setters for polymorphic map values - **/ - - PROPERTY_OPTIONAL_TYPECASTING(uint16_t, uint32_t, budget_percentage); - PROPERTY(time_point_sec, lastclaimbudgettime); - PROPERTY(int64_t, total_weight_of_votes); - PROPERTY(bool, met_initial_votes_threshold); - PROPERTY(uint32_t, number_active_candidates); - PROPERTY(int64_t, total_votes_on_candidates); }; struct [[eosio::table("votes"), eosio::contract("daccustodian")]] vote { @@ -392,7 +256,7 @@ namespace eosdac { using candperms_table = multi_index<"candperms"_n, candperm>; - using state_value_variant2 = + using state_value_variant = std::variant, eosio::name, std::string, eosio::time_point_sec, eosio::asset, eosio::extended_asset>; @@ -400,15 +264,18 @@ namespace eosdac { using dacglobals_singleton = eosio::singleton<"dacglobals"_n, dacglobals>; struct [[eosio::table("dacglobals"), eosio::contract("daccustodian")]] dacglobals { private: - auto set(std::string && key, const state_value_variant2 &value) { + auto set(std::string && key, const state_value_variant &value) { return data.insert_or_assign(std::forward(key), value); } template T get(const std::string &key) const { const auto search = data.find(key); - check(search != data.end(), "Key %s not found in dacglobals data", key); - return std::get(search->second); + if (search == data.end()) { + return T{}; + } else { + return std::get(search->second); + } } template @@ -422,77 +289,41 @@ namespace eosdac { } public: - std::map data = {}; + std::map data = {}; static dacglobals current(const eosio::name account, const eosio::name scope) { - return dacglobals_singleton(account, scope.value).get_or_default(get_migrated_state(account, scope)); + return dacglobals_singleton(account, scope.value).get_or_default(dacglobals()); } void save(const eosio::name account, const eosio::name scope) { dacglobals_singleton(account, scope.value).set(*this, account); - configscontainer(account, scope.value).remove(); - statecontainer2(account, scope.value).remove(); } /** * What follows are type-safe getters/setters for polymorphic map values **/ - PROPERTY_OPTIONAL_TYPECASTING2(uint16_t, uint32_t, budget_percentage); - PROPERTY2(time_point_sec, lastclaimbudgettime); - PROPERTY2(int64_t, total_weight_of_votes); - PROPERTY2(bool, met_initial_votes_threshold); - PROPERTY2(uint32_t, number_active_candidates); - PROPERTY2(int64_t, total_votes_on_candidates); - PROPERTY2(eosio::time_point_sec, lastperiodtime); + PROPERTY_OPTIONAL_TYPECASTING(uint16_t, uint32_t, budget_percentage); + PROPERTY(time_point_sec, lastclaimbudgettime); + PROPERTY(int64_t, total_weight_of_votes); + PROPERTY(bool, met_initial_votes_threshold); + PROPERTY(uint32_t, number_active_candidates); + PROPERTY(int64_t, total_votes_on_candidates); + PROPERTY(eosio::time_point_sec, lastperiodtime); // from config2 - PROPERTY2(eosio::extended_asset, lockupasset); - PROPERTY2(uint8_t, maxvotes); - PROPERTY2(uint8_t, numelected); - PROPERTY2(uint32_t, periodlength); - PROPERTY2(bool, should_pay_via_service_provider); - PROPERTY2(uint32_t, initial_vote_quorum_percent); - PROPERTY2(uint32_t, vote_quorum_percent); - PROPERTY2(uint8_t, auth_threshold_high); - PROPERTY2(uint8_t, auth_threshold_mid); - PROPERTY2(uint8_t, auth_threshold_low); - PROPERTY2(uint32_t, lockup_release_time_delay); - PROPERTY2(eosio::extended_asset, requested_pay_max); - PROPERTY2(uint64_t, token_supply_theshold); - - static dacglobals get_migrated_state(const eosio::name account, const eosio::name dac_id) { - auto new_state = dacglobals{}; - - // migrate from state2 - const auto state_2 = contr_state2::get_current_state(account, dac_id); - const auto p = state_2.get_budget_percentage(); - if (p) { - new_state.set_budget_percentage(*p); - } - new_state.set_lastclaimbudgettime(state_2.get_lastclaimbudgettime()); - new_state.set_total_weight_of_votes(state_2.get_total_weight_of_votes()); - new_state.set_met_initial_votes_threshold(state_2.get_met_initial_votes_threshold()); - new_state.set_number_active_candidates(state_2.get_number_active_candidates()); - new_state.set_total_votes_on_candidates(state_2.get_total_votes_on_candidates()); - new_state.set_lastperiodtime(state_2.lastperiodtime); - - // migrate from config2 - const auto config_2 = contr_config::get_current_configs(account, dac_id); - new_state.set_lockupasset(config_2.lockupasset); - new_state.set_maxvotes(config_2.maxvotes); - new_state.set_numelected(config_2.numelected); - new_state.set_periodlength(config_2.periodlength); - new_state.set_should_pay_via_service_provider(config_2.should_pay_via_service_provider); - new_state.set_initial_vote_quorum_percent(config_2.initial_vote_quorum_percent); - new_state.set_vote_quorum_percent(config_2.vote_quorum_percent); - new_state.set_auth_threshold_high(config_2.auth_threshold_high); - new_state.set_auth_threshold_mid(config_2.auth_threshold_mid); - new_state.set_auth_threshold_low(config_2.auth_threshold_low); - new_state.set_lockup_release_time_delay(config_2.lockup_release_time_delay); - new_state.set_requested_pay_max(config_2.requested_pay_max); - - return new_state; - } + PROPERTY(eosio::extended_asset, lockupasset); + PROPERTY(uint8_t, maxvotes); + PROPERTY(uint8_t, numelected); + PROPERTY(uint32_t, periodlength); + PROPERTY(bool, should_pay_via_service_provider); + PROPERTY(uint32_t, initial_vote_quorum_percent); + PROPERTY(uint32_t, vote_quorum_percent); + PROPERTY(uint8_t, auth_threshold_high); + PROPERTY(uint8_t, auth_threshold_mid); + PROPERTY(uint8_t, auth_threshold_low); + PROPERTY(uint32_t, lockup_release_time_delay); + PROPERTY(eosio::extended_asset, requested_pay_max); + PROPERTY(uint64_t, token_supply_theshold); }; class daccustodian : public contract { @@ -535,10 +366,7 @@ namespace eosdac { ACTION claimbudget(const name &dac_id); ACTION setbudget(const name &dac_id, const uint16_t percentage); ACTION unsetbudget(const name &dac_id); - ACTION migrate1(const name dac_id); -#ifdef MIGRATION_STAGE_2 - ACTION migrate2(const name dac_id); -#endif + #ifdef DEBUG ACTION migratestate(const name &dac_id); ACTION resetvotes(const name &voter, const name &dac_id); diff --git a/contracts/daccustodian/daccustodian.cpp b/contracts/daccustodian/daccustodian.cpp index 486b2246..8880d7d6 100644 --- a/contracts/daccustodian/daccustodian.cpp +++ b/contracts/daccustodian/daccustodian.cpp @@ -22,7 +22,5 @@ #include "debug.cpp" #endif -#include "migration.cpp" - using namespace eosio; using namespace std; diff --git a/contracts/daccustodian/daccustodian.test.ts b/contracts/daccustodian/daccustodian.test.ts index de2f77b3..aabc69d3 100644 --- a/contracts/daccustodian/daccustodian.test.ts +++ b/contracts/daccustodian/daccustodian.test.ts @@ -49,199 +49,11 @@ describe('Daccustodian', () => { somebody = await AccountManager.createAccount(); }); - context('Migration', async () => { - let dacId = 'migratecust'; - const date = new Date(0); - let table_contents = [ - { - cust_name: 'testcust1', - requestedpay: '0.0000 TLM', - rank: 0, - total_vote_power: 0, - number_voters: 0, - avg_vote_time_stamp: date, - }, - { - cust_name: 'testcust2', - requestedpay: '0.0000 TLM', - rank: 0, - total_vote_power: 0, - number_voters: 0, - avg_vote_time_stamp: date, - }, - { - cust_name: 'testcust3', - requestedpay: '0.0000 TLM', - rank: 0, - total_vote_power: 0, - number_voters: 0, - avg_vote_time_stamp: date, - }, - { - cust_name: 'testcust4', - requestedpay: '0.0000 TLM', - rank: 0, - total_vote_power: 0, - number_voters: 0, - avg_vote_time_stamp: date, - }, - { - cust_name: 'testcust5', - requestedpay: '0.0000 TLM', - rank: 0, - total_vote_power: 0, - number_voters: 0, - avg_vote_time_stamp: date, - }, - ]; - it('adding test custodians should work', async () => { - await shared.daccustodian_contract.tstaddcust('testcust1', dacId); - await shared.daccustodian_contract.tstaddcust('testcust2', dacId); - await shared.daccustodian_contract.tstaddcust('testcust3', dacId); - await shared.daccustodian_contract.tstaddcust('testcust4', dacId); - await shared.daccustodian_contract.tstaddcust('testcust5', dacId); - }); - it('should add custodians', async () => { - await assertRowsEqual( - shared.daccustodian_contract.custodiansTable({ - scope: dacId, - }), - table_contents - ); - }); - it('migrate1 should work', async () => { - await shared.daccustodian_contract.migrate1(dacId); - }); - it('should delete custodians', async () => { - await assertRowsEqual( - shared.daccustodian_contract.custodiansTable({ - scope: dacId, - }), - [] - ); - }); - it('should have migrated to custodians2', async () => { - await assertRowsEqual( - shared.daccustodian_contract.custodians2Table({ - scope: dacId, - }), - table_contents - ); - }); - it('migrate2 should work', async () => { - await shared.daccustodian_contract.migrate2(dacId); - }); - it('should have migrated back to custodians', async () => { - await assertRowsEqual( - shared.daccustodian_contract.custodiansTable({ - scope: dacId, - }), - table_contents - ); - }); - }); - context('fillstate', async () => { let dacId = 'migratedac'; before(async () => { await shared.daccustodian_contract.fillstate(dacId); }); - it('dacglobal data should have been migrated from config and state2', async () => { - const res = await shared.daccustodian_contract.dacglobalsTable({ - scope: dacId, - }); - const s = res.rows[0]; - chai.expect(s.data).to.deep.equal([ - { - key: 'auth_threshold_high', - value: ['uint8', 0], - }, - { - key: 'auth_threshold_low', - value: ['uint8', 0], - }, - { - key: 'auth_threshold_mid', - value: ['uint8', 0], - }, - { - key: 'budget_percentage', - value: ['uint32', 123], - }, - { - key: 'initial_vote_quorum_percent', - value: ['uint32', 0], - }, - { - key: 'lastclaimbudgettime', - value: ['time_point_sec', '1970-01-01T00:00:00'], - }, - { - key: 'lastperiodtime', - value: ['time_point_sec', '1970-01-01T00:00:00'], - }, - { - key: 'lockup_release_time_delay', - value: ['uint32', 0], - }, - { - key: 'lockupasset', - value: [ - 'extended_asset', - { - quantity: '0 ', - contract: '', - }, - ], - }, - { - key: 'maxvotes', - value: ['uint8', 5], - }, - { - key: 'met_initial_votes_threshold', - value: ['bool', 0], - }, - { - key: 'number_active_candidates', - value: ['uint32', 0], - }, - { - key: 'numelected', - value: ['uint8', 3], - }, - { - key: 'periodlength', - value: ['uint32', 604800], - }, - { - key: 'requested_pay_max', - value: [ - 'extended_asset', - { - quantity: '0 ', - contract: '', - }, - ], - }, - { - key: 'should_pay_via_service_provider', - value: ['bool', 0], - }, - { - key: 'total_votes_on_candidates', - value: ['int64', 0], - }, - { - key: 'total_weight_of_votes', - value: ['int64', 0], - }, - { - key: 'vote_quorum_percent', - value: ['uint32', 0], - }, - ]); - }); }); context('updateconfige', async () => { @@ -273,7 +85,7 @@ describe('Daccustodian', () => { 'ERR::DAC_NOT_FOUND' ); await assertRowsEqual( - shared.daccustodian_contract.config2Table({ + shared.daccustodian_contract.dacglobalsTable({ scope: 'unknowndac', limit: 1, }), @@ -304,7 +116,7 @@ describe('Daccustodian', () => { 'ERR::UPDATECONFIG_INVALID_AUTH_HIGH_TO_NUM_ELECTED' ); await assertRowsEqual( - shared.daccustodian_contract.config2Table({ + shared.daccustodian_contract.dacglobalsTable({ scope: dacId, limit: 2, }), @@ -335,7 +147,7 @@ describe('Daccustodian', () => { 'ERR::UPDATECONFIG_INVALID_AUTH_HIGH_TO_MID_AUTH' ); await assertRowsEqual( - shared.daccustodian_contract.config2Table({ + shared.daccustodian_contract.dacglobalsTable({ scope: dacId, limit: 2, }), @@ -366,7 +178,7 @@ describe('Daccustodian', () => { 'ERR::UPDATECONFIG_INVALID_AUTH_MID_TO_LOW_AUTH' ); await assertRowsEqual( - shared.daccustodian_contract.config2Table({ + shared.daccustodian_contract.dacglobalsTable({ scope: dacId, limit: 2, }), @@ -608,14 +420,6 @@ describe('Daccustodian', () => { key: 'initial_vote_quorum_percent', value: ['uint32', 31], }, - { - key: 'lastclaimbudgettime', - value: ['time_point_sec', '1970-01-01T00:00:00'], - }, - { - key: 'lastperiodtime', - value: ['time_point_sec', '1970-01-01T00:00:00'], - }, { key: 'lockup_release_time_delay', value: ['uint32', 1233], @@ -634,14 +438,6 @@ describe('Daccustodian', () => { key: 'maxvotes', value: ['uint8', 4], }, - { - key: 'met_initial_votes_threshold', - value: ['bool', 0], - }, - { - key: 'number_active_candidates', - value: ['uint32', 0], - }, { key: 'numelected', value: ['uint8', 5], @@ -668,14 +464,6 @@ describe('Daccustodian', () => { key: 'token_supply_theshold', value: ['uint64', 10000001], }, - { - key: 'total_votes_on_candidates', - value: ['int64', 0], - }, - { - key: 'total_weight_of_votes', - value: ['int64', 0], - }, { key: 'vote_quorum_percent', value: ['uint32', 15], @@ -1016,10 +804,10 @@ describe('Daccustodian', () => { let dacState = await shared.daccustodian_contract.dacglobalsTable({ scope: dacId, }); - chai.expect(dacState.rows[0].data).to.deep.include({ - key: 'total_weight_of_votes', - value: ['int64', 0], - }); + const res = dacState.rows[0].data.find( + (x) => x.key === 'total_weight_of_votes' + ); + chai.expect(res).to.be.undefined; }); }); context('After voting', async () => { diff --git a/contracts/daccustodian/migration.cpp b/contracts/daccustodian/migration.cpp deleted file mode 100644 index 7c60cfbb..00000000 --- a/contracts/daccustodian/migration.cpp +++ /dev/null @@ -1,30 +0,0 @@ - - -ACTION daccustodian::migrate1(const name dac_id) { - auto custodians = custodians_table{get_self(), dac_id.value}; - auto custodians2 = custodians2_table{get_self(), dac_id.value}; - auto itr = custodians.begin(); - while (itr != custodians.end()) { - custodians2.emplace(get_self(), [&](auto &c) { - c.cust_name = itr->cust_name; - c.requestedpay = itr->requestedpay; - c.total_vote_power = itr->total_vote_power; - }); - itr = custodians.erase(itr); - } -} -#ifdef MIGRATION_STAGE_2 -ACTION daccustodian::migrate2(const name dac_id) { - auto custodians = custodians_table{get_self(), dac_id.value}; - auto custodians2 = custodians2_table{get_self(), dac_id.value}; - auto itr = custodians2.begin(); - while (itr != custodians2.end()) { - custodians.emplace(get_self(), [&](auto &c) { - c.cust_name = itr->cust_name; - c.requestedpay = itr->requestedpay; - c.total_vote_power = itr->total_vote_power; - }); - itr = custodians2.erase(itr); - } -} -#endif diff --git a/contracts/daccustodian/newperiod_components.cpp b/contracts/daccustodian/newperiod_components.cpp index cf99395c..3e7b2672 100644 --- a/contracts/daccustodian/newperiod_components.cpp +++ b/contracts/daccustodian/newperiod_components.cpp @@ -104,14 +104,12 @@ void daccustodian::allocateCustodians(bool early_election, name dac_id) { cand_itr++; } else { custodians.emplace(auth_account, [&](custodian &c) { - c.cust_name = cand_itr->candidate_name; - c.requestedpay = cand_itr->requestedpay; - c.total_vote_power = cand_itr->total_vote_power; -#ifdef MIGRATION_STAGE_2 + c.cust_name = cand_itr->candidate_name; + c.requestedpay = cand_itr->requestedpay; + c.total_vote_power = cand_itr->total_vote_power; c.rank = cand_itr->rank; c.number_voters = cand_itr->number_voters; c.avg_vote_time_stamp = cand_itr->avg_vote_time_stamp; -#endif }); currentCustodianCount++; @@ -221,13 +219,6 @@ ACTION daccustodian::claimbudget(const name &dac_id) { #ifdef IS_DEV ACTION daccustodian::fillstate(const name &dac_id) { - auto config = contr_config{}; - config.save(get_self(), dac_id, get_self()); - - auto state2 = contr_state2{}; - state2.set_budget_percentage(123); - state2.save(get_self(), dac_id); - auto globals = dacglobals::current(get_self(), dac_id); globals.save(get_self(), dac_id); }