Skip to content

Commit cc817b2

Browse files
authored
Merge pull request #1800 from dhaneshvb/DAWN-694-BootSeq
DAWN-694 - boot seq Unit tests
2 parents 829dd70 + 0ba686b commit cc817b2

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed

tests/chain_tests/bootseq_tests.cpp

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
#include <boost/test/unit_test.hpp>
2+
#include <eosio/testing/tester.hpp>
3+
#include <eosio/chain/contracts/abi_serializer.hpp>
4+
#include <eosio/chain_plugin/chain_plugin.hpp>
5+
6+
#include <eosio.system/eosio.system.wast.hpp>
7+
#include <eosio.system/eosio.system.abi.hpp>
8+
// These contracts are still under dev
9+
#if _READY
10+
#include <eosio.bios/eosio.bios.wast.hpp>
11+
#include <eosio.bios/eosio.bios.abi,hpp>
12+
#include <eosio.token/eosio.token.wast.hpp>
13+
#include <eosio.token/eosio.token.abi.hpp>
14+
#endif
15+
16+
#include <Runtime/Runtime.h>
17+
18+
#include <fc/variant_object.hpp>
19+
20+
21+
using namespace eosio;
22+
using namespace eosio::chain;
23+
using namespace eosio::chain::contracts;
24+
using namespace eosio::testing;
25+
using namespace fc;
26+
27+
using mvo = fc::mutable_variant_object;
28+
29+
class bootseq_tester : public tester
30+
{
31+
public:
32+
33+
static fc::variant_object producer_parameters_example( int n ) {
34+
return mutable_variant_object()
35+
("target_block_size", 1024 * 1024 + n)
36+
("max_block_size", 10 * 1024 + n)
37+
("target_block_acts_per_scope", 1000 + n)
38+
("max_block_acts_per_scope", 10000 + n)
39+
("target_block_acts", 1100 + n)
40+
("max_block_acts", 11000 + n)
41+
("max_storage_size", 2000 + n)
42+
("max_transaction_lifetime", 3600 + n)
43+
("max_transaction_exec_time", 9900 + n)
44+
("max_authority_depth", 6 + n)
45+
("max_inline_depth", 4 + n)
46+
("max_inline_action_size", 4096 + n)
47+
("max_generated_transaction_size", 64*1024 + n)
48+
("percent_of_max_inflation_rate", 50 + n)
49+
("storage_reserve_ratio", 100 + n);
50+
}
51+
52+
bootseq_tester() {
53+
const auto &accnt = control->get_database().get<account_object, by_name>(config::system_account_name);
54+
abi_def abi;
55+
BOOST_REQUIRE_EQUAL(abi_serializer::to_abi(accnt.abi, abi), true);
56+
abi_ser.set_abi(abi);
57+
}
58+
59+
action_result push_action( const account_name& account, const account_name& signer, const action_name &name, const variant_object &data, bool auth = true ) {
60+
string action_type_name = abi_ser.get_action_type(name);
61+
62+
action act;
63+
act.account = account;
64+
act.name = name;
65+
act.data = abi_ser.variant_to_binary( action_type_name, data );
66+
67+
return base_tester::push_action( std::move(act), auth ? uint64_t(signer) : 0 );
68+
}
69+
70+
71+
void create_currency( name contract, name manager, asset maxsupply ) {
72+
push_action(contract, manager, N(create), mutable_variant_object()
73+
("issuer", manager )
74+
("maximum_supply", maxsupply )
75+
("can_freeze", 0)
76+
("can_recall", 0)
77+
("can_whitelist", 0)
78+
);
79+
}
80+
81+
void issue( name contract, name manager, name to, asset amount ) {
82+
push_action( contract, manager, N(issue), mutable_variant_object()
83+
("to", to )
84+
("quantity", amount )
85+
("memo", "")
86+
);
87+
}
88+
89+
90+
91+
action_result stake(const account_name& from, const account_name& to, const string& net, const string& cpu, const string& storage ) {
92+
return push_action( name(from), name(from), N(delegatebw), mvo()
93+
("from", from)
94+
("receiver", to)
95+
("stake_net", net)
96+
("stake_cpu", cpu)
97+
("stake_storage", storage)
98+
);
99+
}
100+
#if _READY
101+
fc::variant get_total_stake( const account_name& act )
102+
{
103+
vector<char> data = get_row_by_account( config::system_account_name, act, N(totalband), act );
104+
return data.empty() ? fc::variant() : abi_ser.binary_to_variant( "total_resources", data );
105+
}
106+
#endif
107+
108+
action_result stake( const account_name& acnt, const string& net, const string& cpu, const string& storage ) {
109+
return stake( acnt, acnt, net, cpu, storage );
110+
}
111+
112+
asset get_balance( const account_name& act )
113+
{
114+
//return get_currency_balance( config::system_account_name, symbol(SY(4,EOS)), act );
115+
//temporary code. current get_currency_balancy uses table name N(accounts) from currency.h
116+
//generic_currency table name is N(account).
117+
const auto& db = control->get_database();
118+
const auto* tbl = db.find<contracts::table_id_object, contracts::by_code_scope_table>(boost::make_tuple(config::system_account_name, act, N(account)));
119+
share_type result = 0;
120+
121+
// the balance is implied to be 0 if either the table or row does not exist
122+
if (tbl) {
123+
const auto *obj = db.find<contracts::key_value_object, contracts::by_scope_primary>(boost::make_tuple(tbl->id, symbol(SY(4,EOS)).value()));
124+
if (obj) {
125+
//balance is the second field after symbol, so skip the symbol
126+
fc::datastream<const char *> ds(obj->value.data(), obj->value.size());
127+
fc::raw::unpack(ds, result);
128+
}
129+
}
130+
return asset( result, symbol(SY(4,EOS)) );
131+
}
132+
133+
action_result regproducer( const account_name& acnt, int params_fixture = 1 ) {
134+
return push_action( acnt, acnt, N(regproducer), mvo()
135+
("producer", name(acnt).to_string() )
136+
("producer_key", fc::raw::pack( get_public_key( acnt, "active" ) ) )
137+
("prefs", producer_parameters_example( params_fixture ) )
138+
);
139+
}
140+
141+
void set_code_abi(const account_name& account, const char* wast, const char* abi)
142+
{
143+
set_code(account, wast);
144+
set_abi(account, abi);
145+
produce_blocks();
146+
}
147+
148+
149+
abi_serializer abi_ser;
150+
};
151+
152+
BOOST_AUTO_TEST_SUITE(bootseq_tests)
153+
154+
BOOST_FIXTURE_TEST_CASE( bootseq_test, bootseq_tester ) {
155+
try {
156+
157+
// Create the following accounts:
158+
// eosio.msig
159+
// eosio.token
160+
create_accounts({N(eosio.msig), N(eosio.token)});
161+
162+
// Set code for the following accounts:
163+
// eosio.system (code: eosio.bios)
164+
// eosio.msig (code: eosio.msig)
165+
// eosio.token (code: eosio.token)
166+
// These contracts are still under dev
167+
#if _READY
168+
set_code_abi(config::system_account_name, eosio_bios_wast, eosio_bios_abi);
169+
set_code_abi(N(eosio.msig), eosio_msig_wast, eosio_msig_abi);
170+
set_code_abi(N(eosio.token), eosio_token_wast, eosio_token_abi);
171+
#endif
172+
173+
// Set privileges for eosio.msig
174+
auto trace = push_action(N(eosio.bios), N(eosio.bios), N(setprivil), mutable_variant_object()
175+
("account", "eosio.token")
176+
("is_priv", 1)
177+
);
178+
// Todo : how to check the privilege is set? (use is_priv action)
179+
180+
181+
auto expected = asset::from_string("1000000000.0000 EOS");
182+
// Create EOS tokens in eosio.token, set its manager as eosio.system
183+
create_currency(N(eosio.token), config::system_account_name, expected);
184+
// Issue the genesis supply of 1 billion EOS tokens to eosio.system
185+
// Issue the genesis supply of 1 billion EOS tokens to eosio.system
186+
issue(N(eosio.token), config::system_account_name, config::system_account_name, expected);
187+
auto actual = get_balance(config::system_account_name);
188+
BOOST_REQUIRE_EQUAL(expected, actual);
189+
190+
// Create a few genesis accounts
191+
std::vector<account_name> gen_accounts{N(inita), N(initb), N(initc)};
192+
create_accounts(gen_accounts);
193+
// Transfer EOS to genesis accounts
194+
for (auto gen_acc : gen_accounts) {
195+
auto quantity = "10000.0000 EOS";
196+
auto stake_quantity = "5000.0000 EOS";
197+
198+
auto trace = push_action(N(eosio.token), config::system_account_name, N(transfer), mutable_variant_object()
199+
("from", config::system_account_name)
200+
("to", gen_acc)
201+
("quantity", quantity)
202+
("memo", gen_acc)
203+
);
204+
205+
auto balance = get_balance(gen_acc);
206+
BOOST_REQUIRE_EQUAL(asset::from_string(quantity), balance);
207+
#if _READY
208+
// Stake 50% of balance to CPU and other 50% to bandwidth
209+
BOOST_REQUIRE_EQUAL(success(),
210+
stake(config::system_account_name, gen_acc, stake_quantity, stake_quantity, ""));
211+
auto total = get_total_stake(gen_acc);
212+
BOOST_REQUIRE_EQUAL(asset::from_string(stake_quantity).amount, total["net_weight"].as_uint64());
213+
BOOST_REQUIRE_EQUAL(asset::from_string(stake_quantity).amount, total["cpu_weight"].as_uint64());
214+
#endif
215+
}
216+
217+
// Set code eosio.system from eosio.bios to eosio.system
218+
set_code_abi(config::system_account_name, eosio_system_wast, eosio_system_abi);
219+
220+
// Register these genesis accts as producer account
221+
for (auto gen_acc : gen_accounts) {
222+
BOOST_REQUIRE_EQUAL(success(), regproducer(gen_acc));
223+
}
224+
225+
} FC_LOG_AND_RETHROW()
226+
}
227+
228+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)