Observer mode fix (#78)

* Fixed observer mode consensus issues.
* Added startup_mode to fix mode change check.
This commit is contained in:
Ravin Perera
2020-01-17 11:04:45 +05:30
committed by GitHub
parent f8dd2e014b
commit 8cf869cf9e
4 changed files with 42 additions and 40 deletions

View File

@@ -14,8 +14,8 @@ contract_ctx ctx;
// Global configuration struct exposed to the application.
contract_config cfg;
const static char *MODE_OBSERVING = "observing";
const static char *MODE_PROPOSING = "proposing";
const static char *MODE_OBSERVER = "observer";
const static char *MODE_PROPOSER = "proposer";
/**
* Loads and initializes the contract config for execution. Must be called once during application startup.
@@ -31,16 +31,13 @@ int init()
if (validate_contract_dir_paths() != 0 || load_config() != 0 || validate_config() != 0)
return -1;
if (cfg.mode == OPERATING_MODE::PROPOSING)
{
// Append self peer to peer list.
const std::string portstr = std::to_string(cfg.peerport);
const std::string peerid = "0.0.0.0:" + portstr;
cfg.peers.emplace(std::move(peerid), std::make_pair("0.0.0.0", portstr));
// Append self peer to peer list.
const std::string portstr = std::to_string(cfg.peerport);
const std::string peerid = "0.0.0.0:" + portstr;
cfg.peers.emplace(std::move(peerid), std::make_pair("0.0.0.0", portstr));
// Append self pubkey to unl list.
cfg.unl.emplace(cfg.pubkey);
}
// Append self pubkey to unl list.
cfg.unl.emplace(cfg.pubkey);
return 0;
}
@@ -91,7 +88,7 @@ int create_contract()
crypto::generate_signing_keys(cfg.pubkey, cfg.seckey);
binpair_to_hex();
cfg.mode = OPERATING_MODE::PROPOSING;
cfg.startup_mode = OPERATING_MODE::PROPOSER;
cfg.listenip = "0.0.0.0";
cfg.peerport = 22860;
cfg.roundtime = 1000;
@@ -202,15 +199,16 @@ int load_config()
// Load up the values into the struct.
if (d["mode"] == MODE_OBSERVING)
cfg.mode = OPERATING_MODE::OBSERVING;
else if (d["mode"] == MODE_PROPOSING)
cfg.mode = OPERATING_MODE::PROPOSING;
if (d["mode"] == MODE_OBSERVER)
cfg.startup_mode = OPERATING_MODE::OBSERVER;
else if (d["mode"] == MODE_PROPOSER)
cfg.startup_mode = OPERATING_MODE::PROPOSER;
else
{
std::cout << "Invalid mode. 'observing' or 'proposing' expected.\n";
std::cout << "Invalid mode. 'observer' or 'proposer' expected.\n";
return -1;
}
cfg.current_mode = cfg.startup_mode;
cfg.pubkeyhex = d["pubkeyhex"].GetString();
cfg.seckeyhex = d["seckeyhex"].GetString();
@@ -221,18 +219,16 @@ int load_config()
cfg.appbill = d["appbill"].GetString();
cfg.appbillargs = d["appbillargs"].GetString();
// Populate runtime contract execution args.
if (!cfg.binargs.empty())
boost::split(cfg.runtime_binexec_args, cfg.binargs, boost::is_any_of(" "));
cfg.runtime_binexec_args.insert(cfg.runtime_binexec_args.begin(), ( cfg.binary[0] == '/' ? cfg.binary : util::realpath( ctx.contract_dir + "/bin/" + cfg.binary ) ) );
cfg.runtime_binexec_args.insert(cfg.runtime_binexec_args.begin(), (cfg.binary[0] == '/' ? cfg.binary : util::realpath(ctx.contract_dir + "/bin/" + cfg.binary)));
// Populate runtime app bill args.
if (!cfg.appbillargs.empty())
boost::split(cfg.runtime_appbill_args, cfg.appbillargs, boost::is_any_of(" "));
cfg.runtime_appbill_args.insert(cfg.runtime_appbill_args.begin(), ( cfg.appbill[0] == '/' ? cfg.appbill : util::realpath( ctx.contract_dir + "/bin/" + cfg.appbill ) ) );
cfg.runtime_appbill_args.insert(cfg.runtime_appbill_args.begin(), (cfg.appbill[0] == '/' ? cfg.appbill : util::realpath(ctx.contract_dir + "/bin/" + cfg.appbill)));
// Uncomment for docker-based execution.
// std::string volumearg;
@@ -314,7 +310,7 @@ int save_config()
d.SetObject();
rapidjson::Document::AllocatorType &allocator = d.GetAllocator();
d.AddMember("version", rapidjson::StringRef(util::HP_VERSION), allocator);
d.AddMember("mode", rapidjson::StringRef(cfg.mode == OPERATING_MODE::OBSERVING ? MODE_OBSERVING : MODE_PROPOSING),
d.AddMember("mode", rapidjson::StringRef(cfg.startup_mode == OPERATING_MODE::OBSERVER ? MODE_OBSERVER : MODE_PROPOSER),
allocator);
d.AddMember("pubkeyhex", rapidjson::StringRef(cfg.pubkeyhex.data()), allocator);
@@ -611,7 +607,11 @@ int is_schema_valid(const rapidjson::Document &d)
void change_operating_mode(const OPERATING_MODE mode)
{
cfg.mode = mode;
// Do not allow to change the mode if the node was started as an observer.
if (cfg.startup_mode == OPERATING_MODE::OBSERVER)
return;
cfg.current_mode = mode;
}
} // namespace conf