Skip to content

Commit

Permalink
add actionseed, global action sequence (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
winlin authored and qianxiaofeng committed Nov 21, 2018
1 parent 13ae2b0 commit df2422b
Show file tree
Hide file tree
Showing 25 changed files with 1,236 additions and 41 deletions.
22 changes: 22 additions & 0 deletions contracts/eosiolib/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ extern "C" {
*/
size_t transaction_size();

/**
* Get transaction id
*
* @param id : return id
*/
void get_transaction_id( transaction_id_type* id );

/**
* Get the action globally unique sequence
*
* @param seq : return sequence
*/
void get_action_sequence(uint64_t* seq);

/**
* Get the producer's signature for the action
* @param sig : Memory buffer
* @param siglen :Memory buffer size
* @return : Return valid data size
*/
int bpsig_action_time_seed( const char* sig, size_t siglen );

/**
* Gets the block number used for TAPOS on the currently executing transaction.
*
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/apply_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ void apply_context::exec_one( action_trace& trace )
trace.act = act;
trace.context_free = context_free;

const auto& p = control.get_dynamic_global_properties();
global_action_sequence = p.global_action_sequence + 1;

const auto& cfg = control.get_global_properties().configuration;
try {
try {
Expand Down Expand Up @@ -79,6 +82,7 @@ void apply_context::exec_one( action_trace& trace )

r.global_sequence = next_global_sequence();
r.recv_sequence = next_recv_sequence( receiver );
global_action_sequence = 0;

const auto& account_sequence = db.get<account_sequence_object, by_name>(act.account);
r.code_sequence = account_sequence.code_sequence; // could be modified by action execution above
Expand Down
10 changes: 10 additions & 0 deletions libraries/chain/block_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,15 @@ namespace eosio { namespace chain {
return result;
}

void block_header::set_block_extensions_mroot(digest_type& mroot)
{
if (header_extensions.size() < 1)
header_extensions.emplace_back();

header_extensions[0].first = static_cast<uint16_t>(block_header_extensions_type::block_extensions_mroot);
header_extensions[0].second.resize(mroot.data_size());
std::copy(mroot.data(), mroot.data() + mroot.data_size(), header_extensions[0].second.data());
}


} }
4 changes: 3 additions & 1 deletion libraries/chain/block_header_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace eosio { namespace chain {
*/
block_header_state block_header_state::next( const signed_block_header& h, bool trust )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.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" );
Expand Down Expand Up @@ -175,8 +175,10 @@ namespace eosio { namespace chain {
result.header.action_mroot = h.action_mroot;
result.header.transaction_mroot = h.transaction_mroot;
result.header.producer_signature = h.producer_signature;
result.header.header_extensions = h.header_extensions;
result.id = result.header.id();


// ASSUMPTION FROM controller_impl::apply_block = all untrusted blocks will have their signatures pre-validated here
if( !trust ) {
EOS_ASSERT( result.block_signing_key == result.signee(), wrong_signing_key, "block not signed by expected key",
Expand Down
56 changes: 39 additions & 17 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using controller_index_set = index_set<
account_index,
account_sequence_index,
global_property_multi_index,
global_propertyex_multi_index,
global_property2_multi_index,
dynamic_global_property_multi_index,
block_summary_multi_index,
transaction_multi_index,
Expand Down Expand Up @@ -109,6 +109,8 @@ struct pending_state {

optional<block_id_type> _producer_block_id;

std::function<signature_type(digest_type)> _signer;

void push() {
_db_session.push();
}
Expand Down Expand Up @@ -640,11 +642,11 @@ struct controller_impl {
db.create<dynamic_global_property_object>([](auto&){});

// *bos begin*

db.create<global_propertyex_object>([&](auto &gpo) {
gpo.gmr.cpu_us = config::default_free_cpu_limit;
gpo.gmr.net_byte = config::default_free_net_limit;
gpo.gmr.ram_byte = config::default_free_ram_limit;
//guaranteed minimum resources which is abbreviated gmr
db.create<global_property2_object>([&](auto &gpo) {
gpo.gmr.cpu_us = config::default_gmr_cpu_limit;
gpo.gmr.net_byte = config::default_gmr_net_limit;
gpo.gmr.ram_byte = config::default_gmr_ram_limit;
});

sync_name_list(list_type::actor_blacklist_type,true);
Expand Down Expand Up @@ -708,7 +710,7 @@ struct controller_impl {
sync_name_list(list);
}

void sync_list_and_db(list_type list, global_propertyex_object &gprops2,bool isMerge=false)
void sync_list_and_db(list_type list, global_property2_object &gprops2,bool isMerge=false)
{
int64_t lst = static_cast<int64_t>(list);
EOS_ASSERT( list >= list_type::actor_blacklist_type && list < list_type::list_type_count, transaction_exception, "unknown list type : ${l}, ismerge: ${n}", ("l", static_cast<int64_t>(list))("n", isMerge));
Expand Down Expand Up @@ -737,7 +739,7 @@ struct controller_impl {

void sync_name_list(list_type list,bool isMerge=false)
{
const auto &gpo2 = db.get<global_propertyex_object>();
const auto &gpo2 = db.get<global_property2_object>();
db.modify(gpo2, [&](auto &gprops2) {
sync_list_and_db(list, gprops2,isMerge);
});
Expand Down Expand Up @@ -1155,7 +1157,7 @@ struct controller_impl {


void start_block( block_timestamp_type when, uint16_t confirm_block_count, controller::block_status s,
const optional<block_id_type>& producer_block_id )
const optional<block_id_type>& producer_block_id , std::function<signature_type(digest_type)> signer = nullptr)
{
EOS_ASSERT( !pending, block_validate_exception, "pending block already exists" );

Expand All @@ -1174,6 +1176,7 @@ struct controller_impl {

pending->_block_status = s;
pending->_producer_block_id = producer_block_id;
pending->_signer = signer;
pending->_pending_block_state = std::make_shared<block_state>( *head, when ); // promotes pending schedule (if any) to active
pending->_pending_block_state->in_current_chain = true;

Expand Down Expand Up @@ -1241,10 +1244,13 @@ struct controller_impl {

void apply_block( const signed_block_ptr& b, controller::block_status s ) { try {
try {
EOS_ASSERT( b->block_extensions.size() == 0, block_validate_exception, "no supported extensions" );
//EOS_ASSERT( b->block_extensions.size() == 0, block_validate_exception, "no supported extensions" );
auto producer_block_id = b->id();
start_block( b->timestamp, b->confirmed, s , producer_block_id);

pending->_pending_block_state->block->header_extensions = b->header_extensions;
pending->_pending_block_state->block->block_extensions = b->block_extensions;

transaction_trace_ptr trace;

for( const auto& receipt : b->transactions ) {
Expand Down Expand Up @@ -1309,7 +1315,6 @@ struct controller_impl {

void push_block( const signed_block_ptr& b, controller::block_status s ) {
EOS_ASSERT(!pending, block_validate_exception, "it is not valid to push a block when there is a pending block");

auto reset_prod_light_validation = fc::make_scoped_exit([old_value=trusted_producer_light_validation, this]() {
trusted_producer_light_validation = old_value;
});
Expand Down Expand Up @@ -1442,6 +1447,17 @@ struct controller_impl {
pending->_pending_block_state->header.transaction_mroot = merkle( move(trx_digests) );
}

void set_ext_merkle() {
vector<digest_type> ext_digests;
const auto& exts = pending->_pending_block_state->block->block_extensions;
ext_digests.reserve( exts.size());
for( const auto& a : exts )
ext_digests.emplace_back( digest_type::hash(a) );

auto mroot = merkle( move(ext_digests));
pending->_pending_block_state->header.set_block_extensions_mroot(mroot);
}


void finalize_block()
{
Expand All @@ -1466,7 +1482,7 @@ struct controller_impl {
// Update resource limits:
resource_limits.process_account_limit_updates();
const auto& chain_config = self.get_global_properties().configuration;
const auto& gmr = self.get_global_properties2().gmr;
const auto& gmr = self.get_global_properties2().gmr;//guaranteed minimum resources which is abbreviated gmr

uint32_t max_virtual_mult = 1000;
uint64_t CPU_TARGET = EOS_PERCENT(chain_config.max_block_cpu_usage, chain_config.target_block_cpu_usage_pct);
Expand All @@ -1475,14 +1491,15 @@ struct controller_impl {
{EOS_PERCENT(chain_config.max_block_net_usage, chain_config.target_block_net_usage_pct), chain_config.max_block_net_usage, config::block_size_average_window_ms / config::block_interval_ms, max_virtual_mult, {99, 100}, {1000, 999}}
);

resource_limits.set_block_parameters_ex(
resource_limits.set_gmr_parameters(
{ gmr.ram_byte, gmr.cpu_us,gmr.net_byte}
);

resource_limits.process_block_usage(pending->_pending_block_state->block_num);

set_action_merkle();
set_trx_merkle();
set_ext_merkle();

auto p = pending->_pending_block_state;
p->id = p->header.id();
Expand Down Expand Up @@ -1693,9 +1710,9 @@ chainbase::database& controller::mutable_db()const { return my->db; }
const fork_database& controller::fork_db()const { return my->fork_db; }


void controller::start_block( block_timestamp_type when, uint16_t confirm_block_count) {
void controller::start_block( block_timestamp_type when, uint16_t confirm_block_count, std::function<signature_type(digest_type)> signer) {
validate_db_available_size();
my->start_block(when, confirm_block_count, block_status::incomplete, optional<block_id_type>() );
my->start_block(when, confirm_block_count, block_status::incomplete, optional<block_id_type>() , signer);
}

void controller::finalize_block() {
Expand Down Expand Up @@ -1841,6 +1858,11 @@ optional<block_id_type> controller::pending_producer_block_id()const {
return my->pending->_producer_block_id;
}

std::function<signature_type(digest_type)> controller::pending_producer_signer()const {
EOS_ASSERT( my->pending, block_validate_exception, "no pending block" );
return my->pending->_signer;
}

uint32_t controller::last_irreversible_block_num() const {
return std::max(std::max(my->head->bft_irreversible_blocknum, my->head->dpos_irreversible_blocknum), my->snapshot_head_block);
}
Expand Down Expand Up @@ -2213,8 +2235,8 @@ const flat_set<account_name> &controller::get_resource_greylist() const {
}

// *bos begin*
const global_propertyex_object& controller::get_global_properties2()const {
return my->db.get<global_propertyex_object>();
const global_property2_object& controller::get_global_properties2()const {
return my->db.get<global_property2_object>();
}

void controller::set_name_list(int64_t list, int64_t action, std::vector<account_name> name_list)
Expand Down
2 changes: 0 additions & 2 deletions libraries/chain/fork_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ namespace eosio { namespace chain {
block_state_ptr fork_database::add( signed_block_ptr b, bool trust ) {
EOS_ASSERT( b, fork_database_exception, "attempt to add null block" );
EOS_ASSERT( my->head, fork_db_block_not_found, "no head block set" );

const auto& by_id_idx = my->index.get<by_block_id>();
auto existing = by_id_idx.find( b->id() );
EOS_ASSERT( existing == by_id_idx.end(), fork_database_exception, "we already know about this block" );
Expand Down Expand Up @@ -203,7 +202,6 @@ namespace eosio { namespace chain {
/// remove all of the invalid forks built of this id including this id
void fork_database::remove( const block_id_type& id ) {
vector<block_id_type> remove_queue{id};

for( uint32_t i = 0; i < remove_queue.size(); ++i ) {
auto itr = my->index.find( remove_queue[i] );
if( itr != my->index.end() )
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/include/eosio/chain/apply_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ class apply_context {
bool privileged = false;
bool context_free = false;
bool used_context_free_api = false;
uint64_t global_action_sequence = 0;

generic_index<index64_object> idx64;
generic_index<index128_object> idx128;
Expand Down
3 changes: 3 additions & 0 deletions libraries/chain/include/eosio/chain/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ namespace eosio { namespace chain {
}
};

enum class block_extension_type : uint16_t {
bpsig_action_time_seed
};

/**
*/
Expand Down
9 changes: 8 additions & 1 deletion libraries/chain/include/eosio/chain/block_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

namespace eosio { namespace chain {

/* Extended spatial data category
*/
enum block_header_extensions_type : uint16_t {
block_extensions_mroot = 0 // mroot of block extensions
};

struct block_header
{
block_timestamp_type timestamp;
Expand Down Expand Up @@ -32,13 +38,14 @@ namespace eosio { namespace chain {
*/
uint32_t schedule_version = 0;
optional<producer_schedule_type> new_producers;
extensions_type header_extensions;
extensions_type header_extensions; // [0] : mroot of block extensions


digest_type digest()const;
block_id_type id() const;
uint32_t block_num() const { return num_from_id(previous) + 1; }
static uint32_t num_from_id(const block_id_type& id);
void set_block_extensions_mroot(digest_type& mroot);
};


Expand Down
9 changes: 5 additions & 4 deletions libraries/chain/include/eosio/chain/config_xos.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

namespace eosio { namespace chain { namespace config {

const static uint32_t default_free_cpu_limit = 200'000; /// free cpu usage in microseconds
const static uint32_t default_free_net_limit = 10 * 1024; // 10 KB
const static uint32_t default_free_ram_limit = 0; // 0 KB
const static uint16_t default_free_resource_limit_per_day = 1000;
//guaranteed minimum resources which is abbreviated gmr
const static uint32_t default_gmr_cpu_limit = 200'000; /// free cpu usage in microseconds
const static uint32_t default_gmr_net_limit = 10 * 1024; // 10 KB
const static uint32_t default_gmr_ram_limit = 0; // 0 KB
const static uint16_t default_gmr_resource_limit_per_day = 1000;



Expand Down
7 changes: 4 additions & 3 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace eosio { namespace chain {

class dynamic_global_property_object;
class global_property_object;
class global_propertyex_object; // *bos*
class global_property2_object; // *bos*
class permission_object;
class account_object;
using resource_limits::resource_limits_manager;
Expand Down Expand Up @@ -111,7 +111,7 @@ namespace eosio { namespace chain {
* Starts a new pending block session upon which new transactions can
* be pushed.
*/
void start_block( block_timestamp_type time = block_timestamp_type(), uint16_t confirm_block_count = 0 );
void start_block( block_timestamp_type time = block_timestamp_type(), uint16_t confirm_block_count = 0, std::function<signature_type(digest_type)> signer = nullptr );

void abort_block();

Expand Down Expand Up @@ -206,6 +206,7 @@ namespace eosio { namespace chain {
optional<block_id_type> pending_producer_block_id()const;

const producer_schedule_type& active_producers()const;
std::function<signature_type(digest_type)> pending_producer_signer()const;
const producer_schedule_type& pending_producers()const;
optional<producer_schedule_type> proposed_producers()const;

Expand Down Expand Up @@ -234,7 +235,7 @@ namespace eosio { namespace chain {
void remove_resource_greylist(const account_name &name);

// *bos begin*
const global_propertyex_object& get_global_properties2()const; // *bos*
const global_property2_object& get_global_properties2()const; // *bos*
void set_name_list(int64_t list, int64_t action, std::vector<account_name> name_list);

// void list_add_name(const int list, const account_name &name);
Expand Down
Loading

0 comments on commit df2422b

Please sign in to comment.