forked from bitshares/bitshares-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
380 lines (316 loc) · 15.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <graphene/app/application.hpp>
#include <graphene/app/plugin.hpp>
#include <graphene/utilities/tempdir.hpp>
#include <graphene/account_history/account_history_plugin.hpp>
#include <graphene/witness/witness.hpp>
#include <graphene/market_history/market_history_plugin.hpp>
#include <graphene/egenesis/egenesis.hpp>
#include <graphene/wallet/wallet.hpp>
#include <fc/thread/thread.hpp>
#include <fc/smart_ref_impl.hpp>
#include <fc/network/http/websocket.hpp>
#include <fc/rpc/websocket_api.hpp>
#include <fc/rpc/cli.hpp>
#include <boost/filesystem/path.hpp>
#define BOOST_TEST_MODULE Test Application
#include <boost/test/included/unit_test.hpp>
/*********************
* Helper Methods
*********************/
// hack: import create_example_genesis() even though it's a way, way
// specific internal detail
namespace graphene { namespace app { namespace detail {
graphene::chain::genesis_state_type create_example_genesis();
} } } // graphene::app::detail
boost::filesystem::path create_genesis_file(fc::temp_directory& directory) {
boost::filesystem::path genesis_path = boost::filesystem::path{directory.path().generic_string()} / "genesis.json";
fc::path genesis_out = genesis_path;
graphene::chain::genesis_state_type genesis_state = graphene::app::detail::create_example_genesis();
std::cerr << "Creating example genesis state in file " << genesis_out.generic_string() << "\n";
fc::json::save_to_file(genesis_state, genesis_out);
return genesis_path;
}
/***
* @brief Start the application, listening on port 8090
* @param app_dir the temporary directory to use
* @returns the application object
*/
std::shared_ptr<graphene::app::application> start_application(fc::temp_directory& app_dir) {
std::shared_ptr<graphene::app::application> app1(new graphene::app::application{});
app1->register_plugin<graphene::account_history::account_history_plugin>();
app1->register_plugin< graphene::market_history::market_history_plugin >();
app1->register_plugin< graphene::witness_plugin::witness_plugin >();
app1->startup_plugins();
boost::program_options::variables_map cfg;
cfg.emplace("rpc-endpoint", boost::program_options::variable_value(string("127.0.0.1:8090"), false));
cfg.emplace("genesis-json", boost::program_options::variable_value(create_genesis_file(app_dir), false));
cfg.emplace("seed-nodes", boost::program_options::variable_value(string("[]"), false));
app1->initialize(app_dir.path(), cfg);
app1->startup();
fc::usleep(fc::milliseconds(500));
return app1;
}
/****
* Send a block to the db
* @param app the application
* @returns true on success
*/
bool generate_block(std::shared_ptr<graphene::app::application> app) {
try {
fc::ecc::private_key committee_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
auto db = app->chain_database();
auto block_1 = db->generate_block(
db->get_slot_time(1),
db->get_scheduled_witness(1),
committee_key,
database::skip_nothing);
return true;
} catch (exception &e) {
return false;
}
}
/****
* @brief Skip intermediate blocks, and generate a maintenance block
* @param app the application
* @returns true on success
*/
bool generate_maintenance_block(std::shared_ptr<graphene::app::application> app) {
try {
fc::ecc::private_key committee_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
uint32_t skip = ~0;
auto db = app->chain_database();
auto maint_time = db->get_dynamic_global_properties().next_maintenance_time;
auto slots_to_miss = db->get_slot_at_time(maint_time);
db->generate_block(db->get_slot_time(slots_to_miss),
db->get_scheduled_witness(slots_to_miss),
committee_key,
skip);
return true;
} catch (exception& e) {
return false;
}
}
/****************************
* Tests
****************************/
/**
* Start a server and connect using the same calls as the CLI
*/
BOOST_AUTO_TEST_CASE( cli_connect )
{
using namespace graphene::chain;
using namespace graphene::app;
std::shared_ptr<graphene::app::application> app1;
try {
fc::temp_directory app_dir ( graphene::utilities::temp_directory_path() );
app1 = start_application(app_dir);
// connect to the server
graphene::wallet::wallet_data wdata;
wdata.chain_id = app1->chain_database()->get_chain_id();
wdata.ws_server = "ws://127.0.0.1:8090";
wdata.ws_user = "";
wdata.ws_password = "";
fc::http::websocket_client client;
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*(client.connect(wdata.ws_server)));
auto remote_api = apic->get_remote_api< login_api >(1);
BOOST_CHECK(remote_api->login( wdata.ws_user, wdata.ws_password ) );
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
app1->shutdown();
}
/**
* Start a server and connect using the same calls as the CLI
*/
BOOST_AUTO_TEST_CASE( cli_vote_for_2_witnesses )
{
using namespace graphene::chain;
using namespace graphene::app;
std::shared_ptr<graphene::app::application> app1;
try {
fc::temp_directory app_dir( graphene::utilities::temp_directory_path() );
app1 = start_application(app_dir);
// connect to the server
graphene::wallet::wallet_data wdata;
wdata.chain_id = app1->chain_database()->get_chain_id();
wdata.ws_server = "ws://127.0.0.1:8090";
wdata.ws_user = "";
wdata.ws_password = "";
fc::http::websocket_client client;
auto con = client.connect( wdata.ws_server );
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
auto remote_api = apic->get_remote_api< login_api >(1);
BOOST_CHECK(remote_api->login( wdata.ws_user, wdata.ws_password ) );
auto wapiptr = std::make_shared<graphene::wallet::wallet_api>(wdata, remote_api);
std::stringstream wallet_filename;
wallet_filename << app_dir.path().generic_string() << "/wallet.json";
wapiptr->set_wallet_filename(wallet_filename.str());
fc::api<graphene::wallet::wallet_api> wapi(wapiptr);
auto wallet_cli = std::make_shared<fc::rpc::cli>();
for( auto& name_formatter : wapiptr->get_result_formatters() )
wallet_cli->format_result( name_formatter.first, name_formatter.second );
boost::signals2::scoped_connection closed_connection(con->closed.connect([=]{
cerr << "Server has disconnected us.\n";
wallet_cli->stop();
}));
(void)(closed_connection);
BOOST_TEST_MESSAGE("Setting wallet password");
wapiptr->set_password("supersecret");
wapiptr->unlock("supersecret");
// import Nathan account
BOOST_TEST_MESSAGE("Importing nathan key");
std::vector<std::string> nathan_keys{"5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"};
BOOST_CHECK_EQUAL(nathan_keys[0], "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3");
BOOST_CHECK(wapiptr->import_key("nathan", nathan_keys[0]));
BOOST_TEST_MESSAGE("Importing nathan's balance");
std::vector<signed_transaction> import_txs = wapiptr->import_balance("nathan", nathan_keys, true);
account_object nathan_acct_before_upgrade = wapiptr->get_account("nathan");
// upgrade nathan
BOOST_TEST_MESSAGE("Upgrading Nathan to LTM");
signed_transaction upgrade_tx = wapiptr->upgrade_account("nathan", true);
account_object nathan_acct_after_upgrade = wapiptr->get_account("nathan");
// verify that the upgrade was successful
BOOST_CHECK_PREDICATE( std::not_equal_to<uint32_t>(), (nathan_acct_before_upgrade.membership_expiration_date.sec_since_epoch())(nathan_acct_after_upgrade.membership_expiration_date.sec_since_epoch()) );
BOOST_CHECK(nathan_acct_after_upgrade.is_lifetime_member());
// create a new account
graphene::wallet::brain_key_info bki = wapiptr->suggest_brain_key();
BOOST_CHECK(!bki.brain_priv_key.empty());
signed_transaction create_acct_tx = wapiptr->create_account_with_brain_key(bki.brain_priv_key, "jmjatlanta", "nathan", "nathan", true);
// save the private key for this new account in the wallet file
BOOST_CHECK(wapiptr->import_key("jmjatlanta", bki.wif_priv_key));
wapiptr->save_wallet_file(wallet_filename.str());
// attempt to give jmjatlanta some bitsahres
BOOST_TEST_MESSAGE("Transferring bitshares from Nathan to jmjatlanta");
signed_transaction transfer_tx = wapiptr->transfer("nathan", "jmjatlanta", "10000", "BTS", "Here are some BTS for your new account", true);
// get the details for init1
witness_object init1_obj = wapiptr->get_witness("init1");
int init1_start_votes = init1_obj.total_votes;
// Vote for a witness
signed_transaction vote_witness1_tx = wapiptr->vote_for_witness("jmjatlanta", "init1", true, true);
// generate a block to get things started
BOOST_CHECK(generate_block(app1));
// wait for a maintenance interval
BOOST_CHECK(generate_maintenance_block(app1));
// Verify that the vote is there
init1_obj = wapiptr->get_witness("init1");
witness_object init2_obj = wapiptr->get_witness("init2");
int init1_middle_votes = init1_obj.total_votes;
BOOST_CHECK(init1_middle_votes > init1_start_votes);
// Vote for a 2nd witness
int init2_start_votes = init2_obj.total_votes;
signed_transaction vote_witness2_tx = wapiptr->vote_for_witness("jmjatlanta", "init2", true, true);
// send another block to trigger maintenance interval
BOOST_CHECK(generate_maintenance_block(app1));
// Verify that both the first vote and the 2nd are there
init2_obj = wapiptr->get_witness("init2");
init1_obj = wapiptr->get_witness("init1");
int init2_middle_votes = init2_obj.total_votes;
BOOST_CHECK(init2_middle_votes > init2_start_votes);
int init1_last_votes = init1_obj.total_votes;
BOOST_CHECK(init1_last_votes > init1_start_votes);
// wait for everything to finish up
fc::usleep(fc::seconds(1));
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
app1->shutdown();
}
/**
* Start a server and connect using the same calls as the CLI
*/
BOOST_AUTO_TEST_CASE( cli_set_voting_proxy )
{
using namespace graphene::chain;
using namespace graphene::app;
std::shared_ptr<graphene::app::application> app1;
try {
fc::temp_directory app_dir( graphene::utilities::temp_directory_path() );
app1 = start_application(app_dir);
// connect to the server
graphene::wallet::wallet_data wdata;
wdata.chain_id = app1->chain_database()->get_chain_id();
wdata.ws_server = "ws://127.0.0.1:8090";
wdata.ws_user = "";
wdata.ws_password = "";
fc::http::websocket_client client;
auto con = client.connect( wdata.ws_server );
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
auto remote_api = apic->get_remote_api< login_api >(1);
BOOST_CHECK(remote_api->login( wdata.ws_user, wdata.ws_password ) );
auto wapiptr = std::make_shared<graphene::wallet::wallet_api>(wdata, remote_api);
std::stringstream wallet_filename;
wallet_filename << app_dir.path().generic_string() << "/wallet.json";
wapiptr->set_wallet_filename(wallet_filename.str());
fc::api<graphene::wallet::wallet_api> wapi(wapiptr);
auto wallet_cli = std::make_shared<fc::rpc::cli>();
for( auto& name_formatter : wapiptr->get_result_formatters() )
wallet_cli->format_result( name_formatter.first, name_formatter.second );
boost::signals2::scoped_connection closed_connection(con->closed.connect([=]{
cerr << "Server has disconnected us.\n";
wallet_cli->stop();
}));
(void)(closed_connection);
BOOST_TEST_MESSAGE("Setting wallet password");
wapiptr->set_password("supersecret");
wapiptr->unlock("supersecret");
// import Nathan account
BOOST_TEST_MESSAGE("Importing nathan key");
std::vector<std::string> nathan_keys{"5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"};
BOOST_CHECK_EQUAL(nathan_keys[0], "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3");
BOOST_CHECK(wapiptr->import_key("nathan", nathan_keys[0]));
BOOST_TEST_MESSAGE("Importing nathan's balance");
std::vector<signed_transaction> import_txs = wapiptr->import_balance("nathan", nathan_keys, true);
account_object nathan_acct_before_upgrade = wapiptr->get_account("nathan");
// upgrade nathan
BOOST_TEST_MESSAGE("Upgrading Nathan to LTM");
signed_transaction upgrade_tx = wapiptr->upgrade_account("nathan", true);
account_object nathan_acct_after_upgrade = wapiptr->get_account("nathan");
// verify that the upgrade was successful
BOOST_CHECK_PREDICATE( std::not_equal_to<uint32_t>(), (nathan_acct_before_upgrade.membership_expiration_date.sec_since_epoch())(nathan_acct_after_upgrade.membership_expiration_date.sec_since_epoch()) );
BOOST_CHECK(nathan_acct_after_upgrade.is_lifetime_member());
// create a new account
graphene::wallet::brain_key_info bki = wapiptr->suggest_brain_key();
BOOST_CHECK(!bki.brain_priv_key.empty());
signed_transaction create_acct_tx = wapiptr->create_account_with_brain_key(bki.brain_priv_key, "jmjatlanta", "nathan", "nathan", true);
// save the private key for this new account in the wallet file
BOOST_CHECK(wapiptr->import_key("jmjatlanta", bki.wif_priv_key));
wapiptr->save_wallet_file(wallet_filename.str());
// attempt to give jmjatlanta some bitsahres
BOOST_TEST_MESSAGE("Transferring bitshares from Nathan to jmjatlanta");
signed_transaction transfer_tx = wapiptr->transfer("nathan", "jmjatlanta", "10000", "BTS", "Here are some BTS for your new account", true);
// set the voting proxy to nathan
BOOST_TEST_MESSAGE("About to set voting proxy.");
signed_transaction voting_tx = wapiptr->set_voting_proxy("jmjatlanta", "nathan", true);
// wait for everything to finish up
fc::usleep(fc::seconds(1));
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
app1->shutdown();
}