Browse Source

InitialSafeBoxHash optimization

Execute a InitialSafeBoxHash only once
PascalCoin 6 years ago
parent
commit
c7e9e0a3c3
2 changed files with 17 additions and 6 deletions
  1. 12 1
      src/core/UAccounts.pas
  2. 5 5
      src/core/UBlockChain.pas

+ 12 - 1
src/core/UAccounts.pas

@@ -300,6 +300,7 @@ Type
     Class Function LoadSafeBoxStreamHeader(Stream : TStream; var sbHeader : TPCSafeBoxHeader) : Boolean;
     Class Function LoadSafeBoxStreamHeader(Stream : TStream; var sbHeader : TPCSafeBoxHeader) : Boolean;
     Class Function SaveSafeBoxStreamHeader(Stream : TStream; protocol : Word; OffsetStartBlock, OffsetEndBlock, CurrentSafeBoxBlocksCount : Cardinal) : Boolean;
     Class Function SaveSafeBoxStreamHeader(Stream : TStream; protocol : Word; OffsetStartBlock, OffsetEndBlock, CurrentSafeBoxBlocksCount : Cardinal) : Boolean;
     Class Function MustSafeBoxBeSaved(BlocksCount : Cardinal) : Boolean;
     Class Function MustSafeBoxBeSaved(BlocksCount : Cardinal) : Boolean;
+    Class Function InitialSafeboxHash : TRawBytes;
     Procedure SaveSafeBoxBlockToAStream(DestStream : TStream; nBlock : Cardinal);
     Procedure SaveSafeBoxBlockToAStream(DestStream : TStream; nBlock : Cardinal);
     Procedure SaveSafeBoxToAStream(Stream : TStream; FromBlock, ToBlock : Cardinal);
     Procedure SaveSafeBoxToAStream(Stream : TStream; FromBlock, ToBlock : Cardinal);
     class Function CopySafeBoxStream(Source,Dest : TStream; FromBlock, ToBlock : Cardinal; var errors : String) : Boolean;
     class Function CopySafeBoxStream(Source,Dest : TStream; FromBlock, ToBlock : Cardinal; var errors : String) : Boolean;
@@ -2114,7 +2115,7 @@ begin
   StartThreadSafe;
   StartThreadSafe;
   try
   try
     // If No buffer to hash is because it's firts block... so use Genesis: CT_Genesis_Magic_String_For_Old_Block_Hash
     // If No buffer to hash is because it's firts block... so use Genesis: CT_Genesis_Magic_String_For_Old_Block_Hash
-    if (FBufferBlocksHash.Length=0) then Result := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash))
+    if (FBufferBlocksHash.Length=0) then Result := InitialSafeboxHash
     else Result := TCrypto.DoSha256(FBufferBlocksHash.Memory,FBufferBlocksHash.Length);
     else Result := TCrypto.DoSha256(FBufferBlocksHash.Memory,FBufferBlocksHash.Length);
   finally
   finally
     EndThreadSave;
     EndThreadSave;
@@ -3311,6 +3312,16 @@ begin
   Result := True;
   Result := True;
 end;
 end;
 
 
+var _initialSafeboxHash : TRawBytes = Nil;
+
+class function TPCSafeBox.InitialSafeboxHash: TRawBytes;
+begin
+  if (Length(_initialSafeboxHash)=0) then begin
+     _initialSafeboxHash := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash))
+  end;
+  Result := Copy(_initialSafeboxHash);
+end;
+
 function TPCSafeBox.IsValidNewOperationsBlock(const newOperationBlock: TOperationBlock; checkSafeBoxHash : Boolean; var errors: String): Boolean;
 function TPCSafeBox.IsValidNewOperationsBlock(const newOperationBlock: TOperationBlock; checkSafeBoxHash : Boolean; var errors: String): Boolean;
   { This function will check a OperationBlock info as a valid candidate to be included in the safebox
   { This function will check a OperationBlock info as a valid candidate to be included in the safebox
 
 

+ 5 - 5
src/core/UBlockChain.pas

@@ -838,7 +838,7 @@ procedure TPCBank.Clear;
 begin
 begin
   SafeBox.Clear;
   SafeBox.Clear;
   FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
   FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
-  FLastOperationBlock.initial_safe_box_hash := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash)); // Genesis hash
+  FLastOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash; // Genesis hash
   FLastBlockCache.Clear(true);
   FLastBlockCache.Clear(true);
   {$IFDEF HIGHLOG}NewLog(Nil, ltdebug, 'Clear Bank');{$ENDIF}
   {$IFDEF HIGHLOG}NewLog(Nil, ltdebug, 'Clear Bank');{$ENDIF}
 end;
 end;
@@ -967,7 +967,7 @@ begin
     try
     try
       FLastBlockCache.Clear(True);
       FLastBlockCache.Clear(True);
       FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
       FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
-      FLastOperationBlock.initial_safe_box_hash := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash)); // Genesis hash
+      FLastOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash; // Genesis hash
       If FSafeBox.BlocksCount>0 then begin
       If FSafeBox.BlocksCount>0 then begin
         Storage.Initialize;
         Storage.Initialize;
         If Storage.LoadBlockChainBlock(FLastBlockCache,FSafeBox.BlocksCount-1) then begin
         If Storage.LoadBlockChainBlock(FLastBlockCache,FSafeBox.BlocksCount-1) then begin
@@ -1073,7 +1073,7 @@ begin
       If SafeBox.BlocksCount>0 then FLastOperationBlock := SafeBox.Block(SafeBox.BlocksCount-1).blockchainInfo
       If SafeBox.BlocksCount>0 then FLastOperationBlock := SafeBox.Block(SafeBox.BlocksCount-1).blockchainInfo
       else begin
       else begin
         FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
         FLastOperationBlock := TPCOperationsComp.GetFirstBlock;
-        FLastOperationBlock.initial_safe_box_hash := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash)); // Genesis hash
+        FLastOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash; // Genesis hash
       end;
       end;
     finally
     finally
       FBankLock.Release;
       FBankLock.Release;
@@ -1279,7 +1279,7 @@ begin
       FOperationBlock.block := 0;
       FOperationBlock.block := 0;
       FOperationBlock.reward := TPascalCoinProtocol.GetRewardForNewLine(0);
       FOperationBlock.reward := TPascalCoinProtocol.GetRewardForNewLine(0);
       FOperationBlock.compact_target := CT_MinCompactTarget_v1;
       FOperationBlock.compact_target := CT_MinCompactTarget_v1;
-      FOperationBlock.initial_safe_box_hash := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash)); // Nothing for first line
+      FOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash; // Nothing for first line
       FOperationBlock.protocol_version := CT_PROTOCOL_1;
       FOperationBlock.protocol_version := CT_PROTOCOL_1;
       FOperationsHashTree.Max0feeOperationsBySigner := -1;
       FOperationsHashTree.Max0feeOperationsBySigner := -1;
     end;
     end;
@@ -1642,7 +1642,7 @@ begin
       FOperationBlock.block := 0;
       FOperationBlock.block := 0;
       FOperationBlock.reward := TPascalCoinProtocol.GetRewardForNewLine(0);
       FOperationBlock.reward := TPascalCoinProtocol.GetRewardForNewLine(0);
       FOperationBlock.compact_target := CT_MinCompactTarget_v1;
       FOperationBlock.compact_target := CT_MinCompactTarget_v1;
-      FOperationBlock.initial_safe_box_hash := TCrypto.DoSha256(TEncoding.ASCII.GetBytes(CT_Genesis_Magic_String_For_Old_Block_Hash));
+      FOperationBlock.initial_safe_box_hash := TPCSafeBox.InitialSafeboxHash;
       FOperationBlock.protocol_version := CT_PROTOCOL_1;
       FOperationBlock.protocol_version := CT_PROTOCOL_1;
     end;
     end;
     FOperationBlock.proof_of_work:=Nil;
     FOperationBlock.proof_of_work:=Nil;