-
Notifications
You must be signed in to change notification settings - Fork 648
/
database_api.cpp
2558 lines (2190 loc) · 99.4 KB
/
database_api.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) 2017 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/app/database_api.hpp>
#include <graphene/app/util.hpp>
#include <graphene/chain/get_config.hpp>
#include <graphene/chain/hardfork.hpp>
#include <fc/bloom_filter.hpp>
#include <fc/crypto/hex.hpp>
#include <fc/uint128.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <cctype>
#include <cfenv>
#include <iostream>
#define GET_REQUIRED_FEES_MAX_RECURSION 4
typedef std::map< std::pair<graphene::chain::asset_id_type, graphene::chain::asset_id_type>, std::vector<fc::variant> > market_queue_type;
namespace graphene { namespace app {
class database_api_impl : public std::enable_shared_from_this<database_api_impl>
{
public:
explicit database_api_impl( graphene::chain::database& db, const application_options* app_options );
~database_api_impl();
// Objects
fc::variants get_objects(const vector<object_id_type>& ids)const;
// Subscriptions
void set_subscribe_callback( std::function<void(const variant&)> cb, bool notify_remove_create );
void set_pending_transaction_callback( std::function<void(const variant&)> cb );
void set_block_applied_callback( std::function<void(const variant& block_id)> cb );
void cancel_all_subscriptions(bool reset_callback, bool reset_market_subscriptions);
// Blocks and transactions
optional<block_header> get_block_header(uint32_t block_num)const;
map<uint32_t, optional<block_header>> get_block_header_batch(const vector<uint32_t> block_nums)const;
optional<signed_block> get_block(uint32_t block_num)const;
processed_transaction get_transaction( uint32_t block_num, uint32_t trx_in_block )const;
// Globals
chain_property_object get_chain_properties()const;
global_property_object get_global_properties()const;
fc::variant_object get_config()const;
chain_id_type get_chain_id()const;
dynamic_global_property_object get_dynamic_global_properties()const;
// Keys
vector<vector<account_id_type>> get_key_references( vector<public_key_type> key )const;
bool is_public_key_registered(string public_key) const;
// Accounts
account_id_type get_account_id_from_string(const std::string& name_or_id)const;
vector<optional<account_object>> get_accounts(const vector<std::string>& account_names_or_ids)const;
std::map<string,full_account> get_full_accounts( const vector<string>& names_or_ids, bool subscribe );
optional<account_object> get_account_by_name( string name )const;
vector<account_id_type> get_account_references( const std::string account_id_or_name )const;
vector<optional<account_object>> lookup_account_names(const vector<string>& account_names)const;
map<string,account_id_type> lookup_accounts(const string& lower_bound_name, uint32_t limit)const;
uint64_t get_account_count()const;
// Balances
vector<asset> get_account_balances(const std::string& account_name_or_id, const flat_set<asset_id_type>& assets)const;
vector<asset> get_named_account_balances(const std::string& name, const flat_set<asset_id_type>& assets)const;
vector<balance_object> get_balance_objects( const vector<address>& addrs )const;
vector<asset> get_vested_balances( const vector<balance_id_type>& objs )const;
vector<vesting_balance_object> get_vesting_balances( const std::string account_id_or_name )const;
// Assets
asset_id_type get_asset_id_from_string(const std::string& symbol_or_id)const;
vector<optional<asset_object>> get_assets(const vector<std::string>& asset_symbols_or_ids)const;
vector<asset_object> list_assets(const string& lower_bound_symbol, uint32_t limit)const;
vector<optional<asset_object>> lookup_asset_symbols(const vector<string>& symbols_or_ids)const;
uint64_t get_asset_count()const;
// Markets / feeds
vector<limit_order_object> get_limit_orders(const std::string& a, const std::string& b, uint32_t limit)const;
vector<limit_order_object> get_account_limit_orders( const string& account_name_or_id,
const string &base,
const string "e, uint32_t limit,
optional<limit_order_id_type> ostart_id,
optional<price> ostart_price );
vector<call_order_object> get_call_orders(const std::string& a, uint32_t limit)const;
vector<force_settlement_object> get_settle_orders(const std::string& a, uint32_t limit)const;
vector<call_order_object> get_margin_positions( const std::string account_id_or_name )const;
vector<collateral_bid_object> get_collateral_bids(const std::string& asset, uint32_t limit, uint32_t start)const;
void subscribe_to_market(std::function<void(const variant&)> callback, const std::string& a, const std::string& b);
void unsubscribe_from_market(const std::string& a, const std::string& b);
market_ticker get_ticker( const string& base, const string& quote, bool skip_order_book = false )const;
market_volume get_24_volume( const string& base, const string& quote )const;
order_book get_order_book( const string& base, const string& quote, unsigned limit = 50 )const;
vector<market_ticker> get_top_markets( uint32_t limit )const;
vector<market_trade> get_trade_history( const string& base, const string& quote, fc::time_point_sec start, fc::time_point_sec stop, unsigned limit = 100 )const;
vector<market_trade> get_trade_history_by_sequence( const string& base, const string& quote, int64_t start, fc::time_point_sec stop, unsigned limit = 100 )const;
// Witnesses
vector<optional<witness_object>> get_witnesses(const vector<witness_id_type>& witness_ids)const;
fc::optional<witness_object> get_witness_by_account(const std::string account_id_or_name)const;
map<string, witness_id_type> lookup_witness_accounts(const string& lower_bound_name, uint32_t limit)const;
uint64_t get_witness_count()const;
// Committee members
vector<optional<committee_member_object>> get_committee_members(const vector<committee_member_id_type>& committee_member_ids)const;
fc::optional<committee_member_object> get_committee_member_by_account(const std::string account_id_or_name)const;
map<string, committee_member_id_type> lookup_committee_member_accounts(const string& lower_bound_name, uint32_t limit)const;
uint64_t get_committee_count()const;
// Workers
vector<worker_object> get_all_workers()const;
vector<optional<worker_object>> get_workers_by_account(const std::string account_id_or_name)const;
uint64_t get_worker_count()const;
// Votes
vector<variant> lookup_vote_ids( const vector<vote_id_type>& votes )const;
// Authority / validation
std::string get_transaction_hex(const signed_transaction& trx)const;
std::string get_transaction_hex_without_sig(const signed_transaction& trx)const;
set<public_key_type> get_required_signatures( const signed_transaction& trx, const flat_set<public_key_type>& available_keys )const;
set<public_key_type> get_potential_signatures( const signed_transaction& trx )const;
set<address> get_potential_address_signatures( const signed_transaction& trx )const;
bool verify_authority( const signed_transaction& trx )const;
bool verify_account_authority( const string& account_name_or_id, const flat_set<public_key_type>& signers )const;
processed_transaction validate_transaction( const signed_transaction& trx )const;
vector< fc::variant > get_required_fees( const vector<operation>& ops, const std::string& asset_id_or_symbol )const;
// Proposed transactions
vector<proposal_object> get_proposed_transactions( const std::string account_id_or_name )const;
// Blinded balances
vector<blinded_balance_object> get_blinded_balances( const flat_set<commitment_type>& commitments )const;
// Withdrawals
vector<withdraw_permission_object> get_withdraw_permissions_by_giver(const std::string account_id_or_name, withdraw_permission_id_type start, uint32_t limit)const;
vector<withdraw_permission_object> get_withdraw_permissions_by_recipient(const std::string account_id_or_name, withdraw_permission_id_type start, uint32_t limit)const;
//private:
static string price_to_string( const price& _price, const asset_object& _base, const asset_object& _quote );
template<typename T>
void subscribe_to_item( const T& i )const
{
auto vec = fc::raw::pack(i);
if( !_subscribe_callback )
return;
if( !is_subscribed_to_item(i) )
_subscribe_filter.insert( vec.data(), vec.size() );
}
template<typename T>
bool is_subscribed_to_item( const T& i )const
{
if( !_subscribe_callback )
return false;
return _subscribe_filter.contains( i );
}
bool is_impacted_account( const flat_set<account_id_type>& accounts)
{
if( !_subscribed_accounts.size() || !accounts.size() )
return false;
return std::any_of(accounts.begin(), accounts.end(), [this](const account_id_type& account) {
return _subscribed_accounts.find(account) != _subscribed_accounts.end();
});
}
const std::pair<asset_id_type,asset_id_type> get_order_market( const force_settlement_object& order )
{
// TODO cache the result to avoid repeatly fetching from db
asset_id_type backing_id = order.balance.asset_id( _db ).bitasset_data( _db ).options.short_backing_asset;
auto tmp = std::make_pair( order.balance.asset_id, backing_id );
if( tmp.first > tmp.second ) std::swap( tmp.first, tmp.second );
return tmp;
}
const account_object* get_account_from_string( const std::string& name_or_id ) const
{
// TODO cache the result to avoid repeatly fetching from db
FC_ASSERT( name_or_id.size() > 0);
const account_object* account = nullptr;
if (std::isdigit(name_or_id[0]))
account = _db.find(fc::variant(name_or_id, 1).as<account_id_type>(1));
else
{
const auto& idx = _db.get_index_type<account_index>().indices().get<by_name>();
auto itr = idx.find(name_or_id);
if (itr != idx.end())
account = &*itr;
}
FC_ASSERT( account, "no such account" );
return account;
}
const asset_object* get_asset_from_string( const std::string& symbol_or_id ) const
{
// TODO cache the result to avoid repeatly fetching from db
FC_ASSERT( symbol_or_id.size() > 0);
const asset_object* asset = nullptr;
if (std::isdigit(symbol_or_id[0]))
asset = _db.find(fc::variant(symbol_or_id, 1).as<asset_id_type>(1));
else
{
const auto& idx = _db.get_index_type<asset_index>().indices().get<by_symbol>();
auto itr = idx.find(symbol_or_id);
if (itr != idx.end())
asset = &*itr;
}
FC_ASSERT( asset, "no such asset" );
return asset;
}
vector<optional<asset_object>> get_assets(const vector<asset_id_type>& asset_ids)const
{
vector<optional<asset_object>> result; result.reserve(asset_ids.size());
std::transform(asset_ids.begin(), asset_ids.end(), std::back_inserter(result),
[this](asset_id_type id) -> optional<asset_object> {
if(auto o = _db.find(id))
{
subscribe_to_item( id );
return *o;
}
return {};
});
return result;
}
vector<limit_order_object> get_limit_orders(const asset_id_type a, const asset_id_type b, const uint32_t limit)const
{
FC_ASSERT( limit <= 300 );
const auto& limit_order_idx = _db.get_index_type<limit_order_index>();
const auto& limit_price_idx = limit_order_idx.indices().get<by_price>();
vector<limit_order_object> result;
result.reserve(limit*2);
uint32_t count = 0;
auto limit_itr = limit_price_idx.lower_bound(price::max(a,b));
auto limit_end = limit_price_idx.upper_bound(price::min(a,b));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}
count = 0;
limit_itr = limit_price_idx.lower_bound(price::max(b,a));
limit_end = limit_price_idx.upper_bound(price::min(b,a));
while(limit_itr != limit_end && count < limit)
{
result.push_back(*limit_itr);
++limit_itr;
++count;
}
return result;
}
template<typename T>
const std::pair<asset_id_type,asset_id_type> get_order_market( const T& order )
{
return order.get_market();
}
template<typename T>
void enqueue_if_subscribed_to_market(const object* obj, market_queue_type& queue, bool full_object=true)
{
const T* order = dynamic_cast<const T*>(obj);
FC_ASSERT( order != nullptr);
const auto& market = get_order_market( *order );
auto sub = _market_subscriptions.find( market );
if( sub != _market_subscriptions.end() ) {
queue[market].emplace_back( full_object ? obj->to_variant() : fc::variant(obj->id, 1) );
}
}
void broadcast_updates( const vector<variant>& updates );
void broadcast_market_updates( const market_queue_type& queue);
void handle_object_changed(bool force_notify, bool full_object, const vector<object_id_type>& ids, const flat_set<account_id_type>& impacted_accounts, std::function<const object*(object_id_type id)> find_object);
/** called every time a block is applied to report the objects that were changed */
void on_objects_new(const vector<object_id_type>& ids, const flat_set<account_id_type>& impacted_accounts);
void on_objects_changed(const vector<object_id_type>& ids, const flat_set<account_id_type>& impacted_accounts);
void on_objects_removed(const vector<object_id_type>& ids, const vector<const object*>& objs, const flat_set<account_id_type>& impacted_accounts);
void on_applied_block();
bool _notify_remove_create = false;
mutable fc::bloom_filter _subscribe_filter;
std::set<account_id_type> _subscribed_accounts;
std::function<void(const fc::variant&)> _subscribe_callback;
std::function<void(const fc::variant&)> _pending_trx_callback;
std::function<void(const fc::variant&)> _block_applied_callback;
boost::signals2::scoped_connection _new_connection;
boost::signals2::scoped_connection _change_connection;
boost::signals2::scoped_connection _removed_connection;
boost::signals2::scoped_connection _applied_block_connection;
boost::signals2::scoped_connection _pending_trx_connection;
map< pair<asset_id_type,asset_id_type>, std::function<void(const variant&)> > _market_subscriptions;
graphene::chain::database& _db;
const application_options* _app_options = nullptr;
};
//////////////////////////////////////////////////////////////////////
// //
// Constructors //
// //
//////////////////////////////////////////////////////////////////////
database_api::database_api( graphene::chain::database& db, const application_options* app_options )
: my( new database_api_impl( db, app_options ) ) {}
database_api::~database_api() {}
database_api_impl::database_api_impl( graphene::chain::database& db, const application_options* app_options )
:_db(db), _app_options(app_options)
{
wlog("creating database api ${x}", ("x",int64_t(this)) );
_new_connection = _db.new_objects.connect([this](const vector<object_id_type>& ids, const flat_set<account_id_type>& impacted_accounts) {
on_objects_new(ids, impacted_accounts);
});
_change_connection = _db.changed_objects.connect([this](const vector<object_id_type>& ids, const flat_set<account_id_type>& impacted_accounts) {
on_objects_changed(ids, impacted_accounts);
});
_removed_connection = _db.removed_objects.connect([this](const vector<object_id_type>& ids, const vector<const object*>& objs, const flat_set<account_id_type>& impacted_accounts) {
on_objects_removed(ids, objs, impacted_accounts);
});
_applied_block_connection = _db.applied_block.connect([this](const signed_block&){ on_applied_block(); });
_pending_trx_connection = _db.on_pending_transaction.connect([this](const signed_transaction& trx ){
if( _pending_trx_callback ) _pending_trx_callback( fc::variant(trx, GRAPHENE_MAX_NESTED_OBJECTS) );
});
}
database_api_impl::~database_api_impl()
{
elog("freeing database api ${x}", ("x",int64_t(this)) );
}
//////////////////////////////////////////////////////////////////////
// //
// Market ticker constructor //
// //
//////////////////////////////////////////////////////////////////////
market_ticker::market_ticker(const market_ticker_object& mto,
const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote,
const order_book& orders)
{
time = now;
base = asset_base.symbol;
quote = asset_quote.symbol;
percent_change = "0";
lowest_ask = "0";
highest_bid = "0";
fc::uint128 bv;
fc::uint128 qv;
price latest_price = asset( mto.latest_base, mto.base ) / asset( mto.latest_quote, mto.quote );
if( mto.base != asset_base.id )
latest_price = ~latest_price;
latest = database_api_impl::price_to_string( latest_price, asset_base, asset_quote );
if( mto.last_day_base != 0 && mto.last_day_quote != 0 // has trade data before 24 hours
&& ( mto.last_day_base != mto.latest_base || mto.last_day_quote != mto.latest_quote ) ) // price changed
{
price last_day_price = asset( mto.last_day_base, mto.base ) / asset( mto.last_day_quote, mto.quote );
if( mto.base != asset_base.id )
last_day_price = ~last_day_price;
percent_change = price_diff_percent_string( last_day_price, latest_price );
}
if( asset_base.id == mto.base )
{
bv = mto.base_volume;
qv = mto.quote_volume;
}
else
{
bv = mto.quote_volume;
qv = mto.base_volume;
}
base_volume = uint128_amount_to_string( bv, asset_base.precision );
quote_volume = uint128_amount_to_string( qv, asset_quote.precision );
if(!orders.asks.empty())
lowest_ask = orders.asks[0].price;
if(!orders.bids.empty())
highest_bid = orders.bids[0].price;
}
market_ticker::market_ticker(const fc::time_point_sec& now,
const asset_object& asset_base,
const asset_object& asset_quote)
{
time = now;
base = asset_base.symbol;
quote = asset_quote.symbol;
latest = "0";
lowest_ask = "0";
highest_bid = "0";
percent_change = "0";
base_volume = "0";
quote_volume = "0";
}
//////////////////////////////////////////////////////////////////////
// //
// Objects //
// //
//////////////////////////////////////////////////////////////////////
fc::variants database_api::get_objects(const vector<object_id_type>& ids)const
{
return my->get_objects( ids );
}
fc::variants database_api_impl::get_objects(const vector<object_id_type>& ids)const
{
if( _subscribe_callback ) {
for( auto id : ids )
{
if( id.type() == operation_history_object_type && id.space() == protocol_ids ) continue;
if( id.type() == impl_account_transaction_history_object_type && id.space() == implementation_ids ) continue;
this->subscribe_to_item( id );
}
}
fc::variants result;
result.reserve(ids.size());
std::transform(ids.begin(), ids.end(), std::back_inserter(result),
[this](object_id_type id) -> fc::variant {
if(auto obj = _db.find_object(id))
return obj->to_variant();
return {};
});
return result;
}
//////////////////////////////////////////////////////////////////////
// //
// Subscriptions //
// //
//////////////////////////////////////////////////////////////////////
void database_api::set_subscribe_callback( std::function<void(const variant&)> cb, bool notify_remove_create )
{
my->set_subscribe_callback( cb, notify_remove_create );
}
void database_api_impl::set_subscribe_callback( std::function<void(const variant&)> cb, bool notify_remove_create )
{
if( notify_remove_create )
{
FC_ASSERT( _app_options && _app_options->enable_subscribe_to_all,
"Subscribing to universal object creation and removal is disallowed in this server." );
}
cancel_all_subscriptions(false, false);
_subscribe_callback = cb;
_notify_remove_create = notify_remove_create;
}
void database_api::set_pending_transaction_callback( std::function<void(const variant&)> cb )
{
my->set_pending_transaction_callback( cb );
}
void database_api_impl::set_pending_transaction_callback( std::function<void(const variant&)> cb )
{
_pending_trx_callback = cb;
}
void database_api::set_block_applied_callback( std::function<void(const variant& block_id)> cb )
{
my->set_block_applied_callback( cb );
}
void database_api_impl::set_block_applied_callback( std::function<void(const variant& block_id)> cb )
{
_block_applied_callback = cb;
}
void database_api::cancel_all_subscriptions()
{
my->cancel_all_subscriptions(true, true);
}
void database_api_impl::cancel_all_subscriptions( bool reset_callback, bool reset_market_subscriptions )
{
if ( reset_callback )
_subscribe_callback = std::function<void(const fc::variant&)>();
if ( reset_market_subscriptions )
_market_subscriptions.clear();
_notify_remove_create = false;
_subscribed_accounts.clear();
static fc::bloom_parameters param(10000, 1.0/100, 1024*8*8*2);
_subscribe_filter = fc::bloom_filter(param);
}
//////////////////////////////////////////////////////////////////////
// //
// Blocks and transactions //
// //
//////////////////////////////////////////////////////////////////////
optional<block_header> database_api::get_block_header(uint32_t block_num)const
{
return my->get_block_header( block_num );
}
optional<block_header> database_api_impl::get_block_header(uint32_t block_num) const
{
auto result = _db.fetch_block_by_number(block_num);
if(result)
return *result;
return {};
}
map<uint32_t, optional<block_header>> database_api::get_block_header_batch(const vector<uint32_t> block_nums)const
{
return my->get_block_header_batch( block_nums );
}
map<uint32_t, optional<block_header>> database_api_impl::get_block_header_batch(const vector<uint32_t> block_nums) const
{
map<uint32_t, optional<block_header>> results;
for (const uint32_t block_num : block_nums)
{
results[block_num] = get_block_header(block_num);
}
return results;
}
optional<signed_block> database_api::get_block(uint32_t block_num)const
{
return my->get_block( block_num );
}
optional<signed_block> database_api_impl::get_block(uint32_t block_num)const
{
return _db.fetch_block_by_number(block_num);
}
processed_transaction database_api::get_transaction( uint32_t block_num, uint32_t trx_in_block )const
{
return my->get_transaction( block_num, trx_in_block );
}
optional<signed_transaction> database_api::get_recent_transaction_by_id( const transaction_id_type& id )const
{
try {
return my->_db.get_recent_transaction( id );
} catch ( ... ) {
return optional<signed_transaction>();
}
}
processed_transaction database_api_impl::get_transaction(uint32_t block_num, uint32_t trx_num)const
{
auto opt_block = _db.fetch_block_by_number(block_num);
FC_ASSERT( opt_block );
FC_ASSERT( opt_block->transactions.size() > trx_num );
return opt_block->transactions[trx_num];
}
//////////////////////////////////////////////////////////////////////
// //
// Globals //
// //
//////////////////////////////////////////////////////////////////////
chain_property_object database_api::get_chain_properties()const
{
return my->get_chain_properties();
}
chain_property_object database_api_impl::get_chain_properties()const
{
return _db.get(chain_property_id_type());
}
global_property_object database_api::get_global_properties()const
{
return my->get_global_properties();
}
global_property_object database_api_impl::get_global_properties()const
{
return _db.get(global_property_id_type());
}
fc::variant_object database_api::get_config()const
{
return my->get_config();
}
fc::variant_object database_api_impl::get_config()const
{
return graphene::chain::get_config();
}
chain_id_type database_api::get_chain_id()const
{
return my->get_chain_id();
}
chain_id_type database_api_impl::get_chain_id()const
{
return _db.get_chain_id();
}
dynamic_global_property_object database_api::get_dynamic_global_properties()const
{
return my->get_dynamic_global_properties();
}
dynamic_global_property_object database_api_impl::get_dynamic_global_properties()const
{
return _db.get(dynamic_global_property_id_type());
}
//////////////////////////////////////////////////////////////////////
// //
// Keys //
// //
//////////////////////////////////////////////////////////////////////
vector<vector<account_id_type>> database_api::get_key_references( vector<public_key_type> key )const
{
FC_ASSERT(key.size() <= 100, "Number of keys must be 100 or less");
return my->get_key_references( key );
}
/**
* @return all accounts that referr to the key or account id in their owner or active authorities.
*/
vector<vector<account_id_type>> database_api_impl::get_key_references( vector<public_key_type> keys )const
{
const auto& idx = _db.get_index_type<account_index>();
const auto& aidx = dynamic_cast<const base_primary_index&>(idx);
const auto& refs = aidx.get_secondary_index<graphene::chain::account_member_index>();
vector< vector<account_id_type> > final_result;
final_result.reserve(keys.size());
for( auto& key : keys )
{
address a1( pts_address(key, false, 56) );
address a2( pts_address(key, true, 56) );
address a3( pts_address(key, false, 0) );
address a4( pts_address(key, true, 0) );
address a5( key );
subscribe_to_item( key );
subscribe_to_item( a1 );
subscribe_to_item( a2 );
subscribe_to_item( a3 );
subscribe_to_item( a4 );
subscribe_to_item( a5 );
vector<account_id_type> result;
for( auto& a : {a1,a2,a3,a4,a5} )
{
auto itr = refs.account_to_address_memberships.find(a);
if( itr != refs.account_to_address_memberships.end() )
{
result.reserve( result.size() + itr->second.size() );
for( auto item : itr->second )
{
result.push_back(item);
}
}
}
auto itr = refs.account_to_key_memberships.find(key);
if( itr != refs.account_to_key_memberships.end() )
{
result.reserve( result.size() + itr->second.size() );
for( auto item : itr->second ) result.push_back(item);
}
final_result.emplace_back( std::move(result) );
}
return final_result;
}
bool database_api::is_public_key_registered(string public_key) const
{
return my->is_public_key_registered(public_key);
}
bool database_api_impl::is_public_key_registered(string public_key) const
{
// Short-circuit
if (public_key.empty()) {
return false;
}
// Search among all keys using an existing map of *current* account keys
public_key_type key;
try {
key = public_key_type(public_key);
} catch ( ... ) {
// An invalid public key was detected
return false;
}
const auto& idx = _db.get_index_type<account_index>();
const auto& aidx = dynamic_cast<const base_primary_index&>(idx);
const auto& refs = aidx.get_secondary_index<graphene::chain::account_member_index>();
auto itr = refs.account_to_key_memberships.find(key);
bool is_known = itr != refs.account_to_key_memberships.end();
return is_known;
}
//////////////////////////////////////////////////////////////////////
// //
// Accounts //
// //
//////////////////////////////////////////////////////////////////////
account_id_type database_api::get_account_id_from_string(const std::string& name_or_id)const
{
return my->get_account_from_string( name_or_id )->id;
}
vector<optional<account_object>> database_api::get_accounts(const vector<std::string>& account_names_or_ids)const
{
return my->get_accounts( account_names_or_ids );
}
vector<optional<account_object>> database_api_impl::get_accounts(const vector<std::string>& account_names_or_ids)const
{
vector<optional<account_object>> result; result.reserve(account_names_or_ids.size());
std::transform(account_names_or_ids.begin(), account_names_or_ids.end(), std::back_inserter(result),
[this](std::string id_or_name) -> optional<account_object> {
const account_object* account = get_account_from_string(id_or_name);
account_id_type id = account->id;
if(auto o = _db.find(id))
{
subscribe_to_item( id );
return *o;
}
return {};
});
return result;
}
vector<limit_order_object> database_api::get_account_limit_orders( const string& account_name_or_id, const string &base,
const string "e, uint32_t limit, optional<limit_order_id_type> ostart_id, optional<price> ostart_price)
{
return my->get_account_limit_orders( account_name_or_id, base, quote, limit, ostart_id, ostart_price );
}
vector<limit_order_object> database_api_impl::get_account_limit_orders( const string& account_name_or_id, const string &base,
const string "e, uint32_t limit, optional<limit_order_id_type> ostart_id, optional<price> ostart_price)
{
FC_ASSERT( limit <= 101 );
vector<limit_order_object> results;
uint32_t count = 0;
const account_object* account = get_account_from_string(account_name_or_id);
if (account == nullptr)
return results;
auto assets = lookup_asset_symbols( {base, quote} );
FC_ASSERT( assets[0], "Invalid base asset symbol: ${s}", ("s",base) );
FC_ASSERT( assets[1], "Invalid quote asset symbol: ${s}", ("s",quote) );
auto base_id = assets[0]->id;
auto quote_id = assets[1]->id;
if (ostart_price.valid()) {
FC_ASSERT(ostart_price->base.asset_id == base_id, "Base asset inconsistent with start price");
FC_ASSERT(ostart_price->quote.asset_id == quote_id, "Quote asset inconsistent with start price");
}
const auto& index_by_account = _db.get_index_type<limit_order_index>().indices().get<by_account>();
limit_order_multi_index_type::index<by_account>::type::const_iterator lower_itr;
limit_order_multi_index_type::index<by_account>::type::const_iterator upper_itr;
// if both order_id and price are invalid, query the first page
if ( !ostart_id.valid() && !ostart_price.valid() )
{
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, price::max(base_id, quote_id)));
}
else if ( ostart_id.valid() )
{
// in case of the order been deleted during page querying
const limit_order_object *p_loo = _db.find(*ostart_id);
if ( !p_loo )
{
if ( ostart_price.valid() )
{
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, *ostart_price, *ostart_id));
}
else
{
// start order id been deleted, yet not provided price either
FC_THROW("Order id invalid (maybe just been canceled?), and start price not provided");
}
}
else
{
const limit_order_object &loo = *p_loo;
// in case of the order not belongs to specified account or market
FC_ASSERT(loo.sell_price.base.asset_id == base_id, "Order base asset inconsistent");
FC_ASSERT(loo.sell_price.quote.asset_id == quote_id, "Order quote asset inconsistent with order");
FC_ASSERT(loo.seller == account->get_id(), "Order not owned by specified account");
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, loo.sell_price, *ostart_id));
}
}
else
{
// if reach here start_price must be valid
lower_itr = index_by_account.lower_bound(std::make_tuple(account->id, *ostart_price));
}
upper_itr = index_by_account.upper_bound(std::make_tuple(account->id, price::min(base_id, quote_id)));
// Add the account's orders
for ( ; lower_itr != upper_itr && count < limit; ++lower_itr, ++count)
{
const limit_order_object &order = *lower_itr;
results.emplace_back(order);
}
return results;
}
std::map<string,full_account> database_api::get_full_accounts( const vector<string>& names_or_ids, bool subscribe )
{
return my->get_full_accounts( names_or_ids, subscribe );
}
std::map<std::string, full_account> database_api_impl::get_full_accounts( const vector<std::string>& names_or_ids, bool subscribe)
{
const auto& proposal_idx = _db.get_index_type<proposal_index>();
const auto& pidx = dynamic_cast<const base_primary_index&>(proposal_idx);
const auto& proposals_by_account = pidx.get_secondary_index<graphene::chain::required_approval_index>();
std::map<std::string, full_account> results;
for (const std::string& account_name_or_id : names_or_ids)
{
const account_object* account = get_account_from_string(account_name_or_id);
if (account == nullptr)
continue;
if( subscribe )
{
if(_subscribed_accounts.size() < 100) {
_subscribed_accounts.insert( account->get_id() );
subscribe_to_item( account->id );
}
}
full_account acnt;
acnt.account = *account;
acnt.statistics = account->statistics(_db);
acnt.registrar_name = account->registrar(_db).name;
acnt.referrer_name = account->referrer(_db).name;
acnt.lifetime_referrer_name = account->lifetime_referrer(_db).name;
acnt.votes = lookup_vote_ids( vector<vote_id_type>(account->options.votes.begin(),account->options.votes.end()) );
if (account->cashback_vb)
{
acnt.cashback_balance = account->cashback_balance(_db);
}
// Add the account's proposals
auto required_approvals_itr = proposals_by_account._account_to_proposals.find( account->id );
if( required_approvals_itr != proposals_by_account._account_to_proposals.end() )
{
acnt.proposals.reserve( required_approvals_itr->second.size() );
for( auto proposal_id : required_approvals_itr->second )
acnt.proposals.push_back( proposal_id(_db) );
}
// Add the account's balances
const auto& balances = _db.get_index_type< primary_index< account_balance_index > >().get_secondary_index< balances_by_account_index >().get_account_balances( account->id );
for( const auto balance : balances )
acnt.balances.emplace_back( *balance.second );
// Add the account's vesting balances
auto vesting_range = _db.get_index_type<vesting_balance_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(vesting_range.first, vesting_range.second,
[&acnt](const vesting_balance_object& balance) {
acnt.vesting_balances.emplace_back(balance);
});
// Add the account's orders
auto order_range = _db.get_index_type<limit_order_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(order_range.first, order_range.second,
[&acnt] (const limit_order_object& order) {
acnt.limit_orders.emplace_back(order);
});
auto call_range = _db.get_index_type<call_order_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(call_range.first, call_range.second,
[&acnt] (const call_order_object& call) {
acnt.call_orders.emplace_back(call);
});
auto settle_range = _db.get_index_type<force_settlement_index>().indices().get<by_account>().equal_range(account->id);
std::for_each(settle_range.first, settle_range.second,
[&acnt] (const force_settlement_object& settle) {
acnt.settle_orders.emplace_back(settle);
});
// get assets issued by user
auto asset_range = _db.get_index_type<asset_index>().indices().get<by_issuer>().equal_range(account->id);
std::for_each(asset_range.first, asset_range.second,
[&acnt] (const asset_object& asset) {
acnt.assets.emplace_back(asset.id);
});
// get withdraws permissions
auto withdraw_range = _db.get_index_type<withdraw_permission_index>().indices().get<by_from>().equal_range(account->id);
std::for_each(withdraw_range.first, withdraw_range.second,
[&acnt] (const withdraw_permission_object& withdraw) {
acnt.withdraws.emplace_back(withdraw);
});
results[account_name_or_id] = acnt;
}
return results;
}
optional<account_object> database_api::get_account_by_name( string name )const
{
return my->get_account_by_name( name );
}
optional<account_object> database_api_impl::get_account_by_name( string name )const
{
const auto& idx = _db.get_index_type<account_index>().indices().get<by_name>();
auto itr = idx.find(name);
if (itr != idx.end())
return *itr;
return optional<account_object>();
}
vector<account_id_type> database_api::get_account_references( const std::string account_id_or_name )const
{
return my->get_account_references( account_id_or_name );
}
vector<account_id_type> database_api_impl::get_account_references( const std::string account_id_or_name )const
{
const auto& idx = _db.get_index_type<account_index>();
const auto& aidx = dynamic_cast<const base_primary_index&>(idx);
const auto& refs = aidx.get_secondary_index<graphene::chain::account_member_index>();