Skip to content

Commit

Permalink
Merge hyperledger-iroha#712 - 'feature/shared_model-proto-txresp'
Browse files Browse the repository at this point in the history
  • Loading branch information
l4l authored and lebdron committed Dec 19, 2017
2 parents 5b3ac8a + 79350dd commit ba649ae
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 36 deletions.
39 changes: 22 additions & 17 deletions irohad/torii/impl/command_service.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
/*
Copyright 2017 Soramitsu Co., Ltd.
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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 <endpoint.pb.h>
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"
#include "torii/command_service.hpp"
#include "common/types.hpp"
#include "cryptography/ed25519_sha3_impl/internal/sha3_hash.hpp"
#include "endpoint.pb.h"

namespace torii {

Expand All @@ -39,6 +40,7 @@ namespace torii {
auto res = cache_->findItem(iroha_response->tx_hash);
if (not res) {
iroha::protocol::ToriiResponse response;
response.set_tx_hash(iroha_response->tx_hash);
response.set_tx_status(iroha::protocol::NOT_RECEIVED);
cache_->addItem(iroha_response->tx_hash, response);
return;
Expand Down Expand Up @@ -67,6 +69,7 @@ namespace torii {
res->set_tx_status(iroha::protocol::TxStatus::ON_PROCESS);
break;
case iroha::model::TransactionResponse::NOT_RECEIVED:
default:
res->set_tx_status(iroha::protocol::TxStatus::NOT_RECEIVED);
break;
}
Expand All @@ -85,6 +88,7 @@ namespace torii {
}

iroha::protocol::ToriiResponse response;
response.set_tx_hash(tx_hash);
response.set_tx_status(iroha::protocol::TxStatus::ON_PROCESS);

cache_->addItem(tx_hash, response);
Expand All @@ -99,6 +103,7 @@ namespace torii {
if (resp) {
response.CopyFrom(*resp);
} else {
response.set_tx_hash(request.tx_hash());
if (storage_->getBlockQuery()->getTxByHashSync(request.tx_hash())) {
response.set_tx_status(iroha::protocol::TxStatus::COMMITTED);
} else {
Expand Down
1 change: 1 addition & 0 deletions schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ add_library(schema
primitive.pb.cc
queries.pb.cc
responses.pb.cc
endpoint.pb.cc
)
target_link_libraries(schema
protobuf
Expand Down
1 change: 1 addition & 0 deletions schema/endpoint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum TxStatus {

message ToriiResponse {
TxStatus tx_status = 1;
bytes tx_hash = 2;
}

message TxStatusRequest{
Expand Down
46 changes: 46 additions & 0 deletions shared_model/backend/protobuf/common_objects/trivial_proto.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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 "utils/reference_holder.hpp"

namespace shared_model {
namespace proto {
/**
* Simple generic class for handling proto objects
* @tparam Iface is interface to inherit from
* @tparam Proto is protobuf containter
*/
template <typename Iface, typename Proto>
class TrivialProto final : public Iface {
public:
/**
* @tparm ProtoLoader generic param so it can be hanled
* in the load for the boost::variant
*/
template <typename ProtoLoader>
explicit TrivialProto(ProtoLoader &&ref)
: proto_(std::forward<ProtoLoader>(ref)) {}

typename Iface::ModelType *copy() const override {
return new TrivialProto(Proto(*proto_));
}

private:
detail::ReferenceHolder<Proto> proto_;
};
} // namespace proto
} // namespace shared_model
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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 "backend/protobuf/common_objects/trivial_proto.hpp"
#include "endpoint.pb.h"
#include "interfaces/transaction_responses/committed_tx_response.hpp"
#include "interfaces/transaction_responses/stateful_failed_tx_response.hpp"
#include "interfaces/transaction_responses/stateful_valid_tx_response.hpp"
#include "interfaces/transaction_responses/stateless_failed_tx_response.hpp"
#include "interfaces/transaction_responses/stateless_valid_tx_response.hpp"
#include "interfaces/transaction_responses/tx_response.hpp"
#include "interfaces/transaction_responses/unknown_tx_response.hpp"

namespace shared_model {
namespace proto {
using StatelessFailedTxResponse =
TrivialProto<interface::StatelessFailedTxResponse,
iroha::protocol::ToriiResponse>;
using StatelessValidTxResponse =
TrivialProto<interface::StatelessValidTxResponse,
iroha::protocol::ToriiResponse>;
using StatefulFailedTxResponse =
TrivialProto<interface::StatefulFailedTxResponse,
iroha::protocol::ToriiResponse>;
using StatefulValidTxResponse =
TrivialProto<interface::StatefulValidTxResponse,
iroha::protocol::ToriiResponse>;
using CommittedTxResponse = TrivialProto<interface::CommittedTxResponse,
iroha::protocol::ToriiResponse>;
using UnknownTxResponse = TrivialProto<interface::UnknownTxResponse,
iroha::protocol::ToriiResponse>;
} // namespace proto
} // namespace shared_model
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/

#ifndef IROHA_PROTO_TX_RESPONSE_HPP
#define IROHA_PROTO_TX_RESPONSE_HPP

#include "backend/protobuf/transaction_responses/proto_concrete_tx_response.hpp"
#include "utils/lazy_initializer.hpp"
#include "utils/reference_holder.hpp"
#include "utils/variant_deserializer.hpp"

template <typename... T, typename Archive>
auto load(Archive &&ar) {
unsigned which = ar.GetDescriptor()
->FindFieldByName("tx_status")
->enum_type()
->FindValueByNumber(ar.tx_status())
->index();
constexpr unsigned last = boost::mpl::size<T...>::type::value - 1;

return shared_model::detail::variant_impl<T...>::
template load<shared_model::interface::TransactionResponse::
ResponseVariantType>(std::forward<Archive>(ar),
which > last ? last : which);
}

namespace shared_model {
namespace proto {
/**
* TransactionResponse is a status of transaction in system
*/
class TransactionResponse final : public interface::TransactionResponse {
private:
/// PolymorphicWrapper shortcut type
template <typename... Value>
using wrap = boost::variant<detail::PolymorphicWrapper<Value>...>;

public:
/// Type of variant, that handle all concrete tx responses in the system
using ProtoResponseVariantType = wrap<StatelessFailedTxResponse,
StatelessValidTxResponse,
StatefulFailedTxResponse,
StatefulValidTxResponse,
CommittedTxResponse,
UnknownTxResponse>;

/// Type with list of types in ResponseVariantType
using ProtoResponseListType = ProtoResponseVariantType::types;

template <typename TxResponse>
explicit TransactionResponse(TxResponse &&ref)
: response_(std::forward<TxResponse>(ref)),
variant_(detail::makeLazyInitializer(
[this] { return load<ProtoResponseListType>(*response_); })),
hash_([this] { return crypto::Hash(this->response_->tx_hash()); }) {
}

TransactionResponse(TransactionResponse &&r)
: TransactionResponse(std::move(r.response_)) {}
TransactionResponse(const TransactionResponse &r)
: TransactionResponse(r.response_) {}

/**
* @return hash of corresponding transaction
*/
const interface::Transaction::HashType &transactionHash() const override {
return *hash_;
};

/**
* @return attached concrete tx response
*/
const ResponseVariantType &get() const override {
return *variant_;
}

ModelType *copy() const override {
return new TransactionResponse(
iroha::protocol::ToriiResponse(*response_));
}

private:
// ------------------------------| fields |-------------------------------

// proto
detail::ReferenceHolder<iroha::protocol::ToriiResponse> response_;

template <typename T>
using Lazy = detail::LazyInitializer<T>;

/// lazy variant shortcut
using LazyVariantType = Lazy<ResponseVariantType>;

// lazy
const LazyVariantType variant_;

// stub hash
const Lazy<crypto::Hash> hash_;
};
} // namespace proto
} // namespace shared_model
#endif // IROHA_PROTO_TX_RESPONSE_HPP
16 changes: 8 additions & 8 deletions shared_model/interfaces/transaction_responses/tx_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ namespace shared_model {
iroha::model::TransactionResponse> {
private:
/// PolymorphicWrapper shortcut type
template <typename Value>
using w = detail::PolymorphicWrapper<Value>;
template <typename... Value>
using wrap = boost::variant<detail::PolymorphicWrapper<Value>...>;

public:
/// Type of variant, that handle all concrete tx responses in the system
using ResponseVariantType = boost::variant<w<CommittedTxResponse>,
w<StatefulFailedTxResponse>,
w<StatefulValidTxResponse>,
w<StatelessFailedTxResponse>,
w<StatelessValidTxResponse>,
w<UnknownTxResponse>>;
using ResponseVariantType = wrap<StatelessFailedTxResponse,
StatelessValidTxResponse,
StatefulFailedTxResponse,
StatefulValidTxResponse,
CommittedTxResponse,
UnknownTxResponse>;

/// Type with list of types in ResponseVariantType
using ResponseListType = ResponseVariantType::types;
Expand Down
1 change: 1 addition & 0 deletions shared_model/utils/reference_holder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define IROHA_REFERENCE_HOLDER_HPP

#include <boost/variant.hpp>
#include "utils/lazy_initializer.hpp"

namespace shared_model {
namespace detail {
Expand Down
Loading

0 comments on commit ba649ae

Please sign in to comment.