Skip to content

Commit

Permalink
WIP update / modify method and exception , create validate method
Browse files Browse the repository at this point in the history
  • Loading branch information
satellitex committed Mar 2, 2017
1 parent bccc93d commit e39b965
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 42 deletions.
1 change: 1 addition & 0 deletions core/infra/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_link_libraries(config_manager
exception
logger
connection_with_grpc
event_with_grpc
)

add_library(config_format STATIC
Expand Down
114 changes: 95 additions & 19 deletions core/infra/config/peer_service_with_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include <util/use_optional.hpp>
#include <consensus/connection/connection.hpp>
#include <json.hpp>
#include <infra/protobuf/api.pb.h>
#include "peer_service_with_json.hpp"
#include "config_format.hpp"
#include "../../util/exception.hpp"
Expand Down Expand Up @@ -78,10 +79,6 @@ bool PeerServiceConfig::isExistPublicKey( const std::string &publicKey ) {
return findPeerPublicKey( std::move(publicKey) ) != peerList.end();
}

std::vector<peer::Node>::iterator PeerServiceConfig::findPeer( peer::Node &peer ) {
return findPeerPublicKey( peer.getPublicKey() );
}

std::vector<peer::Node>::iterator PeerServiceConfig::findPeerIP( const std::string &ip ) {
return std::find_if( peerList.begin(), peerList.end(),
[&ip]( const peer::Node& p ) { return p.getIP() == ip; } );
Expand Down Expand Up @@ -110,28 +107,28 @@ std::vector<std::string> PeerServiceConfig::getIpList() {
return ret_ips;
}

bool PeerServiceConfig::addPeer( peer::Node &peer ) {
bool PeerServiceConfig::addPeer( const peer::Node &peer ) {
try {
if( isExistIP( peer.getIP() ) )
throw exception::service::DuplicationIPAddPeerException(std::move(peer.getIP()), std::move(peer.getPublicKey()), std::move(peer.getTrustScore()));
throw exception::service::DuplicationIPException(peer.getIP());
if( isExistPublicKey( peer.getPublicKey() ) )
throw exception::service::DuplicationPublicKeyAddPeerException(std::move(peer.getIP()), std::move(peer.getPublicKey()), std::move(peer.getTrustScore()));
throw exception::service::DuplicationPublicKeyException(peer.getPublicKey());
peerList.emplace_back( std::move(peer));
} catch( exception::service::DuplicationPublicKeyAddPeerException& e ) {
} catch( exception::service::DuplicationPublicKeyException& e ) {
logger::warning("addPeer") << e.what();
return false;
} catch( exception::service::DuplicationIPAddPeerException& e ) {
} catch( exception::service::DuplicationIPException& e ) {
logger::warning("addPeer") << e.what();
return false;
}
return true;
}

bool PeerServiceConfig::removePeer( peer::Node &peer ) {
bool PeerServiceConfig::removePeer( const std::string& publicKey ) {
try {
auto it = findPeerPublicKey(peer.getPublicKey());
if (it == peerList.end())
throw exception::service::UnExistFindPeerException(std::move(peer.getIP()), std::move(peer.getPublicKey()), std::move(peer.getTrustScore()));
auto it = findPeerPublicKey( publicKey );
if ( !isExistPublicKey( publicKey ) )
throw exception::service::UnExistFindPeerException(publicKey);
peerList.erase(it);
} catch (exception::service::UnExistFindPeerException& e) {
logger::warning("removePeer") << e.what();
Expand All @@ -140,20 +137,99 @@ bool PeerServiceConfig::removePeer( peer::Node &peer ) {
return true;
}

bool PeerServiceConfig::updatePeer( peer::Node &peer ) {
bool PeerServiceConfig::updatePeer( const std::string& publicKey, const std::map<std::string,std::string>& upd ) {
try {
auto it = findPeerPublicKey(peer.getPublicKey());
if (it == peerList.end())
throw exception::service::UnExistFindPeerException(std::move(peer.getIP()), std::move(peer.getPublicKey()), std::move(peer.getTrustScore()));
peerList.erase(it);
addPeer( peer );
auto it = findPeerPublicKey( publicKey );
if (it == peerList.end() )
throw exception::service::UnExistFindPeerException( publicKey );
/*
if ( upd.count( Api::Peer::default_instance().publickey() ) ) { // update publicKey
const std::string& upd_key = upd.at( Api::Peer::default_instance().publickey() );
auto upd_it = findPeerPublicKey(upd_key);
if( upd_it != it && upd_it != peerList.end() ) throw exception::service::DuplicationPublicKeyException(publicKey);
it->setPublicKey( upd_key );
}
if ( upd.count( Api::Peer::default_instance().address() ) ) { // update address
const std::string& upd_ip = upd.at( (std::string)Api::Peer::default_instance().address() );
auto upd_it = findPeerIP(upd_ip);
if( upd_it != it && upd_it != peerList.end() ) throw exception::service::DuplicationIPException(publicKey);
it->setIP( upd_ip );
}
if ( upd.count( "trust" ) ) { // update trust
it->setTrustScore(upd.at( "trust" ));
}*/

} catch ( exception::service::UnExistFindPeerException& e ) {
logger::warning("updatePeer") << e.what();
return false;
} catch( exception::service::DuplicationPublicKeyException& e ) {
logger::warning("updatePeer") << e.what();
} catch ( exception::service::DuplicationIPException& e ) {
logger::warning("udpatePeer") << e.what();
return false;
}
return true;
}


bool PeerServiceConfig::validate_addPeer( const peer::Node& peer ) {
try {
if( isExistIP( peer.getIP() ) )
throw exception::service::DuplicationIPException(std::move(peer.getIP()));
if( isExistPublicKey( peer.getPublicKey() ) )
throw exception::service::DuplicationPublicKeyException(std::move(peer.getPublicKey()));
} catch( exception::service::DuplicationPublicKeyException& e ) {
logger::warning("validate addPeer") << e.what();
return false;
} catch( exception::service::DuplicationIPException& e ) {
logger::warning("validate addPeer") << e.what();
return false;
}
return true;
}
bool PeerServiceConfig::validate_removePeer( const std::string &publicKey ) {
try {
if ( !isExistPublicKey( publicKey ) )
throw exception::service::UnExistFindPeerException(publicKey);
} catch (exception::service::UnExistFindPeerException& e) {
logger::warning("validate removePeer") << e.what();
return false;
}
return true;
}
bool PeerServiceConfig::validate_updatePeer( const std::string& publicKey, const std::map<std::string,std::string>& upd ) {
try {
auto it = findPeerPublicKey( publicKey );
if ( !isExistPublicKey( publicKey ) )
throw exception::service::UnExistFindPeerException(publicKey);


if ( upd.count( Api::Peer::default_instance().publickey() ) ) { // update publicKey
const std::string& upd_key = upd.at( Api::Peer::default_instance().publickey() );
auto upd_it = findPeerPublicKey(upd_key);
if( upd_it != it && upd_it != peerList.end() ) throw exception::service::DuplicationPublicKeyException(publicKey);
}

if ( upd.count( Api::Peer::default_instance().address() ) ) { // update address
const std::string& upd_ip = upd.at( Api::Peer::default_instance().address() );
auto upd_it = findPeerIP(upd_ip);
if( upd_it != it && upd_it != peerList.end() ) throw exception::service::DuplicationIPException(upd_ip);
}
} catch ( exception::service::UnExistFindPeerException& e ) {
logger::warning("updatePeer") << e.what();
return false;
} catch( exception::service::DuplicationPublicKeyException& e ) {
logger::warning("updatePeer") << e.what();
} catch( exception::service::DuplicationIPException& e ) {
logger::warning("udpatePeer") << e.what();
return false;
}
return true;
}


bool PeerServiceConfig::isLeaderMyPeer() {
if( peerList.empty() ) return false;
auto sorted_peers = getPeerList();
Expand Down
20 changes: 13 additions & 7 deletions core/infra/config/peer_service_with_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ class PeerServiceConfig : config::AbstractConfigManager {

bool isExistIP( const std::string& );
bool isExistPublicKey( const std::string& );
std::vector<peer::Node>::iterator findPeerIP( const std::string& );
std::vector<peer::Node>::iterator findPeerPublicKey( const std::string& );
std::vector<peer::Node>::iterator findPeer( peer::Node& );

std::vector<peer::Node>::iterator findPeerIP( const std::string& ip );
std::vector<peer::Node>::iterator findPeerPublicKey( const std::string& publicKey );
protected:
void parseConfigDataFromString(std::string&& jsonStr) override;

Expand All @@ -51,9 +49,17 @@ class PeerServiceConfig : config::AbstractConfigManager {
std::vector<std::unique_ptr<peer::Node>> getPeerList();
std::vector<std::string> getIpList();

bool addPeer( peer::Node& );
bool removePeer( peer::Node& );
bool updatePeer( peer::Node& );
// invoke when execute transaction
bool addPeer( const peer::Node& );
bool removePeer( const std::string &publicKey );
bool updatePeer( const std::string& publicKey, const std::map<std::string,std::string>& );

// invoke when validator transaction
bool validate_addPeer( const peer::Node& );
bool validate_removePeer( const std::string &publicKey );
bool validate_updatePeer( const std::string& publicKey, const std::map<std::string,std::string>& );

// equatl to isSumeragi
bool isLeaderMyPeer();

virtual std::string getConfigName();
Expand Down
10 changes: 10 additions & 0 deletions core/service/peer_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ namespace peer
return trustScore;
}

void setIP( const std::string& ip ) {
this->ip = ip;
}
void setPublicKey( const std::string& publickey ) {
this->publicKey = publickey;
}
void setTrustScore( const double& trustScore ) {
this->trustScore = trustScore;
}

};

std::string getMyIp();
Expand Down
12 changes: 6 additions & 6 deletions core/util/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ namespace exception {
{}

namespace service {
DuplicationIPAddPeerException::DuplicationIPAddPeerException(const std::string& ip, const std::string& publicKey, const double& trustScore) :
std::domain_error("DuplicationIPAddPeerException\nIP = " + ip + "\npublicKey = " + publicKey + "\ntrustScore = " + std::to_string(trustScore) ) {
DuplicationIPException::DuplicationIPException(const std::string& ip) :
std::domain_error("DuplicationIPException : IP = " + ip + "\n") {
}
DuplicationPublicKeyAddPeerException::DuplicationPublicKeyAddPeerException(const std::string& ip, const std::string& publicKey, const double& trustScore) :
std::domain_error("DuplicationPublicKeyAddPeerException\nIP = " + ip + "\npublicKey = " + publicKey + "\ntrustScore = " + std::to_string(trustScore) ) {
DuplicationPublicKeyException::DuplicationPublicKeyException(const std::string& publicKey) :
std::domain_error("DuplicationPublicKeyException : publicKey = " + publicKey + "\n") {
}
UnExistFindPeerException::UnExistFindPeerException(const std::string& ip, const std::string& publicKey, const double& trustScore) :
std::domain_error("DuplicationIPAddPeerException\nIP = " + ip + "\npublicKey = " + publicKey + "\ntrustScore = " + std::to_string(trustScore) ) {
UnExistFindPeerException::UnExistFindPeerException(const std::string& publicKey) :
std::domain_error("UnExistFindPeerException : publicKey = " + publicKey + "\n") {
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/util/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,17 @@ namespace exception {


namespace service {
class DuplicationIPAddPeerException : public std::domain_error {
class DuplicationIPException : public std::domain_error {
public:
DuplicationIPAddPeerException( const std::string&, const std::string&, const double& );
DuplicationIPException( const std::string& );
};
class DuplicationPublicKeyAddPeerException : public std::domain_error {
class DuplicationPublicKeyException : public std::domain_error {
public:
DuplicationPublicKeyAddPeerException( const std::string&, const std::string&, const double& );
DuplicationPublicKeyException( const std::string& );
};
class UnExistFindPeerException : public std::domain_error {
public:
UnExistFindPeerException( const std::string&, const std::string&, const double& );
UnExistFindPeerException( const std::string& );
};
}

Expand Down
1 change: 1 addition & 0 deletions test/infra/config/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ target_link_libraries(peer_service_with_json_test
consensus_event
signature
config_manager
event_with_grpc
)
add_test(
NAME peer_service_with_json_test
Expand Down
20 changes: 15 additions & 5 deletions test/infra/config/peer_service_with_json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include <gtest/gtest.h>
#include <vector>
#include "../../../core/infra/config/peer_service_with_json.hpp"
#include <infra/protobuf/api.pb.h>
#include "../../../core/service/peer_service.hpp"

TEST( peer_service_with_json_test, initialize_peer_test ) {
Expand All @@ -31,15 +32,20 @@ TEST( peer_service_with_json_test, initialize_peer_test ) {
std::cout << peer->getPublicKey() << std::endl;
std::cout << peer->getTrustScore() << std::endl;
}
std::cout << "API:: address = " << Api::Peer::default_instance().address() << std::endl;
std::cout << "API:: publicKey = " << Api::Peer::default_instance().publickey() << std::endl;
}

TEST( peer_service_with_json_test, add_peer_test ) {
int n = config::PeerServiceConfig::getInstance().getPeerList().size();
peer::Node peer1 = peer::Node( "ip_low", "publicKey1", 0.5 );
peer::Node peer2 = peer::Node( "ip_high", "publicKey2", 1.5 );
peer::Node peer3 = peer::Node( "ip_high", "publicKey1", 1.5 );
ASSERT_TRUE( config::PeerServiceConfig::getInstance().validate_addPeer( peer1 ) );
ASSERT_TRUE( config::PeerServiceConfig::getInstance().addPeer( peer1 ) );
ASSERT_TRUE( config::PeerServiceConfig::getInstance().validate_addPeer( peer2 ) );
ASSERT_TRUE( config::PeerServiceConfig::getInstance().addPeer( peer2 ) );
ASSERT_FALSE( config::PeerServiceConfig::getInstance().validate_addPeer( peer3 ) );
ASSERT_FALSE( config::PeerServiceConfig::getInstance().addPeer( peer3 ) );
std::vector<std::unique_ptr<peer::Node>> peers = config::PeerServiceConfig::getInstance().getPeerList();
for( auto&& peer : peers ) {
Expand All @@ -50,6 +56,7 @@ TEST( peer_service_with_json_test, add_peer_test ) {
ASSERT_TRUE( peers.size() == n+2 );
}

/*
TEST( peer_service_with_json_test, update_peer_test ) {
int n = config::PeerServiceConfig::getInstance().getPeerList().size();
const std::string upd_ip = "updated_ip";
Expand All @@ -71,12 +78,15 @@ TEST( peer_service_with_json_test, update_peer_test ) {
}
ASSERT_TRUE( peers.size() == n );
}
*/

TEST( peer_service_with_json_test, remove_peer_test ) {
int n = config::PeerServiceConfig::getInstance().getPeerList().size();
peer::Node peer = peer::Node( "updated_ip", "publicKey1" );
ASSERT_TRUE( config::PeerServiceConfig::getInstance().removePeer( peer ) );
ASSERT_FALSE( config::PeerServiceConfig::getInstance().removePeer( peer ) );
const std::string rm_key = "publicKey1";
ASSERT_TRUE( config::PeerServiceConfig::getInstance().validate_removePeer( rm_key ) );
ASSERT_TRUE( config::PeerServiceConfig::getInstance().removePeer( rm_key ) );
ASSERT_FALSE( config::PeerServiceConfig::getInstance().validate_removePeer( rm_key ) );
ASSERT_FALSE( config::PeerServiceConfig::getInstance().removePeer( rm_key ) );
std::vector<std::unique_ptr<peer::Node>> peers = config::PeerServiceConfig::getInstance().getPeerList();
for( auto&& peer : peers ) {
std::cout << peer->getIP() << std::endl;
Expand All @@ -91,8 +101,8 @@ TEST( peer_service_with_json_test, leder_peer_check_test ) {
std::vector<std::unique_ptr<peer::Node>> peers = config::PeerServiceConfig::getInstance().getPeerList();
ASSERT_FALSE( config::PeerServiceConfig::getInstance().isLeaderMyPeer() );
for( auto &&peer : peers ) {
if( peer->getIP() != config::PeerServiceConfig::getInstance().getMyIp() ) {
ASSERT_TRUE( config::PeerServiceConfig::getInstance().removePeer( *peer ) );
if( peer->getPublicKey() != config::PeerServiceConfig::getInstance().getMyPublicKey() ) {
ASSERT_TRUE( config::PeerServiceConfig::getInstance().removePeer( peer->getPublicKey() ) );
}
}
ASSERT_TRUE( config::PeerServiceConfig::getInstance().isLeaderMyPeer() );
Expand Down

0 comments on commit e39b965

Please sign in to comment.