forked from Noso-Project/NosoLite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnl_consensus.pas
431 lines (378 loc) · 9.92 KB
/
nl_consensus.pas
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
unit nl_consensus;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, IdTCPClient, IdGlobal, nl_data, nl_functions, strutils, nl_language, Nl_disk,
nosodebug,nosoconsensus;
Type
TTGetNodeStatus = class(TThread)
private
Slot: Integer;
protected
procedure Execute; override;
public
constructor Create(const CreatePaused: Boolean;TSlot:Integer);
end;
TFillThread = class(TThread)
protected
procedure Execute; override;
public
Constructor Create(const CreateSuspended : boolean);
end;
TGetSumThread = class(TThread)
protected
procedure Execute; override;
public
Constructor Create(const CreateSuspended : boolean);
end;
Function GetSyncingThreads():Integer;
Procedure SetSyncingThreads(value:integer);
Procedure CloseSyncingThread();
Function FillNodes():int64;
Procedure RunFillNodes();
function GetSumary():boolean;
Procedure RunGetSumary();
Function CalculateConsensus():NodeData;
Procedure RunUpdateGVTs();
function GetGVTsFile():boolean;
var
NodesFilled : boolean = false;
FillingNodes : boolean = false;
GettingSum : boolean = false;
SumReceived : boolean = false;
GoodSumary : boolean = false;
SyncingThreads : integer = 0;
SyncDuration : int64;
MainConsensus : NodeData;
UpdatingGVTs : boolean = false;
// Critical sections
CS_CSThread : TRTLCriticalSection;
IMPLEMENTATION
// Thread GetNodeStatus
constructor TTGetNodeStatus.Create(const CreatePaused: Boolean; TSlot:Integer);
begin
inherited Create(CreatePaused);
Slot := TSlot;
FreeOnTerminate := True;
end;
procedure TTGetNodeStatus.Execute;
var
TCPClient : TidTCPClient;
LineText : String = '';
Sucess : Boolean = false;
ThisNode : NodeData;
Trys : integer = 0;
Begin
ThisNode := GetNodeIndex(Slot);
TCPClient := TidTCPClient.Create(nil);
TCPclient.Host:=ThisNode.host;
TCPclient.Port:=ThisNode.port;
TCPclient.ConnectTimeout:= 500;
TCPclient.ReadTimeout:=500;
REPEAT
TRY
TCPclient.Connect;
TCPclient.IOHandler.WriteLn('NODESTATUS');
LineText := TCPclient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
TCPclient.Disconnect();
Sucess := true;
EXCEPT on E:Exception do
Sucess := false;
END{try};
Inc(Trys)
until ((Sucess) or (Trys >=3));
TCPClient.Free;
if sucess then
begin
ThisNode.Peers :=Parameter(LineText,1).ToInteger();
ThisNode.block :=Parameter(LineText,2).ToInteger();
ThisNode.Pending :=Parameter(LineText,3).ToInteger();
ThisNode.Branch :=AddCharR(' ',(Parameter(LineText,5)),5);
ThisNode.Version :=Parameter(LineText,6);
ThisNode.MNsHash :=AddCharR(' ',(Parameter(LineText,8)),5);
ThisNode.MNsCount := StrToIntDef(Parameter(LineText,9),0);
ThisNode.Updated :=0;
ThisNode.LBHash :=AddCharR(' ',(Parameter(LineText,10)),5);
ThisNode.NMSDiff :=AddCharR(' ',(Parameter(LineText,11)),5);
ThisNode.LBTimeEnd :=StrToIntDef(Parameter(LineText,12),0);
ThisNode.Checks :=StrToIntDef(Parameter(LineText,14),0);
ThisNode.SumHash :=Parameter(LineText,17);
ThisNode.GVTHash :=Parameter(LineText,18);
EnterCriticalSection(CS_ArrayNodes);
ARRAY_Nodes[slot] := ThisNode;
LeaveCriticalSection(CS_ArrayNodes);
end
else
begin
ThisNode.block := 0;
ThisNode.Pending := 0;
ThisNode.Version := rsError0003;
ThisNode.Branch := rsError0003;
ThisNode.MNsHash := rsError0003;
ThisNode.MNsCount := 0;
ThisNode.Updated := ThisNode.Updated+1;
ThisNode.NMSDiff := rsError0003;
ThisNode.SumHash := rsError0003;
ThisNode.Version := rsError0003;
ThisNode.Peers := 0;
EnterCriticalSection(CS_ArrayNodes);
ARRAY_Nodes[slot] := ThisNode;
LeaveCriticalSection(CS_ArrayNodes);
end;
CloseSyncingThread;
End;
// Thread FillNodes
constructor TFillThread.Create(const CreateSuspended: Boolean);
Begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
End;
procedure TFillThread.Execute;
Begin
FillNodes;
NodesFilled := true;
FillingNodes :=false;
End;
// Thread TGetSumThread
constructor TGetSumThread.Create(const CreateSuspended: Boolean);
Begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
End;
procedure TGetSumThread.Execute;
Begin
if GetSumary then GoodSumary := true
else GoodSumary := false;
SumReceived := true;
End;
Function GetSyncingThreads():Integer;
Begin
EnterCriticalSection(CS_CSThread);
Result := SyncingThreads;
LeaveCriticalSection(CS_CSThread);
End;
Procedure SetSyncingThreads(value:integer);
Begin
EnterCriticalSection(CS_CSThread);
SyncingThreads := value;
LeaveCriticalSection(CS_CSThread);
End;
Procedure CloseSyncingThread();
Begin
EnterCriticalSection(CS_CSThread);
SyncingThreads := SyncingThreads-1;
LeaveCriticalSection(CS_CSThread);
End;
Function FillNodes():int64;
Var
Counter : integer;
UseThread : TTGetNodeStatus;
StartTime : int64;
LCycles : integer = 0;
Begin
SetSyncingThreads(ArrayNodesLength);
StartTime := GetTickCount64;
For Counter := 0 to ArrayNodesLength-1 do
begin
UseThread := TTGetNodeStatus.Create(True,counter);
UseThread.FreeOnTerminate:=true;
UseThread.Start;
Sleep(1);
end;
LCycles := 0;
REPEAT
sleep(1);
//Inc(LCycles);
LCycles := LCycles+1;
UNTIL ( (GetSyncingThreads <= 0) or (GetTickCount64-StartTime>=5000) );
Result := GetTickCount64-StartTime;
ToLog('main',Format('FN: %d ms / %d Cyc / %d OT',[result,Lcycles,GetSyncingThreads]));
End;
Procedure RunFillNodes();
var
ThisThread : TFillThread;
Begin
FillingNodes :=true;
ThisThread := TFillThread.Create(True);
ThisThread.FreeOnTerminate:=true;
ThisThread.Start;
End;
function GetSumary():boolean;
var
TCPClient : TidTCPClient;
MyStream : TMemoryStream;
DownloadedFile : Boolean = false;
HashLine : string;
RanNode : integer;
ThisNode : string;
Begin
Result := false;
TCPClient := TidTCPClient.Create(nil);
ThisNode := GetRandonNode;
TCPClient.Host := Parameter(ThisNode,0);
TCPClient.Port := StrToIntDef(Parameter(ThisNode,1),8080);
TCPClient.ConnectTimeout:= 1000;
TCPClient.ReadTimeout:=800;
MyStream := TMemoryStream.Create;
TRY
TCPClient.Connect;
TCPClient.IOHandler.WriteLn('GETZIPSUMARY');
TRY
HashLine := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
TCPClient.IOHandler.ReadStream(MyStream);
result := true;
MyStream.SaveToFile(ZipSumaryFilename);
EXCEPT on E:Exception do
begin
end;
END{Try};
EXCEPT on E:Exception do
begin
end;
END{try};
if TCPClient.Connected then TCPClient.Disconnect();
TCPClient.Free;
MyStream.Free;
GettingSum := false;
End;
Procedure RunGetSumary();
var
ThisThread : TGetSumThread;
Begin
GettingSum := True;
ThisThread := TGetSumThread.Create(True);
ThisThread.FreeOnTerminate:=true;
ThisThread.Start;
End;
Function CalculateConsensus():NodeData;
var
counter : integer;
ArrT : array of ConsensusData;
CBlock : integer = 0;
CBranch : string = '';
cPending : integer = 0;
function GetHighest():string;
var
maximum : integer = 0;
counter : integer;
MaxIndex : integer = 0;
Begin
for counter := 0 to length(ArrT)-1 do
begin
if ArrT[counter].count> maximum then
begin
maximum := ArrT[counter].count;
MaxIndex := counter;
end;
end;
result := ArrT[MaxIndex].Value;
End;
Procedure AddValue(Tvalue:String);
var
counter : integer;
added : Boolean = false;
ThisItem : ConsensusData;
Begin
for counter := 0 to length(ArrT)-1 do
begin
if Tvalue = ArrT[counter].Value then
begin
ArrT[counter].count+=1;
Added := true;
end;
end;
if not added then
begin
ThisItem.Value:=Tvalue;
ThisItem.count:=1;
Insert(ThisITem,ArrT,length(ArrT));
end;
End;
Begin
Result := Default(NodeData);
// Get the consensus block number
SetLength(ArrT,0);
For counter := 0 to length (ARRAY_Nodes)-1 do
Begin
AddValue(ARRAY_Nodes[counter].block.ToString);
Result.block := GetHighest.ToInteger;
End;
// Get the consensus summary
SetLength(ArrT,0);
For counter := 0 to length (ARRAY_Nodes)-1 do
Begin
AddValue(ARRAY_Nodes[counter].Branch);
Result.Branch := GetHighest;
End;
// Get the consensus pendings
SetLength(ArrT,0);
For counter := 0 to length (ARRAY_Nodes)-1 do
Begin
AddValue(ARRAY_Nodes[counter].Pending.ToString);
Result.Pending := GetHighest.ToInteger;
End;
// Get the consensus GVTHash
SetLength(ArrT,0);
For counter := 0 to length (ARRAY_Nodes)-1 do
Begin
AddValue(ARRAY_Nodes[counter].GVTHash);
Result.GVTHash := GetHighest;
End;
For counter := 0 to length (ARRAY_Nodes)-1 do
begin
if ( (ARRAY_Nodes[counter].block=Result.block) and (ARRAY_Nodes[counter].Branch = Result.Branch) ) then
ARRAY_Nodes[counter].Synced:=true
else ARRAY_Nodes[counter].Synced:=false;
end;
End;
Procedure RunUpdateGVTs();
Begin
if GetGVTsFile then
begin
REF_GVTS := true;
LoadGVTsFile();
end;
UpdatingGVTs := true;
End;
function GetGVTsFile():boolean;
var
TCPClient : TidTCPClient;
MyStream : TMemoryStream;
DownloadedFile : Boolean = false;
HashLine : string;
RanNode : integer;
ThisNode : string;
Begin
Result := false;
TCPClient := TidTCPClient.Create(nil);
ThisNode := GetRandonNode;
TCPClient.Host := Parameter(ThisNode,0);
TCPClient.Port := StrToIntDef(Parameter(ThisNode,1),8080);
TCPClient.ConnectTimeout:= 1000;
TCPClient.ReadTimeout:=800;
MyStream := TMemoryStream.Create;
TRY
TCPClient.Connect;
TCPClient.IOHandler.WriteLn('NSLGVT');
TRY
HashLine := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
TCPClient.IOHandler.ReadStream(MyStream);
result := true;
MyStream.SaveToFile(GVTFilename);
EXCEPT on E:Exception do
begin
end;
END{Try};
EXCEPT on E:Exception do
begin
end;
END{try};
if TCPClient.Connected then TCPClient.Disconnect();
TCPClient.Free;
MyStream.Free;
End;
INITIALIZATION
InitCriticalSection(CS_CSThread);
FINALIZATION
DoneCriticalSection(CS_CSThread);
END.// END UNIT