-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathUPCOperationsBlockValidator.pas
More file actions
358 lines (310 loc) · 10.1 KB
/
Copy pathUPCOperationsBlockValidator.pas
File metadata and controls
358 lines (310 loc) · 10.1 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
unit UPCOperationsBlockValidator;
{ Copyright (c) 2019 by Albert Molina
Distributed under the MIT software license, see the accompanying file LICENSE
or visit http://www.opensource.org/licenses/mit-license.php.
This unit is a part of the PascalCoin Project, an infinitely scalable
cryptocurrency. Find us here:
Web: https://www.pascalcoin.org
Source: https://github.com/PascalCoin/PascalCoin
If you like it, consider a donation using Bitcoin:
16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
THIS LICENSE HEADER MUST NOT BE REMOVED.
}
{
This unit adds a TPCOperationsBlockValidator class that will check
TOperationBlock in a multithread mode
NOTE: This object is only a helper for fast processing speed when
multithreading can help. There is no warranty that will validate all
}
interface
{$I ./../config.inc}
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
Uses UThread, UAccounts, UPCOrderedLists, UBlockChain, Classes, UPCDataTypes,
{$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF};
type
TPCOperationsBlockValidator = Class;
TPCOperationsBlockValidatorThread = Class(TPCThread)
private
FValidator : TPCOperationsBlockValidator;
//
protected
procedure BCExecute; override;
public
Constructor Create(AValidator : TPCOperationsBlockValidator);
End;
TPCOperationsBlockValidator = Class
private
FLockedList : TPCThreadList<TOperationBlock>;
FThreads : TList<TPCOperationsBlockValidatorThread>;
FErrorsList : TStringList;
//
FPCOperationsCompList : TList<TPCOperationsComp>; // Optional field
FLastIndexOperationsBlock : Integer;
//
FValidatedOkCount : Integer;
FValidatedErrorCount : Integer;
protected
function GetNextOperationBlock(var ANextOperationBlock : TOperationBlock; var AIndex : Integer) : Boolean;
procedure SetOperationBlockResult(const AOperationBlock : TOperationBlock; AIndex : Integer; AValidated : Boolean; const AErrorDetected : String);
public
Constructor Create;
destructor Destroy; override;
procedure AddToValidate(AList : TList<TOperationBlock>); overload;
procedure AddToValidate(AOperationBlock : TOperationBlock); overload;
procedure StartThreads;
procedure GetStatus(out AValidatedOk, AValidatedError, APendingToValidate : Integer);
procedure WaitUntilAllValidatedOrErrorFound;
procedure FillErrors(var AErrors : String);
procedure EndThreads;
property ValidatedOkCount : Integer read FValidatedOkCount;
property ValidatedErrorCount : Integer read FValidatedErrorCount;
function ValidateAndWaitUntilTerminate(APCOperationsCompList : TList<TPCOperationsComp>; var AValidatedOkCount, AValidatedErrorCount : Integer) : Integer;
class function MultiThreadValidateOperationsBlock(APCOperationsCompList : TList<TPCOperationsComp>) : Boolean;
End;
implementation
Uses
SysUtils,
ULog, UBaseTypes,
UCommon;
var _Cpus : Integer = 0;
{ TPCOperationsBlockValidator }
procedure TPCOperationsBlockValidator.AddToValidate(AList: TList<TOperationBlock>);
var i : Integer;
LList : TList<TOperationBlock>;
begin
LList := FLockedList.LockList;
try
for i := 0 to AList.Count-1 do begin
LList.Add(AList[i]);
end;
finally
FLockedList.UnlockList;
end;
end;
procedure TPCOperationsBlockValidator.AddToValidate(AOperationBlock: TOperationBlock);
var LList : TList<TOperationBlock>;
begin
LList := FLockedList.LockList;
try
LList.Add(AOperationBlock);
finally
FLockedList.UnlockList;
end;
end;
constructor TPCOperationsBlockValidator.Create;
begin
FLastIndexOperationsBlock := -1;
FLockedList := TPCThreadList<TOperationBlock>.Create(ClassName);
FPCOperationsCompList := Nil; // This field is external
FThreads := Nil;
FErrorsList := TStringList.Create;
end;
destructor TPCOperationsBlockValidator.Destroy;
begin
EndThreads;
FreeAndNil(FLockedList);
FreeAndNil(FErrorsList);
inherited;
end;
procedure TPCOperationsBlockValidator.EndThreads;
var i : Integer;
begin
FLockedList.LockList;
try
if Not Assigned(FThreads) then Exit;
for i := 0 to FThreads.Count-1 do begin
FThreads[i].Terminate;
end;
finally
FLockedList.UnlockList;
end;
// WaitFor without locking
for i := 0 to FThreads.Count-1 do begin
FThreads[i].WaitFor;
end;
// Finished
FLockedList.LockList;
try
for i := 0 to FThreads.Count-1 do begin
FThreads[i].Free;
end;
FreeAndNil(FThreads);
finally
FLockedList.UnlockList;
end;
end;
procedure TPCOperationsBlockValidator.FillErrors(var AErrors: String);
begin
FLockedList.LockList;
try
AErrors := FErrorsList.Text;
finally
FLockedList.UnlockList;
end;
end;
function TPCOperationsBlockValidator.GetNextOperationBlock(var ANextOperationBlock : TOperationBlock; var AIndex : Integer) : Boolean;
var LList : TList<TOperationBlock>;
begin
LList := FLockedList.LockList;
try
// Search new
AIndex := FLastIndexOperationsBlock + 1; // Move to next
if (AIndex<LList.Count) then begin
ANextOperationBlock := LList[AIndex];
Result := True;
FLastIndexOperationsBlock := AIndex;
end else Result := False;
finally
FLockedList.UnlockList;
end;
end;
procedure TPCOperationsBlockValidator.GetStatus(out AValidatedOk, AValidatedError, APendingToValidate: Integer);
var LList : TList<TOperationBlock>;
begin
LList := FLockedList.LockList;
try
AValidatedOk := FValidatedOkCount;
AValidatedError := FValidatedErrorCount;
if LList.Count>0 then
APendingToValidate := LList.Count - (FLastIndexOperationsBlock + 1)
else APendingToValidate := 0;
finally
FLockedList.UnlockList;
end;
end;
class function TPCOperationsBlockValidator.MultiThreadValidateOperationsBlock(APCOperationsCompList: TList<TPCOperationsComp>): Boolean;
var LMultiThreadValidator : TPCOperationsBlockValidator;
LValidatedOk, LValidatedError, LValidatedTotal : Integer;
LTC : TTickCount;
begin
if _Cpus<=0 then begin
_Cpus := TCPUTool.GetLogicalCPUCount;
end;
if _Cpus<=1 then Exit;
if APCOperationsCompList.Count<_Cpus then Exit; // If less than cpus, no need for multithreading...
LTC := TPlatform.GetTickCount;
LMultiThreadValidator := TPCOperationsBlockValidator.Create;
try
LValidatedTotal := LMultiThreadValidator.ValidateAndWaitUntilTerminate(APCOperationsCompList,LValidatedOk,LValidatedError);
LTC := TPlatform.GetElapsedMilliseconds(LTC);
if (LValidatedTotal>0) and (LTC>0) then begin
TLog.NewLog(ltdebug,ClassName,Format('Validated %d Operation blocks info with %d valids and %d Errors in %d miliseconds avg %.2f op/sec',[LValidatedTotal,LValidatedOk,LValidatedError,LTC,LValidatedTotal*1000/LTC]));
end;
Result := LValidatedOk = LValidatedTotal;
finally
LMultiThreadValidator.Free;
end;
end;
procedure TPCOperationsBlockValidator.SetOperationBlockResult(const AOperationBlock : TOperationBlock; AIndex : Integer; AValidated: Boolean; const AErrorDetected : String);
var LList : TList<TOperationBlock>;
begin
LList := FLockedList.LockList;
try
if AValidated then inc(FValidatedOkCount)
else begin
FErrorsList.Add(AErrorDetected + ' ' + TPCOperationsComp.OperationBlockToText(AOperationBlock) );
inc(FValidatedErrorCount);
end;
if Assigned(FPCOperationsCompList) then begin
FPCOperationsCompList[AIndex].HasValidOperationBlockInfo := AValidated;
end;
finally
FLockedList.UnlockList;
end;
end;
procedure TPCOperationsBlockValidator.StartThreads;
var LMaxThreads : Integer;
i : Integer;
LList : TList<TOperationBlock>;
begin
EndThreads; // Ensure no active threads
FValidatedOkCount := 0;
FValidatedErrorCount := 0;
FLastIndexOperationsBlock := -1;
FErrorsList.Clear;
if _Cpus<=0 then begin
_Cpus := TCPUTool.GetLogicalCPUCount;
end;
if (_Cpus>2) then LMaxThreads := _Cpus-1
else LMaxThreads := _Cpus;
if (LMaxThreads<=0) then LMaxThreads := 1;
if (LMaxThreads>7) then LMaxThreads := 7;
FThreads := TList<TPCOperationsBlockValidatorThread>.Create;
FLockedList.LockList;
try
for i := 1 to LMaxThreads do begin
FThreads.Add( TPCOperationsBlockValidatorThread.Create(Self) );
end;
for i := 0 to FThreads.Count-1 do begin
FThreads[i].Suspended := False;
end;
finally
FLockedList.UnlockList;
end;
end;
function TPCOperationsBlockValidator.ValidateAndWaitUntilTerminate(APCOperationsCompList : TList<TPCOperationsComp>; var AValidatedOkCount, AValidatedErrorCount : Integer) : Integer;
var
i,LTerminatedThreads : Integer;
LList : TList<TOperationBlock>;
begin
FValidatedOkCount := 0;
FValidatedErrorCount := 0;
if APCOperationsCompList.Count<=0 then Exit(0);
EndThreads;
LList := FLockedList.LockList;
try
for i := 0 to APCOperationsCompList.Count-1 do begin
LList.Add(APCOperationsCompList[i].OperationBlock);
end;
finally
FLockedList.UnlockList;
end;
FPCOperationsCompList := APCOperationsCompList;
StartThreads;
try
// Wait until error of finalized
WaitUntilAllValidatedOrErrorFound;
Finally
EndThreads;
End;
AValidatedOkCount := FValidatedOkCount;
AValidatedErrorCount := FValidatedErrorCount;
Result := FPCOperationsCompList.Count;
end;
procedure TPCOperationsBlockValidator.WaitUntilAllValidatedOrErrorFound;
var LContinue : Boolean;
LList : TList<TOperationBlock>;
begin
repeat
Sleep(10);
LList := FLockedList.LockList;
try
LContinue := (FValidatedErrorCount=0) And (FLastIndexOperationsBlock+1<LList.Count);
finally
FLockedList.UnlockList;
end;
until (Not LContinue);
end;
{ TPCOperationsBlockValidatorThread }
procedure TPCOperationsBlockValidatorThread.BCExecute;
var LOperationBlock : TOperationBlock;
LErrors : String;
LValidated : Boolean;
LIndex : Integer;
begin
repeat
if FValidator.GetNextOperationBlock( LOperationBlock, LIndex ) then begin
LValidated := TPCSafeBox.IsValidOperationBlock(LOperationBlock,LErrors);
FValidator.SetOperationBlockResult(LOperationBlock, LIndex, LValidated, LErrors);
end else Sleep(1);
until (Terminated);
end;
constructor TPCOperationsBlockValidatorThread.Create(AValidator : TPCOperationsBlockValidator);
begin
FValidator := AValidator;
inherited Create(True);
FreeOnTerminate := False;
end;
end.