Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #8 from eosnetworkfoundation/wasm-config
Browse files Browse the repository at this point in the history
Backport CONFIGURABLE_WASM_LIMITS from 2.1
  • Loading branch information
swatanabe authored Jan 17, 2022
2 parents f58d942 + 9c05385 commit 3e102d5
Show file tree
Hide file tree
Showing 51 changed files with 2,359 additions and 437 deletions.
1 change: 1 addition & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ add_library( eosio_chain
wasm_interface.cpp
wasm_eosio_validation.cpp
wasm_eosio_injection.cpp
wasm_config.cpp
apply_context.cpp
abi_serializer.cpp
asset.cpp
Expand Down
62 changes: 56 additions & 6 deletions libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ struct controller_impl {
set_activation_handler<builtin_protocol_feature_t::get_sender>();
set_activation_handler<builtin_protocol_feature_t::webauthn_key>();
set_activation_handler<builtin_protocol_feature_t::wtmsig_block_signatures>();
set_activation_handler<builtin_protocol_feature_t::configurable_wasm_limits>();

self.irreversible_block.connect([this](const block_state_ptr& bsp) {
wasmif.current_lib(bsp->block_num);
Expand Down Expand Up @@ -943,6 +944,8 @@ struct controller_impl {
// special case for in-place upgrade of global_property_object
if (std::is_same<value_t, global_property_object>::value) {
using v2 = legacy::snapshot_global_property_object_v2;
using v3 = legacy::snapshot_global_property_object_v3;
using v4 = legacy::snapshot_global_property_object_v4;

if (std::clamp(header.version, v2::minimum_version, v2::maximum_version) == header.version ) {
fc::optional<genesis_state> genesis = extract_legacy_genesis_state(*snapshot, header.version);
Expand All @@ -954,7 +957,33 @@ struct controller_impl {
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties,&gs_chain_id](auto& gpo ){
gpo.initalize_from(legacy_global_properties, gs_chain_id);
gpo.initalize_from(legacy_global_properties, gs_chain_id, kv_database_config{},
genesis_state::default_initial_wasm_configuration);
});
});
return; // early out to avoid default processing
}

if (std::clamp(header.version, v3::minimum_version, v3::maximum_version) == header.version ) {
snapshot->read_section<global_property_object>([&db=this->db]( auto &section ) {
v3 legacy_global_properties;
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties](auto& gpo ){
gpo.initalize_from(legacy_global_properties, kv_database_config{},
genesis_state::default_initial_wasm_configuration);
});
});
return; // early out to avoid default processing
}

if (std::clamp(header.version, v4::minimum_version, v4::maximum_version) == header.version) {
snapshot->read_section<global_property_object>([&db = this->db](auto& section) {
v4 legacy_global_properties;
section.read_row(legacy_global_properties, db);

db.create<global_property_object>([&legacy_global_properties](auto& gpo) {
gpo.initalize_from(legacy_global_properties);
});
});
return; // early out to avoid default processing
Expand Down Expand Up @@ -1047,6 +1076,8 @@ struct controller_impl {
genesis.initial_configuration.validate();
db.create<global_property_object>([&genesis,&chain_id=this->chain_id](auto& gpo ){
gpo.configuration = genesis.initial_configuration;
// TODO: Update this when genesis protocol features are enabled.
gpo.wasm_configuration = genesis_state::default_initial_wasm_configuration;
gpo.chain_id = chain_id;
});

Expand Down Expand Up @@ -3291,11 +3322,23 @@ chain_id_type controller::extract_chain_id(snapshot_reader& snapshot) {
}

chain_id_type chain_id;
snapshot.read_section<global_property_object>([&chain_id]( auto &section ){
snapshot_global_property_object global_properties;
section.read_row(global_properties);
chain_id = global_properties.chain_id;
});

using v4 = legacy::snapshot_global_property_object_v4;
if (header.version <= v4::maximum_version) {
snapshot.read_section<global_property_object>([&chain_id]( auto &section ){
v4 global_properties;
section.read_row(global_properties);
chain_id = global_properties.chain_id;
});
}
else {
snapshot.read_section<global_property_object>([&chain_id]( auto &section ){
snapshot_global_property_object global_properties;
section.read_row(global_properties);
chain_id = global_properties.chain_id;
});
}

return chain_id;
}

Expand Down Expand Up @@ -3367,6 +3410,13 @@ void controller_impl::on_activation<builtin_protocol_feature_t::wtmsig_block_sig
} );
}

template<>
void controller_impl::on_activation<builtin_protocol_feature_t::configurable_wasm_limits>() {
db.modify( db.get<protocol_state_object>(), [&]( auto& ps ) {
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "set_wasm_parameters_packed" );
add_intrinsic_to_whitelist( ps.whitelisted_intrinsics, "get_wasm_parameters_packed" );
} );
}


/// End of protocol feature activation handlers
Expand Down
81 changes: 58 additions & 23 deletions libraries/chain/include/eosio/chain/chain_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,30 @@ namespace eosio { namespace chain {
* their preference for each of the parameters in this object, and the blockchain runs according to the median of the
* values specified by the producers.
*/
struct chain_config {
struct chain_config_v0 {

//order must match parameters as ids are used in serialization
enum {
max_block_net_usage_id,
target_block_net_usage_pct_id,
max_transaction_net_usage_id,
base_per_transaction_net_usage_id,
net_usage_leeway_id,
context_free_discount_net_usage_num_id,
context_free_discount_net_usage_den_id,
max_block_cpu_usage_id,
target_block_cpu_usage_pct_id,
max_transaction_cpu_usage_id,
min_transaction_cpu_usage_id,
max_transaction_lifetime_id,
deferred_trx_expiration_window_id,
max_transaction_delay_id,
max_inline_action_size_id,
max_inline_action_depth_id,
max_authority_depth_id,
PARAMS_COUNT
};

uint64_t max_block_net_usage; ///< the maxiumum net usage in instructions for a block
uint32_t target_block_net_usage_pct; ///< the target percent (1% == 100, 100%= 10,000) of maximum net usage; exceeding this triggers congestion handling
uint32_t max_transaction_net_usage; ///< the maximum objectively measured net usage that the chain will allow regardless of account limits
Expand All @@ -35,29 +58,16 @@ struct chain_config {

void validate()const;

inline const chain_config_v0& v0() const {
return *this;
}

template<typename Stream>
friend Stream& operator << ( Stream& out, const chain_config& c ) {
return out << "Max Block Net Usage: " << c.max_block_net_usage << ", "
<< "Target Block Net Usage Percent: " << ((double)c.target_block_net_usage_pct / (double)config::percent_1) << "%, "
<< "Max Transaction Net Usage: " << c.max_transaction_net_usage << ", "
<< "Base Per-Transaction Net Usage: " << c.base_per_transaction_net_usage << ", "
<< "Net Usage Leeway: " << c.net_usage_leeway << ", "
<< "Context-Free Data Net Usage Discount: " << (double)c.context_free_discount_net_usage_num * 100.0 / (double)c.context_free_discount_net_usage_den << "% , "

<< "Max Block CPU Usage: " << c.max_block_cpu_usage << ", "
<< "Target Block CPU Usage Percent: " << ((double)c.target_block_cpu_usage_pct / (double)config::percent_1) << "%, "
<< "Max Transaction CPU Usage: " << c.max_transaction_cpu_usage << ", "
<< "Min Transaction CPU Usage: " << c.min_transaction_cpu_usage << ", "

<< "Max Transaction Lifetime: " << c.max_transaction_lifetime << ", "
<< "Deferred Transaction Expiration Window: " << c.deferred_trx_expiration_window << ", "
<< "Max Transaction Delay: " << c.max_transaction_delay << ", "
<< "Max Inline Action Size: " << c.max_inline_action_size << ", "
<< "Max Inline Action Depth: " << c.max_inline_action_depth << ", "
<< "Max Authority Depth: " << c.max_authority_depth << "\n";
friend Stream& operator << ( Stream& out, const chain_config_v0& c ) {
return c.log(out) << "\n";
}

friend inline bool operator ==( const chain_config& lhs, const chain_config& rhs ) {
friend inline bool operator ==( const chain_config_v0& lhs, const chain_config_v0& rhs ) {
return std::tie( lhs.max_block_net_usage,
lhs.target_block_net_usage_pct,
lhs.max_transaction_net_usage,
Expand Down Expand Up @@ -97,13 +107,38 @@ struct chain_config {
);
};

friend inline bool operator !=( const chain_config& lhs, const chain_config& rhs ) { return !(lhs == rhs); }
friend inline bool operator !=( const chain_config_v0& lhs, const chain_config_v0& rhs ) { return !(lhs == rhs); }

protected:
template<typename Stream>
Stream& log(Stream& out) const{
return out << "Max Block Net Usage: " << max_block_net_usage << ", "
<< "Target Block Net Usage Percent: " << ((double)target_block_net_usage_pct / (double)config::percent_1) << "%, "
<< "Max Transaction Net Usage: " << max_transaction_net_usage << ", "
<< "Base Per-Transaction Net Usage: " << base_per_transaction_net_usage << ", "
<< "Net Usage Leeway: " << net_usage_leeway << ", "
<< "Context-Free Data Net Usage Discount: " << (double)context_free_discount_net_usage_num * 100.0 / (double)context_free_discount_net_usage_den << "% , "

<< "Max Block CPU Usage: " << max_block_cpu_usage << ", "
<< "Target Block CPU Usage Percent: " << ((double)target_block_cpu_usage_pct / (double)config::percent_1) << "%, "
<< "Max Transaction CPU Usage: " << max_transaction_cpu_usage << ", "
<< "Min Transaction CPU Usage: " << min_transaction_cpu_usage << ", "

<< "Max Transaction Lifetime: " << max_transaction_lifetime << ", "
<< "Deferred Transaction Expiration Window: " << deferred_trx_expiration_window << ", "
<< "Max Transaction Delay: " << max_transaction_delay << ", "
<< "Max Inline Action Size: " << max_inline_action_size << ", "
<< "Max Inline Action Depth: " << max_inline_action_depth << ", "
<< "Max Authority Depth: " << max_authority_depth;
}
};

//after adding 1st value to chain_config_v1 change this using to point to v1
using chain_config = chain_config_v0;

} } // namespace eosio::chain

FC_REFLECT(eosio::chain::chain_config,
FC_REFLECT(eosio::chain::chain_config_v0,
(max_block_net_usage)(target_block_net_usage_pct)
(max_transaction_net_usage)(base_per_transaction_net_usage)(net_usage_leeway)
(context_free_discount_net_usage_num)(context_free_discount_net_usage_den)
Expand Down
7 changes: 7 additions & 0 deletions libraries/chain/include/eosio/chain/chain_id_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace eosio {

namespace chain {

namespace legacy {
struct snapshot_global_property_object_v3;
struct snapshot_global_property_object_v4;
}

struct chain_id_type : public fc::sha256 {
using fc::sha256::sha256;

Expand Down Expand Up @@ -51,6 +56,8 @@ namespace chain {
friend struct controller_impl;
friend class global_property_object;
friend struct snapshot_global_property_object;
friend struct legacy::snapshot_global_property_object_v3;
friend struct legacy::snapshot_global_property_object_v4;
};

} } // namespace eosio::chain
Expand Down
8 changes: 7 additions & 1 deletion libraries/chain/include/eosio/chain/chain_snapshot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ struct chain_snapshot_header {
* - WebAuthn keys
* - wtmsig block siganatures: the block header state changed to include producer authorities and additional signatures
* - removed genesis_state and added chain ID to global_property_object
* 4: Updated for v3.0.0 protocol features:
* - forwards compatible with versions 2 and 3
* - kv database
* - Configurable wasm limits
* 5: Updated for v3.0.0 eos features:
* - chain_config update
*/

static constexpr uint32_t minimum_compatible_version = 2;
static constexpr uint32_t current_version = 3;
static constexpr uint32_t current_version = 4;

uint32_t version = current_version;

Expand Down
12 changes: 12 additions & 0 deletions libraries/chain/include/eosio/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ const static uint16_t default_controller_thread_pool_size = 2;
const static uint32_t default_max_variable_signature_length = 16384u;
const static uint32_t default_max_nonprivileged_inline_action_size = 4 * 1024; // 4 KB

const static uint32_t default_max_wasm_mutable_global_bytes = 1024;
const static uint32_t default_max_wasm_table_elements = 1024;
const static uint32_t default_max_wasm_section_elements = 8192;
const static uint32_t default_max_wasm_linear_memory_init = 64*1024;
const static uint32_t default_max_wasm_func_local_bytes = 8192;
const static uint32_t default_max_wasm_nested_structures = 1024;
const static uint32_t default_max_wasm_symbol_bytes = 8192;
const static uint32_t default_max_wasm_module_bytes = 20*1024*1024;
const static uint32_t default_max_wasm_code_bytes = 20*1024*1024;
const static uint32_t default_max_wasm_pages = 528;
const static uint32_t default_max_wasm_call_depth = 251;

const static uint32_t min_net_usage_delta_between_base_and_max_for_trx = 10*1024;
// Should be large enough to allow recovery from badly set blockchain parameters without a hard fork
// (unless net_usage_leeway is set to 0 and so are the net limits of all accounts that can help with resetting blockchain parameters).
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ namespace eosio { namespace chain {
3160009, "No wasm file found" )
FC_DECLARE_DERIVED_EXCEPTION( abi_file_not_found, contract_exception,
3160010, "No abi file found" )
FC_DECLARE_DERIVED_EXCEPTION( wasm_config_unknown_version, contract_exception,
3160015, "Unknown wasm_config version" )

FC_DECLARE_DERIVED_EXCEPTION( producer_exception, chain_exception,
3170000, "Producer exception" )
Expand Down
19 changes: 17 additions & 2 deletions libraries/chain/include/eosio/chain/genesis_state.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <eosio/chain/chain_config.hpp>
#include <eosio/chain/wasm_config.hpp>
#include <eosio/chain/types.hpp>

#include <fc/crypto/sha256.hpp>
Expand All @@ -15,7 +16,7 @@ struct genesis_state {

static const string eosio_root_key;

chain_config initial_configuration = {
chain_config_v0 initial_configuration = {
.max_block_net_usage = config::default_max_block_net_usage,
.target_block_net_usage_pct = config::default_target_block_net_usage_pct,
.max_transaction_net_usage = config::default_max_transaction_net_usage,
Expand All @@ -37,6 +38,20 @@ struct genesis_state {
.max_authority_depth = config::default_max_auth_depth,
};

static constexpr wasm_config default_initial_wasm_configuration {
.max_mutable_global_bytes = config::default_max_wasm_mutable_global_bytes,
.max_table_elements = config::default_max_wasm_table_elements,
.max_section_elements = config::default_max_wasm_section_elements,
.max_linear_memory_init = config::default_max_wasm_linear_memory_init,
.max_func_local_bytes = config::default_max_wasm_func_local_bytes,
.max_nested_structures = config::default_max_wasm_nested_structures,
.max_symbol_bytes = config::default_max_wasm_symbol_bytes,
.max_module_bytes = config::default_max_wasm_module_bytes,
.max_code_bytes = config::default_max_wasm_code_bytes,
.max_pages = config::default_max_wasm_pages,
.max_call_depth = config::default_max_wasm_call_depth
};

time_point initial_timestamp;
public_key_type initial_key;

Expand All @@ -58,6 +73,6 @@ struct genesis_state {

} } // namespace eosio::chain


// @swap initial_timestamp initial_key initial_configuration
FC_REFLECT(eosio::chain::genesis_state,
(initial_timestamp)(initial_key)(initial_configuration))
Loading

0 comments on commit 3e102d5

Please sign in to comment.