Skip to content

Commit

Permalink
add pre_prepares in prepared_cert, requesting missing blocks upon vie…
Browse files Browse the repository at this point in the history
…w change.
  • Loading branch information
oldcold authored and VincentOCL committed Jun 11, 2019
1 parent 299f82f commit 01775ea
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 216 deletions.
18 changes: 9 additions & 9 deletions libraries/chain/include/eosio/chain/pbft.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,43 @@ namespace eosio {

void transit_to_new_view(const pbft_new_view &new_view, psm_state_ptr s);

const vector<pbft_prepare> &get_prepares_cache() const;
const vector<pbft_prepare>& get_prepares_cache() const;

void set_prepares_cache(const vector<pbft_prepare> &pcache);

const vector<pbft_commit> &get_commits_cache() const;
const vector<pbft_commit>& get_commits_cache() const;

void set_commits_cache(const vector<pbft_commit> &ccache);

const vector<pbft_view_change> &get_view_changes_cache() const;
const vector<pbft_view_change>& get_view_changes_cache() const;

void set_view_changes_cache(const vector<pbft_view_change> &vc_cache);

const uint32_t &get_current_view() const;

void set_current_view(const uint32_t &cv);

const pbft_prepared_certificate &get_prepared_certificate() const;
const pbft_prepared_certificate& get_prepared_certificate() const;

void set_prepared_certificate(const pbft_prepared_certificate &pcert);

const vector<pbft_committed_certificate> &get_committed_certificate() const;
const vector<pbft_committed_certificate>& get_committed_certificate() const;

void set_committed_certificate(const vector<pbft_committed_certificate> &ccert);

const pbft_view_changed_certificate &get_view_changed_certificate() const;
const pbft_view_changed_certificate& get_view_changed_certificate() const;

void set_view_changed_certificate(const pbft_view_changed_certificate &vc_cert);

const uint32_t &get_target_view_retries() const;
const uint32_t& get_target_view_retries() const;

void set_target_view_retries(const uint32_t &tv_reties);

const uint32_t &get_target_view() const;
const uint32_t& get_target_view() const;

void set_target_view(const uint32_t &tv);

const uint32_t &get_view_change_timer() const;
const uint32_t& get_view_change_timer() const;

void set_view_change_timer(const uint32_t &vc_timer);

Expand Down
69 changes: 39 additions & 30 deletions libraries/chain/include/eosio/chain/pbft_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <fc/bitutil.hpp>

namespace eosio {
namespace chain {
Expand All @@ -32,10 +33,13 @@ namespace eosio {

struct block_info_type {
block_id_type block_id;
block_num_type block_num = 0;

block_num_type block_num() const {
return fc::endian_reverse_u32(block_id._hash[0]);
}

bool operator==(const block_info_type &rhs) const {
return block_id == rhs.block_id && block_num == rhs.block_num;
return block_id == rhs.block_id;
}

bool operator!=(const block_info_type &rhs) const {
Expand All @@ -59,7 +63,7 @@ namespace eosio {
}

bool empty() const {
return uuid == string()
return uuid.empty()
&& sender == public_key_type()
&& chain_id == chain_id_type("");
}
Expand All @@ -82,9 +86,9 @@ namespace eosio {
}

bool operator<(const pbft_prepare &rhs) const {
if (block_info.block_num < rhs.block_info.block_num) {
if (block_info.block_num() < rhs.block_info.block_num()) {
return true;
} else if (block_info.block_num == rhs.block_info.block_num) {
} else if (block_info.block_num() == rhs.block_info.block_num()) {
return view < rhs.view;
} else {
return false;
Expand Down Expand Up @@ -124,9 +128,9 @@ namespace eosio {
}

bool operator<(const pbft_commit &rhs) const {
if (block_info.block_num < rhs.block_info.block_num) {
if (block_info.block_num() < rhs.block_info.block_num()) {
return true;
} else if (block_info.block_num == rhs.block_info.block_num) {
} else if (block_info.block_num() == rhs.block_info.block_num()) {
return view < rhs.view;
} else {
return false;
Expand Down Expand Up @@ -168,7 +172,7 @@ namespace eosio {
}

bool operator<(const pbft_checkpoint &rhs) const {
return block_info.block_num < rhs.block_info.block_num;
return block_info.block_num() < rhs.block_info.block_num();
}

digest_type digest() const {
Expand All @@ -195,7 +199,7 @@ namespace eosio {
vector<pbft_checkpoint> checkpoints;

bool operator<(const pbft_stable_checkpoint &rhs) const {
return block_info.block_num < rhs.block_info.block_num;
return block_info.block_num() < rhs.block_info.block_num();
}

bool empty() const {
Expand All @@ -208,10 +212,11 @@ namespace eosio {
explicit pbft_prepared_certificate() = default;

block_info_type block_info;
set<block_id_type> pre_prepares;
vector<pbft_prepare> prepares;

bool operator<(const pbft_prepared_certificate &rhs) const {
return block_info.block_num < rhs.block_info.block_num;
return block_info.block_num() < rhs.block_info.block_num();
}

bool empty() const {
Expand All @@ -227,7 +232,7 @@ namespace eosio {
vector<pbft_commit> commits;

bool operator<(const pbft_committed_certificate &rhs) const {
return block_info.block_num < rhs.block_info.block_num;
return block_info.block_num() < rhs.block_info.block_num();
}

bool empty() const {
Expand Down Expand Up @@ -344,15 +349,15 @@ namespace eosio {
block_id_type block_id;
block_num_type block_num = 0;
vector<pbft_prepare> prepares;
bool should_prepared = false;
bool is_prepared = false;
vector<pbft_commit> commits;
bool should_committed = false;
bool is_committed = false;
};

struct pbft_view_change_state {
pbft_view_type view;
vector<pbft_view_change> view_changes;
bool should_view_changed = false;
bool is_view_changed = false;
};

struct pbft_checkpoint_state {
Expand Down Expand Up @@ -387,7 +392,7 @@ namespace eosio {
tag<by_prepare_and_num>,
composite_key<
pbft_state,
member<pbft_state, bool, &pbft_state::should_prepared>,
member<pbft_state, bool, &pbft_state::is_prepared>,
member<pbft_state, uint32_t, &pbft_state::block_num>
>,
composite_key_compare< greater<>, greater<> >
Expand All @@ -396,7 +401,7 @@ namespace eosio {
tag<by_commit_and_num>,
composite_key<
pbft_state,
member<pbft_state, bool, &pbft_state::should_committed>,
member<pbft_state, bool, &pbft_state::is_committed>,
member<pbft_state, uint32_t, &pbft_state::block_num>
>,
composite_key_compare< greater<>, greater<> >
Expand All @@ -418,7 +423,7 @@ namespace eosio {
tag<by_count_and_view>,
composite_key<
pbft_view_change_state,
member<pbft_view_change_state, bool, &pbft_view_change_state::should_view_changed>,
member<pbft_view_change_state, bool, &pbft_view_change_state::is_view_changed>,
member<pbft_view_change_state, pbft_view_type, &pbft_view_change_state::view>
>,
composite_key_compare<greater<>, greater<>>
Expand Down Expand Up @@ -466,8 +471,12 @@ namespace eosio {

void add_pbft_prepare(pbft_prepare &p);

void mark_as_prepared(const block_id_type &bid);

void add_pbft_commit(pbft_commit &c);

void mark_as_committed(const block_id_type &bid);

void add_pbft_view_change(pbft_view_change &vc);

void add_pbft_checkpoint(pbft_checkpoint &cp);
Expand Down Expand Up @@ -567,16 +576,16 @@ namespace eosio {
signal<void(const pbft_checkpoint &)> pbft_incoming_checkpoint;

private:
controller &ctrl;
pbft_state_multi_index_type pbft_state_index;
pbft_view_state_multi_index_type view_state_index;
pbft_checkpoint_state_multi_index_type checkpoint_index;
chain_id_type chain_id = ctrl.get_chain_id();
fc::path pbft_db_dir;
fc::path checkpoints_dir;
boost::uuids::random_generator uuid_generator;
vector<block_num_type> prepare_watermarks;
flat_map<public_key_type, uint32_t> fork_schedules;
controller &ctrl;
pbft_state_multi_index_type pbft_state_index;
pbft_view_state_multi_index_type view_state_index;
pbft_checkpoint_state_multi_index_type checkpoint_index;
chain_id_type chain_id = ctrl.get_chain_id();
fc::path pbft_db_dir;
fc::path checkpoints_dir;
boost::uuids::random_generator uuid_generator;
vector<block_num_type> prepare_watermarks;
flat_map<public_key_type, block_num_type> fork_schedules;

bool is_valid_pbft_message(const pbft_message_common &common);

Expand Down Expand Up @@ -614,7 +623,7 @@ namespace eosio {
}
} /// namespace eosio::chain

FC_REFLECT(eosio::chain::block_info_type, (block_id)(block_num))
FC_REFLECT(eosio::chain::block_info_type, (block_id))
FC_REFLECT_ENUM(eosio::chain::pbft_message_type, (prepare)(commit)(checkpoint)(view_change)(new_view))

FC_REFLECT(eosio::chain::pbft_message_common, (type)(uuid)(sender)(chain_id)(timestamp))
Expand All @@ -630,6 +639,6 @@ FC_REFLECT(eosio::chain::pbft_committed_certificate,(block_info)(commits))
FC_REFLECT(eosio::chain::pbft_view_changed_certificate, (target_view)(view_changes))
FC_REFLECT(eosio::chain::pbft_stable_checkpoint, (block_info)(checkpoints))

FC_REFLECT(eosio::chain::pbft_state, (block_id)(block_num)(prepares)(should_prepared)(commits)(should_committed))
FC_REFLECT(eosio::chain::pbft_view_change_state, (view)(view_changes)(should_view_changed))
FC_REFLECT(eosio::chain::pbft_state, (block_id)(block_num)(prepares)(is_prepared)(commits)(is_committed))
FC_REFLECT(eosio::chain::pbft_view_change_state, (view)(view_changes)(is_view_changed))
FC_REFLECT(eosio::chain::pbft_checkpoint_state, (block_id)(block_num)(checkpoints)(is_stable))
30 changes: 16 additions & 14 deletions libraries/chain/pbft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ namespace eosio {

void psm_machine::transit_to_new_view(const pbft_new_view &new_view, psm_state_ptr s) {

auto valid_nv = false;
bool valid_nv;
try {
valid_nv = pbft_db.is_valid_new_view(new_view);
} catch (const fc::exception& ex) {
Expand Down Expand Up @@ -475,14 +475,15 @@ namespace eosio {
if (!new_view.committed_cert.empty()) {
auto committed_certs = new_view.committed_cert;
std::sort(committed_certs.begin(), committed_certs.end());
for (auto cp :committed_certs) {
for (auto c: cp.commits) {
for (auto cc :committed_certs) {
for (auto c: cc.commits) {
try {
pbft_db.add_pbft_commit(c);
} catch (...) {
wlog( "commit insertion failure: ${cp}", ("cp", cp));
wlog( "commit insertion failure: ${c}", ("c", c));
}
}
pbft_db.mark_as_committed(cc.block_info.block_id);
}
}

Expand All @@ -494,6 +495,7 @@ namespace eosio {
wlog("prepare insertion failure: ${p}", ("p", p));
}
}
pbft_db.mark_as_prepared(new_view.prepared_cert.block_info.block_id);
if (pbft_db.should_prepared()) {
transit_to_prepared_state(s);
return;
Expand Down Expand Up @@ -534,79 +536,79 @@ namespace eosio {
}
}

const vector<pbft_prepare> &psm_machine::get_prepares_cache() const {
const vector<pbft_prepare>& psm_machine::get_prepares_cache() const {
return cache.prepares_cache;
}

void psm_machine::set_prepares_cache(const vector<pbft_prepare> &pcache) {
cache.prepares_cache = pcache;
}

const vector<pbft_commit> &psm_machine::get_commits_cache() const {
const vector<pbft_commit>& psm_machine::get_commits_cache() const {
return cache.commits_cache;
}

void psm_machine::set_commits_cache(const vector<pbft_commit> &ccache) {
cache.commits_cache = ccache;
}

const vector<pbft_view_change> &psm_machine::get_view_changes_cache() const {
const vector<pbft_view_change>& psm_machine::get_view_changes_cache() const {
return cache.view_changes_cache;
}

void psm_machine::set_view_changes_cache(const vector<pbft_view_change> &vc_cache) {
cache.view_changes_cache = vc_cache;
}

const uint32_t &psm_machine::get_current_view() const {
const uint32_t& psm_machine::get_current_view() const {
return current_view;
}

void psm_machine::set_current_view(const uint32_t &cv) {
current_view = cv;
}

const pbft_prepared_certificate &psm_machine::get_prepared_certificate() const {
const pbft_prepared_certificate& psm_machine::get_prepared_certificate() const {
return cache.prepared_certificate;
}

void psm_machine::set_prepared_certificate(const pbft_prepared_certificate &pcert) {
cache.prepared_certificate = pcert;
}

const vector<pbft_committed_certificate> &psm_machine::get_committed_certificate() const {
const vector<pbft_committed_certificate>& psm_machine::get_committed_certificate() const {
return cache.committed_certificate;
}

void psm_machine::set_committed_certificate(const vector<pbft_committed_certificate> &ccert) {
cache.committed_certificate = ccert;
}

const pbft_view_changed_certificate &psm_machine::get_view_changed_certificate() const {
const pbft_view_changed_certificate& psm_machine::get_view_changed_certificate() const {
return cache.view_changed_certificate;
}

void psm_machine::set_view_changed_certificate(const pbft_view_changed_certificate &vc_cert) {
cache.view_changed_certificate = vc_cert;
}

const uint32_t &psm_machine::get_target_view_retries() const {
const uint32_t& psm_machine::get_target_view_retries() const {
return target_view_retries;
}

void psm_machine::set_target_view_retries(const uint32_t &tv_reties) {
target_view_retries = tv_reties;
}

const uint32_t &psm_machine::get_target_view() const {
const uint32_t& psm_machine::get_target_view() const {
return target_view;
}

void psm_machine::set_target_view(const uint32_t &tv) {
target_view = tv;
}

const uint32_t &psm_machine::get_view_change_timer() const {
const uint32_t& psm_machine::get_view_change_timer() const {
return view_change_timer;
}

Expand Down
Loading

0 comments on commit 01775ea

Please sign in to comment.