Browse Source

PIP-0009: integrated RandomHash into Core with conditional activation

Herman Schoenfeld 7 years ago
parent
commit
aa1605c969

+ 3 - 0
src/config.inc

@@ -34,6 +34,9 @@
   {$DEFINE PRODUCTION}
   {$DEFINE PRODUCTION}
   {.$DEFINE TESTNET}
   {.$DEFINE TESTNET}
 
 
+  // Used to activate RandomHash in V4 hard-fork
+  {$DEFINE ACTIVATE_RANDOMHASH_V4}
+
   // Account Key Storage is for memory reduction because uses a unified storage to store all public keys and then use only a pointer to them
   // Account Key Storage is for memory reduction because uses a unified storage to store all public keys and then use only a pointer to them
   {$DEFINE useAccountKeyStorage}
   {$DEFINE useAccountKeyStorage}
 
 

+ 4 - 1
src/core/UAccounts.pas

@@ -739,7 +739,10 @@ begin
     ms.Write(operationBlock.fee,4);
     ms.Write(operationBlock.fee,4);
     ms.Write(operationBlock.timestamp,4);
     ms.Write(operationBlock.timestamp,4);
     ms.Write(operationBlock.nonce,4);
     ms.Write(operationBlock.nonce,4);
-    TCrypto.DoDoubleSha256(ms.Memory,ms.Size,PoW);
+    if CT_ACTIVATE_RANDOMHASH_V4 AND (operationBlock.block >= CT_Protocol_Upgrade_v4_MinBlock) then
+      TCrypto.DoRandomHash(ms.Memory,ms.Size,PoW)
+    else
+      TCrypto.DoDoubleSha256(ms.Memory,ms.Size,PoW);
   finally
   finally
     ms.Free;
     ms.Free;
   end;
   end;

+ 4 - 2
src/core/UBlockChain.pas

@@ -901,7 +901,6 @@ Begin
   end;
   end;
 End;
 End;
 
 
-
 function TPCOperationsComp.AddOperations(operations: TOperationsHashTree; var errors: AnsiString): Integer;
 function TPCOperationsComp.AddOperations(operations: TOperationsHashTree; var errors: AnsiString): Integer;
 Var i : Integer;
 Var i : Integer;
   e : AnsiString;
   e : AnsiString;
@@ -939,7 +938,10 @@ begin
   FStreamPoW.WriteBuffer(FDigest_Part3[1],length(FDigest_Part3));
   FStreamPoW.WriteBuffer(FDigest_Part3[1],length(FDigest_Part3));
   FStreamPoW.Write(FOperationBlock.timestamp,4);
   FStreamPoW.Write(FOperationBlock.timestamp,4);
   FStreamPoW.Write(FOperationBlock.nonce,4);
   FStreamPoW.Write(FOperationBlock.nonce,4);
-  TCrypto.DoDoubleSha256(FStreamPoW.Memory,length(FDigest_Part1)+length(FDigest_Part2_Payload)+length(FDigest_Part3)+8,PoW);
+  if CT_ACTIVATE_RANDOMHASH_V4 AND (FOperationBlock.block >= CT_Protocol_Upgrade_v4_MinBlock) then
+    TCrypto.DoRandomHash(FStreamPoW.Memory,length(FDigest_Part1)+length(FDigest_Part2_Payload)+length(FDigest_Part3)+8,PoW)
+  else
+    TCrypto.DoDoubleSha256(FStreamPoW.Memory,length(FDigest_Part1)+length(FDigest_Part2_Payload)+length(FDigest_Part3)+8,PoW);
 end;
 end;
 
 
 procedure TPCOperationsComp.Calc_Digest_Parts;
 procedure TPCOperationsComp.Calc_Digest_Parts;

+ 2 - 0
src/core/UConst.pas

@@ -178,6 +178,8 @@ Const
   CT_MOLINA  = 1;
   CT_MOLINA  = 1;
   CT_MOLINA_DECIMAL = {$IFDEF FPC}Real(CT_MOLINA/1000.0);{$ELSE}0.0001;{$ENDIF}
   CT_MOLINA_DECIMAL = {$IFDEF FPC}Real(CT_MOLINA/1000.0);{$ELSE}0.0001;{$ENDIF}
 
 
+  CT_ACTIVATE_RANDOMHASH_V4 = {$IFDEF ACTIVATE_RANDOMHASH_V4}True{$ELSE}False{$ENDIF};
+
   // App Params
   // App Params
   CT_PARAM_GridAccountsStream = 'GridAccountsStreamV2';
   CT_PARAM_GridAccountsStream = 'GridAccountsStreamV2';
   CT_PARAM_GridAccountsPos = 'GridAccountsPos';
   CT_PARAM_GridAccountsPos = 'GridAccountsPos';

+ 16 - 1
src/core/UCrypto.pas

@@ -77,6 +77,7 @@ Type
     class procedure DoSha256(const TheMessage : AnsiString; out ResultSha256 : TRawBytes);  overload;
     class procedure DoSha256(const TheMessage : AnsiString; out ResultSha256 : TRawBytes);  overload;
     class function DoDoubleSha256(const TheMessage : AnsiString) : TRawBytes; overload;
     class function DoDoubleSha256(const TheMessage : AnsiString) : TRawBytes; overload;
     class procedure DoDoubleSha256(p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes); overload;
     class procedure DoDoubleSha256(p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes); overload;
+    class procedure DoRandomHash(p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes);
     class function DoRipeMD160_HEXASTRING(const TheMessage : AnsiString) : TRawBytes; overload;
     class function DoRipeMD160_HEXASTRING(const TheMessage : AnsiString) : TRawBytes; overload;
     class function DoRipeMD160AsRaw(p : PAnsiChar; plength : Cardinal) : TRawBytes; overload;
     class function DoRipeMD160AsRaw(p : PAnsiChar; plength : Cardinal) : TRawBytes; overload;
     class function DoRipeMD160AsRaw(const TheMessage : AnsiString) : TRawBytes; overload;
     class function DoRipeMD160AsRaw(const TheMessage : AnsiString) : TRawBytes; overload;
@@ -137,7 +138,7 @@ Const
 implementation
 implementation
 
 
 uses
 uses
-  ULog, UConst, UAccounts;
+  ULog, UConst, UAccounts, URandomHash;
 
 
 Var _initialized : Boolean = false;
 Var _initialized : Boolean = false;
 
 
@@ -642,6 +643,20 @@ begin
     end;
     end;
 end;
 end;
 
 
+{ New at Build 4.0.0 }
+
+class procedure TCrypto.DoRandomHash(p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes);
+var
+  LInput : TBytes;
+  LResult : TBytes;
+begin
+  if Length(ResultSha256) <> 32 then SetLength(ResultSha256, 32);
+  SetLength(LInput, plength);
+  Move(p^, LInput[0], plength);
+  LResult := TRandomHash.Compute(LInput);
+  Move(LResult[0], ResultSha256[1], 32);
+end;
+
 { TBigNum }
 { TBigNum }
 
 
 function TBigNum.Add(BN: TBigNum): TBigNum;
 function TBigNum.Add(BN: TBigNum): TBigNum;

+ 1 - 1
src/pascalcoin_wallet_classic.dproj

@@ -16,7 +16,7 @@
     </PropertyGroup>
     </PropertyGroup>
     <PropertyGroup Condition="'$(Base)'!=''">
     <PropertyGroup Condition="'$(Base)'!=''">
         <DCC_DcuOutput>.\lib</DCC_DcuOutput>
         <DCC_DcuOutput>.\lib</DCC_DcuOutput>
-        <DCC_UnitSearchPath>.\core;.\gui-classic;.\libraries\pascalcoin;.\libraries\synapse;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
+        <DCC_UnitSearchPath>.\core;.\gui-classic;.\libraries\pascalcoin;.\libraries\synapse;.\libraries\generics.collections;.\libraries\hashlib4pascal;.\libraries\sphere10;$(DCC_UnitSearchPath)</DCC_UnitSearchPath>
         <DCC_DependencyCheckOutputName>pascalcoin_wallet_classic.exe</DCC_DependencyCheckOutputName>
         <DCC_DependencyCheckOutputName>pascalcoin_wallet_classic.exe</DCC_DependencyCheckOutputName>
         <DCC_ImageBase>00400000</DCC_ImageBase>
         <DCC_ImageBase>00400000</DCC_ImageBase>
         <DCC_Platform>x86</DCC_Platform>
         <DCC_Platform>x86</DCC_Platform>