This repository has been archived by the owner on Oct 4, 2019. It is now read-only.
forked from steemit/steem
-
Notifications
You must be signed in to change notification settings - Fork 35
/
steem_evaluator.cpp
2579 lines (2185 loc) · 123 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 <golos/chain/steem_evaluator.hpp>
#include <golos/chain/database.hpp>
#include <golos/chain/custom_operation_interpreter.hpp>
#include <golos/chain/steem_objects.hpp>
#include <golos/chain/block_summary_object.hpp>
#include <golos/chain/worker_objects.hpp>
#define GOLOS_CHECK_BANDWIDTH(NOW, NEXT, TYPE, MSG, ...) \
GOLOS_ASSERT((NOW) > (NEXT), golos::bandwidth_exception, MSG, \
("bandwidth",TYPE)("now",NOW)("next",NEXT) __VA_ARGS__)
namespace golos { namespace chain {
using fc::uint128_t;
inline void validate_permlink_0_1(const string &permlink) {
GOLOS_CHECK_VALUE(permlink.size() > STEEMIT_MIN_PERMLINK_LENGTH &&
permlink.size() < STEEMIT_MAX_PERMLINK_LENGTH,
"Permlink is not a valid size. Permlink length should be more ${min} and less ${max}",
("min", STEEMIT_MIN_PERMLINK_LENGTH)("max", 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_THROW_EXCEPTION(invalid_value, "Invalid permlink character: ${s}",
("s", std::string() + c));
}
}
}
struct strcmp_equal {
bool operator()(const shared_string &a, const string &b) {
return a.size() == b.size() ||
std::strcmp(a.c_str(), b.c_str()) == 0;
}
};
void store_account_json_metadata(
database& db, const account_name_type& account, const string& json_metadata, bool skip_empty = false
) {
if (!db.store_metadata_for_account(account)) {
auto meta = db.find<account_metadata_object, by_account>(account);
if (meta != nullptr) {
db.remove(*meta);
}
return;
}
if (skip_empty && json_metadata.size() == 0)
return;
const auto& idx = db.get_index<account_metadata_index>().indices().get<by_account>();
auto itr = idx.find(account);
if (itr != idx.end()) {
db.modify(*itr, [&](account_metadata_object& a) {
from_string(a.json_metadata, json_metadata);
});
} else {
// Note: this branch should be executed only on account creation.
db.create<account_metadata_object>([&](account_metadata_object& a) {
a.account = account;
from_string(a.json_metadata, json_metadata);
});
}
}
void account_create_evaluator::do_apply(const account_create_operation &o) {
const auto& creator = _db.get_account(o.creator);
GOLOS_CHECK_BALANCE(creator, MAIN_BALANCE, o.fee);
if (_db.has_hardfork(STEEMIT_HARDFORK_0_1)) {
const auto& median_props = _db.get_witness_schedule_object().median_props;
auto min_fee = median_props.account_creation_fee;
GOLOS_CHECK_OP_PARAM(o, fee,
GOLOS_CHECK_VALUE(o.fee >= min_fee,
"Insufficient Fee: ${f} required, ${p} provided.", ("f", min_fee)("p", o.fee));
);
}
if (_db.is_producing() ||
_db.has_hardfork(STEEMIT_HARDFORK_0_15__465)) {
for (auto &a : o.owner.account_auths) {
_db.get_account(a.first);
}
for (auto &a : o.active.account_auths) {
_db.get_account(a.first);
}
for (auto &a : o.posting.account_auths) {
_db.get_account(a.first);
}
}
_db.modify(creator, [&](account_object &c) {
c.balance -= o.fee;
});
GOLOS_CHECK_OBJECT_MISSING(_db, account, o.new_account_name);
const auto& props = _db.get_dynamic_global_properties();
const auto& new_account = _db.create<account_object>([&](account_object& acc) {
acc.name = o.new_account_name;
acc.memo_key = o.memo_key;
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 = STEEMIT_INIT_MINER_NAME;
} else {
acc.recovery_account = o.creator;
}
});
store_account_json_metadata(_db, o.new_account_name, o.json_metadata);
_db.create<account_authority_object>([&](account_authority_object &auth) {
auth.account = o.new_account_name;
auth.owner = o.owner;
auth.active = o.active;
auth.posting = o.posting;
auth.last_owner_update = fc::time_point_sec::min();
});
if (o.fee.amount > 0) {
_db.create_vesting(new_account, o.fee);
}
}
struct account_create_with_delegation_extension_visitor {
account_create_with_delegation_extension_visitor(const account_object& a, database& db)
: _a(a), _db(db) {
}
using result_type = void;
const account_object& _a;
database& _db;
void operator()(const account_referral_options& aro) const {
ASSERT_REQ_HF(STEEMIT_HARDFORK_0_19__295, "account_referral_options");
_db.get_account(aro.referrer);
const auto& median_props = _db.get_witness_schedule_object().median_props;
GOLOS_CHECK_LIMIT_PARAM(aro.interest_rate, median_props.max_referral_interest_rate);
GOLOS_CHECK_PARAM(aro.end_date,
GOLOS_CHECK_VALUE(aro.end_date >= _db.head_block_time(), "End date should be in the future"));
GOLOS_CHECK_LIMIT_PARAM(aro.end_date, _db.head_block_time() + median_props.max_referral_term_sec);
GOLOS_CHECK_LIMIT_PARAM(aro.break_fee, median_props.max_referral_break_fee);
_db.modify(_a, [&](account_object& a) {
a.referrer_account = aro.referrer;
a.referrer_interest_rate = aro.interest_rate;
a.referral_end_date = aro.end_date;
a.referral_break_fee = aro.break_fee;
});
}
};
void account_create_with_delegation_evaluator::do_apply(const account_create_with_delegation_operation& o) {
const auto& creator = _db.get_account(o.creator);
GOLOS_CHECK_BALANCE(creator, MAIN_BALANCE, o.fee);
GOLOS_CHECK_BALANCE(creator, AVAILABLE_VESTING, o.delegation);
const auto& v_share_price = _db.get_dynamic_global_properties().get_vesting_share_price();
const auto& median_props = _db.get_witness_schedule_object().median_props;
const auto target = median_props.create_account_min_golos_fee + median_props.create_account_min_delegation;
auto target_delegation = target * v_share_price;
auto min_fee = median_props.account_creation_fee.amount.value;
#ifdef STEEMIT_BUILD_TESTNET
if (!min_fee)
min_fee = 1;
#endif
auto current_delegation = o.fee * target.amount.value / min_fee * v_share_price + o.delegation;
GOLOS_CHECK_LOGIC(current_delegation >= target_delegation,
logic_exception::not_enough_delegation,
"Insufficient Delegation ${f} required, ${p} provided.",
("f", target_delegation)("p", current_delegation)("o.fee", o.fee) ("o.delegation", o.delegation));
auto min_golos = median_props.create_account_min_golos_fee;
GOLOS_CHECK_OP_PARAM(o, fee, {
GOLOS_CHECK_VALUE(o.fee >= min_golos,
"Insufficient Fee: ${f} required, ${p} provided.", ("f", min_golos)("p", o.fee));
});
for (auto& a : o.owner.account_auths) {
_db.get_account(a.first);
}
for (auto& a : o.active.account_auths) {
_db.get_account(a.first);
}
for (auto& a : o.posting.account_auths) {
_db.get_account(a.first);
}
const auto now = _db.head_block_time();
_db.modify(creator, [&](account_object& c) {
c.balance -= o.fee;
c.delegated_vesting_shares += o.delegation;
});
const auto& new_account = _db.create<account_object>([&](account_object& acc) {
acc.name = o.new_account_name;
acc.memo_key = o.memo_key;
acc.created = now;
acc.last_vote_time = now;
acc.mined = false;
acc.recovery_account = o.creator;
acc.received_vesting_shares = o.delegation;
});
store_account_json_metadata(_db, o.new_account_name, o.json_metadata);
_db.create<account_authority_object>([&](account_authority_object& auth) {
auth.account = o.new_account_name;
auth.owner = o.owner;
auth.active = o.active;
auth.posting = o.posting;
auth.last_owner_update = fc::time_point_sec::min();
});
if (o.delegation.amount > 0) {
_db.create<vesting_delegation_object>([&](vesting_delegation_object& d) {
d.delegator = o.creator;
d.delegatee = o.new_account_name;
d.vesting_shares = o.delegation;
d.min_delegation_time = now + fc::seconds(median_props.create_account_delegation_time);
});
}
if (o.fee.amount > 0) {
_db.create_vesting(new_account, o.fee);
}
for (auto& e : o.extensions) {
e.visit(account_create_with_delegation_extension_visitor(new_account, _db));
}
}
void account_update_evaluator::do_apply(const account_update_operation &o) {
if (_db.has_hardfork(STEEMIT_HARDFORK_0_1))
GOLOS_CHECK_OP_PARAM(o, account,
GOLOS_CHECK_VALUE(o.account != STEEMIT_TEMP_ACCOUNT,
"Cannot update temp account."));
if ((_db.has_hardfork(STEEMIT_HARDFORK_0_15__465) ||
_db.is_producing()) && o.posting) { // TODO: Add HF 15
o.posting->validate();
}
const auto &account = _db.get_account(o.account);
const auto &account_auth = _db.get_authority(o.account);
if (o.owner) {
#ifndef STEEMIT_BUILD_TESTNET
if (_db.has_hardfork(STEEMIT_HARDFORK_0_11))
GOLOS_CHECK_BANDWIDTH(_db.head_block_time(),
account_auth.last_owner_update + STEEMIT_OWNER_UPDATE_LIMIT,
bandwidth_exception::change_owner_authority_bandwidth,
"Owner authority can only be updated once an hour.");
#endif
if ((_db.has_hardfork(STEEMIT_HARDFORK_0_15__465) ||
_db.is_producing())) // TODO: Add HF 15
{
for (auto a: o.owner->account_auths) {
_db.get_account(a.first);
}
}
_db.update_owner_authority(account, *o.owner);
}
if (o.active && (_db.has_hardfork(STEEMIT_HARDFORK_0_15__465) ||
_db.is_producing())) // TODO: Add HF 15
{
for (auto a: o.active->account_auths) {
_db.get_account(a.first);
}
}
if (o.posting && (_db.has_hardfork(STEEMIT_HARDFORK_0_15__465) ||
_db.is_producing())) // TODO: Add HF 15
{
for (auto a: o.posting->account_auths) {
_db.get_account(a.first);
}
}
_db.modify(account, [&](account_object &acc) {
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();
}
acc.last_account_update = _db.head_block_time();
});
store_account_json_metadata(_db, account.name, o.json_metadata, true);
if (o.active || o.posting) {
_db.modify(account_auth, [&](account_authority_object &auth) {
if (o.active) {
auth.active = *o.active;
}
if (o.posting) {
auth.posting = *o.posting;
}
});
}
}
void account_metadata_evaluator::do_apply(const account_metadata_operation& o) {
const auto& account = _db.get_account(o.account);
_db.modify(account, [&](account_object& a) {
a.last_account_update = _db.head_block_time();
});
store_account_json_metadata(_db, o.account, o.json_metadata);
}
/**
* 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);
GOLOS_CHECK_LOGIC(!(auth.owner_challenged || auth.active_challenged),
logic_exception::account_is_currently_challenged,
"Operation cannot be processed because account is currently challenged.");
}
const auto &comment = _db.get_comment(o.author, o.permlink);
if (_db.has_hardfork(STEEMIT_HARDFORK_0_21__1013)
&& comment.parent_author == STEEMIT_ROOT_POST_PARENT) {
const auto* wpo = _db.find_worker_proposal(comment.id);
GOLOS_CHECK_LOGIC(!wpo,
logic_exception::cannot_delete_post_with_worker_proposal,
"Cannot delete a post with worker proposal.");
const auto* wto = _db.find_worker_techspec(comment.id);
GOLOS_CHECK_LOGIC(!wto,
logic_exception::cannot_delete_post_with_worker_techspec,
"Cannot delete a post with worker techspec.");
const auto* wto_result = _db.find_worker_result(comment.id);
GOLOS_CHECK_LOGIC(!wto_result,
logic_exception::cannot_delete_post_with_worker_result,
"Cannot delete a post with worker result.");
}
GOLOS_CHECK_LOGIC(comment.children == 0,
logic_exception::cannot_delete_comment_with_replies,
"Cannot delete a comment with replies.");
if (_db.is_producing()) {
GOLOS_CHECK_LOGIC(comment.net_rshares <= 0,
logic_exception::cannot_delete_comment_with_positive_votes,
"Cannot delete a comment with network positive votes.");
}
if (comment.net_rshares > 0) {
return;
}
const auto &vote_idx = _db.get_index<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 != STEEMIT_ROOT_POST_PARENT) {
auto parent = &_db.get_comment(comment.parent_author, comment.parent_permlink);
while (parent) {
_db.modify(*parent, [&](comment_object &p) {
p.children--;
});
if (parent->parent_author != STEEMIT_ROOT_POST_PARENT) {
parent = &_db.get_comment(parent->parent_author, parent->parent_permlink);
} else
{
parent = nullptr;
}
}
}
_db.remove(comment);
}
struct comment_options_extension_visitor {
comment_options_extension_visitor(const comment_object& c, database& db)
: _c(c), _db(db), _a(db.get_account(_c.author)) {
}
using result_type = void;
const comment_object& _c;
database& _db;
const account_object& _a;
void operator()(const comment_payout_beneficiaries& cpb) const {
if (_db.is_producing()) {
GOLOS_CHECK_LOGIC(cpb.beneficiaries.size() <= STEEMIT_MAX_COMMENT_BENEFICIARIES,
logic_exception::cannot_specify_more_beneficiaries,
"Cannot specify more than ${m} beneficiaries.", ("m", STEEMIT_MAX_COMMENT_BENEFICIARIES));
}
uint16_t total_weight = 0;
if (_c.beneficiaries.size() == 1
&& _c.beneficiaries.front().account == _a.referrer_account) {
total_weight += _c.beneficiaries[0].weight;
auto& referrer = _a.referrer_account;
const auto& itr = std::find_if(cpb.beneficiaries.begin(), cpb.beneficiaries.end(),
[&referrer](const beneficiary_route_type& benef) {
return benef.account == referrer;
});
GOLOS_CHECK_LOGIC(itr == cpb.beneficiaries.end(),
logic_exception::beneficiaries_should_be_unique,
"Comment already has '${referrer}' as a referrer-beneficiary.", ("referrer",referrer));
} else {
GOLOS_CHECK_LOGIC(_c.beneficiaries.size() == 0,
logic_exception::comment_already_has_beneficiaries,
"Comment already has beneficiaries specified.");
}
GOLOS_CHECK_LOGIC(_c.abs_rshares == 0,
logic_exception::comment_must_not_have_been_voted,
"Comment must not have been voted on before specifying beneficiaries.");
_db.modify(_c, [&](comment_object& c) {
for (auto& b : cpb.beneficiaries) {
_db.get_account(b.account); // check beneficiary exists
c.beneficiaries.push_back(b);
total_weight += b.weight;
}
});
GOLOS_CHECK_PARAM("beneficiaries", {
GOLOS_CHECK_VALUE(total_weight <= STEEMIT_100_PERCENT,
"Cannot allocate more than 100% of rewards to a comment");
});
}
void operator()( const comment_auction_window_reward_destination& cawrd ) const {
ASSERT_REQ_HF(STEEMIT_HARDFORK_0_19__898, "auction window reward destination option");
const auto& mprops = _db.get_witness_schedule_object().median_props;
if (_db.has_hardfork(STEEMIT_HARDFORK_0_20__1075)) {
GOLOS_CHECK_LOGIC(_c.abs_rshares == 0,
logic_exception::comment_must_not_have_been_voted,
"Comment must not have been voted on changing auction window reward destination.");
}
GOLOS_CHECK_PARAM(cawrd.destination, {
GOLOS_CHECK_VALUE(cawrd.destination != to_reward_fund || mprops.allow_return_auction_reward_to_fund,
"Returning to reward fund is disallowed."
);
});
GOLOS_CHECK_PARAM(cawrd.destination, {
GOLOS_CHECK_VALUE(cawrd.destination != to_curators || mprops.allow_distribute_auction_reward,
"Distributing between curators is disallowed."
);
});
_db.modify(_c, [&](comment_object& c) {
c.auction_window_reward_destination = cawrd.destination;
});
}
void operator()(const comment_curation_rewards_percent& ccrp) const {
ASSERT_REQ_HF(STEEMIT_HARDFORK_0_19__324, "comment curation rewards percent option");
const auto& mprops = _db.get_witness_schedule_object().median_props;
auto percent = ccrp.percent; // Workaround for correct param name in GOLOS_CHECK_PARAM
if (_db.has_hardfork(STEEMIT_HARDFORK_0_20__1075)) {
GOLOS_CHECK_LOGIC(_c.abs_rshares == 0,
logic_exception::comment_must_not_have_been_voted,
"Comment must not have been voted on changing curation rewards percent.");
}
GOLOS_CHECK_PARAM(percent, {
GOLOS_CHECK_VALUE(mprops.min_curation_percent <= ccrp.percent && ccrp.percent <= mprops.max_curation_percent,
"Curation rewards percent must be between ${min} and ${max}.",
("min", mprops.min_curation_percent)("max", mprops.max_curation_percent));
});
_db.modify(_c, [&](comment_object& c) {
c.curation_rewards_percent = ccrp.percent;
});
}
};
void comment_options_evaluator::do_apply(const comment_options_operation &o) {
database &_db = db();
if (_db.has_hardfork(STEEMIT_HARDFORK_0_10)) {
const auto &auth = _db.get_account(o.author);
GOLOS_CHECK_LOGIC(!(auth.owner_challenged || auth.active_challenged),
logic_exception::account_is_currently_challenged,
"Operation cannot be processed because account is currently 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) {
GOLOS_CHECK_LOGIC(comment.abs_rshares == 0,
logic_exception::comment_options_requires_no_rshares,
"One of the included comment options requires the comment to have no rshares allocated to it.");
}
GOLOS_CHECK_LOGIC(comment.allow_curation_rewards >= o.allow_curation_rewards,
logic_exception::curation_rewards_cannot_be_reenabled,
"Curation rewards cannot be re-enabled.");
GOLOS_CHECK_LOGIC(comment.allow_votes >= o.allow_votes,
logic_exception::voting_cannot_be_reenabled,
"Voting cannot be re-enabled.");
GOLOS_CHECK_LOGIC(comment.max_accepted_payout >= o.max_accepted_payout,
logic_exception::comment_cannot_accept_greater_payout,
"A comment cannot accept a greater payout.");
GOLOS_CHECK_LOGIC(comment.percent_steem_dollars >= o.percent_steem_dollars,
logic_exception::comment_cannot_accept_greater_percent_GBG,
"A comment cannot accept a greater percent SBD.");
_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;
});
for (auto& e : o.extensions) {
e.visit(comment_options_extension_visitor(comment, _db));
}
}
struct comment_bandwidth final {
database& db;
const time_point_sec& now;
const comment_operation& op;
const chain_properties& mprops;
const account_object& auth;
mutable const account_bandwidth_object* band = nullptr;
uint16_t calc_reward_weight() const {
band = db.find<account_bandwidth_object, by_account_bandwidth_type>(
std::make_tuple(op.author, bandwidth_type::post));
if (nullptr == band) {
band = &db.create<account_bandwidth_object>([&](account_bandwidth_object &b) {
b.account = op.author;
b.type = bandwidth_type::post;
});
}
if (db.has_hardfork(STEEMIT_HARDFORK_0_19__533_1002)) {
hf19();
} else if (db.has_hardfork(STEEMIT_HARDFORK_0_12__176)) {
hf12();
} else if (db.has_hardfork(STEEMIT_HARDFORK_0_6__113)) {
hf6();
} else {
hf0();
}
return calculate();
}
private:
uint16_t calculate() const {
uint16_t reward_weight = STEEMIT_100_PERCENT;
if (op.parent_author == STEEMIT_ROOT_POST_PARENT) {
auto pb = band->average_bandwidth;
if (db.has_hardfork(STEEMIT_HARDFORK_0_12__176)) {
auto post_delta_time = std::min(
now.sec_since_epoch() - band->last_bandwidth_update.sec_since_epoch(),
STEEMIT_POST_AVERAGE_WINDOW);
auto old_weight =
(pb * (STEEMIT_POST_AVERAGE_WINDOW - post_delta_time)) /
STEEMIT_POST_AVERAGE_WINDOW;
pb = (old_weight + STEEMIT_100_PERCENT);
reward_weight = uint16_t(std::min(
(STEEMIT_POST_WEIGHT_CONSTANT * STEEMIT_100_PERCENT) / (pb.value * pb.value),
uint64_t(STEEMIT_100_PERCENT)));
}
db.modify(*band, [&](account_bandwidth_object &b) {
b.last_bandwidth_update = now;
b.average_bandwidth = pb;
});
}
return reward_weight;
}
void hf19() const {
auto elapsed_seconds = (now - auth.last_post).to_seconds();
if (op.parent_author == STEEMIT_ROOT_POST_PARENT) {
auto consumption = mprops.posts_window / mprops.posts_per_window;
auto regenerated_capacity = std::min(
uint32_t(mprops.posts_window),
uint32_t(elapsed_seconds));
auto current_capacity = std::min(
uint16_t(auth.posts_capacity + regenerated_capacity),
mprops.posts_window);
GOLOS_CHECK_BANDWIDTH(current_capacity + 1, consumption,
bandwidth_exception::post_bandwidth,
"You may only post ${posts_per_window} times in ${posts_window} seconds.",
("posts_per_window", mprops.posts_per_window)
("posts_window", mprops.posts_window));
db.modify(auth, [&](account_object& a) {
a.posts_capacity = current_capacity - consumption;
});
} else {
auto consumption = mprops.comments_window / mprops.comments_per_window;
auto regenerated_capacity = std::min(
uint32_t(mprops.comments_window),
uint32_t(elapsed_seconds));
auto current_capacity = std::min(
uint16_t(auth.comments_capacity + regenerated_capacity),
mprops.comments_window);
GOLOS_CHECK_BANDWIDTH(current_capacity + 1, consumption,
bandwidth_exception::comment_bandwidth,
"You may only comment ${comments_per_window} times in ${comments_window} seconds.",
("comments_per_window", mprops.comments_per_window)
("comments_window", mprops.comments_window));
db.modify(auth, [&](account_object& a) {
a.comments_capacity = current_capacity - consumption;
});
}
}
void hf12() const {
if (op.parent_author == STEEMIT_ROOT_POST_PARENT) {
GOLOS_CHECK_BANDWIDTH(now, band->last_bandwidth_update + STEEMIT_MIN_ROOT_COMMENT_INTERVAL,
bandwidth_exception::post_bandwidth,
"You may only post once every 5 minutes.");
} else {
GOLOS_CHECK_BANDWIDTH(now, auth.last_post + STEEMIT_MIN_REPLY_INTERVAL,
golos::bandwidth_exception::comment_bandwidth,
"You may only comment once every 20 seconds.");
}
}
void hf6() const {
if (op.parent_author == STEEMIT_ROOT_POST_PARENT) {
GOLOS_CHECK_BANDWIDTH(now, auth.last_post + STEEMIT_MIN_ROOT_COMMENT_INTERVAL,
bandwidth_exception::post_bandwidth,
"You may only post once every 5 minutes.");
} else {
GOLOS_CHECK_BANDWIDTH(now, auth.last_post + STEEMIT_MIN_REPLY_INTERVAL,
bandwidth_exception::comment_bandwidth,
"You may only comment once every 20 seconds.");
}
}
void hf0() const {
GOLOS_CHECK_BANDWIDTH(now, auth.last_post + 60,
bandwidth_exception::post_bandwidth,
"You may only post once per minute.");
}
}; // struct check_comment_bandwidth
void comment_evaluator::do_apply(const comment_operation &o) {
try {
if (_db.is_producing() ||
_db.has_hardfork(STEEMIT_HARDFORK_0_5__55))
GOLOS_CHECK_LOGIC(o.title.size() + o.body.size() + o.json_metadata.size(),
logic_exception::cannot_update_comment_because_nothing_changed,
"Cannot update comment because nothing appears to be changing.");
const auto& mprops = _db.get_witness_schedule_object().median_props;
const auto &by_permlink_idx = _db.get_index<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)) {
GOLOS_CHECK_LOGIC(!(auth.owner_challenged || auth.active_challenged),
logic_exception::account_is_currently_challenged,
"Operation cannot be processed because account is currently challenged.");
}
comment_id_type id;
const comment_object *parent = nullptr;
if (o.parent_author != STEEMIT_ROOT_POST_PARENT) {
parent = &_db.get_comment(o.parent_author, o.parent_permlink);
auto max_depth = STEEMIT_MAX_COMMENT_DEPTH;
if (!_db.has_hardfork(STEEMIT_HARDFORK_0_17__430)) {
max_depth = STEEMIT_MAX_COMMENT_DEPTH_PRE_HF17;
} else if (_db.is_producing()) {
max_depth = STEEMIT_SOFT_MAX_COMMENT_DEPTH;
}
GOLOS_CHECK_LOGIC(parent->depth < max_depth,
logic_exception::reached_comment_max_depth,
"Comment is nested ${x} posts deep, maximum depth is ${y}.",
("x", parent->depth)("y", max_depth));
}
auto now = _db.head_block_time();
if (itr == by_permlink_idx.end()) {
if (o.parent_author != STEEMIT_ROOT_POST_PARENT) {
GOLOS_CHECK_LOGIC(_db.get(parent->root_comment).allow_replies,
logic_exception::replies_are_not_allowed,
"The parent comment has disabled replies.");
}
uint16_t reward_weight = comment_bandwidth{_db, now, o, mprops, auth}.calc_reward_weight();
db().modify(auth, [&](account_object &a) {
a.last_post = now;
if (o.parent_author != STEEMIT_ROOT_POST_PARENT) {
a.comment_count++;
} else {
a.post_count++;
}
});
bool referrer_to_delete = false;
_db.create<comment_object>([&](comment_object &com) {
if (_db.has_hardfork(STEEMIT_HARDFORK_0_1)) {
GOLOS_CHECK_OP_PARAM(o, parent_permlink, validate_permlink_0_1(o.parent_permlink));
GOLOS_CHECK_OP_PARAM(o, permlink, validate_permlink_0_1(o.permlink));
}
if (_db.has_hardfork(STEEMIT_HARDFORK_0_19__898)) {
if (mprops.allow_distribute_auction_reward) {
com.auction_window_reward_destination = protocol::to_curators;
} else {
com.auction_window_reward_destination = protocol::to_reward_fund;
}
com.auction_window_size = mprops.auction_window_size;
}
if (_db.has_hardfork(STEEMIT_HARDFORK_0_19__324)) {
com.curation_rewards_percent = mprops.min_curation_percent;
} else {
com.curation_rewards_percent = STEEMIT_MIN_CURATION_PERCENT;
}
com.author = o.author;
from_string(com.permlink, o.permlink);
com.created = _db.head_block_time();
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 == STEEMIT_ROOT_POST_PARENT) {
com.parent_author = "";
from_string(com.parent_permlink, 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_PRE_HF17 :
fc::time_point_sec::maximum();
} else {
com.parent_author = parent->author;
com.parent_permlink = parent->permlink;
com.depth = parent->depth + 1;
com.root_comment = parent->root_comment;
com.cashout_time = fc::time_point_sec::maximum();
}
if (_db.has_hardfork(STEEMIT_HARDFORK_0_17__431)) {
com.cashout_time = com.created + STEEMIT_CASHOUT_WINDOW_SECONDS;
}
if (auth.referrer_account != account_name_type()) {
if (_db.head_block_time() < auth.referral_end_date) {
com.beneficiaries.push_back(beneficiary_route_type(auth.referrer_account,
auth.referrer_interest_rate));
} else {
referrer_to_delete = true;
}
}
});
if (referrer_to_delete) {
_db.modify(auth, [&](account_object& a) {
a.referrer_account = account_name_type();
a.referrer_interest_rate = 0;
a.referral_end_date = time_point_sec::min();
a.referral_break_fee.amount = 0;
});
}
while (parent) {
_db.modify(*parent, [&](comment_object& p) {
p.children++;
});
if (parent->parent_author != STEEMIT_ROOT_POST_PARENT) {
parent = &_db.get_comment(parent->parent_author, parent->parent_permlink);
} else
{
parent = nullptr;
}
}
} else {
// start edit case
const auto& comment = *itr;
_db.modify(comment, [&](comment_object& com) {
strcmp_equal equal;
GOLOS_CHECK_LOGIC(com.parent_author == (parent ? o.parent_author : account_name_type()),
logic_exception::parent_of_comment_cannot_change,
"The parent of a comment cannot change.");
GOLOS_CHECK_LOGIC(equal(com.parent_permlink, o.parent_permlink),
logic_exception::parent_perlink_of_comment_cannot_change,
"The parent permlink of a comment cannot change.");
});
} // end EDIT case
} FC_CAPTURE_AND_RETHROW((o))
}
void escrow_transfer_evaluator::do_apply(const escrow_transfer_operation& o) {
try {
const auto& from_account = _db.get_account(o.from);
_db.get_account(o.to);
_db.get_account(o.agent);
GOLOS_CHECK_LOGIC(o.ratification_deadline > _db.head_block_time(),
logic_exception::escrow_time_in_past,
"The escrow ratification deadline must be after head block time.");
GOLOS_CHECK_LOGIC(o.escrow_expiration > _db.head_block_time(),
logic_exception::escrow_time_in_past,
"The escrow expiration must be after head block time.");
asset steem_spent = o.steem_amount;
asset sbd_spent = o.sbd_amount;
if (o.fee.symbol == STEEM_SYMBOL) {
steem_spent += o.fee;
} else {
sbd_spent += o.fee;
}
GOLOS_CHECK_BALANCE(from_account, MAIN_BALANCE, steem_spent);
GOLOS_CHECK_BALANCE(from_account, MAIN_BALANCE, sbd_spent);
_db.adjust_balance(from_account, -steem_spent);
_db.adjust_balance(from_account, -sbd_spent);
_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.ratification_deadline = o.ratification_deadline;
esc.escrow_expiration = o.escrow_expiration;
esc.sbd_balance = o.sbd_amount;
esc.steem_balance = o.steem_amount;
esc.pending_fee = o.fee;
});
}
FC_CAPTURE_AND_RETHROW((o))
}
void escrow_approve_evaluator::do_apply(const escrow_approve_operation& o) {
try {
const auto& escrow = _db.get_escrow(o.from, o.escrow_id);
GOLOS_CHECK_LOGIC(escrow.to == o.to,
logic_exception::escrow_bad_to,
"Operation 'to' (${o}) does not match escrow 'to' (${e}).", ("o",o.to)("e",escrow.to));
GOLOS_CHECK_LOGIC(escrow.agent == o.agent,
logic_exception::escrow_bad_agent,
"Operation 'agent' (${a}) does not match escrow 'agent' (${e}).", ("o",o.agent)("e",escrow.agent));
GOLOS_CHECK_LOGIC(escrow.ratification_deadline >= _db.head_block_time(),
logic_exception::ratification_deadline_passed,
"The escrow ratification deadline has passed. Escrow can no longer be ratified.");
bool reject_escrow = !o.approve;
if (o.who == o.to) {
GOLOS_CHECK_LOGIC(!escrow.to_approved,
logic_exception::account_already_approved_escrow,
"Account 'to' (${t}) has already approved the escrow.", ("t",o.to));
if (!reject_escrow) {
_db.modify(escrow, [&](escrow_object& esc) {
esc.to_approved = true;
});
}
}
if (o.who == o.agent) {
GOLOS_CHECK_LOGIC(!escrow.agent_approved,
logic_exception::account_already_approved_escrow,
"Account 'agent' (${a}) has already approved the escrow.", ("a",o.agent));
if (!reject_escrow) {
_db.modify(escrow, [&](escrow_object& esc) {
esc.agent_approved = true;
});
}
}
if (reject_escrow) {
const auto &from_account = _db.get_account(o.from);
_db.adjust_balance(from_account, escrow.steem_balance);
_db.adjust_balance(from_account, escrow.sbd_balance);
_db.adjust_balance(from_account, escrow.pending_fee);
_db.remove(escrow);
} else if (escrow.to_approved && escrow.agent_approved) {
const auto &agent_account = _db.get_account(o.agent);
_db.adjust_balance(agent_account, escrow.pending_fee);
_db.modify(escrow, [&](escrow_object& esc) {
esc.pending_fee.amount = 0;
});
}
}
FC_CAPTURE_AND_RETHROW((o))
}
void escrow_dispute_evaluator::do_apply(const escrow_dispute_operation& o) {
try {
_db.get_account(o.from); // Verify from account exists
const auto& e = _db.get_escrow(o.from, o.escrow_id);
GOLOS_CHECK_LOGIC(_db.head_block_time() < e.escrow_expiration,
logic_exception::cannot_dispute_expired_escrow,
"Disputing the escrow must happen before expiration.");
GOLOS_CHECK_LOGIC(e.to_approved && e.agent_approved,
logic_exception::escrow_must_be_approved_first,
"The escrow must be approved by all parties before a dispute can be raised.");
GOLOS_CHECK_LOGIC(!e.disputed,
logic_exception::escrow_already_disputed,
"The escrow is already under dispute.");
GOLOS_CHECK_LOGIC(e.to == o.to,
logic_exception::escrow_bad_to,
"Operation 'to' (${o}) does not match escrow 'to' (${e}).", ("o",o.to)("e",e.to));
GOLOS_CHECK_LOGIC(e.agent == o.agent,
logic_exception::escrow_bad_agent,
"Operation 'agent' (${a}) does not match escrow 'agent' (${e}).", ("o",o.agent)("e",e.agent));