This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a819c8
commit b78df46
Showing
10 changed files
with
717 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (C) 2018 Bluzelle | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License, version 3, | ||
// as published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#include <pbft/pbft_config_store.hpp> | ||
|
||
using namespace bzn; | ||
|
||
pbft_config_store::pbft_config_store() | ||
: current_index(0), next_index(1) | ||
{ | ||
} | ||
|
||
bool | ||
pbft_config_store::add(pbft_configuration::shared_const_ptr config) | ||
{ | ||
// TODO - should we be making a copy here instead? | ||
// currently the added config could be changed externally after being added | ||
return (this->configs.insert(std::make_pair(this->next_index++, std::make_pair(config, false)))).second; | ||
} | ||
|
||
pbft_config_store::config_map::const_iterator | ||
pbft_config_store::find_by_hash(hash_t hash) const | ||
{ | ||
auto config = std::find_if(this->configs.begin(), this->configs.end(), | ||
[hash](auto c) | ||
{ | ||
return c.second.first->get_hash() == hash; | ||
}); | ||
|
||
return config; | ||
} | ||
|
||
bool | ||
pbft_config_store::set_current(const hash_t& hash) | ||
{ | ||
auto config = this->find_by_hash(hash); | ||
if (config == this->configs.end()) | ||
return false; | ||
|
||
this->current_index = config->first; | ||
return true; | ||
} | ||
|
||
bool | ||
pbft_config_store::remove_prior_to(const hash_t& hash) | ||
{ | ||
auto config = this->find_by_hash(hash); | ||
if (config == this->configs.end()) | ||
return false; | ||
|
||
this->configs.erase(this->configs.begin(), config); | ||
return true; | ||
} | ||
|
||
pbft_configuration::shared_const_ptr | ||
pbft_config_store::get(const hash_t& hash) const | ||
{ | ||
auto config = this->find_by_hash(hash); | ||
return config != this->configs.end() ? config->second.first : nullptr; | ||
} | ||
|
||
bool | ||
pbft_config_store::enable(const hash_t& hash, bool val) | ||
{ | ||
// can't find_by_hash here because we need a non-const | ||
auto config = std::find_if(this->configs.begin(), this->configs.end(), | ||
[hash](auto c) | ||
{ | ||
return c.second.first->get_hash() == hash; | ||
}); | ||
|
||
if (config != this->configs.end()) | ||
{ | ||
config->second.second = val; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool | ||
pbft_config_store::is_enabled(const hash_t& hash) const | ||
{ | ||
auto config = this->find_by_hash(hash); | ||
return config != this->configs.end() ? config->second.second : false; | ||
} | ||
|
||
pbft_configuration::shared_const_ptr | ||
pbft_config_store::current() const | ||
{ | ||
auto it = this->configs.find(this->current_index); | ||
return it != this->configs.end() ? it->second.first : nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (C) 2018 Bluzelle | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License, version 3, | ||
// as published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#pragma once | ||
|
||
#include <pbft/pbft_configuration.hpp> | ||
#include <map> | ||
|
||
namespace bzn | ||
{ | ||
using hash_t = std::string; | ||
|
||
class pbft_config_store | ||
{ | ||
public: | ||
pbft_config_store(); | ||
|
||
bool add(pbft_configuration::shared_const_ptr config); | ||
bool remove_prior_to(const hash_t& hash); | ||
pbft_configuration::shared_const_ptr get(const hash_t& hash) const; | ||
|
||
bool set_current(const hash_t& hash); | ||
pbft_configuration::shared_const_ptr current() const; | ||
|
||
bool enable(const hash_t& hash, bool val = true); | ||
bool is_enabled(const hash_t& hash) const; | ||
|
||
private: | ||
using index_t = uint64_t; | ||
using config_map = std::map<index_t, std::pair<pbft_configuration::shared_const_ptr, bool>>; | ||
config_map::const_iterator find_by_hash(hash_t hash) const; | ||
|
||
// a map from the config index to a pair of <config, is_config_enabled> | ||
config_map configs; | ||
index_t current_index; | ||
index_t next_index; | ||
}; | ||
} | ||
|
Oops, something went wrong.