-
Notifications
You must be signed in to change notification settings - Fork 795
/
steem_evaluator.cpp
1563 lines (1282 loc) · 58.9 KB
/
steem_evaluator.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
#include <steemit/chain/database.hpp>
#include <steemit/chain/generic_json_evaluator_registry.hpp>
#include <steemit/chain/steem_evaluator.hpp>
#include <steemit/chain/steem_objects.hpp>
#ifndef IS_LOW_MEM
#include <diff_match_patch.h>
#include <boost/locale/encoding_utf.hpp>
using boost::locale::conv::utf_to_utf;
std::wstring utf8_to_wstring(const std::string& str)
{
return utf_to_utf<wchar_t>(str.c_str(), str.c_str() + str.size());
}
std::string wstring_to_utf8(const std::wstring& str)
{
return utf_to_utf<char>(str.c_str(), str.c_str() + str.size());
}
#endif
#include <fc/uint128.hpp>
#include <fc/utf8.hpp>
#include <limits>
namespace steemit { namespace chain {
using fc::uint128_t;
inline void validate_permlink_0_1( const string& permlink )
{
FC_ASSERT( permlink.size() > STEEMIT_MIN_PERMLINK_LENGTH && permlink.size() < STEEMIT_MAX_PERMLINK_LENGTH );
for( auto c : permlink )
{
switch( c )
{
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i':
case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case '0':
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
case '-':
break;
default:
FC_ASSERT( !"Invalid permlink character:", "${s}", ("s", std::string() + c ) );
}
}
}
void witness_update_evaluator::do_apply( const witness_update_operation& o )
{
db().get_account( o.owner ); // verify owner exists
if ( db().has_hardfork( STEEMIT_HARDFORK_0_1 ) ) FC_ASSERT( o.url.size() <= STEEMIT_MAX_WITNESS_URL_LENGTH );
if( !db().has_hardfork( STEEMIT_HARDFORK_0_12__179) )
FC_ASSERT( o.props.maximum_block_size >= STEEMIT_MIN_BLOCK_SIZE_LIMIT * 2 );
const auto& by_witness_name_idx = db().get_index_type< witness_index >().indices().get< by_name >();
auto wit_itr = by_witness_name_idx.find( o.owner );
if( wit_itr != by_witness_name_idx.end() )
{
db().modify( *wit_itr, [&]( witness_object& w ) {
w.url = o.url;
w.signing_key = o.block_signing_key;
w.props = o.props;
});
}
else
{
db().create< witness_object >( [&]( witness_object& w ) {
w.owner = o.owner;
w.url = o.url;
w.signing_key = o.block_signing_key;
w.created = db().head_block_time();
w.props = o.props;
});
}
}
void account_create_evaluator::do_apply( const account_create_operation& o )
{
const auto& creator = db().get_account( o.creator );
const auto& props = db().get_dynamic_global_properties();
FC_ASSERT( creator.balance >= o.fee, "Isufficient balance to create account", ( "creator.balance", creator.balance )( "required", o.fee ) );
if( db().has_hardfork( STEEMIT_HARDFORK_0_1 ) ) {
const witness_schedule_object& wso = db().get_witness_schedule_object();
FC_ASSERT( o.fee >= wso.median_props.account_creation_fee, "Insufficient Fee: ${f} required, ${p} provided",
("f", wso.median_props.account_creation_fee)
("p", o.fee) );
}
db().modify( creator, [&]( account_object& c ){
c.balance -= o.fee;
});
const auto& new_account = db().create< account_object >( [&]( account_object& acc )
{
acc.name = o.new_account_name;
acc.owner = o.owner;
acc.active = o.active;
acc.posting = o.posting;
acc.memo_key = o.memo_key;
acc.last_owner_update = fc::time_point_sec::min();
acc.created = props.time;
acc.last_vote_time = props.time;
acc.mined = false;
if( !db().has_hardfork( STEEMIT_HARDFORK_0_11__169 ) )
acc.recovery_account = "steem";
else
acc.recovery_account = o.creator;
#ifndef IS_LOW_MEM
acc.json_metadata = o.json_metadata;
#endif
});
if( o.fee.amount > 0 )
db().create_vesting( new_account, o.fee );
}
void account_update_evaluator::do_apply( const account_update_operation& o )
{
if( db().has_hardfork( STEEMIT_HARDFORK_0_1 ) ) FC_ASSERT( o.account != STEEMIT_TEMP_ACCOUNT );
const auto& account = db().get_account( o.account );
if( o.owner )
{
#ifndef IS_TESTNET
if( db().has_hardfork( STEEMIT_HARDFORK_0_11 ) )
FC_ASSERT( db().head_block_time() - account.last_owner_update > STEEMIT_OWNER_UPDATE_LIMIT );
#endif
db().update_owner_authority( account, *o.owner );
}
db().modify( account, [&]( account_object& acc )
{
if( o.active ) acc.active = *o.active;
if( o.posting ) acc.posting = *o.posting;
if( o.memo_key != public_key_type() )
acc.memo_key = o.memo_key;
if( ( o.active || o.owner ) && acc.active_challenged )
{
acc.active_challenged = false;
acc.last_active_proved = db().head_block_time();
}
#ifndef IS_LOW_MEM
if ( o.json_metadata.size() > 0 )
acc.json_metadata = o.json_metadata;
#endif
});
}
/**
* Because net_rshares is 0 there is no need to update any pending payout calculations or parent posts.
*/
void delete_comment_evaluator::do_apply( const delete_comment_operation& o ) {
if( db().has_hardfork( STEEMIT_HARDFORK_0_10 ) )
{
const auto& auth = db().get_account( o.author );
FC_ASSERT( !(auth.owner_challenged || auth.active_challenged ) );
}
const auto& comment = db().get_comment( o.author, o.permlink );
FC_ASSERT( comment.children == 0, "comment cannot have any replies" );
if( db().is_producing() ) {
FC_ASSERT( comment.net_rshares <= 0, "comment cannot have any net positive votes" );
}
if( comment.net_rshares > 0 ) return;
const auto& vote_idx = db().get_index_type<comment_vote_index>().indices().get<by_comment_voter>();
auto vote_itr = vote_idx.lower_bound( comment_id_type(comment.id) );
while( vote_itr != vote_idx.end() && vote_itr->comment == comment.id ) {
const auto& cur_vote = *vote_itr;
++vote_itr;
db().remove(cur_vote);
}
/// this loop can be skiped for validate-only nodes as it is merely gathering stats for indicies
if( db().has_hardfork( STEEMIT_HARDFORK_0_6__80 ) && comment.parent_author.size() != 0 )
{
auto parent = &db().get_comment( comment.parent_author, comment.parent_permlink );
auto now = db().head_block_time();
while( parent )
{
db().modify( *parent, [&]( comment_object& p ){
p.children--;
p.active = now;
});
#ifndef IS_LOW_MEM
if( parent->parent_author.size() )
parent = &db().get_comment( parent->parent_author, parent->parent_permlink );
else
#endif
parent = nullptr;
}
}
/** TODO move category behavior to a plugin, this is not part of consensus */
const category_object* cat = db().find_category( comment.category );
db().modify( *cat, [&]( category_object& c )
{
c.discussions--;
c.last_update = db().head_block_time();
});
db().remove( comment );
}
void comment_options_evaluator::do_apply( const comment_options_operation& o )
{
if( db().has_hardfork( STEEMIT_HARDFORK_0_10 ) )
{
const auto& auth = db().get_account( o.author );
FC_ASSERT( !(auth.owner_challenged || auth.active_challenged ) );
}
const auto& comment = db().get_comment( o.author, o.permlink );
if( !o.allow_curation_rewards || !o.allow_votes || o.max_accepted_payout < comment.max_accepted_payout )
FC_ASSERT( comment.abs_rshares == 0 );
FC_ASSERT( o.extensions.size() == 0 );
FC_ASSERT( comment.allow_curation_rewards >= o.allow_curation_rewards );
FC_ASSERT( comment.allow_votes >= o.allow_votes );
FC_ASSERT( comment.max_accepted_payout >= o.max_accepted_payout );
FC_ASSERT( comment.percent_steem_dollars >= o.percent_steem_dollars );
db().modify( comment, [&]( comment_object& c ) {
c.max_accepted_payout = o.max_accepted_payout;
c.percent_steem_dollars = o.percent_steem_dollars;
c.allow_votes = o.allow_votes;
c.allow_curation_rewards = o.allow_curation_rewards;
});
}
void comment_evaluator::do_apply( const comment_operation& o )
{ try {
if( db().is_producing() || db().has_hardfork( STEEMIT_HARDFORK_0_5__55 ) )
FC_ASSERT( o.title.size() + o.body.size() + o.json_metadata.size(), "something should change" );
const auto& by_permlink_idx = db().get_index_type< comment_index >().indices().get< by_permlink >();
auto itr = by_permlink_idx.find( boost::make_tuple( o.author, o.permlink ) );
const auto& auth = db().get_account( o.author ); /// prove it exists
if( db().has_hardfork( STEEMIT_HARDFORK_0_10 ) )
FC_ASSERT( !(auth.owner_challenged || auth.active_challenged ) );
comment_id_type id;
const comment_object* parent = nullptr;
if( o.parent_author.size() != 0 ) {
parent = &db().get_comment( o.parent_author, o.parent_permlink );
FC_ASSERT( parent->depth < STEEMIT_MAX_COMMENT_DEPTH, "Comment is nested ${x} posts deep, maximum depth is ${y}", ("x",parent->depth)("y",STEEMIT_MAX_COMMENT_DEPTH) );
}
auto now = db().head_block_time();
if ( itr == by_permlink_idx.end() )
{
if( o.parent_author.size() != 0 )
{
FC_ASSERT( parent->root_comment( db() ).allow_replies, "Comment has disabled replies." );
if( db().has_hardfork( STEEMIT_HARDFORK_0_12__177) )
FC_ASSERT( db().calculate_discussion_payout_time( *parent ) != fc::time_point_sec::maximum() );
}
if( db().has_hardfork( STEEMIT_HARDFORK_0_12__176 ) )
{
if( o.parent_author.size() == 0 )
FC_ASSERT( (now - auth.last_root_post) > STEEMIT_MIN_ROOT_COMMENT_INTERVAL, "You may only post once every 5 minutes", ("now",now)("auth.last_root_post",auth.last_root_post) );
else
FC_ASSERT( (now - auth.last_post) > STEEMIT_MIN_REPLY_INTERVAL, "You may only comment once every 20 seconds", ("now",now)("auth.last_post",auth.last_post) );
}
else if( db().has_hardfork( STEEMIT_HARDFORK_0_6__113 ) )
{
if( o.parent_author.size() == 0 )
FC_ASSERT( (now - auth.last_post) > STEEMIT_MIN_ROOT_COMMENT_INTERVAL, "You may only post once every 5 minutes", ("now",now)("auth.last_post",auth.last_post) );
else
FC_ASSERT( (now - auth.last_post) > STEEMIT_MIN_REPLY_INTERVAL, "You may only comment once every 20 seconds", ("now",now)("auth.last_post",auth.last_post) );
}
else
{
FC_ASSERT( (now - auth.last_post) > fc::seconds(60), "You may only post once per minute", ("now",now)("auth.last_post",auth.last_post) );
}
uint16_t reward_weight = STEEMIT_100_PERCENT;
uint64_t post_bandwidth = auth.post_bandwidth;
if( db().has_hardfork( STEEMIT_HARDFORK_0_12__176 ) && o.parent_author.size() == 0 )
{
uint64_t post_delta_time = std::min( db().head_block_time().sec_since_epoch() - auth.last_root_post.sec_since_epoch(), STEEMIT_POST_AVERAGE_WINDOW );
uint32_t old_weight = uint32_t( ( post_bandwidth * ( STEEMIT_POST_AVERAGE_WINDOW - post_delta_time ) ) / STEEMIT_POST_AVERAGE_WINDOW );
post_bandwidth = ( old_weight + STEEMIT_100_PERCENT );
reward_weight = uint16_t( std::min( ( STEEMIT_POST_WEIGHT_CONSTANT * STEEMIT_100_PERCENT ) / ( post_bandwidth * post_bandwidth ), uint64_t( STEEMIT_100_PERCENT ) ) );
}
db().modify( auth, [&]( account_object& a ) {
if( o.parent_author.size() == 0 )
{
a.last_root_post = now;
a.post_bandwidth = uint32_t( post_bandwidth );
}
a.last_post = now;
a.post_count++;
});
const auto& new_comment = db().create< comment_object >( [&]( comment_object& com )
{
if( db().has_hardfork( STEEMIT_HARDFORK_0_1 ) )
{
validate_permlink_0_1( o.parent_permlink );
validate_permlink_0_1( o.permlink );
}
com.author = o.author;
com.permlink = o.permlink;
com.last_update = db().head_block_time();
com.created = com.last_update;
com.active = com.last_update;
com.last_payout = fc::time_point_sec::min();
com.max_cashout_time = fc::time_point_sec::maximum();
com.reward_weight = reward_weight;
if ( o.parent_author.size() == 0 )
{
com.parent_author = "";
com.parent_permlink = o.parent_permlink;
com.category = o.parent_permlink;
com.root_comment = com.id;
com.cashout_time = db().has_hardfork( STEEMIT_HARDFORK_0_12__177 ) ?
db().head_block_time() + STEEMIT_CASHOUT_WINDOW_SECONDS :
fc::time_point_sec::maximum();
}
else
{
com.parent_author = parent->author;
com.parent_permlink = parent->permlink;
com.depth = parent->depth + 1;
com.category = parent->category;
com.root_comment = parent->root_comment;
com.cashout_time = fc::time_point_sec::maximum();
}
#ifndef IS_LOW_MEM
com.title = o.title;
if( o.body.size() < 1024*1024*128 )
{
com.body = o.body;
}
com.json_metadata = o.json_metadata;
#endif
});
/** TODO move category behavior to a plugin, this is not part of consensus */
const category_object* cat = db().find_category( new_comment.category );
if( !cat ) {
cat = &db().create<category_object>( [&]( category_object& c ){
c.name = new_comment.category;
c.discussions = 1;
c.last_update = db().head_block_time();
});
} else {
db().modify( *cat, [&]( category_object& c ){
c.discussions++;
c.last_update = db().head_block_time();
});
}
id = new_comment.id;
/// this loop can be skiped for validate-only nodes as it is merely gathering stats for indicies
auto now = db().head_block_time();
while( parent ) {
db().modify( *parent, [&]( comment_object& p ){
p.children++;
p.active = now;
});
#ifndef IS_LOW_MEM
if( parent->parent_author.size() )
parent = &db().get_comment( parent->parent_author, parent->parent_permlink );
else
#endif
parent = nullptr;
}
}
else // start edit case
{
const auto& comment = *itr;
if( db().has_hardfork( STEEMIT_HARDFORK_0_10 ) )
FC_ASSERT( comment.last_payout == fc::time_point_sec::min() );
db().modify( comment, [&]( comment_object& com )
{
com.last_update = db().head_block_time();
com.active = com.last_update;
if( !parent )
{
FC_ASSERT( com.parent_author == "" );
FC_ASSERT( com.parent_permlink == o.parent_permlink, "The permlink of a comment cannot change" );
}
else
{
FC_ASSERT( com.parent_author == o.parent_author );
FC_ASSERT( com.parent_permlink == o.parent_permlink );
}
#ifndef IS_LOW_MEM
if( o.title.size() ) com.title = o.title;
if( o.json_metadata.size() ) com.json_metadata = o.json_metadata;
if( o.body.size() ) {
try {
diff_match_patch<std::wstring> dmp;
auto patch = dmp.patch_fromText( utf8_to_wstring(o.body) );
if( patch.size() ) {
auto result = dmp.patch_apply( patch, utf8_to_wstring(com.body) );
auto patched_body = wstring_to_utf8(result.first);
if( !fc::is_utf8( patched_body ) ) {
idump(("invalid utf8")(patched_body));
com.body = fc::prune_invalid_utf8(patched_body);
} else { com.body = patched_body; }
}
else { // replace
com.body = o.body;
}
} catch ( ... ) {
com.body = o.body;
}
}
#endif
});
} // end EDIT case
} FC_CAPTURE_AND_RETHROW( (o) ) }
void escrow_transfer_evaluator::do_apply( const escrow_transfer_operation& o ) {
try {
FC_ASSERT( false, "Escrow transfer operation not enabled" );
FC_ASSERT( db().has_hardfork( STEEMIT_HARDFORK_0_9 ) ); /// TODO: remove this after HF9
const auto& from_account = db().get_account(o.from);
db().get_account(o.to);
const auto& agent_account = db().get_account(o.agent);
FC_ASSERT( db().get_balance( from_account, o.amount.symbol ) >= (o.amount + o.fee) );
if( o.fee.amount > 0 ) {
db().adjust_balance( from_account, -o.fee );
db().adjust_balance( agent_account, o.fee );
}
db().adjust_balance( from_account, -o.amount );
db().create<escrow_object>([&]( escrow_object& esc ) {
esc.escrow_id = o.escrow_id;
esc.from = o.from;
esc.to = o.to;
esc.agent = o.agent;
esc.balance = o.amount;
esc.expiration = o.expiration;
});
} FC_CAPTURE_AND_RETHROW( (o) ) }
void escrow_dispute_evaluator::do_apply( const escrow_dispute_operation& o ) {
try {
FC_ASSERT( false, "Escrow dispute operation not enabled" );
FC_ASSERT( db().has_hardfork( STEEMIT_HARDFORK_0_9 ) ); /// TODO: remove this after HF9
const auto& from_account = db().get_account(o.from);
const auto& e = db().get_escrow( o.from, o.escrow_id );
FC_ASSERT( !e.disputed );
FC_ASSERT( e.to == o.to );
db().modify( e, [&]( escrow_object& esc ){
esc.disputed = true;
});
} FC_CAPTURE_AND_RETHROW( (o) ) }
void escrow_release_evaluator::do_apply( const escrow_release_operation& o ) {
try {
FC_ASSERT( false, "Escrow release operation not enabled" );
FC_ASSERT( db().has_hardfork( STEEMIT_HARDFORK_0_9 ) ); /// TODO: remove this after HF9
const auto& from_account = db().get_account(o.from);
const auto& to_account = db().get_account(o.to);
const auto& who_account = db().get_account(o.who);
const auto& e = db().get_escrow( o.from, o.escrow_id );
FC_ASSERT( e.balance >= o.amount && e.balance.symbol == o.amount.symbol );
/// TODO assert o.amount > 0
if( e.expiration > db().head_block_time() ) {
if( o.who == e.from ) FC_ASSERT( o.to == e.to );
else if( o.who == e.to ) FC_ASSERT( o.to == e.from );
else {
FC_ASSERT( e.disputed && o.who == e.agent );
}
} else {
FC_ASSERT( o.who == e.to || o.who == e.from );
}
db().adjust_balance( to_account, o.amount );
if( e.balance == o.amount )
db().remove( e );
else {
db().modify( e, [&]( escrow_object& esc ) {
esc.balance -= o.amount;
});
}
} FC_CAPTURE_AND_RETHROW( (o) ) }
void transfer_evaluator::do_apply( const transfer_operation& o )
{
const auto& from_account = db().get_account(o.from);
const auto& to_account = db().get_account(o.to);
if( from_account.active_challenged )
{
db().modify( from_account, [&]( account_object& a )
{
a.active_challenged = false;
a.last_active_proved = db().head_block_time();
});
}
if( o.amount.symbol != VESTS_SYMBOL ) {
FC_ASSERT( db().get_balance( from_account, o.amount.symbol ) >= o.amount );
db().adjust_balance( from_account, -o.amount );
db().adjust_balance( to_account, o.amount );
} else {
/// TODO: this line can be removed after hard fork
FC_ASSERT( false , "transferring of Steem Power (STMP) is not allowed." );
#if 0
/** allow transfer of vesting balance if the full balance is transferred to a new account
* This will allow combining of VESTS but not division of VESTS
**/
FC_ASSERT( db().get_balance( from_account, o.amount.symbol ) == o.amount );
db().modify( to_account, [&]( account_object& a ){
a.vesting_shares += o.amount;
a.voting_power = std::min( to_account.voting_power, from_account.voting_power );
// Update to_account bandwidth. from_account bandwidth is already updated as a result of the transfer op
/*
auto now = db().head_block_time();
auto delta_time = (now - a.last_bandwidth_update).to_seconds();
uint64_t N = trx_size * STEEMIT_BANDWIDTH_PRECISION;
if( delta_time >= STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS )
a.average_bandwidth = N;
else
{
auto old_weight = a.average_bandwidth * (STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS - delta_time);
auto new_weight = delta_time * N;
a.average_bandwidth = (old_weight + new_weight) / (STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS);
}
a.average_bandwidth += from_account.average_bandwidth;
a.last_bandwidth_update = now;
*/
db().adjust_proxied_witness_votes( a, o.amount.amount, 0 );
});
db().modify( from_account, [&]( account_object& a ){
db().adjust_proxied_witness_votes( a, -o.amount.amount, 0 );
a.vesting_shares -= o.amount;
});
#endif
}
}
void transfer_to_vesting_evaluator::do_apply( const transfer_to_vesting_operation& o )
{
const auto& from_account = db().get_account(o.from);
const auto& to_account = o.to.size() ? db().get_account(o.to) : from_account;
FC_ASSERT( db().get_balance( from_account, STEEM_SYMBOL) >= o.amount );
db().adjust_balance( from_account, -o.amount );
db().create_vesting( to_account, o.amount );
}
void withdraw_vesting_evaluator::do_apply( const withdraw_vesting_operation& o )
{
const auto& account = db().get_account( o.account );
FC_ASSERT( account.vesting_shares >= asset( 0, VESTS_SYMBOL ) );
FC_ASSERT( account.vesting_shares >= o.vesting_shares );
if( !account.mined && db().has_hardfork( STEEMIT_HARDFORK_0_1 ) ) {
const auto& props = db().get_dynamic_global_properties();
const witness_schedule_object& wso = db().get_witness_schedule_object();
asset min_vests = wso.median_props.account_creation_fee * props.get_vesting_share_price();
min_vests.amount.value *= 10;
FC_ASSERT( account.vesting_shares > min_vests,
"Account registered by another account requires 10x account creation fee worth of Steem Power before it can power down" );
}
if( o.vesting_shares.amount == 0 ) {
if( db().is_producing() || db().has_hardfork( STEEMIT_HARDFORK_0_5__57 ) )
FC_ASSERT( account.vesting_withdraw_rate.amount != 0, "this operation would not change the vesting withdraw rate" );
db().modify( account, [&]( account_object& a ) {
a.vesting_withdraw_rate = asset( 0, VESTS_SYMBOL );
a.next_vesting_withdrawal = time_point_sec::maximum();
a.to_withdraw = 0;
a.withdrawn = 0;
});
}
else {
db().modify( account, [&]( account_object& a ) {
auto new_vesting_withdraw_rate = asset( o.vesting_shares.amount / STEEMIT_VESTING_WITHDRAW_INTERVALS, VESTS_SYMBOL );
if( new_vesting_withdraw_rate.amount == 0 )
new_vesting_withdraw_rate.amount = 1;
if( db().is_producing() || db().has_hardfork( STEEMIT_HARDFORK_0_5__57 ) )
FC_ASSERT( account.vesting_withdraw_rate != new_vesting_withdraw_rate, "this operation would not change the vesting withdraw rate" );
a.vesting_withdraw_rate = new_vesting_withdraw_rate;
a.next_vesting_withdrawal = db().head_block_time() + fc::seconds(STEEMIT_VESTING_WITHDRAW_INTERVAL_SECONDS);
a.to_withdraw = o.vesting_shares.amount;
a.withdrawn = 0;
});
}
}
void set_withdraw_vesting_route_evaluator::do_apply( const set_withdraw_vesting_route_operation& o )
{
try
{
FC_ASSERT( db().has_hardfork( STEEMIT_HARDFORK_0_6__78 ) );
const auto& from_account = db().get_account( o.from_account );
const auto& to_account = db().get_account( o.to_account );
const auto& wd_idx = db().get_index_type< withdraw_vesting_route_index >().indices().get< by_withdraw_route >();
auto itr = wd_idx.find( boost::make_tuple( from_account.id, to_account.id ) );
if( itr == wd_idx.end() )
{
FC_ASSERT( o.percent != 0, "Cannot create a 0% destination." );
FC_ASSERT( from_account.withdraw_routes < STEEMIT_MAX_WITHDRAW_ROUTES );
db().create< withdraw_vesting_route_object >( [&]( withdraw_vesting_route_object& wvdo )
{
wvdo.from_account = from_account.id;
wvdo.to_account = to_account.id;
wvdo.percent = o.percent;
wvdo.auto_vest = o.auto_vest;
});
db().modify( from_account, [&]( account_object& a )
{
a.withdraw_routes++;
});
}
else if( o.percent == 0 )
{
db().remove( *itr );
db().modify( from_account, [&]( account_object& a )
{
a.withdraw_routes--;
});
}
else
{
db().modify( *itr, [&]( withdraw_vesting_route_object& wvdo )
{
wvdo.from_account = from_account.id;
wvdo.to_account = to_account.id;
wvdo.percent = o.percent;
wvdo.auto_vest = o.auto_vest;
});
}
itr = wd_idx.upper_bound( boost::make_tuple( from_account.id, account_id_type() ) );
uint16_t total_percent = 0;
while( itr->from_account == from_account.id && itr != wd_idx.end() )
{
total_percent += itr->percent;
++itr;
}
FC_ASSERT( total_percent <= STEEMIT_100_PERCENT, "More than 100% of vesting allocated to destinations" );
}
FC_CAPTURE_AND_RETHROW()
}
void account_witness_proxy_evaluator::do_apply( const account_witness_proxy_operation& o )
{
const auto& account = db().get_account( o.account );
FC_ASSERT( account.proxy != o.proxy, "something must change" );
/// remove all current votes
std::array<share_type, STEEMIT_MAX_PROXY_RECURSION_DEPTH+1> delta;
delta[0] = -account.vesting_shares.amount;
for( int i = 0; i < STEEMIT_MAX_PROXY_RECURSION_DEPTH; ++i )
delta[i+1] = -account.proxied_vsf_votes[i];
db().adjust_proxied_witness_votes( account, delta );
if( o.proxy.size() ) {
const auto& new_proxy = db().get_account( o.proxy );
flat_set<account_id_type> proxy_chain({account.get_id(), new_proxy.get_id()});
proxy_chain.reserve( STEEMIT_MAX_PROXY_RECURSION_DEPTH + 1 );
/// check for proxy loops and fail to update the proxy if it would create a loop
auto cprox = &new_proxy;
while( cprox->proxy.size() != 0 ) {
const auto next_proxy = db().get_account( cprox->proxy );
FC_ASSERT( proxy_chain.insert( next_proxy.get_id() ).second, "Attempt to create a proxy loop" );
cprox = &next_proxy;
FC_ASSERT( proxy_chain.size() <= STEEMIT_MAX_PROXY_RECURSION_DEPTH, "Proxy chain is too long" );
}
/// clear all individual vote records
db().clear_witness_votes( account );
db().modify( account, [&]( account_object& a ) {
a.proxy = o.proxy;
});
/// add all new votes
for( int i = 0; i <= STEEMIT_MAX_PROXY_RECURSION_DEPTH; ++i )
delta[i] = -delta[i];
db().adjust_proxied_witness_votes( account, delta );
} else { /// we are clearing the proxy which means we simply update the account
db().modify( account, [&]( account_object& a ) {
a.proxy = o.proxy;
});
}
}
void account_witness_vote_evaluator::do_apply( const account_witness_vote_operation& o )
{
const auto& voter = db().get_account( o.account );
FC_ASSERT( voter.proxy.size() == 0, "A proxy is currently set, please clear the proxy before voting for a witness" );
const auto& witness = db().get_witness( o.witness );
const auto& by_account_witness_idx = db().get_index_type< witness_vote_index >().indices().get< by_account_witness >();
auto itr = by_account_witness_idx.find( boost::make_tuple( voter.get_id(), witness.get_id() ) );
if( itr == by_account_witness_idx.end() ) {
FC_ASSERT( o.approve, "vote doesn't exist, user must be indicate a desire to approve witness" );
if ( db().has_hardfork( STEEMIT_HARDFORK_0_2 ) )
{
FC_ASSERT( voter.witnesses_voted_for < STEEMIT_MAX_ACCOUNT_WITNESS_VOTES, "account has voted for too many witnesses" ); // TODO: Remove after hardfork 2
db().create<witness_vote_object>( [&]( witness_vote_object& v ) {
v.witness = witness.id;
v.account = voter.id;
});
if( db().has_hardfork( STEEMIT_HARDFORK_0_3 ) ) {
db().adjust_witness_vote( witness, voter.witness_vote_weight() );
}
else {
db().adjust_proxied_witness_votes( voter, voter.witness_vote_weight() );
}
} else {
db().create<witness_vote_object>( [&]( witness_vote_object& v ) {
v.witness = witness.id;
v.account = voter.id;
});
db().modify( witness, [&]( witness_object& w ) {
w.votes += voter.witness_vote_weight();
});
}
db().modify( voter, [&]( account_object& a ) {
a.witnesses_voted_for++;
});
} else {
FC_ASSERT( !o.approve, "vote currently exists, user must be indicate a desire to reject witness" );
if ( db().has_hardfork( STEEMIT_HARDFORK_0_2 ) ) {
if( db().has_hardfork( STEEMIT_HARDFORK_0_3 ) )
db().adjust_witness_vote( witness, -voter.witness_vote_weight() );
else
db().adjust_proxied_witness_votes( voter, -voter.witness_vote_weight() );
} else {
db().modify( witness, [&]( witness_object& w ) {
w.votes -= voter.witness_vote_weight();
});
}
db().modify( voter, [&]( account_object& a ) {
a.witnesses_voted_for--;
});
db().remove( *itr );
}
}
void vote_evaluator::do_apply( const vote_operation& o )
{ try {
const auto& comment = db().get_comment( o.author, o.permlink );
const auto& voter = db().get_account( o.voter );
if( db().has_hardfork( STEEMIT_HARDFORK_0_10 ) )
FC_ASSERT( !(voter.owner_challenged || voter.active_challenged ) );
if( o.weight > 0 ) FC_ASSERT( comment.allow_votes );
if( db().has_hardfork( STEEMIT_HARDFORK_0_12__177 ) && db().calculate_discussion_payout_time( comment ) == fc::time_point_sec::maximum() )
{
#ifndef CLEAR_VOTES
const auto& comment_vote_idx = db().get_index_type< comment_vote_index >().indices().get< by_comment_voter >();
auto itr = comment_vote_idx.find( std::make_tuple( comment.id, voter.id ) );
if( itr == comment_vote_idx.end() )
db().create< comment_vote_object >( [&]( comment_vote_object& cvo )
{
cvo.voter = voter.id;
cvo.comment = comment.id;
cvo.vote_percent = o.weight;
cvo.last_update = db().head_block_time();
});
else
db().modify( *itr, [&]( comment_vote_object& cvo )
{
cvo.vote_percent = o.weight;
cvo.last_update = db().head_block_time();
});
#endif
return;
}
const auto& comment_vote_idx = db().get_index_type< comment_vote_index >().indices().get< by_comment_voter >();
auto itr = comment_vote_idx.find( std::make_tuple( comment.id, voter.id ) );
auto elapsed_seconds = (db().head_block_time() - voter.last_vote_time).to_seconds();
if( db().has_hardfork( STEEMIT_HARDFORK_0_11 ) )
FC_ASSERT( elapsed_seconds >= STEEMIT_MIN_VOTE_INTERVAL_SEC );
auto regenerated_power = (STEEMIT_100_PERCENT * elapsed_seconds) / STEEMIT_VOTE_REGENERATION_SECONDS;
auto current_power = std::min( int64_t(voter.voting_power + regenerated_power), int64_t(STEEMIT_100_PERCENT) );
FC_ASSERT( current_power > 0 );
int64_t abs_weight = abs(o.weight);
auto used_power = (current_power * abs_weight) / STEEMIT_100_PERCENT;
used_power = (used_power/200) + 1;
FC_ASSERT( used_power <= current_power );
int64_t abs_rshares = ((uint128_t(voter.vesting_shares.amount.value) * used_power) / (STEEMIT_100_PERCENT)).to_uint64();
if( abs_rshares == 0 ) abs_rshares = 1;
if( db().is_producing() ) {
FC_ASSERT( abs_rshares > 30000000, "voting weight is too small, please accumulate more voting power or steem power" );
}
// Lazily delete vote
if( itr != comment_vote_idx.end() && itr->num_changes == -1 )
{
if( db().is_producing() || db().has_hardfork( STEEMIT_HARDFORK_0_12__177 ) )
FC_ASSERT( false, "Cannot vote again on a comment after payout" );
db().remove( *itr );
itr = comment_vote_idx.end();
}
if( itr == comment_vote_idx.end() )
{
FC_ASSERT( o.weight != 0, "Vote weight cannot be 0" );
/// this is the rshares voting for or against the post
int64_t rshares = o.weight < 0 ? -abs_rshares : abs_rshares;
if( rshares > 0 && db().has_hardfork( STEEMIT_HARDFORK_0_7 ) )
{
FC_ASSERT( db().head_block_time() < db().calculate_discussion_payout_time( comment ) - STEEMIT_UPVOTE_LOCKOUT );
}
//used_power /= (50*7); /// a 100% vote means use .28% of voting power which should force users to spread their votes around over 50+ posts day for a week
//if( used_power == 0 ) used_power = 1;
db().modify( voter, [&]( account_object& a ){
a.voting_power = current_power - used_power;
a.last_vote_time = db().head_block_time();
});
/// if the current net_rshares is less than 0, the post is getting 0 rewards so it is not factored into total rshares^2
fc::uint128_t old_rshares = std::max(comment.net_rshares.value, int64_t(0));
const auto& root = comment.root_comment( db() );
auto old_root_abs_rshares = root.children_abs_rshares.value;
fc::uint128_t cur_cashout_time_sec = db().calculate_discussion_payout_time( comment ).sec_since_epoch();
fc::uint128_t new_cashout_time_sec;
if( db().has_hardfork( STEEMIT_HARDFORK_0_12__177 ) )
new_cashout_time_sec = db().head_block_time().sec_since_epoch() + STEEMIT_CASHOUT_WINDOW_SECONDS;
else
new_cashout_time_sec = db().head_block_time().sec_since_epoch() + STEEMIT_CASHOUT_WINDOW_SECONDS_PRE_HF12;
auto avg_cashout_sec = ( cur_cashout_time_sec * old_root_abs_rshares + new_cashout_time_sec * abs_rshares ) / ( old_root_abs_rshares + abs_rshares );
FC_ASSERT( abs_rshares > 0 );
auto old_vote_rshares = comment.vote_rshares;
db().modify( comment, [&]( comment_object& c ){
c.net_rshares += rshares;
c.abs_rshares += abs_rshares;
if( rshares > 0 )
c.vote_rshares += rshares;
if( rshares > 0 )
c.net_votes++;
else
c.net_votes--;
if( !db().has_hardfork( STEEMIT_HARDFORK_0_6__114 ) && c.net_rshares == -c.abs_rshares) FC_ASSERT( c.net_votes < 0 );
});
db().modify( root, [&]( comment_object& c )
{
c.children_abs_rshares += abs_rshares;
if( db().has_hardfork( STEEMIT_HARDFORK_0_12__177 ) && c.last_payout > fc::time_point_sec::min() )
c.cashout_time = c.last_payout + STEEMIT_SECOND_CASHOUT_WINDOW;
else
c.cashout_time = fc::time_point_sec( std::min( uint32_t( avg_cashout_sec.to_uint64() ), c.max_cashout_time.sec_since_epoch() ) );
if( c.max_cashout_time == fc::time_point_sec::maximum() )
c.max_cashout_time = db().head_block_time() + fc::seconds( STEEMIT_MAX_CASHOUT_WINDOW_SECONDS );
});
fc::uint128_t new_rshares = std::max( comment.net_rshares.value, int64_t(0));
/// calculate rshares2 value
new_rshares = db().calculate_vshares( new_rshares );
old_rshares = db().calculate_vshares( old_rshares );
const auto& cat = db().get_category( comment.category );
db().modify( cat, [&]( category_object& c ){
c.abs_rshares += abs_rshares;
c.last_update = db().head_block_time();
});
uint64_t max_vote_weight = 0;
/** this verifies uniqueness of voter
*
* cv.weight / c.total_vote_weight ==> % of rshares increase that is accounted for by the vote
*
* W(R) = B * R / ( R + 2S )
* W(R) is bounded above by B. B is fixed at 2^64 - 1, so all weights fit in a 64 bit integer.
*
* The equation for an individual vote is:
* W(R_N) - W(R_N-1), which is the delta increase of proportional weight
*
* c.total_vote_weight =
* W(R_1) - W(R_0) +
* W(R_2) - W(R_1) + ...
* W(R_N) - W(R_N-1) = W(R_N) - W(R_0)
*
* Since W(R_0) = 0, c.total_vote_weight is also bounded above by B and will always fit in a 64 bit integer.
*
**/
const auto& cvo = db().create<comment_vote_object>( [&]( comment_vote_object& cv ){
cv.voter = voter.id;
cv.comment = comment.id;
cv.rshares = rshares;
cv.vote_percent = o.weight;
cv.last_update = db().head_block_time();