/
authority_tests.cpp
1637 lines (1407 loc) · 60.7 KB
/
authority_tests.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) 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 <boost/test/unit_test.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/chain/exceptions.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/committee_member_object.hpp>
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/hardfork.hpp>
#include <graphene/db/simple_index.hpp>
#include <fc/crypto/digest.hpp>
#include "../common/database_fixture.hpp"
using namespace graphene::chain;
using namespace graphene::chain::test;
BOOST_FIXTURE_TEST_SUITE( authority_tests, database_fixture )
BOOST_AUTO_TEST_CASE( simple_single_signature )
{ try {
try {
fc::ecc::private_key nathan_key = fc::ecc::private_key::generate();
const account_object& nathan = create_account("nathan", nathan_key.get_public_key());
const asset_object& core = asset_id_type()(db);
auto old_balance = fund(nathan);
transfer_operation op;
op.from = nathan.id;
op.to = account_id_type();
op.amount = core.amount(500);
trx.operations.push_back(op);
sign(trx, nathan_key);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(nathan, core), static_cast<int64_t>(old_balance - 500));
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
} FC_LOG_AND_RETHROW() }
BOOST_AUTO_TEST_CASE( any_two_of_three )
{
try {
fc::ecc::private_key nathan_key1 = fc::ecc::private_key::regenerate(fc::digest("key1"));
fc::ecc::private_key nathan_key2 = fc::ecc::private_key::regenerate(fc::digest("key2"));
fc::ecc::private_key nathan_key3 = fc::ecc::private_key::regenerate(fc::digest("key3"));
const account_object& nathan = create_account("nathan", nathan_key1.get_public_key() );
const asset_object& core = asset_id_type()(db);
auto old_balance = fund(nathan);
try {
account_update_operation op;
op.account = nathan.id;
op.active = authority(2, public_key_type(nathan_key1.get_public_key()), 1, public_key_type(nathan_key2.get_public_key()), 1, public_key_type(nathan_key3.get_public_key()), 1);
op.owner = *op.active;
trx.operations.push_back(op);
sign(trx, nathan_key1);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
trx.operations.clear();
trx.signatures.clear();
} FC_CAPTURE_AND_RETHROW ((nathan.active))
transfer_operation op;
op.from = nathan.id;
op.to = account_id_type();
op.amount = core.amount(500);
trx.operations.push_back(op);
sign(trx, nathan_key1);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
sign(trx, nathan_key2);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(nathan, core), static_cast<int64_t>(old_balance - 500));
trx.signatures.clear();
sign(trx, nathan_key2);
sign(trx, nathan_key3);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(nathan, core), static_cast<int64_t>(old_balance - 1000));
trx.signatures.clear();
sign(trx, nathan_key1);
sign(trx, nathan_key3);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(nathan, core), static_cast<int64_t>(old_balance - 1500));
trx.signatures.clear();
//sign(trx, fc::ecc::private_key::generate());
sign(trx,nathan_key3);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
BOOST_CHECK_EQUAL(get_balance(nathan, core), static_cast<int64_t>(old_balance - 1500));
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( recursive_accounts )
{
try {
fc::ecc::private_key parent1_key = fc::ecc::private_key::generate();
fc::ecc::private_key parent2_key = fc::ecc::private_key::generate();
const auto& core = asset_id_type()(db);
BOOST_TEST_MESSAGE( "Creating parent1 and parent2 accounts" );
const account_object& parent1 = create_account("parent1", parent1_key.get_public_key());
const account_object& parent2 = create_account("parent2", parent2_key.get_public_key());
BOOST_TEST_MESSAGE( "Creating child account that requires both parent1 and parent2 to approve" );
{
auto make_child_op = make_account("child");
make_child_op.owner = authority(2, account_id_type(parent1.id), 1, account_id_type(parent2.id), 1);
make_child_op.active = authority(2, account_id_type(parent1.id), 1, account_id_type(parent2.id), 1);
trx.operations.push_back(make_child_op);
PUSH_TX( db, trx, ~0 );
trx.operations.clear();
}
const account_object& child = get_account("child");
auto old_balance = fund(child);
BOOST_TEST_MESSAGE( "Attempting to transfer with no signatures, should fail" );
transfer_operation op;
op.from = child.id;
op.amount = core.amount(500);
trx.operations.push_back(op);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
BOOST_TEST_MESSAGE( "Attempting to transfer with parent1 signature, should fail" );
sign(trx,parent1_key);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
trx.signatures.clear();
BOOST_TEST_MESSAGE( "Attempting to transfer with parent2 signature, should fail" );
sign(trx,parent2_key);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
BOOST_TEST_MESSAGE( "Attempting to transfer with parent1 and parent2 signature, should succeed" );
sign(trx,parent1_key);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(child, core), static_cast<int64_t>(old_balance - 500));
trx.operations.clear();
trx.signatures.clear();
BOOST_TEST_MESSAGE( "Adding a key for the child that can override parents" );
fc::ecc::private_key child_key = fc::ecc::private_key::generate();
{
account_update_operation op;
op.account = child.id;
op.active = authority(2, account_id_type(parent1.id), 1, account_id_type(parent2.id), 1,
public_key_type(child_key.get_public_key()), 2);
trx.operations.push_back(op);
sign(trx,parent1_key);
sign(trx,parent2_key);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_REQUIRE_EQUAL(child.active.num_auths(), 3u);
trx.operations.clear();
trx.signatures.clear();
}
op.from = child.id;
op.to = account_id_type();
op.amount = core.amount(500);
trx.operations.push_back(op);
BOOST_TEST_MESSAGE( "Attempting transfer with no signatures, should fail" );
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
BOOST_TEST_MESSAGE( "Attempting transfer just parent1, should fail" );
sign(trx, parent1_key);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
trx.signatures.clear();
BOOST_TEST_MESSAGE( "Attempting transfer just parent2, should fail" );
sign(trx, parent2_key);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
BOOST_TEST_MESSAGE( "Attempting transfer both parents, should succeed" );
sign(trx, parent1_key);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(child, core), static_cast<int64_t>(old_balance - 1000));
trx.signatures.clear();
BOOST_TEST_MESSAGE( "Attempting transfer with just child key, should succeed" );
sign(trx, child_key);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(child, core), static_cast<int64_t>(old_balance - 1500));
trx.operations.clear();
trx.signatures.clear();
BOOST_TEST_MESSAGE( "Creating grandparent account, parent1 now requires authority of grandparent" );
auto grandparent = create_account("grandparent");
fc::ecc::private_key grandparent_key = fc::ecc::private_key::generate();
{
account_update_operation op;
op.account = parent1.id;
op.active = authority(1, account_id_type(grandparent.id), 1);
op.owner = *op.active;
trx.operations.push_back(op);
op.account = grandparent.id;
op.active = authority(1, public_key_type(grandparent_key.get_public_key()), 1);
op.owner = *op.active;
trx.operations.push_back(op);
PUSH_TX( db, trx, ~0 );
trx.operations.clear();
trx.signatures.clear();
}
BOOST_TEST_MESSAGE( "Attempt to transfer using old parent keys, should fail" );
trx.operations.push_back(op);
sign(trx, parent1_key);
sign(trx, parent2_key);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
trx.signatures.clear();
sign( trx, parent2_key );
sign( trx, grandparent_key );
BOOST_TEST_MESSAGE( "Attempt to transfer using parent2_key and grandparent_key" );
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(child, core), static_cast<int64_t>(old_balance - 2000));
trx.clear();
BOOST_TEST_MESSAGE( "Update grandparent account authority to be committee account" );
{
account_update_operation op;
op.account = grandparent.id;
op.active = authority(1, account_id_type(), 1);
op.owner = *op.active;
trx.operations.push_back(op);
PUSH_TX( db, trx, ~0 );
trx.operations.clear();
trx.signatures.clear();
}
BOOST_TEST_MESSAGE( "Create recursion depth failure" );
trx.operations.push_back(op);
sign(trx, parent2_key);
sign(trx, grandparent_key);
sign(trx, init_account_priv_key);
//Fails due to recursion depth.
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
BOOST_TEST_MESSAGE( "verify child key can override recursion checks" );
trx.signatures.clear();
sign(trx, child_key);
PUSH_TX( db, trx, database::skip_transaction_dupe_check );
BOOST_CHECK_EQUAL(get_balance(child, core), static_cast<int64_t>(old_balance - 2500));
trx.operations.clear();
trx.signatures.clear();
BOOST_TEST_MESSAGE( "Verify a cycle fails" );
{
account_update_operation op;
op.account = parent1.id;
op.active = authority(1, account_id_type(child.id), 1);
op.owner = *op.active;
trx.operations.push_back(op);
PUSH_TX( db, trx, ~0 );
trx.operations.clear();
trx.signatures.clear();
}
trx.operations.push_back(op);
sign(trx, parent2_key);
//Fails due to recursion depth.
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx, database::skip_transaction_dupe_check ), fc::exception);
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( proposed_single_account )
{
using namespace graphene::chain;
try {
INVOKE(any_two_of_three);
fc::ecc::private_key committee_key = init_account_priv_key;
fc::ecc::private_key nathan_key1 = fc::ecc::private_key::regenerate(fc::digest("key1"));
fc::ecc::private_key nathan_key2 = fc::ecc::private_key::regenerate(fc::digest("key2"));
fc::ecc::private_key nathan_key3 = fc::ecc::private_key::regenerate(fc::digest("key3"));
const account_object& moneyman = create_account("moneyman", init_account_pub_key);
const account_object& nathan = get_account("nathan");
const asset_object& core = asset_id_type()(db);
transfer(account_id_type()(db), moneyman, core.amount(1000000));
//Following any_two_of_three, nathan's active authority is satisfied by any two of {key1,key2,key3}
BOOST_TEST_MESSAGE( "moneyman is creating proposal for nathan to transfer 100 CORE to moneyman" );
transfer_operation transfer_op;
transfer_op.from = nathan.id;
transfer_op.to = moneyman.get_id();
transfer_op.amount = core.amount(100);
proposal_create_operation op;
op.fee_paying_account = moneyman.id;
op.proposed_ops.emplace_back( transfer_op );
op.expiration_time = db.head_block_time() + fc::days(1);
asset nathan_start_balance = db.get_balance(nathan.get_id(), core.get_id());
{
vector<authority> other;
flat_set<account_id_type> active_set, owner_set;
operation_get_required_authorities(op,active_set,owner_set,other);
BOOST_CHECK_EQUAL(active_set.size(), 1lu);
BOOST_CHECK_EQUAL(owner_set.size(), 0lu);
BOOST_CHECK_EQUAL(other.size(), 0lu);
BOOST_CHECK(*active_set.begin() == moneyman.get_id());
active_set.clear();
other.clear();
operation_get_required_authorities(op.proposed_ops.front().op,active_set,owner_set,other);
BOOST_CHECK_EQUAL(active_set.size(), 1lu);
BOOST_CHECK_EQUAL(owner_set.size(), 0lu);
BOOST_CHECK_EQUAL(other.size(), 0lu);
BOOST_CHECK(*active_set.begin() == nathan.id);
}
trx.operations.push_back(op);
set_expiration( db, trx );
sign( trx, init_account_priv_key );
const proposal_object& proposal = db.get<proposal_object>(PUSH_TX( db, trx ).operation_results.front().get<object_id_type>());
BOOST_CHECK_EQUAL(proposal.required_active_approvals.size(), 1lu);
BOOST_CHECK_EQUAL(proposal.available_active_approvals.size(), 0lu);
BOOST_CHECK_EQUAL(proposal.required_owner_approvals.size(), 0lu);
BOOST_CHECK_EQUAL(proposal.available_owner_approvals.size(), 0lu);
BOOST_CHECK(*proposal.required_active_approvals.begin() == nathan.id);
proposal_update_operation pup;
pup.proposal = proposal.id;
pup.fee_paying_account = nathan.id;
BOOST_TEST_MESSAGE( "Updating the proposal to have nathan's authority" );
pup.active_approvals_to_add.insert(nathan.id);
trx.operations = {pup};
sign( trx, committee_key );
//committee may not add nathan's approval.
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx ), fc::exception);
pup.active_approvals_to_add.clear();
pup.active_approvals_to_add.insert(account_id_type());
trx.operations = {pup};
sign( trx, committee_key );
//committee has no stake in the transaction.
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx ), fc::exception);
trx.signatures.clear();
pup.active_approvals_to_add.clear();
pup.active_approvals_to_add.insert(nathan.id);
trx.operations = {pup};
sign( trx, nathan_key3 );
sign( trx, nathan_key2 );
BOOST_CHECK_EQUAL(get_balance(nathan, core), nathan_start_balance.amount.value);
PUSH_TX( db, trx );
BOOST_CHECK_EQUAL(get_balance(nathan, core), nathan_start_balance.amount.value - 100);
} catch (fc::exception& e) {
edump((e.to_detail_string()));
throw;
}
}
BOOST_AUTO_TEST_CASE( proposal_failure )
{
try
{
ACTORS( (bob) (alice) );
fund( bob, asset(1000000) );
fund( alice, asset(1000000) );
// create proposal that will eventually fail due to lack of funds
transfer_operation top;
top.to = alice_id;
top.from = bob_id;
top.amount = asset(2000000);
proposal_create_operation pop;
pop.proposed_ops.push_back( { top } );
pop.expiration_time = db.head_block_time() + fc::days(1);
pop.fee_paying_account = bob_id;
trx.operations.push_back( pop );
trx.signatures.clear();
sign( trx, bob_private_key );
processed_transaction processed = PUSH_TX( db, trx );
proposal_object prop = db.get<proposal_object>(processed.operation_results.front().get<object_id_type>());
trx.clear();
generate_block();
// add signature
proposal_update_operation up_op;
up_op.proposal = prop.id;
up_op.fee_paying_account = bob_id;
up_op.active_approvals_to_add.emplace( bob_id );
trx.operations.push_back( up_op );
sign( trx, bob_private_key );
PUSH_TX( db, trx );
trx.clear();
// check fail reason
const proposal_object& result = db.get<proposal_object>(prop.id);
BOOST_CHECK(!result.fail_reason.empty());
BOOST_CHECK_EQUAL( result.fail_reason.substr(0, 16), "Assert Exception");
}
FC_LOG_AND_RETHROW()
}
/// Verify that committee authority cannot be invoked in a normal transaction
BOOST_AUTO_TEST_CASE( committee_authority )
{ try {
fc::ecc::private_key nathan_key = fc::ecc::private_key::generate();
fc::ecc::private_key committee_key = init_account_priv_key;
const account_object nathan = create_account("nathan", nathan_key.get_public_key());
const auto& global_params = db.get_global_properties().parameters;
generate_block();
// Signatures are for suckers.
db.modify(db.get_global_properties(), [](global_property_object& p) {
// Turn the review period WAY down, so it doesn't take long to produce blocks to that point in simulated time.
p.parameters.committee_proposal_review_period = fc::days(1).to_seconds();
});
BOOST_TEST_MESSAGE( "transfering 100000 CORE to nathan, signing with committee key should fail because this requires it to be part of a proposal" );
transfer_operation top;
top.to = nathan.id;
top.amount = asset(100000);
trx.operations.push_back(top);
sign(trx, committee_key);
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx ), graphene::chain::invalid_committee_approval );
auto _sign = [&] { trx.signatures.clear(); sign( trx, nathan_key ); };
proposal_create_operation pop;
pop.proposed_ops.push_back({trx.operations.front()});
pop.expiration_time = db.head_block_time() + global_params.committee_proposal_review_period*2;
pop.fee_paying_account = nathan.id;
trx.operations = {pop};
_sign();
// The review period isn't set yet. Make sure it throws.
GRAPHENE_REQUIRE_THROW( PUSH_TX( db, trx ), proposal_create_review_period_required );
pop.review_period_seconds = global_params.committee_proposal_review_period / 2;
trx.operations.back() = pop;
_sign();
// The review period is too short. Make sure it throws.
GRAPHENE_REQUIRE_THROW( PUSH_TX( db, trx ), proposal_create_review_period_insufficient );
pop.review_period_seconds = global_params.committee_proposal_review_period;
trx.operations.back() = pop;
_sign();
proposal_object prop = db.get<proposal_object>(PUSH_TX( db, trx ).operation_results.front().get<object_id_type>());
BOOST_REQUIRE(db.find_object(prop.id));
BOOST_CHECK(prop.expiration_time == pop.expiration_time);
BOOST_CHECK(prop.review_period_time && *prop.review_period_time == pop.expiration_time - *pop.review_period_seconds);
BOOST_CHECK(prop.proposed_transaction.operations.size() == 1);
BOOST_CHECK_EQUAL(get_balance(nathan, asset_id_type()(db)), 0);
BOOST_CHECK(!db.get<proposal_object>(prop.id).is_authorized_to_execute(db));
generate_block();
BOOST_REQUIRE(db.find_object(prop.id));
BOOST_CHECK_EQUAL(get_balance(nathan, asset_id_type()(db)), 0);
BOOST_TEST_MESSAGE( "Checking that the proposal is not authorized to execute" );
BOOST_REQUIRE(!db.get<proposal_object>(prop.id).is_authorized_to_execute(db));
trx.operations.clear();
trx.signatures.clear();
proposal_update_operation uop;
uop.fee_paying_account = GRAPHENE_TEMP_ACCOUNT;
uop.proposal = prop.id;
uop.key_approvals_to_add.emplace(committee_key.get_public_key());
/*
uop.key_approvals_to_add.emplace(1);
uop.key_approvals_to_add.emplace(2);
uop.key_approvals_to_add.emplace(3);
uop.key_approvals_to_add.emplace(4);
uop.key_approvals_to_add.emplace(5);
uop.key_approvals_to_add.emplace(6);
*/
trx.operations.push_back(uop);
sign( trx, committee_key );
db.push_transaction(trx);
BOOST_CHECK_EQUAL(get_balance(nathan, asset_id_type()(db)), 0);
BOOST_CHECK(db.get<proposal_object>(prop.id).is_authorized_to_execute(db));
trx.signatures.clear();
generate_blocks(*prop.review_period_time);
uop.key_approvals_to_add.clear();
uop.key_approvals_to_add.insert(committee_key.get_public_key()); // was 7
trx.operations.back() = uop;
sign( trx, committee_key );
// Should throw because the transaction is now in review.
GRAPHENE_CHECK_THROW(PUSH_TX( db, trx ), fc::exception);
generate_blocks(prop.expiration_time);
BOOST_CHECK_EQUAL(get_balance(nathan, asset_id_type()(db)), 100000);
// proposal deleted
BOOST_CHECK_THROW( db.get<proposal_object>(prop.id), fc::exception );
} FC_LOG_AND_RETHROW() }
BOOST_FIXTURE_TEST_CASE( fired_committee_members, database_fixture )
{ try {
generate_block();
fc::ecc::private_key committee_key = init_account_priv_key;
fc::ecc::private_key committee_member_key = fc::ecc::private_key::generate();
//Meet nathan. He has a little money.
const account_object* nathan = &create_account("nathan");
transfer(account_id_type()(db), *nathan, asset(5000));
generate_block();
nathan = &get_account("nathan");
flat_set<vote_id_type> committee_members;
/*
db.modify(db.get_global_properties(), [](global_property_object& p) {
// Turn the review period WAY down, so it doesn't take long to produce blocks to that point in simulated time.
p.parameters.committee_proposal_review_period = fc::days(1).to_seconds();
});
*/
for( int i = 0; i < 15; ++i )
{
const auto& account = create_account("committee-member" + fc::to_string(i+1), committee_member_key.get_public_key());
upgrade_to_lifetime_member(account);
committee_members.insert(create_committee_member(account).vote_id);
}
BOOST_REQUIRE_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
//A proposal is created to give nathan lots more money.
proposal_create_operation pop = proposal_create_operation::committee_proposal(db.get_global_properties().parameters, db.head_block_time());
pop.fee_paying_account = GRAPHENE_TEMP_ACCOUNT;
pop.expiration_time = db.head_block_time() + *pop.review_period_seconds + fc::days(1).to_seconds();
ilog( "Creating proposal to give nathan money that expires: ${e}", ("e", pop.expiration_time ) );
ilog( "The proposal has a review period of: ${r} sec", ("r",*pop.review_period_seconds) );
transfer_operation top;
top.to = nathan->id;
top.amount = asset(100000);
pop.proposed_ops.emplace_back(top);
trx.operations.push_back(pop);
const proposal_object& prop = db.get<proposal_object>(PUSH_TX( db, trx ).operation_results.front().get<object_id_type>());
proposal_id_type pid = prop.id;
BOOST_CHECK(!pid(db).is_authorized_to_execute(db));
ilog( "commitee member approves proposal" );
//committee key approves of the proposal.
proposal_update_operation uop;
uop.fee_paying_account = GRAPHENE_TEMP_ACCOUNT;
uop.proposal = pid;
uop.key_approvals_to_add.emplace(init_account_pub_key);
trx.operations.back() = uop;
sign( trx, committee_key );
PUSH_TX( db, trx );
BOOST_CHECK(pid(db).is_authorized_to_execute(db));
ilog( "Generating blocks for 2 days" );
generate_block();
BOOST_REQUIRE_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
generate_block();
BOOST_REQUIRE_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
//Time passes... the proposal is now in its review period.
//generate_blocks(*pid(db).review_period_time);
generate_blocks(db.head_block_time() + fc::days(2) );
ilog( "head block time: ${t}", ("t",db.head_block_time()));
fc::time_point_sec maintenance_time = db.get_dynamic_global_properties().next_maintenance_time;
BOOST_CHECK_LT(maintenance_time.sec_since_epoch(), pid(db).expiration_time.sec_since_epoch());
//Yay! The proposal to give nathan more money is authorized.
BOOST_REQUIRE(pid(db).is_authorized_to_execute(db));
nathan = &get_account("nathan");
// no money yet
BOOST_REQUIRE_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
{
//Oh noes! Nathan votes for a whole new slate of committee_members!
account_update_operation op;
op.account = nathan->id;
op.new_options = nathan->options;
op.new_options->votes = committee_members;
trx.operations.push_back(op);
set_expiration( db, trx );
PUSH_TX( db, trx, ~0 );
trx.operations.clear();
}
idump((get_balance(*nathan, asset_id_type()(db))));
// still no money
BOOST_CHECK_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
//Time passes... the set of active committee_members gets updated.
generate_blocks(maintenance_time);
//The proposal is no longer authorized, because the active committee_members got changed.
BOOST_CHECK(!pid(db).is_authorized_to_execute(db));
// still no money
BOOST_CHECK_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
//Time passes... the proposal has now expired.
generate_blocks(pid(db).expiration_time);
BOOST_CHECK(db.find(pid) == nullptr);
//Nathan never got any more money because the proposal was rejected.
BOOST_CHECK_EQUAL(get_balance(*nathan, asset_id_type()(db)), 5000);
} FC_LOG_AND_RETHROW() }
BOOST_FIXTURE_TEST_CASE( proposal_two_accounts, database_fixture )
{ try {
generate_block();
auto nathan_key = generate_private_key("nathan");
auto dan_key = generate_private_key("dan");
const account_object& nathan = create_account("nathan", nathan_key.get_public_key() );
const account_object& dan = create_account("dan", dan_key.get_public_key() );
transfer(account_id_type()(db), nathan, asset(100000));
transfer(account_id_type()(db), dan, asset(100000));
{
transfer_operation top;
top.from = dan.get_id();
top.to = nathan.get_id();
top.amount = asset(500);
proposal_create_operation pop;
pop.proposed_ops.emplace_back(top);
std::swap(top.from, top.to);
pop.proposed_ops.emplace_back(top);
pop.fee_paying_account = nathan.get_id();
pop.expiration_time = db.head_block_time() + fc::days(1);
trx.operations.push_back(pop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
}
const proposal_object& prop = *db.get_index_type<proposal_index>().indices().begin();
BOOST_CHECK(prop.required_active_approvals.size() == 2);
BOOST_CHECK(prop.required_owner_approvals.size() == 0);
BOOST_CHECK(!prop.is_authorized_to_execute(db));
{
proposal_id_type pid = prop.id;
proposal_update_operation uop;
uop.proposal = prop.id;
uop.active_approvals_to_add.insert(nathan.get_id());
uop.fee_paying_account = nathan.get_id();
trx.operations.push_back(uop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(db.find_object(pid) != nullptr);
BOOST_CHECK(!prop.is_authorized_to_execute(db));
uop.active_approvals_to_add = {dan.get_id()};
trx.operations.push_back(uop);
sign( trx, nathan_key );
GRAPHENE_REQUIRE_THROW(PUSH_TX( db, trx ), fc::exception);
sign( trx, dan_key );
PUSH_TX( db, trx );
BOOST_CHECK(db.find_object(pid) == nullptr);
}
} FC_LOG_AND_RETHROW() }
BOOST_FIXTURE_TEST_CASE( proposal_delete, database_fixture )
{ try {
generate_block();
auto nathan_key = generate_private_key("nathan");
auto dan_key = generate_private_key("dan");
const account_object& nathan = create_account("nathan", nathan_key.get_public_key() );
const account_object& dan = create_account("dan", dan_key.get_public_key() );
transfer(account_id_type()(db), nathan, asset(100000));
transfer(account_id_type()(db), dan, asset(100000));
{
transfer_operation top;
top.from = dan.get_id();
top.to = nathan.get_id();
top.amount = asset(500);
proposal_create_operation pop;
pop.proposed_ops.emplace_back(top);
std::swap(top.from, top.to);
top.amount = asset(6000);
pop.proposed_ops.emplace_back(top);
pop.fee_paying_account = nathan.get_id();
pop.expiration_time = db.head_block_time() + fc::days(1);
trx.operations.push_back(pop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
}
const proposal_object& prop = *db.get_index_type<proposal_index>().indices().begin();
BOOST_CHECK(prop.required_active_approvals.size() == 2);
BOOST_CHECK(prop.required_owner_approvals.size() == 0);
BOOST_CHECK(!prop.is_authorized_to_execute(db));
{
proposal_update_operation uop;
uop.fee_paying_account = nathan.get_id();
uop.proposal = prop.id;
uop.active_approvals_to_add.insert(nathan.get_id());
trx.operations.push_back(uop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_active_approvals.size(), 1lu);
std::swap(uop.active_approvals_to_add, uop.active_approvals_to_remove);
trx.operations.push_back(uop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_active_approvals.size(), 0lu);
}
{
proposal_id_type pid = prop.id;
proposal_delete_operation dop;
dop.fee_paying_account = nathan.get_id();
dop.proposal = pid;
trx.operations.push_back(dop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
BOOST_CHECK(db.find_object(pid) == nullptr);
BOOST_CHECK_EQUAL(get_balance(nathan, asset_id_type()(db)), 100000);
}
} FC_LOG_AND_RETHROW() }
BOOST_FIXTURE_TEST_CASE( proposal_owner_authority_delete, database_fixture )
{ try {
generate_block();
auto nathan_key = generate_private_key("nathan");
auto dan_key = generate_private_key("dan");
const account_object& nathan = create_account("nathan", nathan_key.get_public_key() );
const account_object& dan = create_account("dan", dan_key.get_public_key() );
transfer(account_id_type()(db), nathan, asset(100000));
transfer(account_id_type()(db), dan, asset(100000));
{
transfer_operation top;
top.from = dan.get_id();
top.to = nathan.get_id();
top.amount = asset(500);
account_update_operation uop;
uop.account = nathan.get_id();
uop.owner = authority(1, public_key_type(generate_private_key("nathan2").get_public_key()), 1);
proposal_create_operation pop;
pop.proposed_ops.emplace_back(top);
pop.proposed_ops.emplace_back(uop);
std::swap(top.from, top.to);
top.amount = asset(6000);
pop.proposed_ops.emplace_back(top);
pop.fee_paying_account = nathan.get_id();
pop.expiration_time = db.head_block_time() + fc::days(1);
trx.operations.push_back(pop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
}
const proposal_object& prop = *db.get_index_type<proposal_index>().indices().begin();
BOOST_CHECK_EQUAL(prop.required_active_approvals.size(), 1lu);
BOOST_CHECK_EQUAL(prop.required_owner_approvals.size(), 1lu);
BOOST_CHECK(!prop.is_authorized_to_execute(db));
{
proposal_update_operation uop;
uop.fee_paying_account = nathan.get_id();
uop.proposal = prop.id;
uop.owner_approvals_to_add.insert(nathan.get_id());
trx.operations.push_back(uop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_owner_approvals.size(), 1lu);
std::swap(uop.owner_approvals_to_add, uop.owner_approvals_to_remove);
trx.operations.push_back(uop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_owner_approvals.size(), 0lu);
}
{
proposal_id_type pid = prop.id;
proposal_delete_operation dop;
dop.fee_paying_account = nathan.get_id();
dop.proposal = pid;
dop.using_owner_authority = true;
trx.operations.push_back(dop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
BOOST_CHECK(db.find_object(pid) == nullptr);
BOOST_CHECK_EQUAL(get_balance(nathan, asset_id_type()(db)), 100000);
}
} FC_LOG_AND_RETHROW() }
BOOST_FIXTURE_TEST_CASE( proposal_owner_authority_complete, database_fixture )
{ try {
generate_block();
auto nathan_key = generate_private_key("nathan");
auto dan_key = generate_private_key("dan");
const account_object& nathan = create_account("nathan", nathan_key.get_public_key() );
const account_object& dan = create_account("dan", dan_key.get_public_key() );
transfer(account_id_type()(db), nathan, asset(100000));
transfer(account_id_type()(db), dan, asset(100000));
{
transfer_operation top;
top.from = dan.get_id();
top.to = nathan.get_id();
top.amount = asset(500);
account_update_operation uop;
uop.account = nathan.get_id();
uop.owner = authority(1, public_key_type(generate_private_key("nathan2").get_public_key()), 1);
proposal_create_operation pop;
pop.proposed_ops.emplace_back(top);
pop.proposed_ops.emplace_back(uop);
std::swap(top.from, top.to);
top.amount = asset(6000);
pop.proposed_ops.emplace_back(top);
pop.fee_paying_account = nathan.get_id();
pop.expiration_time = db.head_block_time() + fc::days(1);
trx.operations.push_back(pop);
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
}
const proposal_object& prop = *db.get_index_type<proposal_index>().indices().begin();
BOOST_CHECK_EQUAL(prop.required_active_approvals.size(), 1lu);
BOOST_CHECK_EQUAL(prop.required_owner_approvals.size(), 1lu);
BOOST_CHECK(!prop.is_authorized_to_execute(db));
{
proposal_id_type pid = prop.id;
proposal_update_operation uop;
uop.fee_paying_account = nathan.get_id();
uop.proposal = prop.id;
uop.key_approvals_to_add.insert(dan.active.key_auths.begin()->first);
trx.operations.push_back(uop);
set_expiration( db, trx );
sign( trx, nathan_key );
sign( trx, dan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_key_approvals.size(), 1lu);
std::swap(uop.key_approvals_to_add, uop.key_approvals_to_remove);
trx.operations.push_back(uop);
trx.expiration += fc::seconds(1); // Survive trx dupe check
sign( trx, nathan_key );
sign( trx, dan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_key_approvals.size(), 0lu);
std::swap(uop.key_approvals_to_add, uop.key_approvals_to_remove);
trx.operations.push_back(uop);
trx.expiration += fc::seconds(1); // Survive trx dupe check
sign( trx, nathan_key );
sign( trx, dan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(!prop.is_authorized_to_execute(db));
BOOST_CHECK_EQUAL(prop.available_key_approvals.size(), 1lu);
uop.key_approvals_to_add.clear();
uop.owner_approvals_to_add.insert(nathan.get_id());
trx.operations.push_back(uop);
trx.expiration += fc::seconds(1); // Survive trx dupe check
sign( trx, nathan_key );
PUSH_TX( db, trx );
trx.clear();
BOOST_CHECK(db.find_object(pid) == nullptr);
}
} FC_LOG_AND_RETHROW() }
BOOST_FIXTURE_TEST_CASE( max_authority_membership, database_fixture )
{
try
{
//Get a sane head block time
generate_block();
db.modify(db.get_global_properties(), [](global_property_object& p) {
p.parameters.committee_proposal_review_period = fc::hours(1).to_seconds();
});
transaction tx;
processed_transaction ptx;
private_key_type committee_key = init_account_priv_key;
// Sam is the creator of accounts
private_key_type sam_key = generate_private_key("sam");
account_object sam_account_object = create_account( "sam", sam_key );
upgrade_to_lifetime_member(sam_account_object);
account_object committee_account_object = committee_account(db);
const asset_object& core = asset_id_type()(db);
transfer(committee_account_object, sam_account_object, core.amount(100000));
// have Sam create some keys
int keys_to_create = 2*GRAPHENE_DEFAULT_MAX_AUTHORITY_MEMBERSHIP;
vector<private_key_type> private_keys;
private_keys.reserve( keys_to_create );
for( int i=0; i<keys_to_create; i++ )
{
string seed = "this_is_a_key_" + std::to_string(i);
private_key_type privkey = generate_private_key( seed );
private_keys.push_back( privkey );
}
set_expiration( db, tx );
vector<public_key_type> key_ids;
key_ids.reserve( keys_to_create );
for( int i=0; i<keys_to_create; i++ )
key_ids.push_back( private_keys[i].get_public_key() );
// now try registering accounts with n keys, 0 < n < 20
// TODO: Make sure it throws / accepts properly when
// max_account_authority is changed in global parameteres
for( int num_keys=1; num_keys<=keys_to_create; num_keys++ )
{
// try registering account with n keys
authority test_authority;
test_authority.weight_threshold = num_keys;
for( int i=0; i<num_keys; i++ )
test_authority.key_auths[ key_ids[i] ] = 1;
auto check_tx = [&]( const authority& owner_auth,
const authority& active_auth )
{
const uint16_t max_authority_membership = GRAPHENE_DEFAULT_MAX_AUTHORITY_MEMBERSHIP;
account_create_operation anon_create_op;
transaction tx;
anon_create_op.owner = owner_auth;
anon_create_op.active = active_auth;
anon_create_op.registrar = sam_account_object.id;