Skip to content

Commit

Permalink
[ABW]: [NIP] Issue EOSIO#15, undelegate_block_test added to unit test…
Browse files Browse the repository at this point in the history
…s but it still needs to be supplemented

              [Ign] Added ifdefed code that enables unconditional output from eosio::print even during unit tests
  • Loading branch information
syncad committed Nov 8, 2018
1 parent eecb859 commit 0254b95
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
23 changes: 23 additions & 0 deletions contracts/eosio.init/eosio.init.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <eosiolib/eosio.hpp>
#include <eosiolib/singleton.hpp>

#include <beoslib/beos_privileged.hpp>

namespace eosio {

struct beos_global_state_element
Expand Down Expand Up @@ -170,11 +172,32 @@ namespace eosio {
void changeparams( beos_global_state new_params );

inline beos_global_state get_beos_global_state() const;
/*
current block is inside [ starting_block_for_distribution : ending_block_for_distribution )
*/
inline bool is_active_distribution_period() const;
/*
current block is at or above ending_block_for_distribution
*/
inline bool is_past_distribution_period() const;
};

inline beos_global_state init::get_beos_global_state() const
{
return _beos_gstate;
}

inline bool init::is_active_distribution_period() const
{
auto block_no = get_blockchain_block_number();
return _beos_gstate.beos.starting_block_for_distribution <= block_no &&
block_no < _beos_gstate.beos.ending_block_for_distribution;
}

inline bool init::is_past_distribution_period() const
{
auto block_no = get_blockchain_block_number();
return _beos_gstate.beos.ending_block_for_distribution <= block_no;
}

} /// namespace eosio
1 change: 1 addition & 0 deletions contracts/eosio.system/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ namespace eosiosystem {
void system_contract::undelegatebw( account_name from, account_name receiver,
asset unstake_net_quantity, asset unstake_cpu_quantity )
{
eosio_assert( eosio::init( N(beos.init) ).is_past_distribution_period(), "cannot unstake during distribution period" );
eosio_assert( asset() <= unstake_cpu_quantity, "must unstake a positive amount" );
eosio_assert( asset() <= unstake_net_quantity, "must unstake a positive amount" );
eosio_assert( asset() < unstake_cpu_quantity + unstake_net_quantity, "must unstake a positive amount" );
Expand Down
19 changes: 19 additions & 0 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,32 +987,45 @@ class console_api : public context_aware_api {
: context_aware_api(ctx,true)
, ignore(!ctx.control.contracts_console()) {}

//ABW: uncomment the following symbol to unconditionally write eosio::print calls to file (works even during unit tests)
//#define CAVEMEN_DEBUG
#ifdef CAVEMEN_DEBUG
#define DBG(format,value) { FILE *pFile = fopen("debug.log","a"); fprintf(pFile,format "\n",value); fclose(pFile); }
#else
#define DBG(format,value)
#endif

// Kept as intrinsic rather than implementing on WASM side (using prints_l and strlen) because strlen is faster on native side.
void prints(null_terminated_ptr str) {
DBG("%s",(const char *)(str))
if ( !ignore ) {
context.console_append<const char*>(str);
}
}

void prints_l(array_ptr<const char> str, size_t str_len ) {
DBG("%s",string(str, str_len).c_str())
if ( !ignore ) {
context.console_append(string(str, str_len));
}
}

void printi(int64_t val) {
DBG("%lli",val)
if ( !ignore ) {
context.console_append(val);
}
}

void printui(uint64_t val) {
DBG("%lli",val)
if ( !ignore ) {
context.console_append(val);
}
}

void printi128(const __int128& val) {
DBG("%lli",val)
if ( !ignore ) {
bool is_negative = (val < 0);
unsigned __int128 val_magnitude;
Expand All @@ -1033,13 +1046,15 @@ class console_api : public context_aware_api {
}

void printui128(const unsigned __int128& val) {
DBG("%lli",val)
if ( !ignore ) {
fc::uint128_t v(val>>64, static_cast<uint64_t>(val) );
context.console_append(fc::variant(v).get_string());
}
}

void printsf( float val ) {
DBG("%e",val)
if ( !ignore ) {
// Assumes float representation on native side is the same as on the WASM side
auto& console = context.get_console_stream();
Expand All @@ -1053,6 +1068,7 @@ class console_api : public context_aware_api {
}

void printdf( double val ) {
DBG("%e",val)
if ( !ignore ) {
// Assumes double representation on native side is the same as on the WASM side
auto& console = context.get_console_stream();
Expand All @@ -1066,6 +1082,7 @@ class console_api : public context_aware_api {
}

void printqf( const float128_t& val ) {
DBG("%Le",val)
/*
* Native-side long double uses an 80-bit extended-precision floating-point number.
* The easiest solution for now was to use the Berkeley softfloat library to round the 128-bit
Expand Down Expand Up @@ -1093,12 +1110,14 @@ class console_api : public context_aware_api {
}

void printn(const name& value) {
DBG("%s",value.to_string().c_str())
if ( !ignore ) {
context.console_append(value.to_string());
}
}

void printhex(array_ptr<const char> data, size_t data_len ) {
DBG("%s",fc::to_hex(data, data_len).c_str())
if ( !ignore ) {
context.console_append(fc::to_hex(data, data_len));
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/testing/tester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ namespace eosio { namespace testing {
tgs.ram.starting_block_for_distribution = 1;
tgs.ram.ending_block_for_distribution = 2;
tgs.starting_block_for_initial_witness_election = 1;
tgs.beos.starting_block_for_distribution = 1;
tgs.beos.ending_block_for_distribution = 2;

produce_block();

Expand Down
36 changes: 36 additions & 0 deletions unittests/eosio.interchain_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ struct actions: public tester
);
}

action_result unstake( account_name who, asset unstake_net_quantity, asset unstake_cpu_quantity )
{
return push_action( who, N(undelegatebw), mvo()
( "from", who )
( "receiver", who )
( "unstake_net_quantity", unstake_net_quantity )
( "unstake_cpu_quantity", unstake_cpu_quantity ),
system_abi_ser,
config::system_account_name
);
}

};

class eosio_interchain_tester : public actions
Expand Down Expand Up @@ -708,6 +720,30 @@ BOOST_FIXTURE_TEST_CASE( basic_vote_test2, eosio_init_tester ) try {

} FC_LOG_AND_RETHROW()

BOOST_FIXTURE_TEST_CASE( undelegate_block_test, eosio_init_tester ) try {
//see issue #15

test_global_state tgs;

tgs.beos.starting_block_for_distribution = 100;
tgs.beos.ending_block_for_distribution = 105;
tgs.beos.distribution_payment_block_interval_for_distribution = 8;

BOOST_REQUIRE_EQUAL( success(), change_params( tgs ) );

BOOST_REQUIRE_EQUAL( wasm_assert_msg("cannot unstake during distribution period"), unstake( N(alice), asset::from_string("10.0000 BEOS"), asset::from_string("10.0000 BEOS") ) );

produce_blocks( 105 - control->head_block_num() );
BOOST_REQUIRE_EQUAL( control->head_block_num(), 105u );

//ABW: first we need to extend the test with voting (15% required to allow successful unstake) but to do that
//we need delegations working on stake (issue #13) - supplement once it is done
//BOOST_REQUIRE_EQUAL( success(), unstake( N(alice), asset::from_string("10.0000 BEOS"), asset::from_string("10.0000 BEOS") ) );
//CHECK_STATS(alice, "0.0000 PROXY", "20.0000 BEOS", "");

} FC_LOG_AND_RETHROW()


BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(eosio_interchain_tests)
Expand Down

0 comments on commit 0254b95

Please sign in to comment.