forked from Shentist/metaexchange
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaApi.cs
More file actions
542 lines (475 loc) · 15.2 KB
/
MetaApi.cs
File metadata and controls
542 lines (475 loc) · 15.2 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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Net;
using Monsterer.Request;
using WebDaemonShared;
using WebDaemonSharedTables;
using ApiHost;
using MetaData;
using RestLib;
using ServiceStack.Text;
namespace MetaExchange
{
public partial class MetaServer : IDisposable
{
const int kAggregateTimeoutMillis = 5000;
const int kMb = 1024 * 1024;
/// <summary> Make sure this request really came from one of our daemons </summary>
///
/// <remarks> Paul, 21/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> true if it succeeds, false if it fails. </returns>
bool ConfirmDaemon(RequestContext ctx, IDummy dummy)
{
string from = ctx.Request.RemoteEndPoint.Address.ToString();
IEnumerable<string> ourIps = Dns.GetHostEntry(Dns.GetHostName()).AddressList.Select<IPAddress, string>(ip => ip.ToString());
IEnumerable<string> daemons = m_auth.m_Database.GetAllMarkets().Select<MarketRow, string>(r => r.daemon_url);
List<Uri> uris = new List<Uri>();
List<string> ips = new List<string>();
foreach (string d in daemons)
{
IPAddress[] e = Dns.GetHostAddresses(new Uri(d).Host);
foreach (IPAddress ip in e)
{
ips.Add(ip.ToString());
}
}
bool allowed = ips.Contains(from) || ourIps.Contains(from);
if (!allowed)
{
m_Database.LogGeneralException("ConfirmDaemon(" + from + ") failed...");
}
return allowed;
}
/// <summary> Executes the submit address action. </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
async Task OnSubmitAddress(RequestContext ctx, IDummy dummy)
{
// intercept the response and stick it in the site database so we can handle forwarding future queries
string symbolPair = RestHelpers.GetPostArg<string, ApiExceptionMissingParameter>(ctx, WebForms.kSymbolPair);
uint referralUser = RestHelpers.GetPostArg<uint>(ctx, WebForms.kReferralId);
string receivingAddress = RestHelpers.GetPostArg<string, ApiExceptionMissingParameter>(ctx, WebForms.kReceivingAddress);
// do this at the site level, because we need to prevent this from occuring across nodes
if (dummy.m_database.IsAnyDepositAddress(receivingAddress))
{
throw new ApiExceptionInvalidAddress("<internal deposit address>");
}
// forward the post on
string response = await ForwardPostSpecific(ctx, dummy);
if (response != null)
{
// get the juicy data out
SubmitAddressResponse data = JsonSerializer.DeserializeFromString<SubmitAddressResponse>(response);
// pull the market out of the request
MarketRow m = dummy.m_database.GetMarket(symbolPair);
// stick it in the master database
dummy.m_database.InsertSenderToDeposit(data.receiving_address, data.deposit_address, m.symbol_pair, referralUser, true);
if (referralUser > 0)
{
// track referrals
string depositAddress;
if (data.memo != null)
{
depositAddress = data.memo;
}
else
{
depositAddress = data.deposit_address;
}
dummy.m_database.InsertReferralAddress(depositAddress, referralUser);
}
}
}
/// <summary> Executes the push transactions action. </summary>
///
/// <remarks> Paul, 20/02/2015. </remarks>
///
/// <param name="newTrans"> The new transaction. </param>
/// <param name="database"> The database. </param>
void OnPushTransactions(List<TransactionsRow> newTrans, MySqlData database)
{
Dictionary<string, uint> lastSeen = new Dictionary<string, uint>();
try
{
database.BeginTransaction();
foreach (TransactionsRow r in newTrans)
{
// this may have problems with partial transactions
database.InsertTransaction(r.symbol_pair, r.deposit_address, r.order_type, r.received_txid, r.sent_txid, r.amount, r.price, r.fee, r.status, r.date, r.notes, TransactionPolicy.REPLACE);
if (lastSeen.ContainsKey(r.symbol_pair))
{
lastSeen[r.symbol_pair] = Math.Max(r.uid, lastSeen[r.symbol_pair]);
}
else
{
lastSeen[r.symbol_pair] = r.uid;
}
}
// keep the site upto date with last seen transastion uids
foreach (KeyValuePair<string, uint> kvp in lastSeen)
{
database.UpdateLastSeenTransactionForSite(kvp.Key, kvp.Value);
}
database.EndTransaction();
}
catch (Exception)
{
database.RollbackTransaction();
throw;
}
}
/// <summary> Executes the push transactions action. </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
async Task OnPushTransactions(RequestContext ctx, IDummy dummy)
{
if (ConfirmDaemon(ctx, dummy))
{
string allTrans = ctx.Request.Body;
if (ctx.Request.m_Truncated)
{
allTrans += await ctx.Request.GetBody(kMb);
}
List<TransactionsRow> newTrans = JsonSerializer.DeserializeFromString<List<TransactionsRow>>(allTrans);
OnPushTransactions(newTrans, dummy.m_database);
ctx.Respond<bool>(true);
}
else
{
ctx.Respond<bool>(false);
}
}
/// <summary> Executes the push market action. </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task OnPushMarket(RequestContext ctx, IDummy dummy)
{
if (ConfirmDaemon(ctx, dummy))
{
MarketRow market = JsonSerializer.DeserializeFromString<MarketRow>(ctx.Request.Body);
dummy.m_database.UpdateMarketInDatabase(market);
ctx.Respond<bool>(true);
}
else
{
ctx.Respond<bool>(false);
}
return null;
}
/// <summary> Executes the push fee collection action. </summary>
///
/// <remarks> Paul, 06/03/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task OnPushFeeCollection(RequestContext ctx, IDummy dummy)
{
if (ConfirmDaemon(ctx, dummy))
{
long oldRow = m_Database.CountFeeRows();
List<FeeCollectionRow> allFees = JsonSerializer.DeserializeFromString<List<FeeCollectionRow>>(ctx.Request.Body);
try
{
dummy.m_database.BeginTransaction();
foreach (FeeCollectionRow fee in allFees)
{
dummy.m_database.InsertFeeTransaction(fee.symbol_pair,
fee.buy_trxid,
fee.sell_trxid,
fee.buy_fee,
fee.sell_fee,
fee.transaction_processed_uid,
fee.exception,
fee.start_txid,
fee.end_txid,
true);
}
dummy.m_database.EndTransaction();
long newRows = m_Database.CountFeeRows();
if (newRows > oldRow)
{
// here we should process the fees
FeeReporting(allFees);
}
ctx.Respond<bool>(true);
}
catch (Exception)
{
dummy.m_database.RollbackTransaction();
ctx.Respond<bool>(false);
throw;
}
}
else
{
ctx.Respond<bool>(false);
}
return null;
}
/// <summary> Result is exception. </summary>
///
/// <remarks> Paul, 17/03/2015. </remarks>
///
/// <param name="result"> The result. </param>
///
/// <returns> An ApiError. </returns>
ApiError GetExceptionFromResult(string result)
{
ApiError errorCheck = null;
try
{
errorCheck = JsonSerializer.DeserializeFromString<ApiError>(result);
if (errorCheck.error == ApiErrorCode.None)
{
errorCheck = null;
}
}
catch (SerializationException) { }
return errorCheck;
}
/// <summary> Forward track IP bans. </summary>
///
/// <remarks> Paul, 16/02/2015. </remarks>
///
/// <exception cref="ApiExceptionGeneral"> Thrown when an API exception general error condition
/// occurs. </exception>
///
/// <param name="ctx"> The context. </param>
/// <param name="action"> The action. </param>
///
/// <returns> A Task. </returns>
async Task<string> ForwardTrackIpBans(RequestContext ctx, Func<RequestContext, Task<string>> action)
{
// forward request on
try
{
string result = await action(ctx);
ctx.RespondJsonFromString(result);
// handle ban on exception forwarding
if (GetExceptionFromResult(result) != null)
{
throw new ApiExceptionGeneral();
}
return result;
}
catch (WebException e)
{
// make sure to log this so we can check the details
m_Database.LogGeneralException(e.ToString());
throw new ApiExceptionGeneral();
}
}
/// <summary> Forward post specific. </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task<string> ForwardPostSpecific(RequestContext ctx, IDummy dummy)
{
// pull out the daemon address from the market row
string symbolPair = RestHelpers.GetPostArg<string, ApiExceptionMissingParameter>(ctx, WebForms.kSymbolPair);
MarketRow m = dummy.m_database.GetMarket(symbolPair);
// forward the post on
return ForwardTrackIpBans(ctx, c => Rest.ExecutePostAsync(ApiUrl(m.daemon_url, c.Request.Url.LocalPath), c.Request.PostArgString));
}
/// <summary> Aggregate result tasks. </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="c"> The RequestContext to process. </param>
/// <param name="post"> true to post. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task<T>[]. </returns>
Task<T>[] AggregateResultTasks<T>(string url, string postArgs, string getArgs, bool post, MySqlData database)
{
List<MarketRow> allMarkets = database.GetAllMarkets();
// get all unique daemon urls
List<string> allDaemons = allMarkets.Select<MarketRow, string>(r => r.daemon_url).Distinct().ToList();
Task<T>[] allTasks = new Task<T>[allDaemons.Count()];
// execute the get on each one
for (int i = 0; i < allTasks.Length; i++)
{
if (post)
{
allTasks[i] = Rest.JsonApiCallAsync<T>(ApiUrl(allDaemons[i], url), postArgs );
}
else
{
allTasks[i] = Rest.JsonApiGetAsync<T>(ApiUrl(allDaemons[i], url) + (getArgs.Length>0?"?" + getArgs:""));
}
}
return allTasks;
}
/// <summary> Wait and get aggregate list. </summary>
///
/// <remarks> Paul, 21/02/2015. </remarks>
///
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="allTasks"> all tasks. </param>
///
/// <returns> A List<T> </returns>
List<T> WaitAndGetAggregateList<T>(Task<List<T>>[] allTasks)
{
// wait for them all
Task.WaitAll(allTasks, kAggregateTimeoutMillis);
// aggregate the results
List<T> aggregate = new List<T>();
foreach (Task<List<T>> t in allTasks)
{
if (t.IsCompleted)
{
aggregate.AddRange(t.Result);
}
}
return aggregate;
}
/// <summary> Aggregate results of an API call to multiple deamons together in one list of Ts </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="c"> The RequestContext to process. </param>
/// <param name="post"> true to post. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task<string> </returns>
Task<string> AggregateResult<T>(RequestContext c, bool post, IDummy dummy)
{
Task<List<T>>[] allTasks = AggregateResultTasks<List<T>>(c.Request.Url.LocalPath, c.Request.PostArgString, c.Request.Url.Query, post, dummy.m_database);
List<T> aggregate = WaitAndGetAggregateList<T>(allTasks);
// return as a task
return Task.FromResult<string>(JsonSerializer.SerializeToString<List<T>>(aggregate));
}
/// <summary> Forward get. </summary>
///
/// <remarks> Paul, 06/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task ForwardGetAggregate<T>(RequestContext ctx, IDummy dummy)
{
return ForwardTrackIpBans(ctx, c =>
{
return AggregateResult<T>(c, false, dummy);
});
}
/// <summary> Forward post aggregate. </summary>
///
/// <remarks> Paul, 19/02/2015. </remarks>
///
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task ForwardPostAggregate<T>(RequestContext ctx, IDummy dummy)
{
return ForwardTrackIpBans(ctx, c =>
{
return AggregateResult<T>(c, true, dummy);
});
}
/// <summary> Pulls the initial data. </summary>
///
/// <remarks> Paul, 20/02/2015. </remarks>
void PullInitialData()
{
try
{
Task<List<MarketRow>>[] gets = AggregateResultTasks<List<MarketRow>>(Routes.kGetAllMarkets, null, "", false, m_auth.m_Database);
List<MarketRow> aggregate = WaitAndGetAggregateList<MarketRow>(gets);
foreach (MarketRow m in aggregate)
{
m_auth.m_Database.UpdateMarketInDatabase(m);
}
}
catch (Exception) { }
}
/// <summary> Gets the visisble markets in this collection. </summary>
///
/// <remarks> Paul, 28/02/2015. </remarks>
///
/// <param name="initial"> The initial. </param>
///
/// <returns>
/// An enumerator that allows foreach to be used to process the visisble markets in this
/// collection.
/// </returns>
IEnumerable<MarketRow> GetVisisbleMarkets(List<MarketRow> initial)
{
return initial.Where(m => m.visible);
}
/// <summary> Executes the get all markets action. </summary>
///
/// <remarks> Paul, 28/02/2015. </remarks>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task OnGetAllMarkets(RequestContext ctx, IDummy dummy)
{
//ctx.Respond<List<MarketRow>>(GetVisisbleMarkets(m_Database.GetAllMarkets()).ToList());
m_api.SendCorsResponse<List<MarketRow>>(ctx, GetVisisbleMarkets(m_Database.GetAllMarkets()).ToList());
return null;
}
/// <summary> Executes the get market action. </summary>
///
/// <remarks> Paul, 28/02/2015. </remarks>
///
/// <exception cref="ApiExceptionUnknownMarket"> Thrown when an API exception unknown market
/// error condition occurs. </exception>
///
/// <param name="ctx"> The context. </param>
/// <param name="dummy"> The dummy. </param>
///
/// <returns> A Task. </returns>
Task OnGetMarket(RequestContext ctx, IDummy dummy)
{
string symbolPair = RestHelpers.GetPostArg<string, ApiExceptionMissingParameter>(ctx, WebForms.kSymbolPair);
MarketRow market = m_Database.GetMarket(symbolPair);
if (market == null)// || !market.visible)
{
throw new ApiExceptionUnknownMarket(symbolPair);
}
else
{
//ctx.Respond<MarketRow>(market);
m_api.SendCorsResponse<MarketRow>(ctx, market);
}
return null;
}
}
}