-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathUNode.pas
More file actions
1693 lines (1562 loc) · 63.9 KB
/
Copy pathUNode.pas
File metadata and controls
1693 lines (1562 loc) · 63.9 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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit UNode;
{ Copyright (c) 2016 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.
}
{ UNode contains the basic structure to operate
- An app can only contains 1 node.
- A node contains:
- 1 Bank
- 1 NetServer (Accepting incoming connections)
- 1 Operations (Operations has actual BlockChain with Operations and SafeBankTransaction to operate with the Bank)
- 0..x NetClients
- 0..x Miners
}
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
Classes, SysUtils,
{$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF}, UPCDataTypes, UEncoding,
UBlockChain, UNetProtocol, UAccounts, UCrypto, UEPasa, UThread, SyncObjs, ULog, UBaseTypes, UPCOrderedLists;
{$I ./../config.inc}
Type
{ TNode }
TNodeNotifyEvents = Class;
TNode = Class;
TSaveMempoolOperationsThread = Class(TPCThread)
private
FNode : TNode;
FPendingToSave : Boolean;
protected
procedure BCExecute; override;
public
procedure Touch;
End;
TNode = Class(TComponent)
private
FNodeLog : TLog;
FLockMempool : TPCCriticalSection;
FOperationSequenceLock : TPCCriticalSection;
FNotifyList : TList<TNodeNotifyEvents>;
FBank : TPCBank;
FMemPoolOperationsComp : TPCOperationsComp;
FMemPoolAddingOperationsList : TOrderedRawList;
FNetServer : TNetServer;
FBCBankNotify : TPCBankNotify;
FPeerCache : String;
FDisabledsNewBlocksCount : Integer;
FBroadcastData : Boolean;
FUpdateBlockchain: Boolean;
FMaxPayToKeyPurchasePrice: Int64;
FSaveMempoolOperationsThread : TSaveMempoolOperationsThread;
{$IFDEF BufferOfFutureOperations}
FBufferAuxWaitingOperations : TOperationsHashTree;
{$ENDIF}
Procedure OnBankNewBlock(Sender : TObject);
procedure SetNodeLogFilename(const Value: String);
function GetNodeLogFilename: String;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
Class Function Node : TNode;
Class Function NodeExists : Boolean;
Class Procedure DecodeIpStringToNodeServerAddressArray(Const Ips : String; Var NodeServerAddressArray : TNodeServerAddressArray);
Class Function EncodeNodeServerAddressArrayToIpString(Const NodeServerAddressArray : TNodeServerAddressArray) : String;
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
Property Bank : TPCBank read FBank;
Function NetServer : TNetServer;
Procedure NotifyNetClientMessage(Sender : TNetConnection; Const TheMessage : String);
// Return Operations count in the Mempool
function MempoolOperationsCount : Integer;
// Return Account based on current state (Safebox + Mempool operations)
function GetMempoolAccount(AAccountNumber : Cardinal) : TAccount;
// Locking methods to access to the Mempool
function LockMempoolRead : TPCOperationsComp;
procedure UnlockMempoolRead;
function LockMempoolWrite : TPCOperationsComp;
procedure UnlockMempoolWrite;
//
Function AddNewBlockChain(SenderConnection : TNetConnection; NewBlockOperations: TPCOperationsComp; var errors: String): Boolean;
Function AddOperations(SenderConnection : TNetConnection; AOperationsHashTreeToAdd : TOperationsHashTree; OperationsResult : TOperationsResumeList; var errors: String): Integer;
Function AddOperation(SenderConnection : TNetConnection; Operation : TPCOperation; var errors: String): Boolean;
Function SendNodeMessage(Target : TNetConnection; const TheMessage : String; var errors : String) : Boolean;
//
Procedure NotifyBlocksChanged;
//
Function FindOperation(Const AOperationHash : TRawBytes; var AOperationResume : TOperationResume) : TSearchOpHashResult;
Function FindNOperation(block, account, n_operation : Cardinal; var OpResume : TOperationResume) : TSearchOpHashResult;
Function FindNOperations(account, start_block : Cardinal; allow_search_previous : Boolean; n_operation_low, n_operation_high : Cardinal; OpResumeList : TOperationsResumeList) : TSearchOpHashResult;
//
Procedure InitSafeboxAndOperations(max_block_to_read : Cardinal = $FFFFFFFF; restoreProgressNotify : TProgressNotify = Nil);
Procedure AutoDiscoverNodes(Const ips : String);
Function IsBlockChainValid(var WhyNot : String) : Boolean;
Function IsReady(Var CurrentProcess : String) : Boolean;
Property PeerCache : String read FPeerCache write FPeerCache;
Procedure DisableNewBlocks;
Procedure EnableNewBlocks;
Property NodeLogFilename : String read GetNodeLogFilename write SetNodeLogFilename;
Property OperationSequenceLock : TPCCriticalSection read FOperationSequenceLock;
function TryLockNode(MaxWaitMilliseconds : Cardinal) : Boolean;
procedure UnlockNode;
//
function GetAccountsAvailableByPublicKey(const APubKeys : TList<TAccountKey>; out AOnSafebox, AOnMempool : Integer) : Integer; overload;
function GetAccountsAvailableByPublicKey(const APubKey : TAccountKey; out AOnSafebox, AOnMempool : Integer) : Integer; overload;
//
Property BroadcastData : Boolean read FBroadcastData write FBroadcastData;
Property UpdateBlockchain : Boolean read FUpdateBlockchain write FUpdateBlockchain;
procedure MarkVerifiedECDSASignaturesFromMemPool(newOperationsToValidate : TPCOperationsComp);
class function NodeVersion : String;
class function GetPascalCoinDataFolder : String;
class procedure SetPascalCoinDataFolder(const ANewDataFolder : String);
//
function TryFindAccountByKey(const APubKey : TAccountKey; out AAccountNumber : Cardinal) : Boolean;
function TryFindPublicSaleAccount(AMaximumPrice : Int64; APreventRaceCondition : Boolean; out AAccountNumber : Cardinal) : Boolean;
Function TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal): Boolean; overload;
Function TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal; out AErrorMessage: String): Boolean; overload;
Function TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal; out AResolvedKey : TAccountKey; out ARequiresPurchase : boolean): Boolean; overload;
Function TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal; out AResolvedKey : TAccountKey; out ARequiresPurchase : boolean; out AErrorMessage: String): Boolean; overload;
Property MaxPayToKeyPurchasePrice: Int64 read FMaxPayToKeyPurchasePrice write FMaxPayToKeyPurchasePrice;
End;
TThreadSafeNodeNotifyEvent = Class(TPCThread)
FNodeNotifyEvents : TNodeNotifyEvents;
FNotifyBlocksChanged : Boolean;
FNotifyOperationsChanged : Boolean;
Procedure SynchronizedProcess;
protected
procedure BCExecute; override;
public
Constructor Create(ANodeNotifyEvents : TNodeNotifyEvents);
End;
{ TNodeMessage Event }
TNodeMessageEvent = Procedure(NetConnection : TNetConnection; MessageData : String) of object;
{ TNodeMessageManyEvent }
TNodeMessageManyEvent = TArray<TNodeMessageEvent>;
{ TNodeMessageManyEventHelper }
TNodeMessageManyEventHelper = record helper for TNodeMessageManyEvent
procedure Add(listener : TNodeMessageEvent);
procedure Remove(listener : TNodeMessageEvent);
procedure Invoke(NetConnection : TNetConnection; MessageData : String);
end;
{ TNodeNotifyEvents is ThreadSafe and will only notify in the main thread }
TNodeNotifyEvents = Class(TComponent)
private
FNode: TNode;
FOnKeyActivity: TNotifyEvent;
FPendingNotificationsList : TPCThreadList<Pointer>;
FThreadSafeNodeNotifyEvent : TThreadSafeNodeNotifyEvent;
FOnBlocksChanged: TNotifyEvent;
FOnOperationsChanged: TNotifyEvent;
FMessages : TStringList;
FOnNodeMessageEvent: TNodeMessageEvent;
FWatchKeys: TOrderedAccountKeysList;
procedure SetNode(const Value: TNode);
Procedure NotifyBlocksChanged;
Procedure NotifyOperationsChanged;
procedure SetWatchKeys(AValue: TOrderedAccountKeysList);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
Property Node : TNode read FNode write SetNode;
Property OnBlocksChanged : TNotifyEvent read FOnBlocksChanged write FOnBlocksChanged;
Property OnOperationsChanged : TNotifyEvent read FOnOperationsChanged write FOnOperationsChanged;
Property OnNodeMessageEvent : TNodeMessageEvent read FOnNodeMessageEvent write FOnNodeMessageEvent;
Property WatchKeys : TOrderedAccountKeysList read FWatchKeys write SetWatchKeys;
Property OnKeyActivity : TNotifyEvent read FOnKeyActivity write FOnKeyActivity;
End;
TThreadNodeNotifyNewBlock = Class(TPCThread)
FNetConnection : TNetConnection;
FSanitizedOperationsHashTree : TOperationsHashTree;
FNewBlockOperations : TPCOperationsComp;
protected
procedure BCExecute; override;
public
Constructor Create(NetConnection : TNetConnection; MakeACopyOfNewBlockOperations: TPCOperationsComp; MakeACopyOfSanitizedOperationsHashTree : TOperationsHashTree);
destructor Destroy; override;
End;
TThreadNodeNotifyOperations = Class(TPCThread)
FNetConnection : TNetConnection;
protected
procedure BCExecute; override;
public
Constructor Create(NetConnection : TNetConnection; MakeACopyOfOperationsHashTree : TOperationsHashTree);
destructor Destroy; override;
End;
implementation
Uses UOpTransaction, UConst, UTime, UCommon, UPCOperationsSignatureValidator,
UFolderHelper, USettings;
var _Node : TNode;
_PascalCoinDataFolder : String;
{ TNode }
function TNode.AddNewBlockChain(SenderConnection: TNetConnection; NewBlockOperations: TPCOperationsComp; var errors: String): Boolean;
Var i,j,maxResend : Integer;
nc : TNetConnection;
s,sClientRemoteAddr : String;
OpBlock : TOperationBlock;
opsht : TOperationsHashTree;
minBlockResend : Cardinal;
resendOp : TPCOperation;
LLockedMempool : TPCOperationsComp;
begin
Result := false;
errors := '';
if Assigned(SenderConnection) then sClientRemoteAddr := SenderConnection.ClientRemoteAddr
else sClientRemoteAddr:='(SELF)';
if FDisabledsNewBlocksCount>0 then begin
TLog.NewLog(lterror,Classname,Format('Cannot Add new BlockChain due is adding disabled - Connection:%s NewBlock:%s',[
sClientRemoteAddr,TPCOperationsComp.OperationBlockToText(NewBlockOperations.OperationBlock)]));
errors := 'Adding blocks is disabled';
exit;
end;
NewBlockOperations.Lock; // New protection
Try
If NewBlockOperations.OperationBlock.block<>Bank.BlocksCount then begin
errors := 'New block number ('+IntToStr(NewBlockOperations.OperationBlock.block)+') not valid! (Expected '+IntToStr(Bank.BlocksCount)+')';
exit;
end;
OpBlock := NewBlockOperations.OperationBlock;
TLog.NewLog(ltdebug,Classname,Format('Starting AddNewBlockChain %d Operations %d from %s NewBlock:%s',[
OpBlock.block,NewBlockOperations.Count,sClientRemoteAddr,TPCOperationsComp.OperationBlockToText(OpBlock)]));
If Not TPCThread.TryProtectEnterCriticalSection(Self,5000,FLockMempool) then begin
If NewBlockOperations.OperationBlock.block<>Bank.BlocksCount then exit;
s := 'Cannot AddNewBlockChain due blocking lock operations node';
TLog.NewLog(lterror,Classname,s);
if TThread.CurrentThread.ThreadID=MainThreadID then raise Exception.Create(s) else exit;
end;
try
// Check block number:
if TPCOperationsComp.EqualsOperationBlock(Bank.LastOperationBlock,NewBlockOperations.OperationBlock) then begin
errors := 'Duplicated block';
exit;
end;
MarkVerifiedECDSASignaturesFromMemPool(NewBlockOperations); // Improvement speed v4.0.2
// Improvement TNode speed 2.1.6
// Does not need to save a FOperations backup because is Sanitized by "TNode.OnBankNewBlock"
Result := Bank.AddNewBlockChainBlock(NewBlockOperations,TNetData.NetData.NetworkAdjustedTime.GetMaxAllowedTimestampForNewBlock,errors);
if Result then begin
{$IFDEF USE_ABSTRACTMEM}
If Not FBank.IsRestoringFromFile then begin
Bank.SafeBox.PCAbstractMem.FlushCache;
end;
{$ENDIF}
if Assigned(SenderConnection) then begin
FNodeLog.NotifyNewLog(ltupdate,SenderConnection.ClassName,Format(';%d;%s;%s;;%d;%d;%d;%s',[OpBlock.block,sClientRemoteAddr,OpBlock.block_payload.ToPrintable,
OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
end else begin
FNodeLog.NotifyNewLog(ltupdate,ClassName,Format(';%d;%s;%s;;%d;%d;%d;%s',[OpBlock.block,'NIL',OpBlock.block_payload.ToPrintable,
OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
end;
end else begin
if Assigned(SenderConnection) then begin
FNodeLog.NotifyNewLog(lterror,SenderConnection.ClassName,Format(';%d;%s;%s;%s;%d;%d;%d;%s',[OpBlock.block,sClientRemoteAddr,OpBlock.block_payload.ToPrintable,errors,
OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
end else begin
FNodeLog.NotifyNewLog(lterror,ClassName,Format(';%d;%s;%s;%s;%d;%d;%d;%s',[OpBlock.block,'NIL',OpBlock.block_payload.ToPrintable,errors,
OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
end;
end;
if Result then begin
opsht := TOperationsHashTree.Create;
Try
j := Random(5);
If (Bank.LastBlockFound.OperationBlock.block>j) then
minBlockResend:=Bank.LastBlockFound.OperationBlock.block - j
else minBlockResend:=1;
maxResend := CT_MaxResendMemPoolOperations;
i := 0;
LLockedMempool := LockMempoolRead;
Try
While (opsht.OperationsCount<maxResend) And (i<LLockedMempool.Count) do begin
resendOp := LLockedMempool.Operation[i];
j := resendOp.ResendOnBlock;
if ((resendOp.ResendCount<2) and ((j<=0) Or (j<=minBlockResend))) then begin
// Only will "re-send" operations that where received on block <= minBlockResend
opsht.AddOperationToHashTree(resendOp);
// Add to sent operations
resendOp.ResendOnBlock := LLockedMempool.OperationBlock.block;
resendOp.ResendCount := resendOp.ResendCount + 1;
end;
inc(i);
end;
If opsht.OperationsCount>0 then begin
TLog.NewLog(ltinfo,classname,Format('Resending %d operations for new block (Mempool Pending Operations:%d)',[opsht.OperationsCount,LLockedMempool.Count]));
{$IFDEF HIGHLOG}
if opsht.OperationsCount>0 then begin
for i := 0 to opsht.OperationsCount - 1 do begin
TLog.NewLog(ltInfo,ClassName,'Resending ('+inttostr(i+1)+'/'+inttostr(opsht.OperationsCount)+'): '+opsht.GetOperation(i).ToString);
end;
end
{$ENDIF}
end;
Finally
UnlockMempoolRead;
End;
// Notify to clients
{$IFnDEF TESTING_NO_POW_CHECK}
if FBroadcastData then begin
j := TNetData.NetData.ConnectionsCountAll;
for i:=0 to j-1 do begin
if (TNetData.NetData.GetConnection(i,nc)) then begin
if (nc.Connected) And (nc.RemoteOperationBlock.block>0) then begin
if (nc<>SenderConnection) then begin
TThreadNodeNotifyNewBlock.Create(nc,Bank.LastBlockFound,opsht);
end else if (opsht.OperationsCount>0) then begin
// New 4.0.1 Notify not added operations
TThreadNodeNotifyOperations.Create(nc,opsht);
end;
end;
end;
end;
end;
{$ENDIF}
Finally
opsht.Free;
End;
end;
finally
FLockMempool.Release;
TLog.NewLog(ltdebug,Classname,Format('Finalizing AddNewBlockChain %d Operations %d from %s NewBlock:%s',[
OpBlock.block,NewBlockOperations.Count,sClientRemoteAddr,TPCOperationsComp.OperationBlockToText(OpBlock)]));
End;
finally
NewBlockOperations.Unlock;
end;
if Result then begin
// Notify it!
NotifyBlocksChanged;
end;
end;
function TNode.AddOperation(SenderConnection : TNetConnection; Operation: TPCOperation; var errors: String): Boolean;
var ops : TOperationsHashTree;
begin
ops := TOperationsHashTree.Create;
Try
ops.AddOperationToHashTree(Operation);
Result := AddOperations(SenderConnection,ops,Nil,errors)=1;
Finally
ops.Free;
End;
end;
function TNode.AddOperations(SenderConnection : TNetConnection; AOperationsHashTreeToAdd : TOperationsHashTree; OperationsResult : TOperationsResumeList; var errors: String): Integer;
{$IFDEF BufferOfFutureOperations}
Procedure Process_BufferOfFutureOperations(ALockedMempool : TPCOperationsComp; valids_operations : TOperationsHashTree);
Var i,j, nAdded, nDeleted : Integer;
sAcc : TAccount;
ActOp : TPCOperation;
e : String;
Begin
// Prior to add new operations, will try to add waiting ones
nAdded := 0; nDeleted := 0;
For j:=0 to 3 do begin
i := 0;
While (i<FBufferAuxWaitingOperations.OperationsCount) do begin
ActOp := FBufferAuxWaitingOperations.GetOperation(i);
If ALockedMempool.AddOperation(true,ActOp,e) then begin
TLog.NewLog(ltInfo,Classname,Format('AddOperation FromBufferWaitingOperations %d/%d: %s',[i+1,FBufferAuxWaitingOperations.OperationsCount,ActOp.ToString]));
inc(nAdded);
valids_operations.AddOperationToHashTree(ActOp);
FBufferAuxWaitingOperations.Delete(i);
end else begin
sAcc := ALockedMempool.SafeBoxTransaction.Account(ActOp.SignerAccount);
If (sAcc.n_operation>ActOp.N_Operation) Or
((sAcc.n_operation=ActOp.N_Operation) AND (sAcc.balance>0)) then begin
FBufferAuxWaitingOperations.Delete(i);
inc(nDeleted);
end else inc(i);
end;
end;
end;
If (nAdded>0) or (nDeleted>0) or (FBufferAuxWaitingOperations.OperationsCount>0) then begin
TLog.NewLog(ltInfo,Classname,Format('FromBufferWaitingOperations status - Added:%d Deleted:%d Buffer:%d',[nAdded,nDeleted,FBufferAuxWaitingOperations.OperationsCount]));
end;
end;
{$ENDIF}
Var
i,j,nSpam,nError,nRepeated : Integer;
LValids_operations : TOperationsHashTree;
nc : TNetConnection;
e : String;
s : String;
OPR : TOperationResume;
ActOp : TPCOperation;
{$IFDEF BufferOfFutureOperations}sAcc : TAccount;{$ENDIF}
LLockedMempool : TPCOperationsComp;
LOpsToAdd : TList<TPCOperation>;
LTempSafeboxTransaction : TPCSafeBoxTransaction;
LTickCount : TTickCount;
begin
Result := -1; // -1 Means Node is blocked or disabled
if Assigned(OperationsResult) then OperationsResult.Clear;
if FDisabledsNewBlocksCount>0 then begin
errors := Format('Cannot Add Operations due is adding disabled - OpCount:%d',[AOperationsHashTreeToAdd.OperationsCount]);
TLog.NewLog(ltinfo,Classname,errors);
exit;
end;
nSpam := 0;
nRepeated := 0;
nError := 0;
errors := '';
Result := 0;
LTickCount := TPlatform.GetTickCount;
LValids_operations := TOperationsHashTree.Create;
try
LOpsToAdd := TList<TPCOperation>.Create;
try
// In order to allow income operations from multiple threads will divide the
// process in LOCKING steps: (instead of a single global locking)
// 1 - Add received AOperationsHashTreeToAdd in global FMemPoolAddingOperationsList
// without duplicates. This allows receive same operation twice and execute
// only first received
// 2 - Verify signatures in a multithread (if CPU's available)
// 3 - For each not repeated operation, try to add to mempool
// Step 1: Add operations to FMemPoolAddingOperationsList
LLockedMempool := LockMempoolWrite;
try
for i := 0 to AOperationsHashTreeToAdd.OperationsCount-1 do begin
ActOp := AOperationsHashTreeToAdd.GetOperation(i);
j := FMemPoolAddingOperationsList.IndexOf( ActOp.Sha256 );
if (j<0) then begin
LOpsToAdd.Add(ActOp);
FMemPoolAddingOperationsList.Add(ActOp.Sha256);
end;
end;
finally
UnlockMempoolWrite;
end;
// Step 2:
LTempSafeboxTransaction := TPCSafeBoxTransaction.Create(Bank.SafeBox);
try
TPCOperationsSignatureValidator.MultiThreadPreValidateSignatures(LTempSafeboxTransaction,LOpsToAdd,Nil);
finally
LTempSafeboxTransaction.Free;
end;
{$IFDEF BufferOfFutureOperations}
LLockedMempool := LockMempoolWrite;
try
Process_BufferOfFutureOperations(LLockedMempool,LValids_operations);
finally
UnlockMempoolWrite;
end;
{$ENDIF}
// Step 3:
for j := 0 to LOpsToAdd.Count-1 do begin
ActOp := LOpsToAdd[j];
LLockedMempool := LockMempoolWrite;
try
If (LLockedMempool.OperationsHashTree.IndexOfOperation(ActOp)<0) then begin
// Protocol 2 limitation: In order to prevent spam of operations without Fee, will protect it
If (ActOp.OperationFee=0) And (Bank.SafeBox.CurrentProtocol>=CT_PROTOCOL_2) And
(LLockedMempool.OperationsHashTree.CountOperationsBySameSignerWithoutFee(ActOp.SignerAccount)>=CT_MaxAccountOperationsPerBlockWithoutFee) then begin
inc(nSpam);
e := Format('Account %s zero fee operations per block limit:%d',[TAccountComp.AccountNumberToAccountTxtNumber(ActOp.SignerAccount),CT_MaxAccountOperationsPerBlockWithoutFee]);
if (nSpam<=5) then begin // To Limit errors in a String... speed up
if (errors<>'') then errors := errors+' ';
errors := errors+'Op '+IntToStr(j+1)+'/'+IntToStr(LOpsToAdd.Count)+':'+e;
end;
{$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperation spam %d/%d: %s - Error:%s',[(j+1),LOpsToAdd.Count,ActOp.ToString,e]));{$ENDIF}
if Assigned(OperationsResult) then begin
TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
OPR.valid := false;
OPR.NOpInsideBlock:=-1;
OPR.OperationHash := Nil;
OPR.errors := e;
OperationsResult.Add(OPR);
end;
end else begin
if (LLockedMempool.AddOperation(true,ActOp,e)) then begin
inc(Result);
ActOp.DiscoveredOnBlock := LLockedMempool.OperationBlock.block;
LValids_operations.AddOperationToHashTree(ActOp);
{$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperation %d/%d: %s',[(j+1),LOpsToAdd.Count,ActOp.ToString]));{$ENDIF}
if Assigned(OperationsResult) then begin
TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
OPR.NOpInsideBlock:=LLockedMempool.Count-1;
OPR.Balance := LLockedMempool.SafeBoxTransaction.Account(ActOp.SignerAccount).balance;
OperationsResult.Add(OPR);
end;
end else begin
inc(nError);
if (nError<=5) then begin // To Limit errors in a String... speed up
if (errors<>'') then errors := errors+' ';
errors := errors+'Op '+IntToStr(j+1)+'/'+IntToStr(LOpsToAdd.Count)+':'+e;
end;
{$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperation invalid/duplicated %d/%d: %s - Error:%s',[(j+1),LOpsToAdd.Count,ActOp.ToString,e]));{$ENDIF}
if Assigned(OperationsResult) then begin
TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
OPR.valid := false;
OPR.NOpInsideBlock:=-1;
OPR.OperationHash := Nil;
OPR.errors := e;
OperationsResult.Add(OPR);
end;
{$IFDEF BufferOfFutureOperations}
// Used to solve 2.0.0 "invalid order of operations" bug
If (Assigned(SenderConnection)) Then begin
if ActOp.SignerAccount<LLockedMempool.SafeBoxTransaction.FreezedSafeBox.AccountsCount then begin
sAcc := LLockedMempool.SafeBoxTransaction.Account(ActOp.SignerAccount);
If (sAcc.n_operation<ActOp.N_Operation) Or
((sAcc.n_operation=ActOp.N_Operation) AND (sAcc.balance=0) And (ActOp.OperationFee>0) And (ActOp.OpType = CT_Op_Changekey)) then begin
If FBufferAuxWaitingOperations.IndexOfOperation(ActOp)<0 then begin
FBufferAuxWaitingOperations.AddOperationToHashTree(ActOp);
TLog.NewLog(ltInfo,Classname,Format('New FromBufferWaitingOperations %d/%d (new buffer size:%d): %s',[j+1,LOpsToAdd.Count,FBufferAuxWaitingOperations.OperationsCount,ActOp.ToString]));
end;
end;
end;
end;
{$ENDIF}
end;
end;
end else begin
inc(nRepeated);
e := Format('AddOperation made before %d/%d: %s',[(j+1),LOpsToAdd.Count,ActOp.ToString]);
if (nRepeated<=5) then begin // To Limit errors in a String... speed up
if (errors<>'') then errors := errors+' ';
errors := errors + e;
end;
if Assigned(OperationsResult) then begin
TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
OPR.valid := false;
OPR.NOpInsideBlock:=-1;
OPR.OperationHash := Nil;
OPR.errors := e;
OperationsResult.Add(OPR);
end;
{$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperation made before %d/%d: %s',[(j+1),LOpsToAdd.Count,ActOp.ToString]));{$ENDIF}
end;
finally
UnlockMempoolWrite;
end;
end; // for i
If Result<>0 then begin
FSaveMempoolOperationsThread.Touch; // This will indicate to thread that mempool needs to be saved
LTickCount := TPlatform.GetElapsedMilliseconds(LTickCount);
if LTickCount=0 then LTickCount:=1;
if Assigned(SenderConnection) then begin
s := SenderConnection.ClientRemoteAddr;
end else s := '(SELF)';
{$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('Finalizing AddOperations from %s Operations:%d of %d valids:%d spam:%d invalids:%d repeated:%d Miliseconds:%d %.1f ops/sec',
[s,LOpsToAdd.Count,AOperationsHashTreeToAdd.OperationsCount,Result,nSpam,nError,nRepeated,LTickCount,LOpsToAdd.Count * 1000 / LTickCount]));{$ENDIF}
if FBroadcastData then begin
// Send to other nodes
j := TNetData.NetData.ConnectionsCountAll;
for i:=0 to j-1 do begin
If TNetData.NetData.GetConnection(i,nc) then begin
if (nc<>SenderConnection) And (nc.Connected) And (nc.RemoteOperationBlock.block>0) then TThreadNodeNotifyOperations.Create(nc,LValids_operations);
end;
end;
end;
end;
finally
// Remove LOpsToAdd from FMemPoolAddingOperationsList
LLockedMempool := LockMempoolWrite;
try
for i := 0 to LOpsToAdd.Count-1 do begin
ActOp := LOpsToAdd[i];
FMemPoolAddingOperationsList.Remove(ActOp.Sha256);
end;
finally
UnlockMempoolWrite;
end;
LOpsToAdd.Free;
end;
finally
LValids_operations.Free;
end;
// Notify it!
for i := 0 to FNotifyList.Count-1 do begin
TNodeNotifyEvents( FNotifyList[i] ).NotifyOperationsChanged;
end;
end;
procedure TNode.AutoDiscoverNodes(const ips: String);
Var i,j : Integer;
nsarr : TNodeServerAddressArray;
begin
DecodeIpStringToNodeServerAddressArray(ips+';'+PeerCache,nsarr);
for i := low(nsarr) to high(nsarr) do begin
TNetData.NetData.AddServer(nsarr[i]);
end;
j := (CT_MaxServersConnected - TNetData.NetData.ConnectionsCount(true));
if j<=0 then exit;
TNetData.NetData.DiscoverServers;
end;
constructor TNode.Create(AOwner: TComponent);
begin
FMaxPayToKeyPurchasePrice := 0;
FNodeLog := TLog.Create(Self);
FNodeLog.ProcessGlobalLogs := false;
RegisterOperationsClass;
if Assigned(_Node) then raise Exception.Create('Duplicate nodes protection');
TLog.NewLog(ltInfo,ClassName,'TNode.Create '+NodeVersion);
inherited;
FDisabledsNewBlocksCount := 0;
FLockMempool := TPCCriticalSection.Create('TNode_LockMempool');
FOperationSequenceLock := TPCCriticalSection.Create('TNode_OperationSequenceLock');
FBank := TPCBank.Create(Self);
FBCBankNotify := TPCBankNotify.Create(Self);
FBCBankNotify.Bank := FBank;
FBCBankNotify.OnNewBlock := OnBankNewBlock;
FNetServer := TNetServer.Create;
FMemPoolOperationsComp := TPCOperationsComp.Create(Nil);
FMemPoolOperationsComp.bank := FBank;
FNotifyList := TList<TNodeNotifyEvents>.Create;
FMemPoolAddingOperationsList := TOrderedRawList.Create;
{$IFDEF BufferOfFutureOperations}
FBufferAuxWaitingOperations := TOperationsHashTree.Create;
{$ENDIF}
FBroadcastData := True;
FUpdateBlockchain := True;
FSaveMempoolOperationsThread := TSaveMempoolOperationsThread.Create(True);
FSaveMempoolOperationsThread.FNode := Self;
FSaveMempoolOperationsThread.Resume;
if Not Assigned(_Node) then _Node := Self;
end;
class procedure TNode.DecodeIpStringToNodeServerAddressArray(const Ips: String;
var NodeServerAddressArray: TNodeServerAddressArray);
Function GetIp(var ips_string : String; var nsa : TNodeServerAddress) : Boolean;
Const CT_IP_CHARS = ['a'..'z','A'..'Z','0'..'9','.','-','_'];
var i : Integer;
port : String;
begin
nsa := CT_TNodeServerAddress_NUL;
Result := false;
if length(trim(ips_string))=0 then begin
ips_string := '';
exit;
end;
// Delete invalid chars:
i := 0;
while (i<=(ips_string.Length-1)) AND (NOT (ips_string.Chars[i] IN CT_IP_CHARS)) do inc(i);
if (i>0) then ips_string := ips_string.Substring(i,ips_string.Length);
// Capture IP value
i := 0;
while (i<=(ips_string.Length-1)) and (ips_string.Chars[i] in CT_IP_CHARS) do inc(i);
if (i>0) then begin
nsa.ip := ips_string.Substring(0,i);
// Capture possible :Port value
if (i<=(ips_string.Length-1)) and (ips_string.Chars[i]=':') then begin
inc(i);
port := '';
while (i<=(ips_string.Length-1)) and (ips_string.Chars[i] in ['0'..'9']) do begin
port := port + ips_string.Chars[i];
inc(i);
end;
nsa.port := StrToIntDef(port,0);
end;
end;
ips_string := ips_string.Substring(i+1,Length(ips_string));
if nsa.port=0 then nsa.port := CT_NetServer_Port;
Result := (Trim(nsa.ip)<>'');
end;
Var i,j : Integer;
ips_string : String;
nsa : TNodeServerAddress;
begin
SetLength(NodeServerAddressArray,0);
ips_string := Ips;
repeat
If GetIp(ips_string,nsa) then begin
SetLength(NodeServerAddressArray,length(NodeServerAddressArray)+1);
NodeServerAddressArray[High(NodeServerAddressArray)] := nsa;
end;
until (Length(ips_string)=0);
end;
destructor TNode.Destroy;
Var step : String;
begin
TLog.NewLog(ltInfo,ClassName,'TNode.Destroy START');
Try
step := 'Deleting SaveMempoolOperationsThread';
FSaveMempoolOperationsThread.Terminate;
FSaveMempoolOperationsThread.WaitFor;
FreeAndNil(FSaveMempoolOperationsThread);
step := 'Deleting critical section';
FreeAndNil(FLockMempool);
FreeAndNil(FOperationSequenceLock);
step := 'Desactivating server';
FNetServer.Active := false;
step := 'Destroying NetServer';
FreeAndNil(FNetServer);
step := 'Destroying NotifyList';
FreeAndNil(FNotifyList);
step := 'Destroying Operations';
FreeAndNil(FMemPoolOperationsComp);
FreeAndNil(FMemPoolAddingOperationsList);
step := 'Assigning NIL to node var';
if _Node=Self then _Node := Nil;
step := 'Destroying Bank';
FreeAndNil(FBCBankNotify);
FreeAndNil(FBank);
{$IFDEF BufferOfFutureOperations}
FreeAndNil(FBufferAuxWaitingOperations);
{$ENDIF}
step := 'inherited';
FreeAndNil(FNodeLog);
inherited;
Except
On E:Exception do begin
TLog.NewLog(lterror,Classname,'Error destroying Node step: '+step+' Errors ('+E.ClassName+'): ' +E.Message);
Raise;
end;
End;
TLog.NewLog(ltInfo,ClassName,'TNode.Destroy END');
end;
procedure TNode.DisableNewBlocks;
begin
inc(FDisabledsNewBlocksCount);
end;
procedure TNode.EnableNewBlocks;
begin
if FDisabledsNewBlocksCount=0 then raise Exception.Create('Dev error 20160924-1');
dec(FDisabledsNewBlocksCount);
end;
function TNode.TryFindAccountByKey(const APubKey: TAccountKey;
out AAccountNumber: Cardinal): Boolean;
// Finds the smallest numbered account with selected key (or returns false)
var Lpka : TSafeboxPubKeysAndAccounts;
LAccountsNumberList : TAccountsNumbersList;
begin
Result := False;
Lpka := Bank.SafeBox.OrderedAccountKeysList;
if Assigned(Lpka) then begin
LAccountsNumberList := Lpka.GetAccountsUsingThisKey(APubKey);
if Assigned(LAccountsNumberList) then begin
if LAccountsNumberList.Count>0 then begin
AAccountNumber := LAccountsNumberList.Get(0);
Result := True;
end;
end;
end;
end;
function TNode.TryFindPublicSaleAccount(AMaximumPrice: Int64; APreventRaceCondition : Boolean;
out AAccountNumber: Cardinal): Boolean;
// Finds an account at or below argument purchase price (or returns false)
// APreventRaceCondition: When True will return a random account in valid range price
// Limitations: Account must be >0
var LtempAccNumber : Int64;
LLastValidAccount, LCurrAccount : TAccount;
LContinueSearching : Boolean;
begin
Result := False;
// Sorted list: Bank.SafeBox.AccountsOrderedBySalePrice
// Note: List is sorted by Sale price (ASCENDING), but NOT by public/private sale, must check
if Not Bank.SafeBox.AccountsOrderedBySalePrice.FindLowest(LtempAccNumber) then Exit(False);
LCurrAccount := GetMempoolAccount(LtempAccNumber);
if (LCurrAccount.accountInfo.price<=AMaximumPrice)
and (TAccountComp.IsAccountForPublicSale(LCurrAccount.accountInfo)) then begin
LLastValidAccount := LCurrAccount;
LContinueSearching := (APreventRaceCondition) And (Random(50)=0);
end else begin
LLastValidAccount := CT_Account_NUL;
LContinueSearching := True;
end;
while (LCurrAccount.accountInfo.price<=AMaximumPrice) and (LContinueSearching) do begin
if TAccountComp.IsAccountForPublicSale(LCurrAccount.accountInfo) then LLastValidAccount := LCurrAccount;
if Not (Bank.SafeBox.AccountsOrderedBySalePrice.FindSuccessor(LtempAccNumber,LtempAccNumber)) then Break;
LCurrAccount := GetMempoolAccount(LtempAccNumber);
// If price increased, then do not continue and use LastValidAccount
if (LLastValidAccount.account>0)
and (LLastValidAccount.accountInfo.price <> LCurrAccount.accountInfo.price) then Break;
// Continue?
LContinueSearching :=
(LLastValidAccount.account=0) // This means that no valid account has been found yet...
or
(LContinueSearching And (Random(50)=0)); // Random prevention
end;
if (LLastValidAccount.account>0) then begin
AAccountNumber := LLastValidAccount.account;
Result := True;
end else begin
AAccountNumber := 0;
Result := False;
end;
end;
Function TNode.TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal): Boolean;
var LErrMsg : String;
begin
Result := TryResolveEPASA(AEPasa, AResolvedAccount, LErrMsg);
end;
Function TNode.TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal; out AErrorMessage: String): Boolean;
var
LAccountKey : TAccountKey;
LRequiresPurchase : Boolean;
begin
Result := TryResolveEPASA(AEPasa, AResolvedAccount, LAccountKey, LRequiresPurchase, AErrorMessage);
if Result AND AEPasa.IsPayToKey then begin
Result := False;
AErrorMessage := 'EPASA was a pay-to-key style';
end;
end;
Function TNode.TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal; out AResolvedKey : TAccountKey; out ARequiresPurchase : boolean): Boolean;
var LErrMsg : String;
begin
Result := TryResolveEPASA(AEPasa, AResolvedAccount, AResolvedKey, ARequiresPurchase, LErrMsg);
end;
Function TNode.TryResolveEPASA(const AEPasa : TEPasa; out AResolvedAccount: Cardinal; out AResolvedKey : TAccountKey; out ARequiresPurchase : boolean; out AErrorMessage: String): Boolean;
var
LErrMsg : String;
begin
AResolvedAccount := 0;
AResolvedKey.Clear;
ARequiresPurchase := False;
AErrorMessage := '';
if (AEPasa.IsPayToKey) then begin
// Parse account key in EPASA
if NOT TAccountComp.AccountPublicKeyImport(AEPasa.Payload, AResolvedKey, LErrMsg) then begin
AResolvedAccount := CT_AccountNo_NUL;
AResolvedKey := CT_Account_NUL.accountInfo.accountKey;
ARequiresPurchase := False;
AErrorMessage := Format('Invalid key specified in PayToKey EPASA "%s". %s',[AEPasa.ToString(), LErrMsg]);
Exit(False);
end;
// Try to find key in safebox
if TryFindAccountByKey(AResolvedKey, AResolvedAccount) then begin
// Key already exists in SafeBox, so send to that account
ARequiresPurchase := False;
Exit(True);
end;
// If no key found, find optimal public purchase account
if TryFindPublicSaleAccount(MaxPayToKeyPurchasePrice, True, AResolvedAccount) then begin
// Account needs to be purchased
ARequiresPurchase := True;
Exit(True);
end;
// Account could not be resolved
AResolvedAccount := CT_AccountNo_NUL;
AResolvedKey := CT_Account_NUL.accountInfo.accountKey;
ARequiresPurchase := False;
AErrorMessage := 'No account could be resolved for pay to key EPASA';
Exit(False);
end else if (AEPasa.IsAddressedByName) then begin
// Find account by name
AResolvedAccount := Bank.SafeBox.FindAccountByName(AEPasa.AccountName);
AResolvedKey := CT_Account_NUL.accountInfo.accountKey;
ARequiresPurchase := False;
if AResolvedAccount = CT_AccountNo_NUL then begin
// No account with name found
AResolvedAccount := CT_AccountNo_NUL;
AResolvedKey := CT_Account_NUL.accountInfo.accountKey;
ARequiresPurchase := False;
AErrorMessage := Format('No account with name "%s" was found', [AEPasa.AccountName]);
Exit(False);
end;
Exit(True);
end;
// addressed by number
if NOT AEPasa.IsAddressedByNumber then raise Exception.Create('Internal Error c8ecd69d-3621-4f5e-b4f1-9926ab2f5013');
if NOT AEPasa.Account.HasValue then raise Exception.Create('Internal Error 544c8cb9-b700-4b5f-93ca-4d045d0a06ae');
AResolvedAccount := AEPasa.Account.Value;
if (AResolvedAccount < 0) or (AResolvedAccount >= Self.Bank.AccountsCount) then begin
AResolvedAccount := CT_AccountNo_NUL;
AResolvedKey := CT_Account_NUL.accountInfo.accountKey;
ARequiresPurchase := False;
AErrorMessage := Format('Account number %d does not exist in safebox',[AEPasa.Account.Value]);
Exit(False);
end;
Result := true;
end;
function TNode.TryLockNode(MaxWaitMilliseconds: Cardinal): Boolean;
begin
Result := TPCThread.TryProtectEnterCriticalSection(Self,MaxWaitMilliseconds,FLockMempool);
end;
procedure TNode.UnlockNode;
begin
FLockMempool.Release;
end;
procedure TNode.MarkVerifiedECDSASignaturesFromMemPool(newOperationsToValidate: TPCOperationsComp);
var LLockedMempool : TPCOperationsComp;
begin
// Introduced on Build 4.0.2 to increase speed using MEMPOOL verified operations instead of verify again everytime
// Will check if "newOperationsToValidate" operations are on MEMPOOL. If found, will set same FHasValidSignature value in order to mark as verified
LLockedMempool := LockMempoolRead;
try
if newOperationsToValidate = LLockedMempool then Exit; // Is the same, do nothing
if newOperationsToValidate.OperationBlock.protocol_version <> newOperationsToValidate.OperationBlock.protocol_version then Exit; // Must be same protocol
newOperationsToValidate.Lock;
try
LLockedMempool.OperationsHashTree.MarkVerifiedECDSASignatures(newOperationsToValidate.OperationsHashTree);
finally
newOperationsToValidate.Unlock;
end;
finally
UnlockMempoolRead;
end;
end;
class function TNode.EncodeNodeServerAddressArrayToIpString(const NodeServerAddressArray: TNodeServerAddressArray): String;
var i : Integer;
begin
Result := '';
for i := low(NodeServerAddressArray) to high(NodeServerAddressArray) do begin
if (Result<>'') then Result := Result + ';';
Result := Result + NodeServerAddressArray[i].ip;
if NodeServerAddressArray[i].port>0 then begin
Result := Result + ':'+IntToStr(NodeServerAddressArray[i].port);
end;
end;
end;
function TNode.GetNodeLogFilename: String;
begin
Result := FNodeLog.FileName;
end;
function TNode.IsBlockChainValid(var WhyNot : String): Boolean;
Var unixtimediff : Integer;
begin
Result :=false;
if (TNetData.NetData.NetStatistics.ActiveConnections<=0) then begin
WhyNot := 'No connection to check blockchain';
exit;
end;
if (Bank.LastOperationBlock.block<=0) then begin
WhyNot := 'No blockchain';
exit;
end;