-
Notifications
You must be signed in to change notification settings - Fork 643
/
core_messages.hpp
503 lines (439 loc) · 18.5 KB
/
core_messages.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
#include <graphene/net/config.hpp>
#include <fc/crypto/ripemd160.hpp>
#include <fc/crypto/elliptic.hpp>
#include <fc/crypto/sha256.hpp>
#include <fc/network/ip.hpp>
#include <fc/time.hpp>
#include <fc/variant_object.hpp>
#include <fc/exception/exception.hpp>
#include <fc/io/enum_type.hpp>
#include <graphene/protocol/block.hpp>
#include <vector>
namespace graphene { namespace net {
using graphene::protocol::signed_transaction;
using graphene::protocol::block_id_type;
using graphene::protocol::transaction_id_type;
using graphene::protocol::signed_block;
typedef fc::ecc::public_key_data node_id_t;
typedef fc::ripemd160 item_hash_t;
struct item_id
{
uint32_t item_type;
item_hash_t item_hash;
item_id() {}
item_id(uint32_t type, const item_hash_t& hash) :
item_type(type),
item_hash(hash)
{}
bool operator==(const item_id& other) const
{
return item_type == other.item_type &&
item_hash == other.item_hash;
}
};
enum core_message_type_enum
{
trx_message_type = 1000,
block_message_type = 1001,
core_message_type_first = 5000,
item_ids_inventory_message_type = 5001,
blockchain_item_ids_inventory_message_type = 5002,
fetch_blockchain_item_ids_message_type = 5003,
fetch_items_message_type = 5004,
item_not_available_message_type = 5005,
hello_message_type = 5006,
connection_accepted_message_type = 5007,
connection_rejected_message_type = 5008,
address_request_message_type = 5009,
address_message_type = 5010,
closing_connection_message_type = 5011,
current_time_request_message_type = 5012,
current_time_reply_message_type = 5013,
check_firewall_message_type = 5014,
check_firewall_reply_message_type = 5015,
get_current_connections_request_message_type = 5016,
get_current_connections_reply_message_type = 5017,
core_message_type_last = 5099
};
const uint32_t core_protocol_version = GRAPHENE_NET_PROTOCOL_VERSION;
struct trx_message
{
static const core_message_type_enum type;
graphene::protocol::precomputable_transaction trx;
trx_message() {}
trx_message(graphene::protocol::signed_transaction transaction) :
trx(std::move(transaction))
{}
};
struct block_message
{
static const core_message_type_enum type;
block_message(){}
block_message(const signed_block& blk )
:block(blk),block_id(blk.id()){}
signed_block block;
block_id_type block_id;
};
struct item_ids_inventory_message
{
static const core_message_type_enum type;
uint32_t item_type;
std::vector<item_hash_t> item_hashes_available;
item_ids_inventory_message() {}
item_ids_inventory_message(uint32_t item_type, const std::vector<item_hash_t>& item_hashes_available) :
item_type(item_type),
item_hashes_available(item_hashes_available)
{}
};
struct blockchain_item_ids_inventory_message
{
static const core_message_type_enum type;
uint32_t total_remaining_item_count;
uint32_t item_type;
std::vector<item_hash_t> item_hashes_available;
blockchain_item_ids_inventory_message() {}
blockchain_item_ids_inventory_message(uint32_t total_remaining_item_count,
uint32_t item_type,
const std::vector<item_hash_t>& item_hashes_available) :
total_remaining_item_count(total_remaining_item_count),
item_type(item_type),
item_hashes_available(item_hashes_available)
{}
};
struct fetch_blockchain_item_ids_message
{
static const core_message_type_enum type;
uint32_t item_type;
std::vector<item_hash_t> blockchain_synopsis;
fetch_blockchain_item_ids_message() {}
fetch_blockchain_item_ids_message(uint32_t item_type, const std::vector<item_hash_t>& blockchain_synopsis) :
item_type(item_type),
blockchain_synopsis(blockchain_synopsis)
{}
};
struct fetch_items_message
{
static const core_message_type_enum type;
uint32_t item_type;
std::vector<item_hash_t> items_to_fetch;
fetch_items_message() {}
fetch_items_message(uint32_t item_type, const std::vector<item_hash_t>& items_to_fetch) :
item_type(item_type),
items_to_fetch(items_to_fetch)
{}
};
struct item_not_available_message
{
static const core_message_type_enum type;
item_id requested_item;
item_not_available_message() {}
item_not_available_message(const item_id& requested_item) :
requested_item(requested_item)
{}
};
struct hello_message
{
static const core_message_type_enum type;
std::string user_agent;
uint32_t core_protocol_version;
fc::ip::address inbound_address;
uint16_t inbound_port;
uint16_t outbound_port;
node_id_t node_public_key;
fc::ecc::compact_signature signed_shared_secret;
fc::sha256 chain_id;
fc::variant_object user_data;
hello_message() {}
hello_message(const std::string& user_agent,
uint32_t core_protocol_version,
const fc::ip::address& inbound_address,
uint16_t inbound_port,
uint16_t outbound_port,
const node_id_t& node_public_key,
const fc::ecc::compact_signature& signed_shared_secret,
const fc::sha256& chain_id_arg,
const fc::variant_object& user_data ) :
user_agent(user_agent),
core_protocol_version(core_protocol_version),
inbound_address(inbound_address),
inbound_port(inbound_port),
outbound_port(outbound_port),
node_public_key(node_public_key),
signed_shared_secret(signed_shared_secret),
chain_id(chain_id_arg),
user_data(user_data)
{}
};
struct connection_accepted_message
{
static const core_message_type_enum type;
connection_accepted_message() {}
};
enum class rejection_reason_code { unspecified,
different_chain,
already_connected,
connected_to_self,
not_accepting_connections,
blocked,
invalid_hello_message,
client_too_old };
struct connection_rejected_message
{
static const core_message_type_enum type;
std::string user_agent;
uint32_t core_protocol_version;
fc::ip::endpoint remote_endpoint;
std::string reason_string;
fc::enum_type<uint8_t, rejection_reason_code> reason_code;
connection_rejected_message() {}
connection_rejected_message(const std::string& user_agent, uint32_t core_protocol_version,
const fc::ip::endpoint& remote_endpoint, rejection_reason_code reason_code,
const std::string& reason_string) :
user_agent(user_agent),
core_protocol_version(core_protocol_version),
remote_endpoint(remote_endpoint),
reason_string(reason_string),
reason_code(reason_code)
{}
};
struct address_request_message
{
static const core_message_type_enum type;
address_request_message() {}
};
enum class peer_connection_direction { unknown, inbound, outbound };
enum class firewalled_state { unknown, firewalled, not_firewalled };
struct address_info
{
fc::ip::endpoint remote_endpoint;
fc::time_point_sec last_seen_time;
fc::microseconds latency;
node_id_t node_id;
fc::enum_type<uint8_t, peer_connection_direction> direction;
fc::enum_type<uint8_t, firewalled_state> firewalled;
address_info() {}
address_info(const fc::ip::endpoint& remote_endpoint,
const fc::time_point_sec last_seen_time,
const fc::microseconds latency,
const node_id_t& node_id,
peer_connection_direction direction,
firewalled_state firewalled) :
remote_endpoint(remote_endpoint),
last_seen_time(last_seen_time),
latency(latency),
node_id(node_id),
direction(direction),
firewalled(firewalled)
{}
};
struct address_message
{
static const core_message_type_enum type;
std::vector<address_info> addresses;
};
struct closing_connection_message
{
static const core_message_type_enum type;
std::string reason_for_closing;
bool closing_due_to_error;
fc::oexception error;
closing_connection_message() : closing_due_to_error(false) {}
closing_connection_message(const std::string& reason_for_closing,
bool closing_due_to_error = false,
const fc::oexception& error = fc::oexception()) :
reason_for_closing(reason_for_closing),
closing_due_to_error(closing_due_to_error),
error(error)
{}
};
struct current_time_request_message
{
static const core_message_type_enum type;
fc::time_point request_sent_time;
current_time_request_message(){}
current_time_request_message(const fc::time_point request_sent_time) :
request_sent_time(request_sent_time)
{}
};
struct current_time_reply_message
{
static const core_message_type_enum type;
fc::time_point request_sent_time;
fc::time_point request_received_time;
fc::time_point reply_transmitted_time;
current_time_reply_message(){}
current_time_reply_message(const fc::time_point request_sent_time,
const fc::time_point request_received_time,
const fc::time_point reply_transmitted_time = fc::time_point()) :
request_sent_time(request_sent_time),
request_received_time(request_received_time),
reply_transmitted_time(reply_transmitted_time)
{}
};
struct check_firewall_message
{
static const core_message_type_enum type;
node_id_t node_id;
fc::ip::endpoint endpoint_to_check;
};
enum class firewall_check_result
{
unable_to_check,
unable_to_connect,
connection_successful
};
struct check_firewall_reply_message
{
static const core_message_type_enum type;
node_id_t node_id;
fc::ip::endpoint endpoint_checked;
fc::enum_type<uint8_t, firewall_check_result> result;
};
struct get_current_connections_request_message
{
static const core_message_type_enum type;
};
struct current_connection_data
{
uint32_t connection_duration; // in seconds
fc::ip::endpoint remote_endpoint;
node_id_t node_id;
fc::microseconds clock_offset;
fc::microseconds round_trip_delay;
fc::enum_type<uint8_t, peer_connection_direction> connection_direction;
fc::enum_type<uint8_t, firewalled_state> firewalled;
fc::variant_object user_data;
};
struct get_current_connections_reply_message
{
static const core_message_type_enum type;
uint32_t upload_rate_one_minute;
uint32_t download_rate_one_minute;
uint32_t upload_rate_fifteen_minutes;
uint32_t download_rate_fifteen_minutes;
uint32_t upload_rate_one_hour;
uint32_t download_rate_one_hour;
std::vector<current_connection_data> current_connections;
};
} } // graphene::net
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) )
FC_REFLECT_ENUM(graphene::net::rejection_reason_code, (unspecified)
(different_chain)
(already_connected)
(connected_to_self)
(not_accepting_connections)
(blocked)
(invalid_hello_message)
(client_too_old))
FC_REFLECT_ENUM(graphene::net::peer_connection_direction, (unknown)
(inbound)
(outbound))
FC_REFLECT_ENUM(graphene::net::firewalled_state, (unknown)
(firewalled)
(not_firewalled))
FC_REFLECT_ENUM(graphene::net::firewall_check_result, (unable_to_check)
(unable_to_connect)
(connection_successful))
FC_REFLECT_TYPENAME( graphene::net::trx_message )
FC_REFLECT_TYPENAME( graphene::net::block_message )
FC_REFLECT_TYPENAME( graphene::net::item_id )
FC_REFLECT_TYPENAME( graphene::net::item_ids_inventory_message )
FC_REFLECT_TYPENAME( graphene::net::blockchain_item_ids_inventory_message )
FC_REFLECT_TYPENAME( graphene::net::fetch_blockchain_item_ids_message )
FC_REFLECT_TYPENAME( graphene::net::fetch_items_message )
FC_REFLECT_TYPENAME( graphene::net::item_not_available_message )
FC_REFLECT_TYPENAME( graphene::net::hello_message )
FC_REFLECT_TYPENAME( graphene::net::connection_accepted_message )
FC_REFLECT_TYPENAME( graphene::net::connection_rejected_message )
FC_REFLECT_TYPENAME( graphene::net::address_request_message )
FC_REFLECT_TYPENAME( graphene::net::address_info )
FC_REFLECT_TYPENAME( graphene::net::address_message )
FC_REFLECT_TYPENAME( graphene::net::closing_connection_message )
FC_REFLECT_TYPENAME( graphene::net::current_time_request_message )
FC_REFLECT_TYPENAME( graphene::net::current_time_reply_message )
FC_REFLECT_TYPENAME( graphene::net::check_firewall_message )
FC_REFLECT_TYPENAME( graphene::net::check_firewall_reply_message )
FC_REFLECT_TYPENAME( graphene::net::get_current_connections_request_message )
FC_REFLECT_TYPENAME( graphene::net::current_connection_data )
FC_REFLECT_TYPENAME( graphene::net::get_current_connections_reply_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::trx_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::block_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::item_id )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::item_ids_inventory_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::blockchain_item_ids_inventory_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::fetch_blockchain_item_ids_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::fetch_items_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::item_not_available_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::hello_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::connection_accepted_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::connection_rejected_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::address_request_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::address_info )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::address_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::closing_connection_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::current_time_request_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::current_time_reply_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::check_firewall_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::check_firewall_reply_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::get_current_connections_request_message )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::current_connection_data )
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION( graphene::net::get_current_connections_reply_message )
#include <unordered_map>
#include <fc/crypto/city.hpp>
#include <fc/crypto/sha224.hpp>
namespace std
{
template<>
struct hash<graphene::net::item_id>
{
size_t operator()(const graphene::net::item_id& item_to_hash) const
{
return fc::city_hash_size_t((char*)&item_to_hash, sizeof(item_to_hash));
}
};
}