BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface software
elasticsearch_plugin.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <graphene/app/plugin.hpp>
30 
31 namespace graphene { namespace elasticsearch {
32  using namespace chain;
33 
34 //
35 // Plugins should #define their SPACE_ID's so plugins with
36 // conflicting SPACE_ID assignments can be compiled into the
37 // same binary (by simply re-assigning some of the conflicting #defined
38 // SPACE_ID's in a build script).
39 //
40 // Assignment of SPACE_ID's cannot be done at run-time because
41 // various template automagic depends on them being known at compile
42 // time.
43 //
44 #ifndef ELASTICSEARCH_SPACE_ID
45 #define ELASTICSEARCH_SPACE_ID 6
46 #endif
47 
48 namespace detail
49 {
50  class elasticsearch_plugin_impl;
51 }
52 
53 enum mode { only_save = 0 , only_query = 1, all = 2 };
54 
56 {
57  public:
59  virtual ~elasticsearch_plugin();
60 
61  std::string plugin_name()const override;
62  std::string plugin_description()const override;
63  virtual void plugin_set_program_options(
64  boost::program_options::options_description& cli,
65  boost::program_options::options_description& cfg) override;
66  virtual void plugin_initialize(const boost::program_options::variables_map& options) override;
67  virtual void plugin_startup() override;
68 
69  operation_history_object get_operation_by_id(operation_history_id_type id);
70  vector<operation_history_object> get_account_history(const account_id_type account_id,
71  operation_history_id_type stop, unsigned limit, operation_history_id_type start);
72  mode get_running_mode();
73 
75  std::unique_ptr<detail::elasticsearch_plugin_impl> my;
76 
77  private:
79  graphene::utilities::ES prepareHistoryQuery(string query);
80 };
81 
82 
84 {
85  typedef void result_type;
86 
88  asset_id_type fee_asset;
89 
90  asset_id_type transfer_asset_id;
92  account_id_type transfer_from;
93  account_id_type transfer_to;
94 
96  {
97  fee_asset = o.fee.asset_id;
98  fee_amount = o.fee.amount;
99 
100  transfer_asset_id = o.amount.asset_id;
101  transfer_amount = o.amount.amount;
102  transfer_from = o.from;
103  transfer_to = o.to;
104  }
105 
107  account_id_type fill_account_id;
108  asset_id_type fill_pays_asset_id;
110  asset_id_type fill_receives_asset_id;
114 
116  {
117  fee_asset = o.fee.asset_id;
118  fee_amount = o.fee.amount;
119 
120  fill_order_id = o.order_id;
121  fill_account_id = o.account_id;
122  fill_pays_asset_id = o.pays.asset_id;
123  fill_pays_amount = o.pays.amount;
124  fill_receives_asset_id = o.receives.asset_id;
125  fill_receives_amount = o.receives.amount;
126  fill_fill_price = o.fill_price.to_real();
127  fill_is_maker = o.is_maker;
128  }
129 
130  template<typename T>
131  void operator()( const T& o )
132  {
133  fee_asset = o.fee.asset_id;
134  fee_amount = o.fee.amount;
135  }
136 };
137 
141  std::string operation_result;
143  std::string op;
145 };
146 
147 struct block_struct {
150  std::string trx_id;
151 };
152 
153 struct fee_struct {
154  asset_id_type asset;
155  std::string asset_name;
157  double amount_units;
158 };
159 
161  asset_id_type asset;
162  std::string asset_name;
164  double amount_units;
165  account_id_type from;
166  account_id_type to;
167 };
168 
169 struct fill_struct {
170  object_id_type order_id;
171  account_id_type account_id;
172  asset_id_type pays_asset_id;
173  std::string pays_asset_name;
176  asset_id_type receives_asset_id;
177  std::string receives_asset_name;
180  double fill_price;
182  bool is_maker;
183 };
184 
189 };
190 
191 struct bulk_struct {
198 };
199 
202  {
204  vector<string> keys_to_rename;
205  for (auto i = o.begin(); i != o.end(); ++i)
206  {
207  auto& element = (*i).value();
208  if (element.is_object())
209  {
210  const string& name = (*i).key();
211  auto& vo = element.get_object();
212  if (vo.contains(name.c_str()))
213  keys_to_rename.emplace_back(name);
214  element = adapt(vo);
215  }
216  else if (element.is_array())
217  adapt(element.get_array());
218  }
219  for (const auto& i : keys_to_rename)
220  {
221  string new_name = i + "_";
222  o[new_name] = variant(o[i]);
223  o.erase(i);
224  }
225 
226  if (o.find("memo") != o.end())
227  {
228  auto& memo = o["memo"];
229  if (memo.is_string())
230  {
231  o["memo_"] = o["memo"];
232  o.erase("memo");
233  }
234  else if (memo.is_object())
235  {
236  fc::mutable_variant_object tmp(memo.get_object());
237  if (tmp.find("nonce") != tmp.end())
238  {
239  tmp["nonce"] = tmp["nonce"].as_string();
240  o["memo"] = tmp;
241  }
242  }
243  }
244  if (o.find("new_parameters") != o.end())
245  {
246  auto& tmp = o["new_parameters"];
247  if (tmp.is_object())
248  {
249  fc::mutable_variant_object tmp2(tmp.get_object());
250  if (tmp2.find("current_fees") != tmp2.end())
251  {
252  tmp2.erase("current_fees");
253  o["new_parameters"] = tmp2;
254  }
255  }
256  }
257  if (o.find("owner") != o.end() && o["owner"].is_string())
258  {
259  o["owner_"] = o["owner"].as_string();
260  o.erase("owner");
261  }
262  if (o.find("proposed_ops") != o.end())
263  {
264  o["proposed_ops"] = fc::json::to_string(o["proposed_ops"]);
265  }
266  if (o.find("initializer") != o.end())
267  {
268  o["initializer"] = fc::json::to_string(o["initializer"]);
269  }
270  if (o.find("policy") != o.end())
271  {
272  o["policy"] = fc::json::to_string(o["policy"]);
273  }
274  if (o.find("predicates") != o.end())
275  {
276  o["predicates"] = fc::json::to_string(o["predicates"]);
277  }
278  if (o.find("active_special_authority") != o.end())
279  {
280  o["active_special_authority"] = fc::json::to_string(o["active_special_authority"]);
281  }
282  if (o.find("owner_special_authority") != o.end())
283  {
284  o["owner_special_authority"] = fc::json::to_string(o["owner_special_authority"]);
285  }
286 
287 
288  variant v;
290  return v;
291  }
292 
294  {
295  for (auto& array_element : v)
296  {
297  if (array_element.is_object())
298  array_element = adapt(array_element.get_object());
299  else if (array_element.is_array())
300  adapt(array_element.get_array());
301  else
302  array_element = array_element.as_string();
303  }
304  }
305 };
306 
307 } } //graphene::elasticsearch
308 
310 FC_REFLECT( graphene::elasticsearch::operation_history_struct, (trx_in_block)(op_in_trx)(operation_result)(virtual_op)(op)(op_object) )
311 FC_REFLECT( graphene::elasticsearch::block_struct, (block_num)(block_time)(trx_id) )
312 FC_REFLECT( graphene::elasticsearch::fee_struct, (asset)(asset_name)(amount)(amount_units) )
313 FC_REFLECT( graphene::elasticsearch::transfer_struct, (asset)(asset_name)(amount)(amount_units)(from)(to) )
314 FC_REFLECT( graphene::elasticsearch::fill_struct, (order_id)(account_id)(pays_asset_id)(pays_asset_name)(pays_amount)(pays_amount_units)
315  (receives_asset_id)(receives_asset_name)(receives_amount)(receives_amount_units)(fill_price)
316  (fill_price_units)(is_maker))
317 FC_REFLECT( graphene::elasticsearch::visitor_struct, (fee_data)(transfer_data)(fill_data) )
318 FC_REFLECT( graphene::elasticsearch::bulk_struct, (account_history)(operation_history)(operation_type)(operation_id_num)(block_data)(additional_data) )
static string to_string(const variant &v, output_formatting format=stringify_large_ints_and_doubles, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition: json.cpp:638
void erase(const string &key)
std::unique_ptr< detail::elasticsearch_plugin_impl > my
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
tracks the history of all logical operations on blockchain stateAll operations and virtual operations...
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:56
void operator()(const graphene::chain::fill_order_operation &o)
optional< visitor_struct > additional_data
a node in a linked list of operation_history_objectsAccount history is important for users and wallet...
std::vector< variant > variants
Definition: variant.hpp:170
const auto source
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
FC_REFLECT_ENUM(graphene::net::core_message_type_enum,(trx_message_type)(block_message_type)(core_message_type_first)(item_ids_inventory_message_type)(blockchain_item_ids_inventory_message_type)(fetch_blockchain_item_ids_message_type)(fetch_items_message_type)(item_not_available_message_type)(hello_message_type)(connection_accepted_message_type)(connection_rejected_message_type)(address_request_message_type)(address_message_type)(closing_connection_message_type)(current_time_request_message_type)(current_time_reply_message_type)(check_firewall_message_type)(check_firewall_reply_message_type)(get_current_connections_request_message_type)(get_current_connections_reply_message_type)(core_message_type_last))(different_chain)(already_connected)(connected_to_self)(not_accepting_connections)(blocked)(invalid_hello_message)(client_too_old))(inbound)(outbound))(firewalled)(not_firewalled))(unable_to_connect)(connection_successful)) namespace std
Transfers an amount of one asset from one account to another.
Definition: transfer.hpp:45
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
account_transaction_history_object account_history
iterator find(const string &key) const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
asset amount
The amount of asset to transfer from from to to.
Definition: transfer.hpp:58
void operator()(const graphene::chain::transfer_operation &o)
safe< int64_t > share_type
Definition: types.hpp:247
asset_id_type asset_id
Definition: asset.hpp:39
account_id_type from
Account to transfer asset from.
Definition: transfer.hpp:54
#define FC_PACK_MAX_DEPTH
Definition: config.hpp:3
return fromEStoOperation(source)
double to_real() const
Definition: asset.hpp:133
account_id_type to
Account to transfer asset to.
Definition: transfer.hpp:56
An order-perserving dictionary of variant&#39;s.
variant adapt(const variant_object &op)