Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/api/service/impl/api_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ namespace kagome::api {
void ApiServiceImpl::onStorageEvent(SubscriptionSetId set_id,
SessionPtr &session,
const Buffer &key,
const Buffer &data,
const std::optional<Buffer> &data,
const common::Hash256 &block) {
sendEvent(server_,
session,
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/impl/api_service_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ namespace kagome::api {
void onStorageEvent(SubscriptionSetId set_id,
SessionPtr &session,
const Buffer &key,
const Buffer &data,
const std::optional<Buffer> &data,
const common::Hash256 &block);
void onChainEvent(SubscriptionSetId set_id,
SessionPtr &session,
Expand Down
4 changes: 3 additions & 1 deletion core/consensus/grandpa/impl/voting_round_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,9 @@ namespace kagome::consensus::grandpa {
return false;
}

BOOST_ASSERT(prevote_ghost_.has_value());
if (not prevote_ghost_) {
return false;
}
const auto &prevote_ghost = prevote_ghost_.value();

// anything new finalized? finalized blocks are those which have both
Expand Down
4 changes: 0 additions & 4 deletions core/host_api/impl/host_api_factory_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace kagome::host_api {

HostApiFactoryImpl::HostApiFactoryImpl(
const OffchainExtensionConfig &offchain_config,
std::shared_ptr<storage::changes_trie::ChangesTracker> tracker,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<crypto::EcdsaProvider> ecdsa_provider,
std::shared_ptr<crypto::Ed25519Provider> ed25519_provider,
Expand All @@ -23,7 +22,6 @@ namespace kagome::host_api {
offchain_persistent_storage,
std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool)
: offchain_config_(offchain_config),
changes_tracker_{std::move(tracker)},
sr25519_provider_(std::move(sr25519_provider)),
ecdsa_provider_(std::move(ecdsa_provider)),
ed25519_provider_(std::move(ed25519_provider)),
Expand All @@ -33,7 +31,6 @@ namespace kagome::host_api {
bip39_provider_(std::move(bip39_provider)),
offchain_persistent_storage_(std::move(offchain_persistent_storage)),
offchain_worker_pool_(std::move(offchain_worker_pool)) {
BOOST_ASSERT(changes_tracker_ != nullptr);
BOOST_ASSERT(sr25519_provider_ != nullptr);
BOOST_ASSERT(ed25519_provider_ != nullptr);
BOOST_ASSERT(secp256k1_provider_ != nullptr);
Expand All @@ -52,7 +49,6 @@ namespace kagome::host_api {
memory_provider,
core_provider,
storage_provider,
changes_tracker_,
sr25519_provider_,
ecdsa_provider_,
ed25519_provider_,
Expand Down
3 changes: 0 additions & 3 deletions core/host_api/impl/host_api_factory_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "crypto/secp256k1_provider.hpp"
#include "crypto/sr25519_provider.hpp"
#include "host_api/impl/offchain_extension.hpp"
#include "storage/changes_trie/changes_tracker.hpp"

namespace kagome::offchain {
class OffchainPersistentStorage;
Expand All @@ -31,7 +30,6 @@ namespace kagome::host_api {

HostApiFactoryImpl(
const OffchainExtensionConfig &offchain_config,
std::shared_ptr<storage::changes_trie::ChangesTracker> tracker,
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<crypto::EcdsaProvider> ecdsa_provider,
std::shared_ptr<crypto::Ed25519Provider> ed25519_provider,
Expand All @@ -51,7 +49,6 @@ namespace kagome::host_api {

private:
OffchainExtensionConfig offchain_config_;
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker_;
std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
std::shared_ptr<crypto::EcdsaProvider> ecdsa_provider_;
std::shared_ptr<crypto::Ed25519Provider> ed25519_provider_;
Expand Down
3 changes: 1 addition & 2 deletions core/host_api/impl/host_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace kagome::host_api {
std::shared_ptr<const runtime::MemoryProvider> memory_provider,
std::shared_ptr<const runtime::CoreApiFactory> core_provider,
std::shared_ptr<runtime::TrieStorageProvider> storage_provider,
std::shared_ptr<storage::changes_trie::ChangesTracker> tracker,
std::shared_ptr<const crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<const crypto::EcdsaProvider> ecdsa_provider,
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
Expand Down Expand Up @@ -56,7 +55,7 @@ namespace kagome::host_api {
hasher,
memory_provider_,
std::move(core_provider)},
storage_ext_(storage_provider_, memory_provider_, std::move(tracker)),
storage_ext_(storage_provider_, memory_provider_),
child_storage_ext_(storage_provider_, memory_provider_),
offchain_ext_(offchain_config,
memory_provider_,
Expand Down
1 change: 0 additions & 1 deletion core/host_api/impl/host_api_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ namespace kagome::host_api {
std::shared_ptr<const runtime::MemoryProvider> memory_provider,
std::shared_ptr<const runtime::CoreApiFactory> core_provider,
std::shared_ptr<runtime::TrieStorageProvider> storage_provider,
std::shared_ptr<storage::changes_trie::ChangesTracker> tracker,
std::shared_ptr<const crypto::Sr25519Provider> sr25519_provider,
std::shared_ptr<const crypto::EcdsaProvider> ecdsa_provider,
std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
Expand Down
63 changes: 3 additions & 60 deletions core/host_api/impl/storage_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
using kagome::common::Buffer;

namespace {
const auto CHANGES_CONFIG_KEY = kagome::common::Buffer{}.put(":changes_trie");

[[nodiscard]] kagome::storage::trie::StateVersion toStateVersion(
kagome::runtime::WasmI32 state_version_int) {
if (state_version_int == 0) {
Expand All @@ -42,15 +40,12 @@ namespace {
namespace kagome::host_api {
StorageExtension::StorageExtension(
std::shared_ptr<runtime::TrieStorageProvider> storage_provider,
std::shared_ptr<const runtime::MemoryProvider> memory_provider,
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker)
std::shared_ptr<const runtime::MemoryProvider> memory_provider)
: storage_provider_(std::move(storage_provider)),
memory_provider_(std::move(memory_provider)),
changes_tracker_{std::move(changes_tracker)},
logger_{log::createLogger("StorageExtension", "storage_extension")} {
BOOST_ASSERT_MSG(storage_provider_ != nullptr, "storage batch is nullptr");
BOOST_ASSERT_MSG(memory_provider_ != nullptr, "memory provider is nullptr");
BOOST_ASSERT_MSG(changes_tracker_ != nullptr, "changes tracker is nullptr");
}

void StorageExtension::reset() {
Expand Down Expand Up @@ -254,20 +249,9 @@ namespace kagome::host_api {

runtime::WasmSpan StorageExtension::ext_storage_changes_root_version_1(
runtime::WasmSpan parent_hash_data) {
auto parent_hash_span = runtime::PtrSize(parent_hash_data);
auto &memory = memory_provider_->getCurrentMemory()->get();
auto parent_hash_bytes =
memory.loadN(parent_hash_span.ptr, parent_hash_span.size);
common::Hash256 parent_hash;
std::copy_n(parent_hash_bytes.begin(),
common::Hash256::size(),
parent_hash.begin());

auto &&result = calcStorageChangesRoot(parent_hash);
auto &&res = result.has_value()
? std::make_optional(std::move(result.value()))
: std::nullopt;
return memory.storeBuffer(scale::encode(std::move(res)).value());
// https://github.com/paritytech/substrate/pull/10080
return memory.storeBuffer(scale::encode(std::optional<Buffer>()).value());
}

runtime::WasmSpan StorageExtension::ext_storage_next_key_version_1(
Expand Down Expand Up @@ -452,47 +436,6 @@ namespace kagome::host_api {
return runtime::PtrSize(res).ptr;
}

std::optional<common::Buffer> StorageExtension::calcStorageChangesRoot(
common::Hash256 parent_hash) const {
if (not storage_provider_->tryGetPersistentBatch()) {
logger_->error("ext_storage_changes_root persistent batch not found");
return std::nullopt;
}
auto batch = storage_provider_->tryGetPersistentBatch().value();
auto config_bytes_res = batch->tryGet(CHANGES_CONFIG_KEY);
if (config_bytes_res.has_error()) {
logger_->error("ext_storage_changes_root resulted with an error: {}",
config_bytes_res.error().message());
throw std::runtime_error(config_bytes_res.error().message());
}
if (config_bytes_res.value() == std::nullopt) {
return std::nullopt;
}
auto config_res = scale::decode<storage::changes_trie::ChangesTrieConfig>(
config_bytes_res.value().value().get());
if (config_res.has_error()) {
logger_->error("ext_storage_changes_root resulted with an error: {}",
config_res.error().message());
throw std::runtime_error(config_res.error().message());
}
storage::changes_trie::ChangesTrieConfig trie_config = config_res.value();

SL_DEBUG(logger_,
"ext_storage_changes_root constructing changes trie with "
"parent_hash: {}",
parent_hash.toHex());
auto trie_hash_res =
changes_tracker_->constructChangesTrie(parent_hash, trie_config);
if (trie_hash_res.has_error()) {
logger_->error("ext_storage_changes_root resulted with an error: {}",
trie_hash_res.error().message());
throw std::runtime_error(trie_hash_res.error().message());
}
common::Buffer result_buf(trie_hash_res.value());
SL_TRACE_FUNC_CALL(logger_, result_buf, parent_hash);
return result_buf;
}

runtime::WasmSpan StorageExtension::clearPrefix(
common::BufferView prefix, std::optional<uint32_t> limit) {
auto batch = storage_provider_->getCurrentBatch();
Expand Down
8 changes: 1 addition & 7 deletions core/host_api/impl/storage_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "log/logger.hpp"
#include "runtime/types.hpp"
#include "storage/changes_trie/changes_tracker.hpp"
#include "storage/trie/serialization/polkadot_codec.hpp"

namespace kagome::runtime {
Expand All @@ -26,8 +25,7 @@ namespace kagome::host_api {
public:
StorageExtension(
std::shared_ptr<runtime::TrieStorageProvider> storage_provider,
std::shared_ptr<const runtime::MemoryProvider> memory_provider,
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker);
std::shared_ptr<const runtime::MemoryProvider> memory_provider);

void reset();

Expand Down Expand Up @@ -163,9 +161,6 @@ namespace kagome::host_api {
outcome::result<std::optional<common::Buffer>> getStorageNextKey(
const common::Buffer &key) const;

std::optional<common::Buffer> calcStorageChangesRoot(
common::Hash256 parent) const;

runtime::WasmSpan clearPrefix(common::BufferView prefix,
std::optional<uint32_t> limit);

Expand All @@ -177,7 +172,6 @@ namespace kagome::host_api {

std::shared_ptr<runtime::TrieStorageProvider> storage_provider_;
std::shared_ptr<const runtime::MemoryProvider> memory_provider_;
std::shared_ptr<storage::changes_trie::ChangesTracker> changes_tracker_;
storage::trie::PolkadotCodec codec_;
log::Logger logger_;

Expand Down
37 changes: 2 additions & 35 deletions core/primitives/digest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "common/unused.hpp"
#include "primitives/scheduled_change.hpp"
#include "scale/scale.hpp"
#include "storage/changes_trie/changes_trie_config.hpp"

namespace kagome::primitives {
/// Consensus engine unique ID.
Expand All @@ -31,38 +30,6 @@ namespace kagome::primitives {
inline const auto kUnsupportedEngineId_BEEF =
ConsensusEngineId::fromString("BEEF").value();

/// System digest item that contains the root of changes trie at given
/// block. It is created for every block iff runtime supports changes
/// trie creation.
struct ChangesTrieRoot : public common::Hash256 {};

// TODO (kamilsa): workaround unless we bump gtest version to 1.8.1+
// after gtest update use `data` type directly
struct ChangesTrieSignal {
boost::variant<std::optional<storage::changes_trie::ChangesTrieConfig>>
data;

bool operator==(const ChangesTrieSignal &rhs) const {
return data == rhs.data;
}

bool operator!=(const ChangesTrieSignal &rhs) const {
return !operator==(rhs);
}
};

template <class Stream,
typename = std::enable_if_t<Stream::is_encoder_stream>>
Stream &operator<<(Stream &s, const ChangesTrieSignal &sig) {
return s << sig.data;
}

template <class Stream,
typename = std::enable_if_t<Stream::is_decoder_stream>>
Stream &operator>>(Stream &s, ChangesTrieSignal &sig) {
return s >> sig.data;
}

struct Other : public common::Buffer {};

namespace detail {
Expand Down Expand Up @@ -220,12 +187,12 @@ namespace kagome::primitives {
/// https://github.com/paritytech/substrate/blob/polkadot-v0.9.12/primitives/runtime/src/generic/digest.rs#L272
using DigestItem = boost::variant<Other, // 0
Unused<1>, // 1
ChangesTrieRoot, // 2
Unused<2>, // 2
Unused<3>, // 3
Consensus, // 4
Seal, // 5
PreRuntime, // 6
ChangesTrieSignal, // 7
Unused<7>, // 7
RuntimeEnvironmentUpdated>; // 8

/**
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/event_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ namespace kagome::primitives::events {
using StorageSubscriptionEngine =
subscription::SubscriptionEngine<common::Buffer,
std::shared_ptr<api::Session>,
common::Buffer,
std::optional<common::Buffer>,
primitives::BlockHash>;
using StorageSubscriptionEnginePtr =
std::shared_ptr<StorageSubscriptionEngine>;
Expand Down
7 changes: 2 additions & 5 deletions core/runtime/runtime_api/impl/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ namespace kagome::runtime {
const primitives::Block &block) {
OUTCOME_TRY(parent, header_repo_->getBlockHeader(block.header.parent_hash));
BOOST_ASSERT(parent.number == block.header.number - 1);
OUTCOME_TRY(changes_tracker_->onBlockExecutionStart(
block.header.parent_hash, parent.number));
changes_tracker_->onBlockExecutionStart(block.header.parent_hash);
OUTCOME_TRY(executor_->persistentCallAt<void>(
block.header.parent_hash, "Core_execute_block", block));
return outcome::success();
}

outcome::result<storage::trie::RootHash> CoreImpl::initialize_block(
const primitives::BlockHeader &header) {
OUTCOME_TRY(changes_tracker_->onBlockExecutionStart(
header.parent_hash,
header.number - 1)); // parent's number
changes_tracker_->onBlockExecutionStart(header.parent_hash);
const auto res = executor_->persistentCallAt<void>(
header.parent_hash, "Core_initialize_block", header);
if (res.has_value()) {
Expand Down
1 change: 0 additions & 1 deletion core/storage/changes_trie/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

add_library(changes_tracker
impl/storage_changes_tracker_impl.cpp
impl/changes_trie.cpp
)
target_link_libraries(changes_tracker
buffer
Expand Down
Loading