Parcourir la source

Update UAbstractMemBlockchainStorage.pas

PascalCoin il y a 1 an
Parent
commit
e15d2c82ab
1 fichiers modifiés avec 36 ajouts et 1 suppressions
  1. 36 1
      src/core/UAbstractMemBlockchainStorage.pas

+ 36 - 1
src/core/UAbstractMemBlockchainStorage.pas

@@ -265,6 +265,7 @@ type
   TAbstractMemBlockchainStorageSecondary = Class(TAbstractMemBlockchainStorage)
   TAbstractMemBlockchainStorageSecondary = Class(TAbstractMemBlockchainStorage)
   private
   private
     FAuxStorage : TStorage;
     FAuxStorage : TStorage;
+    FSaving : Boolean;
   protected
   protected
     procedure SetReadOnly(const Value: Boolean); override;
     procedure SetReadOnly(const Value: Boolean); override;
     Function DoSaveBlockChain(Operations : TPCOperationsComp) : Boolean; override;
     Function DoSaveBlockChain(Operations : TPCOperationsComp) : Boolean; override;
@@ -1981,6 +1982,7 @@ constructor TAbstractMemBlockchainStorageSecondary.Create(AOwner: TComponent);
 begin
 begin
   inherited;
   inherited;
   FAuxStorage := Nil;
   FAuxStorage := Nil;
+  FSaving := False;
 end;
 end;
 
 
 destructor TAbstractMemBlockchainStorageSecondary.Destroy;
 destructor TAbstractMemBlockchainStorageSecondary.Destroy;
@@ -1995,6 +1997,7 @@ begin
   inherited;
   inherited;
   AFound := False;
   AFound := False;
   if (Assigned(FAuxStorage)) then begin
   if (Assigned(FAuxStorage)) then begin
+    FSaving := True;
     LOperationsComp := TPCOperationsComp.Create(Nil);
     LOperationsComp := TPCOperationsComp.Create(Nil);
     Try
     Try
       if FAuxStorage.LoadBlockChainBlock(LOperationsComp,ABlock) then begin
       if FAuxStorage.LoadBlockChainBlock(LOperationsComp,ABlock) then begin
@@ -2003,6 +2006,7 @@ begin
       end;
       end;
     Finally
     Finally
       LOperationsComp.Free;
       LOperationsComp.Free;
+      FSaving := False;
     End;
     End;
   end;
   end;
 end;
 end;
@@ -2017,6 +2021,35 @@ begin
 end;
 end;
 
 
 function TAbstractMemBlockchainStorageSecondary.DoInitialize: Boolean;
 function TAbstractMemBlockchainStorageSecondary.DoInitialize: Boolean;
+  procedure FillSecondary;
+  var i, LTotal, LNotFound : Integer;
+    Ltc : TTickCount;
+    LOpComp : TPCOperationsComp;
+  begin
+    i := FAuxStorage.LastBlock;
+    if i>=Self.LastBlock then Exit;
+    TLog.NewLog(ltdebug,ClassName,Format('Start filling secondary storage with blocks from %d to %d',[i,Self.LastBlock]));
+    Ltc := TPlatform.GetTickCount;
+    LOpComp := TPCOperationsComp.Create(Nil);
+    try
+      LTotal := 0; LNotFound := 0;
+      while (i<=Self.LastBlock) do begin
+        if (Self.DoLoadBlockChain(LOpComp,i)) then begin
+          inc(LTotal);
+          FAuxStorage.SaveBlockChainBlock(LOpComp);
+        end else inc(LNotFound);
+        inc(i);
+        if TPlatform.GetElapsedMilliseconds(Ltc)>10000 then begin
+          TLog.NewLog(ltdebug,ClassName,Format('Filling secondary storage with blocks current %d to %d done %d not found %d',[i,Self.LastBlock,LTotal,LNotFound]));
+          Ltc := TPlatform.GetTickCount;
+        end;
+      end;
+    finally
+      LOpComp.Free;
+    end;
+    TLog.NewLog(ltdebug,ClassName,Format('Finalized filling secondary storage with blocks to %d done %d not found %d',[Self.LastBlock,LTotal,LNotFound]));
+  end;
+
 begin
 begin
   Result := inherited DoInitialize;
   Result := inherited DoInitialize;
   if (Result) And (Not Assigned(FAuxStorage)) then begin
   if (Result) And (Not Assigned(FAuxStorage)) then begin
@@ -2024,6 +2057,8 @@ begin
     FAuxStorage.Bank := Self.Bank;
     FAuxStorage.Bank := Self.Bank;
     FAuxStorage.ReadOnly := Self.ReadOnly;
     FAuxStorage.ReadOnly := Self.ReadOnly;
     Result := FAuxStorage.Initialize;
     Result := FAuxStorage.Initialize;
+    // Try to fill secondary with newest blocks...
+    FillSecondary;
   end;
   end;
 end;
 end;
 
 
@@ -2040,7 +2075,7 @@ function TAbstractMemBlockchainStorageSecondary.DoSaveBlockChain(
   Operations: TPCOperationsComp): Boolean;
   Operations: TPCOperationsComp): Boolean;
 begin
 begin
   Result := inherited;
   Result := inherited;
-  if (Result) and (Assigned(FAuxStorage)) then begin
+  if (Result) and (Assigned(FAuxStorage)) and (Not FSaving) then begin
     Result := FAuxStorage.SaveBlockChainBlock(Operations);
     Result := FAuxStorage.SaveBlockChainBlock(Operations);
   end;
   end;
 end;
 end;