BitShares-Core  5.0.0
BitShares blockchain implementation and command-line interface 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
virtual const transaction_id_type & id() const
Definition: transaction.cpp:70
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)
#define GRAPHENE_MAX_NESTED_OBJECTS
Definition: config.hpp:31
This class represents an account on the object graphAccounts are the primary unit of authority on the...
Definition: api.cpp:56
#define elog(FORMAT,...)
Definition: logger.hpp:129
signed_transaction transfer(string from, string to, string amount, string asset_symbol, string memo, bool broadcast=false)
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
std::string to_detail_string(log_level ll=log_level::all) const
Definition: exception.cpp:183
fc::api< network_broadcast_api > _remote_net_broadcast
optional< fc::api< network_node_api > > _remote_net_node
pair< transaction_id_type, signed_transaction > broadcast_transaction(signed_transaction tx)
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
string str() const
Definition: ripemd160.cpp:21
#define ilog(FORMAT,...)
Definition: logger.hpp:117
account_multi_index_type my_accounts
string name
The account&#39;s name. This name must be unique among all account names on the graph. May not be empty.
void network_add_nodes(const vector< string > &nodes)
adds a signature to a transaction
signed_transaction issue_asset(string to_account, string amount, string symbol, string memo, bool broadcast=false)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
static endpoint from_string(const string &s)
Definition: ip.cpp:74
std::string to_string(double)
Definition: string.cpp:73
static time_point now()
Definition: time.cpp:13
void flood_network(string prefix, uint32_t number_of_transactions)
void dbg_make_uia(string creator, string symbol)
cerr_t & cerr
Definition: iostream.cpp:176