Scorum
api.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 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 
29 
30 #include <graphene/net/node.hpp>
31 
32 #include <fc/api.hpp>
33 #include <fc/optional.hpp>
34 #include <fc/crypto/elliptic.hpp>
35 #include <fc/network/ip.hpp>
36 
37 #include <boost/container/flat_set.hpp>
38 
39 #include <functional>
40 #include <map>
41 #include <string>
42 #include <vector>
43 
44 namespace scorum {
45 namespace app {
46 using namespace scorum::chain;
47 using namespace fc::ecc;
48 
49 class application;
50 struct api_context;
51 
55 class network_broadcast_api : public std::enable_shared_from_this<network_broadcast_api>
56 {
57 public:
59 
61  {
62  transaction_confirmation(transaction_id_type txid, int32_t bn, int32_t tn, bool ex)
63  : id(txid)
64  , block_num(bn)
65  , trx_num(tn)
66  , expired(ex)
67  {
68  }
69 
71  int32_t block_num = 0;
72  int32_t trx_num = 0;
73  bool expired = false;
74  };
75 
76  typedef std::function<void(variant /*transaction_confirmation*/)> confirmation_callback;
77 
85  void broadcast_transaction(const signed_transaction& trx);
86 
92  void broadcast_transaction_with_callback(confirmation_callback cb, const signed_transaction& trx);
93 
97  fc::variant broadcast_transaction_synchronous(const signed_transaction& trx);
98 
99  void broadcast_block(const signed_block& block);
100 
101  void set_max_block_age(int32_t max_block_age);
102 
103  // implementation detail, not reflected
104  bool check_max_block_age(int32_t max_block_age);
105 
114  void on_applied_block(const signed_block& b);
115 
117  void on_api_startup();
118 
119 private:
120  boost::signals2::scoped_connection _applied_block_connection;
121 
122  std::map<transaction_id_type, confirmation_callback> _callbacks;
123  std::map<time_point_sec, std::vector<transaction_id_type>> _callbacks_expirations;
124 
125  int32_t _max_block_age = -1;
126 
127  application& _app;
128 };
129 
134 {
135 public:
136  network_node_api(const api_context& a);
137 
141  fc::variant_object get_info() const;
142 
147  void add_node(const fc::ip::endpoint& ep);
148 
152  std::vector<graphene::net::peer_status> get_connected_peers() const;
153 
158  fc::variant_object get_advanced_node_parameters() const;
159 
165  void set_advanced_node_parameters(const fc::variant_object& params);
166 
170  std::vector<graphene::net::potential_peer_record> get_potential_peers() const;
171 
173  void on_api_startup();
174 
175 private:
176  application& _app;
177 };
178 
180 {
182  {
183  }
184  scorum_version_info(fc::string bc_v, fc::string s_v, fc::string fc_v)
185  : blockchain_version(bc_v)
186  , scorum_revision(s_v)
187  , fc_revision(fc_v)
188  {
189  }
190 
191  fc::string blockchain_version;
192  fc::string scorum_revision;
193  fc::string fc_revision;
194 };
195 
202 {
203 public:
204  login_api(const api_context& ctx);
205  virtual ~login_api();
206 
216  bool login(const std::string& user, const std::string& password);
217 
218  fc::api_ptr get_api_by_name(const std::string& api_name) const;
219 
220  scorum_version_info get_version();
221 
223  void on_api_startup();
224 
225 private:
226  api_context _ctx;
227 };
228 }
229 } // scorum::app
230 
232 FC_REFLECT(scorum::app::scorum_version_info, (blockchain_version)(scorum_revision)(fc_revision))
233 // FC_REFLECT_TYPENAME( fc::ecc::compact_signature );
234 // FC_REFLECT_TYPENAME( fc::ecc::commitment_type );
235 
237  (broadcast_transaction)(broadcast_transaction_with_callback)(broadcast_transaction_synchronous)(broadcast_block)(
238  set_max_block_age))
240  (get_info)(add_node)(get_connected_peers)(get_potential_peers)(get_advanced_node_parameters)(
241  set_advanced_node_parameters))
242 FC_API(scorum::app::login_api, (login)(get_api_by_name)(get_version))
The login_api class implements the bottom layer of the RPC API.
Definition: api.hpp:202
The network_broadcast_api class allows broadcasting of transactions.
Definition: api.hpp:56
std::function< void(variant)> confirmation_callback
Definition: api.hpp:76
The network_node_api class allows maintenance of p2p connections.
Definition: api.hpp:134
FC_REFLECT(appender_args,(appender)(stream)) FC_REFLECT_DERIVED(file_appender_args
fc::ripemd160 transaction_id_type
Definition: types.hpp:65
Definition: asset.cpp:15
transaction_confirmation(transaction_id_type txid, int32_t bn, int32_t tn, bool ex)
Definition: api.hpp:62
scorum_version_info(fc::string bc_v, fc::string s_v, fc::string fc_v)
Definition: api.hpp:184