From fa8c22961ed1a4dfb87275cddba3145b07f30d2b Mon Sep 17 00:00:00 2001 From: Andrew Petelin Date: Wed, 15 Jan 2020 18:19:11 +0300 Subject: [PATCH] CYB-489 Randpa code enhancements. --- CMakeLists.txt | 6 +- CMakeModules/installer.cmake | 2 +- .../eosio/randpa_plugin/network_messages.hpp | 18 +- .../eosio/randpa_plugin/prefix_chain_tree.hpp | 133 ++++----- .../include/eosio/randpa_plugin/randpa.hpp | 255 +++++++++--------- .../eosio/randpa_plugin/randpa_plugin.hpp | 8 +- .../include/eosio/randpa_plugin/round.hpp | 96 +++---- .../include/eosio/randpa_plugin/types.hpp | 24 +- plugins/randpa_plugin/randpa_plugin.cpp | 231 ++++++++-------- .../tests/randpa_plugin_tests.cpp | 37 +-- simulator/include/randpa.hpp | 7 +- unittests/eosio_system_tester.hpp | 12 +- 12 files changed, 419 insertions(+), 410 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d81755ac2f9..12b9a364e49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -285,11 +285,11 @@ configure_file(${CMAKE_SOURCE_DIR}/libraries/yubihsm/LICENSE ${CMAKE_BINARY_DIR}/licenses/${PROJECT}/LICENSE.yubihsm COPYONLY) install(FILES LICENSE DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ COMPONENT base) -install(FILES libraries/wabt/LICENSE DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.wabt COMPONENT base) -install(FILES libraries/softfloat/COPYING.txt DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.softfloat COMPONENT base) -install(FILES libraries/wasm-jit/LICENSE DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.wavm COMPONENT base) install(FILES libraries/fc/secp256k1/upstream/COPYING DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.secp256k1 COMPONENT base) install(FILES libraries/fc/src/network/LICENSE.go DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ COMPONENT base) +install(FILES libraries/softfloat/COPYING.txt DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.softfloat COMPONENT base) +install(FILES libraries/wabt/LICENSE DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.wabt COMPONENT base) +install(FILES libraries/wasm-jit/LICENSE DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.wavm COMPONENT base) install(FILES libraries/yubihsm/LICENSE DESTINATION ${CMAKE_INSTALL_FULL_DATAROOTDIR}/licenses/${PROJECT}/ RENAME LICENSE.yubihsm COMPONENT base) add_custom_target(base-install diff --git a/CMakeModules/installer.cmake b/CMakeModules/installer.cmake index 53190d44536..6c0680e80e3 100644 --- a/CMakeModules/installer.cmake +++ b/CMakeModules/installer.cmake @@ -43,7 +43,7 @@ else() set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE) set(CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION TRUE) - set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/mixbytes/daobet") + set(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/DaoCasino/DAObet") endif() include(CPack) diff --git a/plugins/randpa_plugin/include/eosio/randpa_plugin/network_messages.hpp b/plugins/randpa_plugin/include/eosio/randpa_plugin/network_messages.hpp index 8ec8d4d4e88..964977b4a11 100644 --- a/plugins/randpa_plugin/include/eosio/randpa_plugin/network_messages.hpp +++ b/plugins/randpa_plugin/include/eosio/randpa_plugin/network_messages.hpp @@ -8,17 +8,24 @@ namespace randpa_finality { +/// Randpa-specific network messages. +/// +/// @tparam T Particular RANDPA message type stored in a message. template class network_msg { public: T data; std::vector signatures; + + // + network_msg() = default; network_msg(const T& data_, const std::vector& signatures_): data(data_), signatures(signatures_) {} network_msg(const T& data_, std::vector&& signatures_): data(data_), signatures(signatures_) {} - network_msg(const T& data_, const std::vector& signature_providers) { - data = data_; - for(const auto &sig_prov : signature_providers) { + network_msg(const T& data_, const std::vector& signature_providers) + : data{data_} + { + for (const auto& sig_prov : signature_providers) { signatures.push_back(sig_prov(hash())); } } @@ -32,6 +39,8 @@ class network_msg { std::vector public_keys() const { std::vector public_keys; + public_keys.reserve(signatures.size()); + for (const auto& sign : signatures) { public_keys.push_back(public_key_type(sign, hash())); } @@ -43,6 +52,7 @@ class network_msg { } }; +// Various message types for RANDPA finality functioning. struct handshake_type { block_id_type lib; @@ -55,7 +65,7 @@ struct handshake_ans_type { struct prevote_type { uint32_t round_num; block_id_type base_block; - std::vector blocks; + block_ids_type blocks; }; struct precommit_type { diff --git a/plugins/randpa_plugin/include/eosio/randpa_plugin/prefix_chain_tree.hpp b/plugins/randpa_plugin/include/eosio/randpa_plugin/prefix_chain_tree.hpp index 1c44c802f24..eb85f80ad7f 100644 --- a/plugins/randpa_plugin/include/eosio/randpa_plugin/prefix_chain_tree.hpp +++ b/plugins/randpa_plugin/include/eosio/randpa_plugin/prefix_chain_tree.hpp @@ -1,43 +1,37 @@ #pragma once #include "types.hpp" -#include -#include + #include +#include #include -#include +#include namespace randpa_finality { -using std::vector; -using std::shared_ptr; -using std::weak_ptr; -using std::pair; -using std::make_pair; -using std::set; -using std::map; - template class prefix_node { public: using conf_type = ConfType; private: using node_type = prefix_node; - using node_ptr = shared_ptr; + using node_ptr = std::shared_ptr; public: block_id_type block_id; - std::map> confirmation_data; - vector adjacent_nodes; - weak_ptr parent; + std::map> confirmation_data; + std::vector adjacent_nodes; + std::weak_ptr parent; public_key_type creator_key; - set active_bp_keys; + std::set active_bp_keys; + + // size_t confirmation_number() const { return confirmation_data.size(); } - node_ptr get_matching_node(block_id_type block_id) { + node_ptr get_matching_node(block_id_type block_id) const { for (const auto& node : adjacent_nodes) { if (node->block_id == block_id) { return node; @@ -46,35 +40,44 @@ class prefix_node { return nullptr; } - bool has_confirmation(const public_key_type& pub_key ) { + bool has_confirmation(const public_key_type& pub_key) const { return confirmation_data.find(pub_key) != confirmation_data.end(); } }; + struct chain_type { block_id_type base_block; - vector blocks; + block_ids_type blocks; }; + class NodeNotFoundError : public std::exception {}; + template class prefix_chain_tree { -private: - using node_ptr = shared_ptr; - using node_weak_ptr = weak_ptr; - using conf_ptr = shared_ptr; + using node_ptr = std::shared_ptr; + using node_unique_ptr = std::unique_ptr; + using node_weak_ptr = std::weak_ptr; + using conf_ptr = std::shared_ptr; struct node_info { node_ptr node; size_t height; }; + std::map last_inserted_block; + std::map block_index; + // lifetime(node) < lifetime(block_index) => destroy the node first + node_ptr root; + node_weak_ptr head_block; + public: - explicit prefix_chain_tree(node_ptr&& root_) { + explicit prefix_chain_tree(node_unique_ptr&& root_) { // some dummy code for root to actually be erased from block_index NodeType *raw_ptr = new NodeType(*root_); - root = shared_ptr(raw_ptr, [this](NodeType *ptr) { + root = std::shared_ptr(raw_ptr, [this](NodeType *ptr) { this->block_index.erase(ptr->block_id); delete ptr; }); @@ -83,28 +86,34 @@ class prefix_chain_tree { prefix_chain_tree() = delete; prefix_chain_tree(const prefix_chain_tree&) = delete; - auto find(const block_id_type& block_id) const { + node_ptr find(const block_id_type& block_id) const { auto itr = block_index.find(block_id); return itr != block_index.end() ? itr->second.lock() : nullptr; } - node_ptr add_confirmations(const chain_type& chain, const public_key_type& sender_key, const conf_ptr& conf) { + node_ptr add_confirmations(const chain_type& chain, + const public_key_type& sender_key, + const conf_ptr& conf) { node_ptr node = nullptr; - vector blocks; + block_ids_type blocks; std::tie(node, blocks) = get_tree_node(chain); if (!node) { return nullptr; } return _add_confirmations(node, blocks, sender_key, conf); + + } void remove_confirmations() { _remove_confirmations(root); } - void insert(const chain_type& chain, const public_key_type& creator_key, const set& active_bp_keys) { + void insert(const chain_type& chain, + const public_key_type& creator_key, + const std::set& active_bp_keys) { node_ptr node = nullptr; - vector blocks; + block_ids_type blocks; std::tie(node, blocks) = get_tree_node(chain); if (!node) { @@ -114,28 +123,28 @@ class prefix_chain_tree { insert_blocks(node, blocks, creator_key, active_bp_keys); } - auto get_final_chain_head(size_t confirmation_number) const { + node_ptr get_final_chain_head(size_t confirmation_number) const { auto head = get_chain_head(root, confirmation_number, 0).node; return head != root ? head : nullptr; } - auto get_root() const { + node_ptr get_root() const { return root; } - auto set_root(const node_ptr& new_root) { + void set_root(const node_ptr& new_root) { root = new_root; root->parent.reset(); } - auto get_head() const { + node_ptr get_head() const { if (!head_block.lock()) { return root; } return head_block.lock(); } - node_ptr get_last_inserted_block(const public_key_type& pub_key) { + node_ptr get_last_inserted_block(const public_key_type& pub_key) const { auto iterator = last_inserted_block.find(pub_key); if (iterator != last_inserted_block.end()) { auto node = iterator->second; @@ -150,7 +159,7 @@ class prefix_chain_tree { chain_type chain { root->block_id, {} }; while (last_node != root) { chain.blocks.push_back(last_node->block_id); - FC_ASSERT(!last_node->parent.expired(), "parent should be exists"); + FC_ASSERT(!last_node->parent.expired(), "parent expired"); last_node = last_node->parent.lock(); } std::reverse(chain.blocks.begin(), chain.blocks.end()); @@ -159,28 +168,22 @@ class prefix_chain_tree { } private: - map last_inserted_block; - map block_index; - // lifetime(node) < lifetime(block_index) => destroy the node first - node_ptr root; - node_weak_ptr head_block; - - pair > get_tree_node(const chain_type& chain) { + std::pair get_tree_node(const chain_type& chain) const { auto node = find(chain.base_block); const auto& blocks = chain.blocks; if (node) { - return {node, std::move(chain.blocks)}; + return { node, std::move(chain.blocks) }; } auto block_itr = std::find_if(blocks.begin(), blocks.end(), [&](const auto& block) { - return (bool) find(block); + return static_cast(find(block)); }); if (block_itr != blocks.end()) { - return { find(*block_itr), vector(block_itr + 1, blocks.end()) }; + return { find(*block_itr), block_ids_type(block_itr + 1, blocks.end()) }; } - return {nullptr, {} }; + return { nullptr, {} }; } node_info get_chain_head(const node_ptr& node, size_t confirmation_number, size_t depth) const { @@ -197,22 +200,26 @@ class prefix_chain_tree { return result; } - void insert_blocks(node_ptr node, const vector& blocks, const public_key_type& creator_key, - const set& active_bp_keys) { + void insert_blocks(node_ptr node, + const block_ids_type& blocks, + const public_key_type& creator_key, + const std::set& active_bp_keys) { for (const auto& block_id : blocks) { auto next_node = node->get_matching_node(block_id); if (!next_node) { - next_node = shared_ptr(new NodeType{block_id, - {}, - {}, - node, - creator_key, - active_bp_keys}, - [this](NodeType *node) { - this->block_index.erase(node->block_id); - delete node; - }); - block_index[block_id] = weak_ptr(next_node); + next_node = std::shared_ptr( + new NodeType{block_id, + {}, + {}, + node, + creator_key, + active_bp_keys}, + [this](NodeType *node) { + this->block_index.erase(node->block_id); + delete node; + } + ); + block_index[block_id] = std::weak_ptr(next_node); node->adjacent_nodes.push_back(next_node); } node = next_node; @@ -224,8 +231,10 @@ class prefix_chain_tree { } } - node_ptr _add_confirmations(node_ptr node, const vector& blocks, const public_key_type& sender_key, - const conf_ptr& conf) { + node_ptr _add_confirmations(node_ptr node, + const block_ids_type& blocks, + const public_key_type& sender_key, + const conf_ptr& conf) { auto max_conf_node = node; node->confirmation_data[sender_key] = conf; diff --git a/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa.hpp b/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa.hpp index 357760b131a..be98ab5239f 100644 --- a/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa.hpp +++ b/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa.hpp @@ -1,56 +1,55 @@ #pragma once + #include "network_messages.hpp" #include "round.hpp" #include #include +#include #include #include -#include -#include -#include #include +#include +#include #include +#include #include -#include namespace randpa_finality { using ::fc::static_variant; -using std::shared_ptr; -using std::unique_ptr; -using std::pair; using mutex_guard = std::lock_guard; const fc::string randpa_logger_name("randpa_plugin"); fc::logger randpa_logger; -#define randpa_dlog( FORMAT, ... ) \ - FC_MULTILINE_MACRO_BEGIN \ - if( randpa_logger.is_enabled( fc::log_level::debug ) ) \ - randpa_logger.log( FC_LOG_MESSAGE( debug, FORMAT, __VA_ARGS__ ) ); \ - FC_MULTILINE_MACRO_END - -#define randpa_ilog( FORMAT, ... ) \ - FC_MULTILINE_MACRO_BEGIN \ - if( randpa_logger.is_enabled( fc::log_level::info ) ) \ - randpa_logger.log( FC_LOG_MESSAGE( info, FORMAT, __VA_ARGS__ ) ); \ - FC_MULTILINE_MACRO_END - -#define randpa_wlog( FORMAT, ... ) \ - FC_MULTILINE_MACRO_BEGIN \ - if( randpa_logger.is_enabled( fc::log_level::warn ) ) \ - randpa_logger.log( FC_LOG_MESSAGE( warn, FORMAT, __VA_ARGS__ ) ); \ - FC_MULTILINE_MACRO_END - -#define randpa_elog( FORMAT, ... ) \ - FC_MULTILINE_MACRO_BEGIN \ - if( randpa_logger.is_enabled( fc::log_level::error ) ) \ - randpa_logger.log( FC_LOG_MESSAGE( error, FORMAT, __VA_ARGS__ ) ); \ - FC_MULTILINE_MACRO_END +#define randpa_dlog(FORMAT, ...) \ + FC_MULTILINE_MACRO_BEGIN \ + if (randpa_logger.is_enabled(fc::log_level::debug)) \ + randpa_logger.log(FC_LOG_MESSAGE(debug, FORMAT, __VA_ARGS__)); \ + FC_MULTILINE_MACRO_END + +#define randpa_ilog(FORMAT, ...) \ + FC_MULTILINE_MACRO_BEGIN \ + if (randpa_logger.is_enabled(fc::log_level::info)) \ + randpa_logger.log(FC_LOG_MESSAGE(info, FORMAT, __VA_ARGS__)); \ + FC_MULTILINE_MACRO_END + +#define randpa_wlog(FORMAT, ...) \ + FC_MULTILINE_MACRO_BEGIN \ + if (randpa_logger.is_enabled(fc::log_level::warn)) \ + randpa_logger.log(FC_LOG_MESSAGE(warn, FORMAT, __VA_ARGS__)); \ + FC_MULTILINE_MACRO_END + +#define randpa_elog(FORMAT, ...) \ + FC_MULTILINE_MACRO_BEGIN \ + if (randpa_logger.is_enabled(fc::log_level::error)) \ + randpa_logger.log( FC_LOG_MESSAGE(error, FORMAT, __VA_ARGS__)); \ + FC_MULTILINE_MACRO_END + template class message_queue { @@ -58,6 +57,7 @@ class message_queue { using message_ptr = std::shared_ptr; public: + /// Add message to the queue. template void push_message(const T& msg) { mutex_guard lock(_message_queue_mutex); @@ -70,59 +70,63 @@ class message_queue { } } + /// Extract next message, or return nullptr (if empty). message_ptr get_next_msg() { mutex_guard lock(_message_queue_mutex); - if (!_message_queue.size()) { + if (_message_queue.empty()) { _need_notify = true; return nullptr; } else { _need_notify = false; } - auto msg = _message_queue.front(); + const auto msg = _message_queue.front(); _message_queue.pop(); - return msg; } + /// Extract next message (if there is no one, wait until it appears in the queue). message_ptr get_next_msg_wait() { while (true) { if (_need_notify) { std::unique_lock lk(_message_queue_mutex); _new_msg_cond.wait(lk, [this](){ - return (bool)_message_queue.size() || _done; + return !_message_queue.empty() || _done; }); } - if (_done) + if (_done) { return nullptr; + } auto msg = get_next_msg(); - if (msg) { return msg; } } } + /// Finish working with queue. void terminate() { _done = true; _new_msg_cond.notify_one(); } - auto size() { + /// Get queue size. + size_t size() const { mutex_guard lock(_message_queue_mutex); return _message_queue.size(); } private: - std::mutex _message_queue_mutex; std::atomic _need_notify { true }; - std::condition_variable _new_msg_cond; std::queue _message_queue; std::atomic _done { false }; + + mutable std::mutex _message_queue_mutex; + std::condition_variable _new_msg_cond; }; @@ -146,6 +150,7 @@ class channel { }; +/// RANDPA-specific network messages. struct randpa_net_msg { uint32_t ses_id; randpa_net_msg_data data; @@ -169,13 +174,15 @@ struct on_new_peer_event { }; using randpa_event_data = static_variant; + +/// External events. struct randpa_event { randpa_event_data data; }; -using randpa_message = static_variant; -using randpa_message_ptr = shared_ptr; +using randpa_message = static_variant; +using randpa_message_ptr = std::shared_ptr; using net_channel = channel; using net_channel_ptr = std::shared_ptr; @@ -187,6 +194,7 @@ using finality_channel = channel; using finality_channel_ptr = std::shared_ptr; using lru_cache_type = boost::compute::detail::lru_cache; + class randpa { public: static constexpr uint32_t round_width = 2; @@ -195,10 +203,10 @@ class randpa { public: randpa() - : _peer_messages{_messages_cache_size}, - _self_messages{_messages_cache_size}, - _last_proofs{_proofs_cache_size} { - } + : _peer_messages{_messages_cache_size} + , _self_messages{_messages_cache_size} + , _last_proofs{_proofs_cache_size} + {} randpa& set_in_net_channel(const net_channel_ptr& ptr) { _in_net_channel = ptr; @@ -220,8 +228,8 @@ class randpa { return *this; } - randpa& set_signature_providers(const vector& signature_providers, - const vector& public_keys) { + randpa& set_signature_providers(const std::vector& signature_providers, + const std::vector& public_keys) { _signature_providers = signature_providers; _public_keys = public_keys; _provided_bp_key = true; @@ -229,8 +237,7 @@ class randpa { return *this; } - void add_signature_provider(const signature_provider_type& signature_provider, - const public_key_type& public_key) { + void add_signature_provider(const signature_provider_type& signature_provider, const public_key_type& public_key) { _signature_providers.push_back(signature_provider); _public_keys.push_back(public_key); _provided_bp_key = true; @@ -238,9 +245,9 @@ class randpa { } void start(prefix_tree_ptr tree) { - FC_ASSERT(_in_net_channel && _in_event_channel, "in channels should be inited"); - FC_ASSERT(_out_net_channel, "out channels should be inited"); - FC_ASSERT(_finality_channel, "finality channel should be inited"); + FC_ASSERT(_in_net_channel && _in_event_channel, "input channels should be initialized"); + FC_ASSERT(_out_net_channel, "output channel should be initialized"); + FC_ASSERT(_finality_channel, "finality channel should be initialized"); _prefix_tree = tree; _lib = tree->get_root()->block_id; @@ -265,7 +272,7 @@ class randpa { } #ifndef SYNC_RANDPA - auto& get_message_queue() { + const message_queue& get_message_queue() const { return _message_queue; } #endif @@ -316,6 +323,8 @@ class randpa { event_channel_ptr _in_event_channel; finality_channel_ptr _finality_channel; + // + void subscribe() { _in_net_channel->subscribe([&](const randpa_net_msg& msg) { randpa_dlog("Randpa received net message, type: ${type}", ("type", msg.data.which())); @@ -352,7 +361,7 @@ class randpa { if (_peer_messages.contains(msg_hash)) { return; } - for (const auto& peer: _peers) { + for (const auto& peer : _peers) { send(peer.second, msg); } _peer_messages.insert(msg_hash, {}); @@ -384,26 +393,27 @@ class randpa { // need handle all messages void process_msg(randpa_message_ptr msg_ptr) { - auto msg = *msg_ptr; + const auto msg = *msg_ptr; switch (msg.which()) { - case randpa_message::tag::value: - process_net_msg(msg.get()); - break; - case randpa_message::tag::value: - process_event(msg.get()); - break; - default: - randpa_wlog("Randpa received unknown message, type: ${type}", ("type", msg.which())); - break; + case randpa_message::tag::value: + process_net_msg(msg.get()); + break; + case randpa_message::tag::value: + process_event(msg.get()); + break; + default: + randpa_wlog("Randpa received unknown message, type: ${type}", ("type", msg.which())); + break; } } + /// Visitor for RANDPA various message types. class net_msg_handler: public fc::visitor { public: - net_msg_handler(randpa& randpa_ref, uint32_t ses_id): - _randpa(randpa_ref), - _ses_id(ses_id) - { } + net_msg_handler(randpa& randpa_ref, uint32_t ses_id) + : _randpa(randpa_ref) + , _ses_id(ses_id) + {} template void operator() (const T& msg) { @@ -427,34 +437,33 @@ class randpa { msg.data.visit(visitor); } - void process_event(const randpa_event& event){ + void process_event(const randpa_event& event) { const auto& data = event.data; switch (data.which()) { - case randpa_event_data::tag::value: - on(data.get()); - break; - case randpa_event_data::tag::value: - on(data.get()); - break; - case randpa_event_data::tag::value: - on(data.get()); - break; - default: - randpa_wlog("Randpa event received, but handler not found, type: ${type}", - ("type", data.which()) - ); - break; - } - } - - bool validate_prevote(const prevote_type& prevote, const public_key_type& prevoter_key, - const block_id_type& best_block, const set& bp_keys) { + case randpa_event_data::tag::value: + on(data.get()); + break; + case randpa_event_data::tag::value: + on(data.get()); + break; + case randpa_event_data::tag::value: + on(data.get()); + break; + default: + randpa_wlog("Randpa event received, but handler not found, type: ${type}", ("type", data.which())); + break; + } + } + + bool validate_prevote(const prevote_type& prevote, + const public_key_type& prevoter_key, + const block_id_type& best_block, + const std::set& bp_keys) const { if (prevote.base_block != best_block && std::find(prevote.blocks.begin(), prevote.blocks.end(), best_block) == prevote.blocks.end()) { randpa_dlog("Best block: ${id} was not found in prevote blocks", ("id", best_block)); } else if (!bp_keys.count(prevoter_key)) { - randpa_dlog("Prevoter public key is not in active bp keys: ${pub_key}", - ("pub_key", prevoter_key)); + randpa_dlog("Prevoter public key is not in active bp keys: ${pub_key}", ("pub_key", prevoter_key)); } else { return true; } @@ -462,15 +471,16 @@ class randpa { return false; } - bool validate_precommit(const precommit_type& precommit, const public_key_type& precommiter_key, - const block_id_type& best_block, const set& bp_keys) { + bool validate_precommit(const precommit_type& precommit, + const public_key_type& precommiter_key, + const block_id_type& best_block, + const std::set& bp_keys) const { if (precommit.block_id != best_block) { randpa_dlog("Precommit block ${pbid}, best block: ${bbid}", - ("pbid", precommit.block_id) - ("bbid", best_block)); + ("pbid", precommit.block_id) + ("bbid", best_block)); } else if (!bp_keys.count(precommiter_key)) { - randpa_dlog("Precommitter public key is not in active bp keys: ${pub_key}", - ("pub_key", precommiter_key)); + randpa_dlog("Precommitter public key is not in active bp keys: ${pub_key}", ("pub_key", precommiter_key)); } else { return true; } @@ -478,16 +488,16 @@ class randpa { return false; } - bool validate_proof(const proof_type& proof) { - auto best_block = proof.best_block; - auto node = _prefix_tree->find(best_block); + bool validate_proof(const proof_type& proof) const { + const auto best_block = proof.best_block; + const auto node = _prefix_tree->find(best_block); if (!node) { randpa_dlog("Received proof for unknown block: ${block_id}", ("block_id", best_block)); return false; } - set prevoted_keys, precommited_keys; + std::set prevoted_keys, precommited_keys; const auto& bp_keys = node->active_bp_keys; for (const auto& prevote : proof.prevotes) { @@ -555,22 +565,22 @@ class randpa { if (_last_prooved_block_num >= get_block_num(proof.best_block)) { randpa_dlog("Skipping proof for ${id} cause last prooved block ${lpb} is higher", - ("id", proof.best_block) - ("lpb", _last_prooved_block_num)); + ("id", proof.best_block) + ("lpb", _last_prooved_block_num)); return; } if (get_block_num(_lib) >= get_block_num(proof.best_block)) { randpa_dlog("Skipping proof for ${id} cause lib ${lib} is higher", - ("id", proof.best_block) - ("lib", _lib)); + ("id", proof.best_block) + ("lib", _lib)); return; } - if (_round && _round->get_state() == randpa_round::state::done) { + if (_round && _round->get_state() == randpa_round::state_type::done) { randpa_dlog("Skipping proof for ${id} cause round ${num} is finished", - ("id", proof.best_block) - ("num", _round->get_num())); + ("id", proof.best_block) + ("num", _round->get_num())); return; } @@ -586,7 +596,7 @@ class randpa { if (_round && _round->get_num() == proof.round_num) { randpa_dlog("Gotta proof for round ${num}", ("num", _round->get_num())); - _round->set_state(randpa_round::state::done); + _round->set_state(randpa_round::state_type::done); } on_proof_gained(proof); } @@ -596,7 +606,6 @@ class randpa { randpa_ilog("Randpa handshake_msg received, ses_id: ${ses_id}, from: ${pk}", ("ses_id", ses_id)("pk", public_key)); try { _peers[public_key] = ses_id; - send(ses_id, handshake_ans_msg(handshake_ans_type { _lib }, _signature_providers)); } catch (const fc::exception& e) { randpa_elog("Randpa handshake_msg handler error, reason: ${e}", ("e", e.what())); @@ -626,8 +635,7 @@ class randpa { try { _prefix_tree->insert({event.prev_block_id, {event.block_id}}, event.creator_key, event.active_bp_keys); - } - catch (const NodeNotFoundError& e) { + } catch (const NodeNotFoundError& e) { randpa_elog("Randpa cannot insert block into tree, base_block: ${base_id}, block: ${id}", ("base_id", event.prev_block_id) ("id", event.block_id) @@ -695,16 +703,14 @@ class randpa { return; } - auto msg_hash = digest_type::hash(msg); - + const auto msg_hash = digest_type::hash(msg); if (_self_messages.contains(msg_hash)) { return; } _self_messages.insert(msg_hash, {}); - auto last_round_num = round_num(_prefix_tree->get_head()->block_id); - + const auto last_round_num = round_num(_prefix_tree->get_head()->block_id); if (last_round_num == msg.data.round_num) { bcast(msg); } @@ -747,8 +753,9 @@ class randpa { } bool is_active_bp(const block_id_type& block_id) const { - if (!_provided_bp_key) + if (!_provided_bp_key) { return false; + } auto node_ptr = _prefix_tree->find(block_id); @@ -789,15 +796,10 @@ class randpa { void new_round(uint32_t round_num, const public_key_type& primary) { _round.reset(new randpa_round(round_num, primary, _prefix_tree, _signature_providers, - [this](const prevote_msg& msg) { - bcast(msg); - }, - [this](const precommit_msg& msg) { - bcast(msg); - }, - [this]() { - finish_round(); - })); + [this](const prevote_msg& msg) { bcast(msg); }, + [this](const precommit_msg& msg) { bcast(msg); }, + [this]() { finish_round(); } + )); } void remove_round() { @@ -812,8 +814,7 @@ class randpa { if (node_ptr) { _prefix_tree->set_root(node_ptr); - } - else { + } else { auto new_irb = std::make_shared(tree_node { lib_id }); _prefix_tree->set_root(new_irb); } diff --git a/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa_plugin.hpp b/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa_plugin.hpp index c700f6ebebf..05c851d0411 100644 --- a/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa_plugin.hpp +++ b/plugins/randpa_plugin/include/eosio/randpa_plugin/randpa_plugin.hpp @@ -1,11 +1,12 @@ #pragma once + #include #include namespace eosio { -using namespace appbase; - +/// Randpa plugin. +/// class randpa_plugin : public appbase::plugin { public: randpa_plugin(); @@ -18,8 +19,9 @@ class randpa_plugin : public appbase::plugin { void plugin_startup(); void plugin_shutdown(); void handle_sighup() override; + private: std::unique_ptr my; }; -} +} // namespace eosio diff --git a/plugins/randpa_plugin/include/eosio/randpa_plugin/round.hpp b/plugins/randpa_plugin/include/eosio/randpa_plugin/round.hpp index 78cf3f57828..7983ad38a9c 100644 --- a/plugins/randpa_plugin/include/eosio/randpa_plugin/round.hpp +++ b/plugins/randpa_plugin/include/eosio/randpa_plugin/round.hpp @@ -10,13 +10,14 @@ using tree_node = prefix_node; using prefix_tree = prefix_chain_tree; using tree_node_ptr = std::shared_ptr; +using tree_node_unique_ptr = std::unique_ptr; using prefix_tree_ptr = std::shared_ptr; using randpa_round_ptr = std::shared_ptr; class randpa_round { public: - enum class state { + enum class state_type { init, // init -> prevote prevote, // prevote -> ready_to_precommit | fail ready_to_precommit, // ready_to_precommit -> precommit @@ -30,22 +31,35 @@ class randpa_round { using precommit_bcaster_type = std::function; using done_cb_type = std::function; + uint32_t num { 0 }; + public_key_type primary; + prefix_tree_ptr tree; + state_type state { state_type::init }; + proof_type proof; + tree_node_ptr best_node; + std::vector signature_providers; + prevote_bcaster_type prevote_bcaster; + precommit_bcaster_type precommit_bcaster; + done_cb_type done_cb; + + std::set prevoted_keys; + std::set precommited_keys; + public: randpa_round(uint32_t num, - const public_key_type& primary, - const prefix_tree_ptr& tree, - const std::vector& signature_providers, - prevote_bcaster_type && prevote_bcaster, - precommit_bcaster_type && precommit_bcaster, - done_cb_type && done_cb - ) : - num(num), - primary(primary), - tree(tree), - signature_providers(signature_providers), - prevote_bcaster(std::move(prevote_bcaster)), - precommit_bcaster(std::move(precommit_bcaster)), - done_cb(std::move(done_cb)) + const public_key_type& primary, + const prefix_tree_ptr& tree, + const std::vector& signature_providers, + prevote_bcaster_type && prevote_bcaster, + precommit_bcaster_type && precommit_bcaster, + done_cb_type && done_cb) + : num{num} + , primary{primary} + , tree{tree} + , signature_providers{signature_providers} + , prevote_bcaster{std::move(prevote_bcaster)} + , precommit_bcaster{std::move(precommit_bcaster)} + , done_cb{std::move(done_cb)} { dlog("Randpa round started, num: ${n}, primary: ${p}", ("n", num) @@ -59,22 +73,22 @@ class randpa_round { return num; } - state get_state() const { + state_type get_state() const { return state; } - void set_state(const state& s) { + void set_state(const state_type& s) { state = s; } proof_type get_proof() { - FC_ASSERT(state == state::done, "state should be `done`"); + FC_ASSERT(state == state_type::done, "state should be `done`"); return proof; } void on(const prevote_msg& msg) { - if (state != state::prevote && state != state::ready_to_precommit) { + if (state != state_type::prevote && state != state_type::ready_to_precommit) { dlog("Skipping prevote, round: ${r}", ("r", num)); return; } @@ -88,7 +102,7 @@ class randpa_round { } void on(const precommit_msg& msg) { - if (state != state::precommit && state != state::ready_to_precommit) { + if (state != state_type::precommit && state != state_type::ready_to_precommit) { dlog("Skipping precommit, round: ${r}", ("r", num)); return; } @@ -102,12 +116,12 @@ class randpa_round { } void end_prevote() { - if (state != state::ready_to_precommit) { + if (state != state_type::ready_to_precommit) { dlog("Round failed, num: ${n}, state: ${s}", ("n", num) ("s", static_cast(state)) ); - state = state::fail; + state = state_type::fail; return; } @@ -121,12 +135,12 @@ class randpa_round { } bool finish() { - if (state != state::done) { + if (state != state_type::done) { dlog("Round failed, num: ${n}, state: ${s}", ("n", num) ("s", static_cast(state)) ); - state = state::fail; + state = state_type::fail; return false; } return true; @@ -134,8 +148,8 @@ class randpa_round { private: void prevote() { - FC_ASSERT(state == state::init, "state should be `init`"); - state = state::prevote; + FC_ASSERT(state == state_type::init, "state should be `init`"); + state = state_type::prevote; auto last_node = tree->get_last_inserted_block(primary); @@ -153,8 +167,8 @@ class randpa_round { } void precommit() { - FC_ASSERT(state == state::ready_to_precommit, "state should be `ready_to_precommit`"); - state = state::precommit; + FC_ASSERT(state == state_type::ready_to_precommit, "state should be `ready_to_precommit`"); + state = state_type::precommit; auto precommit = precommit_type { num, best_node->block_id }; auto msg = precommit_msg(precommit, signature_providers); @@ -249,7 +263,7 @@ class randpa_round { ); if (has_threshold_prevotes(max_prevote_node)) { - state = state::ready_to_precommit; + state = state_type::ready_to_precommit; best_node = max_prevote_node; dlog("Prevote threshold reached, round: ${r}, best block: ${b}", ("r", num) @@ -270,18 +284,18 @@ class randpa_round { ("r", num) ("b", best_node->block_id) ); - state = state::done; + state = state_type::done; done_cb(); return; } } } - tree_node_ptr find_last_node(const block_id_type& base_block, const vector& blocks) { + tree_node_ptr find_last_node(const block_id_type& base_block, const block_ids_type& blocks) { auto block_itr = std::find_if(blocks.rbegin(), blocks.rend(), - [&](const auto& block_id) { - return (bool) tree->find(block_id); - }); + [&](const auto& block_id) { + return (bool) tree->find(block_id); + }); if (block_itr == blocks.rend()) { return tree->find(base_block); @@ -293,20 +307,6 @@ class randpa_round { bool has_threshold_prevotes(const tree_node_ptr& node) { return node->confirmation_number() > 2 * node->active_bp_keys.size() / 3; } - - uint32_t num { 0 }; - public_key_type primary; - prefix_tree_ptr tree; - state state { state::init }; - proof_type proof; - tree_node_ptr best_node; - std::vector signature_providers; - prevote_bcaster_type prevote_bcaster; - precommit_bcaster_type precommit_bcaster; - done_cb_type done_cb; - - std::set prevoted_keys; - std::set precommited_keys; }; } //namespace randpa_finality diff --git a/plugins/randpa_plugin/include/eosio/randpa_plugin/types.hpp b/plugins/randpa_plugin/include/eosio/randpa_plugin/types.hpp index 69f560e2178..20b7dc52106 100644 --- a/plugins/randpa_plugin/include/eosio/randpa_plugin/types.hpp +++ b/plugins/randpa_plugin/include/eosio/randpa_plugin/types.hpp @@ -1,20 +1,20 @@ #pragma once +#include +#include #include -#include -#include +#include +#include #include +#include +#include +#include +#include #include #include -#include -#include -#include -#include #include -#include -#include -#include -#include +#include +#include namespace randpa_finality { @@ -25,8 +25,10 @@ using block_id_type = fc::sha256; using digest_type = fc::sha256; using signature_provider_type = std::function; +using block_ids_type = std::vector; + int32_t get_block_num(const block_id_type& id) { return fc::endian_reverse_u32(id._hash[0]); } -} //namespace randpa_finality \ No newline at end of file +} //namespace randpa_finality diff --git a/plugins/randpa_plugin/randpa_plugin.cpp b/plugins/randpa_plugin/randpa_plugin.cpp index bda7011caf3..5b33b03391b 100644 --- a/plugins/randpa_plugin/randpa_plugin.cpp +++ b/plugins/randpa_plugin/randpa_plugin.cpp @@ -1,19 +1,21 @@ #include + +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include #include -#include -#include + +#include +#include +#include namespace eosio { @@ -24,27 +26,28 @@ using namespace randpa_finality; static appbase::abstract_plugin& _randpa_plugin = app().register_plugin(); - static constexpr uint32_t net_message_types_base = 100; class randpa_plugin_impl { public: - randpa_plugin_impl() {} - randpa _randpa; channels::irreversible_block::channel_type::handle _on_irb_handle; - channels::accepted_block::channel_type::handle _on_accepted_block_handle; - net_plugin::new_peer::channel_type::handle _on_new_peer_handle; + channels::accepted_block::channel_type::handle _on_accepted_block_handle; + net_plugin::new_peer::channel_type::handle _on_new_peer_handle; + + // + + randpa_plugin_impl() {} template static constexpr uint32_t get_net_msg_type(const T& msg = {}) { return net_message_types_base + randpa_net_msg_data::tag::value; } - static auto get_bp_keys(block_state_ptr s) { + static std::set get_bp_keys(block_state_ptr s) { std::set producer_keys; - for (const auto& elem : s->active_schedule.producers) { + for (const auto& elem : s->active_schedule.producers) { producer_keys.insert(elem.block_signing_key); } return producer_keys; @@ -71,82 +74,69 @@ class randpa_plugin_impl { subscribe(in_net_ch); _on_accepted_block_handle = app().get_channel() - .subscribe( [ev_ch, this]( block_state_ptr s ) { - app().get_plugin().update_gauge("randpa_queue_size", _randpa.get_message_queue().size()); - app().get_plugin().update_gauge("head_block_num", get_block_num(_randpa.get_prefix_tree()->get_head()->block_id)); - ev_ch->send(randpa_event { on_accepted_block_event { + .subscribe( [ev_ch, this]( block_state_ptr s ) { + app().get_plugin().update_gauge("randpa_queue_size", _randpa.get_message_queue().size()); + app().get_plugin().update_gauge("head_block_num", get_block_num(_randpa.get_prefix_tree()->get_head()->block_id)); + ev_ch->send(randpa_event { on_accepted_block_event { s->id, s->header.previous, s->block_signing_key, get_bp_keys(s), is_sync(s) - } }); - }); + }}); + }); _on_irb_handle = app().get_channel() - .subscribe( [ev_ch]( block_state_ptr s ) { - app().get_plugin() - .update_gauge("lib_block_num", s->block_num); - - ev_ch->send(randpa_event { on_irreversible_event { s->id } }); - }); + .subscribe( [ev_ch]( block_state_ptr s ) { + app().get_plugin().update_gauge("lib_block_num", s->block_num); + ev_ch->send(randpa_event { on_irreversible_event { s->id } }); + }); _on_new_peer_handle = app().get_channel() - .subscribe( [ev_ch]( uint32_t ses_id ) { - ev_ch->send(randpa_event { on_new_peer_event { ses_id } }); - }); + .subscribe( [ev_ch]( uint32_t ses_id ) { + ev_ch->send(randpa_event { on_new_peer_event { ses_id } }); + }); out_net_ch->subscribe([this](const randpa_net_msg& msg) { - auto data = msg.data; + const auto data = msg.data; switch (data.which()) { - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_prevote_cnt"); - break; - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_precommit_cnt"); - break; - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_proof_cnt"); - break; - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_handshake_cnt"); - break; - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_handshake_ans_cnt"); - break; - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_finality_notice_cnt"); - break; - case randpa_net_msg_data::tag::value: - send(msg.ses_id, data.get()); - app().get_plugin() - .update_counter("randpa_net_out_finality_req_proof_cnt"); - break; - default: - randpa_wlog("randpa message sent, but handler not found, type: ${type}", - ("type", data.which()) - ); - break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_prevote_cnt"); + break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_precommit_cnt"); + break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_proof_cnt"); + break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_handshake_cnt"); + break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_handshake_ans_cnt"); + break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_finality_notice_cnt"); + break; + case randpa_net_msg_data::tag::value: + send(msg.ses_id, data.get()); + app().get_plugin().update_counter("randpa_net_out_finality_req_proof_cnt"); + break; + default: + randpa_wlog("randpa message sent, but handler not found, type: ${type}", ("type", data.which())); + break; } - app().get_plugin() - .update_counter("randpa_net_out_total_cnt"); + app().get_plugin().update_counter("randpa_net_out_total_cnt"); }); finality_ch->subscribe([this](const block_id_type& block_id) { app().get_io_service().post([block_id = block_id]() { - app().get_plugin() .chain() .bft_finalize(block_id); @@ -182,13 +172,15 @@ class randpa_plugin_impl { return fc::time_point::now() - block->header.timestamp > fc::seconds(2); } - prefix_tree_ptr copy_fork_db() { + static prefix_tree_ptr copy_fork_db() { const auto& ctrl = app().get_plugin().chain(); - auto lib_id = ctrl.last_irreversible_block_id(); + const auto lib_id = ctrl.last_irreversible_block_id(); + randpa_dlog("Initializing prefix_chain_tree with ${lib_id}", ("lib_id", lib_id)); - prefix_tree_ptr tree(new prefix_tree(std::make_shared(tree_node { lib_id }))); - randpa_dlog("Copying master chain from fork_db"); + auto root = std::make_unique(tree_node{lib_id}); + prefix_tree_ptr tree(new prefix_tree(std::move(root))); + randpa_dlog("Copying master chain from fork_db"); auto current_block = ctrl.head_block_state(); vector blocks; @@ -200,7 +192,7 @@ class randpa_plugin_impl { auto base_block = lib_id; for (const auto& block_ptr : blocks) { - auto block_id = block_ptr->id; + const auto block_id = block_ptr->id; tree->insert(chain_type{base_block, {block_ptr->id}}, block_ptr->block_signing_key, get_bp_keys(block_ptr)); @@ -215,52 +207,44 @@ class randpa_plugin_impl { } template - void send(uint32_t ses_id, const T& msg) { + static void send(uint32_t ses_id, const T& msg) { app().post(priority::high, [ses_id, msg]() { - app().get_plugin() - .send(ses_id, get_net_msg_type(msg), msg); + app().get_plugin().send(ses_id, get_net_msg_type(msg), msg); }); } template - void subscribe(const net_channel_ptr& ch) { - app().get_plugin() - .subscribe(get_net_msg_type(), - [ch](uint32_t ses_id, const T & msg) { - ch->send(randpa_net_msg { ses_id, msg, fc::time_point::now() }); - switch (randpa_net_msg_data::tag::value) { + static void subscribe(const net_channel_ptr& ch) { + app().get_plugin().subscribe( + get_net_msg_type(), + [ch](uint32_t ses_id, const T& msg) { + ch->send(randpa_net_msg { ses_id, msg, fc::time_point::now() }); + switch (randpa_net_msg_data::tag::value) { case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_prevote_cnt"); + app().get_plugin().update_counter("randpa_net_in_prevote_cnt"); break; case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_precommit_cnt"); + app().get_plugin().update_counter("randpa_net_in_precommit_cnt"); break; case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_proof_cnt"); + app().get_plugin().update_counter("randpa_net_in_proof_cnt"); break; case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_handshake_cnt"); + app().get_plugin().update_counter("randpa_net_in_handshake_cnt"); break; case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_handshake_ans_cnt"); + app().get_plugin().update_counter("randpa_net_in_handshake_ans_cnt"); break; case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_finality_notice_cnt"); + app().get_plugin().update_counter("randpa_net_in_finality_notice_cnt"); break; case randpa_net_msg_data::tag::value: - app().get_plugin() - .update_counter("randpa_net_in_finality_req_proof_cnt"); + app().get_plugin().update_counter("randpa_net_in_finality_req_proof_cnt"); break; + } + app().get_plugin().update_counter("randpa_net_in_total_cnt"); } - app().get_plugin() - .update_counter("randpa_net_in_total_cnt"); - }); + ); } }; @@ -270,26 +254,27 @@ randpa_plugin::~randpa_plugin() {} static signature_provider_type make_key_signature_provider(const private_key_type& key) { - return [key]( const chain::digest_type& digest ) { - return key.sign(digest); - }; + return [key]( const chain::digest_type& digest ) { + return key.sign(digest); + }; } static signature_provider_type make_keosd_signature_provider(const string& url_str, const public_key_type& pubkey) { - fc::url keosd_url; - if(boost::algorithm::starts_with(url_str, "unix://")) - //send the entire string after unix:// to http_plugin. It'll auto-detect which part - // is the unix socket path, and which part is the url to hit on the server - keosd_url = fc::url("unix", url_str.substr(7), ostring(), ostring(), ostring(), ostring(), ovariant_object(), fc::optional()); - else - keosd_url = fc::url(url_str); - - return [keosd_url, pubkey]( const chain::digest_type& digest ) { - fc::variant params; - fc::to_variant(std::make_pair(digest, pubkey), params); - auto deadline = fc::time_point::maximum(); - return app().get_plugin().get_client().post_sync(keosd_url, params, deadline).as(); - }; + fc::url keosd_url; + if (boost::algorithm::starts_with(url_str, "unix://")) { + //send the entire string after unix:// to http_plugin. It'll auto-detect which part + // is the unix socket path, and which part is the url to hit on the server + keosd_url = fc::url("unix", url_str.substr(7), ostring(), ostring(), ostring(), ostring(), ovariant_object(), fc::optional()); + } else { + keosd_url = fc::url(url_str); + } + + return [keosd_url, pubkey]( const chain::digest_type& digest ) { + fc::variant params; + fc::to_variant(std::make_pair(digest, pubkey), params); + auto deadline = fc::time_point::maximum(); + return app().get_plugin().get_client().post_sync(keosd_url, params, deadline).as(); + }; } void randpa_plugin::plugin_initialize(const variables_map& options) { diff --git a/plugins/randpa_plugin/tests/randpa_plugin_tests.cpp b/plugins/randpa_plugin/tests/randpa_plugin_tests.cpp index d8eac3ba5fd..f0e05912d06 100644 --- a/plugins/randpa_plugin/tests/randpa_plugin_tests.cpp +++ b/plugins/randpa_plugin/tests/randpa_plugin_tests.cpp @@ -10,13 +10,16 @@ using namespace eosio; using namespace chain; -using std::vector; using namespace fc::crypto; using namespace randpa_finality; +using std::vector; + using tree_node = prefix_node; using prefix_tree = prefix_chain_tree; +using block_ids_type = vector; + static signature_provider_type make_key_signature_provider(const private_key_type& key) { return [key]( const digest_type& digest ) { return key.sign(digest); @@ -29,20 +32,18 @@ inline auto get_pub_key() { BOOST_AUTO_TEST_SUITE(prefix_chain_tree_tests) -using blocks_type = vector; - BOOST_AUTO_TEST_CASE(prefix_chain_one_node) try { auto lib_block_id = fc::sha256("beef"); - auto root = std::make_shared(tree_node{lib_block_id}); + auto root = std::make_unique(tree_node{lib_block_id}); prefix_tree tree(std::move(root)); BOOST_REQUIRE_EQUAL(nullptr, tree.get_final_chain_head(1)); } FC_LOG_AND_RETHROW() BOOST_AUTO_TEST_CASE(prefix_chain_two_nodes) try { auto lib_block_id = fc::sha256("beef"); - auto root = std::make_shared(tree_node{lib_block_id}); + auto root = std::make_unique(tree_node{lib_block_id}); auto chain = chain_type{lib_block_id, - vector{fc::sha256("a")}}; + block_ids_type{fc::sha256("a")}}; prefix_tree tree(std::move(root)); tree.insert(chain, get_pub_key(), {}); tree.add_confirmations(chain, get_pub_key(), 0); @@ -62,25 +63,25 @@ BOOST_AUTO_TEST_CASE(prefix_chain_test_longest) try { auto pub_key_1 = get_pub_key(); auto pub_key_2 = get_pub_key(); auto lib_block_id = fc::sha256("beef"); - auto root = std::make_shared(tree_node{lib_block_id}); + auto root = std::make_unique(tree_node{lib_block_id}); prefix_tree tree(std::move(root)); std::map blocks; for (char c = 'a'; c <= 'd'; c++) { blocks[c] = fc::sha256(std::string{c}); } - auto chain1 = chain_type { lib_block_id, blocks_type{blocks['a'], blocks['b']} }; - auto chain2 = chain_type { blocks['a'], blocks_type{blocks['c'], blocks['d']} }; + auto chain1 = chain_type { lib_block_id, block_ids_type{blocks['a'], blocks['b']} }; + auto chain2 = chain_type { blocks['a'], block_ids_type{blocks['c'], blocks['d']} }; tree.insert(chain1, pub_key_1, {}); tree.add_confirmations(chain1, pub_key_1, 0); tree.insert(chain2, pub_key_1, {}); tree.add_confirmations(chain2, pub_key_1, 0); - auto chain3 = chain_type {lib_block_id, vector{blocks['a'], blocks['b']}}; + auto chain3 = chain_type {lib_block_id, block_ids_type{blocks['a'], blocks['b']}}; tree.insert(chain3, pub_key_2, {}); tree.add_confirmations(chain3, pub_key_2, 0); BOOST_TEST(blocks['b'] == tree.get_final_chain_head(2)->block_id); - auto chain4 = chain_type {blocks['c'], blocks_type{blocks['d']}}; + auto chain4 = chain_type {blocks['c'], block_ids_type{blocks['d']}}; tree.insert(chain4, pub_key_2, {}); tree.add_confirmations(chain4, pub_key_2, 0); BOOST_TEST(blocks['d'] == tree.get_final_chain_head(2)->block_id); @@ -94,8 +95,8 @@ BOOST_AUTO_TEST_CASE(prefix_chain_internal) try { auto lib_block_id = fc::sha256("beef"); auto node = tree_node{lib_block_id}; - prefix_tree tree(std::make_shared(node)); - auto chain = chain_type{lib_block_id, blocks_type{fc::sha256("abc"), fc::sha256("def")}}; + prefix_tree tree(std::make_unique(node)); + auto chain = chain_type{lib_block_id, block_ids_type{fc::sha256("abc"), fc::sha256("def")}}; tree.insert(chain, pub_key_1, {}); tree.add_confirmations(chain, pub_key_1, 0); @@ -108,7 +109,7 @@ BOOST_AUTO_TEST_CASE(prefix_chain_internal) try { BOOST_TEST(chain_first_node->adjacent_nodes[0] == tree.get_final_chain_head(1)); // add second chain - chain = chain_type{fc::sha256("abc"), blocks_type{fc::sha256("bbc")}}; + chain = chain_type{fc::sha256("abc"), block_ids_type{fc::sha256("bbc")}}; tree.insert(chain, pub_key_2, {}); tree.add_confirmations(chain, pub_key_2, 0); @@ -219,7 +220,7 @@ BOOST_AUTO_TEST_SUITE(last_inserted_block_test) BOOST_AUTO_TEST_CASE(get_last_inserted_block) try { auto lib_block_id = fc::sha256("beef"); - auto root = std::make_shared(tree_node{lib_block_id}); + auto root = std::make_unique(tree_node{lib_block_id}); auto chain1 = chain_type{lib_block_id, {fc::sha256("a")}}; auto chain2 = chain_type{fc::sha256("a"), {fc::sha256("b")}}; auto pub_key1 = get_pub_key(); @@ -241,9 +242,9 @@ BOOST_AUTO_TEST_SUITE(remove_confirmations) BOOST_AUTO_TEST_CASE(remove_confirmations_test) try { auto lib_block_id = fc::sha256("beef"); - auto root = std::make_shared(tree_node{lib_block_id}); + auto root = std::make_unique(tree_node{lib_block_id}); auto chain = chain_type{lib_block_id, - vector{fc::sha256("a")}}; + block_ids_type{fc::sha256("a")}}; prefix_tree tree(std::move(root)); tree.insert(chain, get_pub_key(), {}); tree.add_confirmations(chain, get_pub_key(), 0); @@ -256,4 +257,4 @@ BOOST_AUTO_TEST_CASE(remove_confirmations_test) try { BOOST_TEST(!head); } FC_LOG_AND_RETHROW() -BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file +BOOST_AUTO_TEST_SUITE_END() diff --git a/simulator/include/randpa.hpp b/simulator/include/randpa.hpp index e42d9b36be2..773468956f8 100644 --- a/simulator/include/randpa.hpp +++ b/simulator/include/randpa.hpp @@ -27,9 +27,8 @@ class RandpaNode: public Node { Node(id, std::move(net), std::move(db_), std::move(private_key)) { init(); - prefix_tree_ptr tree(new prefix_tree(std::make_shared(tree_node { - db.last_irreversible_block_id() - }))); + tree_node_unique_ptr root = std::make_unique(tree_node { db.last_irreversible_block_id() });; + prefix_tree_ptr tree(new prefix_tree(std::move(root))); randpa_impl->start(tree); } @@ -53,7 +52,7 @@ class RandpaNode: public Node { } prefix_tree_ptr copy_fork_db() { - tree_node_ptr root = std::make_shared(tree_node { db.last_irreversible_block_id() });; + tree_node_unique_ptr root = std::make_unique(tree_node { db.last_irreversible_block_id() });; prefix_tree_ptr tree(new prefix_tree(std::move(root))); std::queue q; q.push(db.get_root()); diff --git a/unittests/eosio_system_tester.hpp b/unittests/eosio_system_tester.hpp index 8c06f187910..fa071b7a4fe 100644 --- a/unittests/eosio_system_tester.hpp +++ b/unittests/eosio_system_tester.hpp @@ -18,11 +18,11 @@ using namespace fc; using mvo = fc::mutable_variant_object; #ifndef TESTER -#ifdef NON_VALIDATING_TEST -#define TESTER tester -#else -#define TESTER validating_tester -#endif +# ifdef NON_VALIDATING_TEST +# define TESTER tester +# else +# define TESTER validating_tester +# endif #endif namespace eosio_system { @@ -246,7 +246,7 @@ class eosio_system_tester : public TESTER { ("receiver", to) ("stake_net_quantity", net) ("stake_cpu_quantity", cpu) - ("transfer", 0 ) + ("transfer", 0) ); }