Skip to content

Commit

Permalink
Modify for minor code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
motxx committed Jan 5, 2017
1 parent 40a2acf commit 624f761
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 81 deletions.
26 changes: 13 additions & 13 deletions core/infra/connection/connection_with_aeron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ namespace connection {
bool subscription_running(true);

fragment_handler_t receiveMessage() {
return [&](AtomicBuffer& buffer, util::index_t offset, util::index_t length, Header& header) {
return [](AtomicBuffer& buffer, util::index_t offset, util::index_t length, Header& header) {
std::string raw_data = std::string((char *)buffer.buffer() + offset, (unsigned long)length);
// WIP parse json.
// logger::info("receive", raw_data);
for(auto& f : receivers) {
f( peer::getMyIp(), raw_data);
f(peer::getMyIp(), raw_data);
}
};
}
Expand Down Expand Up @@ -93,7 +93,7 @@ namespace connection {
}

fragment_handler_t handler = receiveMessage();
subscription_thread = std::thread([subscription,handler](){
subscription_thread = std::thread([subscription, handler](){
while (subscription_running){
const int fragmentsRead = subscription->poll(handler, FRAGMENTS_LIMIT);
}
Expand All @@ -108,27 +108,27 @@ namespace connection {
return -1;
}
}

void addPublication(std::string ip) {
std::int64_t pid = aeron->addPublication( "aeron:udp?endpoint="+ip+":40123", streamId);
auto publication = aeron->findPublication(pid);
std::int64_t pid = aeron->addPublication("aeron:udp?endpoint=" + ip + ":40123", streamId);
auto publication = aeron->findPublication(pid);
while (!publication) {
std::this_thread::yield();
publication = aeron->findPublication(pid);
}
logger::info("connection", "publication ["+ ip +"]");
publications.insert(std::pair<std::string, std::shared_ptr<Publication>>{ ip, publication});
logger::info("connection", "publication [" + ip + "]");
publications.emplace(ip, publication);
}

bool send(const std::string& to,const std::string& msg) {
bool send(const std::string& to, const std::string& msg) {
logger::info("connection", "Start send");
if(publications.find(to) == publications.end()){
logger::error("connection", to + " is not registerd");
return false;
}
try{
char* message = const_cast<char*>(msg.c_str());
AERON_DECL_ALIGNED(std::uint8_t buffer[4096], 16);
auto message = const_cast<char*>(msg.c_str());
AERON_DECL_ALIGNED(std::uint8_t buffer[4096], 16);
AtomicBuffer srcBuffer(&buffer[0], 4096);
srcBuffer.putBytes(0, reinterpret_cast<std::uint8_t *>(message), strlen(message));
const std::int64_t result = publications[to]->offer(srcBuffer, 0, strlen(message));
Expand Down Expand Up @@ -161,7 +161,7 @@ namespace connection {
logger::info("connection", "send mesage publlications "+ std::to_string(publications.size()));
for(auto& p : publications){
if(p.first != peer::getMyIp()){
send( p.first, msg);
send(p.first, msg);
}
}
return true;
Expand All @@ -171,4 +171,4 @@ namespace connection {
receivers.push_back(callback);
return true;
}
};
};
4 changes: 2 additions & 2 deletions core/infra/protobuf/convertor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace convertor{
auto name = aAccount.name();
std::vector<std::tuple<std::string,long>> assets;
for(const Event::Asset& as: aAccount.assets()){
assets.push_back(std::make_pair(as.name(),(long)as.value()));
assets.emplace_back(as.name(), static_cast<long>(as.value()));
}
return object::Account(
std::move(publicKey),
Expand Down Expand Up @@ -116,7 +116,7 @@ namespace convertor{
auto publicKey = tx.account().publickey();
std::vector<std::tuple<std::string,long>> assets;
for(auto&& as: tx.account().assets()){
assets.push_back(std::make_pair( as.name(), as.value()));
assets.emplace_back(as.name(), as.value());
}
auto issuer = tx.senderpubkey();
auto res = ConsensusEvent<
Expand Down
24 changes: 12 additions & 12 deletions core/infra/server/http_server_with_cappuccino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace http {

json responseError(std::string message){
return json({
{"message", message},
{"message", std::move(message)},
{"status", 400}
});
}
Expand Down Expand Up @@ -82,12 +82,12 @@ namespace http {
if(!data.empty()){
try{

auto publicKey = data["publicKey"].get<std::string>();
auto alias = data["alias"].get<std::string>();
auto timestamp = data["timestamp"].get<int>();
const auto publicKey = data["publicKey"].get<std::string>();
const auto alias = data["alias"].get<std::string>();
const auto timestamp = data["timestamp"].get<int>();

uuid = hash::sha3_256_hex(publicKey);
if(repository::account::findByUuid(uuid).publicKey == "") {
if(repository::account::findByUuid(uuid).publicKey.empty()) {

auto event = ConsensusEvent < Transaction < Add < object::Account >> > (
publicKey.c_str(),
Expand Down Expand Up @@ -164,13 +164,13 @@ namespace http {
auto data = request->json();
if(!data.empty()){
try{
auto assetUuid = data["asset-uuid"].get<std::string>();
auto timestamp = data["timestamp"].get<int>();
auto signature = data["signature"].get<std::string>();
auto command = data["params"]["command"].get<std::string>();
auto value = data["params"]["value"].get<std::string>();
auto sender = data["params"]["sender"].get<std::string>();
auto receiver = data["params"]["receiver"].get<std::string>();
const auto assetUuid = data["asset-uuid"].get<std::string>();
const auto timestamp = data["timestamp"].get<int>();
const auto signature = data["signature"].get<std::string>();
const auto command = data["params"]["command"].get<std::string>();
const auto value = data["params"]["value"].get<std::string>();
const auto sender = data["params"]["sender"].get<std::string>();
const auto receiver = data["params"]["receiver"].get<std::string>();

auto event = ConsensusEvent<Transaction<Transfer<Asset>>>(
sender.c_str(),
Expand Down
35 changes: 10 additions & 25 deletions core/infra/smart_contract/jvm/java_virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,18 @@ namespace smart_contract {

std::cout << "-Djava.class.path=" + std::string(getenv("IROHA_HOME")) +
"/smart_contract/" + contractName <<"/ "<< contractName.c_str() << std::endl;

//options[0].optionString = (char*)"-Djava.security.manager -Djava.security.policy=policy.txt -Djava.class.path=./contract/";

std::vector<std::string> optionStrings = {
"-Djava.class.path=" + std::string(getenv("IROHA_HOME"))
+ "/smart_contract",
"-Djava.security.manager",
"-Djava.security.policy=policy.txt"
};

std::vector<std::vector<char>> optionCharStrings;
for(const auto& s: optionStrings) {
optionCharStrings.emplace_back(s.begin(), s.end());
optionCharStrings.back().push_back('\0');
}

auto options = std::make_unique<JavaVMOption[]>(optionStrings.size());
for(unsigned i=0; i<optionCharStrings.size(); i++) {
options[i].optionString = optionCharStrings[i].data();
}
JavaVMOption options[3];
options[0].optionString = const_cast<char*>(("-Djava.class.path=" + std::string(getenv("IROHA_HOME")) + "/smart_contract").c_str());
options[1].optionString = const_cast<char*>("-Djava.security.manager");
options[2].optionString = const_cast<char*>("-Djava.security.policy=policy.txt");

JavaVMInitArgs vm_args;
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options.get();
vm_args.nOptions = optionStrings.size();
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 3;
//vm_args.ignoreUnrecognized = true;

JNIEnv *env;
JavaVM *jvm;

Expand All @@ -80,15 +65,15 @@ namespace smart_contract {
std::cout << "could not found class : " << contractName << std::endl;
return nullptr;
}

jmethodID cns = env->GetMethodID(cls, "<init>", "()V");
if (cns == nullptr) {
std::cout << "could not get <init> method." << std::endl;
return nullptr;
}

jobject obj = env->NewObject(cls, cns);

return std::make_unique<JavaContext>(
env,
jvm,
Expand Down
3 changes: 1 addition & 2 deletions core/infra/smart_contract/jvm/java_virtual_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ namespace smart_contract {
void execFunction(
const std::unique_ptr<JavaContext> &context,
std::string functionName,
std::unordered_map<std::string,
std::string> params
std::unordered_map<std::string, std::string> params
);

}
Expand Down
10 changes: 5 additions & 5 deletions core/infra/smart_contract/smart_contract_with_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ namespace smart_contract {
std::map<std::string, std::unique_ptr<JavaContext>> vmSet;

void SmartContract::initializeVM(const std::string& contractName){
vmSet.insert(std::make_pair( contractName, smart_contract::initializeVM(contractName)));
vmSet.emplace(contractName, smart_contract::initializeVM(contractName));
}

void SmartContract::finishVM(const std::string& contractName){
if(vmSet.find(contractName) != vmSet.end()){
vmSet.erase(contractName);
}
if(vmSet.find(contractName) != vmSet.end()){
vmSet.erase(contractName);
}
}

void SmartContract::invokeFunction(
Expand All @@ -26,7 +26,7 @@ namespace smart_contract {
) {
if(vmSet.find(contractName) != vmSet.end()){
const auto& context = vmSet.at(contractName);
smart_contract::execFunction( context, functionName, params);
smart_contract::execFunction(context, functionName, params);
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions core/model/commands/transfer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ namespace command {
std::string&& receiver,
Args&&... args
):
T(std::forward<Args>(args)...),
senderPublicKey(std::move(sender)),
receiverPublicKey(std::move(receiver)),
T(std::forward<Args>(args)...)
receiverPublicKey(std::move(receiver))
{}

auto getCommandName() const {
Expand Down
6 changes: 3 additions & 3 deletions core/repository/consensus/merkle_transaction_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ namespace merkle_transaction_repository {
return pevent->transaction().hash();
}

std::string parent = repository::world_state_repository::find(lastInsertion + "_parent");
std::string leftChild = repository::world_state_repository::find(parent + "_leftChild");
std::string rightChild = repository::world_state_repository::find(parent + "_rightChild");
std::string parent = repository::world_state_repository::find(lastInsertion + "_parent");
std::string leftChild = repository::world_state_repository::find(parent + "_leftChild");
std::string rightChild = repository::world_state_repository::find(parent + "_rightChild");

if (rightChild.empty()) {
// insert the event's transaction as the right child
Expand Down
16 changes: 8 additions & 8 deletions core/repository/consensus/merkle_transaction_repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ struct MerkleNode {
}

MerkleNode(
const std::string hash,
const std::string parent,
const std::string leftChild,
const std::string rightChild
std::string hash,
std::string parent,
std::string leftChild,
std::string rightChild
):
hash(hash),
parent(parent),
left(leftChild),
right(rightChild)
hash(std::move(hash)),
parent(std::move(parent)),
left(std::move(leftChild)),
right(std::move(rightChild))
{}

bool isRoot() {
Expand Down
17 changes: 10 additions & 7 deletions core/repository/consensus/transaction_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See the License for the specific language governing permissions and
#include "../../crypto/base64.hpp"
#include <string>
#include <vector>
#include <stdexcept>

namespace repository{

Expand Down Expand Up @@ -73,16 +74,18 @@ namespace repository{
}else if(tx.has_domain()){
auto domain = tx.domain().ownerpublickey() + s2 + tx.domain().name();
return transaction + "DOMAIN" + s + domain;
}else {
throw std::logic_error("Not implemented other than account/asset/domain");
}
}

Event::Transaction s2t(std::string message){
auto main = split( message, s);
auto main = split(message, s);
Event::Transaction tx;
tx.set_type(main[0]);
tx.set_hash(main[1]);
for(auto&& sig: split( main[2], s2)){
auto pub_sig = split( sig, s3);
for(auto&& sig: split(main[2], s2)){
auto pub_sig = split(sig, s3);
if(pub_sig.size() != 2){
break;
}
Expand All @@ -98,7 +101,7 @@ namespace repository{
tx.mutable_account()->set_publickey(account[0]);
tx.mutable_account()->set_name(account[1]);
for(auto&& as: split(account[3], s3)){
auto asset = split(as,s4);
auto asset = split(as, s4);
if(asset.size() != 2){
break;
}
Expand Down Expand Up @@ -139,13 +142,13 @@ namespace repository{
}

Event::Transaction find(std::string key){
std::string txKey = "transaction_"+key;
std::string txKey = "transaction_" + key;
return s2t(world_state_repository::find(txKey));
}


std::vector<Event::Transaction> findByAssetName(std::string name){
std::string txKey = "transaction_"+name+"_";
std::string txKey = "transaction_" + name + "_";
auto data = world_state_repository::findByPrefix(txKey);
std::vector<Event::Transaction> res;
for(auto& s: data){
Expand All @@ -155,4 +158,4 @@ namespace repository{
}

};
};
};
4 changes: 2 additions & 2 deletions core/util/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace random_service{

std::string makeRandomHash() {
std::random_device rd;
return hash::sha3_256_hex(std::to_string(rd()));
static std::mt19937 mt(std::random_device{}());
return hash::sha3_256_hex(std::to_string(mt()));
}

}; // namespace random_service

0 comments on commit 624f761

Please sign in to comment.