Skip to content

Commit

Permalink
Merge pull request #967 from cyberway/witness-last-update
Browse files Browse the repository at this point in the history
Witness last update
  • Loading branch information
soft-bagel-93 authored Sep 16, 2020
2 parents 97c4f1b + 59629ea commit 9639711
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
6 changes: 4 additions & 2 deletions golos.ctrl/golos.ctrl.abi
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@
{"name": "url", "type": "string"},
{"name": "active", "type": "bool"},
{"name": "total_weight", "type": "uint64"},
{"name": "counter_votes", "type": "uint64"}
{"name": "counter_votes", "type": "uint64"},
{"name": "last_update", "type": "time_point_sec$"}
]
}, {
"name": "witness_voter", "base": "",
Expand All @@ -150,7 +151,8 @@
"fields": [
{"name": "witness", "type": "name"},
{"name": "weight", "type": "uint64"},
{"name": "active", "type": "bool"}
{"name": "active", "type": "bool"},
{"name": "last_update", "type": "time_point_sec"}
]
}
],
Expand Down
2 changes: 2 additions & 0 deletions golos.ctrl/include/golos.ctrl/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ namespace golos { namespace config {

static const auto witness_max_url_size = 256;

static const auto witness_expiration_sec = 30*24*60*60;

} } // golos::config
7 changes: 6 additions & 1 deletion golos.ctrl/include/golos.ctrl/golos.ctrl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <eosio/asset.hpp>
#include <eosio/singleton.hpp>
#include <eosio/crypto.hpp>
#include <eosio/binary_extension.hpp>
#include <vector>
#include <string>
#include <common/dispatchers.hpp>
Expand All @@ -25,6 +26,10 @@ struct witness_info {
uint64_t total_weight;
uint64_t counter_votes;

// binary_extension is for compatibility with old DB before contract upgraded
// note: it can be added only to end of table
eosio::binary_extension<time_point_sec, eosio::write_strategy::no_value> last_update;

uint64_t primary_key() const {
return name.value;
}
Expand Down Expand Up @@ -125,10 +130,10 @@ class [[eosio::contract("golos.ctrl")]] control: public contract {
name witness;
uint64_t weight;
bool active;
time_point_sec last_update;
};

void send_witness_event(const witness_info& wi);
void active_witness(golos::name witness, bool flag);
};


Expand Down
47 changes: 28 additions & 19 deletions golos.ctrl/src/golos.ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ void control::regwitness(name witness, string url) {
w.name = witness;
w.url = url;
w.active = true;
w.last_update.emplace(eosio::current_time_point());
};
});

Expand All @@ -166,15 +167,38 @@ void control::unregwitness(name witness) {

void control::stopwitness(name witness) {
assert_started();
require_auth(witness);
active_witness(witness, false);
const auto now = eosio::current_time_point();

// TODO: simplify upsert to allow passing just inner lambda
bool exists = upsert_tbl<witness_tbl>(witness, [&](bool) {
return [&](witness_info& w) {
eosio::check(w.active, "active flag not updated");
if (!has_auth(witness)) {
eosio::check(now - w.last_update.value_or() > eosio::seconds(config::witness_expiration_sec), "recently updated witness can be stopped only by itself");
}
w.active = false;
send_witness_event(w);
};
}, false);
eosio::check(exists, "witness not found");
update_auths();
}

void control::startwitness(name witness) {
eosio::check(!control::is_blocking(config::control_name, witness), "You are blocked.");
assert_started();
require_auth(witness);
active_witness(witness, true);

// TODO: simplify upsert to allow passing just inner lambda
bool exists = upsert_tbl<witness_tbl>(witness, [&](bool) {
return [&](witness_info& w) {
w.active = true;
w.last_update.emplace(eosio::current_time_point());
send_witness_event(w);
};
}, false);
eosio::check(exists, "witness not found");
update_auths();
}

// Note: if not weighted, it's possible to pass all witnesses in vector like in BP actions
Expand Down Expand Up @@ -386,25 +410,10 @@ void control::update_auths() {
}

void control::send_witness_event(const witness_info& wi) {
witnessstate data{wi.name, wi.total_weight, wi.active};
witnessstate data{wi.name, wi.total_weight, wi.active, wi.last_update.value_or()};
eosio::event(_self, "witnessstate"_n, data).send();
}

void control::active_witness(name witness, bool flag) {
// TODO: simplify upsert to allow passing just inner lambda
bool exists = upsert_tbl<witness_tbl>(witness, [&](bool) {
return [&](witness_info& w) {
eosio::check(flag != w.active, "active flag not updated");
w.active = flag;

send_witness_event(w);
};
}, false);
eosio::check(exists, "witness not found");

update_auths();
}

vector<witness_info> control::top_witness_info() {
vector<witness_info> top;
const auto l = props().witnesses.max;
Expand Down

0 comments on commit 9639711

Please sign in to comment.