Skip to content

Commit

Permalink
complete peer_service
Browse files Browse the repository at this point in the history
  • Loading branch information
satellitex committed Jun 14, 2017
1 parent 3b6bd3d commit 5648246
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 188 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ add_subdirectory(logger)
add_subdirectory(ordering)
add_subdirectory(runtime)
add_subdirectory(validation)
add_subdirectory(peer_service)
4 changes: 2 additions & 2 deletions core/ordering/observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.
#include "observer.hpp"
#include "quque.hpp"
#include <common/timer.hpp>
#include <peer_service/self_state.hpp>
//#include <peer_service/self_state.hpp>

namespace ordering {
namespace observer {
Expand All @@ -25,7 +25,7 @@ namespace ordering {
while (1) {
timer::setAwkTimer(5000, []() {
if( queue::isCreateBlock() ) {
if (peer_service::self_state::isLeader()) {
if (1/*peer_service::self_state::isLeader()*/) {
auto block = queue::getBlock();
// ToDo send leader node
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/ordering/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace ordering{

// Temporary implement :: Until Implement Remove Function
while( !tx_queue.empty() ) {
if( tx_queue.top().header().create_time() >= _pre_created ) break
if( tx_queue.top().header().created_time() >= _pre_created ) break;
tx_queue.pop();
}
}
Expand Down
13 changes: 7 additions & 6 deletions core/peer_service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
# transaction_repository
#)

ADD_LIBRARY(membership_service STATIC
ADD_LIBRARY(peer_service STATIC
peer_service.cpp
monitor.cpp
change_state.cpp
self_state.cpp
)

target_link_libraries(membership_service
target_link_libraries(peer_service
timer
exception
logger
config_manager
cache_map
flatbuffer_service
connection_with_grpc_flatbuffer
hash
signature
)
93 changes: 71 additions & 22 deletions core/peer_service/change_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "change_state.hpp"
#include <peer_service/change_state.hpp>
#include <peer_service/monitor.hpp>

#include <unordered_set>

namespace peer_service{
namespace change_state{

// This scope is issue transaction
namespace transtion {
// invoke to issue transaction
void add(const std::string &ip, const peer::Node &){
void add(const std::string &ip, const Node &){

}
void remove(const std::string &ip, const std::string &){
Expand All @@ -33,50 +36,96 @@ namespace peer_service{
void changeTrust(const std::string &ip, const std::string &, const double &){

}
void setActive(const std::string &ip, const std::string &, const bool active){
void setActive(const std::string &ip, const std::string &, const State state){

}
}

// This scope is validation
namespace validation {
bool add(const peer::Node &){

bool add(const Node &peer){
if (monitor::isExistIP(peer._ip))
return false;
if (monitor::isExistPublicKey(peer._public_key))
return false;
return true;
}
bool remove(const std::string &){

bool remove(const std::string &publicKey){
if (!monitor::isExistPublicKey(publicKey))
return false;
return true;
}
bool setTrust(const std::string &, const double &){

bool setTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;
return true;
}
bool changeTrust(const std::string &, const double &){

bool changeTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;
return true;
}
bool setActive(const std::string &, const bool active){

bool setActive(const std::string &publicKey, const State state){
if (!monitor::isExistPublicKey(publicKey))
return false;
return true;
}
}

// This scope is runtime
namespace runtime {
bool add(const peer::Node &){

bool add(const Node &peer){
if (monitor::isExistIP(peer._ip))
return false;
if (monitor::isExistPublicKey(peer._public_key))
return false;
_peer_list.emplace_back( std::make_shared<Node>(peer) );
return true;
}
bool remove(const std::string &){

bool remove(const std::string &publicKey){
if (!monitor::isExistPublicKey(publicKey))
return false;
_peer_list.erase( monitor::findPeerPublicKey(publicKey) );
return true;
}
bool setTrust(const std::string &, const double &){

bool setTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;
monitor::findPeerPublicKey(publicKey)->get()->_trust = trust;
return true;
}
bool changeTrust(const std::string &publicKey, const double &trust){
if (!monitor::isExistPublicKey(publicKey))
return false;
monitor::findPeerPublicKey(publicKey)->get()->_trust += trust;
return true;
}
bool changeTrust(const std::string &, const double &){
bool setActive(const std::string &publicKey, const State state){
if (!monitor::isExistPublicKey(publicKey))
return false;

monitor::findPeerPublicKey(publicKey)->get()->_state = state;
update();
}
bool setActive(const std::string &, const bool active){


void update(){
std::unordered_set<std::string> tmp_active;

for( auto it = _active_peer_list.begin(); it != _active_peer_list.end(); it++ ){
if( it->get()->_state != ACTIVE )
it = _active_peer_list.erase(it);
else
tmp_active.insert( it->get()->_public_key );
}

for( auto it = _peer_list.begin(); it != _peer_list.end(); it++ ){
if( it->get()->_state == ACTIVE && !tmp_active.count(it->get()->_public_key) )
_active_peer_list.emplace_back( *it );
}
}
}

};
};

#endif //IROHA_MONITOR_HPP
14 changes: 8 additions & 6 deletions core/peer_service/change_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,31 @@ namespace peer_service{
// This scope is issue transaction
namespace transtion {
// invoke to issue transaction
void add(const std::string &ip, const peer::Node &);
void add(const std::string &ip, const Node &);
void remove(const std::string &ip, const std::string &);
void setTrust(const std::string &ip, const std::string &, const double &);
void changeTrust(const std::string &ip, const std::string &, const double &);
void setActive(const std::string &ip, const std::string &, const bool active);
void setActive(const std::string &ip, const std::string &, const State state);
}

// This scope is validation
namespace validation {
bool add(const peer::Node &);
bool add(const Node &);
bool remove(const std::string &);
bool setTrust(const std::string &, const double &);
bool changeTrust(const std::string &, const double &);
bool setActive(const std::string &, const bool active);
bool setActive(const std::string &, const State state);
}

// This scope is runtime
namespace runtime {
bool add(const peer::Node &);
bool add(const Node &);
bool remove(const std::string &);
bool setTrust(const std::string &, const double &);
bool changeTrust(const std::string &, const double &);
bool setActive(const std::string &, const bool active);
bool setActive(const std::string &, const State state);

void update();
}
};
};
Expand Down
78 changes: 58 additions & 20 deletions core/peer_service/monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,99 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

#include "monitor.hpp"
#include <peer_service/monitor.hpp>
#include <peer_service/self_state.hpp>


#include <algorithm>

namespace peer_service{
namespace monitor{

Nodes peerList;
std::shared_ptr<peer::Node> getCurrentLeader(){

std::shared_ptr<Node> getCurrentLeader(){
return getActivePeerAt(0);
}
std::string getCurrentLeaderIp(){

return getActivePeerAt(0)->_ip;
}


void initialize(){

if( !_peer_list.empty() ) return;
// TODO Read config.json

// At First myself only
_peer_list.emplace_back(
std::make_shared<Node>(
self_state::getIp(),
self_state::getPublicKey(),
self_state::getName(),
self_state::getTrust(),
self_state::getState()
)
);
}

size_t getMaxFaulty(){

return std::max(0, (getActivePeerSize() - 1) / 3);
}

Nodes getAllPeerList(){

return _peer_list;
}
std::shared_ptr<peer::Node> getPeerAt(unsigned int index){

std::shared_ptr<Node> getPeerAt(unsigned int index){
try {
return _peer_list.at(index);
} catch( const std::out_of_range& oor ){
// TODO Out ot Range Exception
}
}
std::vector<std::string> getAllIpList(){

std::vector<std::string> ret;
for( auto v : _peer_list )
ret.emplace_back( v->_ip );
return ret;
}

Nodes getActivePeerList(){

return _active_peer_list;
}
std::shared_ptr<peer::Node> getActivePeerAt(unsigned int index){

std::shared_ptr<Node> getActivePeerAt(unsigned int index){
try {
return _active_peer_list.at(index);
} catch( const std::out_of_range& oor ){
// TODO Out ot Range Exception
}
}
std::vector<std::string> getActiveIpList(){

std::vector<std::string> ret;
for( auto v : _active_peer_list )
ret.emplace_back( v->_ip );
return ret;
}
int getActivePeerSize(){
return _active_peer_list.size();
}


bool isExistIP(const std::string &){

bool isExistIP(const std::string &ip){
return findPeerIP(ip) != _peer_list.end();
}
bool isExistPublicKey(const std::string &){

bool isExistPublicKey(const std::string &publicKey){
return findPeerPublicKey(publicKey) != _peer_list.end();
}

Nodes::iterator findPeerIP(const std::string &ip){

initialize();
return std::find_if(_peer_list.begin(), _peer_list.end(),
[&ip](const auto &p) { return p->_ip == ip; });
}
Nodes::iterator findPeerPublicKey(const std::string &publicKey){

initialize();
return std::find_if(
_peer_list.begin(), _peer_list.end(),
[&publicKey](const auto &p) { return p->_public_key == publicKey; });
}

}; // namespace monitor
Expand Down
8 changes: 4 additions & 4 deletions core/peer_service/monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ limitations under the License.

#include <peer_service/peer_service.hpp>


namespace peer_service{
namespace monitor{

std::shared_ptr<peer::Node> getCurrentLeader();
std::shared_ptr<Node> getCurrentLeader();
std::string getCurrentLeaderIp();


Expand All @@ -31,12 +30,13 @@ namespace peer_service{
size_t getMaxFaulty();

Nodes getAllPeerList();
std::shared_ptr<peer::Node> getPeerAt(unsigned int index);
std::shared_ptr<Node> getPeerAt(unsigned int index);
std::vector<std::string> getAllIpList();

Nodes getActivePeerList();
std::shared_ptr<peer::Node> getActivePeerAt(unsigned int index);
std::shared_ptr<Node> getActivePeerAt(unsigned int index);
std::vector<std::string> getActiveIpList();
int getActivePeerSize();


bool isExistIP(const std::string &);
Expand Down
3 changes: 2 additions & 1 deletion core/peer_service/peer_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
//
// Created by Takumi Yamashita on 2017/03/16.
//

/*
#include <algorithm>
#include <deque>
#include <regex>
Expand Down Expand Up @@ -344,3 +344,4 @@ bool setActive(const std::string &publicKey, const bool active) {
} // namespace validator
} // namespace transaction
} // namespace peer
*/
Loading

0 comments on commit 5648246

Please sign in to comment.