-
Notifications
You must be signed in to change notification settings - Fork 8
/
btcnet.cpp
183 lines (169 loc) · 5.34 KB
/
btcnet.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
#include "btcnet.hpp"
#include <czmq++/czmqpp.hpp>
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;
void log_to_file(std::ofstream& file,
bc::log_level level, const std::string& domain, const std::string& body)
{
if (body.empty())
return;
file << level_repr(level);
if (!domain.empty())
file << " [" << domain << "]";
file << ": " << body << std::endl;
}
void log_to_both(std::ostream& device, std::ofstream& file,
bc::log_level level, const std::string& domain, const std::string& body)
{
if (body.empty())
return;
std::ostringstream output;
output << level_repr(level);
if (!domain.empty())
output << " [" << domain << "]";
output << ": " << body;
device << output.str() << std::endl;
file << output.str() << std::endl;
}
void output_file(std::ofstream& file, bc::log_level level,
const std::string& domain, const std::string& body)
{
log_to_file(file, level, domain, body);
}
void output_both(std::ofstream& file, bc::log_level level,
const std::string& domain, const std::string& body)
{
log_to_both(std::cout, file, level, domain, body);
}
void warning(std::ofstream& file, bc::log_level level,
const std::string& domain, const std::string& body)
{
if (domain == LOG_BRC)
log_to_both(std::cerr, file, level, domain, body);
else
log_to_file(file, level, domain, body);
}
void error(std::ofstream& file, bc::log_level level,
const std::string& domain, const std::string& body)
{
log_to_both(std::cerr, file, level, domain, body);
}
broadcaster::broadcaster()
: hosts_(pool_), handshake_(pool_), network_(pool_),
broadcast_p2p_(
pool_, hosts_, handshake_, network_)
{
}
void broadcaster::start(size_t threads,
size_t broadcast_hosts, start_handler handle_start)
{
outfile_.open("debug.log");
errfile_.open("error.log");
// Suppress noisy output.
bc::log_debug().set_output_function(
std::bind(output_file, std::ref(outfile_), _1, _2, _3));
bc::log_info().set_output_function(
std::bind(output_both, std::ref(outfile_), _1, _2, _3));
bc::log_warning().set_output_function(
std::bind(warning, std::ref(errfile_), _1, _2, _3));
bc::log_error().set_output_function(
std::bind(error, std::ref(errfile_), _1, _2, _3));
bc::log_fatal().set_output_function(
std::bind(error, std::ref(errfile_), _1, _2, _3));
// Begin initialization.
pool_.spawn(threads);
// Set connection counts.
broadcast_p2p_.set_max_outbound(broadcast_hosts);
// Start connecting to p2p network for broadcasting txs.
broadcast_p2p_.start(handle_start);
}
void broadcaster::stop()
{
pool_.stop();
pool_.join();
}
struct summary_stats
{
size_t current()
{
return success + failure;
}
size_t success = 0;
size_t failure = 0;
};
typedef std::shared_ptr<summary_stats> summary_stats_ptr;
void send_summary(const bc::hash_digest& tx_hash, const summary_stats& stats)
{
bc::log_info(LOG_BRC) << "Sending summary for: "
<< bc::encode_hash(tx_hash);
// Create ZMQ socket.
static czmqpp::context ctx;
static czmqpp::authenticator auth(ctx);
static czmqpp::socket socket(ctx, ZMQ_PUB);
static bool is_initialized = false;
if (!is_initialized)
{
#ifdef ONLY_LOCALHOST_CONNECTIONS
auth.allow("127.0.0.1");
#endif
bc::log_debug(LOG_BRC) << "Initializing summary socket.";
BITCOIN_ASSERT(ctx.self());
BITCOIN_ASSERT(socket.self());
int bind_rc = socket.bind(listen_transport(publish_summary_port));
BITCOIN_ASSERT(bind_rc != -1);
is_initialized = true;
sleep(1);
}
// Create a message.
czmqpp::message msg;
// tx_hash
const czmqpp::data_chunk data_hash(tx_hash.begin(), tx_hash.end());
msg.append(data_hash);
// Success
const auto data_success = bc::to_little_endian(stats.success);
msg.append(bc::to_data_chunk(data_success));
// Failure
const auto data_failure = bc::to_little_endian(stats.failure);
msg.append(bc::to_data_chunk(data_failure));
// Send it.
bool rc = msg.send(socket);
BITCOIN_ASSERT(rc);
}
bool broadcaster::broadcast(const bc::data_chunk& raw_tx)
{
bc::transaction_type tx;
try
{
satoshi_load(raw_tx.begin(), raw_tx.end(), tx);
}
catch (bc::end_of_stream)
{
bc::log_warning(LOG_BRC) << "Bad stream.";
return false;
}
bc::hash_digest tx_hash = bc::hash_transaction(tx);
bc::log_info(LOG_BRC) << "Broadcasting: " << bc::encode_hash(tx_hash);
// Summary stats
summary_stats_ptr stats = std::make_shared<summary_stats>();
// We can ignore the send since we have connections to monitor
// from the network and resend if neccessary anyway.
auto tally_send = [tx_hash, stats](
const std::error_code& ec, size_t total_connections)
{
// Increment respective counters for summary statistics.
if (ec)
++stats->failure;
else
++stats->success;
// Once reach the end, send out summary information.
if (stats->current() == total_connections)
send_summary(tx_hash, *stats);
};
broadcast_p2p_.broadcast(tx, tally_send);
return true;
}
size_t broadcaster::total_connections() const
{
return broadcast_p2p_.total_connections();
}