forked from PascalCoinDev/PascalCoin
/
UAbstractMemBlockchainStorage.pas
2337 lines (2109 loc) · 88.6 KB
/
UAbstractMemBlockchainStorage.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
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 UAbstractMemBlockchainStorage;
{ Copyright (c) 2022 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.
}
{$IFDEF FPC}
{$mode delphi}
{$ENDIF}
interface
{$I ./../config.inc}
uses
Classes, SysUtils, UBlockchain, UThread, UCrypto, math, UAccounts, ULog,
SyncObjs,
{$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF},
UCommon,
UBaseTypes, UPCDataTypes,
UAbstractMem, UFileMem, UCacheMem,
UAbstractMemBTree, UOrderedList,
UFileStorage,
UConst;
type
EAbstractMemBlockchainStorage = Class(Exception);
TBlockchainStorageStats = record
blockInformationCount : Int64;
operationRawDataCount : Int64;
affectedAccountCount : Int64;
startTC : TTickCount;
procedure Clear;
function ToString : String;
function ThroughputPerSecond : Double;
procedure AddTo(var ADest : TBlockchainStorageStats);
end;
PBlockchainStorageStats = ^TBlockchainStorageStats;
{ TAbstractMemBlockchainStorage }
TAbstractMemBlockchainStorage = Class(TStorage)
private
FFileMem : TFileMem;
FStorageLock : TCriticalSection;
FAutoFlushCache: Boolean;
FUseMultithread: Boolean;
FSaveStorageStats : TBlockchainStorageStats;
FPSaveStorageStats: PBlockchainStorageStats;
procedure SetUseMultithread(const Value: Boolean);
type
TOrphanInformation = record
orphan : string;
regsCounter : Integer;
procedure Clear;
procedure CopyFrom(const ASource : TOrphanInformation);
end;
TAMBTreeOrphanInformationByOrphan = Class(TAbstractMemBTreeData<TOrphanInformation>)
protected
function LoadData(const APosition : TAbstractMemPosition) : TOrphanInformation; override;
function SaveData(const AData : TOrphanInformation) : TAMZone; override;
public
procedure Update(const AOrphan : String; AIncrement : Integer);
function GetRegsCountByOrphan(const AOrphan : String) : Integer;
End;
TBlockInformation = record
operationBlock : TOperationBlock;
orphan : String;
operationsCount : Integer;
volume : Int64;
rawDataPosition : TAbstractMemPosition;
procedure Clear;
procedure SetToFindByBlock(ABlock : Integer);
function ToSerialized : TBytes;
function FromSerialized(ABytes : TBytes) : Boolean;
function GetRawData(AAbstractMem : TAbstractMem; var ARawData : TBytes) : Boolean;
function CreateTPCOperationsComp(AAbstractMem : TAbstractMem; ABank: TPCBank) : TPCOperationsComp;
procedure ReadTPCOperationsComp(AAbstractMem : TAbstractMem; AOperationsComp : TPCOperationsComp);
function IsOrphan(const AOrphan : String) : Boolean;
procedure CopyFrom(const ASource : TBlockInformation);
end;
TAMBTreeOperationBlockInformationByOrphanBlock = Class(TAbstractMemBTreeData<TBlockInformation>)
protected
function LoadData(const APosition : TAbstractMemPosition) : TBlockInformation; override;
function SaveData(const AData : TBlockInformation) : TAMZone; override;
procedure DeletedData(const AData: TBlockInformation); override;
public
function GetBlockInformationByBlock(ABlock : Integer) : TBlockInformation;
End;
TOperationRawData = record
rightOpHash : TBytes;
account : Integer;
n_operation : Integer;
block : Integer;
opblock : Integer;
opType : Integer;
opSavedProtocol : Integer;
rawData : TBytes;
procedure Clear;
procedure SetToFindByRightOpHash(const ARightOpHash : TBytes);
procedure SetToFindByBlockOpblock(ABlock, AOpblock : Integer);
function ToSerialized : TBytes;
function FromSerialized(ABytes : TBytes) : Boolean;
procedure CopyFrom(const ASource : TOperationRawData);
function CreateTPCOperation(AAbstractMem : TAbstractMem) : TPCOperation; overload;
function CreateTPCOperation(AAbstractMem : TAbstractMem; out APCOperation : TPCOperation) : Boolean; overload;
end;
TAMBTreeTOperationRawDataByRightOpHash = Class(TAbstractMemBTreeData<TOperationRawData>)
protected
function LoadData(const APosition : TAbstractMemPosition) : TOperationRawData; override;
function SaveData(const AData : TOperationRawData) : TAMZone; override;
End;
TAMBTreeTOperationRawDataByBlockOpBlock_Index = Class(TAbstractMemBTreeDataIndex<TOperationRawData>)
End;
TAffectedAccount = record
account : Integer;
n_operation : Integer;
block : Integer;
opblock : Integer;
procedure Clear;
procedure SetToFindByAccount(AAccount : Integer);
procedure SetToFindByAccountBlockOpblock(AAccount, ABlock, AOpblock : Integer);
function ToSerialized : TBytes;
function FromSerialized(ABytes : TBytes) : Boolean;
procedure CopyFrom(const ASource : TAffectedAccount);
function ToString:String;
end;
TAMBTreeTAffectedAccountByAccountBlockOpBlock = Class(TAbstractMemBTreeData<TAffectedAccount>)
protected
function LoadData(const APosition : TAbstractMemPosition) : TAffectedAccount; override;
function SaveData(const AData : TAffectedAccount) : TAMZone; override;
End;
TPendingData = record
operation : TOperationRawData;
affectedAccounts : Array of TAffectedAccount;
procedure Clear;
end;
TPendingToSaveThread = Class;
TPendingToSave = Class
private
FAMStorage : TAbstractMemBlockchainStorage;
FMaxThreads : Integer;
FPending : TThreadList<TPendingData>;
FThreads : TThreadList<TPendingToSaveThread>;
FOperationsRawData_By_RightOpHash : TAMBTreeTOperationRawDataByRightOpHash;
FAffectedAccounts_By_Account_Block_OpBlock : TAMBTreeTAffectedAccountByAccountBlockOpBlock;
FTotal: Integer;
FMaxPendingsCount: Integer;
FLastLogTC : TTickCount;
procedure SetMaxThreads(const Value: Integer);
protected
procedure ThreadHasFinishedCurrentJob;
public
procedure AddPendingData(const APendingData : TPendingData);
constructor Create(AStorage : TAbstractMemBlockchainStorage; AAMBTreeTOperationRawDataByRightOpHash : TAMBTreeTOperationRawDataByRightOpHash;
AAMBTreeTAffectedAccountByAccountBlockOpBlock : TAMBTreeTAffectedAccountByAccountBlockOpBlock);
destructor Destroy; override;
property MaxThreads : Integer read FMaxThreads write SetMaxThreads;
function PendingsCount : Integer;
property Total : Integer read FTotal write FTotal;
property MaxPendingsCount : Integer read FMaxPendingsCount write FMaxPendingsCount;
End;
TPendingToSaveThread = Class(TPCThread)
private
FPendingToSave : TPendingToSave;
FBusy: Boolean;
protected
procedure BCExecute; override;
public
Constructor Create(APendingToSave : TPendingToSave);
property Busy : Boolean read FBusy write FBusy;
End;
var
FOrphansInformation_By_Orphan : TAMBTreeOrphanInformationByOrphan;
FBlocksInformation_By_OrphanBlock : TAMBTreeOperationBlockInformationByOrphanBlock;
FOperationsRawData_By_RightOpHash : TAMBTreeTOperationRawDataByRightOpHash;
FOperationsRawData_By_Block_OpBlock_Index : TAMBTreeTOperationRawDataByBlockOpBlock_Index;
FAffectedAccounts_By_Account_Block_OpBlock : TAMBTreeTAffectedAccountByAccountBlockOpBlock;
FPendingToSave : TPendingToSave;
FCheckingConsistency : Boolean;
FCheckingConsistencyProgress : String;
FCheckingConsistencyStats : TBlockchainStorageStats;
FLogSaveActivity : Boolean;
FInBlockNotFound : Boolean;
FInBlockSaving : Integer;
function GetFirstBlockNumberByOrphan(const AOrphan : String): Int64;
function GetLastBlockNumberByOrphan(const AOrphan : String): Int64;
Function DoBlockExistsByOrphan(ABlock : Integer; const AOrphan : String; var LBlockInformation : TBlockInformation) : Boolean;
function DeleteBlockChainBlockExt(ABlock : Integer; const AOrphan : String) : Boolean;
Function DoSaveBlockChainExt(Operations : TPCOperationsComp; const AOrphan : String; var AStats : TBlockchainStorageStats) : Boolean;
Function DoLoadBlockChainExt(Operations : TPCOperationsComp; Block : Cardinal; const AOrphan : String) : Boolean;
procedure AddMessage(AMessages : TStrings; const AMessage : String; ARaiseAnException : Boolean);
procedure OnCacheMemFlushedCache(const ASender : TCacheMem; const AProcessDesc : String; AElapsedMilis: Int64);
procedure OnCacheMemLog(ASender : TObject; const ALog : String);
protected
procedure SetReadOnly(const Value: Boolean); override;
Function DoGetBlockInformation(const ABlock : Integer; var AOperationBlock : TOperationBlock; var AOperationsCount : Integer; var AVolume : Int64) : Boolean; override;
Function DoLoadBlockChain(Operations : TPCOperationsComp; Block : Cardinal) : Boolean; override;
Function DoSaveBlockChain(Operations : TPCOperationsComp) : Boolean; override;
Function DoMoveBlockChain(StartBlock : Cardinal; Const DestOrphan : TOrphan) : Boolean; override;
Procedure DoDeleteBlockChainBlocks(StartingDeleteBlock : Cardinal); override;
Function DoBlockExists(Block : Cardinal) : Boolean; override;
function GetFirstBlockNumber: Int64; override;
function GetLastBlockNumber: Int64; override;
function DoInitialize : Boolean; override;
Procedure DoEraseStorage; override;
function CheckBlockConsistency(ARaiseOnError : Boolean; AMessages : TStrings; const ABlockInformation : TBlockInformation; out AOperationsCount, AAffectedAccountsCount : Integer; AThread : TPCThread) : Boolean;
procedure DoBlockNotFound(ABlock : Integer; var AFound : Boolean); virtual;
procedure BlockNotFound(ABlock : Integer; var AFound : Boolean);
Function DoGetBlockOperations(ABlock, AOpBlockStartIndex, AMaxOperations : Integer; var AOperationBlock : TOperationBlock; var AOperationsCount : Integer; var AVolume : Int64; const AOperationsResumeList:TOperationsResumeList) : Boolean; override;
Function DoGetAccountOperations(AAccount : Integer; AMaxDepth, AStartOperation, AMaxOperations, ASearchBackwardsStartingAtBlock: Integer; const AOperationsResumeList:TOperationsResumeList): Boolean; override;
function DoFindOperation(const AOpHash : TBytes; var AOperationResume : TOperationResume) : TSearchOpHashResult; override;
Function DoGetOperation(const ABlock, AOpBlock : Integer; const AOperations : TOperationsHashTree) : Boolean;
public
Constructor Create(AOwner : TComponent); Override;
Destructor Destroy; Override;
procedure FinalizedUpdating;
procedure CheckConsistency(ARaiseOnError: Boolean; AMessages: TStrings; AThread : TPCThread); overload;
function CheckConsistency(const AOrphan : String; AFromBlock , AToBlock : Integer; ARaiseOnError : Boolean; AMessages : TStrings; out ABlocksFound, AOperationsFound, AAffectedAccountsFound : Integer; AThread : TPCThread) : Boolean; overload;
property FileMem : TFileMem read FFileMem;
property AutoFlushCache : Boolean read FAutoFlushCache write FAutoFlushCache;
Procedure FillInfo(AStrings : TStrings);
class function OrphanCompare(const ALeft, ARight : String) : Integer; inline;
function PendingToSave : Integer;
procedure AbortPendingToSave;
property UseMultithread : Boolean read FUseMultithread write SetUseMultithread;
property CheckingConsistency : Boolean read FCheckingConsistency;
property CheckingConsistencyProgress : String read FCheckingConsistencyProgress;
property CheckingConsistencyStats : TBlockchainStorageStats read FCheckingConsistencyStats;
property SaveStorageStats : PBlockchainStorageStats read FPSaveStorageStats;
property LogSaveActivity : Boolean read FLogSaveActivity write FLogSaveActivity;
End;
TAbstractMemBlockchainStorageSecondary = Class(TAbstractMemBlockchainStorage)
private
FAuxStorage : TStorage;
FSaving : Boolean;
protected
procedure SetReadOnly(const Value: Boolean); override;
Function DoSaveBlockChain(Operations : TPCOperationsComp) : Boolean; override;
Function DoMoveBlockChain(StartBlock : Cardinal; Const DestOrphan : TOrphan) : Boolean; override;
Procedure DoDeleteBlockChainBlocks(StartingDeleteBlock : Cardinal); override;
function DoInitialize : Boolean; override;
procedure DoBlockNotFound(ABlock : Integer; var AFound : Boolean); override;
public
Constructor Create(AOwner : TComponent); Override;
Destructor Destroy; Override;
property AuxStorage : TStorage read FAuxStorage;
End;
implementation
function Comparer_TOrphanInformation_By_Orphan(const ALeft, ARight : TAbstractMemBlockchainStorage.TOrphanInformation) : Integer;
begin
Result := TAbstractMemBlockchainStorage.OrphanCompare(ALeft.orphan,ARight.orphan);
end;
function Comparer_TBlockInformation_By_OrphanBlock(const ALeft, ARight : TAbstractMemBlockchainStorage.TBlockInformation) : Integer;
begin
Result := TAbstractMemBlockchainStorage.OrphanCompare(ALeft.orphan,ARight.orphan);
if Result=0 then begin
if ALeft.operationBlock.block<ARight.operationBlock.block then Result := -1
else if ALeft.operationBlock.block>ARight.operationBlock.block then Result := 1
else Result := 0;
end;
end;
function Comparer_TOperationRawData_By_RightOpHash(const ALeft, ARight : TAbstractMemBlockchainStorage.TOperationRawData) : Integer;
begin
Result := BytesCompare(ALeft.rightOpHash,ARight.rightOpHash);
end;
function Comparer_TOperationRawData_By_Block_OpBlock(const ALeft, ARight : TAbstractMemBlockchainStorage.TOperationRawData) : Integer;
begin
Result := ALeft.block - ARight.block;
if Result=0 then Result := ALeft.opblock - ARight.opblock;
end;
function Comparer_TAffectedAccount_By_Account_Block_OpBlock(const ALeft, ARight : TAbstractMemBlockchainStorage.TAffectedAccount) : Integer;
begin
Result := ALeft.account - ARight.account;
if Result=0 then Result := ALeft.block - ARight.block;
if Result=0 then Result := ALeft.opblock - ARight.opblock;
end;
{ TAbstractMemBlockchainStorage }
procedure TAbstractMemBlockchainStorage.AbortPendingToSave;
begin
FreeAndNil(FPendingToSave);
end;
procedure TAbstractMemBlockchainStorage.AddMessage(AMessages: TStrings; const AMessage: String; ARaiseAnException : Boolean);
begin
if Assigned(AMessages) then AMessages.Add(AMessage);
if ARaiseAnException then raise EAbstractMemBlockchainStorage.Create(AMessage);
TLog.NewLog(ltinfo,ClassName,AMessage);
FCheckingConsistencyProgress := AMessage;
end;
procedure TAbstractMemBlockchainStorage.BlockNotFound(ABlock: Integer; var AFound : Boolean);
begin
AFound := False;
if FInBlockNotFound then begin
TLog.NewLog(ltdebug,ClassName,Format('BlockNotFound cannot save Block:%d because saving block:%d',[ABlock,FInBlockSaving]));
Exit;
end;
FInBlockNotFound := True;
try
FInBlockSaving := ABlock;
DoBlockNotFound(ABlock,AFound);
finally
FInBlockNotFound := False;
end;
end;
function TAbstractMemBlockchainStorage.CheckBlockConsistency(
ARaiseOnError: Boolean; AMessages: TStrings; const ABlockInformation : TBlockInformation; out AOperationsCount, AAffectedAccountsCount : Integer;
AThread : TPCThread): Boolean;
var LErrorsCount : Integer;
procedure AddInfo(AIsError : Boolean; const AMessage : String);
Var LTxt : String;
begin
if AIsError then begin
Result := False;
inc(LErrorsCount);
LTxt := 'ERROR: '+AMessage;
end else LTxt := AMessage;
AddMessage(AMessages,LTxt,(AIsError and ARaiseOnError));
end;
var
LOperationsCounter : Integer;
LOperationRawDataToSearch, LOperationRawData,
LOperationRawDataAux, LOperationRawDataAuxFound : TOperationRawData;
LOperationsComp : TPCOperationsComp;
LOperation : TPCOperation;
LRawData : TBytes;
LError : String;
LPos : TAbstractMemPosition;
LAffectedAccountsList : TOrderedList<Cardinal>;
LAffectedAccount, LAffectedAccountFound : TAffectedAccount;
i : Integer;
begin
Result := True;
LErrorsCount := 0;
AAffectedAccountsCount := 0;
AOperationsCount := 0;
if Not ABlockInformation.GetRawData(FFileMem,LRawData) then begin
AddInfo(True,Format('Cannot obtain raw data from block %d at pos %d',[AblockInformation.operationBlock.block,ABlockInformation.rawDataPosition]));
end;
LOperationsComp := ABlockInformation.CreateTPCOperationsComp(FFileMem,Nil);
Try
//
if LOperationsComp.Count<>ABlockInformation.operationsCount then begin
AddInfo(True,Format('Block %d operations count not equal %d <> %d',[AblockInformation.operationBlock.block,AblockInformation.operationsCount,LOperationsComp.Count]));
end;
if LOperationsComp.OperationsHashTree.TotalAmount<>ABlockInformation.volume then begin
AddInfo(True,Format('Block %d volume not equal %d <> %d',[AblockInformation.operationBlock.block,AblockInformation.volume,LOperationsComp.OperationsHashTree.TotalAmount]));
end;
//
LOperationsCounter := 0;
LOperationRawDataToSearch.Clear;
LOperationRawDataToSearch.block := ABlockInformation.operationBlock.block;
LOperationRawDataToSearch.opblock := 0;
if FOperationsRawData_By_Block_OpBlock_Index.FindData(LOperationRawDataToSearch,LPos,LOperationRawData) then begin
repeat
if Assigned(AThread) and AThread.Terminated then Break;
Inc(FCheckingConsistencyStats.operationRawDataCount);
FCheckingConsistencyProgress := Format('Orphan "%s" Block %d Operation %d/%d',[ABlockInformation.orphan,ABlockInformation.operationBlock.block,LOperationRawData.opblock+1,ABlockInformation.operationsCount]);
LOperationRawDataAux.CopyFrom(LOperationRawData);
if not FOperationsRawData_By_RightOpHash.FindData(LOperationRawDataAux,LPos,LOperationRawDataAuxFound) then begin
AddInfo(True,Format('Block %d operation %d not found by searching by OpHash',[LOperationRawData.block,LOperationRawData.opblock]));
end;
//
LOperation := LOperationRawData.CreateTPCOperation(FFileMem);
Try
if (Not BytesEqual(LOperationRawData.rightOpHash,LOperation.RipeMD160)) then raise EAbstractMemBlockchainStorage.Create('ERR 20211116-1');
if LOperationRawData.account<>LOperation.SignerAccount then raise EAbstractMemBlockchainStorage.Create('ERR 20211116-2');
if LOperationRawData.n_operation<>LOperation.N_Operation then raise EAbstractMemBlockchainStorage.Create('ERR 20211116-3');
//
if (Not BytesEqual(LOperation.RipeMD160,LOperationsComp.Operation[LOperationRawData.opblock].RipeMD160)) then raise EAbstractMemBlockchainStorage.Create('ERR 20211116-4');
// Check affected accounts:
LAffectedAccountsList := TOrderedList<Cardinal>.Create(False,TComparison_Cardinal);
Try
LOperation.AffectedAccounts(LAffectedAccountsList);
for i := 0 to LAffectedAccountsList.Count-1 do begin
LAffectedAccount.Clear;
LAffectedAccount.account := LAffectedAccountsList.Get(i);
LAffectedAccount.n_operation := LOperation.GetAccountN_Operation(LAffectedAccount.account);
LAffectedAccount.block := ABlockInformation.operationBlock.block;
LAffectedAccount.opblock := LOperationRawData.opblock;
Inc(FCheckingConsistencyStats.affectedAccountCount);
FCheckingConsistencyProgress := Format('Orphan "%s" Block %d Operation %d/%d Account %d/%d',
[ABlockInformation.orphan,ABlockInformation.operationBlock.block,LOperationRawData.opblock+1,ABlockInformation.operationsCount,
i+1,LAffectedAccountsList.Count]);
if Not FAffectedAccounts_By_Account_Block_OpBlock.FindData(LAffectedAccount,LPos,LAffectedAccountFound) then begin
AddInfo(True,Format('Affected account %d (%d/%d) for block %d opblock %d/%d not found',[LAffectedAccount.account,
i+1,LAffectedAccountsList.Count, LAffectedAccount.block, LAffectedAccount.opblock+1, ABlockInformation.operationsCount]));
end else begin
if LAffectedAccountFound.n_operation <> LOperation.GetAccountN_Operation(LAffectedAccount.account) then begin
AddInfo(True,Format('Invalid n_operation %d for account %d (%d/%d) for block %d opblock %d/%d',[LAffectedAccountFound.n_operation, LAffectedAccount.account,
i+1,LAffectedAccountsList.Count, LAffectedAccount.block, LAffectedAccount.opblock+1, ABlockInformation.operationsCount]));
end;
end;
end;
inc(AAffectedAccountsCount, LAffectedAccountsList.Count);
Finally
LAffectedAccountsList.Free;
End;
Finally
LOperation.Free;
End;
//
inc(LOperationsCounter);
Inc(LOperationRawDataToSearch.opblock);
until Not (FOperationsRawData_By_Block_OpBlock_Index.FindData(LOperationRawDataToSearch,LPos,LOperationRawData));
end;
if LOperationsCounter<>ABlockInformation.operationsCount then begin
AddInfo(True,Format('Block %d has %d operations but %d was found',[ABlockInformation.operationBlock.block,ABlockInformation.operationsCount,LOperationsCounter]));
end;
AOperationsCount := LOperationsCounter;
Finally
LOperationsComp.Free;
End;
end;
procedure TAbstractMemBlockchainStorage.CheckConsistency(ARaiseOnError: Boolean; AMessages: TStrings; AThread : TPCThread);
var LOrphans, LSearch : TOrphanInformation;
LOrphansCount, LBlocksFound, LOperationsFound, LAffectedAccountsFound,
Ltemp1, Ltemp2, Ltemp3 : Integer;
LMyOrphanFound : Boolean;
begin
if FCheckingConsistency then begin
if ARaiseOnError then raise EAbstractMemBlockchainStorage.Create('Checking consistency in process...');
Exit;
end;
LMyOrphanFound := False;
LBlocksFound := 0;
LOperationsFound := 0;
LAffectedAccountsFound := 0;
LOrphansCount := 0;
LOrphans.Clear;
AddMessage(AMessages,Format('Start CheckConsistency process for My Orphan "%s"',[Orphan]),False);
if FOrphansInformation_By_Orphan.FindDataLowest(LOrphans) then begin
repeat
if Assigned(AThread) and (AThread.Terminated) then Break;
inc(LOrphansCount);
AddMessage(AMessages,Format('Start analyzing orphan "%s" with %d registers',[LOrphans.orphan,LOrphans.regsCounter]),False);
if not CheckConsistency(LOrphans.orphan,-1,-1,ARaiseOnError,AMessages,Ltemp1,Ltemp2,Ltemp3,AThread) then begin
AddMessage(AMessages,Format('Errors analyzing orphan "%s"',[LOrphans.orphan]),ARaiseOnError);
end;
inc(LBlocksFound,Ltemp1);
inc(LOperationsFound,Ltemp2);
inc(LAffectedAccountsFound,Ltemp3);
LMyOrphanFound := LMyOrphanFound or (OrphanCompare(LOrphans.orphan,Orphan)=0);
LSearch := LOrphans;
until Not (FOrphansInformation_By_Orphan.FindDataSuccessor(LSearch,LOrphans));
end;
if not LMyOrphanFound then begin
AddMessage(AMessages,Format('Warning: My orphan "%s" not found in list!',[Self.Orphan]),False);
//
if Not CheckConsistency(Self.Orphan,-1,-1,ARaiseOnError,AMessages,Ltemp1,Ltemp2,Ltemp3,AThread) then begin
AddMessage(AMessages,Format('Errors analyzing My orphan "%s"',[Self.Orphan]),ARaiseOnError);
end;
inc(LBlocksFound,Ltemp1);
inc(LOperationsFound,Ltemp2);
inc(LAffectedAccountsFound,Ltemp3);
end;
if (LBlocksFound<>FBlocksInformation_By_OrphanBlock.Count) then begin
AddMessage(AMessages,Format('Error: Found %d blocks but expected %d',[LBlocksFound,FBlocksInformation_By_OrphanBlock.Count]),ARaiseOnError);
end;
if (LOperationsFound<>FOperationsRawData_By_RightOpHash.Count) then begin
AddMessage(AMessages,Format('Error: Found %d operations but expected %d',[LOperationsFound,FOperationsRawData_By_RightOpHash.Count]),ARaiseOnError);
end;
if (LAffectedAccountsFound<>FAffectedAccounts_By_Account_Block_OpBlock.Count) then begin
AddMessage(AMessages,Format('Error: Found %d accounts but expected %d',[LAffectedAccountsFound,FAffectedAccounts_By_Account_Block_OpBlock.Count]),ARaiseOnError);
end;
if (FOperationsRawData_By_RightOpHash.Count<>FOperationsRawData_By_Block_OpBlock_Index.Count) then begin
AddMessage(AMessages,Format('Error: Indexes for operations %d not %d',[FOperationsRawData_By_RightOpHash.Count,FOperationsRawData_By_Block_OpBlock_Index.Count]),ARaiseOnError);
end;
AddMessage(AMessages,Format('Finalized analyzing orphans with %d orphans %d blocks %d operations and %d accounts',
[LOrphansCount,LBlocksFound,LOperationsFound,LAffectedAccountsFound]),False);
end;
function TAbstractMemBlockchainStorage.CheckConsistency(const AOrphan: String;
AFromBlock, AToBlock: Integer;
ARaiseOnError: Boolean; AMessages: TStrings;
out ABlocksFound, AOperationsFound, AAffectedAccountsFound : Integer; AThread : TPCThread): Boolean;
var LMessages : TStringList;
LErrorsCount : Integer;
procedure AddInfo(AIsError : Boolean; const AMessage : String);
Var LTxt : String;
begin
if AIsError then begin
Result := False;
inc(LErrorsCount);
LTxt := 'ERROR: '+AMessage;
if ARaiseOnError then raise Exception.Create(Self.ClassName+' not consistent: '+LTxt);
end else LTxt := 'INFO: '+AMessage;
AddMessage(AMessages,Ltxt,AIsError and ARaiseOnError);
end;
var LBlockInformation, LPreviousBlockInformation : TBlockInformation;
LBlocksInformationMin,LBlocksInformationMax : Integer;
LOperationRawData,LOperationRawDataAux,LOperationRawDataAuxFound : TOperationRawData;
LAffectedAccount : TAffectedAccount;
LPos : TAbstractMemPosition;
LTempOperationsCounter, LTempAccountsCounter : Integer;
LContinue : Boolean;
LOrphanInformation,LOrphanInformationFound : TOrphanInformation;
LTC,LStartTC : TTickCount;
begin
Assert((AToBlock<0) or (AToBlock>=AFromBlock),Format('Invalid from %d to %d values',[AFromBlock,AToBlock]));
Result := True;
LBlocksInformationMin := 0;
LBlocksInformationMax := 0;
ABlocksFound := 0;
AOperationsFound := 0;
AAffectedAccountsFound := 0;
if FCheckingConsistency then begin
if ARaiseOnError then raise EAbstractMemBlockchainStorage.Create('Checking consistency in process...');
Exit;
end;
FCheckingConsistencyStats.Clear;
FCheckingConsistency := True;
Try
LPreviousBlockInformation.Clear;
if (AFromBlock<0) then begin
AFromBlock := GetFirstBlockNumberByOrphan(AOrphan);
end;
LTC := TPlatform.GetTickCount;
LStartTC := LTC;
LContinue := ((AToBlock<0) or (AFromBlock<=AToBlock)) and (DoBlockExistsByOrphan(AFromBlock,AOrphan,LBlockInformation));
if (LContinue) then begin
inc(ABlocksFound);
LPreviousBlockInformation.CopyFrom(LBlockInformation);
// Initialize
LBlocksInformationMin := LBlockInformation.operationBlock.block;
LBlocksInformationMax := LBlockInformation.operationBlock.block;
// Check operations count
if (OrphanCompare(AOrphan,Self.Orphan)=0) then begin
CheckBlockConsistency(ARaiseOnError,AMessages,LBlockInformation,LTempOperationsCounter,LTempAccountsCounter,AThread);
inc(AOperationsFound,LTempOperationsCounter);
inc(AAffectedAccountsFound,LTempAccountsCounter);
end;
//
while (FBlocksInformation_By_OrphanBlock.FindDataSuccessor(LPreviousBlockInformation,LBlockInformation)) do begin
Inc(FCheckingConsistencyStats.blockInformationCount);
FCheckingConsistencyProgress := Format('Orphan "%s" Block %d Operations %d',[LBlockInformation.orphan,LBlockInformation.operationBlock.block,LBlockInformation.operationsCount]);
if Assigned(AThread) and (AThread.Terminated) then Break;
if ((AToBlock>=0) and (AToBlock<LBlockInformation.operationBlock.block)) then break;
if (Not LBlockInformation.IsOrphan(AOrphan)) then Break;
if (LPreviousBlockInformation.operationBlock.block >= LBlockInformation.operationBlock.block) then begin
AddInfo(True,Format('Previous block %d >= current block %d (DUPLICATE OR INVALID ORDER!)',[LPreviousBlockInformation.operationBlock.block,LBlockInformation.operationBlock.block]));
end else if LPreviousBlockInformation.operationBlock.block+1 <> LBlockInformation.operationBlock.block then begin
AddInfo(False,Format('Previous block %d+1 Not current block %d',[LPreviousBlockInformation.operationBlock.block,LBlockInformation.operationBlock.block]));
end;
if LBlocksInformationMax < LBlockInformation.operationBlock.block then LBlocksInformationMax := LBlockInformation.operationBlock.block;
//
// Check operations count
if (OrphanCompare(AOrphan,Self.Orphan)=0) then begin
CheckBlockConsistency(ARaiseOnError,AMessages,LBlockInformation,LTempOperationsCounter,LTempAccountsCounter,AThread);
inc(AOperationsFound,LTempOperationsCounter);
inc(AAffectedAccountsFound,LTempAccountsCounter);
end;
//
inc(ABlocksFound);
LPreviousBlockInformation.CopyFrom(LBlockInformation);
if (TPlatform.GetElapsedMilliseconds(LTC)>6000) then begin
TLog.NewLog(ltdebug,ClassName,Format('Consistency checking %d/%d elapsed %s seconds',[LBlockInformation.operationBlock.block,AToBlock,FormatFloat('0.00',TPlatform.GetElapsedMilliseconds(LStartTC)/1000)]));
LTC := TPlatform.GetTickCount;
end;
end;
end;
if (AToBlock<0) and (GetLastBlockNumberByOrphan(AOrphan)>0) and (GetLastBlockNumberByOrphan(AOrphan)<>LBlocksInformationMax) then begin
AddInfo(True,Format('Last block found %d not what expected %d',[LBlocksInformationMax,GetLastBlockNumberByOrphan(AOrphan)]));
end;
if (OrphanCompare(AOrphan,Self.Orphan)=0) then begin
if (FOperationsRawData_By_RightOpHash.Count<>AOperationsFound) then begin
AddInfo(False,Format('Stored %d operations but only %d on processed blocks',[FOperationsRawData_By_RightOpHash.Count,AOperationsFound]));
end;
if FAffectedAccounts_By_Account_Block_OpBlock.Count<>AAffectedAccountsFound then begin
AddInfo(False,Format('Stored %d affected accounts but only %d on processed blocks',[FAffectedAccounts_By_Account_Block_OpBlock.Count,AAffectedAccountsFound]));
end;
end;
LOrphanInformation.Clear;
LOrphanInformation.orphan := AOrphan;
if FOrphansInformation_By_Orphan.FindData(LOrphanInformation,LPos,LOrphanInformationFound) then begin
if LOrphanInformationFound.regsCounter<>ABlocksFound then begin
AddInfo((AToBlock<0),Format('Orphan information counter expected %d found %d for orphan "%s"',[LOrphanInformationFound.regsCounter,ABlocksFound,AOrphan]));
end;
end else begin
AddInfo(True,Format('Not found information for Orphan "%s"',[AOrphan]));
end;
AddInfo(False,Format('Analyzed from block %d to %d on orphan "%s" (expected %d/%d pending %d) Operations %d Accounts %d',[
LBlocksInformationMin,LBlocksInformationMax,
AOrphan,
ABlocksFound, (LBlocksInformationMax - LBlocksInformationMin + 1), (LBlocksInformationMax - LBlocksInformationMin + 1)- ABlocksFound,
AOperationsFound,AAffectedAccountsFound]));
Finally
FCheckingConsistency := False;
FCheckingConsistencyProgress := '';
End;
end;
constructor TAbstractMemBlockchainStorage.Create(AOwner: TComponent);
begin
inherited;
FInBlockNotFound := False;
FInBlockSaving := 0;
FLogSaveActivity := True;
FSaveStorageStats.Clear;
FPSaveStorageStats := @FSaveStorageStats;
FPSaveStorageStats^.clear;
FCheckingConsistency := False;
FCheckingConsistencyProgress := '';
FCheckingConsistencyStats.Clear;
FUseMultithread := True;
FFileMem := Nil;
FOrphansInformation_By_Orphan := Nil;
FBlocksInformation_By_OrphanBlock := Nil;
FOperationsRawData_By_RightOpHash := Nil;
FOperationsRawData_By_Block_OpBlock_Index := Nil;
FAffectedAccounts_By_Account_Block_OpBlock := Nil;
FAutoFlushCache := True;
FStorageLock := TCriticalSection.Create;
FPendingToSave := Nil;
end;
function TAbstractMemBlockchainStorage.DeleteBlockChainBlockExt(ABlock : Integer; const AOrphan : String) : Boolean;
var LBlock : TBlockInformation;
LOperationRawDataSearch,LOperationRawDataFound : TOperationRawData;
LPos : TAbstractMemPosition;
LOperation : TPCOperation;
LAffectedAccountSearch, LAffectedAccountFound, LAffectedAccount : TAffectedAccount;
LAffectedAccounts : TOrderedList<Cardinal>;
i : Integer;
begin
Result := False;
LBlock.Clear;
LBlock.orphan := AOrphan;
LBlock.operationBlock.block := ABlock;
if Not (FBlocksInformation_By_OrphanBlock.DeleteData(LBlock)) then Exit;
FOrphansInformation_By_Orphan.Update(AOrphan,-1);
Result := True;
if OrphanCompare(AOrphan,Orphan)<>0 then Exit;
// Try to delete all operations and affected accounts:
LOperationRawDataSearch.Clear;
LOperationRawDataSearch.block := LBlock.operationBlock.block;
LOperationRawDataSearch.opblock := MAXINT; // Will search BACKWARDS
FOperationsRawData_By_Block_OpBlock_Index.FindData(LOperationRawDataSearch,LOperationRawDataFound);
while (LOperationRawDataSearch.opblock>=0) and (LOperationRawDataFound.block = LOperationRawDataSearch.block) do begin
// Delete affected accounts
if LOperationRawDataFound.CreateTPCOperation(FFileMem,LOperation) then
try
LAffectedAccounts := TOrderedList<Cardinal>.Create(False,TComparison_Cardinal);
Try
LOperation.AffectedAccounts(LAffectedAccounts);
for i := 0 to LAffectedAccounts.Count-1 do begin
LAffectedAccount.Clear;
LAffectedAccount.account := LAffectedAccounts.Items[i];
LAffectedAccount.block := ABlock;
LAffectedAccount.opblock := LOperationRawDataFound.opblock;
//
if Not FAffectedAccounts_By_Account_Block_OpBlock.DeleteData(LAffectedAccount) then begin
TLog.NewLog(lterror,ClassName,Format('ERR 20211117-01 Affected account %d %d %d not found',[LAffectedAccount.account,LAffectedAccount.block,LAffectedAccount.opblock+1]));
end;
end;
Finally
LAffectedAccounts.Free;
end;
Finally
LOperation.Free;
end;
if not FOperationsRawData_By_RightOpHash.DeleteData(LOperationRawDataFound) then begin
// Found
raise EAbstractMemBlockchainStorage.Create('ERR 20211117-02');
end;
// Go backward
LOperationRawDataSearch.opblock := LOperationRawDataFound.opblock-1;
FOperationsRawData_By_Block_OpBlock_Index.FindData(LOperationRawDataSearch,LOperationRawDataFound);
end;
end;
destructor TAbstractMemBlockchainStorage.Destroy;
begin
UseMultithread := False;
FreeAndNil(FPendingToSave);
FreeAndNil(FFileMem);
FreeAndNil(FOrphansInformation_By_Orphan);
FreeAndNil(FBlocksInformation_By_OrphanBlock);
FreeAndNil(FOperationsRawData_By_RightOpHash);
FreeAndNil(FOperationsRawData_By_Block_OpBlock_Index);
FreeAndNil(FAffectedAccounts_By_Account_Block_OpBlock);
FreeAndNil(FStorageLock);
inherited;
end;
function TAbstractMemBlockchainStorage.DoBlockExists(Block: Cardinal): Boolean;
var LFoundBlock : TBlockInformation;
begin
Result := DoBlockExistsByOrphan(Block,Orphan,LFoundBlock);
if Not Result then begin
BlockNotFound(Block,Result);
end;
end;
function TAbstractMemBlockchainStorage.DoBlockExistsByOrphan(ABlock: Integer;
const AOrphan: String; var LBlockInformation: TBlockInformation): Boolean;
var LSearch : TBlockInformation;
LDataPos : TAbstractMemPosition;
begin
LSearch.Clear;
LSearch.orphan := AOrphan;
LSearch.operationBlock.block := ABlock;
Result := FBlocksInformation_By_OrphanBlock.FindData(LSearch,LDataPos,LBlockInformation);
end;
procedure TAbstractMemBlockchainStorage.DoBlockNotFound(ABlock: Integer; var AFound : Boolean);
begin
// Nothing to do here
AFound := False;
end;
procedure TAbstractMemBlockchainStorage.DoDeleteBlockChainBlocks(StartingDeleteBlock: Cardinal);
begin
FStorageLock.Acquire;
try
while DeleteBlockChainBlockExt(StartingDeleteBlock,Orphan) do inc(StartingDeleteBlock);
FinalizedUpdating;
finally
FStorageLock.Release;
end;
end;
procedure TAbstractMemBlockchainStorage.DoEraseStorage;
begin
FStorageLock.Acquire;
Try
FFileMem.ClearContent(FFileMem.Is64Bits,FFileMem.MemUnitsSize);
FreeAndNil(FFileMem);
DoInitialize;
FinalizedUpdating;
Finally
FStorageLock.Release;
End;
end;
function TAbstractMemBlockchainStorage.DoFindOperation(const AOpHash: TBytes; var AOperationResume: TOperationResume): TSearchOpHashResult;
var LSearch, LFound : TOperationRawData;
LPos : TAbstractMemPosition;
LOperation : TPCOperation;
LMD160Hash : TBytes;
LBlock, LAccount, LN_Operation : Cardinal;
begin
Result := OpHash_invalid_params;
if not (TPCOperation.DecodeOperationHash(AOpHash,LBlock,LAccount,LN_Operation,LMD160Hash)) then Exit;
if Not BlockExists(LBlock) then Exit;
LSearch.Clear;
LSearch.rightOpHash := Copy(AOpHash,12,20);
if (FOperationsRawData_By_RightOpHash.FindData(LSearch,LPos,LFound)) then begin
if LFound.CreateTPCOperation(FFileMem,LOperation) then
Try
if not TPCOperation.OperationToOperationResume(LFound.block,LOperation,True,LAccount,AOperationResume) then Exit;
AOperationResume.NOpInsideBlock := LFound.opblock;
AOperationResume.Balance := -1;
Result := OpHash_found;
Finally
LOperation.Free;
End;
end else Result := OpHash_block_not_found;
end;
function TAbstractMemBlockchainStorage.DoGetAccountOperations(AAccount,
AMaxDepth, AStartOperation, AMaxOperations, ASearchBackwardsStartingAtBlock: Integer;
const AOperationsResumeList: TOperationsResumeList): Boolean;
var LSearch,LFound : TAffectedAccount;
LOperation : TPCOperation;
LOPR : TOperationResume;
LPreviousBlock : Integer;
LOperationsHashTree : TOperationsHashTree;
LLastBalance : Int64;
LAcc : TAccount;
LHasFound : Boolean;
begin
if AMaxOperations=0 then Exit(False);
if AStartOperation<0 then Exit(False);
Result := True;
LSearch.Clear;
LSearch.account := AAccount;
LAcc := Bank.SafeBox.Account(AAccount);
if Not BlockExists(LAcc.GetLastUpdatedBlock) then Exit(False);
if ASearchBackwardsStartingAtBlock>0 then begin
LSearch.block := ASearchBackwardsStartingAtBlock;
end else begin
LSearch.block := MAXINT;
end;
LSearch.opblock := MAXINT;
LFound.Clear;
if Not FAffectedAccounts_By_Account_Block_OpBlock.FindData(LSearch,LFound) then begin
if (LFound.account <> AAccount) then Exit(False);
end;
if (LFound.block = LAcc.GetLastUpdatedBlock) then begin
LLastBalance := LAcc.balance;
end else LLastBalance := -1;
LPreviousBlock := LFound.block;
repeat
// Process back
if (LFound.account<>AAccount) then Break;
if (LFound.block<>LPreviousBlock) then begin
Dec(AMaxDepth);
LPreviousBlock := LFound.block;
if (AMAxDepth=0) then Break;
if Not BlockExists(LFound.block) then Break;
end;
if (AStartOperation>0) then Dec(AStartOperation)
else begin
LOperationsHashTree := TOperationsHashTree.Create;
Try
if DoGetOperation(LFound.block,LFound.opblock,LOperationsHashTree) then begin
if LOperationsHashTree.OperationsCount=1 then begin
LOperation := LOperationsHashTree.GetOperation(0);
if TPCOperation.OperationToOperationResume(LFound.block,LOperation,True,AAccount,LOPR) then begin
LOPR.NOpInsideBlock := LFound.opblock;
LOPR.time := Bank.SafeBox.GetBlockInfo(LFound.block).timestamp;
LOPR.Block := LFound.block;
If LLastBalance>=0 then begin
LOPR.Balance := LLastBalance;
LLastBalance := LLastBalance - ( LOPR.Amount + LOPR.Fee );
end else LOPR.Balance := 0; // Undetermined
AOperationsResumeList.Add(LOPR);
end;
end;
end;
Finally
LOperationsHashTree.Free;
End;
Dec(AMaxOperations);
end;
LSearch.CopyFrom(LFound);
LHasFound := FAffectedAccounts_By_Account_Block_OpBlock.FindDataPrecessor(LSearch,LFound);
until (AMaxDepth=0) or (AMaxOperations=0) or (Not LHasFound);
end;
function TAbstractMemBlockchainStorage.DoGetBlockInformation(const ABlock : Integer;
var AOperationBlock: TOperationBlock; var AOperationsCount: Integer;
var AVolume: Int64): Boolean;
var LBlock,LFoundBlock : TBlockInformation;
LDataPos : TAbstractMemPosition;
begin
if Not BlockExists(ABlock) then Exit(False);
LBlock.Clear;
LBlock.orphan := Orphan;
LBlock.operationBlock.block := ABlock;
if FBlocksInformation_By_OrphanBlock.FindData(LBlock,LDataPos,LFoundBlock) then begin
AOperationBlock := LFoundBlock.operationBlock;
AOperationsCount := LFoundBlock.operationsCount;
AVolume := LFoundBlock.volume;
Result := True;
end else Result := False;
end;
function TAbstractMemBlockchainStorage.DoGetBlockOperations(ABlock,
AOpBlockStartIndex, AMaxOperations: Integer;
var AOperationBlock: TOperationBlock; var AOperationsCount: Integer;
var AVolume: Int64;
const AOperationsResumeList: TOperationsResumeList): Boolean;
var LFound,LSearch : TOperationRawData;
LOperation : TPCOperation;
LOPR : TOperationResume;
begin
//
if AMaxOperations=0 then Exit(False);
if Not BlockExists(ABlock) then Exit(False);
Result := True;
LSearch.Clear;
LSearch.block := ABlock;
LSearch.opblock := AOpBlockStartIndex;
LFound.Clear;
if not FOperationsRawData_By_Block_OpBlock_Index.FindData(LSearch,LFound) then begin
if LFound.block<>ABlock then Exit(False);
LSearch := LFound;
if Not FOperationsRawData_By_Block_OpBlock_Index.FindDataSuccessor(LSearch,LFound) then Exit(False);
if LFound.block<>ABlock then Exit(False);
end;
repeat
if LFound.block<>ABlock then Exit(True);
if LFound.CreateTPCOperation(FFileMem,LOperation) then
Try
if not TPCOperation.OperationToOperationResume(ABlock,LOperation,True,LOperation.SignerAccount,LOPR) then break;
LOPR.NOpInsideBlock := LFound.opblock;
LOPR.Balance := -1;
AOperationsResumeList.Add(LOPR);
Finally
LOperation.Free;
End;
Dec(AMaxOperations);
LSearch := LFound;
until (AMaxOperations=0) or (Not FOperationsRawData_By_Block_OpBlock_Index.FindDataSuccessor(LSearch,LFound));
end;
function TAbstractMemBlockchainStorage.DoGetOperation(const ABlock, AOpBlock: Integer; const AOperations: TOperationsHashTree): Boolean;
var LSearch,LFound : TOperationRawData;
LOp : TPCOperation;
begin
if Not BlockExists(ABlock) then Exit(False);
LSearch.Clear;
LSearch.SetToFindByBlockOpblock(ABlock,AOpBlock);
LFound.Clear;
if Not FOperationsRawData_By_Block_OpBlock_Index.FindData(LSearch,LFound) then Exit(False);
Result := True;
if LFound.CreateTPCOperation(FFileMem,LOp) then
try
AOperations.AddOperationToHashTree( LOp );
finally
LOp.Free;
end;
end;
function TAbstractMemBlockchainStorage.DoInitialize: Boolean;
const CT_HEADER = 'AMBlockchain'; // 12 chars
CT_VERSION : Integer = $00000003; // 4 bytes
var LfdZone : TAMZone;
LfdBytes : TBytes;
LExpectedHeader,