Skip to content

Commit

Permalink
add guard for reversible blocks; change some documentations and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
b1bart committed Jul 16, 2018
1 parent 82b85ed commit f585cf5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 22 deletions.
8 changes: 8 additions & 0 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,7 @@ void controller::sign_block( const std::function<signature_type( const digest_ty

void controller::commit_block() {
validate_db_available_size();
validate_reversible_available_size();
my->commit_block(true);
}

Expand All @@ -1280,6 +1281,7 @@ void controller::abort_block() {

void controller::push_block( const signed_block_ptr& b, block_status s ) {
validate_db_available_size();
validate_reversible_available_size();
my->push_block( b, s );
}

Expand Down Expand Up @@ -1617,6 +1619,12 @@ void controller::validate_db_available_size() const {
EOS_ASSERT(free >= guard, database_guard_exception, "database free: ${f}, guard size: ${g}", ("f", free)("g",guard));
}

void controller::validate_reversible_available_size() const {
const auto free = my->reversible_blocks.get_segment_manager()->get_free_memory();
const auto guard = my->conf.reversible_guard_size;
EOS_ASSERT(free >= guard, reversible_guard_exception, "reversible free: ${f}, guard size: ${g}", ("f", free)("g",guard));
}

bool controller::is_known_unexpired_transaction( const transaction_id_type& id) const {
return db().find<transaction_object, by_trx_id>(id);
}
Expand Down
3 changes: 2 additions & 1 deletion libraries/chain/include/eosio/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ typedef __uint128_t uint128_t;
const static auto default_blocks_dir_name = "blocks";
const static auto reversible_blocks_dir_name = "reversible";
const static auto default_reversible_cache_size = 340*1024*1024ll;/// 1MB * 340 blocks based on 21 producer BFT delay
const static auto default_reversible_guard_size = 2*1024*1024ll;/// 1MB * 340 blocks based on 21 producer BFT delay

const static auto default_state_dir_name = "state";
const static auto forkdb_filename = "forkdb.dat";
const static auto default_state_size = 1*1024*1024*1024ll;
const static auto default_state_guard_size = 1*1024*1024ll;
const static auto default_state_guard_size = 128*1024*1024ll;


const static uint64_t system_account_name = N(eosio);
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace eosio { namespace chain {
uint64_t state_size = chain::config::default_state_size;
uint64_t state_guard_size = chain::config::default_state_guard_size;
uint64_t reversible_cache_size = chain::config::default_reversible_cache_size;
uint64_t reversible_guard_size = chain::config::default_reversible_guard_size;
bool read_only = false;
bool force_all_checks = false;
bool contracts_console = false;
Expand Down Expand Up @@ -185,6 +186,7 @@ namespace eosio { namespace chain {
void validate_expiration( const transaction& t )const;
void validate_tapos( const transaction& t )const;
void validate_db_available_size() const;
void validate_reversible_available_size() const;

bool is_known_unexpired_transaction( const transaction_id_type& id) const;

Expand Down
10 changes: 8 additions & 2 deletions libraries/chain/include/eosio/chain/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,14 @@ namespace eosio { namespace chain {
3060003, "Contract Table Query Exception" )
FC_DECLARE_DERIVED_EXCEPTION( contract_query_exception, database_exception,
3060004, "Contract Query Exception" )
FC_DECLARE_DERIVED_EXCEPTION( database_guard_exception, database_exception,
3060004, "Database usage is at unsafe levels" )

FC_DECLARE_DERIVED_EXCEPTION( guard_exception, database_exception,
3060100, "Database exception" )

FC_DECLARE_DERIVED_EXCEPTION( database_guard_exception, guard_exception,
3060101, "Database usage is at unsafe levels" )
FC_DECLARE_DERIVED_EXCEPTION( reversible_guard_exception, guard_exception,
3060102, "Reversible block log usage is at unsafe levels" )

FC_DECLARE_DERIVED_EXCEPTION( wasm_exception, chain_exception,
3070000, "WASM Exception" )
Expand Down
27 changes: 21 additions & 6 deletions plugins/chain_plugin/chain_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ void chain_plugin::set_program_options(options_description& cli, options_descrip
("wasm-runtime", bpo::value<eosio::chain::wasm_interface::vm_type>()->value_name("wavm/binaryen"), "Override default WASM runtime")
("abi-serializer-max-time-ms", bpo::value<uint32_t>()->default_value(config::default_abi_serializer_max_time_ms),
"Override default maximum ABI serialization time allowed in ms")
("chain-state-db-size-mb", bpo::value<uint64_t>()->default_value(config::default_state_size / (1024 * 1024)), "Maximum size (in MB) of the chain state database")
("chain-state-db-guard-size-mb", bpo::value<uint64_t>()->default_value(config::default_state_guard_size / (1024 * 1024)), "Minimum available size (in MB) in the chain state database before the node shutsdown to prevent corruption")
("reversible-blocks-db-size-mb", bpo::value<uint64_t>()->default_value(config::default_reversible_cache_size / (1024 * 1024)), "Maximum size (in MB) of the reversible blocks database")
("chain-state-db-size-mb", bpo::value<uint64_t>()->default_value(config::default_state_size / (1024 * 1024)), "Maximum size (in MiB) of the chain state database")
("chain-state-db-guard-size-mb", bpo::value<uint64_t>()->default_value(config::default_state_guard_size / (1024 * 1024)), "Safely shut down node when free space remaining in the chain state database drops below this size (in MiB).")
("reversible-blocks-db-size-mb", bpo::value<uint64_t>()->default_value(config::default_reversible_cache_size / (1024 * 1024)), "Maximum size (in MiB) of the reversible blocks database")
("reversible-blocks-db-guard-size-mb", bpo::value<uint64_t>()->default_value(config::default_reversible_guard_size / (1024 * 1024)), "Safely shut down node when free space remaining in the reverseible blocks database drops below this size (in MiB).")
("contracts-console", bpo::bool_switch()->default_value(false),
"print contract's output to console")
("actor-whitelist", boost::program_options::value<vector<string>>()->composing()->multitoken(),
Expand Down Expand Up @@ -349,6 +350,9 @@ void chain_plugin::plugin_initialize(const variables_map& options) {
my->chain_config->reversible_cache_size =
options.at( "reversible-blocks-db-size-mb" ).as<uint64_t>() * 1024 * 1024;

if( options.count( "reversible-blocks-db-guard-size-mb" ))
my->chain_config->reversible_guard_size = options.at( "reversible-blocks-db-guard-size-mb" ).as<uint64_t>() * 1024 * 1024;

if( my->wasm_runtime )
my->chain_config->wasm_runtime = *my->wasm_runtime;

Expand Down Expand Up @@ -564,6 +568,7 @@ void chain_plugin::plugin_startup()
try {
my->chain->startup();
} catch (const database_guard_exception& e) {
log_guard_exception(e);
// make sure to properly close the db
my->chain.reset();
throw;
Expand Down Expand Up @@ -767,10 +772,20 @@ fc::microseconds chain_plugin::get_abi_serializer_max_time() const {
return my->abi_serializer_max_time_ms;
}

void chain_plugin::handle_database_guard_exception(const chain::database_guard_exception& e) const {
elog("Database has reached an unsafe level of usage, shutting down to avoid corrupting the database. "
"Please increase the value set for \"chain-state-db-size-mb\" and restart the process!");
void chain_plugin::log_guard_exception(const chain::guard_exception&e ) const {
if (e.code() == chain::database_guard_exception::code_value) {
elog("Database has reached an unsafe level of usage, shutting down to avoid corrupting the database. "
"Please increase the value set for \"chain-state-db-size-mb\" and restart the process!");
} else if (e.code() == chain::reversible_guard_exception::code_value) {
elog("Reversible block database has reached an unsafe level of usage, shutting down to avoid corrupting the database. "
"Please increase the value set for \"reversible-blocks-db-size-mb\" and restart the process!");
}

dlog("Details: ${details}", ("details", e.to_detail_string()));
}

void chain_plugin::handle_guard_exception(const chain::guard_exception& e) const {
log_guard_exception(e);

// quit the app
app().quit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,10 @@ class chain_plugin : public plugin<chain_plugin> {
chain::chain_id_type get_chain_id() const;
fc::microseconds get_abi_serializer_max_time() const;

void handle_database_guard_exception(const chain::database_guard_exception& e) const;
void handle_guard_exception(const chain::guard_exception& e) const;
private:
void log_guard_exception(const chain::guard_exception& e) const;

unique_ptr<class chain_plugin_impl> my;
};

Expand Down
24 changes: 12 additions & 12 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin
bool except = false;
try {
chain.push_block(block);
} catch ( const database_guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_database_guard_exception(e);
} catch ( const guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_guard_exception(e);
return;
} catch( const fc::exception& e ) {
elog((e.to_detail_string()));
Expand Down Expand Up @@ -384,8 +384,8 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin
send_response(trace);
}

} catch ( const database_guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_database_guard_exception(e);
} catch ( const guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_guard_exception(e);
} catch ( boost::interprocess::bad_alloc& ) {
raise(SIGUSR1);
} CATCH_AND_CALL(send_response);
Expand Down Expand Up @@ -891,8 +891,8 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block(bool
// the state of the chain including this transaction
try {
chain.push_transaction(trx, fc::time_point::maximum());
} catch ( const database_guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_database_guard_exception(e);
} catch ( const guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_guard_exception(e);
return start_block_result::failed;
} FC_LOG_AND_DROP();

Expand Down Expand Up @@ -935,8 +935,8 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block(bool
chain.drop_unapplied_transaction(trx);
}
}
} catch ( const database_guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_database_guard_exception(e);
} catch ( const guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_guard_exception(e);
return start_block_result::failed;
} FC_LOG_AND_DROP();
}
Expand Down Expand Up @@ -976,8 +976,8 @@ producer_plugin_impl::start_block_result producer_plugin_impl::start_block(bool
_blacklisted_transactions.insert(transaction_id_with_expiry{trx, expiration});
}
}
} catch ( const database_guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_database_guard_exception(e);
} catch ( const guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_guard_exception(e);
return start_block_result::failed;
} FC_LOG_AND_DROP();
}
Expand Down Expand Up @@ -1093,8 +1093,8 @@ bool producer_plugin_impl::maybe_produce_block() {
try {
produce_block();
return true;
} catch ( const database_guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_database_guard_exception(e);
} catch ( const guard_exception& e ) {
app().get_plugin<chain_plugin>().handle_guard_exception(e);
return false;
} catch ( boost::interprocess::bad_alloc& ) {
raise(SIGUSR1);
Expand Down

0 comments on commit f585cf5

Please sign in to comment.