-
Notifications
You must be signed in to change notification settings - Fork 648
/
main.cpp
2313 lines (1966 loc) · 96.5 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018 John Jones, 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/key_conversion.hpp>
#include <graphene/utilities/tempdir.hpp>
#include <graphene/account_history/account_history_plugin.hpp>
#include <graphene/api_helper_indexes/api_helper_indexes.hpp>
#include <graphene/market_history/market_history_plugin.hpp>
#include <graphene/custom_operations/custom_operations_plugin.hpp>
#include <graphene/egenesis/egenesis.hpp>
#include <graphene/wallet/wallet.hpp>
#include <graphene/chain/hardfork.hpp>
#include <fc/thread/thread.hpp>
#include <fc/network/http/websocket.hpp>
#include <fc/rpc/websocket_api.hpp>
#include <fc/rpc/cli.hpp>
#include <fc/crypto/base58.hpp>
#include <fc/crypto/hex.hpp>
#include <fc/crypto/aes.hpp>
#include <thread>
#include <boost/filesystem/path.hpp>
#include "../common/init_unit_test_suite.hpp"
#include "../common/genesis_file_util.hpp"
#include "../common/program_options_util.hpp"
#include "../common/utils.hpp"
#ifdef _WIN32
/*****
* Global Initialization for Windows
* ( sets up Winsock stuf )
*/
int sockInit(void)
{
WSADATA wsa_data;
return WSAStartup(MAKEWORD(1,1), &wsa_data);
}
int sockQuit(void)
{
return WSACleanup();
}
#endif
/*********************
* Helper Methods
*********************/
using std::exception;
using std::cerr;
#define INVOKE(test) ((struct test*)this)->test_method();
///////////
/// @brief Start the application
/// @param app_dir the temporary directory to use
/// @param server_port_number to be filled with the rpc endpoint port number
/// @returns the application object
//////////
std::shared_ptr<graphene::app::application> start_application(fc::temp_directory& app_dir, int& server_port_number) {
auto app1 = std::make_shared<graphene::app::application>();
app1->register_plugin<graphene::account_history::account_history_plugin>(true);
app1->register_plugin< graphene::market_history::market_history_plugin >(true);
app1->register_plugin< graphene::grouped_orders::grouped_orders_plugin>(true);
app1->register_plugin< graphene::api_helper_indexes::api_helper_indexes>(true);
app1->register_plugin<graphene::custom_operations::custom_operations_plugin>(true);
auto sharable_cfg = std::make_shared<boost::program_options::variables_map>();
auto& cfg = *sharable_cfg;
server_port_number = fc::network::get_available_port();
auto p2p_port = server_port_number;
for( size_t i = 0; i < 10 && p2p_port == server_port_number; ++i )
{
p2p_port = fc::network::get_available_port();
}
BOOST_REQUIRE( p2p_port != server_port_number );
fc::set_option( cfg, "rpc-endpoint", string("127.0.0.1:") + std::to_string(server_port_number) );
fc::set_option( cfg, "p2p-endpoint", string("0.0.0.0:") + std::to_string(p2p_port) );
fc::set_option( cfg, "genesis-json", create_genesis_file(app_dir) );
fc::set_option( cfg, "seed-nodes", string("[]") );
fc::set_option( cfg, "custom-operations-start-block", uint32_t(1) );
app1->initialize(app_dir.path(), sharable_cfg);
app1->startup();
return app1;
}
///////////
/// Send a block to the db
/// @param app the application
/// @param returned_block the signed block
/// @returns true on success
///////////
bool generate_block(std::shared_ptr<graphene::app::application> app, graphene::chain::signed_block& returned_block)
{
try {
fc::ecc::private_key committee_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
auto db = app->chain_database();
returned_block = 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;
}
}
bool generate_block(std::shared_ptr<graphene::app::application> app)
{
graphene::chain::signed_block returned_block;
return generate_block(app, returned_block);
}
signed_block generate_block(std::shared_ptr<graphene::app::application> app, uint32_t skip, const fc::ecc::private_key& key, int miss_blocks)
{
// skip == ~0 will skip checks specified in database::validation_steps
skip |= database::skip_undo_history_check;
auto db = app->chain_database();
auto block = db->generate_block(db->get_slot_time(miss_blocks + 1),
db->get_scheduled_witness(miss_blocks + 1),
key, skip);
db->clear_pending();
return block;
}
//////
// Generate blocks until the timestamp
//////
uint32_t generate_blocks(std::shared_ptr<graphene::app::application> app, fc::time_point_sec timestamp)
{
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();
generate_block(app);
auto slots_to_miss = db->get_slot_at_time(timestamp);
if( slots_to_miss <= 1 )
return 1;
--slots_to_miss;
generate_block(app, skip, committee_key, slots_to_miss);
return 2;
}
///////////
/// @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;
}
}
///////////
/// Check if hardfork core-2262 has passed
///////////
bool is_hf2262_passed(std::shared_ptr<graphene::app::application> app) {
auto db = app->chain_database();
auto maint_time = db->get_dynamic_global_properties().next_maintenance_time;
return HARDFORK_CORE_2262_PASSED( maint_time );
}
///////////
/// @brief a class to make connecting to the application server easier
///////////
class client_connection
{
public:
/////////
// constructor
/////////
client_connection(
std::shared_ptr<graphene::app::application> app,
const fc::temp_directory& data_dir,
const int server_port_number,
const std::string custom_wallet_filename = "wallet.json"
)
{
wallet_data.chain_id = app->chain_database()->get_chain_id();
wallet_data.ws_server = "ws://127.0.0.1:" + std::to_string(server_port_number);
wallet_data.ws_user = "";
wallet_data.ws_password = "";
websocket_connection = websocket_client.connect( wallet_data.ws_server );
api_connection = std::make_shared<fc::rpc::websocket_api_connection>( websocket_connection,
GRAPHENE_MAX_NESTED_OBJECTS );
remote_login_api = api_connection->get_remote_api< graphene::app::login_api >(1);
BOOST_CHECK(remote_login_api->login( wallet_data.ws_user, wallet_data.ws_password ) );
wallet_api_ptr = std::make_shared<graphene::wallet::wallet_api>(wallet_data, remote_login_api);
wallet_filename = data_dir.path().generic_string() + "/" + custom_wallet_filename;
wallet_api_ptr->set_wallet_filename(wallet_filename);
wallet_api = fc::api<graphene::wallet::wallet_api>(wallet_api_ptr);
wallet_cli = std::make_shared<fc::rpc::cli>(GRAPHENE_MAX_NESTED_OBJECTS);
for( auto& name_formatter : wallet_api_ptr->get_result_formatters() )
wallet_cli->format_result( name_formatter.first, name_formatter.second );
}
~client_connection()
{
wallet_cli->stop();
}
public:
fc::http::websocket_client websocket_client;
graphene::wallet::wallet_data wallet_data;
fc::http::websocket_connection_ptr websocket_connection;
std::shared_ptr<fc::rpc::websocket_api_connection> api_connection;
fc::api<login_api> remote_login_api;
std::shared_ptr<graphene::wallet::wallet_api> wallet_api_ptr;
fc::api<graphene::wallet::wallet_api> wallet_api;
std::shared_ptr<fc::rpc::cli> wallet_cli;
std::string wallet_filename;
};
///////////////////////////////
// Cli Wallet Fixture
///////////////////////////////
struct cli_fixture
{
#ifdef _WIN32
struct socket_maintainer {
socket_maintainer() {
sockInit();
}
~socket_maintainer() {
sockQuit();
}
} sock_maintainer;
#endif
int server_port_number;
fc::temp_directory app_dir;
std::shared_ptr<graphene::app::application> app1;
client_connection con;
std::vector<std::string> nathan_keys;
cli_fixture() :
server_port_number(0),
app_dir( graphene::utilities::temp_directory_path() ),
app1( start_application(app_dir, server_port_number) ),
con( app1, app_dir, server_port_number ),
nathan_keys( {"5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3"} )
{
BOOST_TEST_MESSAGE("Setup cli_wallet::boost_fixture_test_case");
using namespace graphene::chain;
using namespace graphene::app;
try
{
BOOST_TEST_MESSAGE("Setting wallet password");
con.wallet_api_ptr->set_password("supersecret");
con.wallet_api_ptr->unlock("supersecret");
// import Nathan account
BOOST_TEST_MESSAGE("Importing nathan key");
BOOST_CHECK_EQUAL(nathan_keys[0], "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3");
BOOST_CHECK(con.wallet_api_ptr->import_key("nathan", nathan_keys[0]));
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
~cli_fixture()
{
BOOST_TEST_MESSAGE("Cleanup cli_wallet::boost_fixture_test_case");
}
};
///////////////////////////////
// Tests
///////////////////////////////
////////////////
// Start a server and connect using the same calls as the CLI
////////////////
BOOST_FIXTURE_TEST_CASE( cli_connect, cli_fixture )
{
BOOST_TEST_MESSAGE("Testing wallet connection.");
}
////////////////
// Start a server and connect using the same calls as the CLI
// Quit wallet and be sure that file was saved correctly
////////////////
BOOST_FIXTURE_TEST_CASE( cli_quit, cli_fixture )
{
BOOST_TEST_MESSAGE("Testing wallet connection and quit command.");
BOOST_CHECK_THROW( con.wallet_api_ptr->quit(), fc::canceled_exception );
}
BOOST_FIXTURE_TEST_CASE( cli_help_gethelp, cli_fixture )
{
BOOST_TEST_MESSAGE("Testing help and gethelp commands.");
auto formatters = con.wallet_api_ptr->get_result_formatters();
string result = con.wallet_api_ptr->help();
BOOST_CHECK( result.find("gethelp") != string::npos );
if( formatters.find("help") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of help");
string output = formatters["help"](fc::variant(result), fc::variants());
BOOST_CHECK( output.find("gethelp") != string::npos );
}
result = con.wallet_api_ptr->gethelp( "transfer" );
BOOST_CHECK( result.find("usage") != string::npos );
if( formatters.find("gethelp") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of gethelp");
string output = formatters["gethelp"](fc::variant(result), fc::variants());
BOOST_CHECK( output.find("usage") != string::npos );
}
}
BOOST_FIXTURE_TEST_CASE( upgrade_nathan_account, cli_fixture )
{
try
{
BOOST_TEST_MESSAGE("Upgrade Nathan's account");
account_object nathan_acct_before_upgrade, nathan_acct_after_upgrade;
std::vector<signed_transaction> import_txs;
signed_transaction upgrade_tx;
BOOST_TEST_MESSAGE("Importing nathan's balance");
import_txs = con.wallet_api_ptr->import_balance("nathan", nathan_keys, true);
nathan_acct_before_upgrade = con.wallet_api_ptr->get_account("nathan");
// upgrade nathan
BOOST_TEST_MESSAGE("Upgrading Nathan to LTM");
upgrade_tx = con.wallet_api_ptr->upgrade_account("nathan", true);
nathan_acct_after_upgrade = con.wallet_api_ptr->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());
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_FIXTURE_TEST_CASE( create_new_account, cli_fixture )
{
try
{
INVOKE(upgrade_nathan_account);
// create a new account
graphene::wallet::brain_key_info bki = con.wallet_api_ptr->suggest_brain_key();
BOOST_CHECK(!bki.brain_priv_key.empty());
signed_transaction create_acct_tx = con.wallet_api_ptr->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(con.wallet_api_ptr->import_key("jmjatlanta", bki.wif_priv_key));
con.wallet_api_ptr->save_wallet_file(con.wallet_filename);
// attempt to give jmjatlanta some bitshares
BOOST_TEST_MESSAGE("Transferring bitshares from Nathan to jmjatlanta");
signed_transaction transfer_tx = con.wallet_api_ptr->transfer(
"nathan", "jmjatlanta", "10000", "1.3.0", "Here are some CORE token for your new account", true
);
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_FIXTURE_TEST_CASE( uia_tests, cli_fixture )
{
try
{
BOOST_TEST_MESSAGE("Cli UIA Tests");
INVOKE(upgrade_nathan_account);
BOOST_CHECK(generate_block(app1));
account_object nathan_acct = con.wallet_api_ptr->get_account("nathan");
auto formatters = con.wallet_api_ptr->get_result_formatters();
auto check_account_last_history = [&]( string account, string keyword ) {
auto history = con.wallet_api_ptr->get_relative_account_history(account, 0, 1, 0);
BOOST_REQUIRE_GT( history.size(), 0 );
BOOST_CHECK( history[0].description.find( keyword ) != string::npos );
};
auto check_nathan_last_history = [&]( string keyword ) {
check_account_last_history( "nathan", keyword );
};
check_nathan_last_history( "account_upgrade_operation" );
// Create new asset called BOBCOIN
{
BOOST_TEST_MESSAGE("Create UIA 'BOBCOIN'");
graphene::chain::asset_options asset_ops;
asset_ops.issuer_permissions = DEFAULT_UIA_ASSET_ISSUER_PERMISSION;
asset_ops.flags = charge_market_fee | override_authority;
asset_ops.max_supply = 1000000;
asset_ops.core_exchange_rate = price(asset(2),asset(1,asset_id_type(1)));
auto result = con.wallet_api_ptr->create_asset("nathan", "BOBCOIN", 4, asset_ops, {}, true);
if( formatters.find("create_asset") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of create_asset");
string output = formatters["create_asset"](
fc::variant(result, FC_PACK_MAX_DEPTH), fc::variants());
BOOST_CHECK( output.find("BOBCOIN") != string::npos );
}
BOOST_CHECK_THROW( con.wallet_api_ptr->get_asset_name("BOBCOI"), fc::exception );
BOOST_CHECK_EQUAL( con.wallet_api_ptr->get_asset_name("BOBCOIN"), "BOBCOIN" );
BOOST_CHECK_EQUAL( con.wallet_api_ptr->get_asset_symbol("BOBCOIN"), "BOBCOIN" );
BOOST_CHECK_THROW( con.wallet_api_ptr->get_account_name("nath"), fc::exception );
BOOST_CHECK_EQUAL( con.wallet_api_ptr->get_account_name("nathan"), "nathan" );
BOOST_CHECK( con.wallet_api_ptr->get_account_id("nathan") == con.wallet_api_ptr->get_account("nathan").id );
}
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Create User-Issue Asset" );
check_nathan_last_history( "BOBCOIN" );
auto bobcoin = con.wallet_api_ptr->get_asset("BOBCOIN");
BOOST_CHECK( con.wallet_api_ptr->get_asset_id("BOBCOIN") == bobcoin.id );
bool balance_formatter_tested = false;
auto check_bobcoin_balance = [&](string account, int64_t amount) {
auto balances = con.wallet_api_ptr->list_account_balances( account );
size_t count = 0;
for( auto& bal : balances )
{
if( bal.asset_id == bobcoin.id )
{
++count;
BOOST_CHECK_EQUAL( bal.amount.value, amount );
}
}
BOOST_CHECK_EQUAL(count, 1u);
// Testing result formatter
if( !balance_formatter_tested && formatters.find("list_account_balances") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of list_account_balances");
string output = formatters["list_account_balances"](
fc::variant(balances, FC_PACK_MAX_DEPTH ), fc::variants());
BOOST_CHECK( output.find("BOBCOIN") != string::npos );
balance_formatter_tested = true;
}
};
auto check_nathan_bobcoin_balance = [&](int64_t amount) {
check_bobcoin_balance( "nathan", amount );
};
{
// Issue asset
BOOST_TEST_MESSAGE("Issue asset");
con.wallet_api_ptr->issue_asset("init0", "3", "BOBCOIN", "new coin for you", true);
}
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "nathan issue 3 BOBCOIN to init0" );
check_nathan_last_history( "new coin for you" );
check_account_last_history( "init0", "nathan issue 3 BOBCOIN to init0" );
check_account_last_history( "init0", "new coin for you" );
check_bobcoin_balance( "init0", 30000 );
{
// Override transfer, and test sign_memo and read_memo by the way
BOOST_TEST_MESSAGE("Override-transfer BOBCOIN from init0");
auto handle = con.wallet_api_ptr->begin_builder_transaction();
override_transfer_operation op;
op.issuer = con.wallet_api_ptr->get_account( "nathan" ).id;
op.from = con.wallet_api_ptr->get_account( "init0" ).id;
op.to = con.wallet_api_ptr->get_account( "nathan" ).id;
op.amount = bobcoin.amount(10000);
const auto test_bki = con.wallet_api_ptr->suggest_brain_key();
auto test_pubkey = fc::json::to_string( test_bki.pub_key );
test_pubkey = test_pubkey.substr( 1, test_pubkey.size() - 2 );
idump( (test_pubkey) );
op.memo = con.wallet_api_ptr->sign_memo( "nathan", test_pubkey, "get back some coin" );
idump( (op.memo) );
con.wallet_api_ptr->add_operation_to_builder_transaction( handle, op );
con.wallet_api_ptr->set_fees_on_builder_transaction( handle, "1.3.0" );
con.wallet_api_ptr->sign_builder_transaction( handle, true );
auto memo = con.wallet_api_ptr->read_memo( *op.memo );
BOOST_CHECK_EQUAL( memo, "get back some coin" );
op.memo = con.wallet_api_ptr->sign_memo( test_pubkey, "nathan", "another test" );
idump( (op.memo) );
memo = con.wallet_api_ptr->read_memo( *op.memo );
BOOST_CHECK_EQUAL( memo, "another test" );
BOOST_CHECK_THROW( con.wallet_api_ptr->sign_memo( "non-exist-account-or-label", "nathan", "some text" ),
fc::exception );
BOOST_CHECK_THROW( con.wallet_api_ptr->sign_memo( "nathan", "non-exist-account-or-label", "some text" ),
fc::exception );
}
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "nathan force-transfer 1 BOBCOIN from init0 to nathan" );
check_nathan_last_history( "get back some coin" );
check_account_last_history( "init0", "nathan force-transfer 1 BOBCOIN from init0 to nathan" );
check_account_last_history( "init0", "get back some coin" );
check_bobcoin_balance( "init0", 20000 );
check_bobcoin_balance( "nathan", 10000 );
{
// Reserve / burn asset
BOOST_TEST_MESSAGE("Reserve/burn asset");
con.wallet_api_ptr->reserve_asset("nathan", "1", "BOBCOIN", true);
}
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Reserve (burn) 1 BOBCOIN" );
check_nathan_bobcoin_balance( 0 );
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_FIXTURE_TEST_CASE( mpa_tests, cli_fixture )
{
try
{
BOOST_TEST_MESSAGE("Cli MPA Tests");
INVOKE(upgrade_nathan_account);
account_object nathan_acct = con.wallet_api_ptr->get_account("nathan");
auto formatters = con.wallet_api_ptr->get_result_formatters();
// Create new asset called BOBCOIN backed by CORE
try
{
BOOST_TEST_MESSAGE("Create MPA 'BOBCOIN'");
graphene::chain::asset_options asset_ops;
asset_ops.issuer_permissions = ASSET_ISSUER_PERMISSION_ENABLE_BITS_MASK;
asset_ops.flags = charge_market_fee;
asset_ops.max_supply = 1000000;
asset_ops.core_exchange_rate = price(asset(2),asset(1,asset_id_type(1)));
graphene::chain::bitasset_options bit_opts;
auto result = con.wallet_api_ptr->create_asset("nathan", "BOBCOIN", 4, asset_ops, bit_opts, true);
if( formatters.find("create_asset") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of create_asset");
string output = formatters["create_asset"](
fc::variant(result, FC_PACK_MAX_DEPTH), fc::variants());
BOOST_CHECK( output.find("BOBCOIN") != string::npos );
}
}
catch(exception& e)
{
BOOST_FAIL(e.what());
}
catch(...)
{
BOOST_FAIL("Unknown exception creating BOBCOIN");
}
BOOST_CHECK(generate_block(app1));
auto check_nathan_last_history = [&]( string keyword ) {
auto history = con.wallet_api_ptr->get_relative_account_history("nathan", 0, 1, 0);
BOOST_REQUIRE_GT( history.size(), 0 );
BOOST_CHECK( history[0].description.find( keyword ) != string::npos );
};
check_nathan_last_history( "Create BitAsset" );
check_nathan_last_history( "BOBCOIN" );
auto bobcoin = con.wallet_api_ptr->get_asset("BOBCOIN");
{
// Update asset
BOOST_TEST_MESSAGE("Update asset");
auto options = bobcoin.options;
BOOST_CHECK_EQUAL( options.max_supply.value, 1000000 );
options.max_supply = 2000000;
con.wallet_api_ptr->update_asset("BOBCOIN", {}, options, true);
// Check
bobcoin = con.wallet_api_ptr->get_asset("BOBCOIN");
BOOST_CHECK_EQUAL( bobcoin.options.max_supply.value, 2000000 );
}
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Update asset" );
auto bitbobcoin = con.wallet_api_ptr->get_bitasset_data("BOBCOIN");
{
// Update bitasset
BOOST_TEST_MESSAGE("Update bitasset");
auto bitoptions = bitbobcoin.options;
BOOST_CHECK_EQUAL( bitoptions.feed_lifetime_sec, uint32_t(GRAPHENE_DEFAULT_PRICE_FEED_LIFETIME) );
bitoptions.feed_lifetime_sec = 3600u;
con.wallet_api_ptr->update_bitasset("BOBCOIN", bitoptions, true);
// Check
bitbobcoin = con.wallet_api_ptr->get_bitasset_data("BOBCOIN");
BOOST_CHECK_EQUAL( bitbobcoin.options.feed_lifetime_sec, 3600u );
}
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Update bitasset" );
{
// Play with asset fee pool
auto objs = con.wallet_api_ptr->get_object( object_id_type( bobcoin.dynamic_asset_data_id ) )
.as<vector<asset_dynamic_data_object>>( FC_PACK_MAX_DEPTH );
idump( (objs) );
BOOST_REQUIRE_EQUAL( objs.size(), 1u );
asset_dynamic_data_object bobcoin_dyn = objs[0];
idump( (bobcoin_dyn) );
share_type old_pool = bobcoin_dyn.fee_pool;
BOOST_TEST_MESSAGE("Fund fee pool");
con.wallet_api_ptr->fund_asset_fee_pool("nathan", "BOBCOIN", "2", true);
objs = con.wallet_api_ptr->get_object( object_id_type( bobcoin.dynamic_asset_data_id ) )
.as<vector<asset_dynamic_data_object>>( FC_PACK_MAX_DEPTH );
BOOST_REQUIRE_EQUAL( objs.size(), 1u );
bobcoin_dyn = objs[0];
share_type funded_pool = bobcoin_dyn.fee_pool;
BOOST_CHECK_EQUAL( funded_pool.value, old_pool.value + GRAPHENE_BLOCKCHAIN_PRECISION * 2 );
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Fund" );
BOOST_TEST_MESSAGE("Claim fee pool");
con.wallet_api_ptr->claim_asset_fee_pool("BOBCOIN", "1", true);
objs = con.wallet_api_ptr->get_object( object_id_type( bobcoin.dynamic_asset_data_id ) )
.as<vector<asset_dynamic_data_object>>( FC_PACK_MAX_DEPTH );
BOOST_REQUIRE_EQUAL( objs.size(), 1u );
bobcoin_dyn = objs[0];
share_type claimed_pool = bobcoin_dyn.fee_pool;
BOOST_CHECK_EQUAL( claimed_pool.value, old_pool.value + GRAPHENE_BLOCKCHAIN_PRECISION );
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Claim" );
}
{
// Set price feed producer
BOOST_TEST_MESSAGE("Set price feed producer");
asset_bitasset_data_object bob_bitasset = con.wallet_api_ptr->get_bitasset_data( "BOBCOIN" );
BOOST_CHECK_EQUAL( bob_bitasset.feeds.size(), 0u );
auto handle = con.wallet_api_ptr->begin_builder_transaction();
asset_update_feed_producers_operation aufp_op;
aufp_op.issuer = nathan_acct.id;
aufp_op.asset_to_update = bobcoin.id;
aufp_op.new_feed_producers = { nathan_acct.get_id() };
con.wallet_api_ptr->add_operation_to_builder_transaction( handle, aufp_op );
con.wallet_api_ptr->set_fees_on_builder_transaction( handle, "1.3.0" );
con.wallet_api_ptr->sign_builder_transaction( handle, true );
bob_bitasset = con.wallet_api_ptr->get_bitasset_data( "BOBCOIN" );
BOOST_CHECK_EQUAL( bob_bitasset.feeds.size(), 1u );
BOOST_CHECK( bob_bitasset.current_feed.settlement_price.is_null() );
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Update price feed producers" );
}
{
// Publish price feed
BOOST_TEST_MESSAGE("Publish price feed");
price_feed feed;
feed.settlement_price = price( asset(1,bobcoin.get_id()), asset(2) );
feed.core_exchange_rate = price( asset(1,bobcoin.get_id()), asset(1) );
con.wallet_api_ptr->publish_asset_feed( "nathan", "BOBCOIN", feed, true );
asset_bitasset_data_object bob_bitasset = con.wallet_api_ptr->get_bitasset_data( "BOBCOIN" );
BOOST_CHECK( bob_bitasset.current_feed.settlement_price == feed.settlement_price );
BOOST_CHECK(generate_block(app1));
check_nathan_last_history( "Publish price feed" );
}
bool balance_formatter_tested = false;
auto check_bobcoin_balance = [&](string account, int64_t amount) {
auto balances = con.wallet_api_ptr->list_account_balances( account );
size_t count = 0;
for( auto& bal : balances )
{
if( bal.asset_id == bobcoin.id )
{
++count;
BOOST_CHECK_EQUAL( bal.amount.value, amount );
}
}
BOOST_CHECK_EQUAL(count, 1u);
// Testing result formatter
if( !balance_formatter_tested && formatters.find("list_account_balances") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of list_account_balances");
string output = formatters["list_account_balances"](
fc::variant(balances, FC_PACK_MAX_DEPTH ), fc::variants());
BOOST_CHECK( output.find("BOBCOIN") != string::npos );
balance_formatter_tested = true;
}
};
auto check_nathan_bobcoin_balance = [&](int64_t amount) {
check_bobcoin_balance( "nathan", amount );
};
{
// Borrow
BOOST_TEST_MESSAGE("Borrow BOBCOIN");
auto calls = con.wallet_api_ptr->get_call_orders( "BOBCOIN", 10 );
BOOST_CHECK_EQUAL( calls.size(), 0u );
con.wallet_api_ptr->borrow_asset( "nathan", "1", "BOBCOIN", "10", true );
calls = con.wallet_api_ptr->get_call_orders( "BOBCOIN", 10 );
BOOST_REQUIRE_EQUAL( calls.size(), 1u );
BOOST_CHECK_EQUAL( calls.front().debt.value, 10000 );
BOOST_CHECK(generate_block(app1));
check_nathan_bobcoin_balance( 10000 );
check_nathan_last_history( "Adjust debt position" );
}
{
// Settle
BOOST_TEST_MESSAGE("Settle BOBCOIN");
auto settles = con.wallet_api_ptr->get_settle_orders( "BOBCOIN", 10 );
BOOST_CHECK_EQUAL( settles.size(), 0u );
con.wallet_api_ptr->settle_asset( "nathan", "0.2", "BOBCOIN", true );
settles = con.wallet_api_ptr->get_settle_orders( "BOBCOIN", 10 );
BOOST_REQUIRE_EQUAL( settles.size(), 1u );
BOOST_CHECK_EQUAL( settles.front().balance.amount.value, 2000 );
BOOST_CHECK(generate_block(app1));
check_nathan_bobcoin_balance( 8000 );
check_nathan_last_history( "Force-settle" );
}
{
// Transfer
BOOST_TEST_MESSAGE("Transfer some BOBCOIN to init0");
con.wallet_api_ptr->transfer2( "nathan", "init0", "0.5", "BOBCOIN", "" );
con.wallet_api_ptr->transfer( "nathan", "init0", "10000", "1.3.0", "" );
BOOST_CHECK(generate_block(app1));
check_bobcoin_balance( "init0", 5000 );
check_nathan_bobcoin_balance( 3000 );
check_nathan_last_history( "Transfer" );
}
{
// Nathan places an order
BOOST_TEST_MESSAGE("Nathan place an order to buy BOBCOIN");
auto orders = con.wallet_api_ptr->get_limit_orders( "BOBCOIN", "1.3.0", 10 );
BOOST_CHECK_EQUAL( orders.size(), 0u );
con.wallet_api_ptr->sell_asset( "nathan", "100", "1.3.0", "1", "BOBCOIN", 300, false, true );
orders = con.wallet_api_ptr->get_limit_orders( "BOBCOIN", "1.3.0", 10 );
BOOST_REQUIRE_EQUAL( orders.size(), 1u );
BOOST_CHECK_EQUAL( orders.front().for_sale.value, 100 * GRAPHENE_BLOCKCHAIN_PRECISION );
limit_order_id_type nathan_order_id = orders.front().get_id();
BOOST_CHECK(generate_block(app1));
check_nathan_bobcoin_balance( 3000 );
check_nathan_last_history( "Create limit order" );
// init0 place an order to partially fill Nathan's order
BOOST_TEST_MESSAGE("init0 place an order to sell BOBCOIN");
con.wallet_api_ptr->sell_asset( "init0", "0.1", "BOBCOIN", "1", "1.3.0", 200, true, true );
orders = con.wallet_api_ptr->get_limit_orders( "BOBCOIN", "1.3.0", 10 );
BOOST_REQUIRE_EQUAL( orders.size(), 1u );
BOOST_CHECK_EQUAL( orders.front().for_sale.value, 90 * GRAPHENE_BLOCKCHAIN_PRECISION );
BOOST_CHECK(generate_block(app1));
check_bobcoin_balance( "init0", 4000 );
check_nathan_bobcoin_balance( 4000 );
check_nathan_last_history( "as maker" );
// Nathan cancel order
BOOST_TEST_MESSAGE("Nathan cancel order");
con.wallet_api_ptr->cancel_order( nathan_order_id, true );
orders = con.wallet_api_ptr->get_limit_orders( "BOBCOIN", "1.3.0", 10 );
BOOST_CHECK_EQUAL( orders.size(), 0u );
BOOST_CHECK(generate_block(app1));
check_nathan_bobcoin_balance( 4000 );
check_nathan_last_history( "Cancel limit order" );
}
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
///////////////////////
// Start a server and connect using the same calls as the CLI
// Vote for two witnesses, and make sure they both stay there
// after a maintenance block
///////////////////////
BOOST_FIXTURE_TEST_CASE( cli_vote_for_2_witnesses, cli_fixture )
{
try
{
BOOST_TEST_MESSAGE("Cli Vote Test for 2 Witnesses");
INVOKE(create_new_account);
// get the details for init1
witness_object init1_obj = con.wallet_api_ptr->get_witness("init1");
int init1_start_votes = init1_obj.total_votes;
// Vote for a witness
signed_transaction vote_witness1_tx = con.wallet_api_ptr->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 = con.wallet_api_ptr->get_witness("init1");
witness_object init2_obj = con.wallet_api_ptr->get_witness("init2");
int init1_middle_votes = init1_obj.total_votes;
if( !is_hf2262_passed(app1) )
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 = con.wallet_api_ptr->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 = con.wallet_api_ptr->get_witness("init2");
init1_obj = con.wallet_api_ptr->get_witness("init1");
int init2_middle_votes = init2_obj.total_votes;
if( !is_hf2262_passed(app1) )
BOOST_CHECK(init2_middle_votes > init2_start_votes);
int init1_last_votes = init1_obj.total_votes;
if( !is_hf2262_passed(app1) )
BOOST_CHECK(init1_last_votes > init1_start_votes);
{
auto history = con.wallet_api_ptr->get_account_history_by_operations(
"jmjatlanta", {6}, 0, 1); // 6 - account_update_operation
BOOST_REQUIRE_GT( history.details.size(), 0 );
BOOST_CHECK( history.details[0].description.find( "Update Account 'jmjatlanta'" ) != string::npos );
// Testing result formatter
auto formatters = con.wallet_api_ptr->get_result_formatters();
if( formatters.find("get_account_history_by_operations") != formatters.end() )
{
BOOST_TEST_MESSAGE("Testing formatter of get_account_history_by_operations");
string output = formatters["get_account_history_by_operations"](
fc::variant(history, FC_PACK_MAX_DEPTH), fc::variants());
BOOST_CHECK( output.find("Update Account 'jmjatlanta'") != string::npos );
}
}
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_FIXTURE_TEST_CASE( cli_get_signed_transaction_signers, cli_fixture )
{
try
{
INVOKE(upgrade_nathan_account);
// register account and transfer funds
const auto test_bki = con.wallet_api_ptr->suggest_brain_key();
con.wallet_api_ptr->register_account(
"test", test_bki.pub_key, test_bki.pub_key, "nathan", "nathan", 0, true
);
con.wallet_api_ptr->transfer("nathan", "test", "1000", "1.3.0", "", true);
// import key and save wallet
BOOST_CHECK(con.wallet_api_ptr->import_key("test", test_bki.wif_priv_key));
con.wallet_api_ptr->save_wallet_file(con.wallet_filename);
// create transaction and check expected result
auto signed_trx = con.wallet_api_ptr->transfer("test", "nathan", "10", "1.3.0", "", true);
const auto &test_acc = con.wallet_api_ptr->get_account("test");
flat_set<public_key_type> expected_signers = {test_bki.pub_key};
vector<flat_set<account_id_type> > expected_key_refs{{test_acc.get_id(), test_acc.get_id()}};
auto signers = con.wallet_api_ptr->get_transaction_signers(signed_trx);
BOOST_CHECK(signers == expected_signers);
auto key_refs = con.wallet_api_ptr->get_key_references({expected_signers.begin(), expected_signers.end()});
BOOST_CHECK(key_refs == expected_key_refs);
} catch( fc::exception& e ) {
edump((e.to_detail_string()));
throw;
}
}
///////////////////////
// Wallet RPC
// Test adding an unnecessary signature to a transaction
///////////////////////
BOOST_FIXTURE_TEST_CASE(cli_sign_tx_with_unnecessary_signature, cli_fixture) {
try {
auto db = app1->chain_database();
account_object nathan_acct = con.wallet_api_ptr->get_account("nathan");
INVOKE(upgrade_nathan_account);
// Register Bob account
const auto bob_bki = con.wallet_api_ptr->suggest_brain_key();
con.wallet_api_ptr->register_account(
"bob", bob_bki.pub_key, bob_bki.pub_key, "nathan", "nathan", 0, true
);
// Register Charlie account
const graphene::wallet::brain_key_info charlie_bki = con.wallet_api_ptr->suggest_brain_key();
con.wallet_api_ptr->register_account(
"charlie", charlie_bki.pub_key, charlie_bki.pub_key, "nathan", "nathan", 0, true
);
const account_object &charlie_acc = con.wallet_api_ptr->get_account("charlie");
// Import Bob's key
BOOST_CHECK(con.wallet_api_ptr->import_key("bob", bob_bki.wif_priv_key));
// Create transaction with a transfer operation from Nathan to Charlie
transfer_operation top;
top.from = nathan_acct.id;
top.to = charlie_acc.id;
top.amount = asset(5000);
top.fee = db->current_fee_schedule().calculate_fee(top);
signed_transaction test_tx;
test_tx.operations.push_back(top);
// Sign the transaction with the implied nathan's key and the explicitly yet unnecessary Bob's key
auto signed_trx = con.wallet_api_ptr->sign_transaction2(test_tx, {bob_bki.pub_key}, false);