-
Notifications
You must be signed in to change notification settings - Fork 25
/
sanityCheck.h
258 lines (237 loc) · 4.69 KB
/
sanityCheck.h
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
#pragma once
#include <fstream>
#include "logger.h"
static bool isValidIpAddress(char* ipAddress)
{
int a, b, c, d;
int r = sscanf(ipAddress, "%d.%d.%d.%d", &a, &b, &c, &d);
if (r == 4 && (0 <= a && a < 256)
&& (0 <= b && b < 256)
&& (0 <= c && c < 256)
&& (0 <= d && d < 256))
{
return true;
}
return false;
}
static void sanityCheckTxType(uint16_t type)
{
if (type > 1)
{
LOG("unknown input type %u\n", type);
exit(1);
}
}
static void sanityCheckSeed(const char* privKey)
{
if (privKey == NULL)
{
LOG("privKey is null\n");
exit(1);
}
if (strlen(privKey) != 55)
{
LOG("Seed must be 55-char length\n");
exit(1);
}
if (memcmp(privKey, DEFAULT_SEED, 55) == 0)
{
LOG("WARNING: You are using default seed\n");
}
}
static void sanityCheckNode(char* ip, int port)
{
if (port < 1024)
{
LOG("invalid port number\n");
exit(1);
}
if (ip == NULL)
{
LOG("invalid ipv4 address\n");
exit(1);
}
if (!isValidIpAddress(ip))
{
LOG("invalid ipv4 address: %s\n", ip);
exit(1);
}
}
static void sanityCheckAmountTransferAsset(long long amount)
{
if (amount <= 0)
{
LOG("Invalid amount\n");
exit(1);
}
}
static void sanityCheckIdentity(const char* identity)
{
if (identity == NULL)
{
LOG("identity is null\n");
exit(1);
}
for (int i = 0; i < 60; i++)
{
if (identity[i] < 'A' || identity[i] > 'Z')
{
LOG("invalid identity at position %d (%c)", i, identity[i]);
exit(1);
}
}
if (!checkSumIdentity(identity))
{
LOG("Identity checksum failed: %s\n", identity);
exit(1);
}
}
static void sanityCheckTxHash(char* txHash)
{
if (txHash == NULL)
{
LOG("identity is null\n");
exit(1);
}
for (int i = 0; i < 60; i++)
{
if (txHash[i] < 'a' || txHash[i] > 'z')
{
LOG("invalid identity at position %d (%c)", i, txHash[i]);
exit(1);
}
}
}
static void sanityCheckTxAmount(int64_t amount)
{
if (amount < 0)
{
LOG("invalid amount %lld\n", amount);
exit(1);
}
}
static void sanityCheckExtraDataSize(int data_size)
{
if (data_size > 1024)
{
LOG("Invalid data size %d, data size must be smaller than 1024\n", data_size);
exit(1);
}
}
static void sanityCheckRawPacketSize(int data_size)
{
if (data_size > 1024)
{
LOG("Invalid data size %d, data size must be smaller than 1024\n", data_size);
exit(1);
}
}
static void sanityFileExist (const std::string name)
{
std::ifstream f(name.c_str());
if (!f.good())
{
LOG("File %s does not exist\n", name.c_str());
exit(1);
}
}
static void sanityCheckSpecialCommand(int cmd)
{
if (cmd == -1)
{
LOG("Invalid special command");
exit(1);
}
}
static void sanityCheckNumberOfUnit(long long unit)
{
if (unit <= 0)
{
LOG("Invalid number of unit\n");
exit(1);
}
}
static void sanityCheckNumberOfDecimal(char unit)
{
if (unit < 0)
{
LOG("Invalid number of decimal\n");
exit(1);
}
}
static void sanityCheckValidString(const char* str)
{
if (str == nullptr)
{
LOG("Invalid string\n");
exit(1);
}
}
static void sanityCheckUnitofMeasurement(const char* str)
{
if (str == nullptr)
{
LOG("Invalid string\n");
exit(1);
}
if (strlen(str) != 7)
{
LOG("UnitofMeasurement length must be 7, they are powers of the corresponding SI base units:\n");
LOG("AMPERE\n");
LOG("CANDELA\n");
LOG("KELVIN\n");
LOG("KILOGRAM\n");
LOG("METER\n");
LOG("MOLE\n");
LOG("SECOND\n");
exit(1);
}
}
static void sanityCheckMainAuxStatus(const char* str)
{
if (str == nullptr)
{
LOG("Invalid MAIN/AUX string\n");
exit(1);
}
int len = strlen(str);
if (len == 4)
{
if (memcmp(str, "MAIN", 4) != 0)
{
LOG("Invalid MAIN/AUX string. Expected (MAIN/AUX), have %s\n", str);
exit(1);
}
}
else if (len == 3)
{
if (memcmp(str, "AUX", 3) != 0)
{
LOG("Invalid MAIN/AUX string. Expected (MAIN/AUX), have %s\n", str);
exit(1);
}
}
else
{
LOG("Invalid MAIN/AUX string. Expected (MAIN/AUX), have %s\n", str);
exit(1);
}
}
static void checkValidEpoch(int epoch)
{
if (epoch >= 0 && epoch < 1000)
{
return;
}
LOG("Invalid epoch number\n");
exit(1);
}
static void checkValidSolutionThreshold(int thres)
{
if (thres >= 0)
{
return;
}
LOG("Invalid solution threshold\n");
exit(1);
}