From cee33a9c817b3359f003c6ffa671bea30e652ff4 Mon Sep 17 00:00:00 2001 From: turuslan Date: Fri, 2 Dec 2022 17:14:47 +0300 Subject: [PATCH 01/19] BufferOrView Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 74 +++++++++++++++++++++++++++++ core/storage/buffer_map_types.hpp | 9 ++++ core/storage/face/owned_or_view.hpp | 17 +++++++ 3 files changed, 100 insertions(+) create mode 100644 core/common/buffer_or_view.hpp create mode 100644 core/storage/face/owned_or_view.hpp diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp new file mode 100644 index 0000000000..bf56550b74 --- /dev/null +++ b/core/common/buffer_or_view.hpp @@ -0,0 +1,74 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef KAGOME_COMMON_BUFFER_OR_VIEW_HPP +#define KAGOME_COMMON_BUFFER_OR_VIEW_HPP + +#include + +#include "common/buffer.hpp" + +namespace kagome::common { + class BufferOrView { + public: + BufferOrView() = default; + + BufferOrView(const BufferView &view) : variant{view} {} + + BufferOrView(const std::vector &vector) = delete; + BufferOrView(std::vector &&vector) + : variant{Buffer{std::move(vector)}} {} + + BufferOrView(const BufferOrView &) = delete; + BufferOrView(BufferOrView &&) = default; + + BufferOrView &operator=(const BufferOrView &) = delete; + BufferOrView &operator=(BufferOrView &&) = default; + + bool owned() const { + return variant.index() == 1; + } + + BufferView view() const { + if (!owned()) { + return std::get(variant); + } + return BufferView{std::get(variant)}; + } + + operator BufferView() const { + return view(); + } + + size_t size() const { + return view().size(); + } + + // get mutable buffer reference, copy once if view + Buffer &mut() { + if (!owned()) { + auto view = std::get(variant); + variant.emplace(view); + } + return std::get(variant); + } + + // move buffer away, copy once if view + Buffer into() { + auto buffer = std::move(mut()); + variant.emplace(); + return buffer; + } + + private: + std::variant variant; + }; +} // namespace kagome::common + +template <> +struct fmt::formatter + : fmt::formatter {}; + +#endif // KAGOME_COMMON_BUFFER_OR_VIEW_HPP diff --git a/core/storage/buffer_map_types.hpp b/core/storage/buffer_map_types.hpp index c2d1543b73..2efbfb8a8d 100644 --- a/core/storage/buffer_map_types.hpp +++ b/core/storage/buffer_map_types.hpp @@ -12,15 +12,24 @@ */ #include "common/buffer.hpp" +#include "common/buffer_or_view.hpp" #include "storage/face/batch_writeable.hpp" #include "storage/face/generic_maps.hpp" #include "storage/face/write_batch.hpp" +namespace kagome::storage::face { + template <> + struct OwnedOrView { + using type = common::BufferOrView; + }; +} // namespace kagome::storage::face + namespace kagome::storage { using Buffer = common::SLBuffer::max()>; using BufferView = common::BufferView; using BufferConstRef = common::BufferConstRef; + using common::BufferOrView; using BufferBatch = face::WriteBatch; diff --git a/core/storage/face/owned_or_view.hpp b/core/storage/face/owned_or_view.hpp new file mode 100644 index 0000000000..cf1652eff9 --- /dev/null +++ b/core/storage/face/owned_or_view.hpp @@ -0,0 +1,17 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP +#define KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP + +namespace kagome::storage::face { + template + struct OwnedOrView; + + template + using OwnedOrViewOf = typename OwnedOrView::type; +} // namespace kagome::storage::face + +#endif // KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP From e52791a421fec2e1cefc04e1f4e96f34041b7fb3 Mon Sep 17 00:00:00 2001 From: turuslan Date: Fri, 2 Dec 2022 17:29:58 +0300 Subject: [PATCH 02/19] put Signed-off-by: turuslan --- core/blockchain/impl/common.cpp | 2 +- core/blockchain/impl/storage_util.cpp | 9 ++++---- core/blockchain/impl/storage_util.hpp | 2 +- .../host_api/impl/child_storage_extension.cpp | 14 ++++++------- core/host_api/impl/storage_extension.cpp | 4 ++-- core/injector/application_injector.cpp | 4 ++-- core/network/impl/synchronizer_impl.cpp | 3 ++- core/storage/face/writeable.hpp | 6 ++++-- core/storage/in_memory/in_memory_batch.hpp | 12 ++++------- core/storage/in_memory/in_memory_storage.cpp | 17 ++------------- core/storage/in_memory/in_memory_storage.hpp | 5 +---- core/storage/rocksdb/rocksdb.cpp | 7 +------ core/storage/rocksdb/rocksdb.hpp | 4 +--- core/storage/rocksdb/rocksdb_batch.cpp | 7 +------ core/storage/rocksdb/rocksdb_batch.hpp | 4 +--- .../trie/impl/ephemeral_trie_batch_impl.cpp | 7 +------ .../trie/impl/ephemeral_trie_batch_impl.hpp | 3 +-- .../trie/impl/persistent_trie_batch_impl.cpp | 15 +++++-------- .../trie/impl/persistent_trie_batch_impl.hpp | 3 +-- .../trie/impl/topper_trie_batch_impl.cpp | 11 +++------- .../trie/impl/topper_trie_batch_impl.hpp | 3 +-- .../trie/impl/trie_storage_backend_batch.cpp | 7 +------ .../trie/impl/trie_storage_backend_batch.hpp | 8 ++----- .../trie/impl/trie_storage_backend_impl.cpp | 7 +------ .../trie/impl/trie_storage_backend_impl.hpp | 3 +-- .../trie/polkadot_trie/polkadot_trie_impl.cpp | 13 ++++-------- .../trie/polkadot_trie/polkadot_trie_impl.hpp | 6 +----- .../trie/serialization/ordered_trie_hash.hpp | 2 +- .../serialization/trie_serializer_impl.cpp | 2 +- .../rocksdb/rocksdb_integration_test.cpp | 6 +++--- .../polkadot_trie_cursor_test.cpp | 5 +++-- .../trie/polkadot_trie/polkadot_trie_test.cpp | 20 +++++++++++------- .../trie/trie_storage/trie_batch_test.cpp | 19 ++++++++--------- .../mock/core/storage/persistent_map_mock.hpp | 9 +++----- .../core/storage/trie/trie_batches_mock.hpp | 21 ++++++++----------- test/mock/core/storage/write_batch_mock.hpp | 4 ++-- 36 files changed, 99 insertions(+), 175 deletions(-) diff --git a/core/blockchain/impl/common.cpp b/core/blockchain/impl/common.cpp index f18cff97ac..b61df562ff 100644 --- a/core/blockchain/impl/common.cpp +++ b/core/blockchain/impl/common.cpp @@ -35,7 +35,7 @@ namespace kagome::blockchain { auto codec = storage::trie::PolkadotCodec(); for (const auto &[key, val] : key_vals) { - [[maybe_unused]] auto res = trie.put(key, val); + [[maybe_unused]] auto res = trie.put(key, common::BufferView{val}); BOOST_ASSERT_MSG(res.has_value(), "Insertion into trie failed"); } auto root = trie.getRoot(); diff --git a/core/blockchain/impl/storage_util.cpp b/core/blockchain/impl/storage_util.cpp index 51d815c615..a18913ae8a 100644 --- a/core/blockchain/impl/storage_util.cpp +++ b/core/blockchain/impl/storage_util.cpp @@ -30,22 +30,21 @@ namespace kagome::blockchain { auto num_to_idx_key = prependPrefix(numberToIndexKey(block.number), Prefix::ID_TO_LOOKUP_KEY); auto block_lookup_key = numberAndHashToLookupKey(block.number, block.hash); - return map.put(num_to_idx_key, block_lookup_key); + return map.put(num_to_idx_key, std::move(block_lookup_key)); } outcome::result putWithPrefix(storage::BufferStorage &map, prefix::Prefix prefix, BlockNumber num, Hash256 block_hash, - const common::Buffer &value) { + common::BufferOrView &&value) { auto block_lookup_key = numberAndHashToLookupKey(num, block_hash); auto hash_to_idx_key = prependPrefix(block_hash, Prefix::ID_TO_LOOKUP_KEY); - OUTCOME_TRY(map.put(hash_to_idx_key, block_lookup_key)); - auto value_lookup_key = prependPrefix(block_lookup_key, prefix); + OUTCOME_TRY(map.put(hash_to_idx_key, std::move(block_lookup_key))); - return map.put(value_lookup_key, value); + return map.put(value_lookup_key, std::move(value)); } outcome::result hasWithPrefix(const storage::BufferStorage &map, diff --git a/core/blockchain/impl/storage_util.hpp b/core/blockchain/impl/storage_util.hpp index 69c79c1365..5aacae7d1c 100644 --- a/core/blockchain/impl/storage_util.hpp +++ b/core/blockchain/impl/storage_util.hpp @@ -66,7 +66,7 @@ namespace kagome::blockchain { prefix::Prefix prefix, primitives::BlockNumber num, common::Hash256 block_hash, - const common::Buffer &value); + common::BufferOrView &&value); /** * Chech if an entry from the database diff --git a/core/host_api/impl/child_storage_extension.cpp b/core/host_api/impl/child_storage_extension.cpp index a37d9081a7..f71404103b 100644 --- a/core/host_api/impl/child_storage_extension.cpp +++ b/core/host_api/impl/child_storage_extension.cpp @@ -83,19 +83,17 @@ namespace kagome::host_api { runtime::WasmSpan key, runtime::WasmSpan value) { auto &memory = memory_provider_->getCurrentMemory()->get(); - auto [child_key_buffer, key_buffer, value_buffer] = - loadBuffer(memory, child_storage_key, key, value); + auto child_key_buffer = loadBuffer(memory, child_storage_key); + auto key_buffer = loadBuffer(memory, key); + auto value_buffer = loadBuffer(memory, value); SL_TRACE_VOID_FUNC_CALL( logger_, child_key_buffer, key_buffer, value_buffer); auto result = executeOnChildStorage( - child_key_buffer, - [](auto &child_batch, auto &key, auto &value) { - return child_batch->put(key, value); - }, - key_buffer, - value_buffer); + child_key_buffer, [&](auto &child_batch) mutable { + return child_batch->put(key_buffer, std::move(value_buffer)); + }); if (not result) { logger_->error( diff --git a/core/host_api/impl/storage_extension.cpp b/core/host_api/impl/storage_extension.cpp index eb31aee089..8ecdd243f8 100644 --- a/core/host_api/impl/storage_extension.cpp +++ b/core/host_api/impl/storage_extension.cpp @@ -129,7 +129,7 @@ namespace kagome::host_api { SL_TRACE_VOID_FUNC_CALL(logger_, key, value); auto batch = storage_provider_->getCurrentBatch(); - auto put_result = batch->put(key, value); + auto put_result = batch->put(key, std::move(value)); if (not put_result) { logger_->error( "ext_set_storage failed, due to fail in trie db with reason: {}", @@ -374,7 +374,7 @@ namespace kagome::host_api { storage::trie::PolkadotTrieImpl trie; for (auto &&p : pv) { auto &&key = p.first; - auto &&value = p.second; + common::BufferView value = p.second; // already scale-encoded auto put_res = trie.put(key, value); if (not put_res) { diff --git a/core/injector/application_injector.cpp b/core/injector/application_injector.cpp index af0f185355..0cb5657d9d 100644 --- a/core/injector/application_injector.cpp +++ b/core/injector/application_injector.cpp @@ -340,7 +340,7 @@ namespace { for (const auto &[key_, val_] : raw_configs) { auto &key = key_; - auto &val = val_; + common::BufferView val = val_; SL_TRACE( log, "Key: {}, Val: {}", key.toHex(), val.toHex().substr(0, 200)); if (auto res = batch->put(key, val); not res) { @@ -367,7 +367,7 @@ namespace { common::Buffer child_key; child_key.put(storage::kChildStorageDefaultPrefix).put(root_hash); - auto res = batch->put(child_key, root_hash); + auto res = batch->put(child_key, common::BufferView{root_hash}); if (res.has_error()) { common::raise(res.error()); } diff --git a/core/network/impl/synchronizer_impl.cpp b/core/network/impl/synchronizer_impl.cpp index 721f3d36a8..bb8f68ac6d 100644 --- a/core/network/impl/synchronizer_impl.cpp +++ b/core/network/impl/synchronizer_impl.cpp @@ -991,7 +991,8 @@ namespace kagome::network { state_entry.entries[0].key.toHex(), state_entry.entries.size()); for (const auto &entry : state_entry.entries) { - std::ignore = batch->put(entry.key, entry.value); + std::ignore = + batch->put(entry.key, common::BufferView{entry.value}); } // store batch to continue at next state_entry diff --git a/core/storage/face/writeable.hpp b/core/storage/face/writeable.hpp index 2889526161..fc9dbf4233 100644 --- a/core/storage/face/writeable.hpp +++ b/core/storage/face/writeable.hpp @@ -8,6 +8,8 @@ #include +#include "storage/face/owned_or_view.hpp" + namespace kagome::storage::face { /** @@ -25,8 +27,8 @@ namespace kagome::storage::face { * @param value value * @return result containing void if put successful, error otherwise */ - virtual outcome::result put(const K &key, const V &value) = 0; - virtual outcome::result put(const K &key, V &&value) = 0; + virtual outcome::result put(const K &key, + OwnedOrViewOf &&value) = 0; /** * @brief Remove value by key diff --git a/core/storage/in_memory/in_memory_batch.hpp b/core/storage/in_memory/in_memory_batch.hpp index 77698c835e..e31682cada 100644 --- a/core/storage/in_memory/in_memory_batch.hpp +++ b/core/storage/in_memory/in_memory_batch.hpp @@ -18,13 +18,8 @@ namespace kagome::storage { explicit InMemoryBatch(InMemoryStorage &db) : db{db} {} outcome::result put(const BufferView &key, - const Buffer &value) override { - entries[key.toHex()] = value; - return outcome::success(); - } - - outcome::result put(const BufferView &key, Buffer &&value) override { - entries[key.toHex()] = std::move(value); + BufferOrView &&value) override { + entries[key.toHex()] = value.into(); return outcome::success(); } @@ -35,7 +30,8 @@ namespace kagome::storage { outcome::result commit() override { for (auto &entry : entries) { - OUTCOME_TRY(db.put(Buffer::fromHex(entry.first).value(), entry.second)); + OUTCOME_TRY(db.put(Buffer::fromHex(entry.first).value(), + BufferView{entry.second})); } return outcome::success(); } diff --git a/core/storage/in_memory/in_memory_storage.cpp b/core/storage/in_memory/in_memory_storage.cpp index e90788ba03..94bd1b66c8 100644 --- a/core/storage/in_memory/in_memory_storage.cpp +++ b/core/storage/in_memory/in_memory_storage.cpp @@ -31,7 +31,7 @@ namespace kagome::storage { } outcome::result InMemoryStorage::put(const BufferView &key, - const Buffer &value) { + BufferOrView &&value) { auto it = storage.find(key.toHex()); if (it != storage.end()) { size_t old_value_size = it->second.size(); @@ -39,20 +39,7 @@ namespace kagome::storage { size_ -= old_value_size; } size_ += value.size(); - storage[key.toHex()] = value; - return outcome::success(); - } - - outcome::result InMemoryStorage::put(const BufferView &key, - Buffer &&value) { - auto it = storage.find(key.toHex()); - if (it != storage.end()) { - size_t old_value_size = it->second.size(); - BOOST_ASSERT(size_ >= old_value_size); - size_ -= old_value_size; - } - size_ += value.size(); - storage[key.toHex()] = std::move(value); + storage[key.toHex()] = value.into(); return outcome::success(); } diff --git a/core/storage/in_memory/in_memory_storage.hpp b/core/storage/in_memory/in_memory_storage.hpp index 2885c2fe57..3137c89003 100644 --- a/core/storage/in_memory/in_memory_storage.hpp +++ b/core/storage/in_memory/in_memory_storage.hpp @@ -30,10 +30,7 @@ namespace kagome::storage { const common::BufferView &key) const override; outcome::result put(const common::BufferView &key, - const common::Buffer &value) override; - - outcome::result put(const common::BufferView &key, - common::Buffer &&value) override; + BufferOrView &&value) override; outcome::result contains( const common::BufferView &key) const override; diff --git a/core/storage/rocksdb/rocksdb.cpp b/core/storage/rocksdb/rocksdb.cpp index e29f5a79b5..a84a6ece2f 100644 --- a/core/storage/rocksdb/rocksdb.cpp +++ b/core/storage/rocksdb/rocksdb.cpp @@ -153,7 +153,7 @@ namespace kagome::storage { } outcome::result RocksDB::put(const BufferView &key, - const Buffer &value) { + BufferOrView &&value) { auto status = db_->Put(wo_, make_slice(key), make_slice(value)); if (status.ok()) { return outcome::success(); @@ -162,11 +162,6 @@ namespace kagome::storage { return status_as_error(status); } - outcome::result RocksDB::put(const BufferView &key, Buffer &&value) { - Buffer copy(std::move(value)); - return put(key, copy); - } - outcome::result RocksDB::remove(const BufferView &key) { auto status = db_->Delete(wo_, make_slice(key)); if (status.ok()) { diff --git a/core/storage/rocksdb/rocksdb.hpp b/core/storage/rocksdb/rocksdb.hpp index b919298b3b..40d6a9012f 100644 --- a/core/storage/rocksdb/rocksdb.hpp +++ b/core/storage/rocksdb/rocksdb.hpp @@ -49,9 +49,7 @@ namespace kagome::storage { const Key &key) const override; outcome::result put(const BufferView &key, - const Buffer &value) override; - - outcome::result put(const BufferView &key, Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const BufferView &key) override; diff --git a/core/storage/rocksdb/rocksdb_batch.cpp b/core/storage/rocksdb/rocksdb_batch.cpp index e9d4c3853a..190d3fa6b2 100644 --- a/core/storage/rocksdb/rocksdb_batch.cpp +++ b/core/storage/rocksdb/rocksdb_batch.cpp @@ -13,16 +13,11 @@ namespace kagome::storage { RocksDB::Batch::Batch(RocksDB &db) : db_(db) {} outcome::result RocksDB::Batch::put(const BufferView &key, - const Buffer &value) { + BufferOrView &&value) { batch_.Put(make_slice(key), make_slice(value)); return outcome::success(); } - outcome::result RocksDB::Batch::put(const BufferView &key, - Buffer &&value) { - return put(key, static_cast(value)); - } - outcome::result RocksDB::Batch::remove(const BufferView &key) { batch_.Delete(make_slice(key)); return outcome::success(); diff --git a/core/storage/rocksdb/rocksdb_batch.hpp b/core/storage/rocksdb/rocksdb_batch.hpp index 1de24d0d6c..1d5ccdf138 100644 --- a/core/storage/rocksdb/rocksdb_batch.hpp +++ b/core/storage/rocksdb/rocksdb_batch.hpp @@ -22,9 +22,7 @@ namespace kagome::storage { void clear() override; outcome::result put(const BufferView &key, - const Buffer &value) override; - - outcome::result put(const BufferView &key, Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const BufferView &key) override; diff --git a/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp b/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp index 9b75dc93dd..254268f37f 100644 --- a/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp +++ b/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp @@ -48,12 +48,7 @@ namespace kagome::storage::trie { } outcome::result EphemeralTrieBatchImpl::put(const BufferView &key, - const Buffer &value) { - return trie_->put(key, value); - } - - outcome::result EphemeralTrieBatchImpl::put(const BufferView &key, - Buffer &&value) { + BufferOrView &&value) { return trie_->put(key, std::move(value)); } diff --git a/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp b/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp index fd7761b626..aa65cae512 100644 --- a/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp +++ b/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp @@ -28,8 +28,7 @@ namespace kagome::storage::trie { const BufferView &prefix, std::optional limit = std::nullopt) override; outcome::result put(const BufferView &key, - const Buffer &value) override; - outcome::result put(const BufferView &key, Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const BufferView &key) override; outcome::result hash() override; diff --git a/core/storage/trie/impl/persistent_trie_batch_impl.cpp b/core/storage/trie/impl/persistent_trie_batch_impl.cpp index ce00bd203b..443092947c 100644 --- a/core/storage/trie/impl/persistent_trie_batch_impl.cpp +++ b/core/storage/trie/impl/persistent_trie_batch_impl.cpp @@ -99,24 +99,19 @@ namespace kagome::storage::trie { } outcome::result PersistentTrieBatchImpl::put(const BufferView &key, - const Buffer &value) { + BufferOrView &&value) { OUTCOME_TRY(contains, trie_->contains(key)); bool is_new_entry = not contains; - auto res = trie_->put(key, value); + auto value_copy = value.mut(); + auto res = trie_->put(key, std::move(value)); if (res and changes_.has_value()) { - SL_TRACE_VOID_FUNC_CALL(logger_, key, value); + SL_TRACE_VOID_FUNC_CALL(logger_, key, value_copy); - changes_.value()->onPut(key, value, is_new_entry); + changes_.value()->onPut(key, value_copy, is_new_entry); } return res; } - outcome::result PersistentTrieBatchImpl::put(const BufferView &key, - Buffer &&value) { - return put(key, value); // cannot take possession of value, check the - // const-ref version definition - } - outcome::result PersistentTrieBatchImpl::remove(const BufferView &key) { OUTCOME_TRY(trie_->remove(key)); if (changes_.has_value()) { diff --git a/core/storage/trie/impl/persistent_trie_batch_impl.hpp b/core/storage/trie/impl/persistent_trie_batch_impl.hpp index e09170764e..115d628597 100644 --- a/core/storage/trie/impl/persistent_trie_batch_impl.hpp +++ b/core/storage/trie/impl/persistent_trie_batch_impl.hpp @@ -43,8 +43,7 @@ namespace kagome::storage::trie { const BufferView &prefix, std::optional limit = std::nullopt) override; outcome::result put(const BufferView &key, - const Buffer &value) override; - outcome::result put(const BufferView &key, Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const BufferView &key) override; private: diff --git a/core/storage/trie/impl/topper_trie_batch_impl.cpp b/core/storage/trie/impl/topper_trie_batch_impl.cpp index 72379c056a..9d62e65733 100644 --- a/core/storage/trie/impl/topper_trie_batch_impl.cpp +++ b/core/storage/trie/impl/topper_trie_batch_impl.cpp @@ -91,13 +91,8 @@ namespace kagome::storage::trie { } outcome::result TopperTrieBatchImpl::put(const BufferView &key, - const Buffer &value) { - return put(key, Buffer(value)); - } - - outcome::result TopperTrieBatchImpl::put(const BufferView &key, - Buffer &&value) { - cache_.insert_or_assign(Buffer{key}, std::move(value)); + BufferOrView &&value) { + cache_.insert_or_assign(Buffer{key}, value.into()); return outcome::success(); } @@ -128,7 +123,7 @@ namespace kagome::storage::trie { } for (auto it = cache_.begin(); it != cache_.end(); it++) { if (it->second.has_value()) { - OUTCOME_TRY(p->put(it->first, it->second.value())); + OUTCOME_TRY(p->put(it->first, BufferView{it->second.value()})); } else { OUTCOME_TRY(p->remove(it->first)); } diff --git a/core/storage/trie/impl/topper_trie_batch_impl.hpp b/core/storage/trie/impl/topper_trie_batch_impl.hpp index c46b806d60..4e9e1d47c3 100644 --- a/core/storage/trie/impl/topper_trie_batch_impl.hpp +++ b/core/storage/trie/impl/topper_trie_batch_impl.hpp @@ -37,8 +37,7 @@ namespace kagome::storage::trie { bool empty() const override; outcome::result put(const BufferView &key, - const Buffer &value) override; - outcome::result put(const BufferView &key, Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const BufferView &key) override; outcome::result> clearPrefix( const BufferView &prefix, std::optional limit) override; diff --git a/core/storage/trie/impl/trie_storage_backend_batch.cpp b/core/storage/trie/impl/trie_storage_backend_batch.cpp index cf2225434e..641c7d2666 100644 --- a/core/storage/trie/impl/trie_storage_backend_batch.cpp +++ b/core/storage/trie/impl/trie_storage_backend_batch.cpp @@ -25,12 +25,7 @@ namespace kagome::storage::trie { } outcome::result TrieStorageBackendBatch::put( - const common::BufferView &key, const common::Buffer &value) { - return storage_batch_->put(prefixKey(key), value); - } - - outcome::result TrieStorageBackendBatch::put( - const common::BufferView &key, common::Buffer &&value) { + const common::BufferView &key, BufferOrView &&value) { return storage_batch_->put(prefixKey(key), std::move(value)); } diff --git a/core/storage/trie/impl/trie_storage_backend_batch.hpp b/core/storage/trie/impl/trie_storage_backend_batch.hpp index d82bc3706a..12f3b95b81 100644 --- a/core/storage/trie/impl/trie_storage_backend_batch.hpp +++ b/core/storage/trie/impl/trie_storage_backend_batch.hpp @@ -6,8 +6,7 @@ #ifndef KAGOME_STORAGE_TRIE_IMPL_TRIE_STORAGE_BACKEND_BATCH #define KAGOME_STORAGE_TRIE_IMPL_TRIE_STORAGE_BACKEND_BATCH -#include "common/buffer.hpp" -#include "storage/face/write_batch.hpp" +#include "storage/buffer_map_types.hpp" namespace kagome::storage::trie { @@ -27,10 +26,7 @@ namespace kagome::storage::trie { outcome::result commit() override; outcome::result put(const common::BufferView &key, - const common::Buffer &value) override; - - outcome::result put(const common::BufferView &key, - common::Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const common::BufferView &key) override; void clear() override; diff --git a/core/storage/trie/impl/trie_storage_backend_impl.cpp b/core/storage/trie/impl/trie_storage_backend_impl.cpp index 7e67ba2c11..609edaf7c6 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.cpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.cpp @@ -49,12 +49,7 @@ namespace kagome::storage::trie { } outcome::result TrieStorageBackendImpl::put(const BufferView &key, - const Buffer &value) { - return storage_->put(prefixKey(key), value); - } - - outcome::result TrieStorageBackendImpl::put(const BufferView &key, - Buffer &&value) { + BufferOrView &&value) { return storage_->put(prefixKey(key), std::move(value)); } diff --git a/core/storage/trie/impl/trie_storage_backend_impl.hpp b/core/storage/trie/impl/trie_storage_backend_impl.hpp index 2ce8b152bd..e36471fd24 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.hpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.hpp @@ -29,8 +29,7 @@ namespace kagome::storage::trie { bool empty() const override; outcome::result put(const BufferView &key, - const Buffer &value) override; - outcome::result put(const BufferView &key, Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const common::BufferView &key) override; size_t size() const override; diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp b/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp index fc77dc549e..060ffb4abf 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp @@ -293,12 +293,6 @@ namespace kagome::storage::trie { PolkadotTrieImpl::~PolkadotTrieImpl() {} - outcome::result PolkadotTrieImpl::put(const BufferView &key, - const Buffer &value) { - auto value_copy = value; - return put(key, std::move(value_copy)); - } - PolkadotTrie::ConstNodePtr PolkadotTrieImpl::getRoot() const { return nodes_->getRoot(); } @@ -308,7 +302,7 @@ namespace kagome::storage::trie { } outcome::result PolkadotTrieImpl::put(const BufferView &key, - Buffer &&value) { + BufferOrView &&value) { auto k_enc = KeyNibbles::fromByteBuffer(key); NodePtr root = nodes_->getRoot(); @@ -316,8 +310,9 @@ namespace kagome::storage::trie { // insert fetches a sequence of nodes (a path) from the storage and // these nodes are processed in memory, so any changes applied to them // will be written back to the storage only on storeNode call - OUTCOME_TRY(n, - insert(root, k_enc, std::make_shared(k_enc, value))); + OUTCOME_TRY( + n, + insert(root, k_enc, std::make_shared(k_enc, value.into()))); nodes_->setRoot(n); return outcome::success(); diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp b/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp index 902dab8a37..5fabd53f0f 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp @@ -58,12 +58,8 @@ namespace kagome::storage::trie { std::optional limit, const OnDetachCallback &callback) override; - // value will be copied outcome::result put(const common::BufferView &key, - const common::Buffer &value) override; - - outcome::result put(const common::BufferView &key, - common::Buffer &&value) override; + BufferOrView &&value) override; outcome::result remove(const common::BufferView &key) override; diff --git a/core/storage/trie/serialization/ordered_trie_hash.hpp b/core/storage/trie/serialization/ordered_trie_hash.hpp index 83b4f56741..62d5bd14bc 100644 --- a/core/storage/trie/serialization/ordered_trie_hash.hpp +++ b/core/storage/trie/serialization/ordered_trie_hash.hpp @@ -38,7 +38,7 @@ namespace kagome::storage::trie { scale::CompactInteger key = 0; while (it != end) { OUTCOME_TRY(enc, scale::encode(key++)); - OUTCOME_TRY(trie.put(common::Buffer{enc}, *it)); + OUTCOME_TRY(trie.put(enc, BufferView{*it})); it++; } OUTCOME_TRY(enc, codec.encodeNode(*trie.getRoot())); diff --git a/core/storage/trie/serialization/trie_serializer_impl.cpp b/core/storage/trie/serialization/trie_serializer_impl.cpp index 7f6f1f6268..68769d50e0 100644 --- a/core/storage/trie/serialization/trie_serializer_impl.cpp +++ b/core/storage/trie/serialization/trie_serializer_impl.cpp @@ -61,7 +61,7 @@ namespace kagome::storage::trie { return batch->put(hash, std::move(encoded)); })); auto key = codec_->hash256(enc); - OUTCOME_TRY(batch->put(key, enc)); + OUTCOME_TRY(batch->put(key, std::move(enc))); OUTCOME_TRY(batch->commit()); return key; diff --git a/test/core/storage/rocksdb/rocksdb_integration_test.cpp b/test/core/storage/rocksdb/rocksdb_integration_test.cpp index 0953ab61d9..407b56ee87 100644 --- a/test/core/storage/rocksdb/rocksdb_integration_test.cpp +++ b/test/core/storage/rocksdb/rocksdb_integration_test.cpp @@ -37,7 +37,7 @@ struct RocksDb_Integration_Test : public test::BaseRocksDB_Test { * @then {value} is correct */ TEST_F(RocksDb_Integration_Test, Put_Get) { - ASSERT_OUTCOME_SUCCESS_TRY(db_->put(key_, value_)); + ASSERT_OUTCOME_SUCCESS_TRY(db_->put(key_, BufferView{value_})); ASSERT_OUTCOME_SUCCESS(contains, db_->contains(key_)); EXPECT_TRUE(contains); EXPECT_OUTCOME_TRUE_2(val, db_->load(key_)); @@ -72,7 +72,7 @@ TEST_F(RocksDb_Integration_Test, WriteBatch) { ASSERT_TRUE(batch); for (const auto &item : keys) { - ASSERT_OUTCOME_SUCCESS_TRY(batch->put(item, item)); + ASSERT_OUTCOME_SUCCESS_TRY(batch->put(item, BufferView{item})); ASSERT_OUTCOME_SUCCESS(contains, db_->contains(item)); EXPECT_FALSE(contains); } @@ -104,7 +104,7 @@ TEST_F(RocksDb_Integration_Test, Iterator) { } for (const auto &item : keys) { - ASSERT_OUTCOME_SUCCESS_TRY(db_->put(item, item)); + ASSERT_OUTCOME_SUCCESS_TRY(db_->put(item, BufferView{item})); } std::array counter{}; diff --git a/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp b/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp index 5572f6ab73..4e829b6cfe 100644 --- a/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp +++ b/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp @@ -18,6 +18,7 @@ #include "testutil/storage/polkadot_trie_printer.hpp" using kagome::common::Buffer; +using kagome::common::BufferView; using kagome::storage::trie::PolkadotTrie; using kagome::storage::trie::PolkadotTrieCursorImpl; using kagome::storage::trie::PolkadotTrieImpl; @@ -55,7 +56,7 @@ std::tuple, std::set> generateRandomTrie( for (size_t i = 0; i < keys_num; i++) { kagome::common::Buffer key(key_length_gen(), 0); std::generate(key.begin(), key.end(), std::ref(key_gen)); - EXPECT_OUTCOME_TRUE_1(trie->put(key, key)) + EXPECT_OUTCOME_TRUE_1(trie->put(key, BufferView{key})) keys.emplace(std::move(key)); } std::get<0>(res) = std::move(trie); @@ -66,7 +67,7 @@ std::shared_ptr makeTrie( const std::vector> &vals) { auto trie = std::make_shared(); for (auto &p : vals) { - EXPECT_OUTCOME_TRUE_1(trie->put(p.first, p.second)) + EXPECT_OUTCOME_TRUE_1(trie->put(p.first, BufferView{p.second})); } return trie; } diff --git a/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp b/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp index 25b2e17302..7677fd791b 100644 --- a/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp +++ b/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp @@ -14,6 +14,7 @@ #include "testutil/storage/polkadot_trie_printer.hpp" using kagome::common::Buffer; +using kagome::common::BufferView; using kagome::common::Hash256; using kagome::storage::trie::BranchNode; using kagome::storage::trie::KeyNibbles; @@ -59,7 +60,7 @@ const std::vector> TrieTest::data = { void FillSmallTree(PolkadotTrie &trie) { for (auto &entry : TrieTest::data) { - ASSERT_OUTCOME_SUCCESS_TRY(trie.put(entry.first, entry.second)); + ASSERT_OUTCOME_SUCCESS_TRY(trie.put(entry.first, BufferView{entry.second})); } } @@ -98,7 +99,7 @@ TEST_P(TrieTest, RunCommand) { } case Command::PUT: { ASSERT_OUTCOME_SUCCESS_TRY( - trie->put(command.key, command.value.value())); + trie->put(command.key, BufferView{command.value.value()})); break; } case Command::REMOVE: { @@ -308,7 +309,8 @@ TEST_F(TrieTest, Remove) { TEST_F(TrieTest, Replace) { FillSmallTree(*trie); - ASSERT_OUTCOME_SUCCESS_TRY(trie->put(data[1].first, data[3].second)); + ASSERT_OUTCOME_SUCCESS_TRY( + trie->put(data[1].first, BufferView{data[3].second})); ASSERT_OUTCOME_SUCCESS(res, trie->get(data[1].first)); ASSERT_EQ(res.get(), data[3].second); } @@ -324,7 +326,8 @@ TEST_F(TrieTest, ClearPrefix) { {"bat"_buf, "789"_buf}, {"batch"_buf, "0-="_buf}}; for (auto &entry : data) { - ASSERT_OUTCOME_SUCCESS_TRY(trie->put(entry.first, entry.second)); + ASSERT_OUTCOME_SUCCESS_TRY( + trie->put(entry.first, BufferView{entry.second})); } ASSERT_OUTCOME_SUCCESS_TRY( trie->clearPrefix("bar"_buf, std::nullopt, [](const auto &, auto &&) { @@ -535,7 +538,8 @@ TEST_F(TrieTest, GetPath) { {"0a0b0c"_hex2buf, "deadbeef"_hex2buf}}; for (const auto &entry : TrieTest::data) { - ASSERT_OUTCOME_SUCCESS_TRY(trie->put(entry.first, entry.second)); + ASSERT_OUTCOME_SUCCESS_TRY( + trie->put(entry.first, BufferView{entry.second})); } std::vector> path; @@ -568,7 +572,8 @@ TEST_F(TrieTest, GetPathToInvalid) { {"0a0b0c"_hex2buf, "deadbeef"_hex2buf}}; for (const auto &entry : TrieTest::data) { - ASSERT_OUTCOME_SUCCESS_TRY(trie->put(entry.first, entry.second)); + ASSERT_OUTCOME_SUCCESS_TRY( + trie->put(entry.first, BufferView{entry.second})); } EXPECT_OUTCOME_SOME_ERROR( _, @@ -592,7 +597,8 @@ TEST_F(TrieTest, GetNodeReturnsNullptrWhenNotFound) { {"0a0b0c"_hex2buf, "deadbeef"_hex2buf}}; for (auto &entry : TrieTest::data) { - ASSERT_OUTCOME_SUCCESS_TRY(trie->put(entry.first, entry.second)); + ASSERT_OUTCOME_SUCCESS_TRY( + trie->put(entry.first, BufferView{entry.second})); } ASSERT_OUTCOME_SUCCESS( res, diff --git a/test/core/storage/trie/trie_storage/trie_batch_test.cpp b/test/core/storage/trie/trie_storage/trie_batch_test.cpp index d0a2f31f2d..b93ff154c8 100644 --- a/test/core/storage/trie/trie_storage/trie_batch_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_batch_test.cpp @@ -21,6 +21,7 @@ using namespace kagome::storage::trie; using kagome::api::Session; using kagome::common::Buffer; +using kagome::common::BufferOrView; using kagome::common::BufferView; using kagome::common::Hash256; using kagome::primitives::BlockHash; @@ -84,25 +85,22 @@ const std::vector> TrieBatchTest::data = { void FillSmallTrieWithBatch(TrieBatch &batch) { for (auto &entry : TrieBatchTest::data) { - ASSERT_OUTCOME_SUCCESS_TRY(batch.put(entry.first, entry.second)); + ASSERT_OUTCOME_SUCCESS_TRY( + batch.put(entry.first, BufferView{entry.second})); } } class MockDb : public kagome::storage::InMemoryStorage { public: - MOCK_METHOD(outcome::result, - put, - (const BufferView &, const Buffer &), - (override)); - - outcome::result put(const BufferView &k, Buffer &&v) override { - return put(k, v); + MOCK_METHOD(outcome::result, put, (const BufferView &, const Buffer &)); + outcome::result put(const BufferView &k, BufferOrView &&v) override { + return put(k, v.mut()); } // to retain the ability to call the actual implementation of put from the // superclass outcome::result true_put(const BufferView &key, const Buffer &value) { - return InMemoryStorage::put(key, value); + return InMemoryStorage::put(key, BufferView{value}); } }; @@ -169,7 +167,8 @@ TEST_F(TrieBatchTest, Remove) { */ TEST_F(TrieBatchTest, Replace) { auto batch = trie->getPersistentBatchAt(empty_hash).value(); - ASSERT_OUTCOME_SUCCESS_TRY(batch->put(data[1].first, data[3].second)); + ASSERT_OUTCOME_SUCCESS_TRY( + batch->put(data[1].first, BufferView{data[3].second})); ASSERT_OUTCOME_SUCCESS(root_hash, batch->commit()); auto read_batch = trie->getEphemeralBatchAt(root_hash).value(); ASSERT_OUTCOME_SUCCESS(res, read_batch->get(data[1].first)); diff --git a/test/mock/core/storage/persistent_map_mock.hpp b/test/mock/core/storage/persistent_map_mock.hpp index 969c0809e8..0242f04ed1 100644 --- a/test/mock/core/storage/persistent_map_mock.hpp +++ b/test/mock/core/storage/persistent_map_mock.hpp @@ -29,12 +29,9 @@ namespace kagome::storage::face { MOCK_CONST_METHOD0_T(empty, bool()); - MOCK_METHOD(outcome::result, - put, - (const KView &, const V &), - (override)); - outcome::result put(const KView &k, V &&v) override { - return put(k, v); + MOCK_METHOD(outcome::result, put, (const KView &, const V &)); + outcome::result put(const KView &k, OwnedOrViewOf &&v) override { + return put(k, v.mut()); } MOCK_METHOD1_T(remove, outcome::result(const KView &)); diff --git a/test/mock/core/storage/trie/trie_batches_mock.hpp b/test/mock/core/storage/trie/trie_batches_mock.hpp index 5cc8bb3323..7ca72c70ab 100644 --- a/test/mock/core/storage/trie/trie_batches_mock.hpp +++ b/test/mock/core/storage/trie/trie_batches_mock.hpp @@ -36,11 +36,10 @@ namespace kagome::storage::trie { MOCK_METHOD(outcome::result, put, - (const common::BufferView &, const common::Buffer &), - (override)); + (const common::BufferView &, const Buffer &)); outcome::result put(const common::BufferView &k, - common::Buffer &&v) override { - return put(k, v); + BufferOrView &&v) override { + return put(k, v.mut()); } MOCK_METHOD(outcome::result, @@ -87,11 +86,10 @@ namespace kagome::storage::trie { MOCK_METHOD(outcome::result, put, - (const common::BufferView &, const common::Buffer &), - (override)); + (const common::BufferView &, const Buffer &)); outcome::result put(const common::BufferView &k, - common::Buffer &&v) override { - return put(k, v); + BufferOrView &&v) override { + return put(k, v.mut()); } MOCK_METHOD(outcome::result, @@ -127,11 +125,10 @@ namespace kagome::storage::trie { MOCK_METHOD(outcome::result, put, - (const common::BufferView &, const common::Buffer &), - (override)); + (const common::BufferView &, const Buffer &)); outcome::result put(const common::BufferView &k, - common::Buffer &&v) override { - return put(k, v); + BufferOrView &&v) override { + return put(k, v.mut()); } MOCK_METHOD(outcome::result, diff --git a/test/mock/core/storage/write_batch_mock.hpp b/test/mock/core/storage/write_batch_mock.hpp index b3af58487c..7161c0d47d 100644 --- a/test/mock/core/storage/write_batch_mock.hpp +++ b/test/mock/core/storage/write_batch_mock.hpp @@ -20,8 +20,8 @@ namespace kagome::storage::face { MOCK_METHOD(void, clear, (), (override)); MOCK_METHOD2_T(put, outcome::result(const K &key, const V &value)); - outcome::result put(const K &key, V &&value) override { - return put(key, value); + outcome::result put(const K &key, OwnedOrViewOf &&value) override { + return put(key, value.mut()); } MOCK_METHOD1_T(remove, outcome::result(const K &key)); From 7a4395927eafacea4e8d5b2f4ebff6ccf3003bb7 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 14:29:01 +0300 Subject: [PATCH 03/19] equality Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index bf56550b74..68112791cb 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -12,6 +12,8 @@ namespace kagome::common { class BufferOrView { + using Span = gsl::span; + public: BufferOrView() = default; @@ -64,6 +66,19 @@ namespace kagome::common { private: std::variant variant; + + friend bool operator==(const BufferOrView &l, const Span &r) { + return l.view() == r; + } + friend bool operator!=(const BufferOrView &l, const Span &r) { + return l.view() != r; + } + friend bool operator==(const Span &l, const BufferOrView &r) { + return l == r.view(); + } + friend bool operator!=(const Span &l, const BufferOrView &r) { + return l != r.view(); + } }; } // namespace kagome::common From 47365c70f71bee1b32ffc67f5ce297662761543b Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 14:29:37 +0300 Subject: [PATCH 04/19] load Signed-off-by: turuslan --- core/blockchain/impl/common.cpp | 2 +- core/blockchain/impl/common.hpp | 2 +- core/blockchain/impl/storage_util.cpp | 2 +- core/blockchain/impl/storage_util.hpp | 2 +- .../babe/impl/consistency_keeper_impl.cpp | 2 +- core/offchain/impl/offchain_local_storage.cpp | 3 ++- .../impl/offchain_persistent_storage.cpp | 3 ++- core/storage/face/readable.hpp | 8 +++---- core/storage/in_memory/in_memory_storage.cpp | 8 +++---- core/storage/in_memory/in_memory_storage.hpp | 4 ++-- core/storage/rocksdb/rocksdb.cpp | 4 ++-- core/storage/rocksdb/rocksdb.hpp | 5 ++--- .../trie/impl/trie_storage_backend_impl.cpp | 4 ++-- .../trie/impl/trie_storage_backend_impl.hpp | 4 ++-- .../serialization/trie_serializer_impl.cpp | 2 +- test/core/blockchain/block_storage_test.cpp | 13 ++++++----- .../babe/babe_config_repository_test.cpp | 2 +- .../trie_storage_backend_test.cpp | 3 ++- .../mock/core/storage/persistent_map_mock.hpp | 22 ++++++++++++++++--- 19 files changed, 57 insertions(+), 38 deletions(-) diff --git a/core/blockchain/impl/common.cpp b/core/blockchain/impl/common.cpp index b61df562ff..7be4221f7c 100644 --- a/core/blockchain/impl/common.cpp +++ b/core/blockchain/impl/common.cpp @@ -12,7 +12,7 @@ namespace kagome::blockchain { - outcome::result> idToLookupKey( + outcome::result> idToLookupKey( const ReadableBufferStorage &map, const primitives::BlockId &id) { auto key = visit_in_place( id, diff --git a/core/blockchain/impl/common.hpp b/core/blockchain/impl/common.hpp index d1d7bc41b7..cd3b2f5cf6 100644 --- a/core/blockchain/impl/common.hpp +++ b/core/blockchain/impl/common.hpp @@ -21,7 +21,7 @@ namespace kagome::blockchain { * Convert a block ID into a key, which is a first part of a key, by which the * columns are stored in the database */ - outcome::result> idToLookupKey( + outcome::result> idToLookupKey( const ReadableBufferStorage &map, const primitives::BlockId &id); /** diff --git a/core/blockchain/impl/storage_util.cpp b/core/blockchain/impl/storage_util.cpp index a18913ae8a..2a4e9ab1b7 100644 --- a/core/blockchain/impl/storage_util.cpp +++ b/core/blockchain/impl/storage_util.cpp @@ -55,7 +55,7 @@ namespace kagome::blockchain { return map.contains(prependPrefix(key.value(), prefix)); } - outcome::result> getWithPrefix( + outcome::result> getWithPrefix( const storage::BufferStorage &map, prefix::Prefix prefix, const primitives::BlockId &block_id) { diff --git a/core/blockchain/impl/storage_util.hpp b/core/blockchain/impl/storage_util.hpp index 5aacae7d1c..1ebb151805 100644 --- a/core/blockchain/impl/storage_util.hpp +++ b/core/blockchain/impl/storage_util.hpp @@ -86,7 +86,7 @@ namespace kagome::blockchain { * @param block_id - id of the block to get entry for * @return error, or an encoded entry, if any, or std::nullopt, if none */ - outcome::result> getWithPrefix( + outcome::result> getWithPrefix( const storage::BufferStorage &storage, prefix::Prefix prefix, const primitives::BlockId &block_id); diff --git a/core/consensus/babe/impl/consistency_keeper_impl.cpp b/core/consensus/babe/impl/consistency_keeper_impl.cpp index 807f9caef4..5e77c25a21 100644 --- a/core/consensus/babe/impl/consistency_keeper_impl.cpp +++ b/core/consensus/babe/impl/consistency_keeper_impl.cpp @@ -41,7 +41,7 @@ namespace kagome::consensus::babe { } // check if record exists - auto buf_opt = buf_opt_res.value(); + auto &buf_opt = buf_opt_res.value(); if (not buf_opt.has_value()) { return true; } diff --git a/core/offchain/impl/offchain_local_storage.cpp b/core/offchain/impl/offchain_local_storage.cpp index 8d371506a3..7489cfa3a5 100644 --- a/core/offchain/impl/offchain_local_storage.cpp +++ b/core/offchain/impl/offchain_local_storage.cpp @@ -87,6 +87,7 @@ namespace kagome::offchain { throw std::invalid_argument("Off-chain local storage is unavailable yet"); auto iKey = internalKey(key); - return storage_->load(iKey); + OUTCOME_TRY(value, storage_->load(iKey)); + return value.into(); } } // namespace kagome::offchain diff --git a/core/offchain/impl/offchain_persistent_storage.cpp b/core/offchain/impl/offchain_persistent_storage.cpp index c782b9b743..eff1939e55 100644 --- a/core/offchain/impl/offchain_persistent_storage.cpp +++ b/core/offchain/impl/offchain_persistent_storage.cpp @@ -66,7 +66,8 @@ namespace kagome::offchain { outcome::result OffchainPersistentStorageImpl::get( const common::BufferView &key) { auto iKey = internalKey(key); - return storage_->load(iKey); + OUTCOME_TRY(value, storage_->load(iKey)); + return value.into(); } } // namespace kagome::offchain diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index 9f3c7901e9..20c483b988 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -8,7 +8,7 @@ #include -#include "storage/face/map_cursor.hpp" +#include "storage/face/owned_or_view.hpp" namespace kagome::storage::face { @@ -64,7 +64,6 @@ namespace kagome::storage::face { template struct ReadableStorage : public ReadableBase { using Key = K; - using Value = V; virtual ~ReadableStorage() = default; @@ -73,14 +72,15 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result load(const Key &key) const = 0; + virtual outcome::result> load(const Key &key) const = 0; /** * @brief Load value by key * @param key K * @return V if contains(K) or std::nullopt */ - virtual outcome::result> tryLoad(const Key &key) const = 0; + virtual outcome::result>> tryLoad( + const Key &key) const = 0; }; } // namespace kagome::storage::face diff --git a/core/storage/in_memory/in_memory_storage.cpp b/core/storage/in_memory/in_memory_storage.cpp index 94bd1b66c8..54050e1a43 100644 --- a/core/storage/in_memory/in_memory_storage.cpp +++ b/core/storage/in_memory/in_memory_storage.cpp @@ -12,19 +12,19 @@ using kagome::common::Buffer; namespace kagome::storage { - outcome::result InMemoryStorage::load( + outcome::result InMemoryStorage::load( const BufferView &key) const { if (storage.find(key.toHex()) != storage.end()) { - return storage.at(key.toHex()); + return BufferView{storage.at(key.toHex())}; } return DatabaseError::NOT_FOUND; } - outcome::result> InMemoryStorage::tryLoad( + outcome::result> InMemoryStorage::tryLoad( const common::BufferView &key) const { if (storage.find(key.toHex()) != storage.end()) { - return storage.at(key.toHex()); + return BufferView{storage.at(key.toHex())}; } return std::nullopt; diff --git a/core/storage/in_memory/in_memory_storage.hpp b/core/storage/in_memory/in_memory_storage.hpp index 3137c89003..ed2dd5d489 100644 --- a/core/storage/in_memory/in_memory_storage.hpp +++ b/core/storage/in_memory/in_memory_storage.hpp @@ -23,10 +23,10 @@ namespace kagome::storage { public: ~InMemoryStorage() override = default; - outcome::result load( + outcome::result load( const common::BufferView &key) const override; - outcome::result> tryLoad( + outcome::result> tryLoad( const common::BufferView &key) const override; outcome::result put(const common::BufferView &key, diff --git a/core/storage/rocksdb/rocksdb.cpp b/core/storage/rocksdb/rocksdb.cpp index a84a6ece2f..b44bab2c91 100644 --- a/core/storage/rocksdb/rocksdb.cpp +++ b/core/storage/rocksdb/rocksdb.cpp @@ -123,7 +123,7 @@ namespace kagome::storage { return it->Valid(); } - outcome::result RocksDB::load(const BufferView &key) const { + outcome::result RocksDB::load(const BufferView &key) const { std::string value; auto status = db_->Get(ro_, make_slice(key), &value); if (status.ok()) { @@ -135,7 +135,7 @@ namespace kagome::storage { return status_as_error(status); } - outcome::result> RocksDB::tryLoad( + outcome::result> RocksDB::tryLoad( const BufferView &key) const { std::string value; auto status = db_->Get(ro_, make_slice(key), &value); diff --git a/core/storage/rocksdb/rocksdb.hpp b/core/storage/rocksdb/rocksdb.hpp index 40d6a9012f..6d3aa3eaf9 100644 --- a/core/storage/rocksdb/rocksdb.hpp +++ b/core/storage/rocksdb/rocksdb.hpp @@ -42,10 +42,9 @@ namespace kagome::storage { bool empty() const override; - outcome::result load( - const Key &key) const override; + outcome::result load(const Key &key) const override; - outcome::result> tryLoad( + outcome::result> tryLoad( const Key &key) const override; outcome::result put(const BufferView &key, diff --git a/core/storage/trie/impl/trie_storage_backend_impl.cpp b/core/storage/trie/impl/trie_storage_backend_impl.cpp index 609edaf7c6..803c61ce09 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.cpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.cpp @@ -29,12 +29,12 @@ namespace kagome::storage::trie { node_prefix_); } - outcome::result TrieStorageBackendImpl::load( + outcome::result TrieStorageBackendImpl::load( const BufferView &key) const { return storage_->load(prefixKey(key)); } - outcome::result> TrieStorageBackendImpl::tryLoad( + outcome::result> TrieStorageBackendImpl::tryLoad( const BufferView &key) const { return storage_->tryLoad(prefixKey(key)); } diff --git a/core/storage/trie/impl/trie_storage_backend_impl.hpp b/core/storage/trie/impl/trie_storage_backend_impl.hpp index e36471fd24..f1a70832e5 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.hpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.hpp @@ -22,8 +22,8 @@ namespace kagome::storage::trie { std::unique_ptr cursor() override; std::unique_ptr> batch() override; - outcome::result load(const BufferView &key) const override; - outcome::result> tryLoad( + outcome::result load(const BufferView &key) const override; + outcome::result> tryLoad( const BufferView &key) const override; outcome::result contains(const BufferView &key) const override; bool empty() const override; diff --git a/core/storage/trie/serialization/trie_serializer_impl.cpp b/core/storage/trie/serialization/trie_serializer_impl.cpp index 68769d50e0..444e42ce7b 100644 --- a/core/storage/trie/serialization/trie_serializer_impl.cpp +++ b/core/storage/trie/serialization/trie_serializer_impl.cpp @@ -84,7 +84,7 @@ namespace kagome::storage::trie { Buffer enc; if (codec_->isMerkleHash(db_key)) { OUTCOME_TRY(db, backend_->load(db_key)); - enc = std::move(db); + enc = db.into(); } else { // `isMerkleHash(db_key) == false` means `db_key` is value itself enc = db_key; diff --git a/test/core/blockchain/block_storage_test.cpp b/test/core/blockchain/block_storage_test.cpp index b8af91c756..80a5f3e214 100644 --- a/test/core/blockchain/block_storage_test.cpp +++ b/test/core/blockchain/block_storage_test.cpp @@ -56,7 +56,7 @@ class BlockStorageTest : public testing::Test { .WillRepeatedly(Return(genesis_block_hash)); // check if storage contained genesis block - EXPECT_CALL(*storage, tryLoad(_)).WillRepeatedly(Return(std::nullopt)); + EXPECT_CALL(*storage, tryLoadMock(_)).WillRepeatedly(Return(std::nullopt)); // put genesis block into storage EXPECT_CALL(*storage, put(_, _)).WillRepeatedly(Return(outcome::success())); @@ -92,7 +92,8 @@ TEST_F(BlockStorageTest, CreateWithEmptyStorage) { .WillRepeatedly(Return(genesis_block_hash)); // check if storage contained genesis block - EXPECT_CALL(*empty_storage, tryLoad(_)).WillRepeatedly(Return(std::nullopt)); + EXPECT_CALL(*empty_storage, tryLoadMock(_)) + .WillRepeatedly(Return(std::nullopt)); // put genesis block into storage EXPECT_CALL(*empty_storage, put(_, _)) @@ -112,7 +113,7 @@ TEST_F(BlockStorageTest, CreateWithEmptyStorage) { TEST_F(BlockStorageTest, CreateWithExistingGenesis) { // trying to get header of block number 0 (genesis block) EXPECT_CALL(*storage, contains(_)).WillOnce(Return(outcome::success(true))); - EXPECT_CALL(*storage, tryLoad(_)) + EXPECT_CALL(*storage, tryLoadMock(_)) // trying to get header of block number 0 (genesis block) .WillOnce(Return(Buffer{genesis_block_hash})); @@ -131,7 +132,7 @@ TEST_F(BlockStorageTest, CreateWithStorageError) { std::make_shared>(); // check if storage contained genesis block - EXPECT_CALL(*empty_storage, tryLoad(_)) + EXPECT_CALL(*empty_storage, tryLoadMock(_)) .WillOnce(Return(kagome::storage::DatabaseError::IO_ERROR)); EXPECT_OUTCOME_ERROR( @@ -150,7 +151,7 @@ TEST_F(BlockStorageTest, PutBlock) { EXPECT_CALL(*hasher, blake2b_256(_)).WillOnce(Return(regular_block_hash)); - EXPECT_CALL(*storage, tryLoad(_)).WillOnce(Return(std::nullopt)); + EXPECT_CALL(*storage, tryLoadMock(_)).WillOnce(Return(std::nullopt)); Block block; block.header.number = 1; @@ -167,7 +168,7 @@ TEST_F(BlockStorageTest, PutBlock) { TEST_F(BlockStorageTest, PutWithStorageError) { auto block_storage = createWithGenesis(); - EXPECT_CALL(*storage, tryLoad(_)) + EXPECT_CALL(*storage, tryLoadMock(_)) .WillOnce(Return(Buffer{1, 1, 1, 1})) .WillOnce(Return(kagome::storage::DatabaseError::IO_ERROR)); diff --git a/test/core/consensus/babe/babe_config_repository_test.cpp b/test/core/consensus/babe/babe_config_repository_test.cpp index ebaee5d60a..6da3bc90c6 100644 --- a/test/core/consensus/babe/babe_config_repository_test.cpp +++ b/test/core/consensus/babe/babe_config_repository_test.cpp @@ -58,7 +58,7 @@ class BabeConfigRepositoryTest : public testing::Test { persistent_storage = std::make_shared>(); - EXPECT_CALL(*persistent_storage, tryLoad(_)) + EXPECT_CALL(*persistent_storage, tryLoadMock(_)) .WillRepeatedly(Return(std::nullopt)); block_tree = std::make_shared(); diff --git a/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp b/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp index aea52d6065..922f3f46aa 100644 --- a/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp @@ -54,7 +54,8 @@ TEST_F(TrieDbBackendTest, Put) { TEST_F(TrieDbBackendTest, Get) { Buffer prefixed{kNodePrefix}; prefixed.put("abc"_buf); - EXPECT_CALL(*storage, load(BufferView{prefixed})).WillOnce(Return("123"_buf)); + EXPECT_CALL(*storage, loadMock(BufferView{prefixed})) + .WillOnce(Return("123"_buf)); EXPECT_OUTCOME_TRUE_1(backend.load("abc"_buf)); } diff --git a/test/mock/core/storage/persistent_map_mock.hpp b/test/mock/core/storage/persistent_map_mock.hpp index 0242f04ed1..1e75058396 100644 --- a/test/mock/core/storage/persistent_map_mock.hpp +++ b/test/mock/core/storage/persistent_map_mock.hpp @@ -20,10 +20,26 @@ namespace kagome::storage::face { cursor, std::unique_ptr::Cursor>()); - MOCK_CONST_METHOD1_T(load, outcome::result(const KView &)); + MOCK_METHOD(outcome::result, loadMock, (const KView &), (const)); - MOCK_CONST_METHOD1_T(tryLoad, - outcome::result>(const KView &)); + MOCK_METHOD(outcome::result>, + tryLoadMock, + (const KView &), + (const)); + + outcome::result> load(const KView &key) const override { + OUTCOME_TRY(value, loadMock(key)); + return std::move(value); + } + + outcome::result>> tryLoad( + const KView &key) const override { + OUTCOME_TRY(value, tryLoadMock(key)); + if (value) { + return std::move(*value); + } + return std::nullopt; + } MOCK_CONST_METHOD1_T(contains, outcome::result(const KView &)); From 0b432bfd2bf39b404da9376cd0efc86bdcfe644c Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 14:51:07 +0300 Subject: [PATCH 05/19] use alias Signed-off-by: turuslan --- core/blockchain/impl/block_storage_impl.cpp | 2 -- core/storage/in_memory/in_memory_batch.hpp | 3 +-- core/storage/in_memory/in_memory_storage.cpp | 3 +-- core/storage/in_memory/in_memory_storage.hpp | 4 +--- .../storage/trie/impl/trie_storage_backend_batch.cpp | 4 +--- .../storage/trie/impl/trie_storage_backend_batch.hpp | 12 ++++-------- core/storage/trie/impl/trie_storage_backend_impl.cpp | 3 +-- core/storage/trie/impl/trie_storage_backend_impl.hpp | 2 +- core/storage/trie/trie_batches.hpp | 2 -- .../storage/trie/trie_storage/trie_batch_test.cpp | 1 - .../core/storage/trie/trie_storage_backend_mock.hpp | 5 +---- 11 files changed, 11 insertions(+), 30 deletions(-) diff --git a/core/blockchain/impl/block_storage_impl.cpp b/core/blockchain/impl/block_storage_impl.cpp index 380f1593ee..02271a9749 100644 --- a/core/blockchain/impl/block_storage_impl.cpp +++ b/core/blockchain/impl/block_storage_impl.cpp @@ -12,8 +12,6 @@ namespace kagome::blockchain { using primitives::Block; using primitives::BlockId; - using storage::face::MapCursor; - using storage::face::WriteBatch; using Buffer = common::Buffer; using Prefix = prefix::Prefix; diff --git a/core/storage/in_memory/in_memory_batch.hpp b/core/storage/in_memory/in_memory_batch.hpp index e31682cada..c56650786e 100644 --- a/core/storage/in_memory/in_memory_batch.hpp +++ b/core/storage/in_memory/in_memory_batch.hpp @@ -12,8 +12,7 @@ namespace kagome::storage { using kagome::common::Buffer; - class InMemoryBatch - : public kagome::storage::face::WriteBatch { + class InMemoryBatch : public BufferBatch { public: explicit InMemoryBatch(InMemoryStorage &db) : db{db} {} diff --git a/core/storage/in_memory/in_memory_storage.cpp b/core/storage/in_memory/in_memory_storage.cpp index 54050e1a43..eb84cc1a4b 100644 --- a/core/storage/in_memory/in_memory_storage.cpp +++ b/core/storage/in_memory/in_memory_storage.cpp @@ -60,8 +60,7 @@ namespace kagome::storage { return outcome::success(); } - std::unique_ptr> - InMemoryStorage::batch() { + std::unique_ptr InMemoryStorage::batch() { return std::make_unique(*this); } diff --git a/core/storage/in_memory/in_memory_storage.hpp b/core/storage/in_memory/in_memory_storage.hpp index ed2dd5d489..d978890cda 100644 --- a/core/storage/in_memory/in_memory_storage.hpp +++ b/core/storage/in_memory/in_memory_storage.hpp @@ -39,9 +39,7 @@ namespace kagome::storage { outcome::result remove(const common::BufferView &key) override; - std::unique_ptr< - kagome::storage::face::WriteBatch> - batch() override; + std::unique_ptr batch() override; std::unique_ptr cursor() override; diff --git a/core/storage/trie/impl/trie_storage_backend_batch.cpp b/core/storage/trie/impl/trie_storage_backend_batch.cpp index 641c7d2666..0236c168b5 100644 --- a/core/storage/trie/impl/trie_storage_backend_batch.cpp +++ b/core/storage/trie/impl/trie_storage_backend_batch.cpp @@ -8,9 +8,7 @@ namespace kagome::storage::trie { TrieStorageBackendBatch::TrieStorageBackendBatch( - std::unique_ptr> - storage_batch, - common::Buffer node_prefix) + std::unique_ptr storage_batch, common::Buffer node_prefix) : storage_batch_{std::move(storage_batch)}, node_prefix_{std::move(node_prefix)} { BOOST_ASSERT(storage_batch_ != nullptr); diff --git a/core/storage/trie/impl/trie_storage_backend_batch.hpp b/core/storage/trie/impl/trie_storage_backend_batch.hpp index 12f3b95b81..b23498c52b 100644 --- a/core/storage/trie/impl/trie_storage_backend_batch.hpp +++ b/core/storage/trie/impl/trie_storage_backend_batch.hpp @@ -14,13 +14,10 @@ namespace kagome::storage::trie { * Batch implementation for TrieStorageBackend * @see TrieStorageBackend */ - class TrieStorageBackendBatch - : public face::WriteBatch { + class TrieStorageBackendBatch : public BufferBatch { public: - TrieStorageBackendBatch( - std::unique_ptr> - storage_batch, - common::Buffer node_prefix); + TrieStorageBackendBatch(std::unique_ptr storage_batch, + common::Buffer node_prefix); ~TrieStorageBackendBatch() override = default; outcome::result commit() override; @@ -34,8 +31,7 @@ namespace kagome::storage::trie { private: common::Buffer prefixKey(const common::BufferView &key) const; - std::unique_ptr> - storage_batch_; + std::unique_ptr storage_batch_; common::Buffer node_prefix_; }; diff --git a/core/storage/trie/impl/trie_storage_backend_impl.cpp b/core/storage/trie/impl/trie_storage_backend_impl.cpp index 803c61ce09..6ec8a27cff 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.cpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.cpp @@ -23,8 +23,7 @@ namespace kagome::storage::trie { ->cursor(); // TODO(Harrm): perhaps should iterate over trie nodes only } - std::unique_ptr> - TrieStorageBackendImpl::batch() { + std::unique_ptr TrieStorageBackendImpl::batch() { return std::make_unique(storage_->batch(), node_prefix_); } diff --git a/core/storage/trie/impl/trie_storage_backend_impl.hpp b/core/storage/trie/impl/trie_storage_backend_impl.hpp index f1a70832e5..abf6b88967 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.hpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.hpp @@ -20,7 +20,7 @@ namespace kagome::storage::trie { ~TrieStorageBackendImpl() override = default; std::unique_ptr cursor() override; - std::unique_ptr> batch() override; + std::unique_ptr batch() override; outcome::result load(const BufferView &key) const override; outcome::result> tryLoad( diff --git a/core/storage/trie/trie_batches.hpp b/core/storage/trie/trie_batches.hpp index 7edd492e38..43bed0a7ad 100644 --- a/core/storage/trie/trie_batches.hpp +++ b/core/storage/trie/trie_batches.hpp @@ -19,8 +19,6 @@ namespace kagome::storage::trie { public: ~TrieBatch() override = default; - using Cursor = - face::Iterable::Cursor; std::unique_ptr cursor() final { return trieCursor(); } diff --git a/test/core/storage/trie/trie_storage/trie_batch_test.cpp b/test/core/storage/trie/trie_storage/trie_batch_test.cpp index b93ff154c8..219d2119c5 100644 --- a/test/core/storage/trie/trie_storage/trie_batch_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_batch_test.cpp @@ -25,7 +25,6 @@ using kagome::common::BufferOrView; using kagome::common::BufferView; using kagome::common::Hash256; using kagome::primitives::BlockHash; -using kagome::storage::face::WriteBatch; using kagome::subscription::SubscriptionEngine; using testing::_; using testing::Invoke; diff --git a/test/mock/core/storage/trie/trie_storage_backend_mock.hpp b/test/mock/core/storage/trie/trie_storage_backend_mock.hpp index cfc4e7e0b5..a815f82745 100644 --- a/test/mock/core/storage/trie/trie_storage_backend_mock.hpp +++ b/test/mock/core/storage/trie/trie_storage_backend_mock.hpp @@ -14,10 +14,7 @@ namespace kagome::storage::trie { class TrieStorageBackendMock : public TrieStorageBackend { public: - MOCK_METHOD(std::unique_ptr>, - batch, - (), - (override)); + MOCK_METHOD(std::unique_ptr, batch, (), (override)); MOCK_METHOD(std::unique_ptr>, cursor, From ad06b838b64e84cb5fb6b0cb9db3a7525578b3de Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 15:54:52 +0300 Subject: [PATCH 06/19] cursor value Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 12 ++++++++++++ core/storage/buffer_map_types.hpp | 3 +-- core/storage/face/generic_maps.hpp | 5 ++--- core/storage/face/map_cursor.hpp | 3 ++- core/storage/rocksdb/rocksdb_cursor.cpp | 2 +- core/storage/rocksdb/rocksdb_cursor.hpp | 2 +- .../trie/polkadot_trie/polkadot_trie_cursor.hpp | 6 ++---- .../polkadot_trie/polkadot_trie_cursor_impl.cpp | 5 ++--- .../polkadot_trie/polkadot_trie_cursor_impl.hpp | 2 +- core/storage/trie/trie_batches.hpp | 7 +++---- .../storage/rocksdb/rocksdb_integration_test.cpp | 2 +- .../polkadot_trie/polkadot_trie_cursor_test.cpp | 14 +++++++------- .../storage/trie/polkadot_trie_cursor_dummy.hpp | 4 ++-- .../core/storage/trie/polkadot_trie_cursor_mock.h | 5 +---- 14 files changed, 38 insertions(+), 34 deletions(-) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index 68112791cb..9e89314025 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -79,6 +79,18 @@ namespace kagome::common { friend bool operator!=(const Span &l, const BufferOrView &r) { return l != r.view(); } + friend bool operator==(const BufferOrView &l, const Buffer &r) { + return l.view() == BufferView{r}; + } + friend bool operator!=(const BufferOrView &l, const Buffer &r) { + return l.view() != BufferView{r}; + } + friend bool operator==(const Buffer &l, const BufferOrView &r) { + return BufferView{l} == r.view(); + } + friend bool operator!=(const Buffer &l, const BufferOrView &r) { + return BufferView{l} != r.view(); + } }; } // namespace kagome::common diff --git a/core/storage/buffer_map_types.hpp b/core/storage/buffer_map_types.hpp index 2efbfb8a8d..41905b3dd1 100644 --- a/core/storage/buffer_map_types.hpp +++ b/core/storage/buffer_map_types.hpp @@ -39,8 +39,7 @@ namespace kagome::storage { using BufferMap = face::GenericMap; - using BufferMapCursor = - face::MapCursor; + using BufferMapCursor = face::MapCursor; using BufferStorageCursor = face::MapCursor; diff --git a/core/storage/face/generic_maps.hpp b/core/storage/face/generic_maps.hpp index e9a08dc595..63f6a60489 100644 --- a/core/storage/face/generic_maps.hpp +++ b/core/storage/face/generic_maps.hpp @@ -19,9 +19,8 @@ namespace kagome::storage::face { * @tparam V value type */ template - struct ReadOnlyMap - : public Iterable::ConstValueView, KView>, - public ReadableMap {}; + struct ReadOnlyMap : public Iterable, + public ReadableMap {}; template struct ReadOnlyStorage : public Iterable, diff --git a/core/storage/face/map_cursor.hpp b/core/storage/face/map_cursor.hpp index 23e35efcc3..a1b88ad5e1 100644 --- a/core/storage/face/map_cursor.hpp +++ b/core/storage/face/map_cursor.hpp @@ -9,6 +9,7 @@ #include #include "outcome/outcome.hpp" +#include "storage/face/owned_or_view.hpp" namespace kagome::storage::face { @@ -62,7 +63,7 @@ namespace kagome::storage::face { * @brief Getter for value of the element currently pointed at. * @return value if isValid() */ - virtual std::optional value() const = 0; + virtual std::optional> value() const = 0; }; } // namespace kagome::storage::face diff --git a/core/storage/rocksdb/rocksdb_cursor.cpp b/core/storage/rocksdb/rocksdb_cursor.cpp index f945aa2fce..89fd864c84 100644 --- a/core/storage/rocksdb/rocksdb_cursor.cpp +++ b/core/storage/rocksdb/rocksdb_cursor.cpp @@ -41,7 +41,7 @@ namespace kagome::storage { : std::nullopt; } - std::optional RocksDBCursor::value() const { + std::optional RocksDBCursor::value() const { return isValid() ? std::make_optional(make_buffer(i_->value())) : std::nullopt; } diff --git a/core/storage/rocksdb/rocksdb_cursor.hpp b/core/storage/rocksdb/rocksdb_cursor.hpp index e101d59e94..d460fc4562 100644 --- a/core/storage/rocksdb/rocksdb_cursor.hpp +++ b/core/storage/rocksdb/rocksdb_cursor.hpp @@ -29,7 +29,7 @@ namespace kagome::storage { std::optional key() const override; - std::optional value() const override; + std::optional value() const override; private: std::shared_ptr i_; diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_cursor.hpp b/core/storage/trie/polkadot_trie/polkadot_trie_cursor.hpp index 0e0293f1e8..6a569e9194 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_cursor.hpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_cursor.hpp @@ -6,16 +6,14 @@ #ifndef KAGOME_CORE_STORAGE_TRIE_POLKADOT_TRIE_POLKADOT_TRIE_CURSOR #define KAGOME_CORE_STORAGE_TRIE_POLKADOT_TRIE_POLKADOT_TRIE_CURSOR -#include "storage/face/map_cursor.hpp" +#include "storage/buffer_map_types.hpp" #include "common/buffer.hpp" #include "storage/trie/polkadot_trie/trie_node.hpp" namespace kagome::storage::trie { - class PolkadotTrieCursor : public face::MapCursor { + class PolkadotTrieCursor : public BufferStorageCursor { public: virtual ~PolkadotTrieCursor() override = default; diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.cpp b/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.cpp index ebdb9438e5..7120314edb 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.cpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.cpp @@ -373,13 +373,12 @@ namespace kagome::storage::trie { return std::nullopt; } - std::optional PolkadotTrieCursorImpl::value() const { + std::optional PolkadotTrieCursorImpl::value() const { if (const auto *search_state = std::get_if(&state_); search_state != nullptr) { const auto &value_opt = search_state->getCurrent().value; if (value_opt) { - return std::make_optional( - std::cref(value_opt.value())); + return BufferView{*value_opt}; } return std::nullopt; } diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.hpp b/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.hpp index 1a4a210f7b..fa87564f69 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.hpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_cursor_impl.hpp @@ -64,7 +64,7 @@ namespace kagome::storage::trie { [[nodiscard]] std::optional key() const override; - [[nodiscard]] std::optional value() const override; + [[nodiscard]] std::optional value() const override; private: outcome::result seekLowerBoundInternal( diff --git a/core/storage/trie/trie_batches.hpp b/core/storage/trie/trie_batches.hpp index 43bed0a7ad..9b56ec20b9 100644 --- a/core/storage/trie/trie_batches.hpp +++ b/core/storage/trie/trie_batches.hpp @@ -12,10 +12,9 @@ namespace kagome::storage::trie { - class TrieBatch - : public face::ReadableMap, - public face::Writeable, - public face::Iterable { + class TrieBatch : public face::ReadableMap, + public face::Writeable, + public face::Iterable { public: ~TrieBatch() override = default; diff --git a/test/core/storage/rocksdb/rocksdb_integration_test.cpp b/test/core/storage/rocksdb/rocksdb_integration_test.cpp index 407b56ee87..c2a74709c8 100644 --- a/test/core/storage/rocksdb/rocksdb_integration_test.cpp +++ b/test/core/storage/rocksdb/rocksdb_integration_test.cpp @@ -117,7 +117,7 @@ TEST_F(RocksDb_Integration_Test, Iterator) { auto v = it->value().value(); EXPECT_EQ(k, v); - logger->info("key: {}, value: {}", k.toHex(), v.toHex()); + logger->info("key: {}, value: {}", k.toHex(), v.view().toHex()); EXPECT_GE(k[0], 0); EXPECT_LT(k[0], size); diff --git a/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp b/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp index 4e829b6cfe..210c0d64ed 100644 --- a/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp +++ b/test/core/storage/trie/polkadot_trie/polkadot_trie_cursor_test.cpp @@ -109,7 +109,7 @@ TEST_F(PolkadotTrieCursorTest, NextOnSmallTrie) { ASSERT_TRUE(cursor.key()); ASSERT_TRUE(cursor.value()); ASSERT_EQ(cursor.key().value(), p.first); - ASSERT_EQ(cursor.value().value().get(), p.second); + ASSERT_EQ(cursor.value().value(), p.second); } EXPECT_OUTCOME_SUCCESS(r1, cursor.next()) ASSERT_FALSE(cursor.isValid()); @@ -190,9 +190,9 @@ TEST_F(PolkadotTrieCursorTest, LowerBoundKeyNotPresent) { auto trie = makeTrie(lex_sorted_vals); auto cursor = trie->trieCursor(); cursor->seekLowerBound("06066666"_hex2buf).value(); - ASSERT_EQ(cursor->value().value().get(), "0607"_hex2buf); + ASSERT_EQ(cursor->value().value(), "0607"_hex2buf); EXPECT_OUTCOME_TRUE_1(cursor->next()) - ASSERT_EQ(cursor->value().value().get(), "060708"_hex2buf); + ASSERT_EQ(cursor->value().value(), "060708"_hex2buf); } /** @@ -218,9 +218,9 @@ TEST_F(PolkadotTrieCursorTest, LowerBoundMiddleFromRoot) { auto trie = makeTrie(lex_sorted_vals); auto cursor = trie->trieCursor(); cursor->seekLowerBound("03"_hex2buf).value(); - ASSERT_EQ(cursor->value().value().get(), "05"_hex2buf); + ASSERT_EQ(cursor->value().value(), "05"_hex2buf); EXPECT_OUTCOME_TRUE_1(cursor->next()) - ASSERT_EQ(cursor->value().value().get(), "06"_hex2buf); + ASSERT_EQ(cursor->value().value(), "06"_hex2buf); } /** @@ -234,9 +234,9 @@ TEST_F(PolkadotTrieCursorTest, LowerBoundFirstKey) { auto cursor = trie->trieCursor(); cursor->seekLowerBound("00"_hex2buf).value(); - ASSERT_EQ(cursor->value().value().get(), "0102"_hex2buf); + ASSERT_EQ(cursor->value().value(), "0102"_hex2buf); EXPECT_OUTCOME_TRUE_1(cursor->next()) - ASSERT_EQ(cursor->value().value().get(), "0103"_hex2buf); + ASSERT_EQ(cursor->value().value(), "0103"_hex2buf); } /** diff --git a/test/core/storage/trie/polkadot_trie_cursor_dummy.hpp b/test/core/storage/trie/polkadot_trie_cursor_dummy.hpp index 955bc2a9d4..315ac8a7ab 100644 --- a/test/core/storage/trie/polkadot_trie_cursor_dummy.hpp +++ b/test/core/storage/trie/polkadot_trie_cursor_dummy.hpp @@ -61,8 +61,8 @@ namespace kagome::storage::trie { return current_->first; } - std::optional value() const override { - return current_->second; + std::optional value() const override { + return BufferView{current_->second}; } }; } // namespace kagome::storage::trie diff --git a/test/mock/core/storage/trie/polkadot_trie_cursor_mock.h b/test/mock/core/storage/trie/polkadot_trie_cursor_mock.h index b6e6acda7a..3f66c11ea2 100644 --- a/test/mock/core/storage/trie/polkadot_trie_cursor_mock.h +++ b/test/mock/core/storage/trie/polkadot_trie_cursor_mock.h @@ -34,10 +34,7 @@ namespace kagome::storage::trie { MOCK_METHOD(std::optional, key, (), (const, override)); - MOCK_METHOD(std::optional, - value, - (), - (const, override)); + MOCK_METHOD(std::optional, value, (), (const, override)); }; } // namespace kagome::storage::trie From 152e3db27527108c777098c85e6515b7b3ab3580 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 19:25:50 +0300 Subject: [PATCH 07/19] get Signed-off-by: turuslan --- .../child_state/impl/child_state_api_impl.cpp | 18 ++-- .../api/service/state/impl/state_api_impl.cpp | 15 ++- core/common/monadic_utils.hpp | 5 +- .../host_api/impl/child_storage_extension.cpp | 24 +++-- core/host_api/impl/storage_extension.cpp | 12 ++- core/host_api/impl/storage_extension.hpp | 3 +- core/log/logger.hpp | 6 +- .../impl/state_protocol_observer_impl.cpp | 13 +-- .../common/runtime_environment_factory.cpp | 4 +- core/runtime/common/storage_code_provider.cpp | 2 +- .../common/trie_storage_provider_impl.cpp | 7 +- core/storage/face/readable.hpp | 7 +- .../trie/impl/ephemeral_trie_batch_impl.cpp | 4 +- .../trie/impl/ephemeral_trie_batch_impl.hpp | 4 +- .../trie/impl/persistent_trie_batch_impl.cpp | 6 +- .../trie/impl/persistent_trie_batch_impl.hpp | 4 +- .../trie/impl/topper_trie_batch_impl.cpp | 10 +- .../trie/impl/topper_trie_batch_impl.hpp | 5 +- .../trie/polkadot_trie/polkadot_trie_impl.cpp | 10 +- .../trie/polkadot_trie/polkadot_trie_impl.hpp | 4 +- core/utils/kagome_db_editor.cpp | 7 +- core/utils/storage_explorer.cpp | 2 +- .../child_state/child_state_api_test.cpp | 16 ++-- .../core/api/service/state/state_api_test.cpp | 6 +- .../authority/authority_manager_test.cpp | 2 +- .../host_api/child_storage_extension_test.cpp | 15 +-- test/core/host_api/storage_extension_test.cpp | 12 +-- test/core/runtime/runtime_test_base.hpp | 6 +- .../runtime/storage_code_provider_test.cpp | 9 +- .../runtime/trie_storage_provider_test.cpp | 2 +- .../trie/polkadot_trie/polkadot_trie_test.cpp | 12 +-- .../trie/trie_storage/trie_batch_test.cpp | 8 +- .../trie/trie_storage/trie_storage_test.cpp | 6 +- .../core/storage/trie/trie_batches_mock.hpp | 93 ++++++++----------- 34 files changed, 163 insertions(+), 196 deletions(-) diff --git a/core/api/service/child_state/impl/child_state_api_impl.cpp b/core/api/service/child_state/impl/child_state_api_impl.cpp index 03e0d49367..b020537c37 100644 --- a/core/api/service/child_state/impl/child_state_api_impl.cpp +++ b/core/api/service/child_state/impl/child_state_api_impl.cpp @@ -51,8 +51,7 @@ namespace kagome::api { OUTCOME_TRY(initial_trie_reader, storage_->getEphemeralBatchAt(header.state_root)); OUTCOME_TRY(child_root, initial_trie_reader->get(child_storage_key)); - OUTCOME_TRY(child_root_hash, - common::Hash256::fromSpan(gsl::make_span(child_root.get()))); + OUTCOME_TRY(child_root_hash, common::Hash256::fromSpan(child_root)); OUTCOME_TRY(child_storage_trie_reader, storage_->getEphemeralBatchAt(child_root_hash)); auto cursor = child_storage_trie_reader->trieCursor(); @@ -90,8 +89,7 @@ namespace kagome::api { OUTCOME_TRY(initial_trie_reader, storage_->getEphemeralBatchAt(header.state_root)); OUTCOME_TRY(child_root, initial_trie_reader->get(child_storage_key)); - OUTCOME_TRY(child_root_hash, - common::Hash256::fromSpan(gsl::make_span(child_root.get()))); + OUTCOME_TRY(child_root_hash, common::Hash256::fromSpan(child_root)); OUTCOME_TRY(child_storage_trie_reader, storage_->getEphemeralBatchAt(child_root_hash)); auto cursor = child_storage_trie_reader->trieCursor(); @@ -132,13 +130,12 @@ namespace kagome::api { OUTCOME_TRY(header, header_repo_->getBlockHeader(at)); OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root)); OUTCOME_TRY(child_root, trie_reader->get(child_storage_key)); - OUTCOME_TRY(child_root_hash, - common::Hash256::fromSpan(gsl::make_span(child_root.get()))); + OUTCOME_TRY(child_root_hash, common::Hash256::fromSpan(child_root)); OUTCOME_TRY(child_storage_trie_reader, storage_->getEphemeralBatchAt(child_root_hash)); auto res = child_storage_trie_reader->tryGet(key); - return common::map_result_optional(res, - [](const auto &r) { return r.get(); }); + return common::map_result_optional( + std::move(res), [](common::BufferOrView &&r) { return r.into(); }); } outcome::result> @@ -164,11 +161,10 @@ namespace kagome::api { OUTCOME_TRY(header, header_repo_->getBlockHeader(at)); OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root)); OUTCOME_TRY(child_root, trie_reader->get(child_storage_key)); - OUTCOME_TRY(child_root_hash, - common::Hash256::fromSpan(gsl::make_span(child_root.get()))); + OUTCOME_TRY(child_root_hash, common::Hash256::fromSpan(child_root)); OUTCOME_TRY(child_storage_trie_reader, storage_->getEphemeralBatchAt(child_root_hash)); OUTCOME_TRY(value, child_storage_trie_reader->get(key)); - return value.get().size(); + return value.size(); } } // namespace kagome::api diff --git a/core/api/service/state/impl/state_api_impl.cpp b/core/api/service/state/impl/state_api_impl.cpp index 323532bbb1..07beee18dc 100644 --- a/core/api/service/state/impl/state_api_impl.cpp +++ b/core/api/service/state/impl/state_api_impl.cpp @@ -124,8 +124,8 @@ namespace kagome::api { OUTCOME_TRY(header, header_repo_->getBlockHeader(at)); OUTCOME_TRY(trie_reader, storage_->getEphemeralBatchAt(header.state_root)); auto res = trie_reader->tryGet(key); - return common::map_result_optional(res, - [](const auto &r) { return r.get(); }); + return common::map_result_optional( + std::move(res), [](common::BufferOrView &&r) { return r.into(); }); } outcome::result> @@ -164,14 +164,13 @@ namespace kagome::api { OUTCOME_TRY(batch, storage_->getEphemeralBatchAt(header.state_root)); StorageChangeSet change{block, {}}; for (auto &key : keys) { - OUTCOME_TRY(opt_value, batch->tryGet(key)); + OUTCOME_TRY(opt_get, batch->tryGet(key)); + auto opt_value = common::map_optional( + std::move(opt_get), + [](common::BufferOrView &&r) { return r.into(); }); auto it = last_values.find(key); if (it == last_values.end() || it->second != opt_value) { - std::optional opt_buffer = - opt_value ? std::make_optional(opt_value.value().get()) - : std::nullopt; - change.changes.push_back( - StorageChangeSet::Change{common::Buffer{key}, opt_buffer}); + change.changes.push_back(StorageChangeSet::Change{key, opt_value}); } last_values[key] = std::move(opt_value); } diff --git a/core/common/monadic_utils.hpp b/core/common/monadic_utils.hpp index 8f6b9ff0e0..b4188ef915 100644 --- a/core/common/monadic_utils.hpp +++ b/core/common/monadic_utils.hpp @@ -98,8 +98,9 @@ namespace kagome::common { template > outcome::result> map_result_optional( outcome::result> &&res_opt, F const &f) { - return map_result(res_opt, [&f](auto &opt) { - return map_optional(opt, [&f](auto &v) { return f(std::move(v)); }); + return map_result(std::move(res_opt), [&f](std::optional &&opt) { + return map_optional(std::move(opt), + [&f](T &&v) { return f(std::move(v)); }); }); } diff --git a/core/host_api/impl/child_storage_extension.cpp b/core/host_api/impl/child_storage_extension.cpp index f71404103b..ab31d462b1 100644 --- a/core/host_api/impl/child_storage_extension.cpp +++ b/core/host_api/impl/child_storage_extension.cpp @@ -132,7 +132,10 @@ namespace kagome::host_api { } // okay to throw, we want to end this runtime call with error return memory.storeBuffer( - scale::encode(result.value()).value()); + scale::encode( + common::map_optional(result.value(), + [](auto &r) { return r.view(); })) + .value()); }, key_buffer) .value(); @@ -273,17 +276,13 @@ namespace kagome::host_api { loadBuffer(memory, child_storage_key, key); auto [value_ptr, value_size] = runtime::PtrSize(value_out); - auto value = executeOnChildStorage>( + auto value = executeOnChildStorage>( child_key_buffer, - [](auto &child_batch, auto &key) { - return common::map_result_optional( - child_batch->tryGet(key), - [](auto &v) -> common::Buffer { return v.get(); }); - }, + [](auto &child_batch, auto &key) { return child_batch->tryGet(key); }, key_buffer); std::optional res{std::nullopt}; - if (auto data_opt_res = value; data_opt_res.has_value()) { - auto &data_opt = data_opt_res.value(); + if (value) { + auto &data_opt = value.value(); if (data_opt.has_value()) { common::BufferView data = data_opt.value(); data = data.subspan(std::min(offset, data.size())); @@ -305,10 +304,9 @@ namespace kagome::host_api { offset); } } else { - SL_ERROR(logger_, - "Error in ext_storage_read_version_1: {}", - data_opt_res.error()); - throw std::runtime_error{data_opt_res.error().message()}; + SL_ERROR( + logger_, "Error in ext_storage_read_version_1: {}", value.error()); + throw std::runtime_error{value.error().message()}; } return memory.storeBuffer(scale::encode(res).value()); diff --git a/core/host_api/impl/storage_extension.cpp b/core/host_api/impl/storage_extension.cpp index 8ecdd243f8..5c1ddf9bbc 100644 --- a/core/host_api/impl/storage_extension.cpp +++ b/core/host_api/impl/storage_extension.cpp @@ -8,6 +8,7 @@ #include #include "clock/impl/clock_impl.hpp" +#include "common/monadic_utils.hpp" #include "log/profiling_logger.hpp" #include "runtime/common/runtime_transaction_error.hpp" #include "runtime/memory_provider.hpp" @@ -78,7 +79,7 @@ namespace kagome::host_api { if (auto data_opt_res = get(key); data_opt_res.has_value()) { auto &data_opt = data_opt_res.value(); if (data_opt.has_value()) { - common::BufferView data = data_opt.value().get(); + common::BufferView data = *data_opt; data = data.subspan(std::min(offset, data.size())); auto written = std::min(data.size(), value.size); memory.storeBuffer(value.ptr, data.subspan(0, written)); @@ -98,7 +99,7 @@ namespace kagome::host_api { return memory.storeBuffer(scale::encode(res).value()); } - outcome::result> StorageExtension::get( + outcome::result> StorageExtension::get( const common::BufferView &key) const { auto batch = storage_provider_->getCurrentBatch(); return batch->tryGet(key); @@ -156,7 +157,10 @@ namespace kagome::host_api { auto &option = result.value(); - return memory.storeBuffer(scale::encode(option).value()); + return memory.storeBuffer( + scale::encode(common::map_optional(option, [](auto &r) { + return r.view(); + })).value()); } void StorageExtension::ext_storage_clear_version_1( @@ -472,7 +476,7 @@ namespace kagome::host_api { // SAFETY: key obtained by getStorageNextKey method, thus must exist in // the storage auto value_opt = get(current_key).value(); - if (value_opt and value_opt.value().get() == empty_hash) { + if (value_opt == empty_hash) { auto batch = storage_provider_->getCurrentBatch(); auto remove_res = batch->remove(current_key); if (not remove_res) { diff --git a/core/host_api/impl/storage_extension.hpp b/core/host_api/impl/storage_extension.hpp index 98014ef8c8..ae187035bd 100644 --- a/core/host_api/impl/storage_extension.hpp +++ b/core/host_api/impl/storage_extension.hpp @@ -8,6 +8,7 @@ #include +#include "common/buffer_or_view.hpp" #include "log/logger.hpp" #include "runtime/types.hpp" #include "storage/trie/serialization/polkadot_codec.hpp" @@ -143,7 +144,7 @@ namespace kagome::host_api { * @param key Buffer representation of the key * @return result containing Buffer with the value */ - outcome::result> get( + outcome::result> get( const common::BufferView &key) const; /** diff --git a/core/log/logger.hpp b/core/log/logger.hpp index d263dcc802..f618132f27 100644 --- a/core/log/logger.hpp +++ b/core/log/logger.hpp @@ -65,11 +65,7 @@ namespace kagome::log { return arg; } - template ().begin())>, - uint8_t>>> - std::string format_arg(T const &buffer) { + inline std::string format_arg(gsl::span buffer) { if (buffer.size() == 0) return ""; std::string res; if (std::all_of(buffer.begin(), buffer.end(), isalnum)) { diff --git a/core/network/impl/state_protocol_observer_impl.cpp b/core/network/impl/state_protocol_observer_impl.cpp index 5a58a34696..bfcc13b177 100644 --- a/core/network/impl/state_protocol_observer_impl.cpp +++ b/core/network/impl/state_protocol_observer_impl.cpp @@ -62,7 +62,7 @@ namespace kagome::network { const auto &value_opt = value_res.value(); if (value_opt.has_value()) { entry.entries.emplace_back( - StateEntry{cursor->key().value(), value_opt.value()}); + StateEntry{cursor->key().value(), {*value_opt}}); size += entry.entries.back().key.size() + entry.entries.back().value.size(); } @@ -106,9 +106,8 @@ namespace kagome::network { } if (auto value_res = batch->tryGet(parent_key); value_res.has_value() && value_res.value().has_value()) { - OUTCOME_TRY( - hash, - storage::trie::RootHash::fromSpan(value_res.value().value().get())); + OUTCOME_TRY(hash, + storage::trie::RootHash::fromSpan(*value_res.value())); OUTCOME_TRY( entry_res, this->getEntry(hash, request.start[1], MAX_RESPONSE_BYTES - size)); @@ -129,15 +128,13 @@ namespace kagome::network { value_res.has_value()) { const auto &value = value_res.value(); auto &entry = response.entries.front(); - entry.entries.emplace_back( - StateEntry{cursor->key().value(), value.value()}); + entry.entries.emplace_back(StateEntry{cursor->key().value(), {*value}}); size += entry.entries.back().key.size() + entry.entries.back().value.size(); // if key is child state storage hash iterate child storage keys if (boost::starts_with(cursor->key().value(), child_prefix)) { OUTCOME_TRY(hash, - storage::trie::RootHash::fromSpan( - value_res.value().value().get())); + storage::trie::RootHash::fromSpan(*value_res.value())); OUTCOME_TRY(entry_res, this->getEntry( hash, common::Buffer(), MAX_RESPONSE_BYTES - size)); diff --git a/core/runtime/common/runtime_environment_factory.cpp b/core/runtime/common/runtime_environment_factory.cpp index 1bea3f0483..255990e4f4 100644 --- a/core/runtime/common/runtime_environment_factory.cpp +++ b/core/runtime/common/runtime_environment_factory.cpp @@ -124,7 +124,7 @@ namespace kagome::runtime { auto heappages_res = env.storage_provider->getCurrentBatch()->get(heappages_key); if (heappages_res.has_value()) { - const auto &heappages = heappages_res.value().get(); + const auto &heappages = heappages_res.value(); if (sizeof(uint64_t) != heappages.size()) { parent_factory->logger_->error( "Unable to read :heappages value. Type size mismatch. " @@ -132,7 +132,7 @@ namespace kagome::runtime { sizeof(uint64_t), heappages.size()); } else { - uint64_t pages = common::le_bytes_to_uint64(heappages.asVector()); + uint64_t pages = common::le_bytes_to_uint64(heappages.view()); env.memory_provider->getCurrentMemory()->get().resize( pages * kMemoryPageSize); parent_factory->logger_->trace( diff --git a/core/runtime/common/storage_code_provider.cpp b/core/runtime/common/storage_code_provider.cpp index 4643ab3909..dcb5b74caa 100644 --- a/core/runtime/common/storage_code_provider.cpp +++ b/core/runtime/common/storage_code_provider.cpp @@ -55,7 +55,7 @@ namespace kagome::runtime { outcome::result StorageCodeProvider::setCodeFromBatch( const storage::trie::EphemeralTrieBatch &batch) const { OUTCOME_TRY(code, batch.get(storage::kRuntimeCodeKey)); - OUTCOME_TRY(uncompressCodeIfNeeded(code.get(), cached_code_)); + OUTCOME_TRY(uncompressCodeIfNeeded(code, cached_code_)); return outcome::success(); } } // namespace kagome::runtime diff --git a/core/runtime/common/trie_storage_provider_impl.cpp b/core/runtime/common/trie_storage_provider_impl.cpp index 9b4c85c943..6324500165 100644 --- a/core/runtime/common/trie_storage_provider_impl.cpp +++ b/core/runtime/common/trie_storage_provider_impl.cpp @@ -81,10 +81,9 @@ namespace kagome::runtime { root_path.toHex()); OUTCOME_TRY(child_root_value, getCurrentBatch()->tryGet(root_path)); auto child_root_hash = - child_root_value ? common::Hash256::fromSpan( - gsl::make_span(child_root_value.value().get())) - .value() - : trie_serializer_->getEmptyRootHash(); + child_root_value + ? common::Hash256::fromSpan(*child_root_value).value() + : trie_serializer_->getEmptyRootHash(); OUTCOME_TRY(child_batch, trie_storage_->getPersistentBatchAt(child_root_hash)); child_batches_.emplace(root_path, std::move(child_batch)); diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index 20c483b988..63947d2c2d 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -39,9 +39,6 @@ namespace kagome::storage::face { template struct ReadableMap : public ReadableBase { using Key = K; - using Value = V; - using ValueView = std::reference_wrapper; - using ConstValueView = std::reference_wrapper; virtual ~ReadableMap() = default; @@ -50,14 +47,14 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result get(const Key &key) const = 0; + virtual outcome::result> get(const Key &key) const = 0; /** * @brief Get value by key * @param key K * @return V if contains(K) or std::nullopt */ - virtual outcome::result> tryGet( + virtual outcome::result>> tryGet( const Key &key) const = 0; }; diff --git a/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp b/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp index 254268f37f..1a8c18abb6 100644 --- a/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp +++ b/core/storage/trie/impl/ephemeral_trie_batch_impl.cpp @@ -16,12 +16,12 @@ namespace kagome::storage::trie { BOOST_ASSERT(trie_ != nullptr); } - outcome::result EphemeralTrieBatchImpl::get( + outcome::result EphemeralTrieBatchImpl::get( const BufferView &key) const { return trie_->get(key); } - outcome::result> EphemeralTrieBatchImpl::tryGet( + outcome::result> EphemeralTrieBatchImpl::tryGet( const BufferView &key) const { return trie_->tryGet(key); } diff --git a/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp b/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp index aa65cae512..78ce672d51 100644 --- a/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp +++ b/core/storage/trie/impl/ephemeral_trie_batch_impl.hpp @@ -18,8 +18,8 @@ namespace kagome::storage::trie { std::shared_ptr trie); ~EphemeralTrieBatchImpl() override = default; - outcome::result get(const BufferView &key) const override; - outcome::result> tryGet( + outcome::result get(const BufferView &key) const override; + outcome::result> tryGet( const BufferView &key) const override; std::unique_ptr trieCursor() override; outcome::result contains(const BufferView &key) const override; diff --git a/core/storage/trie/impl/persistent_trie_batch_impl.cpp b/core/storage/trie/impl/persistent_trie_batch_impl.cpp index 443092947c..e8a2f017ec 100644 --- a/core/storage/trie/impl/persistent_trie_batch_impl.cpp +++ b/core/storage/trie/impl/persistent_trie_batch_impl.cpp @@ -62,13 +62,13 @@ namespace kagome::storage::trie { return std::make_unique(shared_from_this()); } - outcome::result PersistentTrieBatchImpl::get( + outcome::result PersistentTrieBatchImpl::get( const BufferView &key) const { return trie_->get(key); } - outcome::result> - PersistentTrieBatchImpl::tryGet(const BufferView &key) const { + outcome::result> PersistentTrieBatchImpl::tryGet( + const BufferView &key) const { return trie_->tryGet(key); } diff --git a/core/storage/trie/impl/persistent_trie_batch_impl.hpp b/core/storage/trie/impl/persistent_trie_batch_impl.hpp index 115d628597..73f6f488df 100644 --- a/core/storage/trie/impl/persistent_trie_batch_impl.hpp +++ b/core/storage/trie/impl/persistent_trie_batch_impl.hpp @@ -33,8 +33,8 @@ namespace kagome::storage::trie { outcome::result commit() override; std::unique_ptr batchOnTop() override; - outcome::result get(const BufferView &key) const override; - outcome::result> tryGet( + outcome::result get(const BufferView &key) const override; + outcome::result> tryGet( const BufferView &key) const override; std::unique_ptr trieCursor() override; outcome::result contains(const BufferView &key) const override; diff --git a/core/storage/trie/impl/topper_trie_batch_impl.cpp b/core/storage/trie/impl/topper_trie_batch_impl.cpp index 9d62e65733..227aa718b8 100644 --- a/core/storage/trie/impl/topper_trie_batch_impl.cpp +++ b/core/storage/trie/impl/topper_trie_batch_impl.cpp @@ -28,20 +28,20 @@ namespace kagome::storage::trie { const std::shared_ptr &parent) : parent_(parent) {} - outcome::result TopperTrieBatchImpl::get( + outcome::result TopperTrieBatchImpl::get( const BufferView &key) const { OUTCOME_TRY(opt_value, tryGet(key)); if (opt_value) { - return opt_value.value(); + return std::move(*opt_value); } return TrieError::NO_VALUE; } - outcome::result> - TopperTrieBatchImpl::tryGet(const BufferView &key) const { + outcome::result> TopperTrieBatchImpl::tryGet( + const BufferView &key) const { if (auto it = cache_.find(key); it != cache_.end()) { if (it->second.has_value()) { - return it->second.value(); + return BufferView{it->second.value()}; } return std::nullopt; } diff --git a/core/storage/trie/impl/topper_trie_batch_impl.hpp b/core/storage/trie/impl/topper_trie_batch_impl.hpp index 4e9e1d47c3..7f44c02407 100644 --- a/core/storage/trie/impl/topper_trie_batch_impl.hpp +++ b/core/storage/trie/impl/topper_trie_batch_impl.hpp @@ -24,9 +24,8 @@ namespace kagome::storage::trie { explicit TopperTrieBatchImpl(const std::shared_ptr &parent); - outcome::result get( - const BufferView &key) const override; - outcome::result> tryGet( + outcome::result get(const BufferView &key) const override; + outcome::result> tryGet( const BufferView &key) const override; /** diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp b/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp index 060ffb4abf..58e6473705 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_impl.cpp @@ -452,24 +452,24 @@ namespace kagome::storage::trie { return br; } - outcome::result PolkadotTrieImpl::get( + outcome::result PolkadotTrieImpl::get( const common::BufferView &key) const { OUTCOME_TRY(opt_value, tryGet(key)); if (opt_value.has_value()) { - return opt_value.value(); + return std::move(*opt_value); } return TrieError::NO_VALUE; } - outcome::result> - PolkadotTrieImpl::tryGet(const common::BufferView &key) const { + outcome::result> PolkadotTrieImpl::tryGet( + const common::BufferView &key) const { if (not nodes_->getRoot()) { return std::nullopt; } auto nibbles = KeyNibbles::fromByteBuffer(key); OUTCOME_TRY(node, getNode(nodes_->getRoot(), nibbles)); if (node && node->value) { - return node->value.value(); + return BufferView{node->value.value()}; } return std::nullopt; } diff --git a/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp b/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp index 5fabd53f0f..9602b148f4 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie_impl.hpp @@ -63,10 +63,10 @@ namespace kagome::storage::trie { outcome::result remove(const common::BufferView &key) override; - outcome::result get( + outcome::result get( const common::BufferView &key) const override; - outcome::result> tryGet( + outcome::result> tryGet( const common::BufferView &key) const override; std::unique_ptr trieCursor() override; diff --git a/core/utils/kagome_db_editor.cpp b/core/utils/kagome_db_editor.cpp index d847f4326e..ca31199146 100644 --- a/core/utils/kagome_db_editor.cpp +++ b/core/utils/kagome_db_editor.cpp @@ -148,9 +148,8 @@ void child_storage_root_hashes( if (auto value_res = batch->tryGet(key.value()); value_res.has_value() && value_res.value().has_value()) { auto &value_opt = value_res.value(); - log->trace("Found child root hash {}", value_opt.value().get()); - hashes.insert( - common::Hash256::fromSpan(value_opt.value().get()).value()); + log->trace("Found child root hash {}", *value_opt); + hashes.insert(common::Hash256::fromSpan(*value_opt).value()); } res = cursor->next(); key = cursor->key(); @@ -422,7 +421,7 @@ int db_editor_main(int argc, const char **argv) { count = 0; while (cursor->key().has_value()) { ofs << " - " - << check(batch->get(check(cursor->key()).value())).value().get() + << check(batch->get(check(cursor->key()).value())).value().view() << "\n"; if (not(++count % 50000)) { log->trace("{} values were dumped.", count); diff --git a/core/utils/storage_explorer.cpp b/core/utils/storage_explorer.cpp index 0c48316fef..2a43208e86 100644 --- a/core/utils/storage_explorer.cpp +++ b/core/utils/storage_explorer.cpp @@ -280,7 +280,7 @@ class QueryStateCommand : public Command { } auto &value_opt = value_res.value(); if (value_opt.has_value()) { - std::cout << "Value is " << value_opt->get().toHex() << "\n"; + std::cout << "Value is " << value_opt->view().toHex() << "\n"; } else { std::cout << "No value by given key\n"; } diff --git a/test/core/api/service/child_state/child_state_api_test.cpp b/test/core/api/service/child_state/child_state_api_test.cpp index c1979c66b1..bc99631269 100644 --- a/test/core/api/service/child_state/child_state_api_test.cpp +++ b/test/core/api/service/child_state/child_state_api_test.cpp @@ -74,7 +74,7 @@ namespace kagome::api { auto batch = std::make_unique(); static const auto key = "a"_buf; static const common::Buffer value{"1"_hash256}; - EXPECT_CALL(*batch, get(key.view())) + EXPECT_CALL(*batch, getMock(key.view())) .WillRepeatedly(testing::Return(value)); return batch; })); @@ -83,7 +83,7 @@ namespace kagome::api { auto batch = std::make_unique(); static const auto key = "b"_buf; static const common::Buffer value = "2"_buf; - EXPECT_CALL(*batch, tryGet(key.view())) + EXPECT_CALL(*batch, tryGetMock(key.view())) .WillRepeatedly( testing::Return(std::make_optional(std::cref(value)))); return batch; @@ -102,7 +102,7 @@ namespace kagome::api { auto batch = std::make_unique(); static const auto key = "c"_buf; static const common::Buffer value{"3"_hash256}; - EXPECT_CALL(*batch, get(key.view())) + EXPECT_CALL(*batch, getMock(key.view())) .WillRepeatedly(testing::Return(value)); return batch; })); @@ -111,7 +111,7 @@ namespace kagome::api { auto batch = std::make_unique(); static const auto key = "d"_buf; static const auto value = "4"_buf; - EXPECT_CALL(*batch, tryGet(key.view())) + EXPECT_CALL(*batch, tryGetMock(key.view())) .WillRepeatedly( testing::Return(std::make_optional(std::cref(value)))); return batch; @@ -145,7 +145,7 @@ namespace kagome::api { EXPECT_CALL(*storage_, getEphemeralBatchAt("6789"_hash256)) .WillOnce(testing::Invoke([&](auto &root) { auto batch = std::make_unique(); - EXPECT_CALL(*batch, get(child_storage_key.view())) + EXPECT_CALL(*batch, getMock(child_storage_key.view())) .WillOnce(testing::Return(common::Buffer("2020"_hash256))); return batch; })); @@ -201,7 +201,7 @@ namespace kagome::api { EXPECT_CALL(*storage_, getEphemeralBatchAt("6789"_hash256)) .WillOnce(testing::Invoke([&](auto &root) { auto batch = std::make_unique(); - EXPECT_CALL(*batch, get(child_storage_key.view())) + EXPECT_CALL(*batch, getMock(child_storage_key.view())) .WillOnce(testing::Return(common::Buffer("2020"_hash256))); return batch; })); @@ -255,14 +255,14 @@ namespace kagome::api { .WillOnce(testing::Invoke([&](auto &root) { auto batch = std::make_unique(); static auto v = common::Buffer("2020"_hash256); - EXPECT_CALL(*batch, get(child_storage_key.view())) + EXPECT_CALL(*batch, getMock(child_storage_key.view())) .WillOnce(testing::Return(v)); return batch; })); EXPECT_CALL(*storage_, getEphemeralBatchAt("2020"_hash256)) .WillOnce(testing::Invoke([&](auto &root) { auto batch = std::make_unique(); - EXPECT_CALL(*batch, get(key.view())) + EXPECT_CALL(*batch, getMock(key.view())) .WillOnce(testing::Return(expected_result)); return batch; })); diff --git a/test/core/api/service/state/state_api_test.cpp b/test/core/api/service/state/state_api_test.cpp index bf5f55c236..68fa2452e2 100644 --- a/test/core/api/service/state/state_api_test.cpp +++ b/test/core/api/service/state/state_api_test.cpp @@ -85,7 +85,7 @@ namespace kagome::api { EXPECT_CALL(*storage_, getEphemeralBatchAt(_)) .WillRepeatedly(testing::Invoke([&in_buf, &out_buf](auto &root) { auto batch = std::make_unique(); - EXPECT_CALL(*batch, tryGet(in_buf.view())) + EXPECT_CALL(*batch, tryGetMock(in_buf.view())) .WillRepeatedly(testing::Return(std::cref(out_buf))); return batch; })); @@ -384,7 +384,7 @@ namespace kagome::api { auto batch = std::make_unique(); for (auto &key : keys) { - EXPECT_CALL(*batch, tryGet(key.view())) + EXPECT_CALL(*batch, tryGetMock(key.view())) .WillOnce(testing::Return(common::Buffer(root))); } return batch; @@ -456,7 +456,7 @@ namespace kagome::api { auto batch = std::make_unique(); for (auto &key : keys) { - EXPECT_CALL(*batch, tryGet(key.view())) + EXPECT_CALL(*batch, tryGetMock(key.view())) .WillOnce(testing::Return(common::Buffer(root))); } return batch; diff --git a/test/core/consensus/authority/authority_manager_test.cpp b/test/core/consensus/authority/authority_manager_test.cpp index 80760dbe0d..a9f76122d8 100644 --- a/test/core/consensus/authority/authority_manager_test.cpp +++ b/test/core/consensus/authority/authority_manager_test.cpp @@ -69,7 +69,7 @@ class AuthorityManagerTest : public testing::Test { EXPECT_CALL(*trie_storage, getEphemeralBatchAt(_)) .WillRepeatedly(testing::Invoke([] { auto batch = std::make_unique(); - EXPECT_CALL(*batch, tryGet(_)) + EXPECT_CALL(*batch, tryGetMock(_)) .WillRepeatedly( Return(storage::Buffer::fromHex("0000000000000000").value())); return batch; diff --git a/test/core/host_api/child_storage_extension_test.cpp b/test/core/host_api/child_storage_extension_test.cpp index 0441746279..a9ef5f41e0 100644 --- a/test/core/host_api/child_storage_extension_test.cpp +++ b/test/core/host_api/child_storage_extension_test.cpp @@ -152,11 +152,9 @@ TEST_P(ReadOutcomeParameterizedTest, GetTest) { // 'func' (lambda) auto ¶m = GetParam(); - auto param_ref = kagome::common::map_result_optional( - param, [](auto &v) { return std::cref(v); }); - EXPECT_CALL(*trie_child_storage_batch_, tryGet(key.view())) - .WillOnce(Return(param_ref)); + EXPECT_CALL(*trie_child_storage_batch_, tryGetMock(key.view())) + .WillOnce(Return(param)); // results if (GetParam().has_error()) { @@ -244,13 +242,8 @@ TEST_P(ReadOutcomeParameterizedTest, ReadTest) { // 'func' (lambda) auto ¶m = GetParam(); - EXPECT_CALL(*trie_child_storage_batch_, tryGet(key.view())) - .WillOnce(Return([&]() -> outcome::result> { - if (param.has_error()) return param.as_failure(); - auto &opt = param.value(); - if (opt) return std::cref(opt.value()); - return std::nullopt; - }())); + EXPECT_CALL(*trie_child_storage_batch_, tryGetMock(key.view())) + .WillOnce(Return(param)); // results diff --git a/test/core/host_api/storage_extension_test.cpp b/test/core/host_api/storage_extension_test.cpp index 6536ebbcd2..1f309c2162 100644 --- a/test/core/host_api/storage_extension_test.cpp +++ b/test/core/host_api/storage_extension_test.cpp @@ -317,7 +317,7 @@ TEST_P(OutcomeParameterizedTest, StorageReadTest) { EXPECT_CALL(*memory_, loadN(key.ptr, key.size)).WillOnce(Return(key_data)); EXPECT_CALL(*storage_provider_, getCurrentBatch()) .WillOnce(Return(trie_batch_)); - EXPECT_CALL(*trie_batch_, tryGet(key_data.view())) + EXPECT_CALL(*trie_batch_, tryGetMock(key_data.view())) .WillOnce(Return(value_data)); EXPECT_CALL( *memory_, @@ -367,7 +367,7 @@ TEST_F(StorageExtensionTest, ExtStorageAppendTest) { Buffer vals_encoded; { // @when there is no value by given key in trie - EXPECT_CALL(*trie_batch_, tryGet(key_data.view())) + EXPECT_CALL(*trie_batch_, tryGetMock(key_data.view())) .WillOnce(Return(std::nullopt)); // @then storage is inserted by scale encoded vector containing @@ -383,7 +383,7 @@ TEST_F(StorageExtensionTest, ExtStorageAppendTest) { { // @when there is a value by given key (inserted above) - EXPECT_CALL(*trie_batch_, tryGet(key_data.view())) + EXPECT_CALL(*trie_batch_, tryGetMock(key_data.view())) .WillOnce(Return(vals_encoded)); // @then storage is inserted by scale encoded vector containing two @@ -426,7 +426,7 @@ TEST_F(StorageExtensionTest, ExtStorageAppendTestCompactLenChanged) { { // @when encoded vals is stored by given key - EXPECT_CALL(*trie_batch_, tryGet(key_data.view())) + EXPECT_CALL(*trie_batch_, tryGetMock(key_data.view())) .WillOnce(Return(vals_encoded)); // @when storage is inserted by one more value by the same key @@ -535,7 +535,7 @@ TEST_F(StorageExtensionTest, StorageGetV1Test) { .WillOnce(Return(value_span)); // expect key-value pair was put to db - EXPECT_CALL(*trie_batch_, tryGet(key.view())).WillOnce(Return(value)); + EXPECT_CALL(*trie_batch_, tryGetMock(key.view())).WillOnce(Return(value)); ASSERT_EQ(value_span, storage_extension_->ext_storage_get_version_1(key_span)); @@ -651,7 +651,7 @@ TEST_F(StorageExtensionTest, RootTest) { return cursor; })); - EXPECT_CALL(*trie_batch_, tryGet(current_key.view())) + EXPECT_CALL(*trie_batch_, tryGetMock(current_key.view())) .WillOnce( Return(outcome::success(std::make_optional(std::cref(empty_hash))))); EXPECT_CALL(*trie_batch_, remove(current_key.view())) diff --git a/test/core/runtime/runtime_test_base.hpp b/test/core/runtime/runtime_test_base.hpp index 7750fe237e..9f1080a62e 100644 --- a/test/core/runtime/runtime_test_base.hpp +++ b/test/core/runtime/runtime_test_base.hpp @@ -67,11 +67,9 @@ class RuntimeTestBase : public ::testing::Test { using Digest = primitives::Digest; using PersistentTrieBatchMock = storage::trie::PersistentTrieBatchMock; using EphemeralTrieBatchMock = storage::trie::EphemeralTrieBatchMock; - using TopperTrieBatchMock = storage::trie::TopperTrieBatchMock; void initStorage() { using storage::trie::PersistentTrieBatch; - using storage::trie::TopperTrieBatchMock; auto random_generator = std::make_shared(); auto sr25519_provider = @@ -192,7 +190,7 @@ class RuntimeTestBase : public ::testing::Test { template void prepareStorageBatchExpectations(BatchMock &batch) { - ON_CALL(batch, get(_)).WillByDefault(testing::Invoke([](auto &key) { + ON_CALL(batch, getMock(_)).WillByDefault(testing::Invoke([](auto &key) { static common::Buffer buf; return std::cref(buf); })); @@ -209,7 +207,7 @@ class RuntimeTestBase : public ::testing::Test { return cursor; })); static auto heappages_key = ":heappages"_buf; - EXPECT_CALL(batch, get(heappages_key.view())); + EXPECT_CALL(batch, getMock(heappages_key.view())); } primitives::BlockHeader createBlockHeader(primitives::BlockHash const &hash, diff --git a/test/core/runtime/storage_code_provider_test.cpp b/test/core/runtime/storage_code_provider_test.cpp index b6f8fdc914..c053ec8c04 100644 --- a/test/core/runtime/storage_code_provider_test.cpp +++ b/test/core/runtime/storage_code_provider_test.cpp @@ -53,7 +53,8 @@ TEST_F(StorageCodeProviderTest, GetCodeWhenNoStorageUpdates) { EXPECT_CALL(*trie_db, getEphemeralBatchAt(first_state_root)) .WillOnce(Invoke([this]() { auto batch = std::make_unique(); - EXPECT_CALL(*batch, get(common::BufferView{storage::kRuntimeCodeKey})) + EXPECT_CALL(*batch, + getMock(common::BufferView{storage::kRuntimeCodeKey})) .WillOnce(Return(state_code_)); return batch; })); @@ -94,7 +95,8 @@ TEST_F(StorageCodeProviderTest, DISABLED_GetCodeWhenStorageUpdates) { EXPECT_CALL(*trie_db, getEphemeralBatchAt(first_state_root)) .WillOnce(Invoke([this]() { auto batch = std::make_unique(); - EXPECT_CALL(*batch, get(common::BufferView{storage::kRuntimeCodeKey})) + EXPECT_CALL(*batch, + getMock(common::BufferView{storage::kRuntimeCodeKey})) .WillOnce(Return(state_code_)); return batch; })); @@ -108,7 +110,8 @@ TEST_F(StorageCodeProviderTest, DISABLED_GetCodeWhenStorageUpdates) { EXPECT_CALL(*trie_db, getEphemeralBatchAt(second_state_root)) .WillOnce(Invoke([&new_state_code](auto &) { auto batch = std::make_unique(); - EXPECT_CALL(*batch, get(common::BufferView{storage::kRuntimeCodeKey})) + EXPECT_CALL(*batch, + getMock(common::BufferView{storage::kRuntimeCodeKey})) .WillOnce(Return(new_state_code)); return batch; })); diff --git a/test/core/runtime/trie_storage_provider_test.cpp b/test/core/runtime/trie_storage_provider_test.cpp index 96a67b817e..90fa8eea73 100644 --- a/test/core/runtime/trie_storage_provider_test.cpp +++ b/test/core/runtime/trie_storage_provider_test.cpp @@ -84,7 +84,7 @@ TEST_F(TrieStorageProviderTest, NestedTransactions) { ASSERT_OUTCOME_SUCCESS( val, batch->get(Buffer(std::vector(key.begin(), key.end())))); - actual_view += val.get().asString(); + actual_view += val.mut().asString(); } EXPECT_EQ(actual_view, expected_view); }; diff --git a/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp b/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp index 7677fd791b..405a159467 100644 --- a/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp +++ b/test/core/storage/trie/polkadot_trie/polkadot_trie_test.cpp @@ -88,7 +88,7 @@ TEST_P(TrieTest, RunCommand) { case Command::GET: { if (command.value) { ASSERT_OUTCOME_SUCCESS(val, trie->get(command.key)); - ASSERT_EQ(val.get(), command.value.value()); + ASSERT_EQ(val, command.value.value()); } else { EXPECT_OUTCOME_FALSE(err, trie->get(command.key)); ASSERT_EQ( @@ -268,18 +268,18 @@ TEST_F(TrieTest, Put) { for (auto &entry : data) { ASSERT_OUTCOME_SUCCESS(res, trie->get(entry.first)); - ASSERT_EQ(res.get(), entry.second); + ASSERT_EQ(res, entry.second); } ASSERT_OUTCOME_SUCCESS_TRY(trie->put("102030"_hex2buf, "0a0b0c"_hex2buf)); ASSERT_OUTCOME_SUCCESS_TRY(trie->put("104050"_hex2buf, "0a0b0c"_hex2buf)); ASSERT_OUTCOME_SUCCESS_TRY(trie->put("102030"_hex2buf, "010203"_hex2buf)); ASSERT_OUTCOME_SUCCESS(v1, trie->get("102030"_hex2buf)); - ASSERT_EQ(v1.get(), "010203"_hex2buf); + ASSERT_EQ(v1, "010203"_hex2buf); ASSERT_OUTCOME_SUCCESS(v2, trie->get("104050"_hex2buf)); - ASSERT_EQ(v2.get(), "0a0b0c"_hex2buf); + ASSERT_EQ(v2, "0a0b0c"_hex2buf); ASSERT_OUTCOME_SUCCESS_TRY(trie->put("1332"_hex2buf, ""_buf)); ASSERT_OUTCOME_SUCCESS(v3, trie->get("1332"_hex2buf)); - ASSERT_EQ(v3.get(), ""_buf); + ASSERT_EQ(v3, ""_buf); } /** @@ -312,7 +312,7 @@ TEST_F(TrieTest, Replace) { ASSERT_OUTCOME_SUCCESS_TRY( trie->put(data[1].first, BufferView{data[3].second})); ASSERT_OUTCOME_SUCCESS(res, trie->get(data[1].first)); - ASSERT_EQ(res.get(), data[3].second); + ASSERT_EQ(res, data[3].second); } /** diff --git a/test/core/storage/trie/trie_storage/trie_batch_test.cpp b/test/core/storage/trie/trie_storage/trie_batch_test.cpp index 219d2119c5..85bb555e48 100644 --- a/test/core/storage/trie/trie_storage/trie_batch_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_batch_test.cpp @@ -122,7 +122,7 @@ TEST_F(TrieBatchTest, Put) { new_batch = trie->getEphemeralBatchAt(root_hash).value(); for (auto &entry : data) { ASSERT_OUTCOME_SUCCESS(res, new_batch->get(entry.first)); - ASSERT_EQ(res.get(), entry.second); + ASSERT_EQ(res, entry.second); } ASSERT_OUTCOME_SUCCESS_TRY( @@ -130,9 +130,9 @@ TEST_F(TrieBatchTest, Put) { ASSERT_OUTCOME_SUCCESS_TRY( new_batch->put("104050"_hex2buf, "0a0b0c"_hex2buf)); ASSERT_OUTCOME_SUCCESS(v1, new_batch->get("102030"_hex2buf)); - ASSERT_EQ(v1.get(), "010203"_hex2buf); + ASSERT_EQ(v1, "010203"_hex2buf); ASSERT_OUTCOME_SUCCESS(v2, new_batch->get("104050"_hex2buf)); - ASSERT_EQ(v2.get(), "0a0b0c"_hex2buf); + ASSERT_EQ(v2, "0a0b0c"_hex2buf); } /** @@ -171,7 +171,7 @@ TEST_F(TrieBatchTest, Replace) { ASSERT_OUTCOME_SUCCESS(root_hash, batch->commit()); auto read_batch = trie->getEphemeralBatchAt(root_hash).value(); ASSERT_OUTCOME_SUCCESS(res, read_batch->get(data[1].first)); - ASSERT_EQ(res.get(), data[3].second); + ASSERT_EQ(res, data[3].second); } /** diff --git a/test/core/storage/trie/trie_storage/trie_storage_test.cpp b/test/core/storage/trie/trie_storage/trie_storage_test.cpp index c93a0c7c8f..9038dd710a 100644 --- a/test/core/storage/trie/trie_storage/trie_storage_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_storage_test.cpp @@ -80,11 +80,11 @@ TEST(TriePersistencyTest, CreateDestroyCreate) { .value(); auto batch = storage->getPersistentBatchAt(root).value(); EXPECT_OUTCOME_TRUE(v1, batch->get("123"_buf)); - ASSERT_EQ(v1.get(), "abc"_buf); + ASSERT_EQ(v1, "abc"_buf); EXPECT_OUTCOME_TRUE(v2, batch->get("345"_buf)); - ASSERT_EQ(v2.get(), "def"_buf); + ASSERT_EQ(v2, "def"_buf); EXPECT_OUTCOME_TRUE(v3, batch->get("678"_buf)); - ASSERT_EQ(v3.get(), "xyz"_buf); + ASSERT_EQ(v3, "xyz"_buf); boost::filesystem::remove_all("/tmp/kagome_rocksdb_persistency_test"); } diff --git a/test/mock/core/storage/trie/trie_batches_mock.hpp b/test/mock/core/storage/trie/trie_batches_mock.hpp index 7ca72c70ab..658503dea2 100644 --- a/test/mock/core/storage/trie/trie_batches_mock.hpp +++ b/test/mock/core/storage/trie/trie_batches_mock.hpp @@ -14,15 +14,29 @@ namespace kagome::storage::trie { class PersistentTrieBatchMock : public PersistentTrieBatch { public: - MOCK_METHOD(outcome::result, - get, + MOCK_METHOD(outcome::result, + getMock, (const common::BufferView &), - (const, override)); + (const)); - MOCK_METHOD(outcome::result>, - tryGet, + MOCK_METHOD(outcome::result>, + tryGetMock, (const common::BufferView &), - (const, override)); + (const)); + + outcome::result get(const BufferView &key) const override { + OUTCOME_TRY(value, getMock(key)); + return std::move(value); + } + + outcome::result> tryGet( + const BufferView &key) const override { + OUTCOME_TRY(value, tryGetMock(key)); + if (value) { + return std::move(*value); + } + return std::nullopt; + } MOCK_METHOD(std::unique_ptr, trieCursor, @@ -64,15 +78,29 @@ namespace kagome::storage::trie { class EphemeralTrieBatchMock : public EphemeralTrieBatch { public: - MOCK_METHOD(outcome::result, - get, + MOCK_METHOD(outcome::result, + getMock, (const common::BufferView &), - (const, override)); + (const)); - MOCK_METHOD(outcome::result>, - tryGet, + MOCK_METHOD(outcome::result>, + tryGetMock, (const common::BufferView &), - (const, override)); + (const)); + + outcome::result get(const BufferView &key) const override { + OUTCOME_TRY(value, getMock(key)); + return std::move(value); + } + + outcome::result> tryGet( + const BufferView &key) const override { + OUTCOME_TRY(value, tryGetMock(key)); + if (value) { + return std::move(*value); + } + return std::nullopt; + } MOCK_METHOD(std::unique_ptr, trieCursor, @@ -106,47 +134,6 @@ namespace kagome::storage::trie { MOCK_METHOD(outcome::result, hash, (), (override)); }; - - class TopperTrieBatchMock : public TopperTrieBatch { - public: - MOCK_METHOD(outcome::result, writeBack, (), (override)); - - MOCK_METHOD(outcome::result, - get, - (const common::BufferView &), - (const, override)); - - MOCK_METHOD(outcome::result, - contains, - (const common::BufferView &), - (const, override)); - - MOCK_METHOD(bool, empty, (), (const, override)); - - MOCK_METHOD(outcome::result, - put, - (const common::BufferView &, const Buffer &)); - outcome::result put(const common::BufferView &k, - BufferOrView &&v) override { - return put(k, v.mut()); - } - - MOCK_METHOD(outcome::result, - remove, - (const common::BufferView &), - (override)); - - MOCK_METHOD(std::unique_ptr, - trieCursor, - (), - (override)); - - MOCK_METHOD((outcome::result>), - clearPrefix, - (const common::BufferView &buf, std::optional limit), - (override)); - }; - } // namespace kagome::storage::trie #endif // KAGOME_TEST_MOCK_CORE_STORAGE_TRIE_TRIE_BATCHES_MOCK From a2c284fe8cd2f1d11e49c73019eddb050b5b5c0a Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 19:26:11 +0300 Subject: [PATCH 08/19] equality Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 35 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index 9e89314025..6b72f1eb89 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -6,6 +6,7 @@ #ifndef KAGOME_COMMON_BUFFER_OR_VIEW_HPP #define KAGOME_COMMON_BUFFER_OR_VIEW_HPP +#include #include #include "common/buffer.hpp" @@ -13,6 +14,8 @@ namespace kagome::common { class BufferOrView { using Span = gsl::span; + template + using AsSpan = std::enable_if_t>; public: BufferOrView() = default; @@ -67,29 +70,21 @@ namespace kagome::common { private: std::variant variant; - friend bool operator==(const BufferOrView &l, const Span &r) { - return l.view() == r; + template > + friend bool operator==(const BufferOrView &l, const T &r) { + return l.view() == Span{r}; } - friend bool operator!=(const BufferOrView &l, const Span &r) { - return l.view() != r; + template > + friend bool operator!=(const BufferOrView &l, const T &r) { + return l.view() != Span{r}; } - friend bool operator==(const Span &l, const BufferOrView &r) { - return l == r.view(); + template > + friend bool operator==(const T &l, const BufferOrView &r) { + return Span{l} == r.view(); } - friend bool operator!=(const Span &l, const BufferOrView &r) { - return l != r.view(); - } - friend bool operator==(const BufferOrView &l, const Buffer &r) { - return l.view() == BufferView{r}; - } - friend bool operator!=(const BufferOrView &l, const Buffer &r) { - return l.view() != BufferView{r}; - } - friend bool operator==(const Buffer &l, const BufferOrView &r) { - return BufferView{l} == r.view(); - } - friend bool operator!=(const Buffer &l, const BufferOrView &r) { - return BufferView{l} != r.view(); + template > + friend bool operator!=(const T &l, const BufferOrView &r) { + return Span{l} != r.view(); } }; } // namespace kagome::common From 4557590742ff3f469c4179c9f81e865e064fb339 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 19:26:17 +0300 Subject: [PATCH 09/19] moved Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index 6b72f1eb89..c0bdf00d77 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -17,6 +17,8 @@ namespace kagome::common { template using AsSpan = std::enable_if_t>; + struct Moved {}; + public: BufferOrView() = default; @@ -33,6 +35,10 @@ namespace kagome::common { BufferOrView &operator=(BufferOrView &&) = default; bool owned() const { + if (variant.index() == 2) { + // moved with `.into()` + abort(); + } return variant.index() == 1; } @@ -63,12 +69,12 @@ namespace kagome::common { // move buffer away, copy once if view Buffer into() { auto buffer = std::move(mut()); - variant.emplace(); + variant.emplace(); return buffer; } private: - std::variant variant; + std::variant variant; template > friend bool operator==(const BufferOrView &l, const T &r) { From ea3d9e7a66131bde600763750e6c6966a74be0dd Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 19:50:41 +0300 Subject: [PATCH 10/19] rename trait Signed-off-by: turuslan --- core/storage/buffer_map_types.hpp | 2 +- core/storage/face/map_cursor.hpp | 2 +- core/storage/face/owned_or_view.hpp | 4 ++-- core/storage/face/readable.hpp | 8 ++++---- core/storage/face/writeable.hpp | 3 +-- test/mock/core/storage/persistent_map_mock.hpp | 6 +++--- test/mock/core/storage/write_batch_mock.hpp | 2 +- 7 files changed, 13 insertions(+), 14 deletions(-) diff --git a/core/storage/buffer_map_types.hpp b/core/storage/buffer_map_types.hpp index 41905b3dd1..1cbb5f08e0 100644 --- a/core/storage/buffer_map_types.hpp +++ b/core/storage/buffer_map_types.hpp @@ -19,7 +19,7 @@ namespace kagome::storage::face { template <> - struct OwnedOrView { + struct OwnedOrViewTrait { using type = common::BufferOrView; }; } // namespace kagome::storage::face diff --git a/core/storage/face/map_cursor.hpp b/core/storage/face/map_cursor.hpp index a1b88ad5e1..e3683d3595 100644 --- a/core/storage/face/map_cursor.hpp +++ b/core/storage/face/map_cursor.hpp @@ -63,7 +63,7 @@ namespace kagome::storage::face { * @brief Getter for value of the element currently pointed at. * @return value if isValid() */ - virtual std::optional> value() const = 0; + virtual std::optional> value() const = 0; }; } // namespace kagome::storage::face diff --git a/core/storage/face/owned_or_view.hpp b/core/storage/face/owned_or_view.hpp index cf1652eff9..558b7e8a26 100644 --- a/core/storage/face/owned_or_view.hpp +++ b/core/storage/face/owned_or_view.hpp @@ -8,10 +8,10 @@ namespace kagome::storage::face { template - struct OwnedOrView; + struct OwnedOrViewTrait; template - using OwnedOrViewOf = typename OwnedOrView::type; + using OwnedOrView = typename OwnedOrViewTrait::type; } // namespace kagome::storage::face #endif // KAGOME_STORAGE_FACE_OWNED_OR_VIEW_HPP diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index 63947d2c2d..d594c53916 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -47,14 +47,14 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result> get(const Key &key) const = 0; + virtual outcome::result> get(const Key &key) const = 0; /** * @brief Get value by key * @param key K * @return V if contains(K) or std::nullopt */ - virtual outcome::result>> tryGet( + virtual outcome::result>> tryGet( const Key &key) const = 0; }; @@ -69,14 +69,14 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result> load(const Key &key) const = 0; + virtual outcome::result> load(const Key &key) const = 0; /** * @brief Load value by key * @param key K * @return V if contains(K) or std::nullopt */ - virtual outcome::result>> tryLoad( + virtual outcome::result>> tryLoad( const Key &key) const = 0; }; diff --git a/core/storage/face/writeable.hpp b/core/storage/face/writeable.hpp index fc9dbf4233..3b333ed252 100644 --- a/core/storage/face/writeable.hpp +++ b/core/storage/face/writeable.hpp @@ -27,8 +27,7 @@ namespace kagome::storage::face { * @param value value * @return result containing void if put successful, error otherwise */ - virtual outcome::result put(const K &key, - OwnedOrViewOf &&value) = 0; + virtual outcome::result put(const K &key, OwnedOrView &&value) = 0; /** * @brief Remove value by key diff --git a/test/mock/core/storage/persistent_map_mock.hpp b/test/mock/core/storage/persistent_map_mock.hpp index 1e75058396..0cc9453d22 100644 --- a/test/mock/core/storage/persistent_map_mock.hpp +++ b/test/mock/core/storage/persistent_map_mock.hpp @@ -27,12 +27,12 @@ namespace kagome::storage::face { (const KView &), (const)); - outcome::result> load(const KView &key) const override { + outcome::result> load(const KView &key) const override { OUTCOME_TRY(value, loadMock(key)); return std::move(value); } - outcome::result>> tryLoad( + outcome::result>> tryLoad( const KView &key) const override { OUTCOME_TRY(value, tryLoadMock(key)); if (value) { @@ -46,7 +46,7 @@ namespace kagome::storage::face { MOCK_CONST_METHOD0_T(empty, bool()); MOCK_METHOD(outcome::result, put, (const KView &, const V &)); - outcome::result put(const KView &k, OwnedOrViewOf &&v) override { + outcome::result put(const KView &k, OwnedOrView &&v) override { return put(k, v.mut()); } diff --git a/test/mock/core/storage/write_batch_mock.hpp b/test/mock/core/storage/write_batch_mock.hpp index 7161c0d47d..f6ebc454fb 100644 --- a/test/mock/core/storage/write_batch_mock.hpp +++ b/test/mock/core/storage/write_batch_mock.hpp @@ -20,7 +20,7 @@ namespace kagome::storage::face { MOCK_METHOD(void, clear, (), (override)); MOCK_METHOD2_T(put, outcome::result(const K &key, const V &value)); - outcome::result put(const K &key, OwnedOrViewOf &&value) override { + outcome::result put(const K &key, OwnedOrView &&value) override { return put(key, value.mut()); } From 21fc6367830859cd42fa100913108c79674651df Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 20:00:07 +0300 Subject: [PATCH 11/19] load -> get Signed-off-by: turuslan --- core/blockchain/impl/block_storage_impl.cpp | 4 ++-- core/blockchain/impl/common.cpp | 2 +- core/blockchain/impl/storage_util.cpp | 2 +- .../babe/impl/babe_config_repository_impl.cpp | 4 ++-- core/consensus/babe/impl/consistency_keeper_impl.cpp | 2 +- .../grandpa/impl/authority_manager_impl.cpp | 4 ++-- core/network/impl/peer_manager_impl.cpp | 2 +- core/network/impl/synchronizer_impl.cpp | 2 +- core/offchain/impl/offchain_local_storage.cpp | 4 ++-- core/offchain/impl/offchain_persistent_storage.cpp | 4 ++-- core/runtime/common/runtime_upgrade_tracker_impl.cpp | 3 +-- core/storage/face/readable.hpp | 4 ++-- core/storage/in_memory/in_memory_storage.cpp | 4 ++-- core/storage/in_memory/in_memory_storage.hpp | 4 ++-- core/storage/rocksdb/rocksdb.cpp | 4 ++-- core/storage/rocksdb/rocksdb.hpp | 4 ++-- core/storage/trie/impl/trie_storage_backend_impl.cpp | 8 ++++---- core/storage/trie/impl/trie_storage_backend_impl.hpp | 4 ++-- .../trie/serialization/trie_serializer_impl.cpp | 2 +- test/core/blockchain/block_storage_test.cpp | 12 ++++++------ .../consensus/babe/babe_config_repository_test.cpp | 2 +- .../storage/rocksdb/rocksdb_integration_test.cpp | 6 +++--- .../trie/trie_storage/trie_storage_backend_test.cpp | 4 ++-- test/mock/core/storage/persistent_map_mock.hpp | 12 ++++++------ 24 files changed, 51 insertions(+), 52 deletions(-) diff --git a/core/blockchain/impl/block_storage_impl.cpp b/core/blockchain/impl/block_storage_impl.cpp index 02271a9749..d94484322a 100644 --- a/core/blockchain/impl/block_storage_impl.cpp +++ b/core/blockchain/impl/block_storage_impl.cpp @@ -254,7 +254,7 @@ namespace kagome::blockchain { auto num_to_idx_key = prependPrefix(numberToIndexKey(block.number), Prefix::ID_TO_LOOKUP_KEY); - OUTCOME_TRY(num_to_idx_val_opt, storage_->tryLoad(num_to_idx_key.view())); + OUTCOME_TRY(num_to_idx_val_opt, storage_->tryGet(num_to_idx_key.view())); if (num_to_idx_val_opt == block_lookup_key) { if (auto res = storage_->remove(num_to_idx_key); res.has_error()) { SL_ERROR(logger_, @@ -299,7 +299,7 @@ namespace kagome::blockchain { } OUTCOME_TRY(leaves_opt, - storage_->tryLoad(storage::kBlockTreeLeavesLookupKey)); + storage_->tryGet(storage::kBlockTreeLeavesLookupKey)); if (not leaves_opt.has_value()) { return BlockStorageError::BLOCK_TREE_LEAVES_NOT_FOUND; } diff --git a/core/blockchain/impl/common.cpp b/core/blockchain/impl/common.cpp index 7be4221f7c..299ab400b2 100644 --- a/core/blockchain/impl/common.cpp +++ b/core/blockchain/impl/common.cpp @@ -24,7 +24,7 @@ namespace kagome::blockchain { return prependPrefix(hash, prefix::Prefix::ID_TO_LOOKUP_KEY); }); - OUTCOME_TRY(key_opt, map.tryLoad(key)); + OUTCOME_TRY(key_opt, map.tryGet(key)); return std::move(key_opt); } diff --git a/core/blockchain/impl/storage_util.cpp b/core/blockchain/impl/storage_util.cpp index 2a4e9ab1b7..26713bdf51 100644 --- a/core/blockchain/impl/storage_util.cpp +++ b/core/blockchain/impl/storage_util.cpp @@ -61,7 +61,7 @@ namespace kagome::blockchain { const primitives::BlockId &block_id) { OUTCOME_TRY(key, idToLookupKey(map, block_id)); if (!key.has_value()) return std::nullopt; - return map.tryLoad(prependPrefix(key.value(), prefix)); + return map.tryGet(prependPrefix(key.value(), prefix)); } common::Buffer numberToIndexKey(primitives::BlockNumber n) { diff --git a/core/consensus/babe/impl/babe_config_repository_impl.cpp b/core/consensus/babe/impl/babe_config_repository_impl.cpp index 3d5cf58b67..4ab9d5c344 100644 --- a/core/consensus/babe/impl/babe_config_repository_impl.cpp +++ b/core/consensus/babe/impl/babe_config_repository_impl.cpp @@ -103,7 +103,7 @@ namespace kagome::consensus::babe { // 1. Load last state OUTCOME_TRY(encoded_last_state_opt, - persistent_storage_->tryLoad( + persistent_storage_->tryGet( storage::kBabeConfigRepoStateLookupKey("last"))); if (encoded_last_state_opt.has_value()) { @@ -138,7 +138,7 @@ namespace kagome::consensus::babe { block_number > 0; block_number -= kSavepointBlockInterval) { OUTCOME_TRY(encoded_saved_state_opt, - persistent_storage_->tryLoad( + persistent_storage_->tryGet( storage::kBabeConfigRepoStateLookupKey(block_number))); if (not encoded_saved_state_opt.has_value()) { diff --git a/core/consensus/babe/impl/consistency_keeper_impl.cpp b/core/consensus/babe/impl/consistency_keeper_impl.cpp index 5e77c25a21..05d174c9d6 100644 --- a/core/consensus/babe/impl/consistency_keeper_impl.cpp +++ b/core/consensus/babe/impl/consistency_keeper_impl.cpp @@ -32,7 +32,7 @@ namespace kagome::consensus::babe { bool ConsistencyKeeperImpl::prepare() { // try to get record - auto buf_opt_res = storage_->tryLoad(storage::kApplyingBlockInfoLookupKey); + auto buf_opt_res = storage_->tryGet(storage::kApplyingBlockInfoLookupKey); if (buf_opt_res.has_error()) { SL_WARN(logger_, "Can't check existence of partial applied block", diff --git a/core/consensus/grandpa/impl/authority_manager_impl.cpp b/core/consensus/grandpa/impl/authority_manager_impl.cpp index b9e3d537cf..cc1fafbe9c 100644 --- a/core/consensus/grandpa/impl/authority_manager_impl.cpp +++ b/core/consensus/grandpa/impl/authority_manager_impl.cpp @@ -105,7 +105,7 @@ namespace kagome::consensus::grandpa { // 1. Load last state OUTCOME_TRY(encoded_last_state_opt, - persistent_storage_->tryLoad( + persistent_storage_->tryGet( storage::kAuthorityManagerStateLookupKey("last"))); if (encoded_last_state_opt.has_value()) { @@ -142,7 +142,7 @@ namespace kagome::consensus::grandpa { block_number -= kSavepointBlockInterval) { OUTCOME_TRY( encoded_saved_state_opt, - persistent_storage_->tryLoad( + persistent_storage_->tryGet( storage::kAuthorityManagerStateLookupKey(block_number))); if (not encoded_saved_state_opt.has_value()) { diff --git a/core/network/impl/peer_manager_impl.cpp b/core/network/impl/peer_manager_impl.cpp index 79d6b7083f..4c3510a6a6 100644 --- a/core/network/impl/peer_manager_impl.cpp +++ b/core/network/impl/peer_manager_impl.cpp @@ -745,7 +745,7 @@ namespace kagome::network { std::vector PeerManagerImpl::loadLastActivePeers() { - auto get_res = storage_->load(storage::kActivePeersKey); + auto get_res = storage_->get(storage::kActivePeersKey); if (not get_res) { SL_ERROR(log_, "List of last active peers cannot be obtained from storage. " diff --git a/core/network/impl/synchronizer_impl.cpp b/core/network/impl/synchronizer_impl.cpp index bb8f68ac6d..d5cc96de06 100644 --- a/core/network/impl/synchronizer_impl.cpp +++ b/core/network/impl/synchronizer_impl.cpp @@ -117,7 +117,7 @@ namespace kagome::network { /** @see AppStateManager::takeControl */ bool SynchronizerImpl::prepare() { auto opt_res = - buffer_storage_->tryLoad(storage::kBlockOfIncompleteSyncStateLookupKey); + buffer_storage_->tryGet(storage::kBlockOfIncompleteSyncStateLookupKey); if (opt_res.has_error()) { SL_ERROR( log_, "Can't check of incomplete state sync: {}", opt_res.error()); diff --git a/core/offchain/impl/offchain_local_storage.cpp b/core/offchain/impl/offchain_local_storage.cpp index 7489cfa3a5..b166fc81ea 100644 --- a/core/offchain/impl/offchain_local_storage.cpp +++ b/core/offchain/impl/offchain_local_storage.cpp @@ -64,7 +64,7 @@ namespace kagome::offchain { auto iKey = internalKey(key); std::lock_guard lg(mutex_); - OUTCOME_TRY(get_opt, storage_->tryLoad(iKey)); + OUTCOME_TRY(get_opt, storage_->tryGet(iKey)); std::optional existing; if (get_opt.has_value()) { @@ -87,7 +87,7 @@ namespace kagome::offchain { throw std::invalid_argument("Off-chain local storage is unavailable yet"); auto iKey = internalKey(key); - OUTCOME_TRY(value, storage_->load(iKey)); + OUTCOME_TRY(value, storage_->get(iKey)); return value.into(); } } // namespace kagome::offchain diff --git a/core/offchain/impl/offchain_persistent_storage.cpp b/core/offchain/impl/offchain_persistent_storage.cpp index eff1939e55..3eb3f171b5 100644 --- a/core/offchain/impl/offchain_persistent_storage.cpp +++ b/core/offchain/impl/offchain_persistent_storage.cpp @@ -47,7 +47,7 @@ namespace kagome::offchain { common::Buffer value) { auto iKey = internalKey(key); std::lock_guard lg(mutex_); - OUTCOME_TRY(get_opt, storage_->tryLoad(iKey)); + OUTCOME_TRY(get_opt, storage_->tryGet(iKey)); std::optional existing; if (get_opt.has_value()) { @@ -66,7 +66,7 @@ namespace kagome::offchain { outcome::result OffchainPersistentStorageImpl::get( const common::BufferView &key) { auto iKey = internalKey(key); - OUTCOME_TRY(value, storage_->load(iKey)); + OUTCOME_TRY(value, storage_->get(iKey)); return value.into(); } diff --git a/core/runtime/common/runtime_upgrade_tracker_impl.cpp b/core/runtime/common/runtime_upgrade_tracker_impl.cpp index 60508c050c..3c518ec606 100644 --- a/core/runtime/common/runtime_upgrade_tracker_impl.cpp +++ b/core/runtime/common/runtime_upgrade_tracker_impl.cpp @@ -25,8 +25,7 @@ namespace kagome::runtime { BOOST_ASSERT(code_substitutes); BOOST_ASSERT(block_storage); - OUTCOME_TRY(encoded_opt, - storage->tryLoad(storage::kRuntimeHashesLookupKey)); + OUTCOME_TRY(encoded_opt, storage->tryGet(storage::kRuntimeHashesLookupKey)); std::vector saved_data{}; if (encoded_opt.has_value()) { diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index d594c53916..97f0ae2af4 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -69,14 +69,14 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result> load(const Key &key) const = 0; + virtual outcome::result> get(const Key &key) const = 0; /** * @brief Load value by key * @param key K * @return V if contains(K) or std::nullopt */ - virtual outcome::result>> tryLoad( + virtual outcome::result>> tryGet( const Key &key) const = 0; }; diff --git a/core/storage/in_memory/in_memory_storage.cpp b/core/storage/in_memory/in_memory_storage.cpp index eb84cc1a4b..b9b67cf7d3 100644 --- a/core/storage/in_memory/in_memory_storage.cpp +++ b/core/storage/in_memory/in_memory_storage.cpp @@ -12,7 +12,7 @@ using kagome::common::Buffer; namespace kagome::storage { - outcome::result InMemoryStorage::load( + outcome::result InMemoryStorage::get( const BufferView &key) const { if (storage.find(key.toHex()) != storage.end()) { return BufferView{storage.at(key.toHex())}; @@ -21,7 +21,7 @@ namespace kagome::storage { return DatabaseError::NOT_FOUND; } - outcome::result> InMemoryStorage::tryLoad( + outcome::result> InMemoryStorage::tryGet( const common::BufferView &key) const { if (storage.find(key.toHex()) != storage.end()) { return BufferView{storage.at(key.toHex())}; diff --git a/core/storage/in_memory/in_memory_storage.hpp b/core/storage/in_memory/in_memory_storage.hpp index d978890cda..4d7082a8c6 100644 --- a/core/storage/in_memory/in_memory_storage.hpp +++ b/core/storage/in_memory/in_memory_storage.hpp @@ -23,10 +23,10 @@ namespace kagome::storage { public: ~InMemoryStorage() override = default; - outcome::result load( + outcome::result get( const common::BufferView &key) const override; - outcome::result> tryLoad( + outcome::result> tryGet( const common::BufferView &key) const override; outcome::result put(const common::BufferView &key, diff --git a/core/storage/rocksdb/rocksdb.cpp b/core/storage/rocksdb/rocksdb.cpp index b44bab2c91..f3359f66e3 100644 --- a/core/storage/rocksdb/rocksdb.cpp +++ b/core/storage/rocksdb/rocksdb.cpp @@ -123,7 +123,7 @@ namespace kagome::storage { return it->Valid(); } - outcome::result RocksDB::load(const BufferView &key) const { + outcome::result RocksDB::get(const BufferView &key) const { std::string value; auto status = db_->Get(ro_, make_slice(key), &value); if (status.ok()) { @@ -135,7 +135,7 @@ namespace kagome::storage { return status_as_error(status); } - outcome::result> RocksDB::tryLoad( + outcome::result> RocksDB::tryGet( const BufferView &key) const { std::string value; auto status = db_->Get(ro_, make_slice(key), &value); diff --git a/core/storage/rocksdb/rocksdb.hpp b/core/storage/rocksdb/rocksdb.hpp index 6d3aa3eaf9..0291366550 100644 --- a/core/storage/rocksdb/rocksdb.hpp +++ b/core/storage/rocksdb/rocksdb.hpp @@ -42,9 +42,9 @@ namespace kagome::storage { bool empty() const override; - outcome::result load(const Key &key) const override; + outcome::result get(const Key &key) const override; - outcome::result> tryLoad( + outcome::result> tryGet( const Key &key) const override; outcome::result put(const BufferView &key, diff --git a/core/storage/trie/impl/trie_storage_backend_impl.cpp b/core/storage/trie/impl/trie_storage_backend_impl.cpp index 6ec8a27cff..4d1dd409c9 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.cpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.cpp @@ -28,14 +28,14 @@ namespace kagome::storage::trie { node_prefix_); } - outcome::result TrieStorageBackendImpl::load( + outcome::result TrieStorageBackendImpl::get( const BufferView &key) const { - return storage_->load(prefixKey(key)); + return storage_->get(prefixKey(key)); } - outcome::result> TrieStorageBackendImpl::tryLoad( + outcome::result> TrieStorageBackendImpl::tryGet( const BufferView &key) const { - return storage_->tryLoad(prefixKey(key)); + return storage_->tryGet(prefixKey(key)); } outcome::result TrieStorageBackendImpl::contains( diff --git a/core/storage/trie/impl/trie_storage_backend_impl.hpp b/core/storage/trie/impl/trie_storage_backend_impl.hpp index abf6b88967..77ad7499b2 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.hpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.hpp @@ -22,8 +22,8 @@ namespace kagome::storage::trie { std::unique_ptr cursor() override; std::unique_ptr batch() override; - outcome::result load(const BufferView &key) const override; - outcome::result> tryLoad( + outcome::result get(const BufferView &key) const override; + outcome::result> tryGet( const BufferView &key) const override; outcome::result contains(const BufferView &key) const override; bool empty() const override; diff --git a/core/storage/trie/serialization/trie_serializer_impl.cpp b/core/storage/trie/serialization/trie_serializer_impl.cpp index 444e42ce7b..0dd3969d65 100644 --- a/core/storage/trie/serialization/trie_serializer_impl.cpp +++ b/core/storage/trie/serialization/trie_serializer_impl.cpp @@ -83,7 +83,7 @@ namespace kagome::storage::trie { } Buffer enc; if (codec_->isMerkleHash(db_key)) { - OUTCOME_TRY(db, backend_->load(db_key)); + OUTCOME_TRY(db, backend_->get(db_key)); enc = db.into(); } else { // `isMerkleHash(db_key) == false` means `db_key` is value itself diff --git a/test/core/blockchain/block_storage_test.cpp b/test/core/blockchain/block_storage_test.cpp index 80a5f3e214..1371be6788 100644 --- a/test/core/blockchain/block_storage_test.cpp +++ b/test/core/blockchain/block_storage_test.cpp @@ -56,7 +56,7 @@ class BlockStorageTest : public testing::Test { .WillRepeatedly(Return(genesis_block_hash)); // check if storage contained genesis block - EXPECT_CALL(*storage, tryLoadMock(_)).WillRepeatedly(Return(std::nullopt)); + EXPECT_CALL(*storage, tryGetMock(_)).WillRepeatedly(Return(std::nullopt)); // put genesis block into storage EXPECT_CALL(*storage, put(_, _)).WillRepeatedly(Return(outcome::success())); @@ -92,7 +92,7 @@ TEST_F(BlockStorageTest, CreateWithEmptyStorage) { .WillRepeatedly(Return(genesis_block_hash)); // check if storage contained genesis block - EXPECT_CALL(*empty_storage, tryLoadMock(_)) + EXPECT_CALL(*empty_storage, tryGetMock(_)) .WillRepeatedly(Return(std::nullopt)); // put genesis block into storage @@ -113,7 +113,7 @@ TEST_F(BlockStorageTest, CreateWithEmptyStorage) { TEST_F(BlockStorageTest, CreateWithExistingGenesis) { // trying to get header of block number 0 (genesis block) EXPECT_CALL(*storage, contains(_)).WillOnce(Return(outcome::success(true))); - EXPECT_CALL(*storage, tryLoadMock(_)) + EXPECT_CALL(*storage, tryGetMock(_)) // trying to get header of block number 0 (genesis block) .WillOnce(Return(Buffer{genesis_block_hash})); @@ -132,7 +132,7 @@ TEST_F(BlockStorageTest, CreateWithStorageError) { std::make_shared>(); // check if storage contained genesis block - EXPECT_CALL(*empty_storage, tryLoadMock(_)) + EXPECT_CALL(*empty_storage, tryGetMock(_)) .WillOnce(Return(kagome::storage::DatabaseError::IO_ERROR)); EXPECT_OUTCOME_ERROR( @@ -151,7 +151,7 @@ TEST_F(BlockStorageTest, PutBlock) { EXPECT_CALL(*hasher, blake2b_256(_)).WillOnce(Return(regular_block_hash)); - EXPECT_CALL(*storage, tryLoadMock(_)).WillOnce(Return(std::nullopt)); + EXPECT_CALL(*storage, tryGetMock(_)).WillOnce(Return(std::nullopt)); Block block; block.header.number = 1; @@ -168,7 +168,7 @@ TEST_F(BlockStorageTest, PutBlock) { TEST_F(BlockStorageTest, PutWithStorageError) { auto block_storage = createWithGenesis(); - EXPECT_CALL(*storage, tryLoadMock(_)) + EXPECT_CALL(*storage, tryGetMock(_)) .WillOnce(Return(Buffer{1, 1, 1, 1})) .WillOnce(Return(kagome::storage::DatabaseError::IO_ERROR)); diff --git a/test/core/consensus/babe/babe_config_repository_test.cpp b/test/core/consensus/babe/babe_config_repository_test.cpp index 6da3bc90c6..6ec4913b1f 100644 --- a/test/core/consensus/babe/babe_config_repository_test.cpp +++ b/test/core/consensus/babe/babe_config_repository_test.cpp @@ -58,7 +58,7 @@ class BabeConfigRepositoryTest : public testing::Test { persistent_storage = std::make_shared>(); - EXPECT_CALL(*persistent_storage, tryLoadMock(_)) + EXPECT_CALL(*persistent_storage, tryGetMock(_)) .WillRepeatedly(Return(std::nullopt)); block_tree = std::make_shared(); diff --git a/test/core/storage/rocksdb/rocksdb_integration_test.cpp b/test/core/storage/rocksdb/rocksdb_integration_test.cpp index c2a74709c8..903fb73dff 100644 --- a/test/core/storage/rocksdb/rocksdb_integration_test.cpp +++ b/test/core/storage/rocksdb/rocksdb_integration_test.cpp @@ -40,7 +40,7 @@ TEST_F(RocksDb_Integration_Test, Put_Get) { ASSERT_OUTCOME_SUCCESS_TRY(db_->put(key_, BufferView{value_})); ASSERT_OUTCOME_SUCCESS(contains, db_->contains(key_)); EXPECT_TRUE(contains); - EXPECT_OUTCOME_TRUE_2(val, db_->load(key_)); + EXPECT_OUTCOME_TRUE_2(val, db_->get(key_)); EXPECT_EQ(val, value_); } @@ -53,7 +53,7 @@ TEST_F(RocksDb_Integration_Test, Get_NonExistent) { ASSERT_OUTCOME_SUCCESS(contains, db_->contains(key_)); EXPECT_FALSE(contains); ASSERT_OUTCOME_SUCCESS_TRY(db_->remove(key_)); - auto r = db_->load(key_); + auto r = db_->get(key_); EXPECT_FALSE(r); EXPECT_EQ(r.error().value(), (int)DatabaseError::NOT_FOUND); } @@ -82,7 +82,7 @@ TEST_F(RocksDb_Integration_Test, WriteBatch) { for (const auto &item : expected) { ASSERT_OUTCOME_SUCCESS(contains, db_->contains(item)); EXPECT_TRUE(contains); - ASSERT_OUTCOME_SUCCESS(val, db_->load(item)); + ASSERT_OUTCOME_SUCCESS(val, db_->get(item)); EXPECT_EQ(val, item); } diff --git a/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp b/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp index 922f3f46aa..23c8ccf5c9 100644 --- a/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp @@ -54,9 +54,9 @@ TEST_F(TrieDbBackendTest, Put) { TEST_F(TrieDbBackendTest, Get) { Buffer prefixed{kNodePrefix}; prefixed.put("abc"_buf); - EXPECT_CALL(*storage, loadMock(BufferView{prefixed})) + EXPECT_CALL(*storage, getMock(BufferView{prefixed})) .WillOnce(Return("123"_buf)); - EXPECT_OUTCOME_TRUE_1(backend.load("abc"_buf)); + EXPECT_OUTCOME_TRUE_1(backend.get("abc"_buf)); } /** diff --git a/test/mock/core/storage/persistent_map_mock.hpp b/test/mock/core/storage/persistent_map_mock.hpp index 0cc9453d22..f118cb37ab 100644 --- a/test/mock/core/storage/persistent_map_mock.hpp +++ b/test/mock/core/storage/persistent_map_mock.hpp @@ -20,21 +20,21 @@ namespace kagome::storage::face { cursor, std::unique_ptr::Cursor>()); - MOCK_METHOD(outcome::result, loadMock, (const KView &), (const)); + MOCK_METHOD(outcome::result, getMock, (const KView &), (const)); MOCK_METHOD(outcome::result>, - tryLoadMock, + tryGetMock, (const KView &), (const)); - outcome::result> load(const KView &key) const override { - OUTCOME_TRY(value, loadMock(key)); + outcome::result> get(const KView &key) const override { + OUTCOME_TRY(value, getMock(key)); return std::move(value); } - outcome::result>> tryLoad( + outcome::result>> tryGet( const KView &key) const override { - OUTCOME_TRY(value, tryLoadMock(key)); + OUTCOME_TRY(value, tryGetMock(key)); if (value) { return std::move(*value); } From 6753f6321f234865de5651aff35dd0444d3237d4 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 20:31:05 +0300 Subject: [PATCH 12/19] remove alias Signed-off-by: turuslan --- core/storage/face/readable.hpp | 16 +++++----------- core/storage/rocksdb/rocksdb.hpp | 6 +++--- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index 97f0ae2af4..3d63df8172 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -14,8 +14,6 @@ namespace kagome::storage::face { template struct ReadableBase { - using Key = K; - virtual ~ReadableBase() = default; /** @@ -23,7 +21,7 @@ namespace kagome::storage::face { * @param key K * @return true if key has value, false if does not, or error at . */ - virtual outcome::result contains(const Key &key) const = 0; + virtual outcome::result contains(const K &key) const = 0; /** * @brief Returns true if the storage is empty. @@ -38,8 +36,6 @@ namespace kagome::storage::face { */ template struct ReadableMap : public ReadableBase { - using Key = K; - virtual ~ReadableMap() = default; /** @@ -47,7 +43,7 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result> get(const Key &key) const = 0; + virtual outcome::result> get(const K &key) const = 0; /** * @brief Get value by key @@ -55,13 +51,11 @@ namespace kagome::storage::face { * @return V if contains(K) or std::nullopt */ virtual outcome::result>> tryGet( - const Key &key) const = 0; + const K &key) const = 0; }; template struct ReadableStorage : public ReadableBase { - using Key = K; - virtual ~ReadableStorage() = default; /** @@ -69,7 +63,7 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result> get(const Key &key) const = 0; + virtual outcome::result> get(const K &key) const = 0; /** * @brief Load value by key @@ -77,7 +71,7 @@ namespace kagome::storage::face { * @return V if contains(K) or std::nullopt */ virtual outcome::result>> tryGet( - const Key &key) const = 0; + const K &key) const = 0; }; } // namespace kagome::storage::face diff --git a/core/storage/rocksdb/rocksdb.hpp b/core/storage/rocksdb/rocksdb.hpp index 0291366550..0c49e24f97 100644 --- a/core/storage/rocksdb/rocksdb.hpp +++ b/core/storage/rocksdb/rocksdb.hpp @@ -38,14 +38,14 @@ namespace kagome::storage { std::unique_ptr cursor() override; - outcome::result contains(const Key &key) const override; + outcome::result contains(const BufferView &key) const override; bool empty() const override; - outcome::result get(const Key &key) const override; + outcome::result get(const BufferView &key) const override; outcome::result> tryGet( - const Key &key) const override; + const BufferView &key) const override; outcome::result put(const BufferView &key, BufferOrView &&value) override; From 4dda6b93eb90f55635cfaf0fb64b164c59a25192 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 20:31:47 +0300 Subject: [PATCH 13/19] remove unused Signed-off-by: turuslan --- core/common/buffer.hpp | 3 --- core/host_api/impl/child_storage_extension.cpp | 1 - core/runtime/runtime_api/impl/session_keys_api.cpp | 2 +- core/runtime/runtime_api/impl/session_keys_api.hpp | 2 +- core/runtime/runtime_api/session_keys_api.hpp | 2 +- core/storage/buffer_map_types.hpp | 1 - test/core/host_api/child_storage_extension_test.cpp | 1 - test/mock/core/runtime/session_keys_api_mock.hpp | 2 +- 8 files changed, 4 insertions(+), 10 deletions(-) diff --git a/core/common/buffer.hpp b/core/common/buffer.hpp index b2023ece33..257fae2c0f 100644 --- a/core/common/buffer.hpp +++ b/core/common/buffer.hpp @@ -245,9 +245,6 @@ namespace kagome::common { static inline const Buffer kEmptyBuffer{}; - using BufferMutRef = std::reference_wrapper; - using BufferConstRef = std::reference_wrapper; - namespace literals { /// creates a buffer filled with characters from the original string /// mind that it does not perform unhexing, there is ""_unhex for it diff --git a/core/host_api/impl/child_storage_extension.cpp b/core/host_api/impl/child_storage_extension.cpp index ab31d462b1..b1931db1a5 100644 --- a/core/host_api/impl/child_storage_extension.cpp +++ b/core/host_api/impl/child_storage_extension.cpp @@ -18,7 +18,6 @@ #include using kagome::common::Buffer; -using kagome::common::BufferConstRef; using kagome::storage::trie::TrieError; namespace kagome::host_api { diff --git a/core/runtime/runtime_api/impl/session_keys_api.cpp b/core/runtime/runtime_api/impl/session_keys_api.cpp index 65e3da79aa..f8ef7dd7da 100644 --- a/core/runtime/runtime_api/impl/session_keys_api.cpp +++ b/core/runtime/runtime_api/impl/session_keys_api.cpp @@ -24,7 +24,7 @@ namespace kagome::runtime { outcome::result>> SessionKeysApiImpl::decode_session_keys( const primitives::BlockHash &block_hash, - common::BufferConstRef encoded) const { + common::BufferView encoded) const { return executor_ ->callAt>>( block_hash, "SessionKeys_decode_session_keys", encoded); diff --git a/core/runtime/runtime_api/impl/session_keys_api.hpp b/core/runtime/runtime_api/impl/session_keys_api.hpp index 312d0b10f3..79d7a0979a 100644 --- a/core/runtime/runtime_api/impl/session_keys_api.hpp +++ b/core/runtime/runtime_api/impl/session_keys_api.hpp @@ -22,7 +22,7 @@ namespace kagome::runtime { outcome::result>> decode_session_keys(const primitives::BlockHash &block_hash, - common::BufferConstRef encoded) const override; + common::BufferView encoded) const override; private: std::shared_ptr executor_; diff --git a/core/runtime/runtime_api/session_keys_api.hpp b/core/runtime/runtime_api/session_keys_api.hpp index 331f418f1e..15e2bffcca 100644 --- a/core/runtime/runtime_api/session_keys_api.hpp +++ b/core/runtime/runtime_api/session_keys_api.hpp @@ -38,7 +38,7 @@ namespace kagome::runtime { virtual outcome::result< std::vector>> decode_session_keys(const primitives::BlockHash &block_hash, - common::BufferConstRef encoded) const = 0; + common::BufferView encoded) const = 0; }; } // namespace kagome::runtime diff --git a/core/storage/buffer_map_types.hpp b/core/storage/buffer_map_types.hpp index 1cbb5f08e0..6f5e1ea060 100644 --- a/core/storage/buffer_map_types.hpp +++ b/core/storage/buffer_map_types.hpp @@ -28,7 +28,6 @@ namespace kagome::storage { using Buffer = common::SLBuffer::max()>; using BufferView = common::BufferView; - using BufferConstRef = common::BufferConstRef; using common::BufferOrView; using BufferBatch = face::WriteBatch; diff --git a/test/core/host_api/child_storage_extension_test.cpp b/test/core/host_api/child_storage_extension_test.cpp index a9ef5f41e0..ad9fbd7131 100644 --- a/test/core/host_api/child_storage_extension_test.cpp +++ b/test/core/host_api/child_storage_extension_test.cpp @@ -27,7 +27,6 @@ #include "testutil/prepare_loggers.hpp" using kagome::common::Buffer; -using kagome::common::BufferConstRef; using kagome::host_api::ChildStorageExtension; using kagome::runtime::Memory; using kagome::runtime::MemoryMock; diff --git a/test/mock/core/runtime/session_keys_api_mock.hpp b/test/mock/core/runtime/session_keys_api_mock.hpp index 734a34035a..09148a8811 100644 --- a/test/mock/core/runtime/session_keys_api_mock.hpp +++ b/test/mock/core/runtime/session_keys_api_mock.hpp @@ -22,7 +22,7 @@ namespace kagome::runtime { MOCK_METHOD(outcome::result>, decode_session_keys, - (const primitives::BlockHash &, common::BufferConstRef), + (const primitives::BlockHash &, common::BufferView), (const, override)); }; From 2d7b8e1d3fbe39f7e96e32182934c600aa71115f Mon Sep 17 00:00:00 2001 From: turuslan Date: Sat, 3 Dec 2022 20:36:28 +0300 Subject: [PATCH 14/19] merge readable Signed-off-by: turuslan --- core/blockchain/impl/common.hpp | 2 +- core/storage/face/generic_maps.hpp | 4 +-- core/storage/face/readable.hpp | 43 ++++++------------------------ core/storage/trie/trie_batches.hpp | 2 +- 4 files changed, 12 insertions(+), 39 deletions(-) diff --git a/core/blockchain/impl/common.hpp b/core/blockchain/impl/common.hpp index cd3b2f5cf6..80736357aa 100644 --- a/core/blockchain/impl/common.hpp +++ b/core/blockchain/impl/common.hpp @@ -15,7 +15,7 @@ namespace kagome::blockchain { using ReadableBufferStorage = - storage::face::ReadableStorage; + storage::face::Readable; /** * Convert a block ID into a key, which is a first part of a key, by which the diff --git a/core/storage/face/generic_maps.hpp b/core/storage/face/generic_maps.hpp index 63f6a60489..a291097c95 100644 --- a/core/storage/face/generic_maps.hpp +++ b/core/storage/face/generic_maps.hpp @@ -20,11 +20,11 @@ namespace kagome::storage::face { */ template struct ReadOnlyMap : public Iterable, - public ReadableMap {}; + public Readable {}; template struct ReadOnlyStorage : public Iterable, - public ReadableStorage {}; + public Readable {}; /** * @brief An abstraction over a readable, writeable, iterable key-value map. diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index 3d63df8172..a905061839 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -11,10 +11,14 @@ #include "storage/face/owned_or_view.hpp" namespace kagome::storage::face { - - template - struct ReadableBase { - virtual ~ReadableBase() = default; + /** + * @brief A mixin for read-only map. + * @tparam K key type + * @tparam V value type + */ + template + struct Readable { + virtual ~Readable() = default; /** * @brief Checks if given key-value binding exists in the storage. @@ -27,16 +31,6 @@ namespace kagome::storage::face { * @brief Returns true if the storage is empty. */ virtual bool empty() const = 0; - }; - - /** - * @brief A mixin for read-only map. - * @tparam K key type - * @tparam V value type - */ - template - struct ReadableMap : public ReadableBase { - virtual ~ReadableMap() = default; /** * @brief Get value by key @@ -53,27 +47,6 @@ namespace kagome::storage::face { virtual outcome::result>> tryGet( const K &key) const = 0; }; - - template - struct ReadableStorage : public ReadableBase { - virtual ~ReadableStorage() = default; - - /** - * @brief Load value by key - * @param key K - * @return V - */ - virtual outcome::result> get(const K &key) const = 0; - - /** - * @brief Load value by key - * @param key K - * @return V if contains(K) or std::nullopt - */ - virtual outcome::result>> tryGet( - const K &key) const = 0; - }; - } // namespace kagome::storage::face #endif // KAGOME_READABLE_HPP diff --git a/core/storage/trie/trie_batches.hpp b/core/storage/trie/trie_batches.hpp index 9b56ec20b9..76affcd330 100644 --- a/core/storage/trie/trie_batches.hpp +++ b/core/storage/trie/trie_batches.hpp @@ -12,7 +12,7 @@ namespace kagome::storage::trie { - class TrieBatch : public face::ReadableMap, + class TrieBatch : public face::Readable, public face::Writeable, public face::Iterable { public: From eed002adcf5f1107975098a4132631dbdbf34c91 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sun, 4 Dec 2022 05:42:05 +0300 Subject: [PATCH 15/19] view Signed-off-by: turuslan --- core/blockchain/impl/common.hpp | 2 +- core/storage/buffer_map_types.hpp | 23 ++++++------- core/storage/face/generic_maps.hpp | 28 ++++++++-------- core/storage/face/iterable.hpp | 5 ++- core/storage/face/map_cursor.hpp | 6 ++-- core/storage/face/readable.hpp | 7 ++-- core/storage/face/view.hpp | 17 ++++++++++ core/storage/face/writeable.hpp | 6 ++-- core/storage/in_memory/in_memory_storage.hpp | 2 +- .../trie/polkadot_trie/polkadot_trie.hpp | 6 ++-- core/storage/trie/trie_batches.hpp | 6 ++-- test/core/blockchain/block_storage_test.cpp | 12 +++---- .../babe/babe_config_repository_test.cpp | 8 ++--- test/core/network/synchronizer_test.cpp | 10 ++---- .../trie_storage_backend_test.cpp | 8 ++--- .../mock/core/storage/persistent_map_mock.hpp | 32 ++++++++++--------- test/mock/core/storage/write_batch_mock.hpp | 8 +++-- 17 files changed, 97 insertions(+), 89 deletions(-) create mode 100644 core/storage/face/view.hpp diff --git a/core/blockchain/impl/common.hpp b/core/blockchain/impl/common.hpp index 80736357aa..18e6642aa8 100644 --- a/core/blockchain/impl/common.hpp +++ b/core/blockchain/impl/common.hpp @@ -15,7 +15,7 @@ namespace kagome::blockchain { using ReadableBufferStorage = - storage::face::Readable; + storage::face::Readable; /** * Convert a block ID into a key, which is a first part of a key, by which the diff --git a/core/storage/buffer_map_types.hpp b/core/storage/buffer_map_types.hpp index 6f5e1ea060..7e3864ca18 100644 --- a/core/storage/buffer_map_types.hpp +++ b/core/storage/buffer_map_types.hpp @@ -22,26 +22,23 @@ namespace kagome::storage::face { struct OwnedOrViewTrait { using type = common::BufferOrView; }; + + template <> + struct ViewTrait { + using type = common::BufferView; + }; } // namespace kagome::storage::face namespace kagome::storage { - - using Buffer = common::SLBuffer::max()>; - using BufferView = common::BufferView; + using common::Buffer; using common::BufferOrView; + using common::BufferView; - using BufferBatch = face::WriteBatch; - - using ReadOnlyBufferMap = face::ReadOnlyMap; - - using BufferStorage = face::GenericStorage; - - using BufferMap = face::GenericMap; - - using BufferMapCursor = face::MapCursor; + using BufferBatch = face::WriteBatch; - using BufferStorageCursor = face::MapCursor; + using BufferStorage = face::GenericStorage; + using BufferStorageCursor = face::MapCursor; } // namespace kagome::storage #endif // KAGOME_BUFFER_MAP_TYPES_HPP diff --git a/core/storage/face/generic_maps.hpp b/core/storage/face/generic_maps.hpp index a291097c95..2e957e52a7 100644 --- a/core/storage/face/generic_maps.hpp +++ b/core/storage/face/generic_maps.hpp @@ -18,28 +18,26 @@ namespace kagome::storage::face { * @tparam K key type * @tparam V value type */ - template - struct ReadOnlyMap : public Iterable, - public Readable {}; + template + struct ReadOnlyMap : Iterable, Readable {}; - template - struct ReadOnlyStorage : public Iterable, - public Readable {}; + template + struct ReadOnlyStorage : Iterable, Readable {}; /** * @brief An abstraction over a readable, writeable, iterable key-value map. * @tparam K key type * @tparam V value type */ - template - struct GenericMap : public ReadOnlyMap, - public Writeable, - public BatchWriteable {}; - - template - struct GenericStorage : public ReadOnlyStorage, - public Writeable, - public BatchWriteable { + template + struct GenericMap : ReadOnlyMap, + Writeable, + BatchWriteable {}; + + template + struct GenericStorage : ReadOnlyStorage, + Writeable, + BatchWriteable { /** * Reports RAM state size * @return size in bytes diff --git a/core/storage/face/iterable.hpp b/core/storage/face/iterable.hpp index 1095aead5f..d84eebd08d 100644 --- a/core/storage/face/iterable.hpp +++ b/core/storage/face/iterable.hpp @@ -16,11 +16,10 @@ namespace kagome::storage::face { * @brief A mixin for an iterable map. * @tparam K map key type * @tparam V map value type - * @tparam KView map key view type */ - template + template struct Iterable { - using Cursor = MapCursor; + using Cursor = MapCursor; virtual ~Iterable() = default; diff --git a/core/storage/face/map_cursor.hpp b/core/storage/face/map_cursor.hpp index e3683d3595..3eedf5c7f4 100644 --- a/core/storage/face/map_cursor.hpp +++ b/core/storage/face/map_cursor.hpp @@ -10,6 +10,7 @@ #include "outcome/outcome.hpp" #include "storage/face/owned_or_view.hpp" +#include "storage/face/view.hpp" namespace kagome::storage::face { @@ -17,9 +18,8 @@ namespace kagome::storage::face { * @brief An abstraction over generic map cursor. * @tparam K key type * @tparam V value type - * @tparam KView key view type */ - template + template struct MapCursor { virtual ~MapCursor() = default; @@ -33,7 +33,7 @@ namespace kagome::storage::face { * @brief Find given key and seek iterator to this key. * @return error if any, true if \arg key found, false otherwise */ - virtual outcome::result seek(const KView &key) = 0; + virtual outcome::result seek(const View &key) = 0; /** * @brief Same as std::rbegin(...);, e.g. points to the last valid element diff --git a/core/storage/face/readable.hpp b/core/storage/face/readable.hpp index a905061839..cf6192f9cc 100644 --- a/core/storage/face/readable.hpp +++ b/core/storage/face/readable.hpp @@ -9,6 +9,7 @@ #include #include "storage/face/owned_or_view.hpp" +#include "storage/face/view.hpp" namespace kagome::storage::face { /** @@ -25,7 +26,7 @@ namespace kagome::storage::face { * @param key K * @return true if key has value, false if does not, or error at . */ - virtual outcome::result contains(const K &key) const = 0; + virtual outcome::result contains(const View &key) const = 0; /** * @brief Returns true if the storage is empty. @@ -37,7 +38,7 @@ namespace kagome::storage::face { * @param key K * @return V */ - virtual outcome::result> get(const K &key) const = 0; + virtual outcome::result> get(const View &key) const = 0; /** * @brief Get value by key @@ -45,7 +46,7 @@ namespace kagome::storage::face { * @return V if contains(K) or std::nullopt */ virtual outcome::result>> tryGet( - const K &key) const = 0; + const View &key) const = 0; }; } // namespace kagome::storage::face diff --git a/core/storage/face/view.hpp b/core/storage/face/view.hpp new file mode 100644 index 0000000000..87cfabd939 --- /dev/null +++ b/core/storage/face/view.hpp @@ -0,0 +1,17 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef KAGOME_STORAGE_FACE_VIEW_HPP +#define KAGOME_STORAGE_FACE_VIEW_HPP + +namespace kagome::storage::face { + template + struct ViewTrait; + + template + using View = typename ViewTrait::type; +} // namespace kagome::storage::face + +#endif // KAGOME_STORAGE_FACE_VIEW_HPP diff --git a/core/storage/face/writeable.hpp b/core/storage/face/writeable.hpp index 3b333ed252..0c9dab097f 100644 --- a/core/storage/face/writeable.hpp +++ b/core/storage/face/writeable.hpp @@ -9,6 +9,7 @@ #include #include "storage/face/owned_or_view.hpp" +#include "storage/face/view.hpp" namespace kagome::storage::face { @@ -27,14 +28,15 @@ namespace kagome::storage::face { * @param value value * @return result containing void if put successful, error otherwise */ - virtual outcome::result put(const K &key, OwnedOrView &&value) = 0; + virtual outcome::result put(const View &key, + OwnedOrView &&value) = 0; /** * @brief Remove value by key * @param key K * @return error code if error happened */ - virtual outcome::result remove(const K &key) = 0; + virtual outcome::result remove(const View &key) = 0; }; } // namespace kagome::storage::face diff --git a/core/storage/in_memory/in_memory_storage.hpp b/core/storage/in_memory/in_memory_storage.hpp index 4d7082a8c6..6d3d4f929d 100644 --- a/core/storage/in_memory/in_memory_storage.hpp +++ b/core/storage/in_memory/in_memory_storage.hpp @@ -41,7 +41,7 @@ namespace kagome::storage { std::unique_ptr batch() override; - std::unique_ptr cursor() override; + std::unique_ptr cursor() override; size_t size() const override; diff --git a/core/storage/trie/polkadot_trie/polkadot_trie.hpp b/core/storage/trie/polkadot_trie/polkadot_trie.hpp index 57fafc2d23..8fd7bcecd4 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie.hpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie.hpp @@ -17,10 +17,8 @@ namespace kagome::storage::trie { * For specification see Polkadot Runtime Environment Protocol Specification * '2.1.2 The General Tree Structure' and further */ - class PolkadotTrie - : public face:: - ReadOnlyMap, - public face::Writeable { + class PolkadotTrie : public face::ReadOnlyMap, + public face::Writeable { public: using NodePtr = std::shared_ptr; using ConstNodePtr = std::shared_ptr; diff --git a/core/storage/trie/trie_batches.hpp b/core/storage/trie/trie_batches.hpp index 76affcd330..a1d4302d0f 100644 --- a/core/storage/trie/trie_batches.hpp +++ b/core/storage/trie/trie_batches.hpp @@ -12,9 +12,9 @@ namespace kagome::storage::trie { - class TrieBatch : public face::Readable, - public face::Writeable, - public face::Iterable { + class TrieBatch : public face::Readable, + public face::Writeable, + public face::Iterable { public: ~TrieBatch() override = default; diff --git a/test/core/blockchain/block_storage_test.cpp b/test/core/blockchain/block_storage_test.cpp index 1371be6788..74786def45 100644 --- a/test/core/blockchain/block_storage_test.cpp +++ b/test/core/blockchain/block_storage_test.cpp @@ -27,7 +27,7 @@ using kagome::primitives::BlockData; using kagome::primitives::BlockHash; using kagome::primitives::BlockHeader; using kagome::primitives::BlockNumber; -using kagome::storage::face::GenericStorageMock; +using kagome::storage::BufferStorageMock; using kagome::storage::trie::RootHash; using scale::encode; using testing::_; @@ -43,8 +43,8 @@ class BlockStorageTest : public testing::Test { root_hash.fill(1); } std::shared_ptr hasher = std::make_shared(); - std::shared_ptr> storage = - std::make_shared>(); + std::shared_ptr storage = + std::make_shared(); BlockHash genesis_block_hash{{'g', 'e', 'n', 'e', 's', 'i', 's'}}; BlockHash regular_block_hash{{'r', 'e', 'g', 'u', 'l', 'a', 'r'}}; @@ -84,8 +84,7 @@ TEST_F(BlockStorageTest, CreateWithGenesis) { * @then storage will be initialized by genesis block */ TEST_F(BlockStorageTest, CreateWithEmptyStorage) { - auto empty_storage = - std::make_shared>(); + auto empty_storage = std::make_shared(); // calculate hash of genesis block at put block header EXPECT_CALL(*hasher, blake2b_256(_)) @@ -128,8 +127,7 @@ TEST_F(BlockStorageTest, CreateWithExistingGenesis) { * @then initialisation will fail */ TEST_F(BlockStorageTest, CreateWithStorageError) { - auto empty_storage = - std::make_shared>(); + auto empty_storage = std::make_shared(); // check if storage contained genesis block EXPECT_CALL(*empty_storage, tryGetMock(_)) diff --git a/test/core/consensus/babe/babe_config_repository_test.cpp b/test/core/consensus/babe/babe_config_repository_test.cpp index 6ec4913b1f..1fade8f21b 100644 --- a/test/core/consensus/babe/babe_config_repository_test.cpp +++ b/test/core/consensus/babe/babe_config_repository_test.cpp @@ -34,7 +34,7 @@ using primitives::BlockId; using primitives::BlockInfo; using primitives::events::ChainSubscriptionEngine; using runtime::BabeApiMock; -using storage::face::GenericStorageMock; +using storage::BufferStorageMock; using std::chrono_literals::operator""ms; @@ -56,8 +56,7 @@ class BabeConfigRepositoryTest : public testing::Test { app_state_manager = std::make_shared(); EXPECT_CALL(*app_state_manager, atPrepare(_)).WillOnce(Return()); - persistent_storage = - std::make_shared>(); + persistent_storage = std::make_shared(); EXPECT_CALL(*persistent_storage, tryGetMock(_)) .WillRepeatedly(Return(std::nullopt)); @@ -91,8 +90,7 @@ class BabeConfigRepositoryTest : public testing::Test { primitives::BabeConfiguration babe_config; std::shared_ptr app_state_manager; - std::shared_ptr> - persistent_storage; + std::shared_ptr persistent_storage; std::shared_ptr block_tree; std::shared_ptr header_repo; std::shared_ptr babe_api; diff --git a/test/core/network/synchronizer_test.cpp b/test/core/network/synchronizer_test.cpp index 8a38599661..48f43e957c 100644 --- a/test/core/network/synchronizer_test.cpp +++ b/test/core/network/synchronizer_test.cpp @@ -39,6 +39,7 @@ using primitives::BlockHash; using primitives::BlockHeader; using primitives::BlockInfo; using primitives::BlockNumber; +using storage::BufferStorageMock; using ::testing::_; using ::testing::AnyNumber; @@ -112,13 +113,8 @@ class SynchronizerTest std::make_shared(); std::shared_ptr hasher = std::make_shared(); - std::shared_ptr> - buffer_storage = std::make_shared< - storage::face::GenericStorageMock>(); + std::shared_ptr buffer_storage = + std::make_shared(); std::shared_ptr synchronizer; diff --git a/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp b/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp index 23c8ccf5c9..ec2243fb59 100644 --- a/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp +++ b/test/core/storage/trie/trie_storage/trie_storage_backend_test.cpp @@ -15,7 +15,7 @@ using kagome::common::Buffer; using kagome::common::BufferView; -using kagome::storage::face::GenericStorageMock; +using kagome::storage::BufferStorageMock; using kagome::storage::face::WriteBatchMock; using kagome::storage::trie::TrieStorageBackendImpl; using testing::Invoke; @@ -25,8 +25,8 @@ static const Buffer kNodePrefix{1}; class TrieDbBackendTest : public testing::Test { public: - std::shared_ptr> storage = - std::make_shared>(); + std::shared_ptr storage = + std::make_shared(); TrieStorageBackendImpl backend{storage, kNodePrefix}; }; @@ -65,7 +65,7 @@ TEST_F(TrieDbBackendTest, Get) { * @then it delegates them to the underlying storage batch with added prefixes */ TEST_F(TrieDbBackendTest, Batch) { - auto batch_mock = std::make_unique>(); + auto batch_mock = std::make_unique>(); auto buf_abc = Buffer{kNodePrefix}.put("abc"_buf); EXPECT_CALL(*batch_mock, put(buf_abc.view(), "123"_buf)) .WillOnce(Return(outcome::success())); diff --git a/test/mock/core/storage/persistent_map_mock.hpp b/test/mock/core/storage/persistent_map_mock.hpp index f118cb37ab..a95017dcc8 100644 --- a/test/mock/core/storage/persistent_map_mock.hpp +++ b/test/mock/core/storage/persistent_map_mock.hpp @@ -8,32 +8,30 @@ #include -#include "storage/face/batch_writeable.hpp" +#include "storage/buffer_map_types.hpp" #include "storage/face/generic_maps.hpp" namespace kagome::storage::face { - template - struct GenericStorageMock : public face::GenericStorage { - MOCK_METHOD0_T(batch, std::unique_ptr>()); + template + struct GenericStorageMock : public GenericStorage { + MOCK_METHOD0_T(batch, std::unique_ptr>()); - MOCK_METHOD0_T( - cursor, - std::unique_ptr::Cursor>()); + MOCK_METHOD0_T(cursor, std::unique_ptr>()); - MOCK_METHOD(outcome::result, getMock, (const KView &), (const)); + MOCK_METHOD(outcome::result, getMock, (const View &), (const)); MOCK_METHOD(outcome::result>, tryGetMock, - (const KView &), + (const View &), (const)); - outcome::result> get(const KView &key) const override { + outcome::result> get(const View &key) const override { OUTCOME_TRY(value, getMock(key)); return std::move(value); } outcome::result>> tryGet( - const KView &key) const override { + const View &key) const override { OUTCOME_TRY(value, tryGetMock(key)); if (value) { return std::move(*value); @@ -41,19 +39,23 @@ namespace kagome::storage::face { return std::nullopt; } - MOCK_CONST_METHOD1_T(contains, outcome::result(const KView &)); + MOCK_CONST_METHOD1_T(contains, outcome::result(const View &)); MOCK_CONST_METHOD0_T(empty, bool()); - MOCK_METHOD(outcome::result, put, (const KView &, const V &)); - outcome::result put(const KView &k, OwnedOrView &&v) override { + MOCK_METHOD(outcome::result, put, (const View &, const V &)); + outcome::result put(const View &k, OwnedOrView &&v) override { return put(k, v.mut()); } - MOCK_METHOD1_T(remove, outcome::result(const KView &)); + MOCK_METHOD1_T(remove, outcome::result(const View &)); MOCK_CONST_METHOD0_T(size, size_t()); }; } // namespace kagome::storage::face +namespace kagome::storage { + using BufferStorageMock = face::GenericStorageMock; +} // namespace kagome::storage + #endif // KAGOME_PERSISTENT_MAP_MOCK_HPP diff --git a/test/mock/core/storage/write_batch_mock.hpp b/test/mock/core/storage/write_batch_mock.hpp index f6ebc454fb..18351950a1 100644 --- a/test/mock/core/storage/write_batch_mock.hpp +++ b/test/mock/core/storage/write_batch_mock.hpp @@ -19,12 +19,14 @@ namespace kagome::storage::face { MOCK_METHOD(void, clear, (), (override)); - MOCK_METHOD2_T(put, outcome::result(const K &key, const V &value)); - outcome::result put(const K &key, OwnedOrView &&value) override { + MOCK_METHOD2_T(put, + outcome::result(const View &key, const V &value)); + outcome::result put(const View &key, + OwnedOrView &&value) override { return put(key, value.mut()); } - MOCK_METHOD1_T(remove, outcome::result(const K &key)); + MOCK_METHOD1_T(remove, outcome::result(const View &key)); }; } // namespace kagome::storage::face From a4bd20d720a858101434c8aa359fd452f0d1a986 Mon Sep 17 00:00:00 2001 From: turuslan Date: Sun, 4 Dec 2022 06:17:34 +0300 Subject: [PATCH 16/19] merge interfaces Signed-off-by: turuslan --- core/storage/face/batch_writeable.hpp | 4 +++- core/storage/face/generic_maps.hpp | 24 ++++--------------- .../trie/impl/trie_storage_backend_impl.cpp | 5 ---- .../trie/impl/trie_storage_backend_impl.hpp | 2 -- .../trie/polkadot_trie/polkadot_trie.hpp | 3 +-- core/storage/trie/trie_batches.hpp | 4 +--- 6 files changed, 10 insertions(+), 32 deletions(-) diff --git a/core/storage/face/batch_writeable.hpp b/core/storage/face/batch_writeable.hpp index 8c2f85c317..93b544b771 100644 --- a/core/storage/face/batch_writeable.hpp +++ b/core/storage/face/batch_writeable.hpp @@ -27,7 +27,9 @@ namespace kagome::storage::face { * @brief Creates new Write Batch - an object, which can be used to * efficiently write bulk data. */ - virtual std::unique_ptr> batch() = 0; + virtual std::unique_ptr> batch() { + abort(); + } }; } // namespace kagome::storage::face diff --git a/core/storage/face/generic_maps.hpp b/core/storage/face/generic_maps.hpp index 2e957e52a7..6ecb2859da 100644 --- a/core/storage/face/generic_maps.hpp +++ b/core/storage/face/generic_maps.hpp @@ -12,37 +12,23 @@ #include "storage/face/writeable.hpp" namespace kagome::storage::face { - - /** - * @brief An abstraction over a readable and iterable key-value map. - * @tparam K key type - * @tparam V value type - */ - template - struct ReadOnlyMap : Iterable, Readable {}; - - template - struct ReadOnlyStorage : Iterable, Readable {}; - /** * @brief An abstraction over a readable, writeable, iterable key-value map. * @tparam K key type * @tparam V value type */ template - struct GenericMap : ReadOnlyMap, - Writeable, - BatchWriteable {}; - - template - struct GenericStorage : ReadOnlyStorage, + struct GenericStorage : Readable, + Iterable, Writeable, BatchWriteable { /** * Reports RAM state size * @return size in bytes */ - virtual size_t size() const = 0; + virtual size_t size() const { + abort(); + } }; } // namespace kagome::storage::face diff --git a/core/storage/trie/impl/trie_storage_backend_impl.cpp b/core/storage/trie/impl/trie_storage_backend_impl.cpp index 4d1dd409c9..599884fcc1 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.cpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.cpp @@ -60,9 +60,4 @@ namespace kagome::storage::trie { const common::BufferView &key) const { return common::Buffer{node_prefix_}.put(key); } - - size_t TrieStorageBackendImpl::size() const { - return storage_->size(); - } - } // namespace kagome::storage::trie diff --git a/core/storage/trie/impl/trie_storage_backend_impl.hpp b/core/storage/trie/impl/trie_storage_backend_impl.hpp index 77ad7499b2..1babd15d8f 100644 --- a/core/storage/trie/impl/trie_storage_backend_impl.hpp +++ b/core/storage/trie/impl/trie_storage_backend_impl.hpp @@ -32,8 +32,6 @@ namespace kagome::storage::trie { BufferOrView &&value) override; outcome::result remove(const common::BufferView &key) override; - size_t size() const override; - private: common::Buffer prefixKey(const common::BufferView &key) const; diff --git a/core/storage/trie/polkadot_trie/polkadot_trie.hpp b/core/storage/trie/polkadot_trie/polkadot_trie.hpp index 8fd7bcecd4..41c2d4249e 100644 --- a/core/storage/trie/polkadot_trie/polkadot_trie.hpp +++ b/core/storage/trie/polkadot_trie/polkadot_trie.hpp @@ -17,8 +17,7 @@ namespace kagome::storage::trie { * For specification see Polkadot Runtime Environment Protocol Specification * '2.1.2 The General Tree Structure' and further */ - class PolkadotTrie : public face::ReadOnlyMap, - public face::Writeable { + class PolkadotTrie : public BufferStorage { public: using NodePtr = std::shared_ptr; using ConstNodePtr = std::shared_ptr; diff --git a/core/storage/trie/trie_batches.hpp b/core/storage/trie/trie_batches.hpp index a1d4302d0f..b16c0d6660 100644 --- a/core/storage/trie/trie_batches.hpp +++ b/core/storage/trie/trie_batches.hpp @@ -12,9 +12,7 @@ namespace kagome::storage::trie { - class TrieBatch : public face::Readable, - public face::Writeable, - public face::Iterable { + class TrieBatch : public BufferStorage { public: ~TrieBatch() override = default; From e5aa896a03f1d808d691fea0a23170d197c9972b Mon Sep 17 00:00:00 2001 From: turuslan Date: Tue, 6 Dec 2022 12:04:33 +0300 Subject: [PATCH 17/19] boost variant Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index c0bdf00d77..1a61cbe103 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -6,8 +6,9 @@ #ifndef KAGOME_COMMON_BUFFER_OR_VIEW_HPP #define KAGOME_COMMON_BUFFER_OR_VIEW_HPP +#include +#include #include -#include #include "common/buffer.hpp" @@ -35,18 +36,18 @@ namespace kagome::common { BufferOrView &operator=(BufferOrView &&) = default; bool owned() const { - if (variant.index() == 2) { + if (variant.which() == 2) { // moved with `.into()` abort(); } - return variant.index() == 1; + return variant.which() == 1; } BufferView view() const { if (!owned()) { - return std::get(variant); + return boost::get(variant); } - return BufferView{std::get(variant)}; + return BufferView{boost::get(variant)}; } operator BufferView() const { @@ -60,21 +61,21 @@ namespace kagome::common { // get mutable buffer reference, copy once if view Buffer &mut() { if (!owned()) { - auto view = std::get(variant); - variant.emplace(view); + auto view = boost::get(variant); + variant = Buffer{view}; } - return std::get(variant); + return boost::get(variant); } // move buffer away, copy once if view Buffer into() { auto buffer = std::move(mut()); - variant.emplace(); + variant = Moved{}; return buffer; } private: - std::variant variant; + boost::variant variant; template > friend bool operator==(const BufferOrView &l, const T &r) { From ce6b6a7aea037767da0f77f4dda04cf5ad16e779 Mon Sep 17 00:00:00 2001 From: turuslan Date: Tue, 6 Dec 2022 12:50:55 +0300 Subject: [PATCH 18/19] throw Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 3 +-- core/storage/face/batch_writeable.hpp | 2 +- core/storage/face/generic_maps.hpp | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index 1a61cbe103..93c9fc82da 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -37,8 +37,7 @@ namespace kagome::common { bool owned() const { if (variant.which() == 2) { - // moved with `.into()` - abort(); + throw std::logic_error{"Tried to use moved BufferOrView"}; } return variant.which() == 1; } diff --git a/core/storage/face/batch_writeable.hpp b/core/storage/face/batch_writeable.hpp index 93b544b771..666a078358 100644 --- a/core/storage/face/batch_writeable.hpp +++ b/core/storage/face/batch_writeable.hpp @@ -28,7 +28,7 @@ namespace kagome::storage::face { * efficiently write bulk data. */ virtual std::unique_ptr> batch() { - abort(); + throw std::logic_error{"BatchWriteable::batch not implemented"}; } }; diff --git a/core/storage/face/generic_maps.hpp b/core/storage/face/generic_maps.hpp index 6ecb2859da..0077ae8099 100644 --- a/core/storage/face/generic_maps.hpp +++ b/core/storage/face/generic_maps.hpp @@ -27,7 +27,7 @@ namespace kagome::storage::face { * @return size in bytes */ virtual size_t size() const { - abort(); + throw std::logic_error{"GenericStorage::size not implemented"}; } }; From af0ab8557e24c05197eb3a9bc00fdbd98663895b Mon Sep 17 00:00:00 2001 From: turuslan Date: Tue, 6 Dec 2022 13:02:41 +0300 Subject: [PATCH 19/19] comments Signed-off-by: turuslan --- core/common/buffer_or_view.hpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/common/buffer_or_view.hpp b/core/common/buffer_or_view.hpp index 93c9fc82da..ec9fa2b4b9 100644 --- a/core/common/buffer_or_view.hpp +++ b/core/common/buffer_or_view.hpp @@ -13,6 +13,7 @@ #include "common/buffer.hpp" namespace kagome::common { + /// Moved owned buffer or readonly view. class BufferOrView { using Span = gsl::span; template @@ -35,6 +36,7 @@ namespace kagome::common { BufferOrView &operator=(const BufferOrView &) = delete; BufferOrView &operator=(BufferOrView &&) = default; + /// Is buffer owned. bool owned() const { if (variant.which() == 2) { throw std::logic_error{"Tried to use moved BufferOrView"}; @@ -42,6 +44,7 @@ namespace kagome::common { return variant.which() == 1; } + /// Get view. BufferView view() const { if (!owned()) { return boost::get(variant); @@ -49,15 +52,17 @@ namespace kagome::common { return BufferView{boost::get(variant)}; } + /// Get view. operator BufferView() const { return view(); } + /// Byte size. size_t size() const { return view().size(); } - // get mutable buffer reference, copy once if view + /// Get mutable buffer reference. Copy once if view. Buffer &mut() { if (!owned()) { auto view = boost::get(variant); @@ -66,7 +71,7 @@ namespace kagome::common { return boost::get(variant); } - // move buffer away, copy once if view + /// Move buffer away. Copy once if view. Buffer into() { auto buffer = std::move(mut()); variant = Moved{};