This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement non-copyable block (#1542)
Implement non-copyable block Signed-off-by: Nikita Alekseev <n.alekseev2104@gmail.com>
- Loading branch information
Showing
15 changed files
with
339 additions
and
150 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
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
41 changes: 41 additions & 0 deletions
41
shared_model/backend/protobuf/common_objects/noncopyable_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,41 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef IROHA_NONCOPYABLE_PROTO_HPP | ||
#define IROHA_NONCOPYABLE_PROTO_HPP | ||
|
||
/** | ||
* Generic class for handling proto objects which are not intended to be copied. | ||
* @tparam Iface is interface to inherit from | ||
* @tparam Proto is protobuf container | ||
* @tparam Impl is implementation of Iface | ||
*/ | ||
template <typename Iface, typename Proto, typename Impl> | ||
class NonCopyableProto : public Iface { | ||
public: | ||
using TransportType = Proto; | ||
|
||
/* | ||
* Construct object from transport. Transport can be moved or copied. | ||
*/ | ||
template <typename Transport> | ||
NonCopyableProto(Transport &&ref) : proto_(std::forward<Transport>(ref)){} | ||
|
||
NonCopyableProto(const NonCopyableProto &o) = delete; | ||
NonCopyableProto &operator=(const NonCopyableProto &o) = delete; | ||
|
||
const Proto &getTransport() const { | ||
return proto_; | ||
} | ||
|
||
protected: | ||
typename Iface::ModelType *clone() const override final { | ||
return new Impl(proto_); | ||
} | ||
|
||
Proto proto_; | ||
}; | ||
|
||
#endif // IROHA_NONCOPYABLE_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,80 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "backend/protobuf/block.hpp" | ||
|
||
namespace shared_model { | ||
namespace proto { | ||
Block::Block(Block &&o) noexcept : NonCopyableProto(std::move(o.proto_)) {} | ||
|
||
Block &Block::operator=(Block &&o) noexcept { | ||
proto_ = std::move(o.proto_); | ||
payload_ = *proto_.mutable_payload(); | ||
|
||
transactions_.invalidate(); | ||
blob_.invalidate(); | ||
prev_hash_.invalidate(); | ||
signatures_.invalidate(); | ||
payload_blob_.invalidate(); | ||
|
||
return *this; | ||
} | ||
|
||
interface::types::TransactionsCollectionType Block::transactions() const { | ||
return *transactions_; | ||
} | ||
|
||
interface::types::HeightType Block::height() const { | ||
return payload_.height(); | ||
} | ||
|
||
const interface::types::HashType &Block::prevHash() const { | ||
return *prev_hash_; | ||
} | ||
|
||
const interface::types::BlobType &Block::blob() const { | ||
return *blob_; | ||
} | ||
|
||
interface::types::SignatureRangeType Block::signatures() const { | ||
return *signatures_; | ||
} | ||
|
||
// TODO Alexey Chernyshov - 2018-03-28 - | ||
// rework code duplication from transaction, block after fix protobuf | ||
// https://soramitsu.atlassian.net/browse/IR-1175 | ||
bool Block::addSignature(const crypto::Signed &signed_blob, | ||
const crypto::PublicKey &public_key) { | ||
// if already has such signature | ||
if (std::find_if(signatures_->begin(), | ||
signatures_->end(), | ||
[&public_key](const auto &signature) { | ||
return signature.publicKey() == public_key; | ||
}) | ||
!= signatures_->end()) { | ||
return false; | ||
} | ||
|
||
auto sig = proto_.add_signatures(); | ||
sig->set_signature(crypto::toBinaryString(signed_blob)); | ||
sig->set_pubkey(crypto::toBinaryString(public_key)); | ||
|
||
signatures_.invalidate(); | ||
return true; | ||
} | ||
|
||
interface::types::TimestampType Block::createdTime() const { | ||
return payload_.created_time(); | ||
} | ||
|
||
interface::types::TransactionsNumberType Block::txsNumber() const { | ||
return payload_.tx_number(); | ||
} | ||
|
||
const interface::types::BlobType &Block::payload() const { | ||
return *payload_blob_; | ||
} | ||
} // namespace proto | ||
} // namespace shared_model |
Oops, something went wrong.