From cda36cb94bef01581314ca1a62924249695fd70d Mon Sep 17 00:00:00 2001 From: arhag Date: Thu, 22 Mar 2018 11:16:55 -0400 Subject: [PATCH] Separate out C++ blockchain parameter setter/getter helper functions from C privileged API --- contracts/eosio.system/common.hpp | 6 +- contracts/eosio.system/voting.hpp | 2 +- contracts/eosiolib/eosiolib.cpp | 40 +++++----- contracts/eosiolib/privileged.h | 29 ++----- contracts/eosiolib/privileged.hpp | 23 +++++- libraries/chain/wasm_interface.cpp | 122 ++++++++++++++--------------- 6 files changed, 113 insertions(+), 109 deletions(-) diff --git a/contracts/eosio.system/common.hpp b/contracts/eosio.system/common.hpp index fa9772332..ac73218f5 100644 --- a/contracts/eosio.system/common.hpp +++ b/contracts/eosio.system/common.hpp @@ -21,11 +21,11 @@ namespace eosiosystem { static constexpr uint32_t blocks_per_producer = 6; static constexpr uint32_t seconds_per_day = 24 * 3600; static constexpr uint32_t days_per_4years = 1461; - + struct eosio_parameters : eosio::blockchain_parameters { uint32_t percent_of_max_inflation_rate = 0; uint32_t storage_reserve_ratio = 1000; // ratio * 1000 - + EOSLIB_SERIALIZE_DERIVED( eosio_parameters, eosio::blockchain_parameters, (percent_of_max_inflation_rate)(storage_reserve_ratio) ) }; @@ -48,7 +48,7 @@ namespace eosiosystem { static eosio_global_state& get_default_parameters() { static eosio_global_state dp; - get_blockchain_parameters(&dp); + get_blockchain_parameters(dp); return dp; } }; diff --git a/contracts/eosio.system/voting.hpp b/contracts/eosio.system/voting.hpp index 87d98a6a3..eb271499b 100644 --- a/contracts/eosio.system/voting.hpp +++ b/contracts/eosio.system/voting.hpp @@ -351,7 +351,7 @@ namespace eosiosystem { auto issue_quantity = parameters.blocks_per_cycle * (parameters.payment_per_block + parameters.payment_to_eos_bucket); currency::inline_issue(SystemAccount, issue_quantity); - set_blockchain_parameters(¶meters); + set_blockchain_parameters(parameters); global_state_singleton::set(parameters); } diff --git a/contracts/eosiolib/eosiolib.cpp b/contracts/eosiolib/eosiolib.cpp index 6c20de566..d2d8a9de1 100644 --- a/contracts/eosiolib/eosiolib.cpp +++ b/contracts/eosiolib/eosiolib.cpp @@ -4,6 +4,21 @@ namespace eosio { + void set_blockchain_parameters(const eosio::blockchain_parameters& params) { + char buf[sizeof(eosio::blockchain_parameters)]; + eosio::datastream ds( buf, sizeof(buf) ); + ds << params; + set_blockchain_parameters_packed( buf, ds.tellp() ); + } + + void get_blockchain_parameters(eosio::blockchain_parameters& params) { + char buf[sizeof(eosio::blockchain_parameters)]; + size_t size = get_blockchain_parameters_packed( buf, sizeof(buf) ); + eosio_assert( size <= sizeof(buf), "buffer is too small" ); + eosio::datastream ds( buf, size_t(size) ); + ds >> params; + } + using ::memset; using ::memcpy; @@ -42,7 +57,7 @@ namespace eosio { const uint32_t current_memory_size = reinterpret_cast(sbrk(0)); if(static_cast(current_memory_size) < 0) return nullptr; - + //grab up to the end of the current WASM memory page provided that it has 1KiB remaining, otherwise // grow to end of next page uint32_t heap_adj; @@ -290,7 +305,7 @@ namespace eosio { return nullptr; } - if( *orig_ptr_size > size ) + if( *orig_ptr_size > size ) { // use a buffer_ptr to allocate the memory to free char* const new_ptr = ptr + size + _size_marker; @@ -481,7 +496,8 @@ namespace eosio { }; memory_manager memory_heap; -} + +} /// namespace eosio extern "C" { @@ -509,22 +525,4 @@ void free(void* ptr) return eosio::memory_heap.free(ptr); } -void set_blockchain_parameters_packed(char* data, size_t datalen); - -void set_blockchain_parameters(const struct blockchain_parameters* params) { - auto data = eosio::pack( *static_cast(params) ); - set_blockchain_parameters_packed( data.data(), data.size() ); -} - -int get_blockchain_parameters_packed(char* data, size_t datalen); - -void get_blockchain_parameters(struct blockchain_parameters* params) { - char buf[sizeof(struct blockchain_parameters)]; - size_t size = get_blockchain_parameters_packed( buf, sizeof(buf) ); - eosio_assert( size <= sizeof(buf), "buffer is too small" ); - static_assert(sizeof(blockchain_parameters) == sizeof(eosio::blockchain_parameters), "data structures are not the same"); - eosio::datastream ds( buf, size_t(size) ); - ds >> *reinterpret_cast(params); -} - } diff --git a/contracts/eosiolib/privileged.h b/contracts/eosiolib/privileged.h index bc9f3a8ff..6710bdbce 100644 --- a/contracts/eosiolib/privileged.h +++ b/contracts/eosiolib/privileged.h @@ -1,6 +1,8 @@ #pragma once +#ifdef __cplusplus extern "C" { +#endif /** * @defgroup privilegedapi Privileged API @@ -10,32 +12,15 @@ extern "C" { void set_resource_limits( account_name account, uint64_t ram_bytes, uint64_t net_weight, uint64_t cpu_weight, int64_t ignored); - void set_active_producers( char *producer_data, size_t producer_data_size ); + void set_active_producers( char *producer_data, uint32_t producer_data_size ); bool is_privileged( account_name account ); - struct blockchain_parameters { - uint32_t target_block_size; - uint32_t max_block_size; + void set_blockchain_parameters_packed(char* data, uint32_t datalen); - uint32_t target_block_acts_per_scope; - uint32_t max_block_acts_per_scope; - - uint32_t target_block_acts; ///< regardless of the amount of parallelism, this defines target compute time per block - uint32_t max_block_acts; ///< regardless of the amount of parallelism, this maximum compute time per block - - uint64_t max_storage_size; - uint32_t max_transaction_lifetime; - uint32_t max_transaction_exec_time; - uint16_t max_authority_depth; - uint16_t max_inline_depth; - uint32_t max_inline_action_size; - uint32_t max_generated_transaction_size; - }; - - void set_blockchain_parameters(const struct blockchain_parameters* params); - - void get_blockchain_parameters(struct blockchain_parameters* params); + uint32_t get_blockchain_parameters_packed(char* data, uint32_t datalen); ///@ } privilegedcapi +#ifdef __cplusplus } +#endif diff --git a/contracts/eosiolib/privileged.hpp b/contracts/eosiolib/privileged.hpp index 56e41c8b0..88178f2bf 100644 --- a/contracts/eosiolib/privileged.hpp +++ b/contracts/eosiolib/privileged.hpp @@ -4,7 +4,24 @@ #include "types.h" namespace eosio { - struct blockchain_parameters : ::blockchain_parameters { + + struct blockchain_parameters { + uint32_t target_block_size; + uint32_t max_block_size; + + uint32_t target_block_acts_per_scope; + uint32_t max_block_acts_per_scope; + + uint32_t target_block_acts; ///< regardless of the amount of parallelism, this defines target compute time per block + uint32_t max_block_acts; ///< regardless of the amount of parallelism, this maximum compute time per block + + uint64_t max_storage_size; + uint32_t max_transaction_lifetime; + uint32_t max_transaction_exec_time; + uint16_t max_authority_depth; + uint16_t max_inline_depth; + uint32_t max_inline_action_size; + uint32_t max_generated_transaction_size; EOSLIB_SERIALIZE( blockchain_parameters, (target_block_size)(max_block_size)(target_block_acts_per_scope) (max_block_acts_per_scope)(target_block_acts)(max_block_acts)(max_storage_size) @@ -13,6 +30,10 @@ namespace eosio { ) }; + void set_blockchain_parameters(const eosio::blockchain_parameters& params); + + void get_blockchain_parameters(eosio::blockchain_parameters& params); + struct producer_key { account_name producer_name; public_key block_signing_key; diff --git a/libraries/chain/wasm_interface.cpp b/libraries/chain/wasm_interface.cpp index d7e5852cb..391d6a4fe 100644 --- a/libraries/chain/wasm_interface.cpp +++ b/libraries/chain/wasm_interface.cpp @@ -512,7 +512,7 @@ class privileged_api : public context_aware_api { }); } - int get_blockchain_parameters_packed( array_ptr packed_blockchain_parameters, size_t datalen) { + uint32_t get_blockchain_parameters_packed( array_ptr packed_blockchain_parameters, size_t datalen) { auto& gpo = context.controller.get_global_properties(); auto size = fc::raw::pack_size( gpo.configuration ); if ( size <= datalen ) { @@ -574,15 +574,15 @@ class softfloat_api : public context_aware_api { return a; } // float unops - float32_t _eosio_f32_abs( float32_t a ) { - a.v &= ~(1 << 31); - return a; + float32_t _eosio_f32_abs( float32_t a ) { + a.v &= ~(1 << 31); + return a; } - float32_t _eosio_f32_neg( float32_t a ) { + float32_t _eosio_f32_neg( float32_t a ) { uint32_t sign = a.v >> 31; - a.v &= ~(1 << 31); + a.v &= ~(1 << 31); a.v |= (!sign << 31); - return a; + return a; } float32_t _eosio_f32_sqrt( float32_t a ) { return f32_sqrt( a ); } // ceil, floor, trunc and nearest are lifted from libc @@ -677,15 +677,15 @@ class softfloat_api : public context_aware_api { } // double unops - float64_t _eosio_f64_abs( float64_t a ) { - a.v &= ~(uint64_t(1) << 63); - return a; + float64_t _eosio_f64_abs( float64_t a ) { + a.v &= ~(uint64_t(1) << 63); + return a; } - float64_t _eosio_f64_neg( float64_t a ) { + float64_t _eosio_f64_neg( float64_t a ) { uint64_t sign = a.v >> 63; - a.v &= ~(uint64_t(1) << 63); + a.v &= ~(uint64_t(1) << 63); a.v |= (uint64_t(!sign) << 63); - return a; + return a; } float64_t _eosio_f64_sqrt( float64_t a ) { return f64_sqrt( a ); } // ceil, floor, trunc and nearest are lifted from libc @@ -744,7 +744,7 @@ class softfloat_api : public context_aware_api { private: - static constexpr float32_t inv_float_eps = { 0x4B000000 }; + static constexpr float32_t inv_float_eps = { 0x4B000000 }; static constexpr float64_t inv_double_eps = { 0x4330000000000000 }; }; class producer_api : public context_aware_api { @@ -1281,58 +1281,58 @@ class compiler_builtins : public context_aware_api { lhs %= rhs; ret = lhs; } - - void __addtf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + + void __addtf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ lb, hb }}; - ret = f128_add( a, b ); + ret = f128_add( a, b ); } - void __subtf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + void __subtf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ lb, hb }}; - ret = f128_sub( a, b ); + ret = f128_sub( a, b ); } - void __multf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + void __multf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ lb, hb }}; - ret = f128_mul( a, b ); + ret = f128_mul( a, b ); } - void __divtf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + void __divtf3( float128_t& ret, uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ lb, hb }}; - ret = f128_div( a, b ); + ret = f128_div( a, b ); } - int __eqtf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __eqtf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; - return f128_eq( a, b ); + return f128_eq( a, b ); } - int __netf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __netf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; - return !f128_eq( a, b ); + return !f128_eq( a, b ); } - int __getf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __getf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; - return !f128_lt( a, b ); + return !f128_lt( a, b ); } - int __gttf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __gttf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; - return !f128_lt( a, b ) && !f128_eq( a, b ); + return !f128_lt( a, b ) && !f128_eq( a, b ); } - int __letf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __letf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; - return f128_le( a, b ); + return f128_le( a, b ); } - int __lttf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __lttf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; - return f128_lt( a, b ); + return f128_lt( a, b ); } - int __cmptf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __cmptf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; if ( f128_lt( a, b ) ) @@ -1341,7 +1341,7 @@ class compiler_builtins : public context_aware_api { return 0; return 1; } - int __unordtf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { + int __unordtf2( uint64_t la, uint64_t ha, uint64_t lb, uint64_t hb ) { float128_t a = {{ la, ha }}; float128_t b = {{ la, ha }}; if ( f128_isSignalingNaN( a ) || f128_isSignalingNaN( b ) ) @@ -1363,39 +1363,39 @@ class compiler_builtins : public context_aware_api { return i32_to_f128(i); } */ - void __extendsftf2( float128_t& ret, uint32_t f ) { + void __extendsftf2( float128_t& ret, uint32_t f ) { float32_t in = { f }; - ret = f32_to_f128( in ); + ret = f32_to_f128( in ); } - void __extenddftf2( float128_t& ret, float64_t in ) { + void __extenddftf2( float128_t& ret, float64_t in ) { edump(("warning in flaot64..." )); // float64_t in = { f }; - ret = f64_to_f128( in ); + ret = f64_to_f128( in ); } - int64_t __fixtfdi( uint64_t l, uint64_t h ) { + int64_t __fixtfdi( uint64_t l, uint64_t h ) { float128_t f = {{ l, h }}; - return f128_to_i64( f, 0, false ); - } - int32_t __fixtfsi( uint64_t l, uint64_t h ) { + return f128_to_i64( f, 0, false ); + } + int32_t __fixtfsi( uint64_t l, uint64_t h ) { float128_t f = {{ l, h }}; - return f128_to_i32( f, 0, false ); - } - uint64_t __fixunstfdi( uint64_t l, uint64_t h ) { + return f128_to_i32( f, 0, false ); + } + uint64_t __fixunstfdi( uint64_t l, uint64_t h ) { float128_t f = {{ l, h }}; - return f128_to_ui64( f, 0, false ); - } - uint32_t __fixunstfsi( uint64_t l, uint64_t h ) { + return f128_to_ui64( f, 0, false ); + } + uint32_t __fixunstfsi( uint64_t l, uint64_t h ) { float128_t f = {{ l, h }}; - return f128_to_ui32( f, 0, false ); - } - uint64_t __trunctfdf2( uint64_t l, uint64_t h ) { + return f128_to_ui32( f, 0, false ); + } + uint64_t __trunctfdf2( uint64_t l, uint64_t h ) { float128_t f = {{ l, h }}; - return f128_to_f64( f ).v; - } - uint32_t __trunctfsf2( uint64_t l, uint64_t h ) { + return f128_to_f64( f ).v; + } + uint32_t __trunctfsf2( uint64_t l, uint64_t h ) { float128_t f = {{ l, h }}; - return f128_to_f32( f ).v; - } + return f128_to_f32( f ).v; + } static constexpr uint32_t SHIFT_WIDTH = (sizeof(uint64_t)*8)-1; }; @@ -1510,8 +1510,8 @@ REGISTER_INTRINSICS(compiler_builtins, (__floatsitf, void (int, int) ) (__floatunsitf, void (int, int) ) (__floatsidf, float64_t(int) ) - (__extendsftf2, void(int, int) ) - (__extenddftf2, void(int, float64_t) ) + (__extendsftf2, void(int, int) ) + (__extenddftf2, void(int, float64_t) ) (__fixtfdi, int64_t(int64_t, int64_t) ) (__fixtfsi, int(int64_t, int64_t) ) (__fixunstfdi, int64_t(int64_t, int64_t) ) -- GitLab