-
Notifications
You must be signed in to change notification settings - Fork 650
/
Copy pathproposal_evaluator.cpp
383 lines (332 loc) · 16.2 KB
/
proposal_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
/*
* Copyright (c) 2015-2018 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 <graphene/chain/database.hpp>
#include <graphene/chain/proposal_evaluator.hpp>
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/hardfork.hpp>
namespace graphene { namespace chain {
namespace detail {
void check_asset_options_hf_1268(const fc::time_point_sec& block_time, const asset_options& options);
void check_vesting_balance_policy_hf_1268(const fc::time_point_sec& block_time, const vesting_policy_initializer& policy);
}
struct proposal_operation_hardfork_visitor
{
typedef void result_type;
const database& db;
const fc::time_point_sec block_time;
const fc::time_point_sec next_maintenance_time;
proposal_operation_hardfork_visitor( const database& _db, const fc::time_point_sec bt )
: db( _db ), block_time(bt), next_maintenance_time( db.get_dynamic_global_properties().next_maintenance_time ) {}
template<typename T>
void operator()(const T &v) const {}
void operator()(const graphene::chain::call_order_update_operation &v) const {
// TODO If this never ASSERTs before HF 1465, it can be removed
FC_ASSERT( block_time < SOFTFORK_CORE_1465_TIME
|| block_time > HARDFORK_CORE_1465_TIME
|| v.delta_debt.asset_id == asset_id_type(113) // CNY
|| v.delta_debt.amount < 0
|| (v.delta_debt.asset_id( db ).bitasset_data_id
&& (*(v.delta_debt.asset_id( db ).bitasset_data_id))( db ).is_prediction_market )
, "Soft fork - preventing proposal with call_order_update!" );
// TODO review and cleanup code below after hard fork
// hf_834
if (next_maintenance_time <= HARDFORK_CORE_834_TIME) {
FC_ASSERT( !v.extensions.value.target_collateral_ratio.valid(),
"Can not set target_collateral_ratio in call_order_update_operation before hardfork 834." );
}
}
// hf_620
void operator()(const graphene::chain::asset_create_operation &v) const {
if (block_time < HARDFORK_CORE_620_TIME) {
static const std::locale &loc = std::locale::classic();
FC_ASSERT(isalpha(v.symbol.back(), loc), "Asset ${s} must end with alpha character before hardfork 620", ("s", v.symbol));
}
detail::check_asset_options_hf_1268(block_time, v.common_options);
}
// hf_1268
void operator()(const graphene::chain::asset_update_operation &v) const {
detail::check_asset_options_hf_1268(block_time, v.new_options);
}
// hf_1268
void operator()(const graphene::chain::vesting_balance_create_operation &v) const {
detail::check_vesting_balance_policy_hf_1268(block_time, v.policy);
}
// hf_199
void operator()(const graphene::chain::asset_update_issuer_operation &v) const {
if (block_time < HARDFORK_CORE_199_TIME) {
FC_ASSERT(false, "Not allowed until hardfork 199");
}
}
// hf_188
void operator()(const graphene::chain::asset_claim_pool_operation &v) const {
if (block_time < HARDFORK_CORE_188_TIME) {
FC_ASSERT(false, "Not allowed until hardfork 188");
}
}
// hf_588
// issue #588
//
// As a virtual operation which has no evaluator `asset_settle_cancel_operation`
// originally won't be packed into blocks, yet its loose `validate()` method
// make it able to slip into blocks.
//
// We need to forbid this operation being packed into blocks via proposal but
// this will lead to a hardfork (this operation in proposal will denied by new
// node while accept by old node), so a hardfork guard code needed and a
// consensus upgrade over all nodes needed in future. And because the
// `validate()` method not suitable to check database status, so we put the
// code here.
//
// After the hardfork, all nodes will deny packing this operation into a block,
// and then we will check whether exists a proposal containing this kind of
// operation, if not exists, we can harden the `validate()` method to deny
// it in a earlier stage.
//
void operator()(const graphene::chain::asset_settle_cancel_operation &v) const {
if (block_time > HARDFORK_CORE_588_TIME) {
FC_ASSERT(!"Virtual operation");
}
}
void operator()(const graphene::chain::committee_member_update_global_parameters_operation &op) const {
if (block_time < HARDFORK_CORE_1468_TIME) {
FC_ASSERT(!op.new_parameters.extensions.value.updatable_htlc_options.valid(), "Unable to set HTLC options before hardfork 1468");
FC_ASSERT(!op.new_parameters.current_fees->exists<htlc_create_operation>());
FC_ASSERT(!op.new_parameters.current_fees->exists<htlc_redeem_operation>());
FC_ASSERT(!op.new_parameters.current_fees->exists<htlc_extend_operation>());
}
}
void operator()(const graphene::chain::htlc_create_operation &op) const {
FC_ASSERT( block_time >= HARDFORK_CORE_1468_TIME, "Not allowed until hardfork 1468" );
}
void operator()(const graphene::chain::htlc_redeem_operation &op) const {
FC_ASSERT( block_time >= HARDFORK_CORE_1468_TIME, "Not allowed until hardfork 1468" );
}
void operator()(const graphene::chain::htlc_extend_operation &op) const {
FC_ASSERT( block_time >= HARDFORK_CORE_1468_TIME, "Not allowed until hardfork 1468" );
}
// loop and self visit in proposals
void operator()(const graphene::chain::proposal_create_operation &v) const {
bool already_contains_proposal_update = false;
for (const op_wrapper &op : v.proposed_ops)
{
op.op.visit(*this);
// Do not allow more than 1 proposal_update in a proposal
if ( op.op.which() == operation::tag<proposal_update_operation>().value )
{
FC_ASSERT( !already_contains_proposal_update, "At most one proposal update can be nested in a proposal!" );
already_contains_proposal_update = true;
}
}
}
};
struct hardfork_visitor_214 // non-recursive proposal visitor
{
typedef void result_type;
template<typename T>
void operator()(const T &v) const {}
void operator()(const proposal_update_operation &v) const {
FC_ASSERT(false, "Not allowed until hardfork 214");
}
};
void hardfork_visitor_1479::operator()(const proposal_update_operation &v)
{
if( nested_update_count == 0 || v.proposal.instance.value > max_update_instance )
max_update_instance = v.proposal.instance.value;
nested_update_count++;
}
void hardfork_visitor_1479::operator()(const proposal_delete_operation &v)
{
if( nested_update_count == 0 || v.proposal.instance.value > max_update_instance )
max_update_instance = v.proposal.instance.value;
nested_update_count++;
}
// loop and self visit in proposals
void hardfork_visitor_1479::operator()(const graphene::chain::proposal_create_operation &v)
{
for (const op_wrapper &op : v.proposed_ops)
op.op.visit(*this);
}
void_result proposal_create_evaluator::do_evaluate(const proposal_create_operation& o)
{ try {
const database& d = db();
// Calling the proposal hardfork visitor
const fc::time_point_sec block_time = d.head_block_time();
proposal_operation_hardfork_visitor vtor( d, block_time );
vtor( o );
if( block_time < HARDFORK_CORE_214_TIME )
{ // cannot be removed after hf, unfortunately
hardfork_visitor_214 hf214;
for (const op_wrapper &op : o.proposed_ops)
op.op.visit( hf214 );
}
vtor_1479( o );
const auto& global_parameters = d.get_global_properties().parameters;
FC_ASSERT( o.expiration_time > block_time, "Proposal has already expired on creation." );
FC_ASSERT( o.expiration_time <= block_time + global_parameters.maximum_proposal_lifetime,
"Proposal expiration time is too far in the future.");
FC_ASSERT( !o.review_period_seconds || fc::seconds(*o.review_period_seconds) < (o.expiration_time - block_time),
"Proposal review period must be less than its overall lifetime." );
{
// If we're dealing with the committee authority, make sure this transaction has a sufficient review period.
flat_set<account_id_type> auths;
vector<authority> other;
for( auto& op : o.proposed_ops )
{
operation_get_required_authorities(op.op, auths, auths, other);
}
FC_ASSERT( other.size() == 0 ); // TODO: what about other???
if( auths.find(GRAPHENE_COMMITTEE_ACCOUNT) != auths.end() )
{
GRAPHENE_ASSERT(
o.review_period_seconds.valid(),
proposal_create_review_period_required,
"Review period not given, but at least ${min} required",
("min", global_parameters.committee_proposal_review_period)
);
GRAPHENE_ASSERT(
*o.review_period_seconds >= global_parameters.committee_proposal_review_period,
proposal_create_review_period_insufficient,
"Review period of ${t} specified, but at least ${min} required",
("t", *o.review_period_seconds)
("min", global_parameters.committee_proposal_review_period)
);
}
}
for( const op_wrapper& op : o.proposed_ops )
_proposed_trx.operations.push_back(op.op);
_proposed_trx.validate();
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
object_id_type proposal_create_evaluator::do_apply(const proposal_create_operation& o)
{ try {
database& d = db();
const proposal_object& proposal = d.create<proposal_object>([&](proposal_object& proposal) {
_proposed_trx.expiration = o.expiration_time;
proposal.proposed_transaction = _proposed_trx;
proposal.expiration_time = o.expiration_time;
proposal.proposer = o.fee_paying_account;
if( o.review_period_seconds )
proposal.review_period_time = o.expiration_time - *o.review_period_seconds;
//Populate the required approval sets
flat_set<account_id_type> required_active;
vector<authority> other;
// TODO: consider caching values from evaluate?
for( auto& op : _proposed_trx.operations )
operation_get_required_authorities(op, required_active, proposal.required_owner_approvals, other);
//All accounts which must provide both owner and active authority should be omitted from the active authority set;
//owner authority approval implies active authority approval.
std::set_difference(required_active.begin(), required_active.end(),
proposal.required_owner_approvals.begin(), proposal.required_owner_approvals.end(),
std::inserter(proposal.required_active_approvals, proposal.required_active_approvals.begin()));
if( d.head_block_time() > HARDFORK_CORE_1479_TIME )
FC_ASSERT( vtor_1479.nested_update_count == 0 || proposal.id.instance() > vtor_1479.max_update_instance,
"Cannot update/delete a proposal with a future id!" );
else if( vtor_1479.nested_update_count > 0 && proposal.id.instance() <= vtor_1479.max_update_instance )
{
// prevent approval
transfer_operation top;
top.from = GRAPHENE_NULL_ACCOUNT;
top.to = GRAPHENE_RELAXED_COMMITTEE_ACCOUNT;
top.amount = asset( GRAPHENE_MAX_SHARE_SUPPLY );
proposal.proposed_transaction.operations.emplace_back( top );
wlog( "Issue 1479: ${p}", ("p",proposal) );
}
});
return proposal.id;
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result proposal_update_evaluator::do_evaluate(const proposal_update_operation& o)
{ try {
database& d = db();
_proposal = &o.proposal(d);
if( _proposal->review_period_time && d.head_block_time() >= *_proposal->review_period_time )
FC_ASSERT( o.active_approvals_to_add.empty() && o.owner_approvals_to_add.empty(),
"This proposal is in its review period. No new approvals may be added." );
for( account_id_type id : o.active_approvals_to_remove )
{
FC_ASSERT( _proposal->available_active_approvals.find(id) != _proposal->available_active_approvals.end(),
"", ("id", id)("available", _proposal->available_active_approvals) );
}
for( account_id_type id : o.owner_approvals_to_remove )
{
FC_ASSERT( _proposal->available_owner_approvals.find(id) != _proposal->available_owner_approvals.end(),
"", ("id", id)("available", _proposal->available_owner_approvals) );
}
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result proposal_update_evaluator::do_apply(const proposal_update_operation& o)
{ try {
database& d = db();
// Potential optimization: if _executed_proposal is true, we can skip the modify step and make push_proposal skip
// signature checks. This isn't done now because I just wrote all the proposals code, and I'm not yet 100% sure the
// required approvals are sufficient to authorize the transaction.
d.modify(*_proposal, [&o](proposal_object& p) {
p.available_active_approvals.insert(o.active_approvals_to_add.begin(), o.active_approvals_to_add.end());
p.available_owner_approvals.insert(o.owner_approvals_to_add.begin(), o.owner_approvals_to_add.end());
for( account_id_type id : o.active_approvals_to_remove )
p.available_active_approvals.erase(id);
for( account_id_type id : o.owner_approvals_to_remove )
p.available_owner_approvals.erase(id);
for( const auto& id : o.key_approvals_to_add )
p.available_key_approvals.insert(id);
for( const auto& id : o.key_approvals_to_remove )
p.available_key_approvals.erase(id);
});
// If the proposal has a review period, don't bother attempting to authorize/execute it.
// Proposals with a review period may never be executed except at their expiration.
if( _proposal->review_period_time )
return void_result();
if( _proposal->is_authorized_to_execute(d) )
{
// All required approvals are satisfied. Execute!
_executed_proposal = true;
try {
_processed_transaction = d.push_proposal(*_proposal);
} catch(fc::exception& e) {
d.modify(*_proposal, [&e](proposal_object& p) {
p.fail_reason = e.to_string(fc::log_level(fc::log_level::all));
});
wlog("Proposed transaction ${id} failed to apply once approved with exception:\n----\n${reason}\n----\nWill try again when it expires.",
("id", o.proposal)("reason", e.to_detail_string()));
_proposal_failed = true;
}
}
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result proposal_delete_evaluator::do_evaluate(const proposal_delete_operation& o)
{ try {
database& d = db();
_proposal = &o.proposal(d);
auto required_approvals = o.using_owner_authority? &_proposal->required_owner_approvals
: &_proposal->required_active_approvals;
FC_ASSERT( required_approvals->find(o.fee_paying_account) != required_approvals->end(),
"Provided authority is not authoritative for this proposal.",
("provided", o.fee_paying_account)("required", *required_approvals));
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result proposal_delete_evaluator::do_apply(const proposal_delete_operation& o)
{ try {
db().remove(*_proposal);
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
} } // graphene::chain