Browse Source

Fast SafeboxRootHash (PIP-0030) calculation using buffered TBytesBuffer32Safebox variable

Also introducing new min difficulty (min target) for V5 testnet
PascalCoin 6 years ago
parent
commit
4e93382dbc
3 changed files with 20 additions and 9 deletions
  1. 16 7
      src/core/UAccounts.pas
  2. 3 1
      src/core/UConst.pas
  3. 1 1
      src/core/UPCSafeBoxRootHash.pas

+ 16 - 7
src/core/UAccounts.pas

@@ -278,7 +278,7 @@ Type
     FOrderedAccountKeysList : TOrderedAccountKeysList;
     FOrderedAccountKeysList : TOrderedAccountKeysList;
     //
     //
     FListOfOrderedAccountKeysList : TList<TOrderedAccountKeysList>;
     FListOfOrderedAccountKeysList : TList<TOrderedAccountKeysList>;
-    FBufferBlocksHash: TBytesBuffer;
+    FBufferBlocksHash: TBytesBuffer32Safebox;
     FOrderedByName : TOrderedRawList;
     FOrderedByName : TOrderedRawList;
     FTotalBalance: Int64;
     FTotalBalance: Int64;
     FSafeBoxHash : TRawBytes;
     FSafeBoxHash : TRawBytes;
@@ -362,7 +362,7 @@ Type
     Function GetMinimumAvailableSnapshotBlock : Integer;
     Function GetMinimumAvailableSnapshotBlock : Integer;
     Function HasSnapshotForBlock(block_number : Cardinal) : Boolean;
     Function HasSnapshotForBlock(block_number : Cardinal) : Boolean;
     Property OrderedAccountKeysList : TOrderedAccountKeysList read FOrderedAccountKeysList;
     Property OrderedAccountKeysList : TOrderedAccountKeysList read FOrderedAccountKeysList;
-    property BufferBlocksHash: TBytesBuffer read FBufferBlocksHash; // Warning: For testing purposes, do not use this property! TODO delete or put in protected mode
+    property BufferBlocksHash: TBytesBuffer32Safebox read FBufferBlocksHash; // Warning: For testing purposes, do not use this property! TODO delete or put in protected mode
   End;
   End;
 
 
 
 
@@ -754,7 +754,12 @@ begin
       Result := TCrypto.DoSha256(ABlocksHashBuffer.Memory,ABlocksHashBuffer.Length);
       Result := TCrypto.DoSha256(ABlocksHashBuffer.Memory,ABlocksHashBuffer.Length);
     end else begin
     end else begin
       // Implementation of PIP-0030, SafeboxHash will be calculated based on a MerkleTree obtaining a SafeboxRootHash
       // Implementation of PIP-0030, SafeboxHash will be calculated based on a MerkleTree obtaining a SafeboxRootHash
-      Result := TPCSafeboxRootHash.CalcSafeBoxRootHash(ABlocksHashBuffer);
+      if ABlocksHashBuffer is TBytesBuffer32Safebox then begin
+        TBytesBuffer32Safebox(ABlocksHashBuffer).SafeBoxHashCalcType := sbh_Merkle_Root_Hash;
+        Result := TBytesBuffer32Safebox(ABlocksHashBuffer).GetSafeBoxHash;
+      end else begin
+        Result := TPCSafeboxRootHash.CalcSafeBoxRootHash(ABlocksHashBuffer);
+      end;
     end;
     end;
   end;
   end;
 end;
 end;
@@ -899,10 +904,12 @@ end;
 
 
 class function TPascalCoinProtocol.MinimumTarget(protocol_version: Integer): Cardinal;
 class function TPascalCoinProtocol.MinimumTarget(protocol_version: Integer): Cardinal;
 begin
 begin
-  if protocol_version<CT_PROTOCOL_4 then begin
-    Result := CT_MinCompactTarget_v1;
-  end else begin
+  if protocol_version>=CT_PROTOCOL_5 then begin
+    Result := CT_MinCompactTarget_v5
+  end else if protocol_version>=CT_PROTOCOL_4 then begin
     Result := CT_MinCompactTarget_v4;
     Result := CT_MinCompactTarget_v4;
+  end else begin
+    Result := CT_MinCompactTarget_v1;
   end;
   end;
 end;
 end;
 
 
@@ -2619,7 +2626,8 @@ begin
   FDeletedNamesSincePreviousSafebox := TOrderedRawList.Create;
   FDeletedNamesSincePreviousSafebox := TOrderedRawList.Create;
   FSubChains := TList<TPCSafeBox>.Create;
   FSubChains := TList<TPCSafeBox>.Create;
   FOrderedAccountKeysList := TOrderedAccountKeysList.Create(Nil,True);
   FOrderedAccountKeysList := TOrderedAccountKeysList.Create(Nil,True);
-  FBufferBlocksHash := TBytesBuffer.Create(1000*32);
+  FBufferBlocksHash := TBytesBuffer32Safebox.Create(1000*32);
+  FBufferBlocksHash.SafeBoxHashCalcType := sbh_Single_Sha256;
   Clear;
   Clear;
 end;
 end;
 
 
@@ -3017,6 +3025,7 @@ begin
   TLog.NewLog(ltInfo,ClassName,'Start Upgrade to protocol 5 - Old Safeboxhash:'+TCrypto.ToHexaString(FSafeBoxHash)+' calculated: '+TCrypto.ToHexaString(LAux)+' Blocks: '+IntToStr(BlocksCount));
   TLog.NewLog(ltInfo,ClassName,'Start Upgrade to protocol 5 - Old Safeboxhash:'+TCrypto.ToHexaString(FSafeBoxHash)+' calculated: '+TCrypto.ToHexaString(LAux)+' Blocks: '+IntToStr(BlocksCount));
   FCurrentProtocol := CT_PROTOCOL_5;
   FCurrentProtocol := CT_PROTOCOL_5;
   FBufferBlocksHash.Clear;
   FBufferBlocksHash.Clear;
+  FBufferBlocksHash.SafeBoxHashCalcType := sbh_Merkle_Root_Hash;
   for LBlockNumber := 0 to BlocksCount - 1 do begin
   for LBlockNumber := 0 to BlocksCount - 1 do begin
     {$IFDEF uselowmem}
     {$IFDEF uselowmem}
     TBaseType.To32Bytes(CalcBlockHash( Block(LBlockNumber), CT_PROTOCOL_5),PBlockAccount(FBlockAccountsList.Items[LBlockNumber])^.block_hash);
     TBaseType.To32Bytes(CalcBlockHash( Block(LBlockNumber), CT_PROTOCOL_5),PBlockAccount(FBlockAccountsList.Items[LBlockNumber])^.block_hash);

+ 3 - 1
src/core/UConst.pas

@@ -75,6 +75,8 @@ Const
   CT_CompactTarget_Reset_v4: Cardinal = // First compact target of block if using Protocol 4 and RandomHash is Active
   CT_CompactTarget_Reset_v4: Cardinal = // First compact target of block if using Protocol 4 and RandomHash is Active
     {$IFDEF PRODUCTION}$16000000{$ELSE}$08000000{$ENDIF};
     {$IFDEF PRODUCTION}$16000000{$ELSE}$08000000{$ENDIF};
   {$ENDIF}
   {$ENDIF}
+  CT_MinCompactTarget_v5: Cardinal = // Minimum compact target of block if using Protocol 5 or higher
+    {$IFDEF PRODUCTION}$12000000{$ELSE}{$IFDEF TESTNET}$10000000{$ELSE}{$ENDIF}{$ENDIF};
 
 
 
 
   CT_CalcNewTargetBlocksAverage: Cardinal = 100;
   CT_CalcNewTargetBlocksAverage: Cardinal = 100;
@@ -116,7 +118,7 @@ Const
   CT_PROTOCOL_3 = 3;
   CT_PROTOCOL_3 = 3;
   CT_PROTOCOL_4 = 4;
   CT_PROTOCOL_4 = 4;
   CT_PROTOCOL_5 = 5;
   CT_PROTOCOL_5 = 5;
-  CT_BUILD_PROTOCOL = CT_PROTOCOL_4;
+  CT_BUILD_PROTOCOL = CT_PROTOCOL_5;
 
 
   CT_BlockChain_Protocol_Available: Word = 4; // Protocol 4 flag
   CT_BlockChain_Protocol_Available: Word = 4; // Protocol 4 flag
   CT_Protocol_Upgrade_v2_MinBlock = {$IFDEF PRODUCTION}115000{$ELSE}47{$ENDIF};
   CT_Protocol_Upgrade_v2_MinBlock = {$IFDEF PRODUCTION}115000{$ELSE}47{$ENDIF};

+ 1 - 1
src/core/UPCSafeBoxRootHash.pas

@@ -381,7 +381,7 @@ constructor TBytesBuffer32Safebox.Create(ADefaultIncrement: Integer);
 begin
 begin
   FNextLevelBytesBuffer := Nil;
   FNextLevelBytesBuffer := Nil;
   FSafeBoxHashCalcType := sbh_Single_Sha256;
   FSafeBoxHashCalcType := sbh_Single_Sha256;
-  inherited;
+  inherited Create(ADefaultIncrement);
 end;
 end;
 
 
 destructor TBytesBuffer32Safebox.Destroy;
 destructor TBytesBuffer32Safebox.Destroy;