-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathUBlockChain.pas
More file actions
4494 lines (4198 loc) · 177 KB
/
Copy pathUBlockChain.pas
File metadata and controls
4494 lines (4198 loc) · 177 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 UBlockChain;
{ 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.
}
{$I ./../config.inc}
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
Classes,{$IFnDEF FPC}Windows,{$ENDIF}UCrypto, UAccounts, ULog, UThread, SyncObjs, UBaseTypes, SysUtils,
{$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF},
{$IFDEF USE_ABSTRACTMEM}UPCAbstractMem,{$ENDIF}
UPCDataTypes, UChunk, UOrderedList;
{
Bank BlockChain:
Safe Box content: (See Unit "UAccounts.pas" to see pascal code)
+--------------+--------------------------------------------------+------------+------------+
+ BlockAccount + Each BlockAccount has N "Account" + Timestamp + Block Hash +
+ +--------------------------------------------------+ + +
+ + Addr B0 + Public key + Balance + updated + n_op + + +
+ + Addr B1 + Public key + Balance + updated + n_op + + +
+ + ...... + + +
+ + Addr B4 + Public key + Balance + updated + n_op + + +
+--------------+---------+----------------------------------------+------------+------------+
+ 0 + 0 + pk_aaaaaaa + 100.0000 + 0 + 0 + 1461701856 + Sha256() +
+ + 1 + pk_aaaaaaa + 0.0000 + 0 + 0 + + = h1111111 +
+ + 2 + pk_aaaaaaa + 0.0000 + 0 + 0 + + +
+ + 3 + pk_aaaaaaa + 0.0000 + 0 + 0 + + +
+ + 4 + pk_aaaaaaa + 0.0000 + 0 + 0 + + +
+--------------+---------+----------------------------------------+------------+------------+
+ 1 + 5 + pk_bbbbbbb + 100.0000 + 0 + 0 + 1461702960 + Sha256() +
+ + 6 + pk_bbbbbbb + 0.0000 + 0 + 0 + + = h2222222 +
+ + 7 + pk_bbbbbbb + 0.0000 + 0 + 0 + + +
+ + 8 + pk_bbbbbbb + 0.0000 + 0 + 0 + + +
+ + 9 + pk_bbbbbbb + 0.0000 + 0 + 0 + + +
+--------------+---------+----------------------------------------+------------+------------+
+ ................ +
+--------------+---------+----------------------------------------+------------+------------+
+ 5 + 25 + pk_bbbbbbb + 100.0000 + 0 + 0 + 1461713484 + Sha256() +
+ + 26 + pk_bbbbbbb + 0.0000 + 0 + 0 + + = h3333333 +
+ + 27 + pk_bbbbbbb + 0.0000 + 0 + 0 + + +
+ + 28 + pk_bbbbbbb + 0.0000 + 0 + 0 + + +
+ + 29 + pk_bbbbbbb + 0.0000 + 0 + 0 + + +
+--------------+---------+----------------------------------------+------------+------------+
+ Safe Box Hash : Sha256(h1111111 + h2222222 + ... + h3333333) = sbh_A1 +
+-------------------------------------------------------------------------------------------+
BlockChain:
To generate a BlockChain (block X) we need the previous "Safe Box Hash"
(the Safe Box Hash number X-1, generated when BlockChain X-1 was generated)
Each BlockChain block generates a new "Safe Box" with a new "Safe Box Hash"
With this method, Safe Box is unique after a BlockChain, so we can assume
that a hard coded Safe Box X is the same that to load all previous BlockChain
from 0 to X. Conclusion: It's not necessary historical operations (block chains)
to work with Pascal Coin
Some BlockChain fields:
+-------+-----------------+----------+------+-----+-----+------------+--------+-------+---------------+---------------+-----------------+---------------+-----------------------+
+ Block + Account key + reward + fee + protocols + timestamp + target + nonce + Miner Payload + safe box hash + operations hash + Proof of Work + Operations stream +
+-------+-----------------+----------+------+-----+-----+------------+--------+-------+---------------+---------------+-----------------+---------------+-----------------------+
+ 0 + (hard coded) + 100.0000 + 0 + 1 + 0 + 1461701856 + trgt_1 + ... + (Hard coded) + (Hard coded) + Sha256(Operat.) + 000000C3F5... + Operations of block 0 +
+-------+-----------------+----------+------+-----+-----+------------+--------+-------+---------------+---------------+-----------------+---------------+-----------------------+
+ 1 + hhhhhhhhhhhhhhh + 100.0000 + 0 + 1 + 0 + 1461701987 + trgt_1 + ... + ... + SFH block 0 + Sha256(Operat.) + 000000A987... + Operations of block 1 +
+-------+-----------------+----------+------+-----+-----+------------+--------+-------+---------------+---------------+-----------------+---------------+-----------------------+
+ 2 + iiiiiiiiiiiiiii + 100.0000 + 0.43 + 1 + 0 + 1461702460 + trgt_1 + ... + ... + SFH block 1 + Sha256(Operat.) + 0000003A1C... + Operations of block 2 +
+-------+-----------------+----------+------+-----+-----+------------+--------+-------+---------------+---------------+-----------------+---------------+-----------------------+
+ ..... +
+-------+-----------------+----------+------+-----+-----+------------+--------+-------+---------------+---------------+-----------------+---------------+-----------------------+
Considerations:
- Account Key: Is a public key that will have all new generated Accounts of the Safe Box
- Protocols are 2 values: First indicate protocol of this block, second future candidate protocol that is allowed by miner who made this. (For protocol upgrades)
- Safe Box Has: Each Block of the Bloch Chain is made in base of a previous Safe Box. This value hard codes consistency
- Operations Stream includes all the operations that will be made to the Safe Box after this block is generated. A hash value of Operations stream is "Operations Hash"
Operations:
Each Block of the Block Chain has its owns operations that will be used to change Safe Box after block is completed and included in BlockChain
Operations of actual Protocol (version 1) can be one of this:
- Transaction from 1 account to 1 account
- Change AccountKey of an account
- Recover balance from an unused account (lost keys)
Each Operation has a Hash value that is used to generate "Operations Hash". Operations Hash is a Sha256 of all the Operations included
inside it hashed like a Merkle Tree.
In unit "UOpTransaction.pas" you can see how each Operation Works.
}
Type
TSearchOpHashResult = (OpHash_found, OpHash_invalid_params, OpHash_block_not_found);
// Moved from UOpTransaction to here
TOpChangeAccountInfoType = (public_key, account_name, account_type, list_for_public_sale, list_for_private_sale, delist, account_data, list_for_account_swap, list_for_coin_swap );
TOpChangeAccountInfoTypes = Set of TOpChangeAccountInfoType;
TOperationPayload = record
{ As described on PIP-0027 (introduced on Protocol V5)
the payload of an operation will contain an initial byte that will
provide information about the payload content.
The "payload_type" byte value will help in payload decoding if good used
but there is no core checking that payload_type has been used properly.
It's job of any third party app (Layer 2) working with payloads to
check/ensure they can read/decode properly Payload value if the
content is not saved using E-PASA standard (PIP-0027) }
payload_type : Byte;
payload_raw : TRawBytes;
end;
// MultiOp... will allow a MultiOperation
TMultiOpData = record
ID : TGUID;
Sequence : UInt16;
&Type : UInt16;
end;
TMultiOpSender = Record
Account : Cardinal;
Amount : Int64;
N_Operation : Cardinal;
OpData : TMultiOpData; // Filled only when Operation is TOpData type
Payload : TOperationPayload;
Signature : TECDSA_SIG;
end;
TMultiOpSenders = Array of TMultiOpSender;
TMultiOpReceiver = Record
Account : Cardinal;
Amount : Int64;
Payload : TOperationPayload;
end;
TMultiOpReceivers = Array of TMultiOpReceiver;
TMultiOpChangeInfo = Record
Account: Cardinal;
N_Operation : Cardinal;
Changes_type : TOpChangeAccountInfoTypes; // bits mask. $0001 = New account key , $0002 = New name , $0004 = New type
New_Accountkey: TAccountKey; // If (changes_mask and $0001)=$0001 then change account key
New_Name: TRawBytes; // If (changes_mask and $0002)=$0002 then change name
New_Type: Word; // If (changes_mask and $0004)=$0004 then change type
New_Data: TRawBytes;
Seller_Account : Int64;
Account_Price : Int64;
Locked_Until_Block : Cardinal;
Hashed_secret : TRawBytes;
Fee: Int64;
Signature: TECDSA_SIG;
end;
TMultiOpChangesInfo = Array of TMultiOpChangeInfo;
TOperationResume = Record
valid : Boolean;
Block : Cardinal;
NOpInsideBlock : Integer;
OpType : Word;
OpSubtype : Word;
time : Cardinal;
AffectedAccount : Cardinal;
SignerAccount : Int64; // Is the account that executes this operation
n_operation : Cardinal;
DestAccount : Int64; //
SellerAccount : Int64; // Protocol 2 - only used when is a pay to transaction
newKey : TAccountKey;
OperationTxt : String;
Amount : Int64;
Fee : Int64;
Balance : Int64;
OriginalPayload : TOperationPayload;
PrintablePayload : String;
DecodedEPasaPayload : String;
OperationHash : TRawBytes;
OperationHash_OLD : TRawBytes; // Will include old oeration hash value
errors : String;
// New on V3 for PIP-0017
isMultiOperation : Boolean;
Senders : TMultiOpSenders;
Receivers : TMultiOpReceivers;
Changers : TMultiOpChangesInfo;
end;
TPCBank = Class;
TPCBankNotify = Class;
TPCOperation = Class;
TPCOperationClass = Class of TPCOperation;
TOperationsResumeList = TList<TOperationResume>;
TOpReference = UInt64;
TOpReferenceArray = Array of TopReference;
{ TPCOperation }
TPCOperation = Class
private
FResendOnBlock: Integer;
FDiscoveredOnBlock: Integer;
FResendCount: Integer;
Protected
FProtocolVersion : Word;
FHasValidSignature : Boolean;
FUsedPubkeyForSignature : TECDSA_Public;
FBufferedSha256 : TRawBytes;
FBufferedRipeMD160 : TRawBytes; // OPID is a RipeMD160 of the GetBufferForOpHash(True) value, 20 bytes length
procedure InitializeData(AProtocolVersion : Word); virtual;
function SaveOpToStream(Stream: TStream; SaveExtendedData : Boolean): Boolean; virtual; abstract;
function LoadOpFromStream(Stream: TStream; LoadExtendedData : Boolean): Boolean; virtual; abstract;
procedure FillOperationResume(Block : Cardinal; getInfoForAllAccounts : Boolean; Affected_account_number : Cardinal; var OperationResume : TOperationResume); virtual;
function IsValidECDSASignature(const PubKey: TECDSA_Public; const Signature: TECDSA_SIG): Boolean;
procedure CopyUsedPubkeySignatureFrom(SourceOperation : TPCOperation); virtual;
function SaveOperationPayloadToStream(const AStream : TStream; const APayload : TOperationPayload) : Boolean;
function LoadOperationPayloadFromStream(const AStream : TStream; out APayload : TOperationPayload) : Boolean;
public
constructor Create(AProtocolVersion : Word); virtual;
destructor Destroy; override;
property ProtocolVersion : Word read FProtocolVersion;
function GetBufferForOpHash(UseProtocolV2 : Boolean): TRawBytes; virtual;
function DoOperation(AccountPreviousUpdatedBlock : TAccountPreviousBlockInfo; AccountTransaction : TPCSafeBoxTransaction; var errors: String): Boolean; virtual; abstract;
procedure AffectedAccounts(list : TOrderedList<Cardinal>); virtual; abstract;
class function OpType: Byte; virtual; abstract;
Class Function OperationToOperationResume(Block : Cardinal; Operation : TPCOperation; getInfoForAllAccounts : Boolean; Affected_account_number : Cardinal; var OperationResume : TOperationResume) : Boolean; virtual;
Function GetDigestToSign : TRawBytes; virtual; abstract;
function OperationAmount : Int64; virtual; abstract;
function OperationAmountByAccount(account : Cardinal) : Int64; virtual;
function OperationFee: Int64; virtual; abstract;
function OperationPayload : TOperationPayload; virtual; abstract;
function SignerAccount : Cardinal; virtual; abstract;
procedure SignerAccounts(list : TList<Cardinal>); virtual;
function IsSignerAccount(account : Cardinal) : Boolean; virtual;
function IsAffectedAccount(account : Cardinal) : Boolean; virtual;
function DestinationAccount : Int64; virtual;
function SellerAccount : Int64; virtual;
function N_Operation : Cardinal; virtual; abstract;
function GetAccountN_Operation(account : Cardinal) : Cardinal; virtual;
function SaveToNettransfer(Stream: TStream): Boolean;
function LoadFromNettransfer(Stream: TStream): Boolean;
function SaveToStorage(Stream: TStream): Boolean;
function LoadFromStorage(Stream: TStream; LoadProtocolVersion : Word; APreviousUpdatedBlocks : TAccountPreviousBlockInfo): Boolean;
Property HasValidSignature : Boolean read FHasValidSignature;
Class function OperationHash_OLD(op : TPCOperation; Block : Cardinal) : TRawBytes;
Class function OperationHashValid(op : TPCOperation; Block : Cardinal) : TRawBytes;
class function IsValidOperationHash(const AOpHash : String) : Boolean;
class function TryParseOperationHash(const AOpHash : String; var block, account, n_operation: Cardinal; var md160Hash : TRawBytes) : Boolean;
Class function DecodeOperationHash(Const operationHash : TRawBytes; var block, account,n_operation : Cardinal; var md160Hash : TRawBytes) : Boolean;
Class function EqualOperationHashes(Const operationHash1, operationHash2 : TRawBytes) : Boolean;
Class function FinalOperationHashAsHexa(Const operationHash : TRawBytes) : String;
class function OperationHashAsHexa(const operationHash : TRawBytes) : String;
class function GetOpReferenceAccount(const opReference : TOpReference) : Cardinal;
class function GetOpReferenceN_Operation(const opReference : TOpReference) : Cardinal;
class function CreateOperationFromStream(AStream : TStream; var AOperation : TPCOperation) : Boolean;
function Sha256 : TRawBytes;
function RipeMD160 : TRawBytes;
function GetOpReference : TOpReference;
function GetOpID : TRawBytes; // OPID is RipeMD160 hash of the operation
//
function GetOperationStreamData : TBytes;
function GetOperationStreamData_OLD_V4_Version : TBytes; // deprecated
class function GetOperationFromStreamData(AUseV5EncodeStyle : Boolean; ACurrentProtocol: word; StreamData : TBytes) : TPCOperation;
//
function IsValidSignatureBasedOnCurrentSafeboxState(ASafeBoxTransaction : TPCSafeBoxTransaction) : Boolean; virtual; abstract;
property DiscoveredOnBlock : Integer read FDiscoveredOnBlock write FDiscoveredOnBlock;
property ResendOnBlock : Integer read FResendOnBlock write FResendOnBlock;
property ResendCount : Integer read FResendCount write FResendCount;
End;
TPCOperationStorage = Record
ptrPCOperation : TPCOperation;
locksCount : Integer;
end;
PPCOperationTStorage = ^TPCOperationStorage;
{ TPCOperationsStorage }
// TPCOperationsStorage will be used as a global Operations storage useful when
// operations are stored on TOperationsHashTree because will use only one instance
// of operation used on multiple OperationsHashTree lists. For example when
// propagating operations to connected nodes, will only use one instance
TPCOperationsStorage = Class
private
FIntTotalNewOps : Integer;
FIntTotalAdded : Integer;
FIntTotalDeleted : Integer;
FMaxLocksCount : Integer;
FMaxLocksValue : Integer;
FPCOperationsStorageList : TPCThreadList<Pointer>; // Lock thread to POperationTStorage list
Function FindOrderedByPtrPCOperation(lockedThreadList : TList<Pointer>; const Value: TPCOperation; out Index: Integer): Boolean;
protected
public
Constructor Create;
Destructor Destroy; override;
//
function LockPCOperationsStorage : TList<Pointer>;
procedure UnlockPCOperationsStorage;
Function Count : Integer;
procedure AddPCOperation(APCOperation : TPCOperation);
procedure RemovePCOperation(APCOperation : TPCOperation);
function FindPCOperation(APCOperation : TPCOperation) : Boolean;
function FindPCOperationAndIncCounterIfFound(APCOperation : TPCOperation) : Boolean;
class function PCOperationsStorage : TPCOperationsStorage;
procedure GetStats(strings : TStrings);
end;
{ TOperationsHashTree }
TOperationsHashTree = Class
private
FListOrderedByAccountsData : TList<Pointer>;
FListOrderedBySha256 : TList<Integer>; // Improvement TOperationsHashTree speed 2.1.6
FListOrderedByOpReference : TList<Integer>;
FHashTreeOperations : TPCThreadList<Pointer>; // Improvement TOperationsHashTree speed 2.1.6
FHashTree: TRawBytes;
FOnChanged: TNotifyEvent;
FTotalAmount : Int64;
FTotalFee : Int64;
FMax0feeOperationsBySigner : Integer;
FHasOpRecoverOperations : Boolean;
function InternalCanAddOperationToHashTree(lockedThreadList : TList<Pointer>; op : TPCOperation) : Boolean;
function InternalAddOperationToHashTree(list : TList<Pointer>; op : TPCOperation; CalcNewHashTree : Boolean) : Boolean;
Function FindOrderedByOpReference(lockedThreadList : TList<Pointer>; const Value: TOpReference; var Index: Integer): Boolean;
Function FindOrderedBySha(lockedThreadList : TList<Pointer>; const Value: TRawBytes; var Index: Integer): Boolean;
Function FindOrderedByAccountData(lockedThreadList : TList<Pointer>; const account_number : Cardinal; var Index: Integer): Boolean;
function GetHashTree: TRawBytes;
procedure SetMax0feeOperationsBySigner(const Value: Integer);
public
Constructor Create;
Destructor Destroy; Override;
function CanAddOperationToHashTree(op : TPCOperation) : Boolean;
function AddOperationToHashTree(op : TPCOperation) : Boolean;
Procedure ClearHastThree;
Property HashTree : TRawBytes read GetHashTree;
Function OperationsCount : Integer;
Function GetOperation(index : Integer) : TPCOperation;
Function GetOperationsAffectingAccount(account_number : Cardinal; List : TList<Cardinal>) : Integer;
Procedure CopyFromHashTree(Sender : TOperationsHashTree);
Property TotalAmount : Int64 read FTotalAmount;
Property TotalFee : Int64 read FTotalFee;
function SaveOperationsHashTreeToStream(AStream: TStream; ASaveToStorage : Boolean): Boolean;
function LoadOperationsHashTreeFromStream(AStream: TStream; ALoadingFromStorage : Boolean; ASetOperationsToProtocolVersion : Word; ALoadFromStorageVersion : Word; APreviousUpdatedBlocks : TAccountPreviousBlockInfo; var AErrors : String): Boolean; overload;
function LoadOperationsHashTreeFromStream(AStream: TStream; ALoadingFromStorage : Boolean; ASetOperationsToProtocolVersion : Word; ALoadFromStorageVersion : Word; APreviousUpdatedBlocks : TAccountPreviousBlockInfo; AAllow0FeeOperations : Boolean; var AOperationsCount, AProcessedCount : Integer; var AErrors : String): Boolean; overload;
function IndexOfOperation(op : TPCOperation) : Integer;
function CountOperationsBySameSignerWithoutFee(account_number : Cardinal) : Integer;
Procedure Delete(index : Integer);
function IndexOfOpReference(const opReference : TOpReference) : Integer;
procedure RemoveByOpReference(const opReference : TOpReference);
Property OnChanged : TNotifyEvent read FOnChanged write FOnChanged;
Property Max0feeOperationsBySigner : Integer Read FMax0feeOperationsBySigner write SetMax0feeOperationsBySigner;
procedure MarkVerifiedECDSASignatures(operationsHashTreeToMark : TOperationsHashTree);
Property HasOpRecoverOperations : Boolean read FHasOpRecoverOperations;
// Will add all operations of the HashTree to then end of AList without removing previous objects
function GetOperationsList(AList : TList<TPCOperation>; AAddOnlyOperationsWithoutNotVerifiedSignature : Boolean) : Integer;
End;
{ TPCOperationsComp }
TPCOperationsComp = Class
private
FBank: TPCBank;
FSafeBoxTransaction : TPCSafeBoxTransaction;
FOperationBlock: TOperationBlock;
FOperationsHashTree : TOperationsHashTree;
FDigest_Part1 : TRawBytes;
FDigest_Part2_Payload : TRawBytes;
FDigest_Part3 : TRawBytes;
FIsOnlyOperationBlock: Boolean;
FStreamPoW : TMemoryStream;
FDisableds : Integer;
FOperationsLock : TPCCriticalSection;
FPreviousUpdatedBlocks : TAccountPreviousBlockInfo; // New Protocol V3 struct to store previous updated blocks
FHasValidOperationBlockInfo : Boolean;
function GetOperation(index: Integer): TPCOperation;
procedure SetBank(const value: TPCBank);
procedure SetnOnce(const value: Cardinal);
procedure Settimestamp(const value: Cardinal);
function GetnOnce: Cardinal;
function Gettimestamp: Cardinal;
procedure SetAccountKey(const value: TAccountKey);
function GetAccountKey: TAccountKey;
Procedure Calc_Digest_Parts;
Procedure Calc_Digest_Part3;
Procedure CalcProofOfWork(fullcalculation : Boolean; var PoW: TRawBytes);
function GetBlockPayload: TRawBytes;
procedure SetBlockPayload(const Value: TRawBytes);
procedure OnOperationsHashTreeChanged(Sender : TObject);
protected
function SaveBlockToStreamExt(save_only_OperationBlock : Boolean; Stream: TStream; SaveToStorage : Boolean): Boolean;
function LoadBlockFromStreamExt(Stream: TStream; LoadingFromStorage : Boolean; var errors: String): Boolean;
public
Constructor Create(ABank: TPCBank);
Destructor Destroy; Override;
Procedure CopyFromExceptAddressKey(Operations : TPCOperationsComp);
Procedure CopyFrom(Operations : TPCOperationsComp);
Function AddOperation(Execute : Boolean; op: TPCOperation; var errors: String): Boolean;
Function AddOperations(operations: TOperationsHashTree; var errors: String): Integer;
Property Operation[index: Integer]: TPCOperation read GetOperation;
Property bank: TPCBank read FBank write SetBank;
Procedure Clear(DeleteOperations : Boolean);
Function Count: Integer;
Property OperationBlock: TOperationBlock read FOperationBlock;
procedure SetOperationBlock(const ANewValues : TOperationBlock); // For testing purposes only
Class Function OperationBlockToText(const OperationBlock: TOperationBlock) : String;
Class Function SaveOperationBlockToStream(Const OperationBlock: TOperationBlock; Stream: TStream) : Boolean;
class Function LoadOperationBlockFromStream(AStream : TStream; var Asoob : Byte; var AOperationBlock : TOperationBlock) : Boolean;
Property AccountKey: TAccountKey read GetAccountKey write SetAccountKey;
Property nonce: Cardinal read GetnOnce write SetnOnce;
Property timestamp: Cardinal read Gettimestamp write Settimestamp;
Property BlockPayload : TRawBytes read GetBlockPayload write SetBlockPayload;
function Update_And_RecalcPOW(newNOnce, newTimestamp : Cardinal; newBlockPayload : TRawBytes) : Boolean;
procedure UpdateTimestamp;
function SaveBlockToStorage(Stream: TStream): Boolean;
function SaveBlockToStream(save_only_OperationBlock : Boolean; Stream: TStream): Boolean;
function LoadBlockFromStorage(Stream: TStream; var errors: String): Boolean;
function LoadBlockFromStream(Stream: TStream; var errors: String): Boolean;
//
Function GetMinerRewardPseudoOperation : TOperationResume;
Function AddMinerRecover(LRecoverAccounts: TAccountList; const ANewAccountKey : TAccountKey) : Boolean;
Function ValidateOperationBlock(var errors : String) : Boolean;
Property IsOnlyOperationBlock : Boolean read FIsOnlyOperationBlock;
Procedure Lock;
Procedure Unlock;
//
Procedure SanitizeOperations;
Class Function RegisterOperationClass(OpClass: TPCOperationClass): Boolean;
Class Function IndexOfOperationClass(OpClass: TPCOperationClass): Integer;
Class Function IndexOfOperationClassByOpType(OpType: Cardinal): Integer;
Class Function GetOperationClassByOpType(OpType: Cardinal): TPCOperationClass;
Class Function GetFirstBlock : TOperationBlock;
Class Function EqualsOperationBlock(Const OperationBlock1,OperationBlock2 : TOperationBlock):Boolean;
//
Property SafeBoxTransaction : TPCSafeBoxTransaction read FSafeBoxTransaction;
Property OperationsHashTree : TOperationsHashTree read FOperationsHashTree;
Property PoW_Digest_Part1 : TRawBytes read FDigest_Part1;
Property PoW_Digest_Part2_Payload : TRawBytes read FDigest_Part2_Payload;
Property PoW_Digest_Part3 : TRawBytes read FDigest_Part3;
//
Property PreviousUpdatedBlocks : TAccountPreviousBlockInfo read FPreviousUpdatedBlocks; // New Protocol V3 struct to store previous updated blocks
Property HasValidOperationBlockInfo : Boolean read FHasValidOperationBlockInfo write FHasValidOperationBlockInfo;
End;
TPCBankLog = procedure(sender: TPCBank; Operations: TPCOperationsComp; Logtype: TLogType ; const Logtxt: String) of object;
TPCBankNotify = Class(TComponent)
private
FOnNewBlock: TNotifyEvent;
FBank: TPCBank;
procedure SetBank(const Value: TPCBank);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); Override;
Procedure NotifyNewBlock;
public
Constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
Property Bank : TPCBank read FBank write SetBank;
Property OnNewBlock : TNotifyEvent read FOnNewBlock write FOnNewBlock;
End;
TOrphan = RawByteString;
TCheckPointStruct = {$IFDEF USE_ABSTRACTMEM}TPCAbstractMem{$ELSE}TStream{$ENDIF};
{ TStorage }
TStorage = Class(TComponent)
private
FBank : TPCBank;
FReadOnly: Boolean;
FPendingBufferOperationsStream : TFileStream;
procedure SetBank(const Value: TPCBank);
Function GetPendingBufferOperationsStream : TFileStream;
protected
FIsMovingBlockchain : Boolean;
FStorageFilename: String;
procedure SetReadOnly(const Value: Boolean); virtual;
Function DoLoadBlockChain(Operations : TPCOperationsComp; Block : Cardinal) : Boolean; virtual; abstract;
Function DoSaveBlockChain(Operations : TPCOperationsComp) : Boolean; virtual; abstract;
Function DoMoveBlockChain(StartBlock : Cardinal; Const DestOrphan : TOrphan) : Boolean; virtual; abstract;
Procedure DoDeleteBlockChainBlocks(StartingDeleteBlock : Cardinal); virtual; abstract;
Function DoBlockExists(Block : Cardinal) : Boolean; virtual; abstract;
function GetFirstBlockNumber: Int64; virtual; abstract;
function GetLastBlockNumber: Int64; virtual; abstract;
function DoInitialize:Boolean; virtual; abstract;
Procedure DoEraseStorage; virtual; abstract;
Procedure DoSavePendingBufferOperations(OperationsHashTree : TOperationsHashTree); virtual;
Procedure DoLoadPendingBufferOperations(OperationsHashTree : TOperationsHashTree); virtual;
Function DoGetBlockInformation(const ABlock : Integer; var AOperationBlock : TOperationBlock; var AOperationsCount : Integer; var AVolume : Int64) : Boolean; virtual;
Function DoGetBlockOperations(ABlock, AOpBlockStartIndex, AMaxOperations : Integer; var AOperationBlock : TOperationBlock; var AOperationsCount : Integer; var AVolume : Int64; const AOperationsResumeList:TOperationsResumeList) : Boolean; virtual;
Function DoGetAccountOperations(AAccount : Integer; AMaxDepth, AStartOperation, AMaxOperations, ASearchBackwardsStartingAtBlock: Integer; const AOperationsResumeList:TOperationsResumeList): Boolean; virtual;
function DoFindOperation(const AOpHash : TBytes; var AOperationResume : TOperationResume) : TSearchOpHashResult; virtual;
public
Function LoadBlockChainBlock(Operations : TPCOperationsComp; Block : Cardinal) : Boolean;
Function SaveBlockChainBlock(Operations : TPCOperationsComp) : Boolean;
Function MoveBlockChainBlocks(StartBlock : Cardinal; Const DestOrphan : TOrphan; DestStorage : TStorage) : Boolean;
Procedure DeleteBlockChainBlocks(StartingDeleteBlock : Cardinal);
Constructor Create(AOwner : TComponent); Override;
Destructor Destroy; override;
Property ReadOnly : Boolean read FReadOnly write SetReadOnly;
Property Bank : TPCBank read FBank write SetBank;
Procedure CopyConfiguration(Const CopyFrom : TStorage); virtual;
Property FirstBlock : Int64 read GetFirstBlockNumber;
Property LastBlock : Int64 read GetLastBlockNumber;
Function Initialize : Boolean;
Procedure EraseStorage; // Erase Blockchain storage
Procedure SavePendingBufferOperations(OperationsHashTree : TOperationsHashTree);
Procedure LoadPendingBufferOperations(OperationsHashTree : TOperationsHashTree);
Function BlockExists(Block : Cardinal) : Boolean;
function Orphan : String;
Function GetBlockInformation(ABlock : Integer; var AOperationBlock : TOperationBlock; var AOperationsCount : Integer; var AVolume : Int64) : Boolean;
Function GetBlockOperations(ABlock, AOpBlockStartIndex, AMaxOperations : Integer; var AOperationBlock : TOperationBlock; var AOperationsCount : Integer; var AVolume : Int64; const AOperationsResumeList:TOperationsResumeList) : Boolean;
Function GetAccountOperations(AAccount : Integer; AMaxDepth, AStartOperation, AMaxOperations, ASearchBackwardsStartingAtBlock: Integer; const AOperationsResumeList:TOperationsResumeList): Boolean;
function FindOperation(const AOpHash : TBytes; var AOperationResume : TOperationResume) : TSearchOpHashResult;
property StorageFilename : String read FStorageFilename write FStorageFilename;
End;
TStorageClass = Class of TStorage;
{ TPCBank }
TPCBank = Class(TComponent)
private
FStorage : TStorage;
FSafeBox: TPCSafeBox;
FLastBlockCache : TPCOperationsComp;
FLastOperationBlock: TOperationBlock;
FIsRestoringFromFile: Boolean;
FOnLog: TPCBankLog;
FBankLock: TPCCriticalSection;
FNotifyList : TList<TPCBankNotify>;
FStorageClass: TStorageClass;
FOrphan: TOrphan;
function GetStorage: TStorage;
procedure SetStorageClass(const Value: TStorageClass);
Function DoSaveBank : Boolean;
public
Constructor Create(AOwner: TComponent); Override;
Destructor Destroy; Override;
Function BlocksCount: Cardinal;
Function AccountsCount : Cardinal;
procedure AssignTo(Dest: TPersistent); Override;
function GetActualTargetSecondsAverage(BackBlocks : Cardinal): Real;
function GetTargetSecondsAverage(FromBlock,BackBlocks : Cardinal): Real;
function GetTargetSecondsMedian(AFromBlock: Cardinal; ABackBlocks : Integer): Real;
function LoadBankFromChunks(AChunks : TPCSafeboxChunks; checkSafeboxHash : TRawBytes; previousCheckedSafebox : TPCSafebox; progressNotify : TProgressNotify; var errors : String) : Boolean;
function LoadBankFromStream(Stream : TStream; useSecureLoad : Boolean; checkSafeboxHash : TRawBytes; previousCheckedSafebox : TPCSafebox; progressNotify : TProgressNotify; var errors : String) : Boolean;
Procedure Clear;
Function LoadOperations(Operations : TPCOperationsComp; Block : Cardinal) : Boolean;
Property SafeBox : TPCSafeBox read FSafeBox;
Function AddNewBlockChainBlock(Operations: TPCOperationsComp; MaxAllowedTimestamp : Cardinal; var errors: String): Boolean;
Procedure DiskRestoreFromOperations(max_block : Int64; restoreProgressNotify : TProgressNotify = Nil);
Procedure UpdateValuesFromSafebox;
Procedure NewLog(Operations: TPCOperationsComp; Logtype: TLogType; const Logtxt: String);
Property OnLog: TPCBankLog read FOnLog write FOnLog;
Property LastOperationBlock : TOperationBlock read FLastOperationBlock; // TODO: Use
Property Storage : TStorage read GetStorage;
Property StorageClass : TStorageClass read FStorageClass write SetStorageClass;
Function IsReady(Var CurrentProcess : String) : Boolean;
Property LastBlockFound : TPCOperationsComp read FLastBlockCache;
Function OpenSafeBoxCheckpoint(ABlockCount : Cardinal) : TCheckPointStruct;
Class Function GetSafeboxCheckpointingFileName(Const ABaseDataFolder : String; ABlock : Cardinal) : String;
Class Function GetStorageFolder(Const AOrphan : String) : String;
Function RestoreBank(AMax_block : Int64; AOrphan : String; ARestoreProgressNotify : TProgressNotify) : Boolean;
Function LoadBankFileInfo(Const AFilename : String; var ASafeBoxHeader : TPCSafeBoxHeader) : Boolean;
Property Orphan : TOrphan read FOrphan write FOrphan;
Function SaveBank(forceSave : Boolean) : Boolean;
Property IsRestoringFromFile : Boolean read FIsRestoringFromFile;
End;
Const
CT_Safebox_Extension = {$IFDEF USE_ABSTRACTMEM}'.am_safebox'{$ELSE}'.safebox'{$ENDIF};
CT_TOperationPayload_NUL : TOperationPayload = (payload_type:0;payload_raw:Nil);
CT_TOperationResume_NUL : TOperationResume = (valid:false;Block:0;NOpInsideBlock:-1;OpType:0;OpSubtype:0;time:0;AffectedAccount:0;SignerAccount:-1;n_operation:0;DestAccount:-1;SellerAccount:-1;newKey:(EC_OpenSSL_NID:0;x:Nil;y:Nil);OperationTxt:'';Amount:0;Fee:0;Balance:0;OriginalPayload:(payload_type:0;payload_raw:nil);PrintablePayload:'';DecodedEPasaPayload:'';OperationHash:Nil;OperationHash_OLD:Nil;errors:'';isMultiOperation:False;Senders:Nil;Receivers:Nil;changers:Nil);
CT_TMultiOpSender_NUL : TMultiOpSender = (Account:0;Amount:0;N_Operation:0;Payload:(payload_type:0;payload_raw:Nil);Signature:(r:Nil;s:Nil));
CT_TMultiOpReceiver_NUL : TMultiOpReceiver = (Account:0;Amount:0;Payload:(payload_type:0;payload_raw:Nil));
CT_TMultiOpChangeInfo_NUL : TMultiOpChangeInfo = (Account:0;N_Operation:0;Changes_type:[];New_Accountkey:(EC_OpenSSL_NID:0;x:Nil;y:Nil);New_Name:Nil;New_Type:0;New_Data:Nil;Seller_Account:-1;Account_Price:-1;Locked_Until_Block:0;
Hashed_secret:Nil;
Fee:0;Signature:(r:Nil;s:Nil));
CT_TOpChangeAccountInfoType_Txt : Array[Low(TOpChangeAccountInfoType)..High(TOpChangeAccountInfoType)] of String = ('public_key','account_name','account_type','list_for_public_sale','list_for_private_sale', 'delist', 'account_data','list_for_account_swap','list_for_coin_swap');
implementation
uses
Variants,
UTime, UConst, UOpTransaction, UPCOrderedLists,
UPCOperationsSignatureValidator,
UPCOperationsBlockValidator,
UAbstractMemBlockchainStorage,
UNode;
{ TPCOperationsStorage }
var
_PCOperationsStorage : TPCOperationsStorage;
function TPCOperationsStorage.FindOrderedByPtrPCOperation(lockedThreadList: TList<Pointer>; const Value: TPCOperation; out Index: Integer): Boolean;
var L, H, I: Integer;
C : PtrInt;
begin
Result := False;
L := 0;
H := lockedThreadList.Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := PtrInt(PPCOperationTStorage(lockedThreadList[I])^.ptrPCOperation) - PtrInt(Value);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
Index := L;
end;
constructor TPCOperationsStorage.Create;
begin
FPCOperationsStorageList := TPCThreadList<Pointer>.Create(ClassName);
FIntTotalNewOps := 0;
FIntTotalAdded := 0;
FIntTotalDeleted := 0;
FMaxLocksCount := 0;
FMaxLocksValue := 0;
end;
destructor TPCOperationsStorage.Destroy;
Var list : TList<Pointer>;
P : PPCOperationTStorage;
i : Integer;
pc : TPCOperation;
begin
list := LockPCOperationsStorage;
try
for i:=0 to list.Count-1 do begin
P := list[i];
pc := P^.ptrPCOperation;
P^.ptrPCOperation := Nil;
P^.locksCount:=-1;
pc.Free;
Dispose(P);
end;
inc(FIntTotalDeleted,list.Count);
finally
list.Clear;
UnlockPCOperationsStorage;
end;
FreeAndNil(FPCOperationsStorageList);
inherited Destroy;
end;
function TPCOperationsStorage.LockPCOperationsStorage: TList<Pointer>;
begin
Result := FPCOperationsStorageList.LockList;
end;
procedure TPCOperationsStorage.UnlockPCOperationsStorage;
begin
FPCOperationsStorageList.UnlockList;
end;
function TPCOperationsStorage.Count: Integer;
var list : TList<Pointer>;
begin
list := LockPCOperationsStorage;
try
Result := list.Count;
finally
UnlockPCOperationsStorage;
end;
end;
procedure TPCOperationsStorage.AddPCOperation(APCOperation: TPCOperation);
var P : PPCOperationTStorage;
list : TList<Pointer>;
iPos : Integer;
begin
list := LockPCOperationsStorage;
try
if FindOrderedByPtrPCOperation(list,APCOperation,iPos) then begin
P := list[iPos];
end else begin
New(P);
P^.locksCount:=0;
P^.ptrPCOperation := APCOperation;
list.Insert(iPos,P);
inc(FIntTotalNewOps);
end;
inc(P^.locksCount);
inc(FIntTotalAdded);
if (P^.locksCount>FMaxLocksValue) then begin
FMaxLocksValue:=P^.locksCount;
FMaxLocksCount:=0;
end;
inc(FMaxLocksCount);
finally
UnlockPCOperationsStorage;
end;
end;
procedure TPCOperationsStorage.RemovePCOperation(APCOperation: TPCOperation);
var P : PPCOperationTStorage;
list : TList<Pointer>;
iPos : Integer;
begin
list := LockPCOperationsStorage;
try
if FindOrderedByPtrPCOperation(list,APCOperation,iPos) then begin
P := list[iPos];
Dec(P^.locksCount);
if (P^.locksCount<=0) then begin
// Remove
list.Delete(iPos);
P^.ptrPCOperation := Nil;
Dispose(P);
APCOperation.Free;
end;
inc(FIntTotalDeleted);
end else begin
TLog.NewLog(lterror,ClassName,'ERROR DEV 20181218-2 Operation not found in storage to remove: '+APCOperation.ToString);
end;
finally
UnlockPCOperationsStorage;
end;
end;
function TPCOperationsStorage.FindPCOperation(APCOperation: TPCOperation): Boolean;
var list : TList<Pointer>;
iPos : Integer;
begin
list := LockPCOperationsStorage;
Try
Result := FindOrderedByPtrPCOperation(list,APCOperation,iPos);
finally
UnlockPCOperationsStorage;
end;
end;
function TPCOperationsStorage.FindPCOperationAndIncCounterIfFound(APCOperation: TPCOperation): Boolean;
var list : TList<Pointer>;
iPos : Integer;
begin
list := LockPCOperationsStorage;
Try
Result := FindOrderedByPtrPCOperation(list,APCOperation,iPos);
if Result then begin
Inc(PPCOperationTStorage(list[iPos])^.locksCount);
inc(FIntTotalAdded);
if (PPCOperationTStorage(list[iPos])^.locksCount>FMaxLocksValue) then begin
FMaxLocksValue:=PPCOperationTStorage(list[iPos])^.locksCount;
FMaxLocksCount:=0;
end;
inc(FMaxLocksCount);
end;
finally
UnlockPCOperationsStorage;
end;
end;
class function TPCOperationsStorage.PCOperationsStorage: TPCOperationsStorage;
begin
Result := _PCOperationsStorage;
end;
procedure TPCOperationsStorage.GetStats(strings: TStrings);
var list : TList<Pointer>;
i : Integer;
P : PPCOperationTStorage;
begin
list := LockPCOperationsStorage;
try
strings.Add(Format('%s Operations:%d NewAdded:%d Added:%d Deleted:%d',[ClassName,list.Count,FIntTotalNewOps,FIntTotalAdded,FIntTotalDeleted]));
strings.Add(Format('MaxLocks:%d MaxLocksCount:%d',[FMaxLocksValue,FMaxLocksCount]));
for i:=0 to list.Count-1 do begin
P := PPCOperationTStorage(list[i]);
strings.Add(Format('%d %s',[P^.locksCount,P^.ptrPCOperation.ToString]));
end;
finally
UnlockPCOperationsStorage;
end;
end;
{ TPCBank }
function TPCBank.AccountsCount: Cardinal;
begin
Result := FSafeBox.AccountsCount;
end;
function TPCBank.AddNewBlockChainBlock(Operations: TPCOperationsComp; MaxAllowedTimestamp : Cardinal; var errors: String): Boolean;
Var i : Integer;
begin
TPCThread.ProtectEnterCriticalSection(Self,FBankLock);
Try
Result := False;
errors := '';
Operations.Lock; // New Protection
Try
If Not Operations.ValidateOperationBlock(errors) then begin
exit;
end;
if (Operations.OperationBlock.block > 0) then begin
if ((MaxAllowedTimestamp>0) And (Operations.OperationBlock.timestamp>MaxAllowedTimestamp)) then begin
errors := 'Invalid timestamp (Future time: New timestamp '+Inttostr(Operations.OperationBlock.timestamp)+' > max allowed '+inttostr(MaxAllowedTimestamp)+')';
exit;
end;
end;
// Ok, include!
// WINNER !!!
// Congrats!
if Not Operations.SafeBoxTransaction.Commit(Operations.OperationBlock,errors) then begin
exit;
end;
// Initialize values
FLastOperationBlock := Operations.OperationBlock;
// log it!
NewLog(Operations, ltupdate,
Format('New block height:%d nOnce:%d timestamp:%d Operations:%d Fee:%d SafeBoxBalance:%d=%d PoW:%s Operations previous Safe Box hash:%s Future old Safe Box hash for next block:%s',
[ Operations.OperationBlock.block,Operations.OperationBlock.nonce,Operations.OperationBlock.timestamp,
Operations.Count,
Operations.OperationBlock.fee,
SafeBox.TotalBalance,
Operations.SafeBoxTransaction.TotalBalance,
TCrypto.ToHexaString(Operations.OperationBlock.proof_of_work),
TCrypto.ToHexaString(Operations.OperationBlock.initial_safe_box_hash),
TCrypto.ToHexaString(SafeBox.SafeBoxHash)]));
// Save Operations to disk
if Not FIsRestoringFromFile then begin
Storage.SaveBlockChainBlock(Operations);
end;
FLastBlockCache.CopyFrom(Operations);
Operations.Clear(true);
Result := true;
Finally
if Not Result then begin
NewLog(Operations, lterror, 'Invalid new block '+inttostr(Operations.OperationBlock.block)+': ' + errors+ ' > '+TPCOperationsComp.OperationBlockToText(Operations.OperationBlock));
end;
Operations.Unlock;
End;
Finally
FBankLock.Release;
End;
if Result then begin
for i := 0 to FNotifyList.Count - 1 do begin
TPCBankNotify(FNotifyList.Items[i]).NotifyNewBlock;
end;
end;
end;
procedure TPCBank.AssignTo(Dest: TPersistent);
var d : TPCBank;
begin
if (Not (Dest is TPCBank)) then begin
inherited;
exit;
end;
if (Self=Dest) then exit;
d := TPCBank(Dest);
d.SafeBox.CopyFrom(SafeBox);
d.FLastOperationBlock := FLastOperationBlock;
d.FIsRestoringFromFile := FIsRestoringFromFile;
d.FLastBlockCache.CopyFrom( FLastBlockCache );
end;
function TPCBank.BlocksCount: Cardinal;
begin
Result := SafeBox.BlocksCount;
end;
procedure TPCBank.Clear;
begin
SafeBox.Clear;
FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
FLastOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash; // Genesis hash
FLastBlockCache.Clear(true);
{$IFDEF HIGHLOG}NewLog(Nil, ltdebug, 'Clear Bank');{$ENDIF}
end;
constructor TPCBank.Create(AOwner: TComponent);
begin
inherited;
FStorage := Nil;
FStorageClass := Nil;
FBankLock := TPCCriticalSection.Create('TPCBank_BANKLOCK');
FIsRestoringFromFile := False;
FOnLog := Nil;
FSafeBox := TPCSafeBox.Create;
FNotifyList := TList<TPCBankNotify>.Create;
FLastBlockCache := TPCOperationsComp.Create(Nil);
FIsRestoringFromFile:=False;
Clear;
end;
destructor TPCBank.Destroy;
var step : String;
begin
Try
step := 'Deleting critical section';
FreeAndNil(FBankLock);
step := 'Clear';
Clear;
step := 'Destroying LastBlockCache';
FreeAndNil(FLastBlockCache);
step := 'Destroying SafeBox';
FreeAndNil(FSafeBox);
step := 'Destroying NotifyList';
FreeAndNil(FNotifyList);
step := 'Destroying Storage';
FreeAndNil(FStorage);
step := 'inherited';
inherited;
Except
On E:Exception do begin
TLog.NewLog(lterror,Classname,'Error destroying Bank step: '+step+' Errors ('+E.ClassName+'): ' +E.Message);
Raise;
end;
End;
end;
procedure TPCBank.DiskRestoreFromOperations(max_block : Int64; restoreProgressNotify : TProgressNotify = Nil);
Var
errors: String;
n : Int64;
tc, LStartProcessTC : TTickCount;
LBlocks : TList<TPCOperationsComp>;
LTmpPCOperationsComp : TPCOperationsComp;
i,j, LProgressBlock, LProgressEndBlock, LOpsInBlocks : Integer;
LSafeboxTransaction : TPCSafeBoxTransaction;
LTempSafebox : TPCSafeBox;
begin
if FIsRestoringFromFile then begin
TLog.NewLog(lterror,Classname,'Is Restoring!!!');
raise Exception.Create('Is restoring!');
end;
tc := TPlatform.GetTickCount;
LStartProcessTC := tc;
TPCThread.ProtectEnterCriticalSection(Self,FBankLock);
try
FIsRestoringFromFile := true;
try
Clear;
Storage.Initialize;
If (max_block<Storage.LastBlock) or (Storage.LastBlock<0) then n := max_block
else n := Storage.LastBlock;
RestoreBank(n,Orphan,restoreProgressNotify);
// Restore last blockchain
if (BlocksCount>0) And (SafeBox.CurrentProtocol=CT_PROTOCOL_1) then begin
if Not Storage.LoadBlockChainBlock(FLastBlockCache,BlocksCount-1) then begin
NewLog(nil,lterror,'Cannot find blockchain '+inttostr(BlocksCount-1)+' so cannot accept bank current block '+inttostr(BlocksCount));
Clear;
end else begin
FLastOperationBlock := FLastBlockCache.OperationBlock;
end;
end;
If SafeBox.BlocksCount>0 then FLastOperationBlock := SafeBox.GetBlockInfo(SafeBox.BlocksCount-1)
else begin
FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
FLastOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash; // Genesis hash
end;
NewLog(Nil, ltinfo,'Start restoring from disk operations (Max '+inttostr(max_block)+') BlockCount: '+inttostr(BlocksCount)+' Orphan: ' +Orphan);
LBlocks := TList<TPCOperationsComp>.Create;
try
LProgressBlock := 0;
LProgressEndBlock := Storage.LastBlock - BlocksCount;
while ((BlocksCount<=max_block)) do begin
i := BlocksCount;
j := i + 999;
// Load a batch of TPCOperationsComp;
try
LOpsInBlocks := 0;