-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathProgram.cs
More file actions
320 lines (291 loc) · 11.4 KB
/
Copy pathProgram.cs
File metadata and controls
320 lines (291 loc) · 11.4 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Trinity;
using TSLBit;
using System.IO;
using Trinity.Storage;
using Trinity.TSL.Lib;
using Newtonsoft.Json;
using System.Threading;
namespace Generator
{
class BitQuery : BitQueryBaseBase
{
}
internal class Program
{
struct freqPara
{
public Int64 CellID;
public List<string> addrs;
public Int64 time;
}
static void Main(string[] args)
{
BitQuery server = new BitQuery();
server.Start();
LoadBlocks(2000000000, 1, 2);
updateCircle(2);
//LongReqWriter reqMsg = new LongReqWriter(1);
//IntResReader resMsg = Global.CloudStorage.IsFreqToBitQueryBase(0, reqMsg);
//Console.WriteLine(resMsg.value);
Global.LocalStorage.SaveStorage();
Console.WriteLine("Main done...");
}
//load block and update isAmount and isFreq
public static void LoadBlocks(long amountMax, long timespan, int step)
{
Console.WriteLine("LoadBlocks begin...");
//load cellIDs
List<Int64> cellIDs = new List<Int64>();
using (StreamReader cellIDsReader = new StreamReader(@"D:\Bit\cellIDs.txt"))
{
string cellIDLine;
while (null != (cellIDLine = cellIDsReader.ReadLine()))
{
cellIDs.Add(Int64.Parse(cellIDLine));
}
}
//read json line by line
using (StreamReader reader = new StreamReader(@"D:\Bit\block.txt"))
{
//statistics some characters
int count = 0;
Queue<freqPara> txsQueue = new Queue<freqPara>();
List<Int64> cellIDsFreq = new List<long>();
Int64 preCellID = -1;
Int64 preTime = -1;
//for circle
List<Int64> circleTxs = new List<Int64>();
string line;
while (null != (line = reader.ReadLine()))
{
//convert string to object
JSONBack jsonBack = ConvertToJSONBack(line);
if (jsonBack.amount > 0)
{
//convert List<Input> to List<In> for compatible
List<In> ins = new List<In>();
List<Int64> inIDs = new List<long>();
foreach (Input _in in jsonBack.ins)
{
In __in = new In(_in.tx_index, _in.addr);
ins.Add(__in);
inIDs.Add(_in.tx_index);
}
//high frequency from second
if (jsonBack.time - preTime > timespan)
{
if (txsQueue.Count > 0)
txsQueue.Clear();
}
else
{
while (txsQueue.Count > 0 && jsonBack.time - txsQueue.Peek().time > timespan)
txsQueue.Dequeue();
List<string> addrs = new List<string>();
foreach (Input _in in jsonBack.ins)
addrs.Add(_in.addr);
foreach (string _out in jsonBack.outs)
addrs.Add(_out);
foreach (freqPara pre in txsQueue)
{
foreach (string addr in pre.addrs)
{
if (addrs.Contains(addr))
{
if (!cellIDsFreq.Contains(pre.CellID))
{
cellIDsFreq.Add(pre.CellID);
}
//count++;
}
}
}
freqPara para;
para.CellID = jsonBack.CellID;
para.addrs = addrs;
para.time = jsonBack.time;
txsQueue.Enqueue(para);
}
preCellID = jsonBack.CellID;
preTime = jsonBack.time;
try
{
Tx tx = new Tx(jsonBack.CellID, jsonBack.time, jsonBack.hash, ins, inIDs, jsonBack.outs, jsonBack.amount, (int)(jsonBack.amount / amountMax), cellIDsFreq.Count);
Global.LocalStorage.SaveTx(tx);
//if (jsonBack.amount > amountMax)
//{
// Int64 cellID = jsonBack.CellID;
// Thread t = new Thread(() => { DFSFind(step, 0, cellID, jsonBack.outs, cellID, circleTxs, cellIDs); });
// t.IsBackground = true;
// t.Start();
// //DFSFind(step, 0, cellID, jsonBack.outs, cellID, circleTxs, cellIDs);
//}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
throw;
}
if (cellIDsFreq.Count > 0)
{
foreach (Int64 cellID in cellIDsFreq)
{
using (var tx = Global.LocalStorage.UseTx(cellID))
{
if (tx.isFreq < cellIDsFreq.Count)
tx.isFreq = cellIDsFreq.Count;
}
}
cellIDsFreq.Clear();
}
}
}
foreach (Int64 cellID in circleTxs)
{
count++;
using (var tx = Global.LocalStorage.UseTx(cellID))
{
tx.isCycle = true;
}
}
Console.WriteLine(count);
}
}
public struct Indulge
{
public Int64 cellID;
public string addrBegin;
public string addrEnd;
}
public static void updateCircle(int step)
{
Console.WriteLine("updateCircle begin...");
int count = 0;
List<Int64> circleTxs = new List<Int64>();
//List<Indulge> indulges = new List<Indulge>();
//read cellIDs
List<Int64> cellIDs = new List<Int64>();
using (StreamReader reader = new StreamReader(@"D:\Bit\cellIDs.txt"))
{
string line;
while (null != (line = reader.ReadLine()))
{
cellIDs.Add(Int64.Parse(line));
}
}
foreach (Int64 cellID in cellIDs)
{
List<string> outs = new List<string>();
using (var tx = Global.LocalStorage.UseTx(cellID, CellAccessOptions.ReturnNullOnCellNotFound))
{
if (tx == null)
continue;
//Console.WriteLine("foreach begin..." + tx.CellID);
foreach (string _out in tx.outs)
{
outs.Add(_out);
}
}
Int64 cellIDCopy = cellID;
Thread t = new Thread(() => { DFSFind(step, 0, cellIDCopy, outs, cellIDCopy, circleTxs, cellIDs); });
t.IsBackground = true;
t.Start();
//DFSFind(step, 0, cellID, outs, cellID, circleTxs, cellIDs);
}
foreach (Int64 cellID in circleTxs)
{
count++;
using (var tx = Global.LocalStorage.UseTx(cellID))
{
tx.isCycle = true;
}
}
Console.WriteLine(count);
Console.WriteLine("updateCircle end...");
}
public static bool DFSFind(int step, int stepNow, Int64 inCellID, List<string> outs, Int64 outCellID, List<Int64> circleTxs, List<Int64> cellIDs)
{
if (stepNow >= step)
return false;
List<Input> ins = new List<Input>();
using (var tx = Global.LocalStorage.UseTx(inCellID, CellAccessOptions.ReturnNullOnCellNotFound))
{
if (tx == null)
return false;
foreach (In _in in tx.ins)
{
ins.Add(new Input(_in.addr, _in.tx_index));
}
}
if (stepNow != 0)
{
//before in = now out
foreach (Input input in ins)
{
foreach (string _out in outs)
{
if (input.addr == _out)
{
if (!circleTxs.Contains(outCellID))
circleTxs.Add(outCellID);
if (!circleTxs.Contains(inCellID))
circleTxs.Add(inCellID);
Console.WriteLine(outCellID);
}
}
}
}
//compare paster
List<Thread> threads = new List<Thread>();
bool[] HasCircles = new bool[ins.Count];
for (int i = 0; i < ins.Count; i++)
{
int index = i;
Input input = ins[index];
if (cellIDs.Contains(input.tx_index))
{
Thread t = new Thread(() => { HasCircles[index] = DFSFind(step, stepNow + 1, input.tx_index, outs, outCellID, circleTxs, cellIDs); });
t.IsBackground = true;
t.Start();
threads.Add(t);
//HasCircles[index] = DFSFind(step, stepNow + 1, input.tx_index, outs, outCellID, circleTxs, cellIDs);
}
}
foreach (Thread t in threads)
{
t.Join();
}
foreach (bool hasCircle in HasCircles)
{
if (hasCircle == true)
{
lock (circleTxs)
{
if (!circleTxs.Contains(inCellID))
circleTxs.Add(inCellID);
return true;
}
}
}
return false;
}
public static JSONBack ConvertToJSONBack(string jsonStr)
{
JSONBack JSONBack = new JSONBack();
try
{
JSONBack = JsonConvert.DeserializeObject<JSONBack>(jsonStr);
}
catch (Exception e)
{
return null;
}
return JSONBack;
}
}
}