-
Notifications
You must be signed in to change notification settings - Fork 650
/
Copy pathasset_evaluator.cpp
974 lines (803 loc) · 39.6 KB
/
asset_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
/*
* 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/asset_evaluator.hpp>
#include <graphene/chain/asset_object.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/market_object.hpp>
#include <graphene/chain/database.hpp>
#include <graphene/chain/exceptions.hpp>
#include <graphene/chain/hardfork.hpp>
#include <graphene/chain/is_authorized_asset.hpp>
#include <functional>
namespace graphene { namespace chain {
namespace detail {
// TODO review and remove code below and links to it after hf_1774
void check_asset_options_hf_1774(const fc::time_point_sec& block_time, const asset_options& options)
{
if( block_time < HARDFORK_1774_TIME )
{
FC_ASSERT( !options.extensions.value.reward_percent.valid() ||
*options.extensions.value.reward_percent < GRAPHENE_100_PERCENT,
"Asset extension reward percent must be less than 100% till HARDFORK_1774_TIME!");
}
}
// TODO review and remove code below and links to it after HARDFORK_BSIP_81_TIME
void check_asset_options_hf_bsip81(const fc::time_point_sec& block_time, const asset_options& options)
{
if (block_time < HARDFORK_BSIP_81_TIME) {
// Taker fees should not be set until activation of BSIP81
FC_ASSERT(!options.extensions.value.taker_fee_percent.valid(),
"Taker fee percent should not be defined before HARDFORK_BSIP_81_TIME");
}
}
}
void_result asset_create_evaluator::do_evaluate( const asset_create_operation& op )
{ try {
const database& d = db();
const auto& chain_parameters = d.get_global_properties().parameters;
FC_ASSERT( op.common_options.whitelist_authorities.size() <= chain_parameters.maximum_asset_whitelist_authorities );
FC_ASSERT( op.common_options.blacklist_authorities.size() <= chain_parameters.maximum_asset_whitelist_authorities );
detail::check_asset_options_hf_1774(d.head_block_time(), op.common_options);
// Check that all authorities do exist
for( auto id : op.common_options.whitelist_authorities )
d.get_object(id);
for( auto id : op.common_options.blacklist_authorities )
d.get_object(id);
auto& asset_indx = d.get_index_type<asset_index>().indices().get<by_symbol>();
auto asset_symbol_itr = asset_indx.find( op.symbol );
FC_ASSERT( asset_symbol_itr == asset_indx.end() );
// Define now from the current block time
const time_point_sec now = d.head_block_time();
// This must remain due to "BOND.CNY" being allowed before this HF
if( now > HARDFORK_385_TIME )
{
auto dotpos = op.symbol.rfind( '.' );
if( dotpos != std::string::npos )
{
auto prefix = op.symbol.substr( 0, dotpos );
auto asset_symbol_itr = asset_indx.find( prefix );
FC_ASSERT( asset_symbol_itr != asset_indx.end(),
"Asset ${s} may only be created by issuer of asset ${p}, but asset ${p} has not been created",
("s",op.symbol)("p",prefix) );
FC_ASSERT( asset_symbol_itr->issuer == op.issuer, "Asset ${s} may only be created by issuer of ${p}, ${i}",
("s",op.symbol)("p",prefix)("i", op.issuer(d).name) );
}
}
if( op.bitasset_opts )
{
const asset_object& backing = op.bitasset_opts->short_backing_asset(d);
if( backing.is_market_issued() )
{
const asset_bitasset_data_object& backing_bitasset_data = backing.bitasset_data(d);
const asset_object& backing_backing = backing_bitasset_data.options.short_backing_asset(d);
FC_ASSERT( !backing_backing.is_market_issued(),
"May not create a bitasset backed by a bitasset backed by a bitasset." );
FC_ASSERT( op.issuer != GRAPHENE_COMMITTEE_ACCOUNT || backing_backing.get_id() == asset_id_type(),
"May not create a blockchain-controlled market asset which is not backed by CORE.");
} else
FC_ASSERT( op.issuer != GRAPHENE_COMMITTEE_ACCOUNT || backing.get_id() == asset_id_type(),
"May not create a blockchain-controlled market asset which is not backed by CORE.");
FC_ASSERT( op.bitasset_opts->feed_lifetime_sec > chain_parameters.block_interval &&
op.bitasset_opts->force_settlement_delay_sec > chain_parameters.block_interval );
}
if( op.is_prediction_market )
{
FC_ASSERT( op.bitasset_opts );
FC_ASSERT( op.precision == op.bitasset_opts->short_backing_asset(d).precision );
}
// Check the taker fee percent
detail::check_asset_options_hf_bsip81(now, op.common_options);
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
void asset_create_evaluator::pay_fee()
{
fee_is_odd = core_fee_paid.value & 1;
core_fee_paid -= core_fee_paid.value/2;
generic_evaluator::pay_fee();
}
object_id_type asset_create_evaluator::do_apply( const asset_create_operation& op )
{ try {
database& d = db();
bool hf_429 = fee_is_odd && d.head_block_time() > HARDFORK_CORE_429_TIME;
const asset_dynamic_data_object& dyn_asset =
d.create<asset_dynamic_data_object>( [hf_429,this]( asset_dynamic_data_object& a ) {
a.current_supply = 0;
a.fee_pool = core_fee_paid - (hf_429 ? 1 : 0);
});
if( fee_is_odd && !hf_429 )
{
d.modify( d.get_core_dynamic_data(), []( asset_dynamic_data_object& dd ) {
dd.current_supply++;
});
}
asset_bitasset_data_id_type bit_asset_id;
auto next_asset_id = d.get_index_type<asset_index>().get_next_id();
if( op.bitasset_opts.valid() )
bit_asset_id = d.create<asset_bitasset_data_object>( [&op,next_asset_id]( asset_bitasset_data_object& a ) {
a.options = *op.bitasset_opts;
a.is_prediction_market = op.is_prediction_market;
a.asset_id = next_asset_id;
}).id;
const asset_object& new_asset =
d.create<asset_object>( [&op,next_asset_id,&dyn_asset,bit_asset_id]( asset_object& a ) {
a.issuer = op.issuer;
a.symbol = op.symbol;
a.precision = op.precision;
a.options = op.common_options;
if( a.options.core_exchange_rate.base.asset_id.instance.value == 0 )
a.options.core_exchange_rate.quote.asset_id = next_asset_id;
else
a.options.core_exchange_rate.base.asset_id = next_asset_id;
a.dynamic_asset_data_id = dyn_asset.id;
if( op.bitasset_opts.valid() )
a.bitasset_data_id = bit_asset_id;
});
FC_ASSERT( new_asset.id == next_asset_id, "Unexpected object database error, object id mismatch" );
return new_asset.id;
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result asset_issue_evaluator::do_evaluate( const asset_issue_operation& o )
{ try {
const database& d = db();
const asset_object& a = o.asset_to_issue.asset_id(d);
FC_ASSERT( o.issuer == a.issuer );
FC_ASSERT( !a.is_market_issued(), "Cannot manually issue a market-issued asset." );
to_account = &o.issue_to_account(d);
FC_ASSERT( is_authorized_asset( d, *to_account, a ) );
asset_dyn_data = &a.dynamic_asset_data_id(d);
FC_ASSERT( (asset_dyn_data->current_supply + o.asset_to_issue.amount) <= a.options.max_supply );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_issue_evaluator::do_apply( const asset_issue_operation& o )
{ try {
db().adjust_balance( o.issue_to_account, o.asset_to_issue );
db().modify( *asset_dyn_data, [&o]( asset_dynamic_data_object& data ){
data.current_supply += o.asset_to_issue.amount;
});
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_reserve_evaluator::do_evaluate( const asset_reserve_operation& o )
{ try {
const database& d = db();
const asset_object& a = o.amount_to_reserve.asset_id(d);
GRAPHENE_ASSERT(
!a.is_market_issued(),
asset_reserve_invalid_on_mia,
"Cannot reserve ${sym} because it is a market-issued asset",
("sym", a.symbol)
);
from_account = &o.payer(d);
FC_ASSERT( is_authorized_asset( d, *from_account, a ) );
asset_dyn_data = &a.dynamic_asset_data_id(d);
FC_ASSERT( (asset_dyn_data->current_supply - o.amount_to_reserve.amount) >= 0 );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_reserve_evaluator::do_apply( const asset_reserve_operation& o )
{ try {
db().adjust_balance( o.payer, -o.amount_to_reserve );
db().modify( *asset_dyn_data, [&o]( asset_dynamic_data_object& data ){
data.current_supply -= o.amount_to_reserve.amount;
});
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_fund_fee_pool_evaluator::do_evaluate(const asset_fund_fee_pool_operation& o)
{ try {
database& d = db();
const asset_object& a = o.asset_id(d);
asset_dyn_data = &a.dynamic_asset_data_id(d);
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_fund_fee_pool_evaluator::do_apply(const asset_fund_fee_pool_operation& o)
{ try {
db().adjust_balance(o.from_account, -o.amount);
db().modify( *asset_dyn_data, [&o]( asset_dynamic_data_object& data ) {
data.fee_pool += o.amount;
});
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
static void validate_new_issuer( const database& d, const asset_object& a, account_id_type new_issuer )
{ try {
FC_ASSERT(d.find_object(new_issuer));
if( a.is_market_issued() && new_issuer == GRAPHENE_COMMITTEE_ACCOUNT )
{
const asset_object& backing = a.bitasset_data(d).options.short_backing_asset(d);
if( backing.is_market_issued() )
{
const asset_object& backing_backing = backing.bitasset_data(d).options.short_backing_asset(d);
FC_ASSERT( backing_backing.get_id() == asset_id_type(),
"May not create a blockchain-controlled market asset which is not backed by CORE.");
} else
FC_ASSERT( backing.get_id() == asset_id_type(),
"May not create a blockchain-controlled market asset which is not backed by CORE.");
}
} FC_CAPTURE_AND_RETHROW( (a)(new_issuer) ) }
void_result asset_update_evaluator::do_evaluate(const asset_update_operation& o)
{ try {
const database& d = db();
const time_point_sec now = d.head_block_time();
const asset_object& a = o.asset_to_update(d);
auto a_copy = a;
a_copy.options = o.new_options;
a_copy.validate();
if( o.new_issuer )
{
FC_ASSERT( now < HARDFORK_CORE_199_TIME,
"Since Hardfork #199, updating issuer requires the use of asset_update_issuer_operation.");
validate_new_issuer( d, a, *o.new_issuer );
}
detail::check_asset_options_hf_1774(d.head_block_time(), o.new_options);
if( a.dynamic_asset_data_id(d).current_supply != 0 )
{
// new issuer_permissions must be subset of old issuer permissions
FC_ASSERT(!(o.new_options.issuer_permissions & ~a.options.issuer_permissions),
"Cannot reinstate previously revoked issuer permissions on an asset.");
}
// changed flags must be subset of old issuer permissions
FC_ASSERT(!((o.new_options.flags ^ a.options.flags) & ~a.options.issuer_permissions),
"Flag change is forbidden by issuer permissions");
asset_to_update = &a;
FC_ASSERT( o.issuer == a.issuer,
"Incorrect issuer for asset! (${o.issuer} != ${a.issuer})",
("o.issuer", o.issuer)("a.issuer", a.issuer) );
const auto& chain_parameters = d.get_global_properties().parameters;
FC_ASSERT( o.new_options.whitelist_authorities.size() <= chain_parameters.maximum_asset_whitelist_authorities );
for( auto id : o.new_options.whitelist_authorities )
d.get_object(id);
FC_ASSERT( o.new_options.blacklist_authorities.size() <= chain_parameters.maximum_asset_whitelist_authorities );
for( auto id : o.new_options.blacklist_authorities )
d.get_object(id);
// Check the taker fee percent
detail::check_asset_options_hf_bsip81(now, o.new_options);
return void_result();
} FC_CAPTURE_AND_RETHROW((o)) }
void_result asset_update_evaluator::do_apply(const asset_update_operation& o)
{ try {
database& d = db();
// If we are now disabling force settlements, cancel all open force settlement orders
if( (o.new_options.flags & disable_force_settle) && asset_to_update->can_force_settle() )
{
const auto& idx = d.get_index_type<force_settlement_index>().indices().get<by_expiration>();
// Funky iteration code because we're removing objects as we go. We have to re-initialize itr every loop instead
// of simply incrementing it.
for( auto itr = idx.lower_bound(o.asset_to_update);
itr != idx.end() && itr->settlement_asset_id() == o.asset_to_update;
itr = idx.lower_bound(o.asset_to_update) )
d.cancel_settle_order(*itr);
}
// For market-issued assets, if core change rate changed, update flag in bitasset data
if( asset_to_update->is_market_issued()
&& asset_to_update->options.core_exchange_rate != o.new_options.core_exchange_rate )
{
const auto& bitasset = asset_to_update->bitasset_data(d);
if( !bitasset.asset_cer_updated )
{
d.modify( bitasset, [](asset_bitasset_data_object& b)
{
b.asset_cer_updated = true;
});
}
}
d.modify(*asset_to_update, [&o](asset_object& a) {
if( o.new_issuer )
a.issuer = *o.new_issuer;
a.options = o.new_options;
});
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_update_issuer_evaluator::do_evaluate(const asset_update_issuer_operation& o)
{ try {
database& d = db();
const asset_object& a = o.asset_to_update(d);
validate_new_issuer( d, a, o.new_issuer );
asset_to_update = &a;
FC_ASSERT( o.issuer == a.issuer,
"Incorrect issuer for asset! (${o.issuer} != ${a.issuer})",
("o.issuer", o.issuer)("a.issuer", a.issuer) );
return void_result();
} FC_CAPTURE_AND_RETHROW((o)) }
void_result asset_update_issuer_evaluator::do_apply(const asset_update_issuer_operation& o)
{ try {
database& d = db();
d.modify(*asset_to_update, [&](asset_object& a) {
a.issuer = o.new_issuer;
});
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
/****************
* Loop through assets, looking for ones that are backed by the asset being changed. When found,
* perform checks to verify validity
*
* @param d the database
* @param op the bitasset update operation being performed
* @param new_backing_asset
* @param true if after hf 922/931 (if nothing triggers, this and the logic that depends on it
* should be removed).
*/
void check_children_of_bitasset(database& d, const asset_update_bitasset_operation& op,
const asset_object& new_backing_asset)
{
// no need to do these checks if the new backing asset is CORE
if ( new_backing_asset.get_id() == asset_id_type() )
return;
// loop through all assets that have this asset as a backing asset
const auto& idx = d.get_index_type<graphene::chain::asset_bitasset_data_index>()
.indices()
.get<by_short_backing_asset>();
auto backed_range = idx.equal_range(op.asset_to_update);
std::for_each( backed_range.first, backed_range.second,
[&new_backing_asset, &d, &op](const asset_bitasset_data_object& bitasset_data)
{
const auto& child = bitasset_data.asset_id(d);
FC_ASSERT( child.get_id() != op.new_options.short_backing_asset,
"A BitAsset would be invalidated by changing this backing asset ('A' backed by 'B' backed by 'A')." );
FC_ASSERT( child.issuer != GRAPHENE_COMMITTEE_ACCOUNT,
"A blockchain-controlled market asset would be invalidated by changing this backing asset." );
FC_ASSERT( !new_backing_asset.is_market_issued(),
"A non-blockchain controlled BitAsset would be invalidated by changing this backing asset.");
} ); // end of lambda and std::for_each()
} // check_children_of_bitasset
void_result asset_update_bitasset_evaluator::do_evaluate(const asset_update_bitasset_operation& op)
{ try {
database& d = db();
const asset_object& asset_obj = op.asset_to_update(d);
FC_ASSERT( asset_obj.is_market_issued(), "Cannot update BitAsset-specific settings on a non-BitAsset." );
FC_ASSERT( op.issuer == asset_obj.issuer, "Only asset issuer can update bitasset_data of the asset." );
const asset_bitasset_data_object& current_bitasset_data = asset_obj.bitasset_data(d);
FC_ASSERT( !current_bitasset_data.has_settlement(), "Cannot update a bitasset after a global settlement has executed" );
// hf 922_931 is a consensus/logic change. This hf cannot be removed.
bool after_hf_core_922_931 = ( d.get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_922_931_TIME );
// Are we changing the backing asset?
if( op.new_options.short_backing_asset != current_bitasset_data.options.short_backing_asset )
{
FC_ASSERT( asset_obj.dynamic_asset_data_id(d).current_supply == 0,
"Cannot update a bitasset if there is already a current supply." );
const asset_object& new_backing_asset = op.new_options.short_backing_asset(d); // check if the asset exists
if( after_hf_core_922_931 )
{
FC_ASSERT( op.new_options.short_backing_asset != asset_obj.get_id(),
"Cannot update an asset to be backed by itself." );
if( current_bitasset_data.is_prediction_market )
{
FC_ASSERT( asset_obj.precision == new_backing_asset.precision,
"The precision of the asset and backing asset must be equal." );
}
if( asset_obj.issuer == GRAPHENE_COMMITTEE_ACCOUNT )
{
if( new_backing_asset.is_market_issued() )
{
FC_ASSERT( new_backing_asset.bitasset_data(d).options.short_backing_asset == asset_id_type(),
"May not modify a blockchain-controlled market asset to be backed by an asset which is not "
"backed by CORE." );
check_children_of_bitasset( d, op, new_backing_asset );
}
else
{
FC_ASSERT( new_backing_asset.get_id() == asset_id_type(),
"May not modify a blockchain-controlled market asset to be backed by an asset which is not "
"market issued asset nor CORE." );
}
}
else
{
// not a committee issued asset
// If we're changing to a backing_asset that is not CORE, we need to look at any
// asset ( "CHILD" ) that has this one as a backing asset. If CHILD is committee-owned,
// the change is not allowed. If CHILD is user-owned, then this asset's backing
// asset must be either CORE or a UIA.
if ( new_backing_asset.get_id() != asset_id_type() ) // not backed by CORE
{
check_children_of_bitasset( d, op, new_backing_asset );
}
}
// Check if the new backing asset is itself backed by something. It must be CORE or a UIA
if ( new_backing_asset.is_market_issued() )
{
asset_id_type backing_backing_asset_id = new_backing_asset.bitasset_data(d).options.short_backing_asset;
FC_ASSERT( (backing_backing_asset_id == asset_id_type() || !backing_backing_asset_id(d).is_market_issued()),
"A BitAsset cannot be backed by a BitAsset that itself is backed by a BitAsset.");
}
}
}
const auto& chain_parameters = d.get_global_properties().parameters;
if( after_hf_core_922_931 )
{
FC_ASSERT( op.new_options.feed_lifetime_sec > chain_parameters.block_interval,
"Feed lifetime must exceed block interval." );
FC_ASSERT( op.new_options.force_settlement_delay_sec > chain_parameters.block_interval,
"Force settlement delay must exceed block interval." );
}
bitasset_to_update = ¤t_bitasset_data;
asset_to_update = &asset_obj;
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
/*******
* @brief Apply requested changes to bitasset options
*
* This applies the requested changes to the bitasset object. It also cleans up the
* releated feeds
*
* @param op the requested operation
* @param db the database
* @param bdo the actual database object
* @param asset_to_update the asset_object related to this bitasset_data_object
* @returns true if the feed price is changed, and after hf core-868-890
*/
static bool update_bitasset_object_options(
const asset_update_bitasset_operation& op, database& db,
asset_bitasset_data_object& bdo, const asset_object& asset_to_update )
{
const fc::time_point_sec next_maint_time = db.get_dynamic_global_properties().next_maintenance_time;
bool after_hf_core_868_890 = ( next_maint_time > HARDFORK_CORE_868_890_TIME );
// If the minimum number of feeds to calculate a median has changed, we need to recalculate the median
bool should_update_feeds = false;
if( op.new_options.minimum_feeds != bdo.options.minimum_feeds )
should_update_feeds = true;
// after hardfork core-868-890, we also should call update_median_feeds if the feed_lifetime_sec changed
if( after_hf_core_868_890
&& op.new_options.feed_lifetime_sec != bdo.options.feed_lifetime_sec )
{
should_update_feeds = true;
}
// feeds must be reset if the backing asset is changed after hardfork core-868-890
bool backing_asset_changed = false;
bool is_witness_or_committee_fed = false;
if( after_hf_core_868_890
&& op.new_options.short_backing_asset != bdo.options.short_backing_asset )
{
backing_asset_changed = true;
should_update_feeds = true;
if( asset_to_update.options.flags & ( witness_fed_asset | committee_fed_asset ) )
is_witness_or_committee_fed = true;
}
bdo.options = op.new_options;
// are we modifying the underlying? If so, reset the feeds
if( backing_asset_changed )
{
if( is_witness_or_committee_fed )
{
bdo.feeds.clear();
}
else
{
// for non-witness-feeding and non-committee-feeding assets, modify all feeds
// published by producers to nothing, since we can't simply remove them. For more information:
// https://github.com/bitshares/bitshares-core/pull/832#issuecomment-384112633
for( auto& current_feed : bdo.feeds )
{
current_feed.second.second.settlement_price = price();
}
}
}
if( should_update_feeds )
{
const auto old_feed = bdo.current_feed;
bdo.update_median_feeds( db.head_block_time(), next_maint_time );
// We need to call check_call_orders if the settlement price changes after hardfork core-868-890
return ( after_hf_core_868_890 && ! (old_feed == bdo.current_feed) );
}
return false;
}
void_result asset_update_bitasset_evaluator::do_apply(const asset_update_bitasset_operation& op)
{
try
{
auto& db_conn = db();
const auto& asset_being_updated = (*asset_to_update);
bool to_check_call_orders = false;
db_conn.modify( *bitasset_to_update,
[&op, &asset_being_updated, &to_check_call_orders, &db_conn]( asset_bitasset_data_object& bdo )
{
to_check_call_orders = update_bitasset_object_options( op, db_conn, bdo, asset_being_updated );
});
if( to_check_call_orders )
// Process margin calls, allow black swan, not for a new limit order
db_conn.check_call_orders( asset_being_updated, true, false, bitasset_to_update );
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) )
}
void_result asset_update_feed_producers_evaluator::do_evaluate(const asset_update_feed_producers_evaluator::operation_type& o)
{ try {
database& d = db();
FC_ASSERT( o.new_feed_producers.size() <= d.get_global_properties().parameters.maximum_asset_feed_publishers,
"Cannot specify more feed producers than maximum allowed" );
const asset_object& a = o.asset_to_update(d);
FC_ASSERT(a.is_market_issued(), "Cannot update feed producers on a non-BitAsset.");
FC_ASSERT(!(a.options.flags & committee_fed_asset), "Cannot set feed producers on a committee-fed asset.");
FC_ASSERT(!(a.options.flags & witness_fed_asset), "Cannot set feed producers on a witness-fed asset.");
FC_ASSERT( a.issuer == o.issuer, "Only asset issuer can update feed producers of an asset" );
asset_to_update = &a;
// Make sure all producers exist. Check these after asset because account lookup is more expensive
for( auto id : o.new_feed_producers )
d.get_object(id);
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_update_feed_producers_evaluator::do_apply(const asset_update_feed_producers_evaluator::operation_type& o)
{ try {
database& d = db();
const auto head_time = d.head_block_time();
const auto next_maint_time = d.get_dynamic_global_properties().next_maintenance_time;
const asset_bitasset_data_object& bitasset_to_update = asset_to_update->bitasset_data(d);
d.modify( bitasset_to_update, [&o,head_time,next_maint_time](asset_bitasset_data_object& a) {
//This is tricky because I have a set of publishers coming in, but a map of publisher to feed is stored.
//I need to update the map such that the keys match the new publishers, but not munge the old price feeds from
//publishers who are being kept.
// TODO possible performance optimization:
// Since both the map and the set are ordered by account already, we can iterate through them only once
// and avoid lookups while iterating by maintaining two iterators at same time.
// However, this operation is not used much, and both the set and the map are small,
// so likely we won't gain much with the optimization.
//First, remove any old publishers who are no longer publishers
for( auto itr = a.feeds.begin(); itr != a.feeds.end(); )
{
if( !o.new_feed_producers.count(itr->first) )
itr = a.feeds.erase(itr);
else
++itr;
}
//Now, add any new publishers
for( const account_id_type acc : o.new_feed_producers )
{
a.feeds[acc];
}
a.update_median_feeds( head_time, next_maint_time );
});
// Process margin calls, allow black swan, not for a new limit order
d.check_call_orders( *asset_to_update, true, false, &bitasset_to_update );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_global_settle_evaluator::do_evaluate(const asset_global_settle_evaluator::operation_type& op)
{ try {
const database& d = db();
asset_to_settle = &op.asset_to_settle(d);
FC_ASSERT( asset_to_settle->is_market_issued(), "Can only globally settle market-issued assets" );
FC_ASSERT( asset_to_settle->can_global_settle(), "The global_settle permission of this asset is disabled" );
FC_ASSERT( asset_to_settle->issuer == op.issuer, "Only asset issuer can globally settle an asset" );
FC_ASSERT( asset_to_settle->dynamic_data(d).current_supply > 0, "Can not globally settle an asset with zero supply" );
const asset_bitasset_data_object& _bitasset_data = asset_to_settle->bitasset_data(d);
// if there is a settlement for this asset, then no further global settle may be taken
FC_ASSERT( !_bitasset_data.has_settlement(), "This asset has settlement, cannot global settle again" );
const auto& idx = d.get_index_type<call_order_index>().indices().get<by_collateral>();
FC_ASSERT( !idx.empty(), "Internal error: no debt position found" );
auto itr = idx.lower_bound( price::min( _bitasset_data.options.short_backing_asset, op.asset_to_settle ) );
FC_ASSERT( itr != idx.end() && itr->debt_type() == op.asset_to_settle, "Internal error: no debt position found" );
const call_order_object& least_collateralized_short = *itr;
FC_ASSERT(least_collateralized_short.get_debt() * op.settle_price <= least_collateralized_short.get_collateral(),
"Cannot force settle at supplied price: least collateralized short lacks sufficient collateral to settle.");
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result asset_global_settle_evaluator::do_apply(const asset_global_settle_evaluator::operation_type& op)
{ try {
database& d = db();
d.globally_settle_asset( *asset_to_settle, op.settle_price );
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result asset_settle_evaluator::do_evaluate(const asset_settle_evaluator::operation_type& op)
{ try {
const database& d = db();
asset_to_settle = &op.amount.asset_id(d);
FC_ASSERT(asset_to_settle->is_market_issued());
const auto& bitasset = asset_to_settle->bitasset_data(d);
FC_ASSERT(asset_to_settle->can_force_settle() || bitasset.has_settlement() );
if( bitasset.is_prediction_market )
FC_ASSERT( bitasset.has_settlement(), "global settlement must occur before force settling a prediction market" );
else if( bitasset.current_feed.settlement_price.is_null()
&& ( d.head_block_time() <= HARDFORK_CORE_216_TIME // TODO check whether the HF check can be removed
|| !bitasset.has_settlement() ) )
FC_THROW_EXCEPTION(insufficient_feeds, "Cannot force settle with no price feed.");
FC_ASSERT( d.get_balance( op.account, op.amount.asset_id ) >= op.amount, "Insufficient balance" );
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
operation_result asset_settle_evaluator::do_apply(const asset_settle_evaluator::operation_type& op)
{ try {
database& d = db();
const auto& bitasset = asset_to_settle->bitasset_data(d);
if( bitasset.has_settlement() )
{
const auto& mia_dyn = asset_to_settle->dynamic_asset_data_id(d);
auto settled_amount = op.amount * bitasset.settlement_price; // round down, in favor of global settlement fund
if( op.amount.amount == mia_dyn.current_supply )
settled_amount.amount = bitasset.settlement_fund; // avoid rounding problems
else
FC_ASSERT( settled_amount.amount <= bitasset.settlement_fund ); // should be strictly < except for PM with zero outcome
if( settled_amount.amount == 0 && !bitasset.is_prediction_market )
{
if( d.get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_184_TIME )
FC_THROW( "Settle amount is too small to receive anything due to rounding" );
// else do nothing. Before the hf, something for nothing issue (#184, variant F) could occur
}
asset pays = op.amount;
if( op.amount.amount != mia_dyn.current_supply
&& settled_amount.amount != 0
&& d.get_dynamic_global_properties().next_maintenance_time > HARDFORK_CORE_342_TIME )
{
pays = settled_amount.multiply_and_round_up( bitasset.settlement_price );
}
d.adjust_balance( op.account, -pays );
if( settled_amount.amount > 0 )
{
d.modify( bitasset, [&]( asset_bitasset_data_object& obj ){
obj.settlement_fund -= settled_amount.amount;
});
// The account who settles pays market fees to the issuer of the collateral asset after HF core-1780
//
// TODO Check whether the HF check can be removed after the HF.
// Note: even if logically it can be removed, perhaps the removal will lead to a small
// performance loss. Needs testing.
if( d.head_block_time() >= HARDFORK_CORE_1780_TIME )
{
const bool is_maker = false; // Settlement orders are takers
auto issuer_fees = d.pay_market_fees( fee_paying_account, settled_amount.asset_id(d), settled_amount , is_maker );
settled_amount -= issuer_fees;
}
if( settled_amount.amount > 0 )
d.adjust_balance( op.account, settled_amount );
}
d.modify( mia_dyn, [&]( asset_dynamic_data_object& obj ){
obj.current_supply -= pays.amount;
});
return settled_amount;
}
else
{
d.adjust_balance( op.account, -op.amount );
return d.create<force_settlement_object>([&](force_settlement_object& s) {
s.owner = op.account;
s.balance = op.amount;
s.settlement_date = d.head_block_time() + asset_to_settle->bitasset_data(d).options.force_settlement_delay_sec;
}).id;
}
} FC_CAPTURE_AND_RETHROW( (op) ) }
void_result asset_publish_feeds_evaluator::do_evaluate(const asset_publish_feed_operation& o)
{ try {
database& d = db();
const asset_object& base = o.asset_id(d);
//Verify that this feed is for a market-issued asset and that asset is backed by the base
FC_ASSERT( base.is_market_issued(), "Can only publish price feeds for market-issued assets" );
const asset_bitasset_data_object& bitasset = base.bitasset_data(d);
if( bitasset.is_prediction_market || d.head_block_time() <= HARDFORK_CORE_216_TIME )
{
FC_ASSERT( !bitasset.has_settlement(), "No further feeds may be published after a settlement event" );
}
// the settlement price must be quoted in terms of the backing asset
FC_ASSERT( o.feed.settlement_price.quote.asset_id == bitasset.options.short_backing_asset,
"Quote asset type in settlement price should be same as backing asset of this asset" );
if( d.head_block_time() > HARDFORK_480_TIME )
{
if( !o.feed.core_exchange_rate.is_null() )
{
FC_ASSERT( o.feed.core_exchange_rate.quote.asset_id == asset_id_type(),
"Quote asset in core exchange rate should be CORE asset" );
}
}
else
{
if( (!o.feed.settlement_price.is_null()) && (!o.feed.core_exchange_rate.is_null()) )
{
// Old buggy code, but we have to live with it
FC_ASSERT( o.feed.settlement_price.quote.asset_id == o.feed.core_exchange_rate.quote.asset_id, "Bad feed" );
}
}
//Verify that the publisher is authoritative to publish a feed
if( base.options.flags & witness_fed_asset )
{
FC_ASSERT( d.get(GRAPHENE_WITNESS_ACCOUNT).active.account_auths.count(o.publisher),
"Only active witnesses are allowed to publish price feeds for this asset" );
}
else if( base.options.flags & committee_fed_asset )
{
FC_ASSERT( d.get(GRAPHENE_COMMITTEE_ACCOUNT).active.account_auths.count(o.publisher),
"Only active committee members are allowed to publish price feeds for this asset" );
}
else
{
FC_ASSERT( bitasset.feeds.count(o.publisher),
"The account is not in the set of allowed price feed producers of this asset" );
}
asset_ptr = &base;
bitasset_ptr = &bitasset;
return void_result();
} FC_CAPTURE_AND_RETHROW((o)) }
void_result asset_publish_feeds_evaluator::do_apply(const asset_publish_feed_operation& o)
{ try {
database& d = db();
const auto head_time = d.head_block_time();
const auto next_maint_time = d.get_dynamic_global_properties().next_maintenance_time;
const asset_object& base = *asset_ptr;
const asset_bitasset_data_object& bad = *bitasset_ptr;
auto old_feed = bad.current_feed;
// Store medians for this asset
d.modify( bad , [&o,head_time,next_maint_time](asset_bitasset_data_object& a) {
a.feeds[o.publisher] = make_pair( head_time, o.feed );
a.update_median_feeds( head_time, next_maint_time );
});
if( !(old_feed == bad.current_feed) )
{
// Check whether need to revive the asset and proceed if need
if( bad.has_settlement() // has globally settled, implies head_block_time > HARDFORK_CORE_216_TIME
&& !bad.current_feed.settlement_price.is_null() ) // has a valid feed
{
bool should_revive = false;
const auto& mia_dyn = base.dynamic_asset_data_id(d);
if( mia_dyn.current_supply == 0 ) // if current supply is zero, revive the asset
should_revive = true;
else // if current supply is not zero, when collateral ratio of settlement fund is greater than MCR, revive the asset
{
if( next_maint_time <= HARDFORK_CORE_1270_TIME )
{
// before core-1270 hard fork, calculate call_price and compare to median feed
if( ~price::call_price( asset(mia_dyn.current_supply, o.asset_id),
asset(bad.settlement_fund, bad.options.short_backing_asset),
bad.current_feed.maintenance_collateral_ratio ) < bad.current_feed.settlement_price )
should_revive = true;
}
else
{
// after core-1270 hard fork, calculate collateralization and compare to maintenance_collateralization
if( price( asset( bad.settlement_fund, bad.options.short_backing_asset ),
asset( mia_dyn.current_supply, o.asset_id ) ) > bad.current_maintenance_collateralization )
should_revive = true;
}
}
if( should_revive )
d.revive_bitasset(base);
}
// Process margin calls, allow black swan, not for a new limit order
d.check_call_orders( base, true, false, bitasset_ptr );
}
return void_result();
} FC_CAPTURE_AND_RETHROW((o)) }
void_result asset_claim_fees_evaluator::do_evaluate( const asset_claim_fees_operation& o )
{ try {
FC_ASSERT( o.amount_to_claim.asset_id(db()).issuer == o.issuer, "Asset fees may only be claimed by the issuer" );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_claim_fees_evaluator::do_apply( const asset_claim_fees_operation& o )
{ try {
database& d = db();
const asset_object& a = o.amount_to_claim.asset_id(d);
const asset_dynamic_data_object& addo = a.dynamic_asset_data_id(d);
FC_ASSERT( o.amount_to_claim.amount <= addo.accumulated_fees, "Attempt to claim more fees than have accumulated", ("addo",addo) );
d.modify( addo, [&]( asset_dynamic_data_object& _addo ) {
_addo.accumulated_fees -= o.amount_to_claim.amount;
});
d.adjust_balance( o.issuer, o.amount_to_claim );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_claim_pool_evaluator::do_evaluate( const asset_claim_pool_operation& o )
{ try {
FC_ASSERT( o.asset_id(db()).issuer == o.issuer, "Asset fee pool may only be claimed by the issuer" );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
void_result asset_claim_pool_evaluator::do_apply( const asset_claim_pool_operation& o )
{ try {
database& d = db();
const asset_object& a = o.asset_id(d);
const asset_dynamic_data_object& addo = a.dynamic_asset_data_id(d);
FC_ASSERT( o.amount_to_claim.amount <= addo.fee_pool, "Attempt to claim more fees than is available", ("addo",addo) );
d.modify( addo, [&o]( asset_dynamic_data_object& _addo ) {
_addo.fee_pool -= o.amount_to_claim.amount;
});
d.adjust_balance( o.issuer, o.amount_to_claim );
return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }
} } // graphene::chain