Skip to content

Commit

Permalink
Merge pull request EOSIO#87 from eosiosg/feature/dpos-pbft-bos-upgrade
Browse files Browse the repository at this point in the history
Feature/dpos pbft bos upgrade
  • Loading branch information
Thaipanda authored Apr 30, 2019
2 parents 739ec29 + 57e14e4 commit d3e4f5f
Show file tree
Hide file tree
Showing 48 changed files with 4,886 additions and 176 deletions.
2 changes: 1 addition & 1 deletion contracts/eosio.system/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ namespace eosiosystem {

auto fee = ( tokens_out.amount + 199 ) / 200; /// .5% fee (round up)
// since tokens_out.amount was asserted to be at least 2 earlier, fee.amount < tokens_out.amount

if( fee > 0 ) {
INLINE_ACTION_SENDER(eosio::token, transfer)( N(eosio.token), {account,N(active)},
{ account, N(eosio.ramfee), asset(fee), std::string("sell ram fee") } );
Expand Down
2 changes: 1 addition & 1 deletion contracts/eosio.system/eosio.system.abi
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,4 @@
],
"ricardian_clauses": [],
"abi_extensions": []
}
}
2 changes: 1 addition & 1 deletion contracts/eosio.system/exchange_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace eosiosystem {

uint64_t primary_key()const { return supply.symbol; }

asset convert_to_exchange( connector& c, asset in );
asset convert_to_exchange( connector& c, asset in );
asset convert_from_exchange( connector& c, asset in );
asset convert( asset from, symbol_type to );

Expand Down
7 changes: 7 additions & 0 deletions contracts/eosiolib/eosiolib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ namespace eosio {
ds >> params;
}

void set_upgrade_parameters(const eosio::upgrade_parameters& params) {
char buf[sizeof(eosio::upgrade_parameters)];
eosio::datastream<char *> ds( buf, sizeof(buf) );
ds << params;
set_upgrade_parameters_packed( buf, ds.tellp() );
}

using ::memset;
using ::memcpy;

Expand Down
1 change: 1 addition & 0 deletions contracts/eosiolib/privileged.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ extern "C" {
*/
uint32_t get_blockchain_parameters_packed(char* data, uint32_t datalen);

void set_upgrade_parameters_packed(char* data, uint32_t datalen);
/**
* @brief Activate new feature
* Activate new feature
Expand Down
10 changes: 10 additions & 0 deletions contracts/eosiolib/privileged.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ namespace eosio {
)
};

struct upgrade_parameters {
uint32_t target_block_num;

EOSLIB_SERIALIZE(upgrade_parameters,
(target_block_num)
)
};

/**
* @brief Set the blockchain parameters
* Set the blockchain parameters
Expand All @@ -122,6 +130,8 @@ namespace eosio {
*/
void get_blockchain_parameters(eosio::blockchain_parameters& params);

void set_upgrade_parameters(const eosio::upgrade_parameters& params);

///@} priviledgedcppapi

/**
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ add_library( eosio_chain
block_header_state.cpp
block_state.cpp
fork_database.cpp
pbft_database.cpp
pbft.cpp
controller.cpp
authorization_manager.cpp
resource_limits.cpp
Expand Down
77 changes: 55 additions & 22 deletions libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace eosio { namespace chain {
* contain a transaction mroot, action mroot, or new_producers as those components
* are derived from chain state.
*/
block_header_state block_header_state::generate_next( block_timestamp_type when )const {
block_header_state block_header_state::generate_next( block_timestamp_type when, bool new_version )const {
block_header_state result;

if( when != block_timestamp_type() ) {
Expand Down Expand Up @@ -62,9 +62,17 @@ namespace eosio { namespace chain {
result.pending_schedule = pending_schedule;
result.dpos_proposed_irreversible_blocknum = dpos_proposed_irreversible_blocknum;
result.bft_irreversible_blocknum = bft_irreversible_blocknum;
result.pbft_stable_checkpoint_blocknum = pbft_stable_checkpoint_blocknum;


if (new_version) {
result.dpos_irreversible_blocknum = dpos_irreversible_blocknum;
} else {
result.producer_to_last_implied_irb[prokey.producer_name] = result.dpos_proposed_irreversible_blocknum;
result.dpos_irreversible_blocknum = result.calc_dpos_last_irreversible();
}


result.producer_to_last_implied_irb[prokey.producer_name] = result.dpos_proposed_irreversible_blocknum;
result.dpos_irreversible_blocknum = result.calc_dpos_last_irreversible();

/// grow the confirmed count
static_assert(std::numeric_limits<uint8_t>::max() >= (config::max_producers * 2 / 3) + 1, "8bit confirmations may not be able to hold all of the needed confirmations");
Expand All @@ -73,23 +81,30 @@ namespace eosio { namespace chain {
auto num_active_producers = active_schedule.producers.size();
uint32_t required_confs = (uint32_t)(num_active_producers * 2 / 3) + 1;

if( confirm_count.size() < config::maximum_tracked_dpos_confirmations ) {
result.confirm_count.reserve( confirm_count.size() + 1 );
result.confirm_count = confirm_count;
result.confirm_count.resize( confirm_count.size() + 1 );
result.confirm_count.back() = (uint8_t)required_confs;
} else {
result.confirm_count.resize( confirm_count.size() );
memcpy( &result.confirm_count[0], &confirm_count[1], confirm_count.size() - 1 );
result.confirm_count.back() = (uint8_t)required_confs;
if (!new_version) {
if (confirm_count.size() < config::maximum_tracked_dpos_confirmations) {
result.confirm_count.reserve(confirm_count.size() + 1);
result.confirm_count = confirm_count;
result.confirm_count.resize(confirm_count.size() + 1);
result.confirm_count.back() = (uint8_t) required_confs;
} else {
result.confirm_count.resize(confirm_count.size());
memcpy(&result.confirm_count[0], &confirm_count[1], confirm_count.size() - 1);
result.confirm_count.back() = (uint8_t) required_confs;
}
}

return result;
} /// generate_next

bool block_header_state::maybe_promote_pending() {
if( pending_schedule.producers.size() &&
dpos_irreversible_blocknum >= pending_schedule_lib_num )
bool block_header_state::maybe_promote_pending( bool new_version ) {

bool should_promote_pending = pending_schedule.producers.size();
if ( !new_version ) {
should_promote_pending = should_promote_pending && dpos_irreversible_blocknum >= pending_schedule_lib_num;
}

if (should_promote_pending)
{
active_schedule = move( pending_schedule );

Expand All @@ -99,7 +114,13 @@ namespace eosio { namespace chain {
if( existing != producer_to_last_produced.end() ) {
new_producer_to_last_produced[pro.producer_name] = existing->second;
} else {
new_producer_to_last_produced[pro.producer_name] = dpos_irreversible_blocknum;
//TODO: max of bft and dpos lib
if (new_version) {
new_producer_to_last_produced[pro.producer_name] = bft_irreversible_blocknum;
} else {
new_producer_to_last_produced[pro.producer_name] = dpos_irreversible_blocknum;
}

}
}

Expand All @@ -109,7 +130,13 @@ namespace eosio { namespace chain {
if( existing != producer_to_last_implied_irb.end() ) {
new_producer_to_last_implied_irb[pro.producer_name] = existing->second;
} else {
new_producer_to_last_implied_irb[pro.producer_name] = dpos_irreversible_blocknum;
//TODO: max of bft and dpos lib
if (new_version) {
new_producer_to_last_implied_irb[pro.producer_name] = bft_irreversible_blocknum;
} else {
new_producer_to_last_implied_irb[pro.producer_name] = dpos_irreversible_blocknum;
}

}
}

Expand Down Expand Up @@ -141,13 +168,13 @@ namespace eosio { namespace chain {
*
* If the header specifies new_producers then apply them accordingly.
*/
block_header_state block_header_state::next( const signed_block_header& h, bool skip_validate_signee )const {
block_header_state block_header_state::next( const signed_block_header& h, bool skip_validate_signee, bool new_version )const {
EOS_ASSERT( h.timestamp != block_timestamp_type(), block_validate_exception, "", ("h",h) );
//EOS_ASSERT( h.header_extensions.size() == 0, block_validate_exception, "no supported extensions" );

EOS_ASSERT( h.timestamp > header.timestamp, block_validate_exception, "block must be later in time" );
EOS_ASSERT( h.previous == id, unlinkable_block_exception, "block must link to current state" );
auto result = generate_next( h.timestamp );
auto result = generate_next( h.timestamp, new_version);
EOS_ASSERT( result.header.producer == h.producer, wrong_producer, "wrong producer specified" );
EOS_ASSERT( result.header.schedule_version == h.schedule_version, producer_schedule_exception, "schedule_version in signed block is corrupted" );

Expand All @@ -161,9 +188,11 @@ namespace eosio { namespace chain {
/// below this point is state changes that cannot be validated with headers alone, but never-the-less,
/// must result in header state changes

result.set_confirmed( h.confirmed );

auto was_pending_promoted = result.maybe_promote_pending();
result.set_confirmed(h.confirmed, new_version);


auto was_pending_promoted = result.maybe_promote_pending(new_version);

if( h.new_producers ) {
EOS_ASSERT( !was_pending_promoted, producer_schedule_exception, "cannot set pending producer schedule in the same block in which pending was promoted to active" );
Expand All @@ -185,14 +214,18 @@ namespace eosio { namespace chain {
return result;
} /// next

void block_header_state::set_confirmed( uint16_t num_prev_blocks ) {
void block_header_state::set_confirmed( uint16_t num_prev_blocks, bool new_version ) {
/*
idump((num_prev_blocks)(confirm_count.size()));
for( uint32_t i = 0; i < confirm_count.size(); ++i ) {
std::cerr << "confirm_count["<<i<<"] = " << int(confirm_count[i]) << "\n";
}
*/
if (new_version) {
header.confirmed = 0;
return;
}
header.confirmed = num_prev_blocks;

int32_t i = (int32_t)(confirm_count.size() - 1);
Expand Down
8 changes: 4 additions & 4 deletions libraries/chain/block_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace eosio { namespace chain {

block_state::block_state( const block_header_state& prev, block_timestamp_type when )
:block_header_state( prev.generate_next( when ) ),
block_state::block_state( const block_header_state& prev, block_timestamp_type when, bool new_version )
:block_header_state( prev.generate_next( when, new_version) ),
block( std::make_shared<signed_block>() )
{
static_cast<block_header&>(*block) = header;
}

block_state::block_state( const block_header_state& prev, signed_block_ptr b, bool skip_validate_signee )
:block_header_state( prev.next( *b, skip_validate_signee )), block( move(b) )
block_state::block_state( const block_header_state& prev, signed_block_ptr b, bool skip_validate_signee, bool new_version )
:block_header_state( prev.next( *b, skip_validate_signee, new_version)), block( move(b) )
{ }


Expand Down
Loading

0 comments on commit d3e4f5f

Please sign in to comment.