forked from steem-witnesses/steem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sps_evaluator.cpp
147 lines (112 loc) · 5.05 KB
/
sps_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
#include <steem/chain/steem_fwd.hpp>
#include <steem/protocol/sps_operations.hpp>
#include <steem/chain/database.hpp>
#include <steem/chain/steem_evaluator.hpp>
#include <steem/chain/sps_objects.hpp>
#include <steem/chain/util/sps_helper.hpp>
namespace steem { namespace chain {
using steem::chain::create_proposal_evaluator;
void create_proposal_evaluator::do_apply( const create_proposal_operation& o )
{
try
{
FC_ASSERT( _db.has_hardfork( STEEM_PROPOSALS_HARDFORK ), "Proposals functionality not enabled until hardfork ${hf}", ("hf", STEEM_PROPOSALS_HARDFORK) );
/** start date can be earlier than head_block_time - otherwise creating a proposal can be difficult,
since passed date should be adjusted by potential transaction execution delay (i.e. 3 sec
as a time for processed next block).
*/
FC_ASSERT(o.end_date > _db.head_block_time(), "Can't create inactive proposals...");
asset fee_sbd( STEEM_TREASURY_FEE, SBD_SYMBOL );
//treasury account must exist, also we need it later to change its balance
const auto& treasury_account =_db.get_account( STEEM_TREASURY_ACCOUNT );
const auto& owner_account = _db.get_account( o.creator );
const auto* receiver_account = _db.find_account( o.receiver );
/// Just to check the receiver account exists.
FC_ASSERT(receiver_account != nullptr, "Specified receiver account: ${r} must exist in the blockchain",
("r", o.receiver));
const auto* commentObject = _db.find_comment(o.creator, o.permlink);
if(commentObject == nullptr)
{
commentObject = _db.find_comment(o.receiver, o.permlink);
FC_ASSERT(commentObject != nullptr, "Proposal permlink must point to the article posted by creator or receiver");
}
_db.create< proposal_object >( [&]( proposal_object& proposal )
{
proposal.proposal_id = proposal.id;
proposal.creator = o.creator;
proposal.receiver = o.receiver;
proposal.start_date = o.start_date;
proposal.end_date = o.end_date;
proposal.daily_pay = o.daily_pay;
proposal.subject = o.subject.c_str();
proposal.permlink = o.permlink.c_str();
});
_db.adjust_balance( owner_account, -fee_sbd );
/// Fee shall be paid to the treasury
_db.adjust_balance(treasury_account, fee_sbd );
}
FC_CAPTURE_AND_RETHROW( (o) )
}
void update_proposal_votes_evaluator::do_apply( const update_proposal_votes_operation& o )
{
try
{
FC_ASSERT( _db.has_hardfork( STEEM_PROPOSALS_HARDFORK ), "Proposals functionality not enabled until hardfork ${hf}", ("hf", STEEM_PROPOSALS_HARDFORK) );
const auto& pidx = _db.get_index< proposal_index >().indices().get< by_proposal_id >();
const auto& pvidx = _db.get_index< proposal_vote_index >().indices().get< by_voter_proposal >();
for( const auto id : o.proposal_ids )
{
//checking if proposal id exists
auto found_id = pidx.find( id );
if( found_id == pidx.end() || found_id->removed )
continue;
auto found = pvidx.find( boost::make_tuple( o.voter, id ) );
if( o.approve )
{
if( found == pvidx.end() )
_db.create< proposal_vote_object >( [&]( proposal_vote_object& proposal_vote )
{
proposal_vote.voter = o.voter;
proposal_vote.proposal_id = id;
} );
}
else
{
if( found != pvidx.end() )
_db.remove( *found );
}
}
}
FC_CAPTURE_AND_RETHROW( (o) )
}
void remove_proposal_evaluator::do_apply(const remove_proposal_operation& op)
{
try
{
FC_ASSERT( _db.has_hardfork( STEEM_PROPOSALS_HARDFORK ), "Proposals functionality not enabled until hardfork ${hf}", ("hf", STEEM_PROPOSALS_HARDFORK) );
sps_helper::remove_proposals( _db, op.proposal_ids, op.proposal_owner );
/*
Because of performance removing proposals are restricted due to the `sps_remove_threshold` threshold.
Therefore all proposals are marked with flag `removed` and `end_date` is moved beyond 'head_time + STEEM_PROPOSAL_MAINTENANCE_CLEANUP`
flag `removed` - it's information for 'sps_api' plugin
moving `end_date` - triggers the algorithm in `sps_processor::remove_proposals`
When automatic actions will be introduced, this code will disappear.
*/
for( const auto id : op.proposal_ids )
{
const auto& pidx = _db.get_index< proposal_index >().indices().get< by_proposal_id >();
auto found_id = pidx.find( id );
if( found_id == pidx.end() || found_id->removed )
continue;
_db.modify( *found_id, [&]( proposal_object& proposal )
{
proposal.removed = true;
auto head_date = _db.head_block_time();
auto new_end_date = head_date - fc::seconds( STEEM_PROPOSAL_MAINTENANCE_CLEANUP );
proposal.end_date = new_end_date;
} );
}
}
FC_CAPTURE_AND_RETHROW( (op) )
}
} } // steem::chain