forked from hyperledger-iroha/iroha-dco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge hyperledger-iroha#712 - 'feature/shared_model-proto-txresp'
- Loading branch information
Showing
11 changed files
with
367 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
shared_model/backend/protobuf/common_objects/trivial_proto.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
47 changes: 47 additions & 0 deletions
47
shared_model/backend/protobuf/transaction_responses/proto_concrete_tx_response.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
116 changes: 116 additions & 0 deletions
116
shared_model/backend/protobuf/transaction_responses/proto_tx_response.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.