forked from qubic/qubic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.cpp
More file actions
412 lines (373 loc) · 18 KB
/
connection.cpp
File metadata and controls
412 lines (373 loc) · 18 KB
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
#ifdef _MSC_VER
#pragma comment(lib, "Ws2_32.lib")
#include <Winsock2.h>
#include <Ws2tcpip.h>
#define close(x) closesocket(x)
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include <cstring>
#include <string>
#include <stdexcept>
#include "connection.h"
#include "logger.h"
#include "structs.h"
// includes for template instantiations
#include "quottery.h"
#include "qx_struct.h"
#include "qvault.h"
#include "qearn.h"
#include "msvault.h"
#include "qswap_struct.h"
#include "test_utils.h"
#include "nostromo.h"
#include "qutil.h"
#include "qbond.h"
#define DEFAULT_TIMEOUT_MSEC 1000
#ifdef _MSC_VER
static bool setTimeout(int serverSocket, int optName, unsigned long milliseconds)
{
DWORD tv = milliseconds;
if (setsockopt(serverSocket, SOL_SOCKET, optName, (const char*)&tv, sizeof tv) != 0)
{
LOG("setsockopt failed with error: %d\n", WSAGetLastError());
return false;
}
return true;
}
static int connect(const char* nodeIp, int nodePort)
{
WSADATA wsa_data;
WSAStartup(MAKEWORD(2, 0), &wsa_data);
int serverSocket = int(socket(AF_INET, SOCK_STREAM, 0));
if (!setTimeout(serverSocket, SO_RCVTIMEO, DEFAULT_TIMEOUT_MSEC))
return -1;
if (!setTimeout(serverSocket, SO_SNDTIMEO, DEFAULT_TIMEOUT_MSEC))
return -1;
sockaddr_in addr;
memset((char*)&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(nodePort);
if (inet_pton(AF_INET, nodeIp, &addr.sin_addr) <= 0)
{
LOG("Error translating command line ip address to usable one.");
return -1;
}
int res = connect(serverSocket, (const sockaddr*)&addr, sizeof(addr));
if (res < 0)
{
LOG("Failed to connect %s | error %d\n", nodeIp, res);
return -1;
}
return serverSocket;
}
#else
static bool setTimeout(int serverSocket, int optName, unsigned long milliseconds)
{
struct timeval tv;
tv.tv_sec = milliseconds / 1000;
tv.tv_usec = (milliseconds % 1000) * 1000;
if (setsockopt(serverSocket, SOL_SOCKET, optName, (const char*)&tv, sizeof tv) != 0)
return false;
return true;
}
static int connect(const char* nodeIp, int nodePort)
{
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (!setTimeout(serverSocket, SO_RCVTIMEO, DEFAULT_TIMEOUT_MSEC))
return -1;
if (!setTimeout(serverSocket, SO_SNDTIMEO, DEFAULT_TIMEOUT_MSEC))
return -1;
sockaddr_in addr;
memset((char*)&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(nodePort);
if (inet_pton(AF_INET, nodeIp, &addr.sin_addr) <= 0)
{
LOG("Error translating command line ip address to usable one.");
return -1;
}
if (connect(serverSocket, (const sockaddr *)&addr, sizeof(addr)) < 0)
{
LOG("Failed to connect %s\n", nodeIp);
return -1;
}
return serverSocket;
}
#endif
QubicConnection::QubicConnection(const char* nodeIp, int nodePort)
{
memset(mNodeIp, 0, 32);
memcpy(mNodeIp, nodeIp, strlen(nodeIp));
mNodePort = nodePort;
mSocket = connect(nodeIp, nodePort);
if (mSocket < 0)
throw std::logic_error("Unable to establish connection.");
// receive handshake - exchange peer packets
mHandshakeData.resize(sizeof(ExchangePublicPeers));
uint8_t* data = mHandshakeData.data();
*((ExchangePublicPeers*)data) = receivePacketWithHeaderAs<ExchangePublicPeers>();
// If node has no ComputorList or a self-generated ComputorList it will requestComputor upon tcp initialization
// Ignore this message if it is here
// This waits for timeout if RequestComputors is not sent. Temporarily reduce timeout to reduce waiting time.
setTimeout(mSocket, SO_RCVTIMEO, DEFAULT_TIMEOUT_MSEC / 5);
try
{
*((RequestComputors*)data) = receivePacketWithHeaderAs<RequestComputors>();
}
catch(std::logic_error) {}
setTimeout(mSocket, SO_RCVTIMEO, DEFAULT_TIMEOUT_MSEC);
}
void QubicConnection::getHandshakeData(std::vector<uint8_t>& buffer)
{
buffer = mHandshakeData;
}
QubicConnection::~QubicConnection()
{
close(mSocket);
}
// Receive the requested number of bytes (sz) or less if sz bytes have not been received after timeout. Return number of received bytes.
int QubicConnection::receiveData(uint8_t* buffer, int sz)
{
int totalRecvSz = 0;
while (sz)
{
// Note that recv may return before sz bytes have been received, it only blocks until socket timeout if no
// data has been received!
// Linux manual page:
// "If no messages are available at the socket, the receive calls wait for a message to arrive [...]
// The receive calls normally return any data available, up to the requested amount,
// rather than waiting for receipt of the full amount requested."
// Microsoft docs:
// "For connection-oriented sockets (type SOCK_STREAM for example), calling recv will
// return as much data as is currently available up to the size of the buffer specified. [...]
// If no incoming data is available at the socket, the recv call blocks and waits for data to arrive [...]"
int recvSz = recv(mSocket, (char*)buffer + totalRecvSz, sz, 0);
if (recvSz <= 0)
{
// timeout, closed connection, or other error
break;
}
totalRecvSz += recvSz;
sz -= recvSz;
}
return totalRecvSz;
}
int QubicConnection::receiveAllDataOrThrowException(uint8_t* buffer, int sz)
{
int recvSz = receiveData(buffer, sz);
if (recvSz != sz)
{
throw std::logic_error("Received incomplete data! Expected " + std::to_string(sz) + " bytes, received " + std::to_string(recvSz) + " bytes");
}
return recvSz;
}
void QubicConnection::resolveConnection()
{
mSocket = connect(mNodeIp, mNodePort);
if (mSocket < 0)
throw std::logic_error("Unable to establish connection.");
}
// Receive the next qubic packet with a RequestResponseHeader that matches T
template <typename T>
T QubicConnection::receivePacketWithHeaderAs()
{
T result;
receivePacketWithHeaderAs(result);
return result;
}
// Receive the next qubic packet with a RequestResponseHeader that matches T
template <typename T>
void QubicConnection::receivePacketWithHeaderAs(T& result)
{
// first receive the header
RequestResponseHeader header;
int recvByte = -1, packetSize = -1, remainingSize = -1;
while (true)
{
recvByte = receiveData((uint8_t*)&header, sizeof(RequestResponseHeader));
if (recvByte != sizeof(RequestResponseHeader))
{
throw std::logic_error("No connection.");
}
if (header.type() == END_RESPOND)
{
throw EndResponseReceived();
}
if (header.type() != T::type())
{
// skip this packet and keep receiving
packetSize = header.size();
remainingSize = packetSize - sizeof(RequestResponseHeader);
receiveAllDataOrThrowException(mBuffer, remainingSize);
continue;
}
break;
}
packetSize = header.size();
remainingSize = packetSize - sizeof(RequestResponseHeader);
memset(&result, 0, sizeof(T));
if (remainingSize)
{
memset(mBuffer, 0, sizeof(T));
receiveAllDataOrThrowException(mBuffer, remainingSize);
result = *((T*)mBuffer);
}
}
// same as receivePacketWithHeaderAs but without the header
template <typename T>
T QubicConnection::receivePacketAs()
{
int packetSize = sizeof(T);
T result;
memset(&result, 0, sizeof(T));
int recvByte = receiveData(mBuffer, packetSize);
if (recvByte != packetSize)
{
throw std::logic_error("Unexpected data size.");
}
result = *((T*)mBuffer);
return result;
}
template <typename T>
std::vector<T> QubicConnection::getLatestVectorPacketAs()
{
std::vector<T> results;
while (true)
{
try
{
results.push_back(receivePacketWithHeaderAs<T>());
}
catch (EndResponseReceived)
{
break;
}
catch (std::logic_error& e)
{
LOG("%s\n", e.what());
break;
}
}
return results;
}
int QubicConnection::sendData(uint8_t* buffer, int sz)
{
// also skip printing packets of size 8 (typically used during the preparation step, not the final stage)
if (!std::string(g_printToScreen).empty() && sz != 8) {
std::string printType = g_printToScreen;
// Do not print the first 8 bytes (header)
printBytes(buffer + 8, sz - 8, printType);
// this operation may break the normal flow, we need to skip printing error messages to console
if (!std::freopen("/dev/null", "w", stdout)) {}
if (!std::freopen("/dev/null", "w", stderr)) {}
return 0;
} else {
int size = sz;
int numberOfBytes;
while (size)
{
if ((numberOfBytes = send(mSocket, (char*)buffer, size, 0)) <= 0)
{
return 0;
}
buffer += numberOfBytes;
size -= numberOfBytes;
}
return sz - size;
}
}
template SpecialCommand QubicConnection::receivePacketWithHeaderAs<SpecialCommand>();
template SpecialCommandToggleMainModeResquestAndResponse QubicConnection::receivePacketWithHeaderAs<SpecialCommandToggleMainModeResquestAndResponse>();
template SpecialCommandSetSolutionThresholdResquestAndResponse QubicConnection::receivePacketWithHeaderAs<SpecialCommandSetSolutionThresholdResquestAndResponse>();
template SpecialCommandSendTime QubicConnection::receivePacketWithHeaderAs<SpecialCommandSendTime>();
template SpecialCommandSetConsoleLoggingModeRequestAndResponse QubicConnection::receivePacketWithHeaderAs<SpecialCommandSetConsoleLoggingModeRequestAndResponse>();
template SpecialCommandSaveSnapshotRequestAndResponse QubicConnection::receivePacketWithHeaderAs<SpecialCommandSaveSnapshotRequestAndResponse>();
template GetSendToManyV1Fee_output QubicConnection::receivePacketWithHeaderAs<GetSendToManyV1Fee_output>();
template CurrentTickInfo QubicConnection::receivePacketWithHeaderAs<CurrentTickInfo>();
template CurrentSystemInfo QubicConnection::receivePacketWithHeaderAs<CurrentSystemInfo>();
template TickData QubicConnection::receivePacketWithHeaderAs<TickData>();
template RespondTxStatus QubicConnection::receivePacketWithHeaderAs<RespondTxStatus>();
template BroadcastComputors QubicConnection::receivePacketWithHeaderAs<BroadcastComputors>();
template RespondContractIPO QubicConnection::receivePacketWithHeaderAs<RespondContractIPO>();
template std::vector<RespondActiveIPO> QubicConnection::getLatestVectorPacketAs<RespondActiveIPO>();
template SpecialCommandExecutionFeeMultiplierRequestAndResponse QubicConnection::receivePacketWithHeaderAs<SpecialCommandExecutionFeeMultiplierRequestAndResponse>();
// QUOTTERY
template qtryBasicInfo_output QubicConnection::receivePacketWithHeaderAs<qtryBasicInfo_output>();
template getBetInfo_output QubicConnection::receivePacketWithHeaderAs<getBetInfo_output>();
template getBetOptionDetail_output QubicConnection::receivePacketWithHeaderAs<getBetOptionDetail_output>();
template getActiveBet_output QubicConnection::receivePacketWithHeaderAs<getActiveBet_output>();
template getActiveBetByCreator_output QubicConnection::receivePacketWithHeaderAs<getActiveBetByCreator_output>();
// QX
template QxFees_output QubicConnection::receivePacketWithHeaderAs<QxFees_output>();
template qxGetAssetOrder_output QubicConnection::receivePacketWithHeaderAs<qxGetAssetOrder_output>();
template qxGetEntityOrder_output QubicConnection::receivePacketWithHeaderAs<qxGetEntityOrder_output>();
// QVAULT
template QVaultGetData_output QubicConnection::receivePacketWithHeaderAs<QVaultGetData_output>();
// QEARN
template QEarnGetLockInfoPerEpoch_output QubicConnection::receivePacketWithHeaderAs<QEarnGetLockInfoPerEpoch_output>();
template QEarnGetUserLockedInfo_output QubicConnection::receivePacketWithHeaderAs<QEarnGetUserLockedInfo_output>();
template QEarnGetStateOfRound_output QubicConnection::receivePacketWithHeaderAs<QEarnGetStateOfRound_output>();
template QEarnGetUserLockStatus_output QubicConnection::receivePacketWithHeaderAs<QEarnGetUserLockStatus_output>();
template QEarnGetStatsPerEpoch_output QubicConnection::receivePacketWithHeaderAs<QEarnGetStatsPerEpoch_output>();
template QEarnGetEndedStatus_output QubicConnection::receivePacketWithHeaderAs<QEarnGetEndedStatus_output>();
template QEarnGetBurnedAndBoostedStats_output QubicConnection::receivePacketWithHeaderAs<QEarnGetBurnedAndBoostedStats_output>();
template QEarnGetBurnedAndBoostedStatsPerEpoch_output QubicConnection::receivePacketWithHeaderAs<QEarnGetBurnedAndBoostedStatsPerEpoch_output>();
// QSWAP
template QswapFees_output QubicConnection::receivePacketWithHeaderAs<QswapFees_output>();
template qswapGetLiquidityOf_output QubicConnection::receivePacketWithHeaderAs<qswapGetLiquidityOf_output>();
template qswapGetPoolBasicState_output QubicConnection::receivePacketWithHeaderAs<qswapGetPoolBasicState_output>();
template qswapQuote_output QubicConnection::receivePacketWithHeaderAs<qswapQuote_output>();
template ExchangePublicPeers QubicConnection::receivePacketAs<ExchangePublicPeers>();
template std::vector<Tick> QubicConnection::getLatestVectorPacketAs<Tick>();
template std::vector<RespondOwnedAssets> QubicConnection::getLatestVectorPacketAs<RespondOwnedAssets>();
template std::vector<RespondPossessedAssets> QubicConnection::getLatestVectorPacketAs<RespondPossessedAssets>();
template std::vector<RespondAssets> QubicConnection::getLatestVectorPacketAs<RespondAssets>();
template std::vector<RespondAssetsWithSiblings> QubicConnection::getLatestVectorPacketAs<RespondAssetsWithSiblings>();
// MSVAULT
template MsVaultGetBalanceOf_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetBalanceOf_output>();
template MsVaultGetReleaseStatus_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetReleaseStatus_output>();
template MsVaultGetVaults_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetVaults_output>();
template MsVaultGetVaultName_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetVaultName_output>();
template MsVaultGetRevenueInfo_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetRevenueInfo_output>();
template MsVaultGetFees_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetFees_output>();
template MsVaultGetVaultOwners_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetVaultOwners_output>();
template MsVaultGetVaultAssetBalances_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetVaultAssetBalances_output>();
template MsVaultGetAssetReleaseStatus_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetAssetReleaseStatus_output>();
template MsVaultGetManagedAssetBalance_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetManagedAssetBalance_output>();
template MsVaultIsShareHolder_output QubicConnection::receivePacketWithHeaderAs<MsVaultIsShareHolder_output>();
template MsVaultGetFeeVotes_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetFeeVotes_output>();
template MsVaultGetFeeVotesOwner_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetFeeVotesOwner_output>();
template MsVaultGetFeeVotesScore_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetFeeVotesScore_output>();
template MsVaultGetUniqueFeeVotes_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetUniqueFeeVotes_output>();
template MsVaultGetUniqueFeeVotesRanking_output QubicConnection::receivePacketWithHeaderAs<MsVaultGetUniqueFeeVotesRanking_output>();
// NOSTROMO
template NOSTROMOGetStats_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetStats_output>();
template NOSTROMOGetTierLevelByUser_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetTierLevelByUser_output>();
template NOSTROMOGetUserVoteStatus_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetUserVoteStatus_output>();
template NOSTROMOCheckTokenCreatability_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOCheckTokenCreatability_output>();
template NOSTROMOGetNumberOfInvestedProjects_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetNumberOfInvestedProjects_output>();
template NOSTROMOGetProjectByIndex_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetProjectByIndex_output>();
template NOSTROMOGetFundarasingByIndex_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetFundarasingByIndex_output>();
template NOSTROMOGetProjectIndexListByCreator_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetProjectIndexListByCreator_output>();
template NOSTROMOGetInfoUserInvested_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetInfoUserInvested_output>();
template NOSTROMOGetMaxClaimAmount_output QubicConnection::receivePacketWithHeaderAs<NOSTROMOGetMaxClaimAmount_output>();
// QBOND
template GetInfoPerEpoch_output QubicConnection::receivePacketWithHeaderAs<GetInfoPerEpoch_output>();
template GetOrders_output QubicConnection::receivePacketWithHeaderAs<GetOrders_output>();
template GetUserOrders_output QubicConnection::receivePacketWithHeaderAs<GetUserOrders_output>();
template MBondsTable_output QubicConnection::receivePacketWithHeaderAs<MBondsTable_output>();
template GetUserMBonds_output QubicConnection::receivePacketWithHeaderAs<GetUserMBonds_output>();
template GetFees_output QubicConnection::receivePacketWithHeaderAs<GetFees_output>();
template GetEarnedFees_output QubicConnection::receivePacketWithHeaderAs<GetEarnedFees_output>();
template GetCFA_output QubicConnection::receivePacketWithHeaderAs<GetCFA_output>();
// TESTING
template QpiFunctionsOutput QubicConnection::receivePacketWithHeaderAs<QpiFunctionsOutput>();
// QUTIL
template GetCurrentResult_output QubicConnection::receivePacketWithHeaderAs<GetCurrentResult_output>();
template GetPollsByCreator_output QubicConnection::receivePacketWithHeaderAs<GetPollsByCreator_output>();
template GetCurrentPollId_output QubicConnection::receivePacketWithHeaderAs<GetCurrentPollId_output>();
template GetPollInfo_output QubicConnection::receivePacketWithHeaderAs<GetPollInfo_output>();