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 #163 from brianjohnson5972/fix_slow_test_stake
Browse files Browse the repository at this point in the history
Fix slow_test stake test
  • Loading branch information
brianjohnson5972 authored Aug 15, 2017
2 parents 7f1080a + 15487cb commit 85e4c46
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ script:
- WASM_LLVM_CONFIG=$TRAVIS_BUILD_DIR/ext/wasm-compiler/bin/llvm-config ext/cmake-3.9.0-Linux-x86_64/bin/cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++-4.0 -DCMAKE_C_COMPILER=clang-4.0 -DBOOST_ROOT=$TRAVIS_BUILD_DIR/ext -DSecp256k1_ROOT_DIR=$TRAVIS_BUILD_DIR/ext
- ninja -j4
- tests/chain_test
- tests/slow_test
- tests/api_test
4 changes: 2 additions & 2 deletions contracts/test_api/test_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ unsigned int test_message::read_message() {
char buffer[100];
uint32_t total = 0;

WASM_ASSERT( currentCode() == N(test_api), "currentCode() == N(test_api)" );
WASM_ASSERT( currentCode() == N(testapi), "currentCode() == N(testapi)" );

WASM_ASSERT(messageSize() == sizeof(dummy_message), "messageSize() == sizeof(dummy_message)");

Expand Down Expand Up @@ -42,7 +42,7 @@ unsigned int test_message::read_message_to_64k() {
}

unsigned int test_message::require_notice() {
if( currentCode() == N(test_api) ) {
if( currentCode() == N(testapi) ) {
eos::requireNotice( N(acc1) );
eos::requireNotice( N(acc2) );
eos::requireNotice( N(acc1), N(acc2) );
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace eos { namespace chain {
}

#ifdef NDEBUG
const int CHECKTIME_LIMIT = 2000;
const int CHECKTIME_LIMIT = 3000;
#else
const int CHECKTIME_LIMIT = 12000;
const int CHECKTIME_LIMIT = 18000;
#endif

DEFINE_INTRINSIC_FUNCTION0(env,checktime,checktime,none) {
Expand Down
32 changes: 13 additions & 19 deletions libraries/native_contract/eos_contract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,6 @@ void apply_eos_newaccount(apply_context& context) {
});
}

/**
* This method is called when the claim message is delivered to @staked
* staked::validate_staked_claim must require that @eos be notified.
*
* This method trusts that staked::precondition_staked_claim verifies that claim.amount is
* available.
*/
void apply_staked_claim(apply_context& context) {
auto claim = context.msg.as<types::claim>();
const auto& claimant = context.db.get<BalanceObject, byOwnerName>(claim.account);
context.mutable_db.modify(claimant, [&claim](BalanceObject& a) {
a.balance += claim.amount;
});
}

/**
*
* @ingroup native_eos
Expand Down Expand Up @@ -164,6 +149,8 @@ void apply_eos_lock(apply_context& context) {
context.require_recipient(lock.to);
context.require_recipient(lock.from);

context.require_authorization(lock.from);

const auto& locker = context.db.get<BalanceObject, byOwnerName>(lock.from);

EOS_ASSERT( locker.balance >= lock.amount, message_precondition_exception,
Expand All @@ -180,6 +167,8 @@ void apply_eos_lock(apply_context& context) {
void apply_eos_unlock(apply_context& context) {
auto unlock = context.msg.as<types::unlock>();

context.require_authorization(unlock.account);

EOS_ASSERT(unlock.amount >= 0, message_validate_exception, "Unlock amount cannot be negative");

const auto& balance = context.db.get<StakedBalanceObject, byOwnerName>(unlock.account);
Expand Down Expand Up @@ -226,20 +215,25 @@ void apply_eos_claim(apply_context& context) {

EOS_ASSERT(claim.amount > 0, message_validate_exception, "Claim amount must be positive");

context.require_authorization(claim.account);

auto balance = context.db.find<StakedBalanceObject, byOwnerName>(claim.account);
EOS_ASSERT(balance != nullptr, message_precondition_exception,
"Could not find staked balance for ${name}", ("name", claim.account));
auto balanceReleaseTime = balance->lastUnstakingTime + config::StakedBalanceCooldownSeconds;
auto now = context.db.get(dynamic_global_property_object::id_type()).time;
auto now = context.controller.head_block_time();
EOS_ASSERT(now >= balanceReleaseTime, message_precondition_exception,
"Cannot claim balance until ${releaseDate}", ("releaseDate", balanceReleaseTime));
EOS_ASSERT(balance->unstakingBalance >= claim.amount, message_precondition_exception,
"Cannot claim ${claimAmount} as only ${available} is available for claim",
("claimAmount", claim.amount)("available", balance->unstakingBalance));

context.mutable_db.modify(context.db.get<StakedBalanceObject, byOwnerName>(claim.account),
[&claim](StakedBalanceObject& sbo) {
sbo.unstakingBalance -= claim.amount;
const auto& stakedBalance = context.db.get<StakedBalanceObject, byOwnerName>(claim.account);
stakedBalance.finishUnstakingTokens(claim.amount, context.mutable_db);

const auto& liquidBalance = context.db.get<BalanceObject, byOwnerName>(claim.account);
context.mutable_db.modify(liquidBalance, [&claim](BalanceObject& a) {
a.balance += claim.amount;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ class StakedBalanceObject : public chainbase::object<chain::staked_balance_objec
* updating vote tallies
*/
void beginUnstakingTokens(types::ShareType amount, chainbase::database& db) const;
/**
* @brief Finish unstaking the specified amount of stake
* @param amount The amount of stake to finish unstaking
* @param db Read-write reference to the database
*
* This method will update this object's balances. There aren't really any invariants to maintain on this call, as
* the tokens are already unstaked and removed from vote tallies, but it is provided for completeness' sake.
*/
void finishUnstakingTokens(types::ShareType amount, chainbase::database& db) const;

/**
* @brief Propagate the specified change in stake to the producer votes or the proxy
Expand Down
6 changes: 6 additions & 0 deletions libraries/native_contract/staked_balance_objects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ void StakedBalanceObject::beginUnstakingTokens(ShareType amount, chainbase::data
});
}

void StakedBalanceObject::finishUnstakingTokens(ShareType amount, chainbase::database& db) const {
db.modify(*this, [&amount](StakedBalanceObject& sbo) {
sbo.unstakingBalance -= amount;
});
}

void StakedBalanceObject::propagateVotes(ShareType stakeDelta, chainbase::database& db) const {
if (producerVotes.contains<ProducerSlate>())
// This account votes for producers directly; update their stakes
Expand Down
38 changes: 27 additions & 11 deletions tests/api_tests/api_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void SetCode( testing_blockchain& chain, AccountName account, const char* wast )
}
} FC_LOG_AND_RETHROW( ) }

uint32_t CallFunction( testing_blockchain& chain, const types::Message& msg, const vector<char>& data, const vector<AccountName>& scope = {N(test_api)}) {
uint32_t CallFunction( testing_blockchain& chain, const types::Message& msg, const vector<char>& data, const vector<AccountName>& scope = {N(testapi)}) {
static int expiration = 1;
eos::chain::SignedTransaction trx;
trx.scope = scope;
Expand Down Expand Up @@ -148,8 +148,8 @@ uint64_t TEST_METHOD(const char* CLASS, const char *METHOD) {
return ( (uint64_t(DJBH(CLASS))<<32) | uint32_t(DJBH(METHOD)) );
}

#define CALL_TEST_FUNCTION(TYPE, AUTH, DATA) CallFunction(chain, Message{"test_api", AUTH, TYPE}, DATA)
#define CALL_TEST_FUNCTION_SCOPE(TYPE, AUTH, DATA, SCOPE) CallFunction(chain, Message{"test_api", AUTH, TYPE}, DATA, SCOPE)
#define CALL_TEST_FUNCTION(TYPE, AUTH, DATA) CallFunction(chain, Message{"testapi", AUTH, TYPE}, DATA)
#define CALL_TEST_FUNCTION_SCOPE(TYPE, AUTH, DATA, SCOPE) CallFunction(chain, Message{"testapi", AUTH, TYPE}, DATA, SCOPE)

bool is_access_violation(fc::unhandled_exception const & e) {
try {
Expand Down Expand Up @@ -192,17 +192,29 @@ uint32_t last_fnc_err = 0;
std::STREAM.rdbuf(oldbuf); \
}

void send_set_code_message(testing_blockchain& chain, types::setcode& handler, AccountName account)
{
eos::chain::SignedTransaction trx;
handler.account = account;
trx.scope = { account };
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{account,"active"});
trx.setMessage(0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
trx.set_reference_block(chain.head_block_id());
chain.push_transaction(trx);
chain.produce_blocks(1);
}

BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
{ try {

//std::string test_api_wast_str(test_api_wast);
auto test_api_wast_str = readFile2("/home/matu/Documents/Dev/eos/contracts/test_api/test_api.wast");
//std::cout << test_api_wast << std::endl;
auto wasm = assemble_wast( test_api_wast );

Make_Blockchain(chain);
chain.produce_blocks(2);
Make_Account(chain, test_api);
Make_Account(chain, testapi);
Make_Account(chain, another);
Make_Account(chain, acc1);
Make_Account(chain, acc2);
Expand All @@ -211,9 +223,13 @@ BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
chain.produce_blocks(1);

//Set test code
SetCode(chain, "test_api", test_api_wast_str.c_str());
SetCode(chain, "acc1", test_api_wast_str.c_str());
SetCode(chain, "acc2", test_api_wast_str.c_str());
types::setcode handler;
handler.code.resize(wasm.size());
handler.code.assign(wasm.begin(), wasm.end());

send_set_code_message(chain, handler, "testapi");
send_set_code_message(chain, handler, "acc1");
send_set_code_message(chain, handler, "acc2");

//Test types
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_types", "types_size"), {}, {} ) == WASM_TEST_PASS, "test_types::types_size()" );
Expand Down Expand Up @@ -341,7 +357,7 @@ BOOST_FIXTURE_TEST_CASE(test_all, testing_fixture)
BOOST_CHECK_MESSAGE( CALL_TEST_FUNCTION( TEST_METHOD("test_db", "key_i64_general"), {}, {} ) == WASM_TEST_PASS, "test_db::key_i64_general()" );
BOOST_CHECK_EQUAL( std::distance(idx.begin(), idx.end()) , 4);

auto itr = idx.lower_bound( boost::make_tuple( N(test_api), N(test_api), N(test_table)) );
auto itr = idx.lower_bound( boost::make_tuple( N(testapi), N(testapi), N(test_table)) );

BOOST_CHECK_EQUAL((uint64_t)itr->key, N(alice)); ++itr;
BOOST_CHECK_EQUAL((uint64_t)itr->key, N(bob)); ++itr;
Expand Down
14 changes: 7 additions & 7 deletions tests/common/macro_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define MKACCT_IMPL(chain, name, creator, active, owner, recovery, deposit) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names({ #creator, "eos" }); \
trx.scope = sort_names({ #creator, config::EosContractName }); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{#creator, "active"}}, \
"newaccount", types::newaccount{#creator, #name, owner, active, recovery, deposit}); \
Expand Down Expand Up @@ -146,7 +146,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define STAKE4(chain, sender, recipient, amount) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { #sender, #recipient } ); \
trx.scope = sort_names( { #sender, #recipient, config::EosContractName } ); \
trx.emplaceMessage(config::EosContractName, vector<types::AccountPermission>{{#sender, "active"}}, \
"lock", types::lock{#sender, #recipient, amount}); \
trx.expiration = chain.head_block_time() + 100; \
Expand All @@ -159,7 +159,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define BEGIN_UNSTAKE3(chain, account, amount) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { "eos" } ); \
trx.scope = sort_names( { config::EosContractName } ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{#account, "active"}}, \
"unlock", types::unlock{#account, amount}); \
Expand All @@ -172,7 +172,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define FINISH_UNSTAKE3(chain, account, amount) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( { "eos", #account } ); \
trx.scope = sort_names( { config::EosContractName, #account } ); \
trx.emplaceMessage(config::EosContractName, vector<types::AccountPermission>{{#account, "active"}}, \
"claim", types::claim{#account, amount}); \
trx.expiration = chain.head_block_time() + 100; \
Expand All @@ -184,7 +184,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define MKPDCR4(chain, owner, key, cfg) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {#owner, "eos"} ); \
trx.scope = sort_names( {#owner, config::EosContractName} ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{#owner, "active"}}, \
"setproducer", types::setproducer{#owner, key, cfg}); \
Expand All @@ -201,7 +201,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define APPDCR4(chain, voter, producer, approved) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {#voter, "eos"} ); \
trx.scope = sort_names( {#voter, config::EosContractName} ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{#voter, "active"}}, \
"okproducer", types::okproducer{#voter, #producer, approved? 1 : 0}); \
Expand All @@ -214,7 +214,7 @@ inline std::vector<Name> sort_names( std::vector<Name>&& names ) {
#define UPPDCR4(chain, owner, key, cfg) \
{ \
eos::chain::SignedTransaction trx; \
trx.scope = sort_names( {owner, "eos"} ); \
trx.scope = sort_names( {owner, config::EosContractName} ); \
trx.emplaceMessage(config::EosContractName, \
vector<types::AccountPermission>{{owner, "active"}}, \
"setproducer", types::setproducer{owner, key, cfg}); \
Expand Down
26 changes: 16 additions & 10 deletions tests/slow_tests/slow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ BOOST_FIXTURE_TEST_CASE(multiindex, testing_fixture)
BOOST_FIXTURE_TEST_CASE(tapos_wrap, testing_fixture)
{ try {
Make_Blockchain(chain)
Make_Account(chain, system);
Make_Account(chain, acct);
chain.produce_blocks(1);
Transfer_Asset(chain, inita, system, Asset(1000) );
Transfer_Asset(chain, system, acct, Asset(5));
Stake_Asset(chain, acct, Asset(5).amount);
wlog("Hang on, this will take a minute...");
Expand All @@ -185,6 +188,9 @@ BOOST_FIXTURE_TEST_CASE(stake, testing_fixture)
// Create account sam with default balance of 100, and stake 55 of it
Make_Blockchain(chain);
Make_Account(chain, sam);

chain.produce_blocks();

Transfer_Asset(chain, inita, sam, Asset(55) );

// MakeAccount should start sam out with some staked balance
Expand Down Expand Up @@ -290,6 +296,7 @@ void SetCode( testing_blockchain& chain, AccountName account, const char* wast )
trx.scope = {account};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{account,"active"});
trx.setMessage(0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
trx.set_reference_block(chain.head_block_id());
Expand Down Expand Up @@ -343,6 +350,7 @@ BOOST_FIXTURE_TEST_CASE(create_script, testing_fixture)
trx.scope = {"currency"};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{"currency","active"});
trx.setMessage(0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
trx.set_reference_block(chain.head_block_id());
Expand Down Expand Up @@ -422,10 +430,10 @@ void BuyCurrency( testing_blockchain& chain, AccountName buyer, AccountName exch
}

BOOST_FIXTURE_TEST_CASE(create_exchange, testing_fixture) {
try {
try {
Make_Blockchain(chain);
chain.produce_blocks(2);
Make_Account(chain, system);
Make_Account(chain, currency);
Make_Account(chain, exchange);
chain.produce_blocks(1);
Expand All @@ -439,8 +447,6 @@ BOOST_FIXTURE_TEST_CASE(create_exchange, testing_fixture) {
TransferCurrency( chain, "currency", "inita", 1000 );
TransferCurrency( chain, "currency", "initb", 2000 );

Transfer_Asset(chain, system, inita, Asset(50));
Transfer_Asset(chain, system, initb, Asset(50));
chain.produce_blocks(1);
ilog( "transfering funds to the exchange" );
TransferCurrency( chain, "inita", "exchange", 1000 );
Expand All @@ -464,23 +470,21 @@ BOOST_FIXTURE_TEST_CASE(create_exchange, testing_fixture) {
chain.produce_blocks(1);

wlog( "start buy and sell" );
SellCurrency( chain, "initb", "exchange", 1, 100, .5 );
SellCurrency( chain, "initb", "exchange", 1, 100, .75 );
SellCurrency( chain, "initb", "exchange", 1, 100, .85 );
uint64_t order_num = 1;
SellCurrency( chain, "initb", "exchange", order_num++, 100, .5 );
SellCurrency( chain, "initb", "exchange", order_num++, 100, .75 );
SellCurrency( chain, "initb", "exchange", order_num++, 100, .85 );
//BOOST_REQUIRE_THROW( SellCurrency( chain, "initb", "exchange", 1, 100, .5 ), fc::exception ); // order id already exists
//SellCurrency( chain, "initb", "exchange", 2, 100, .75 );

// BuyCurrency( chain, "initb", "exchange", 1, 50, .25 );
BuyCurrency( chain, "initb", "exchange", 1, 50, .5 );
BuyCurrency( chain, "initb", "exchange", order_num++, 50, .5 );
//BOOST_REQUIRE_THROW( BuyCurrency( chain, "initb", "exchange", 1, 50, .25 ), fc::exception ); // order id already exists

/// this should buy 5 from initb order 2 at a price of .75
//BuyCurrency( chain, "initb", "exchange", 2, 50, .8 );

} FC_LOG_AND_RETHROW()
}catch(...) {
elog( "unexpected exception" );
}
}

//Test account script float rejection
Expand Down Expand Up @@ -1111,6 +1115,7 @@ R"(
trx.scope = {"simplecoin"};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{"simplecoin","active"});
trx.setMessage(0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
trx.set_reference_block(chain.head_block_id());
Expand Down Expand Up @@ -1147,6 +1152,7 @@ BOOST_FIXTURE_TEST_CASE(create_script_w_loop, testing_fixture)
trx.scope = {"currency"};
trx.messages.resize(1);
trx.messages[0].code = config::EosContractName;
trx.messages[0].authorization.emplace_back(types::AccountPermission{"currency","active"});
trx.setMessage(0, "setcode", handler);
trx.expiration = chain.head_block_time() + 100;
trx.set_reference_block(chain.head_block_id());
Expand Down

0 comments on commit 85e4c46

Please sign in to comment.