-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathasset_evaluator.cpp
1566 lines (1324 loc) · 69.2 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
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
/*
* 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!");
}
}
void check_bitasset_options_hf_bsip74( const fc::time_point_sec& block_time, const bitasset_options& options)
{
// HF_REMOVABLE: Following hardfork check should be removable after hardfork date passes:
FC_ASSERT( block_time >= HARDFORK_CORE_BSIP74_TIME
|| !options.extensions.value.margin_call_fee_ratio.valid(),
"A BitAsset's MCFR cannot be set before Hardfork BSIP74" );
}
// 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");
}
}
// TODO review and remove code below and links to it after HARDFORK_BSIP_48_75_TIME
void check_asset_options_hf_bsip_48_75(const fc::time_point_sec& block_time, const asset_options& options)
{
if ( !HARDFORK_BSIP_48_75_PASSED( block_time ) )
{
// new issuer permissions should not be set until activation of BSIP_48_75
FC_ASSERT( 0 == (options.issuer_permissions & (uint16_t)(~ASSET_ISSUER_PERMISSION_ENABLE_BITS_MASK)),
"New asset issuer permission bits should not be set before HARDFORK_BSIP_48_75_TIME" );
// Note: no check for flags here because we didn't check in the past
}
}
// TODO review and remove code below and links to it after HARDFORK_BSIP_48_75_TIME
void check_bitasset_options_hf_bsip_48_75(const fc::time_point_sec& block_time, const bitasset_options& options)
{
if ( !HARDFORK_BSIP_48_75_PASSED( block_time ) )
{
// new params should not be set until activation of BSIP_48_75
FC_ASSERT( !options.extensions.value.maintenance_collateral_ratio.valid(),
"Maintenance collateral ratio should not be defined by asset owner "
"before HARDFORK_BSIP_48_75_TIME" );
FC_ASSERT( !options.extensions.value.maximum_short_squeeze_ratio.valid(),
"Maximum short squeeze ratio should not be defined by asset owner "
"before HARDFORK_BSIP_48_75_TIME" );
}
}
// TODO review and remove code below and links to it after HARDFORK_BSIP_48_75_TIME
void check_asset_update_extensions_hf_bsip_48_75( const fc::time_point_sec& block_time,
const asset_update_operation::ext& extensions )
{
if ( !HARDFORK_BSIP_48_75_PASSED( block_time ) )
{
// new extensions should not be set until activation of BSIP_48_75
FC_ASSERT( !extensions.new_precision.valid(),
"new_precision should not be set before HARDFORK_BSIP_48_75_TIME" );
FC_ASSERT( !extensions.skip_core_exchange_rate.valid(),
"skip_core_exchange_rate should not be set before HARDFORK_BSIP_48_75_TIME" );
}
}
// TODO review and remove code below and links to it after HARDFORK_BSIP_77_TIME
void check_asset_publish_feed_extensions_hf_bsip77( const fc::time_point_sec& block_time,
const asset_publish_feed_operation::ext& extensions )
{
if ( !HARDFORK_BSIP_77_PASSED( block_time ) )
{
// new extensions should not be set until activation of BSIP_77
FC_ASSERT( !extensions.initial_collateral_ratio.valid(),
"Initial collateral ratio should not be defined before HARDFORK_BSIP_77_TIME" );
}
}
// TODO review and remove code below and links to it after HARDFORK_BSIP_77_TIME
void check_bitasset_options_hf_bsip77(const fc::time_point_sec& block_time, const bitasset_options& options)
{
if ( !HARDFORK_BSIP_77_PASSED( block_time ) ) {
// ICR should not be set until activation of BSIP77
FC_ASSERT(!options.extensions.value.initial_collateral_ratio.valid(),
"Initial collateral ratio should not be defined before HARDFORK_BSIP_77_TIME");
}
}
void check_bitasset_options_hf_bsip87(const fc::time_point_sec& block_time, const bitasset_options& options)
{
// HF_REMOVABLE: Following hardfork check should be removable after hardfork date passes:
FC_ASSERT( !options.extensions.value.force_settle_fee_percent.valid()
|| block_time >= HARDFORK_CORE_BSIP87_TIME,
"A BitAsset's FSFP cannot be set before Hardfork BSIP87" );
}
void check_asset_claim_fees_hardfork_87_74_collatfee(const fc::time_point_sec& block_time,
const asset_claim_fees_operation& op)
{
// HF_REMOVABLE: Following hardfork check should be removable after hardfork date passes:
FC_ASSERT( !op.extensions.value.claim_from_asset_id.valid() ||
block_time >= HARDFORK_CORE_BSIP_87_74_COLLATFEE_TIME,
"Collateral-denominated fees are not yet active and therefore cannot be claimed." );
}
void check_asset_options_hf_core2281( const fc::time_point_sec& next_maint_time, const asset_options& options)
{
// HF_REMOVABLE: Following hardfork check should be removable after hardfork date passes:
if ( !HARDFORK_CORE_2281_PASSED(next_maint_time) )
{
// new issuer permissions should not be set until activation of the hardfork
FC_ASSERT( 0 == (options.issuer_permissions & asset_issuer_permission_flags::disable_collateral_bidding),
"New asset issuer permission bit 'disable_collateral_bidding' should not be set "
"before Hardfork core-2281" );
// Note: checks about flags are more complicated due to old bugs,
// and likely can not be removed after hardfork, so do not put them here
}
}
void check_asset_options_hf_core2467(const fc::time_point_sec& next_maint_time, const asset_options& options)
{
// HF_REMOVABLE: Following hardfork check should be removable after hardfork date passes:
if ( !HARDFORK_CORE_2467_PASSED(next_maint_time) )
{
// new issuer permissions should not be set until activation of the hardfork
FC_ASSERT( 0 == (options.issuer_permissions & asset_issuer_permission_flags::disable_bsrm_update),
"New asset issuer permission bit 'disable_bsrm_update' should not be set "
"before Hardfork core-2467" );
}
}
void check_bitasset_opts_hf_core2467(const fc::time_point_sec& next_maint_time, const bitasset_options& options)
{
// HF_REMOVABLE: Following hardfork check should be removable after hardfork date passes:
if ( !HARDFORK_CORE_2467_PASSED(next_maint_time) )
{
FC_ASSERT( !options.extensions.value.black_swan_response_method.valid(),
"A BitAsset's black swan response method cannot be set before Hardfork core-2467" );
}
}
} // graphene::chain::detail
void_result asset_create_evaluator::do_evaluate( const asset_create_operation& op )
{ try {
const database& d = db();
const time_point_sec now = d.head_block_time();
const fc::time_point_sec next_maint_time = d.get_dynamic_global_properties().next_maintenance_time;
// Hardfork Checks:
detail::check_asset_options_hf_1774(now, op.common_options);
detail::check_asset_options_hf_bsip_48_75(now, op.common_options);
detail::check_asset_options_hf_bsip81(now, op.common_options);
detail::check_asset_options_hf_core2281( next_maint_time, op.common_options ); // HF_REMOVABLE
detail::check_asset_options_hf_core2467( next_maint_time, op.common_options ); // HF_REMOVABLE
if( op.bitasset_opts ) {
detail::check_bitasset_options_hf_bsip_48_75( now, *op.bitasset_opts );
detail::check_bitasset_options_hf_bsip74( now, *op.bitasset_opts ); // HF_REMOVABLE
detail::check_bitasset_options_hf_bsip77( now, *op.bitasset_opts ); // HF_REMOVABLE
detail::check_bitasset_options_hf_bsip87( now, *op.bitasset_opts ); // HF_REMOVABLE
detail::check_bitasset_opts_hf_core2467( next_maint_time, *op.bitasset_opts ); // HF_REMOVABLE
}
// TODO move as many validations as possible to validate() if not triggered before hardfork
if( HARDFORK_CORE_2281_PASSED( next_maint_time ) )
{
op.common_options.validate_flags( op.bitasset_opts.valid() );
}
else if( HARDFORK_BSIP_48_75_PASSED( now ) )
{
// do not allow the 'disable_collateral_bidding' bit
op.common_options.validate_flags( op.bitasset_opts.valid(), false );
}
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 );
// 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() );
// 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 );
}
return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }
void asset_create_evaluator::pay_fee()
{
constexpr int64_t two = 2;
fee_is_odd = ( ( core_fee_paid.value % two ) != 0 );
core_fee_paid -= core_fee_paid.value / two;
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." );
FC_ASSERT( !a.is_liquidity_pool_share_asset(), "Cannot manually issue a liquidity pool share asset." );
FC_ASSERT( a.can_create_new_supply(), "Can not create new supply" );
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 = fee_paying_account;
FC_ASSERT( is_authorized_asset( d, *from_account, a ) );
asset_dyn_data = &a.dynamic_asset_data_id(d);
if( !a.is_liquidity_pool_share_asset() )
{
FC_ASSERT( asset_dyn_data->current_supply >= o.amount_to_reserve.amount,
"Can not reserve an amount that is more than the current supply" );
}
else
{
FC_ASSERT( asset_dyn_data->current_supply > o.amount_to_reserve.amount,
"The asset is a liquidity pool share asset thus can only reserve an amount "
"that is less than the current supply" );
}
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 fc::time_point_sec next_maint_time = d.get_dynamic_global_properties().next_maintenance_time;
// Hardfork Checks:
detail::check_asset_options_hf_1774(now, o.new_options);
detail::check_asset_options_hf_bsip_48_75(now, o.new_options);
detail::check_asset_options_hf_bsip81(now, o.new_options);
detail::check_asset_options_hf_core2281( next_maint_time, o.new_options ); // HF_REMOVABLE
detail::check_asset_options_hf_core2467( next_maint_time, o.new_options ); // HF_REMOVABLE
detail::check_asset_update_extensions_hf_bsip_48_75( now, o.extensions.value );
bool hf_bsip_48_75_passed = ( HARDFORK_BSIP_48_75_PASSED( now ) );
bool hf_core_2281_passed = ( HARDFORK_CORE_2281_PASSED( next_maint_time ) );
bool hf_core_2467_passed = ( HARDFORK_CORE_2467_PASSED( next_maint_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 );
}
if( a.is_market_issued() )
bitasset_data = &a.bitasset_data(d);
if( hf_core_2467_passed )
{
// Unable to set non-UIA issuer permission bits on UIA
if( !a.is_market_issued() )
FC_ASSERT( 0 == ( o.new_options.issuer_permissions & NON_UIA_ONLY_ISSUER_PERMISSION_MASK ),
"Unable to set non-UIA issuer permission bits on UIA" );
// Unable to set disable_bsrm_update issuer permission bit on PM
else if( bitasset_data->is_prediction_market )
FC_ASSERT( 0 == ( o.new_options.issuer_permissions & disable_bsrm_update ),
"Unable to set disable_bsrm_update issuer permission bit on PM" );
// else do nothing
}
uint16_t enabled_issuer_permissions_mask = a.options.get_enabled_issuer_permissions_mask();
if( hf_bsip_48_75_passed && a.is_market_issued() && bitasset_data->is_prediction_market )
{
// Note: if the global_settle permission was unset, it should be corrected
FC_ASSERT( a_copy.can_global_settle(),
"The global_settle permission should be enabled for prediction markets" );
enabled_issuer_permissions_mask |= global_settle;
}
const auto& dyn_data = a.dynamic_asset_data_id(d);
if( dyn_data.current_supply != 0 )
{
// new issuer_permissions must be subset of old issuer permissions
if( hf_core_2467_passed && !a.is_market_issued() ) // for UIA, ignore non-UIA bits
FC_ASSERT( 0 == ( ( o.new_options.get_enabled_issuer_permissions_mask()
& (uint16_t)(~enabled_issuer_permissions_mask) ) & UIA_ASSET_ISSUER_PERMISSION_MASK ),
"Cannot reinstate previously revoked issuer permissions on a UIA if current supply is non-zero, "
"unless to unset non-UIA issuer permission bits.");
else if( hf_core_2467_passed && bitasset_data->is_prediction_market ) // for PM, ignore disable_bsrm_update
FC_ASSERT( 0 == ( ( o.new_options.get_enabled_issuer_permissions_mask()
& (uint16_t)(~enabled_issuer_permissions_mask) ) & (uint16_t)(~disable_bsrm_update) ),
"Cannot reinstate previously revoked issuer permissions on a PM if current supply is non-zero, "
"unless to unset the disable_bsrm_update issuer permission bit.");
else
FC_ASSERT( 0 == ( o.new_options.get_enabled_issuer_permissions_mask()
& (uint16_t)(~enabled_issuer_permissions_mask) ),
"Cannot reinstate previously revoked issuer permissions on an asset if current supply is non-zero.");
// precision can not be changed
FC_ASSERT( !o.extensions.value.new_precision.valid(),
"Cannot update precision if current supply is non-zero" );
if( hf_bsip_48_75_passed ) // TODO review after hard fork, probably can assert unconditionally
{
FC_ASSERT( dyn_data.current_supply <= o.new_options.max_supply,
"Max supply should not be smaller than current supply" );
}
}
// If an invalid bit was set in flags, it should be unset
// TODO move as many validations as possible to validate() if not triggered before hardfork
if( hf_core_2281_passed )
{
o.new_options.validate_flags( a.is_market_issued() );
}
else if( hf_bsip_48_75_passed )
{
// do not allow the 'disable_collateral_bidding' bit
o.new_options.validate_flags( a.is_market_issued(), false );
}
// changed flags must be subset of old issuer permissions
if( hf_bsip_48_75_passed )
{
// Note: if an invalid bit was set, it can be unset regardless of the permissions
uint16_t valid_flags_mask = hf_core_2281_passed ? VALID_FLAGS_MASK
: (VALID_FLAGS_MASK & (uint16_t)(~disable_collateral_bidding));
uint16_t check_bits = a.is_market_issued() ? valid_flags_mask : UIA_VALID_FLAGS_MASK;
FC_ASSERT( 0 == ( (o.new_options.flags ^ a.options.flags) & check_bits
& (uint16_t)(~enabled_issuer_permissions_mask) ),
"Flag change is forbidden by issuer permissions" );
}
else
{
FC_ASSERT( 0 == ( (o.new_options.flags ^ a.options.flags) & (uint16_t)(~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) );
FC_ASSERT( a.can_update_max_supply() || a.options.max_supply == o.new_options.max_supply,
"Can not update max supply" );
if( o.extensions.value.new_precision.valid() )
{
FC_ASSERT( *o.extensions.value.new_precision != a.precision,
"Specified a new precision but it does not change" );
if( a.is_market_issued() )
FC_ASSERT( !bitasset_data->is_prediction_market, "Can not update precision of a prediction market" );
// If any other asset is backed by this asset, this asset's precision can't be updated
const auto& idx = d.get_index_type<graphene::chain::asset_bitasset_data_index>()
.indices().get<by_short_backing_asset>();
auto itr = idx.lower_bound( o.asset_to_update );
bool backing_another_asset = ( itr != idx.end() && itr->options.short_backing_asset == o.asset_to_update );
FC_ASSERT( !backing_another_asset,
"Asset ${a} is backed by this asset, can not update precision",
("a",itr->asset_id) );
}
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);
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( 0 != (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);
}
// If we are now disabling collateral bidding, cancel all open collateral bids
if( 0 != (o.new_options.flags & disable_collateral_bidding) && asset_to_update->can_bid_collateral() )
{
const auto& bid_idx = d.get_index_type< collateral_bid_index >().indices().get<by_price>();
auto itr = bid_idx.lower_bound( o.asset_to_update );
const auto end = bid_idx.upper_bound( o.asset_to_update );
while( itr != end )
{
const collateral_bid_object& bid = *itr;
++itr;
d.cancel_bid( bid );
}
}
// For market-issued assets, if core exchange rate changed, update flag in bitasset data
if( !o.extensions.value.skip_core_exchange_rate.valid() && asset_to_update->is_market_issued()
&& asset_to_update->options.core_exchange_rate != o.new_options.core_exchange_rate )
{
const auto& bitasset = ( bitasset_data ? *bitasset_data : 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;
if( o.extensions.value.new_precision.valid() )
a.precision = *o.extensions.value.new_precision;
if( o.extensions.value.skip_core_exchange_rate.valid() )
{
const auto old_cer = a.options.core_exchange_rate;
a.options = o.new_options;
a.options.core_exchange_rate = old_cer;
}
else
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(const 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 {
const database& d = db();
const time_point_sec now = d.head_block_time();
const fc::time_point_sec next_maint_time = d.get_dynamic_global_properties().next_maintenance_time;
// Hardfork Checks:
detail::check_bitasset_options_hf_bsip_48_75( now, op.new_options );
detail::check_bitasset_options_hf_bsip74( now, op.new_options ); // HF_REMOVABLE
detail::check_bitasset_options_hf_bsip77( now, op.new_options ); // HF_REMOVABLE
detail::check_bitasset_options_hf_bsip87( now, op.new_options ); // HF_REMOVABLE
detail::check_bitasset_opts_hf_core2467( next_maint_time, op.new_options ); // HF_REMOVABLE
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);
if( !HARDFORK_CORE_2282_PASSED( next_maint_time ) )
FC_ASSERT( !current_bitasset_data.has_settlement(),
"Cannot update a bitasset after a global settlement has executed" );
if( current_bitasset_data.is_prediction_market )
FC_ASSERT( !op.new_options.extensions.value.black_swan_response_method.valid(),
"Can not set black_swan_response_method for Prediction Markets" );
// TODO simplify code below when made sure operator==(optional,optional) works
if( !asset_obj.can_owner_update_mcr() )
{
// check if MCR will change
const auto& old_mcr = current_bitasset_data.options.extensions.value.maintenance_collateral_ratio;
const auto& new_mcr = op.new_options.extensions.value.maintenance_collateral_ratio;
bool mcr_changed = ( ( old_mcr.valid() != new_mcr.valid() )
|| ( old_mcr.valid() && *old_mcr != *new_mcr ) );
FC_ASSERT( !mcr_changed, "No permission to update MCR" );
}
if( !asset_obj.can_owner_update_icr() )
{
// check if ICR will change
const auto& old_icr = current_bitasset_data.options.extensions.value.initial_collateral_ratio;
const auto& new_icr = op.new_options.extensions.value.initial_collateral_ratio;
bool icr_changed = ( ( old_icr.valid() != new_icr.valid() )
|| ( old_icr.valid() && *old_icr != *new_icr ) );
FC_ASSERT( !icr_changed, "No permission to update ICR" );
}
if( !asset_obj.can_owner_update_mssr() )
{
// check if MSSR will change
const auto& old_mssr = current_bitasset_data.options.extensions.value.maximum_short_squeeze_ratio;
const auto& new_mssr = op.new_options.extensions.value.maximum_short_squeeze_ratio;
bool mssr_changed = ( ( old_mssr.valid() != new_mssr.valid() )
|| ( old_mssr.valid() && *old_mssr != *new_mssr ) );
FC_ASSERT( !mssr_changed, "No permission to update MSSR" );
}
// check if BSRM will change
const auto old_bsrm = current_bitasset_data.get_black_swan_response_method();
const auto new_bsrm = op.new_options.get_black_swan_response_method();
if( old_bsrm != new_bsrm )
{
FC_ASSERT( asset_obj.can_owner_update_bsrm(), "No permission to update BSRM" );
FC_ASSERT( !current_bitasset_data.has_settlement(),
"Unable to update BSRM when the asset has been globally settled" );
// Note: it is probably OK to allow BSRM update, be conservative here so far
using bsrm_type = bitasset_options::black_swan_response_type;
if( bsrm_type::individual_settlement_to_fund == old_bsrm )
FC_ASSERT( !current_bitasset_data.has_individual_settlement(),
"Unable to update BSRM when the individual settlement pool is not empty" );
else if( bsrm_type::individual_settlement_to_order == old_bsrm )
FC_ASSERT( !d.find_settled_debt_order( op.asset_to_update ),
"Unable to update BSRM when there exists an individual settlement order" );
// Since we do not allow updating in some cases (above), only check no_settlement here
if( bsrm_type::no_settlement == old_bsrm || bsrm_type::no_settlement == new_bsrm )
update_feeds_due_to_bsrm_change = true;
}
// hf 922_931 is a consensus/logic change. This hf cannot be removed.
bool after_hf_core_922_931 = ( next_maint_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( !current_bitasset_data.has_settlement(),
"Cannot change backing asset after a global settlement has executed" );
const asset_dynamic_data_object& dyn = asset_obj.dynamic_asset_data_id(d);
FC_ASSERT( dyn.current_supply == 0,
"Cannot change backing asset if there is already a current supply." );
FC_ASSERT( dyn.accumulated_collateral_fees == 0,
"Must claim collateral-denominated fees before changing backing asset." );
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, and checks conditions that might necessitate a call to check_call_orders.
* Called from asset_update_bitasset_evaluator::do_apply().
*
* @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 we should check call orders, such as if if the feed price is changed, or some
* cases after hf core-868-890, or if the margin_call_fee_ratio has changed, which affects the
* matching price of margin call orders.
*/
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,
bool update_feeds_due_to_bsrm_change )
{
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_bitasset_current_feed 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( 0 != ( asset_to_update.options.flags & ( witness_fed_asset | committee_fed_asset ) ) )
is_witness_or_committee_fed = true;
}
// TODO simplify code below when made sure operator==(optional,optional) works
// check if ICR will change
if( !should_update_feeds )
{
const auto& old_icr = bdo.options.extensions.value.initial_collateral_ratio;
const auto& new_icr = op.new_options.extensions.value.initial_collateral_ratio;
bool icr_changed = ( ( old_icr.valid() != new_icr.valid() )
|| ( old_icr.valid() && *old_icr != *new_icr ) );
should_update_feeds = icr_changed;
}
// check if MCR will change
if( !should_update_feeds )
{
const auto& old_mcr = bdo.options.extensions.value.maintenance_collateral_ratio;
const auto& new_mcr = op.new_options.extensions.value.maintenance_collateral_ratio;
bool mcr_changed = ( ( old_mcr.valid() != new_mcr.valid() )
|| ( old_mcr.valid() && *old_mcr != *new_mcr ) );
should_update_feeds = mcr_changed;
}
// check if MSSR will change
if( !should_update_feeds )
{
const auto& old_mssr = bdo.options.extensions.value.maximum_short_squeeze_ratio;
const auto& new_mssr = op.new_options.extensions.value.maximum_short_squeeze_ratio;
bool mssr_changed = ( ( old_mssr.valid() != new_mssr.valid() )
|| ( old_mssr.valid() && *old_mssr != *new_mssr ) );
should_update_feeds = mssr_changed;
}
// check if MCFR will change
const auto& old_mcfr = bdo.options.extensions.value.margin_call_fee_ratio;
const auto& new_mcfr = op.new_options.extensions.value.margin_call_fee_ratio;
const bool mcfr_changed = ( ( old_mcfr.valid() != new_mcfr.valid() )
|| ( old_mcfr.valid() && *old_mcfr != *new_mcfr ) );
// Apply changes to bitasset options
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();
}
}
}
bool feed_actually_changed = false;
if( should_update_feeds || update_feeds_due_to_bsrm_change )
{
const auto old_feed = bdo.current_feed;
// skip recalculating median feed if it is not needed
db.update_bitasset_current_feed( bdo, !should_update_feeds );