-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInstance.cs
603 lines (498 loc) · 18.1 KB
/
Instance.cs
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
namespace akash_dep
{
public class Instance
{
readonly Wallet m_wallet;
public static String AKASH_YML_PATH = "deploy.yml";
public static String AKASH_YML_EDITED_PATH = "deploy_submit.yml";
public static String AKASH_HOME = "~/.akash";
public static String GAS_CONFIG = "--gas-prices=\"0.025uakt\" --gas=\"auto\" --gas-adjustment=1.25";
public static long AKASH_PRICE_LIMIT = 180; // no more than this amount, otherwise too expensive
public long m_dseq = -1;
public String m_provider = "";
public EnvVarsReplacer m_replacer = new EnvVarsReplacer();
public JArray m_currentLeases = null;
static Dictionary<String, String> badList = new Dictionary<string, string>();
public void MarkBad()
{
if (CheckIsBad(m_provider))
{
Console.WriteLine("duplicate bad");
return;
}
String badID = m_provider;
if (String.IsNullOrEmpty(badID)) return;
var stream = File.AppendText("bad.txt");
stream.WriteLine(badID);
stream.Close();
badList.Add(badID, "2");
Console.WriteLine("added to ban " + badID);
}
public static void LoadBad()
{
try
{
var stream = File.ReadAllLines("bad.txt");
foreach (var st in stream)
{
if (String.IsNullOrWhiteSpace(st)) continue;
badList.Add(st, "1");
Console.WriteLine("added " + st + " to ban");
}
}
catch
{
}
}
public bool CheckIsBad(String prov)
{
String value;
if (badList.TryGetValue(prov, out value))
{
Console.WriteLine("banned " + prov);
return true;
}
return false;
}
public Instance(ref Wallet wallet)
{
m_wallet = wallet;
m_replacer.Add("AKASH_HOME", AKASH_HOME);
}
public static void LoadCfg(JToken cfg)
{
AKASH_YML_PATH = cfg["AKASH_YML_PATH"].ToString();
AKASH_YML_EDITED_PATH = cfg["AKASH_YML_EDITED_PATH"].ToString();
AKASH_HOME = cfg["AKASH_HOME"].ToString();
GAS_CONFIG = cfg["GAS_CONFIG"].ToString();
AKASH_PRICE_LIMIT = cfg["AKASH_PRICE_LIMIT"].ToObject<long>();
}
// Currently basic renaming to fix naming problems with pool statistics
public void PrepareYml(long numInstances)
{
String text = File.ReadAllText(AKASH_YML_PATH);
String text2replace = "aktDEP_RandomName_10";
String rndDeployText = "aktDEP_" + Converters.RandomString(5); // Current limit of the pool we are using
rndDeployText += "_" + numInstances; // add num instances
text = text.Replace(text2replace, rndDeployText);
// update correct number of instances
String inst2replace = "count: 10";
text = text.Replace(inst2replace, "count: " + numInstances);
Console.WriteLine("new name " + rndDeployText);
File.WriteAllText(AKASH_YML_EDITED_PATH,text);
}
public void ClearCaches()
{
File.Delete("bids.txt");
File.Delete("check.txt");
File.Delete("closed.txt");
File.Delete("deploy.txt");
File.Delete("lease.txt");
File.Delete("manifest.txt");
}
public bool LoadFromDepJson(JToken deployment)
{
try
{
var deployment_id = deployment["deployment_id"];
m_dseq = deployment_id["dseq"].ToObject<long>();
SetSeqVars(); // Update the db for future uses
}
catch
{
Console.WriteLine("loading vars from js failed " + deployment.ToString());
return false;
}
return true;
}
public static long FindDseq(JToken js)
{
JArray logs = (JArray)js["logs"];
JArray events = (JArray)logs[0]["events"];
foreach (var ev in events)
{
if (ev["type"].ToString() == "akash.v1")
{
var attributes = (JArray)ev["attributes"];
foreach (var at in attributes)
{
if (at["key"].ToString() == "dseq")
{
return at["value"].ToObject<long>();
}
}
}
}
return -1;
}
public void SetSeqVars()
{
m_replacer.Add("AKASH_GSEQ", "1");
m_replacer.Add("AKASH_OSEQ", "1");
m_replacer.Add("AKASH_DSEQ", m_dseq.ToString());
}
public void SetProviderVars()
{
m_replacer.Add("AKASH_PROVIDER", m_provider);
}
public String Prepare(String val)
{
return m_replacer.Prepare(val);
}
public void UpdateExec(ref String exec)
{
exec = Akash.Prepare(exec);
exec = m_wallet.Prepare(exec);
exec = Prepare(exec);
}
public bool Create(long numInstances)
{
PrepareYml(numInstances);
Console.WriteLine("creating new deployment, inst " + numInstances);
String exec = "tx deployment create \"" + AKASH_YML_EDITED_PATH + "\" " +
"--from $AKASH_KEY_NAME --node $AKASH_NODE --chain-id $AKASH_CHAIN_ID --keyring-backend $AKASH_KEYRING_BACKEND " +
"--yes " + PrepareGas();
UpdateExec(ref exec);
Akash.PushAkash(exec);
String js_str;
if (File.Exists("deploy.txt"))
{
Console.WriteLine("cached deploy.txt");
Akash.Clear();
js_str = File.ReadAllText("deploy.txt");
}
else
{
js_str = Akash.Send();
}
JToken js = Converters.STRtoJS(js_str);
try
{
m_dseq = FindDseq(js);
}
catch
{
Console.WriteLine("invalid dseq");
return false;
}
if (m_dseq == -1)
{
return false;
}
File.WriteAllText("deploy.txt", js_str); // Save valid
Console.WriteLine("creating new deployment OK, got dseq " + m_dseq);
return true;
}
public String FindBestLease(JToken js)
{
Console.WriteLine(m_dseq + " searching for best lease");
JToken curBest = null;
int numOpened = 0;
JArray bids = (JArray)js["bids"];
Console.WriteLine("got bids " + bids.Count);
foreach (var bid_ in bids)
{
var bid = bid_["bid"];
String state = bid["state"].ToString();
if (state != "open") continue;
numOpened++;
String priceType = bid["price"]["denom"].ToString();
if (priceType != "uakt")
{
continue; // ignore all new denom specs
}
JToken bidPriceJS = bid["price"];
long curPrice = bidPriceJS["amount"].ToObject<long>();
if (curPrice > m_wallet.GetNumAKT() || curPrice > AKASH_PRICE_LIMIT)
{
Console.WriteLine("too expensive " + curPrice + "uakt");
continue; // out of money or too expensive for us
}
else
{
Console.WriteLine("got bid " + curPrice + "uakt");
}
String curLeaseID = bid["bid_id"]["provider"].ToString();
if (CheckIsBad(curLeaseID)) continue; // Skipped banned ones
if (curBest == null) // Save primary
{
curBest = bid;
continue;
}
long bestPrice = curBest["price"]["amount"].ToObject<long>();
if (curPrice < bestPrice)
{
curBest = bid;
continue;
}
}
if (curBest == null)
{
Console.WriteLine("no good lease was found! numOpened " + numOpened);
return null;
}
String leaseID = curBest["bid_id"]["provider"].ToString();
long price = curBest["price"]["amount"].ToObject<long>();
Console.WriteLine("good lease price " + price + "uakt");
Console.WriteLine("good lease was found id " + leaseID);
return leaseID;
}
public bool CreateLease()
{
Console.WriteLine(m_dseq + " creating lease");
SetSeqVars();
String exec = "query market bid list --owner $AKASH_ACCOUNT_ADDRESS --node $AKASH_NODE --dseq $AKASH_DSEQ";
UpdateExec(ref exec);
Akash.PushAkash(exec);
String js_raw;
if (File.Exists("bids.txt"))
{
Console.WriteLine("cached bids.txt");
Akash.Clear();
js_raw = File.ReadAllText("bids.txt");
}
else
{
String yam_str = Akash.Send();
js_raw = Converters.YAMLtoJSON(yam_str);
}
JToken js = Converters.STRtoJS(js_raw);
try
{
JArray bids = (JArray)js["bids"];
if (bids.Count == 0)
{
Console.WriteLine("bids are not ready");
return false;
}
m_provider = FindBestLease(js);
}
catch
{
Console.WriteLine("bids have bad data");
return false;
}
if (m_provider != null)
{
Console.WriteLine("lease was queried");
File.WriteAllText("bids.txt", js_raw);
return true;
}
return false;
}
public bool SelectLease()
{
Console.WriteLine(m_dseq + " selecting lease " + m_provider);
if (File.Exists("lease.txt"))
{
Console.WriteLine("cached lease.txt");
return true;
}
SetSeqVars();
SetProviderVars();
String exec = "tx market lease create --chain-id $AKASH_CHAIN_ID --node $AKASH_NODE " +
"--owner $AKASH_ACCOUNT_ADDRESS --dseq $AKASH_DSEQ --gseq $AKASH_GSEQ --oseq $AKASH_OSEQ --keyring-backend $AKASH_KEYRING_BACKEND " +
"--provider $AKASH_PROVIDER --from $AKASH_KEY_NAME --yes " + PrepareGas();
UpdateExec(ref exec);
Akash.PushAkash(exec);
String yam_str = Akash.Send();
if (!String.IsNullOrEmpty(yam_str))
{
Console.WriteLine("selecting lease ok");
File.WriteAllText("lease.txt", yam_str);
return true;
}
else
{
Console.WriteLine("selecting lease failed");
return false;
}
}
public String GetLeaseState()
{
if (m_currentLeases == null || m_currentLeases.Count == 0)
{
Console.WriteLine("getting lease state failed, no leases");
return "none";
}
try
{
JToken lease = m_currentLeases[0]["lease"];
String lease_state = lease["state"].ToString();
return lease_state;
}
catch
{
return "unknown";
}
}
public JToken GetLeasePrice()
{
if (m_currentLeases == null || m_currentLeases.Count == 0)
{
return null;
}
try
{
JToken lease = m_currentLeases[0]["lease"];
JToken price = lease["price"];
return price;
}
catch
{
return "unknown";
}
}
public int GetNumLeases()
{
if (m_currentLeases == null)
{
return -1;
}
return m_currentLeases.Count;
}
public bool CheckLease()
{
Console.WriteLine(m_dseq + " checking lease");
SetSeqVars();
SetProviderVars();
String exec = "query market lease list --owner $AKASH_ACCOUNT_ADDRESS --node $AKASH_NODE --dseq $AKASH_DSEQ";
UpdateExec(ref exec);
Akash.PushAkash(exec);
String yam_str = Akash.Send();
JToken js = Converters.YAMLtoJSONToken(yam_str);
try
{
m_currentLeases = (JArray)js["leases"];
}
catch
{
Console.WriteLine("got invalid json");
return false;
}
if (m_currentLeases.Count > 0)
{
File.WriteAllText("check.txt", js.ToString());
// We also load new values from lease for future uses or control
JToken lease = m_currentLeases[0]["lease"];
JToken leaseID = lease["lease_id"];
m_provider = leaseID["provider"].ToString();
return true;
}
else
{
Console.WriteLine("0 leases");
File.Delete("lease.txt");
}
return false;
}
public bool SendManifestEvent()
{
Console.WriteLine(m_dseq + " sending manifest event");
SetSeqVars();
SetProviderVars();
String exec = "tx deployment update \"" + AKASH_YML_EDITED_PATH + "\" " +
"--dseq $AKASH_DSEQ --from $AKASH_KEY_NAME --chain-id $AKASH_CHAIN_ID " +
"--node $AKASH_NODE --yes "+ PrepareGas();
UpdateExec(ref exec);
Akash.PushAkash(exec);
String js_str = Akash.Send();
JToken js = Converters.STRtoJS(js_str);
try
{
String events = js["logs"][0]["events"].ToString();
if (events.Contains("deployment-updated") && events.Contains("update-deployment"))
{
Console.WriteLine("sending manifest event ok");
File.WriteAllText("manifest_event.txt", js_str);
return true;
}
else
{
Console.WriteLine("err in manifests " + js_str);
}
}
catch
{
Console.WriteLine("crash in manifests " + js_str);
}
return false;
}
public bool SendManifest()
{
Console.WriteLine(m_dseq + " sending manifest");
SetSeqVars();
SetProviderVars();
String exec = "provider send-manifest \"" + AKASH_YML_EDITED_PATH + "\" " +
"--node $AKASH_NODE --dseq $AKASH_DSEQ --provider $AKASH_PROVIDER --keyring-backend $AKASH_KEYRING_BACKEND " +
"--home $AKASH_HOME --from $AKASH_KEY_NAME";
UpdateExec(ref exec);
Akash.PushAkash(exec);
String yam_str = Akash.Send();
if (yam_str.Contains("status: PASS")) // Fails to convert to json, so we query directly
{
Console.WriteLine("sending manifest ok");
File.WriteAllText("manifest.txt", yam_str);
return true;
}
else
{
Console.WriteLine("sending manifest failed");
}
return false;
}
public bool Close()
{
Console.WriteLine(m_dseq + " closing");
String exec = "tx deployment close --node $AKASH_NODE --chain-id $AKASH_CHAIN_ID " +
"--dseq $AKASH_DSEQ --owner $AKASH_ACCOUNT_ADDRESS --from $AKASH_KEY_NAME " +
"--keyring-backend $AKASH_KEYRING_BACKEND --yes " + PrepareGas();
UpdateExec(ref exec);
Akash.PushAkash(exec);
String yam_str = Akash.Send();
File.WriteAllText("closed.txt", yam_str);
return true;
}
String PrepareGas()
{
return GAS_CONFIG;
}
public bool Deposit(long uakt)
{
Console.WriteLine(m_dseq + " depositing " + Converters.UAKTtoAKT(uakt) + "akt to " + m_dseq);
String exec = "tx deployment deposit " + uakt + "uakt --node $AKASH_NODE --chain-id $AKASH_CHAIN_ID " +
"--dseq $AKASH_DSEQ --owner $AKASH_ACCOUNT_ADDRESS --from $AKASH_KEY_NAME " +
"--keyring-backend $AKASH_KEYRING_BACKEND --yes " + PrepareGas();
UpdateExec(ref exec);
Akash.PushAkash(exec);
String yam_str = Akash.Send();
JToken js = Converters.STRtoJS(yam_str);
if (yam_str == null || js == null)
{
Console.WriteLine("invalid data " + yam_str);
return false;
}
JArray events = (JArray)js["logs"][0]["events"];
bool checkTransfer = false;
foreach (var ev in events)
{
if (ev["type"].ToString() == "transfer")
{
checkTransfer = true;
}
}
if (!checkTransfer)
{
Console.WriteLine("failed transfer");
return false;
}
File.WriteAllText("deposit.txt", yam_str);
Console.WriteLine("deposit ok");
return true;
}
}
}