提交 c85e061d 编写于 作者: K Kayan

created unit test cases for 353(DAWN-472) for ram_limit, weighted cpu/net limit

上级 41ea5d97
......@@ -1029,5 +1029,34 @@ BOOST_AUTO_TEST_CASE(transaction_mroot)
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_CASE(account_ram_limit) { try {
const int64_t ramlimit = 5000;
validating_tester chain;
resource_limits_manager mgr = chain.control->get_mutable_resource_limits_manager();
account_name acc1 = N(test1);
chain.create_account(acc1);
mgr.set_account_limits(acc1, ramlimit, -1, -1 );
transaction_trace trace = chain.create_account(N(acc2), acc1);
chain.produce_block();
BOOST_ASSERT(trace.status == transaction_trace::executed);
trace = chain.create_account(N(acc3), acc1);
chain.produce_block();
BOOST_ASSERT(trace.status == transaction_trace::executed);
BOOST_REQUIRE_EXCEPTION(
chain.create_account(N(acc4), acc1),
tx_resource_exhausted,
[] (const tx_resource_exhausted &e)->bool {
BOOST_REQUIRE_EQUAL(std::string("transaction exhausted allowed resources"), e.what());
return true;
}
);
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_SUITE_END()
......@@ -521,6 +521,70 @@ BOOST_FIXTURE_TEST_CASE(cpu_usage_tests, tester ) try {
BOOST_REQUIRE_EQUAL(true, limit > 101 && limit < 250);
} FC_LOG_AND_RETHROW()
// test weighted cpu limit
BOOST_FIXTURE_TEST_CASE(weighted_cpu_limit_tests, tester ) try {
resource_limits_manager mgr = control->get_mutable_resource_limits_manager();
create_accounts( {N(f_tests)} );
create_accounts( {N(acc2)} );
bool pass = false;
std::string code = R"=====(
(module
(import "env" "require_auth" (func $require_auth (param i64)))
(import "env" "eosio_assert" (func $eosio_assert (param i32 i32)))
(table 0 anyfunc)
(memory $0 1)
(export "apply" (func $apply))
(func $i64_trunc_u_f64 (param $0 f64) (result i64) (i64.trunc_u/f64 (get_local $0)))
(func $test (param $0 i64))
(func $apply (param $0 i64)(param $1 i64)(param $2 i64)
)=====";
for (int i = 0; i < 1024; ++i) {
code += "(call $test (call $i64_trunc_u_f64 (f64.const 1)))\n";
}
code += "))";
produce_blocks(1);
set_code(N(f_tests), code.c_str());
produce_blocks(10);
mgr.set_account_limits(N(f_tests), -1, -1, 1);
int count = 0;
while (count < 12) {
signed_transaction trx;
for (int i = 0; i < 100; ++i) {
action act;
act.account = N(f_tests);
act.name = N() + (i * 16);
act.authorization = vector<permission_level>{{N(f_tests),config::active_name}};
trx.actions.push_back(act);
}
set_transaction_headers(trx);
trx.sign(get_private_key( N(f_tests), "active" ), chain_id_type());
try {
push_transaction(trx);
produce_blocks(1);
BOOST_REQUIRE_EQUAL(true, chain_has_transaction(trx.id()));
pass = true;
count++;
} catch (eosio::chain::tx_resource_exhausted &) {
BOOST_ASSERT(count == 10);
break;
}
BOOST_REQUIRE_EQUAL(true, validate());
if (count == 10) { // add a big weight on acc2, making f_tests out of resource
mgr.set_account_limits(N(acc2), -1, -1, 1000);
}
}
BOOST_ASSERT(count == 10);
} FC_LOG_AND_RETHROW()
/**
* Make sure WASM "start" method is used correctly
*/
......@@ -1345,4 +1409,59 @@ BOOST_FIXTURE_TEST_CASE(net_usage_tests, tester ) try {
} FC_LOG_AND_RETHROW()
BOOST_FIXTURE_TEST_CASE(weighted_net_usage_tests, tester ) try {
account_name account = N(f_tests);
account_name acc2 = N(acc2);
create_accounts({account, acc2});
int ver = 0;
auto check = [&](int coderepeat)-> bool {
std::string code = R"=====(
(module
(import "env" "require_auth" (func $require_auth (param i64)))
(import "env" "eosio_assert" (func $eosio_assert (param i32 i32)))
(table 0 anyfunc)
(memory $0 1)
(export "apply" (func $apply))
(func $i64_trunc_u_f64 (param $0 f64) (result i64) (i64.trunc_u/f64 (get_local $0)))
(func $test (param $0 i64))
(func $apply (param $0 i64)(param $1 i64)(param $2 i64)
)=====";
for (int i = 0; i < coderepeat; ++i) {
code += "(call $test (call $i64_trunc_u_f64 (f64.const ";
code += (char)('0' + ver);
code += ")))\n";
}
code += "))"; ver++;
produce_blocks(1);
signed_transaction trx;
auto wasm = ::eosio::chain::wast_to_wasm(code);
trx.actions.emplace_back( vector<permission_level>{{account,config::active_name}},
contracts::setcode{
.account = account,
.vmtype = 0,
.vmversion = 0,
.code = bytes(wasm.begin(), wasm.end())
});
set_transaction_headers(trx);
trx.sign( get_private_key( account, "active" ), chain_id_type() );
try {
push_transaction(trx);
produce_blocks(1);
return true;
} catch (tx_resource_exhausted &) {
return false;
}
};
BOOST_REQUIRE_EQUAL(true, check(128)); // no limits, should pass
resource_limits_manager mgr = control->get_mutable_resource_limits_manager();
mgr.set_account_limits(account, -1, 1, -1); // set weight = 1 for account
BOOST_REQUIRE_EQUAL(true, check(128));
mgr.set_account_limits(acc2, -1, 1000, -1); // set a big weight for other account
BOOST_REQUIRE_EQUAL(false, check(128));
} FC_LOG_AND_RETHROW()
BOOST_AUTO_TEST_SUITE_END()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册