Skip to content

Commit

Permalink
Fix finalization after conclude dispute against candidate (#1809)
Browse files Browse the repository at this point in the history
* fix: finalization after conclusion against candidate
* fix: processing of active leaves update
* refactor: remove unused member of block executor
* feature: marking block as reverted
* refactor: TreeNode ctor and use getBlockInfo
* refactor: rename best *leaf* to best *block*
* feature: method for force refresh best block
* feature: force refresh best block if needed after some block reversion
* refactor: BlockTree::getBestContaining for considering reverted block
* refactor: BlockTree test
* fix: processing of on chain disputes
* refactor: use bestBlock() instead getBestChain() where it is possible

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>

---------

Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon authored Sep 29, 2023
1 parent d3474ba commit fd8d4b7
Show file tree
Hide file tree
Showing 42 changed files with 647 additions and 549 deletions.
2 changes: 1 addition & 1 deletion core/api/service/author/impl/author_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace kagome::api {
outcome::result<common::Buffer> AuthorApiImpl::rotateKeys() {
OUTCOME_TRY(encoded_session_keys,
keys_api_->generate_session_keys(
block_tree_.get()->bestLeaf().hash, std::nullopt));
block_tree_.get()->bestBlock().hash, std::nullopt));
return encoded_session_keys;
}

Expand Down
5 changes: 3 additions & 2 deletions core/api/service/impl/api_service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ namespace kagome::api {
-> outcome::result<ApiServiceImpl::PubsubSubscriptionId> {
auto &session = session_context.storage_sub;
const auto id = session->generateSubscriptionSetId();
const auto &best_block_hash = block_tree_->bestLeaf().hash;
const auto &best_block_hash = block_tree_->bestBlock().hash;
const auto &header =
block_tree_->getBlockHeader(best_block_hash);
BOOST_ASSERT(header.has_value());
Expand Down Expand Up @@ -379,7 +379,8 @@ namespace kagome::api {
const auto id = session->generateSubscriptionSetId();
session->subscribe(id, primitives::events::ChainEventType::kNewHeads);

auto header = block_tree_->getBlockHeader(block_tree_->bestLeaf().hash);
auto header =
block_tree_->getBlockHeader(block_tree_->bestBlock().hash);
if (!header.has_error()) {
session_context.messages = uploadMessagesListFromCache();
forJsonData(server_,
Expand Down
2 changes: 1 addition & 1 deletion core/api/service/payment/impl/payment_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace kagome::api {
if (at.has_value()) {
return api_->query_info(at.value(), extrinsic, len);
}
return api_->query_info(block_tree_->bestLeaf().hash, extrinsic, len);
return api_->query_info(block_tree_->bestBlock().hash, extrinsic, len);
}

} // namespace kagome::api
12 changes: 6 additions & 6 deletions core/api/service/state/impl/state_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace kagome::api {
common::Buffer data,
const std::optional<primitives::BlockHash> &opt_at) const {
auto at =
opt_at.has_value() ? opt_at.value() : block_tree_->bestLeaf().hash;
opt_at.has_value() ? opt_at.value() : block_tree_->bestBlock().hash;
return executor_->callAt(at, method, data);
}

Expand Down Expand Up @@ -134,7 +134,7 @@ namespace kagome::api {
// TODO(Harrm): Optimize once changes trie is enabled (and a warning/assert
// for now that will fire once it is, just not to forget)
auto to =
opt_to.has_value() ? opt_to.value() : block_tree_->bestLeaf().hash;
opt_to.has_value() ? opt_to.value() : block_tree_->bestBlock().hash;
if (keys.size() > static_cast<ssize_t>(kMaxKeySetSize)) {
return Error::MAX_KEY_SET_SIZE_EXCEEDED;
}
Expand Down Expand Up @@ -184,15 +184,15 @@ namespace kagome::api {
gsl::span<const common::Buffer> keys,
std::optional<primitives::BlockHash> opt_at) const {
auto at =
opt_at.has_value() ? opt_at.value() : block_tree_->bestLeaf().hash;
opt_at.has_value() ? opt_at.value() : block_tree_->bestBlock().hash;
return queryStorage(keys, at, at);
}

outcome::result<StateApi::ReadProof> StateApiImpl::getReadProof(
gsl::span<const common::Buffer> keys,
std::optional<primitives::BlockHash> opt_at) const {
auto at =
opt_at.has_value() ? opt_at.value() : block_tree_->bestLeaf().hash;
opt_at.has_value() ? opt_at.value() : block_tree_->bestBlock().hash;
std::unordered_set<common::Buffer> proof;
auto prove = [&](common::BufferView raw) { proof.emplace(raw); };
OUTCOME_TRY(header, header_repo_->getBlockHeader(at));
Expand All @@ -209,7 +209,7 @@ namespace kagome::api {
if (at) {
return runtime_core_->version(at.value());
}
return runtime_core_->version(block_tree_->bestLeaf().hash);
return runtime_core_->version(block_tree_->bestBlock().hash);
}

outcome::result<uint32_t> StateApiImpl::subscribeStorage(
Expand Down Expand Up @@ -253,7 +253,7 @@ namespace kagome::api {
}

outcome::result<std::string> StateApiImpl::getMetadata() {
OUTCOME_TRY(data, metadata_->metadata(block_tree_->bestLeaf().hash));
OUTCOME_TRY(data, metadata_->metadata(block_tree_->bestBlock().hash));
return common::hex_lower_0x(data);
}

Expand Down
2 changes: 1 addition & 1 deletion core/api/service/system/impl/system_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace kagome::api {
std::string_view account_address) const {
OUTCOME_TRY(account_id, primitives::decodeSs58(account_address, *hasher_));
OUTCOME_TRY(nonce,
account_nonce_api_->account_nonce(block_tree_->bestLeaf().hash,
account_nonce_api_->account_nonce(block_tree_->bestBlock().hash,
account_id));

return adjustNonce(account_id, nonce);
Expand Down
2 changes: 1 addition & 1 deletion core/application/modes/print_chain_info_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace kagome::application::mode {
int PrintChainInfoMode::run() const {
auto &genesis_hash = block_tree_->getGenesisBlockHash();
auto finalized = block_tree_->getLastFinalized();
auto best = block_tree_->bestLeaf();
auto best = block_tree_->bestBlock();

rapidjson::Document document;
document.SetObject();
Expand Down
2 changes: 1 addition & 1 deletion core/authority_discovery/publisher/address_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace kagome::authority_discovery {

OUTCOME_TRY(
authorities,
authority_discovery_api_->authorities(block_tree_->bestLeaf().hash));
authority_discovery_api_->authorities(block_tree_->bestBlock().hash));

auto audi_key = keys_->getAudiKeyPair(authorities);
if (not audi_key) {
Expand Down
2 changes: 1 addition & 1 deletion core/authority_discovery/query/query_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace kagome::authority_discovery {
std::unique_lock lock{mutex_};
OUTCOME_TRY(
authorities,
authority_discovery_api_->authorities(block_tree_->bestLeaf().hash));
authority_discovery_api_->authorities(block_tree_->bestBlock().hash));
OUTCOME_TRY(local_keys,
crypto_store_->getSr25519PublicKeys(
crypto::KnownKeyTypeId::KEY_TYPE_AUDI));
Expand Down
16 changes: 6 additions & 10 deletions core/blockchain/block_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef KAGOME_BLOCK_TREE_HPP
#define KAGOME_BLOCK_TREE_HPP
#pragma once

#include <cstdint>
#include <optional>
Expand Down Expand Up @@ -125,7 +124,7 @@ namespace kagome::blockchain {
* @param block_hashes is vector of reverted block hashes
*/
virtual outcome::result<void> markAsRevertedBlocks(
const std::vector<primitives::BlockInfo> &blocks) = 0;
const std::vector<primitives::BlockHash> &block_hashes) = 0;

/**
* Add a new block to the tree
Expand Down Expand Up @@ -211,10 +210,10 @@ namespace kagome::blockchain {
* Get a best leaf of the tree
* @return best leaf
*
* @note best leaf is also a result of "SelectBestChain": if we are the
* leader, we connect a block, which we constructed, to that best leaf
* @note best block is also a result of "SelectBestChain": if we are the
* leader, we connect a block, which we constructed, to that best block
*/
virtual primitives::BlockInfo bestLeaf() const = 0;
virtual primitives::BlockInfo bestBlock() const = 0;

/**
* @brief Get the most recent block of the best (longest) chain among
Expand All @@ -225,8 +224,7 @@ namespace kagome::blockchain {
* the target one) may possess
*/
virtual outcome::result<primitives::BlockInfo> getBestContaining(
const primitives::BlockHash &target_hash,
const std::optional<primitives::BlockNumber> &max_number) const = 0;
const primitives::BlockHash &target_hash) const = 0;

/**
* Get all leaves of our tree
Expand Down Expand Up @@ -260,5 +258,3 @@ namespace kagome::blockchain {
};

} // namespace kagome::blockchain

#endif // KAGOME_BLOCK_TREE_HPP
Loading

0 comments on commit fd8d4b7

Please sign in to comment.