Skip to content

Commit

Permalink
Merge branch 'develop' into feature/docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
MizukiSonoko committed May 3, 2017
2 parents 5ff1183 + 65e3b2b commit fbc502d
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ ADD_LIBRARY(repository STATIC
target_link_libraries(repository
ametsuchi
flatbuffer_service
connection_with_grpc_flatbuffer
)
3 changes: 3 additions & 0 deletions core/ametsuchi/repository.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace repository{

const std::string getMerkleRoot();

namespace front_repository{
void initialize_repository();
}
namespace permission{
iroha::AccountPermissionRoot getPermissionRootOf(const flatbuffers::String &key);
std::vector<const iroha::AccountPermissionLedger*> getPermissionLedgerOf(const flatbuffers::String &key);
Expand Down
23 changes: 22 additions & 1 deletion core/ametsuchi/respository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

#include <infra/ametsuchi/include/ametsuchi/ametsuchi.h>
#include <service/flatbuffer_service.h>
#include <main_generated.h>
#include <memory>
#include <service/connection.hpp>
#include <main_generated.h>
#include <endpoint_generated.h>
#include <asset_generated.h>

namespace repository {

Expand Down Expand Up @@ -78,4 +81,22 @@ namespace repository {

}

namespace front_repository{
void initialize_repository(){
connection::iroha::AssetRepositoryImpl::AccountGetAsset::receive([=](
const std::string & /* from */, flatbuffers::unique_ptr_t &&query_ptr) -> std::vector<const ::iroha::Asset *>{
if(db == nullptr) init();
const iroha::AssetQuery& query = *flatbuffers::GetRoot<iroha::AssetQuery>(query_ptr.get());
auto ln = query.ledger_name();
auto dn = query.domain_name();
auto an = query.asset_name();
if(ln == nullptr || dn == nullptr || an == nullptr) {
return db->accountGetAllAssets(query.pubKey(), query.uncommitted());
}else{
std::vector<const ::iroha::Asset *> res{db->accountGetAsset(query.pubKey(), ln, dn, an, query.uncommitted())};
return res;
}
});
}
}
};
126 changes: 123 additions & 3 deletions core/infra/connection/connection_with_grpc_flatbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ limitations under the License.
#include <memory>
#include <string>
#include <vector>
#include <asset_generated.h>

namespace connection {
/**
Expand All @@ -43,6 +44,8 @@ namespace connection {
using ConsensusEvent = ::iroha::ConsensusEvent;
using Ping = ::iroha::Ping;
using Response = ::iroha::Response;
using AssetResponse = ::iroha::AssetResponse;
using AssetQuery = ::iroha::AssetQuery;
using Transaction = ::iroha::Transaction;
using TransactionWrapper = ::iroha::TransactionWrapper;
using Signature = ::iroha::Signature;
Expand Down Expand Up @@ -90,6 +93,29 @@ namespace connection {
std::shared_ptr<CallBackFunc> receiver_;
};

template<class CallBackFunc, class T>
class ReceiverWithReturen {
public:
VoidHandler set(CallBackFunc &&rhs) {
if (receiver_) {
return makeUnexpected(exception::DuplicateSetArgumentException(
"Receiver<" + std::string(typeid(CallBackFunc).name()) + ">",
__FILE__));
}

receiver_ = std::make_shared<CallBackFunc>(rhs);
return {};
}

// ToDo rewrite operator() overload.
T invoke(const std::string &from, flatbuffers::unique_ptr_t &&arg) {
return (*receiver_)(from, std::move(arg));
}

private:
std::shared_ptr<CallBackFunc> receiver_;
};

/**
* Verify
*/
Expand Down Expand Up @@ -247,9 +273,9 @@ namespace connection {

fbb.Finish(event.value());

const std::string from = "from";
connection::iroha::SumeragiImpl::Verify::receiver.invoke(
from, std::move(fbb.ReleaseBufferPointer()));
const std::string from = "from";
connection::iroha::SumeragiImpl::Verify::receiver.invoke(
from, std::move(fbb.ReleaseBufferPointer()));
}

auto tx_str = flatbuffer_service::toString(
Expand Down Expand Up @@ -489,6 +515,100 @@ namespace connection {
} // namespace Sumeragi Impl
} // namespace MemberShipService

/************************************************************************************
* AssetRepository
************************************************************************************/
namespace iroha {
namespace AssetRepositoryImpl {
namespace AccountGetAsset {
// ToDo more clear
ReceiverWithReturen<AccountGetAsset::CallBackFunc, std::vector<const ::iroha::Asset *>> receiver;

void receive(AccountGetAsset::CallBackFunc&& callback) {
receiver.set(std::move(callback));
}
}
}
}
/**
* AssetRepositoryConnectionServiceImpl
*/
class AssetRepositoryConnectionServiceImpl final : public ::iroha::AssetRepository::Service {
public:
Status AccountGetAsset(ServerContext *context,
const flatbuffers::BufferRef<::iroha::AssetQuery> *requestRef,
flatbuffers::BufferRef<::iroha::AssetResponse> *responseRef) override {
fbbResponse.Clear();
std::vector<const ::iroha::Asset *> assets;
{
const auto q = requestRef->GetRoot();
flatbuffers::FlatBufferBuilder fbb;
auto req_offset = ::iroha::CreateAssetQueryDirect(fbb,
q->pubKey()->c_str(),
q->ledger_name()->c_str(),
q->domain_name()->c_str(),
q->asset_name()->c_str(),
q->uncommitted()
);

fbb.Finish(req_offset);
assets = connection::iroha::AssetRepositoryImpl::AccountGetAsset::receiver.invoke(
"from", // TODO: Specify 'from'
fbb.ReleaseBufferPointer()
);

std::vector<uint8_t> types;
std::vector<flatbuffers::Offset<::iroha::Asset>> res_assets;
{
flatbuffers::FlatBufferBuilder fbb_;
for(const ::iroha::Asset* asset: assets){
if(asset->asset_type() == ::iroha::AnyAsset::Currency){
fbb_.Clear();
res_assets.push_back(::iroha::CreateAsset(fbb_,
asset->asset_type(),
::iroha::CreateCurrencyDirect(fbb_,
asset->asset_as_Currency()->currency_name()->c_str(),
asset->asset_as_Currency()->domain_name()->c_str(),
asset->asset_as_Currency()->ledger_name()->c_str(),
asset->asset_as_Currency()->description()->c_str(),
asset->asset_as_Currency()->amount()->c_str(),
asset->asset_as_Currency()->precision()
).Union()
));
}
}
}
auto responseOffset = ::iroha::CreateAssetResponseDirect(
fbbResponse, "Success", ::iroha::Code::COMMIT,
&res_assets
);
fbbResponse.Finish(responseOffset);

*responseRef = flatbuffers::BufferRef<AssetResponse>(
fbbResponse.GetBufferPointer(), fbbResponse.GetSize());
}
return Status::OK;
}
private:
flatbuffers::Offset<::iroha::Signature> sign(
flatbuffers::FlatBufferBuilder &fbb, const std::string &tx) {
const auto stamp = datetime::unixtime();
const auto hashWithTimestamp =
hash::sha3_256_hex(tx + std::to_string(stamp));
const auto signature = signature::sign(
hashWithTimestamp,
config::PeerServiceConfig::getInstance().getMyPublicKey(),
config::PeerServiceConfig::getInstance().getMyPrivateKey());
const std::vector<uint8_t> sigblob(signature.begin(), signature.end());
return ::iroha::CreateSignatureDirect(
fbb, config::PeerServiceConfig::getInstance().getMyPublicKey().c_str(),
&sigblob, stamp);
};

flatbuffers::FlatBufferBuilder fbbResponse;
};


/************************************************************************************
* Run server
************************************************************************************/
Expand Down
13 changes: 13 additions & 0 deletions core/service/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

#include <utils/expected.hpp>

#include <main_generated.h>

#include <functional>
#include <memory>
#include <string>
Expand Down Expand Up @@ -82,6 +84,17 @@ namespace HostDiscovery {
}
} // namespace iroha::SumeragiImpl::Verify

namespace iroha {
namespace AssetRepositoryImpl {
namespace AccountGetAsset {
using CallBackFunc = std::function<std::vector<const ::iroha::Asset *>(
const std::string & /* from */, flatbuffers::unique_ptr_t && /* message */)>;

void receive(AccountGetAsset::CallBackFunc &&callback);
}
}
} // namespace iroha::AssetRepositoryImpl

/************************************************************************************
* Kagami ( means Ping )
************************************************************************************/
Expand Down
1 change: 1 addition & 0 deletions peer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ target_link_libraries(iroha-main
pthread
thread_pool
json
repository
)
2 changes: 2 additions & 0 deletions peer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include <consensus/sumeragi.hpp>
#include <infra/config/peer_service_with_json.hpp>
#include <utils/logger.hpp>
#include <ametsuchi/repository.hpp>

std::atomic_bool running(true);

Expand All @@ -44,6 +45,7 @@ int main() {
logger::setLogLevel(logger::LogLevel::Debug);

connection::initialize_peer();
repository::front_repository::initialize_repository();
sumeragi::initializeSumeragi();
// peer::izanami::startIzanami();

Expand Down
2 changes: 1 addition & 1 deletion schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ target_link_libraries(endpoint_fbs
pthread
dl
flatbuffers
)
)
21 changes: 21 additions & 0 deletions schema/endpoint.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ table Ping {
sender: string;
}

table AssetQuery{
pubKey: string;
ledger_name: string;
domain_name: string;
asset_name: string;
uncommitted: bool;
}

table AssetResponse {
message: string (required);
code: Code;
assets: [Asset];
}

// Used by sending transaction
rpc_service Sumeragi {

Expand All @@ -26,6 +40,13 @@ rpc_service Sumeragi {
Verify(ConsensusEvent):Response (streaming: "none");
}

// Used by sending transaction
rpc_service AssetRepository {

AccountGetAsset(AssetQuery):AssetResponse (streaming: "none");

}

rpc_service Hijiri {
// Kagami means ping
Kagami(Ping):Response (streaming: "none");
Expand Down
6 changes: 3 additions & 3 deletions schema/main.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ table TransactionWrapper {

// TODO
table TransactionResponse {
message: string;
code: Code;
transaction: Transaction;
message: string (required);
code: Code;
transactions: [TransactionWrapper];
}

table ReceiverConfirmation {
Expand Down
16 changes: 16 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ target_link_libraries(test_sumeragi
connection_with_grpc_flatbuffer
)

###########################
# Ametsuchi test #
###########################

add_executable(check_ametsuchi
check_ametsuchi.cpp
)

target_link_libraries(check_ametsuchi
connection_with_grpc_flatbuffer
)

###########################
# Makesumeragi test #
###########################

add_executable(make_sumeragi
make_sumeragi.cpp
)
Expand Down
Loading

0 comments on commit fbc502d

Please sign in to comment.