Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset opt-in status when the election is rescheduled #648

Merged
merged 3 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 65 additions & 16 deletions contracts/eden/include/elections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,25 @@ namespace eden
EOSIO_REFLECT(current_election_state_pending_date);
EOSIO_COMPARE(current_election_state_pending_date);

struct current_election_state_registration
struct current_election_state_registration_v0
{
eosio::block_timestamp start_time;
uint16_t
election_threshold; // The election may be moved forward if active membership reached this
// The election may be moved forward if active membership reached this
uint16_t election_threshold;
// The number of times that the election schedule has been updated
// always > 0
uint8_t election_schedule_version = 1;
};
EOSIO_REFLECT(current_election_state_registration, start_time, election_threshold);
EOSIO_COMPARE(current_election_state_registration);
EOSIO_REFLECT(current_election_state_registration_v0, start_time, election_threshold);
EOSIO_COMPARE(current_election_state_registration_v0);

struct current_election_state_registration_v1 : current_election_state_registration_v0
{
};
EOSIO_REFLECT(current_election_state_registration_v1,
base current_election_state_registration_v0,
election_schedule_version);
EOSIO_COMPARE(current_election_state_registration_v1);

struct election_seeder
{
Expand All @@ -130,27 +141,44 @@ namespace eden
EOSIO_REFLECT(election_seeder, current, start_time, end_time);
EOSIO_COMPARE(election_seeder);

struct current_election_state_seeding
struct current_election_state_seeding_v0
{
election_seeder seed;
std::uint8_t election_schedule_version = 1;
};
EOSIO_REFLECT(current_election_state_seeding_v0, seed);
EOSIO_COMPARE(current_election_state_seeding_v0);

struct current_election_state_seeding_v1 : current_election_state_seeding_v0
{
};
EOSIO_REFLECT(current_election_state_seeding, seed);
EOSIO_COMPARE(current_election_state_seeding);
EOSIO_REFLECT(current_election_state_seeding_v1,
base current_election_state_seeding_v0,
election_schedule_version)

// In this phase, every voter is assigned a unique random integer id in [0,N)
struct current_election_state_init_voters
struct current_election_state_init_voters_v0
{
uint16_t next_member_idx;
election_rng rng;
eosio::name last_processed = {};
uint16_t next_report_index = 0;
uint8_t election_schedule_version = 1;
};
EOSIO_REFLECT(current_election_state_init_voters,
EOSIO_REFLECT(current_election_state_init_voters_v0,
next_member_idx,
rng,
last_processed,
next_report_index)
EOSIO_COMPARE(current_election_state_init_voters);
EOSIO_COMPARE(current_election_state_init_voters_v0);

struct current_election_state_init_voters_v1 : current_election_state_init_voters_v0
{
};
EOSIO_REFLECT(current_election_state_init_voters_v1,
base current_election_state_init_voters_v0,
election_schedule_version)
EOSIO_COMPARE(current_election_state_init_voters_v1);

struct current_election_state_active
{
Expand Down Expand Up @@ -188,15 +216,35 @@ namespace eden
EOSIO_COMPARE(current_election_state_final);

using current_election_state = std::variant<current_election_state_pending_date,
current_election_state_registration,
current_election_state_seeding,
current_election_state_init_voters,
current_election_state_registration_v0,
current_election_state_seeding_v0,
current_election_state_init_voters_v0,
current_election_state_active,
current_election_state_post_round,
current_election_state_final>;
current_election_state_final,
current_election_state_registration_v1,
current_election_state_seeding_v1,
current_election_state_init_voters_v1>;
using current_election_state_singleton =
eosio::singleton<"elect.curr"_n, current_election_state>;

template <typename T>
T* get_if_derived(current_election_state* state)
{
return std::visit(
[](auto& s) -> T* {
if constexpr (std::is_base_of_v<T, std::decay_t<decltype(s)>>)
{
return &s;
}
else
{
return nullptr;
}
},
*state);
}

// Requirements:
// - Except for the last round, the group size shall be in [4,6]
// - The last round has a minimum group size of 3
Expand All @@ -219,7 +267,7 @@ namespace eden

void set_state_sing(const current_election_state& new_value);
void add_voter(election_rng& rng, uint8_t round, uint16_t& next_index, eosio::name member);
uint32_t randomize_voters(current_election_state_init_voters& state, uint32_t max_steps);
uint32_t randomize_voters(current_election_state_init_voters_v0& state, uint32_t max_steps);
std::vector<eosio::name> extract_board();
void finish_election(std::vector<eosio::name>&& board, eosio::name winner);
void set_board_permission(const std::vector<eosio::name>& board);
Expand All @@ -234,6 +282,7 @@ namespace eden
{
}
std::optional<eosio::block_timestamp> get_next_election_time();
std::uint8_t election_schedule_version();
void set_time(uint8_t day, const std::string& time);
void set_default_election(eosio::time_point_sec origin_time);
void trigger_election();
Expand Down
9 changes: 2 additions & 7 deletions contracts/eden/include/members.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ namespace eden
active_member = 1
};

using election_participation_status_type = uint8_t;
enum election_participation_status : election_participation_status_type
{
not_in_election,
in_election
};
inline constexpr std::uint8_t not_in_election = 0;

struct migrate_member_v0
{
Expand All @@ -38,7 +33,7 @@ namespace eden
member_status_type status;
uint64_t nft_template_id;
// Only reflected in v1
election_participation_status_type election_participation_status = not_in_election;
uint8_t election_participation_status = 0;
uint8_t election_rank = 0;
eosio::name representative{uint64_t(-1)};
std::optional<eosio::public_key> encryption_key;
Expand Down
10 changes: 0 additions & 10 deletions contracts/eden/src/actions/elect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ namespace eden

members members{get_self()};
const auto& member = members.get_member(voter);
if (participating)
{
eosio::check(member.election_participation_status() == not_in_election,
"Not currently opted out");
}
else
{
eosio::check(member.election_participation_status() == in_election,
"Not currently opted in");
}
members.election_opt(member, participating);
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/eden/src/actions/induct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace eden
if (elect_state.exists())
{
auto state = elect_state.get();
if (auto* reg = std::get_if<current_election_state_registration>(&state);
if (auto* reg = get_if_derived<current_election_state_registration_v0>(&state);
reg && members.stats().active_members >= reg->election_threshold)
{
elections elections{get_self()};
Expand Down
Loading