BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
wallet_network.cpp
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 #include "wallet_api_impl.hpp"
25 
26 /****
27  * Methods to handle network / debug / debug_node
28  */
29 
30 namespace graphene { namespace wallet { namespace detail {
31 
32  void wallet_api_impl::network_add_nodes( const vector<string>& nodes )
33  {
35  for( const string& node_address : nodes )
36  {
37  (*_remote_net_node)->add_node( fc::ip::endpoint::from_string( node_address ) );
38  }
39  }
40 
42  {
44  const auto peers = (*_remote_net_node)->get_connected_peers();
45  vector< variant > result;
46  result.reserve( peers.size() );
47  for( const auto& peer : peers )
48  {
49  variant v;
51  result.push_back( v );
52  }
53  return result;
54  }
55 
56  void wallet_api_impl::flood_network(string prefix, uint32_t number_of_transactions)
57  {
58  try
59  {
60  const account_object& master = *_wallet.my_accounts.get<by_name>().lower_bound("import");
61  int number_of_accounts = number_of_transactions / 3;
62  number_of_transactions -= number_of_accounts;
63  try {
64  dbg_make_uia(master.name, "SHILL");
65  } catch(...) {/* Ignore; the asset probably already exists.*/}
66 
68  for( int i = 0; i < number_of_accounts; ++i )
69  {
70  std::ostringstream brain_key;
71  brain_key << "brain key for account " << prefix << i;
73  brain_key.str(), prefix + fc::to_string(i), master.name, master.name,
74  /* broadcast = */ true, /* save wallet = */ false);
75  }
77  ilog("Created ${n} accounts in ${time} milliseconds",
78  ("n", number_of_accounts)("time", (end - start).count() / 1000));
79 
80  start = fc::time_point::now();
81  for( int i = 0; i < number_of_accounts; ++i )
82  {
83  signed_transaction trx = transfer(master.name, prefix + fc::to_string(i), "10", "CORE", "", true);
84  trx = transfer(master.name, prefix + fc::to_string(i), "1", "CORE", "", true);
85  }
86  end = fc::time_point::now();
87  ilog("Transferred to ${n} accounts in ${time} milliseconds",
88  ("n", number_of_accounts*2)("time", (end - start).count() / 1000));
89 
90  start = fc::time_point::now();
91  for( int i = 0; i < number_of_accounts; ++i )
92  {
93  signed_transaction trx = issue_asset(prefix + fc::to_string(i), "1000", "SHILL", "", true);
94  }
95  end = fc::time_point::now();
96  ilog("Issued to ${n} accounts in ${time} milliseconds",
97  ("n", number_of_accounts)("time", (end - start).count() / 1000));
98  }
99  catch (...)
100  {
101  throw;
102  }
103 
104  }
105 
106  pair<transaction_id_type,signed_transaction> wallet_api_impl::broadcast_transaction(signed_transaction tx)
107  {
108  try {
109  _remote_net_broadcast->broadcast_transaction(tx);
110  }
111  catch (const fc::exception& e) {
112  elog("Caught exception while broadcasting tx ${id}: ${e}",
113  ("id", tx.id().str())("e", e.to_detail_string()));
114  throw;
115  }
116  return std::make_pair(tx.id(),tx);
117  }
118 
120  {
121  if( _remote_net_node )
122  return;
123  try
124  {
125  _remote_net_node = _remote_api->network_node();
126  }
127  catch( const fc::exception& e )
128  {
129  std::cerr << "\nCouldn't get network node API. You probably are not configured\n"
130  "to access the network API on the witness_node you are\n"
131  "connecting to. Please follow the instructions in README.md to set up an apiaccess file.\n"
132  "\n";
133  throw;
134  }
135  }
136 
137 }}} // namespace graphene::wallet::detail
graphene::wallet::detail::wallet_api_impl::issue_asset
signed_transaction issue_asset(string to_account, string amount, string symbol, string memo, bool broadcast=false)
Definition: wallet_asset.cpp:324
fc::exception
Used to generate a useful error report when an exception is thrown.
Definition: exception.hpp:56
fc::to_string
std::string to_string(double)
Definition: string.cpp:73
graphene::protocol::transaction::id
virtual const transaction_id_type & id() const
Definition: transaction.cpp:70
fc::ip::endpoint::from_string
static endpoint from_string(const string &s)
Definition: ip.cpp:81
wallet_api_impl.hpp
graphene::wallet::detail::wallet_api_impl::_remote_api
fc::api< login_api > _remote_api
Definition: wallet_api_impl.hpp:413
graphene::wallet::detail::wallet_api_impl::use_network_node_api
void use_network_node_api()
Definition: wallet_network.cpp:125
graphene::protocol::signed_transaction
adds a signature to a transaction
Definition: transaction.hpp:134
ilog
#define ilog(FORMAT,...)
Definition: logger.hpp:117
graphene::wallet::detail::wallet_api_impl::dbg_make_uia
void dbg_make_uia(string creator, string symbol)
Definition: wallet_debug.cpp:28
graphene::chain::account_object
This class represents an account on the object graph.
Definition: account_object.hpp:180
graphene::wallet::detail::wallet_api_impl::network_get_connected_peers
vector< variant > network_get_connected_peers()
Definition: wallet_network.cpp:47
fc::ripemd160::str
string str() const
Definition: ripemd160.cpp:21
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::exception::to_detail_string
std::string to_detail_string(log_level ll=log_level::all) const
Definition: exception.cpp:183
graphene::wallet::wallet_data::my_accounts
account_multi_index_type my_accounts
Definition: wallet_structs.hpp:154
fc::cerr
cerr_t & cerr
Definition: iostream.cpp:176
graphene::wallet::detail::wallet_api_impl::create_account_with_brain_key
signed_transaction create_account_with_brain_key(string brain_key, string account_name, string registrar_account, string referrer_account, bool broadcast=false, bool save_wallet=true)
Definition: wallet_account.cpp:101
graphene::wallet::detail::wallet_api_impl::network_add_nodes
void network_add_nodes(const vector< string > &nodes)
Definition: wallet_network.cpp:38
graphene::wallet::detail::wallet_api_impl::_wallet
wallet_data _wallet
Definition: wallet_api_impl.hpp:407
fc::time_point::now
static time_point now()
Definition: time.cpp:13
graphene::chain::account_object::name
string name
The account's name. This name must be unique among all account names on the graph....
Definition: account_object.hpp:209
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
graphene::wallet::detail::wallet_api_impl::_remote_net_broadcast
fc::api< network_broadcast_api > _remote_net_broadcast
Definition: wallet_api_impl.hpp:415
fc::time_point
Definition: time.hpp:44
graphene::wallet::detail::wallet_api_impl::transfer
signed_transaction transfer(string from, string to, string amount, string asset_symbol, string memo, bool broadcast=false)
Definition: wallet_transfer.cpp:57
GRAPHENE_MAX_NESTED_OBJECTS
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:33
graphene::wallet::detail::wallet_api_impl::flood_network
void flood_network(string prefix, uint32_t number_of_transactions)
Definition: wallet_network.cpp:62
graphene::wallet::detail::wallet_api_impl::_remote_net_node
optional< fc::api< network_node_api > > _remote_net_node
Definition: wallet_api_impl.hpp:418
graphene::wallet::detail::wallet_api_impl::broadcast_transaction
pair< transaction_id_type, signed_transaction > broadcast_transaction(signed_transaction tx)
Definition: wallet_network.cpp:112
graphene
Definition: api.cpp:48
elog
#define elog(FORMAT,...)
Definition: logger.hpp:129