Skip to content

Commit

Permalink
Merge pull request #44 from DaoCasino/randpa-code-enhancement-489
Browse files Browse the repository at this point in the history
WIP: CYB-489 Randpa code enhancements.
  • Loading branch information
adrianopol authored Jan 22, 2020
2 parents 5c13ee0 + fa8c229 commit 9711a42
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 410 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion CMakeModules/installer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@

namespace randpa_finality {

/// Randpa-specific network messages.
///
/// @tparam T Particular RANDPA message type stored in a message.
template<class T>
class network_msg {
public:
T data;
std::vector<signature_type> signatures;

//

network_msg() = default;
network_msg(const T& data_, const std::vector<signature_type>& signatures_): data(data_), signatures(signatures_) {}
network_msg(const T& data_, std::vector<signature_type>&& signatures_): data(data_), signatures(signatures_) {}
network_msg(const T& data_, const std::vector<signature_provider_type>& signature_providers) {
data = data_;
for(const auto &sig_prov : signature_providers) {
network_msg(const T& data_, const std::vector<signature_provider_type>& signature_providers)
: data{data_}
{
for (const auto& sig_prov : signature_providers) {
signatures.push_back(sig_prov(hash()));
}
}
Expand All @@ -32,6 +39,8 @@ class network_msg {

std::vector<public_key_type> public_keys() const {
std::vector<public_key_type> public_keys;
public_keys.reserve(signatures.size());

for (const auto& sign : signatures) {
public_keys.push_back(public_key_type(sign, hash()));
}
Expand All @@ -43,6 +52,7 @@ class network_msg {
}
};

// Various message types for RANDPA finality functioning.

struct handshake_type {
block_id_type lib;
Expand All @@ -55,7 +65,7 @@ struct handshake_ans_type {
struct prevote_type {
uint32_t round_num;
block_id_type base_block;
std::vector<block_id_type> blocks;
block_ids_type blocks;
};

struct precommit_type {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
#pragma once

#include "types.hpp"
#include <memory>
#include <vector>

#include <map>
#include <memory>
#include <set>
#include <queue>
#include <vector>

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 <typename ConfType>
class prefix_node {
public:
using conf_type = ConfType;
private:
using node_type = prefix_node<conf_type>;
using node_ptr = shared_ptr<node_type>;
using node_ptr = std::shared_ptr<node_type>;

public:
block_id_type block_id;
std::map<public_key_type, shared_ptr<conf_type>> confirmation_data;
vector<node_ptr> adjacent_nodes;
weak_ptr<node_type> parent;
std::map<public_key_type, std::shared_ptr<conf_type>> confirmation_data;
std::vector<node_ptr> adjacent_nodes;
std::weak_ptr<node_type> parent;
public_key_type creator_key;
set<public_key_type> active_bp_keys;
std::set<public_key_type> 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;
Expand All @@ -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<block_id_type> blocks;
block_ids_type blocks;
};


class NodeNotFoundError : public std::exception {};


template <typename NodeType>
class prefix_chain_tree {
private:
using node_ptr = shared_ptr<NodeType>;
using node_weak_ptr = weak_ptr<NodeType>;
using conf_ptr = shared_ptr<typename NodeType::conf_type>;
using node_ptr = std::shared_ptr<NodeType>;
using node_unique_ptr = std::unique_ptr<NodeType>;
using node_weak_ptr = std::weak_ptr<NodeType>;
using conf_ptr = std::shared_ptr<typename NodeType::conf_type>;

struct node_info {
node_ptr node;
size_t height;
};

std::map<public_key_type, node_weak_ptr> last_inserted_block;
std::map<block_id_type, node_weak_ptr> 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<NodeType>(raw_ptr, [this](NodeType *ptr) {
root = std::shared_ptr<NodeType>(raw_ptr, [this](NodeType *ptr) {
this->block_index.erase(ptr->block_id);
delete ptr;
});
Expand All @@ -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<block_id_type> 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<public_key_type>& active_bp_keys) {
void insert(const chain_type& chain,
const public_key_type& creator_key,
const std::set<public_key_type>& active_bp_keys) {
node_ptr node = nullptr;
vector<block_id_type> blocks;
block_ids_type blocks;
std::tie(node, blocks) = get_tree_node(chain);

if (!node) {
Expand All @@ -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;
Expand All @@ -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());
Expand All @@ -159,28 +168,22 @@ class prefix_chain_tree {
}

private:
map<public_key_type, node_weak_ptr> last_inserted_block;
map<block_id_type, node_weak_ptr> block_index;
// lifetime(node) < lifetime(block_index) => destroy the node first
node_ptr root;
node_weak_ptr head_block;

pair<node_ptr, vector<block_id_type> > get_tree_node(const chain_type& chain) {
std::pair<node_ptr, block_ids_type> 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<bool>(find(block));
});

if (block_itr != blocks.end()) {
return { find(*block_itr), vector<block_id_type>(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 {
Expand All @@ -197,22 +200,26 @@ class prefix_chain_tree {
return result;
}

void insert_blocks(node_ptr node, const vector<block_id_type>& blocks, const public_key_type& creator_key,
const set<public_key_type>& active_bp_keys) {
void insert_blocks(node_ptr node,
const block_ids_type& blocks,
const public_key_type& creator_key,
const std::set<public_key_type>& 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<NodeType>(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<NodeType>(next_node);
next_node = std::shared_ptr<NodeType>(
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<NodeType>(next_node);
node->adjacent_nodes.push_back(next_node);
}
node = next_node;
Expand All @@ -224,8 +231,10 @@ class prefix_chain_tree {
}
}

node_ptr _add_confirmations(node_ptr node, const vector<block_id_type>& 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;

Expand Down
Loading

0 comments on commit 9711a42

Please sign in to comment.