浏览代码

Core Mining: integrate TRandomHashFast

Herman Schoenfeld 7 年之前
父节点
当前提交
9456e7ce4c
共有 2 个文件被更改,包括 29 次插入10 次删除
  1. 16 3
      src/core/UCrypto.pas
  2. 13 7
      src/core/UPoolMinerThreads.pas

+ 16 - 3
src/core/UCrypto.pas

@@ -25,7 +25,7 @@ unit UCrypto;
 interface
 
 uses
-  Classes, SysUtils, UOpenSSL, UOpenSSLdef;
+  Classes, SysUtils, UOpenSSL, UOpenSSLdef, URandomHash;
 
 Type
   ECryptoException = Class(Exception);
@@ -82,6 +82,7 @@ Type
     class procedure DoDoubleSha256(p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes); overload;
     class function DoRandomHash(const TheMessage : AnsiString) : TRawBytes; overload;
     class procedure DoRandomHash(p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes); overload;
+    class procedure DoRandomHash(AFastHasher : TRandomHashFast; p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes); overload;
     class function DoRipeMD160_HEXASTRING(const TheMessage : AnsiString) : TRawBytes; overload;
     class function DoRipeMD160AsRaw(p : PAnsiChar; plength : Cardinal) : TRawBytes; overload;
     class function DoRipeMD160AsRaw(const TheMessage : AnsiString) : TRawBytes; overload;
@@ -142,7 +143,7 @@ Const
 implementation
 
 uses
-  ULog, UConst, UAccounts, URandomHash;
+  ULog, UConst, UAccounts;
 
 Var _initialized : Boolean = false;
 
@@ -662,7 +663,19 @@ begin
   if Length(ResultSha256) <> 32 then SetLength(ResultSha256, 32);
   SetLength(LInput, plength);
   Move(p^, LInput[0], plength);
-  LResult := TRandomHash.Compute(LInput);
+  LResult := TRandomHashFast.Compute(LInput);
+  Move(LResult[0], ResultSha256[1], 32);
+end;
+
+class procedure TCrypto.DoRandomHash(AFastHasher : TRandomHashFast; 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 := AFastHasher.Hash(LInput);
   Move(LResult[0], ResultSha256[1], 32);
 end;
 

+ 13 - 7
src/core/UPoolMinerThreads.pas

@@ -43,6 +43,7 @@ Const
 Type
 
   TCustomMinerDeviceThread = Class;
+
   TCustomMinerDeviceThreadClass = Class of TCustomMinerDeviceThread;
 
   TOnFoundNonce = Procedure(Sender : TCustomMinerDeviceThread; Timestamp, nOnce : Cardinal) of object;
@@ -81,6 +82,7 @@ Type
   End;
 
   { TCustomMinerDeviceThread }
+
   TCustomMinerDeviceThread = Class(TPCThread)
   private
     FIsMining: Boolean;
@@ -117,8 +119,6 @@ Type
     Property PoolMinerThread : TPoolMinerThread read FPoolMinerThread;
   end;
 
-
-
   { TCPUDeviceThread }
 
   TCPUDeviceThread = Class(TCustomMinerDeviceThread)
@@ -164,7 +164,7 @@ Type
 
 implementation
 
-uses UConst, UTime, UJSONFunctions, UNode, UNetProtocol;
+uses UConst, UTime, UJSONFunctions, UNode, UNetProtocol, UMemory;
 
 { TPoolMinerThread }
 
@@ -739,11 +739,14 @@ Var
   AuxStats : TMinerStats;
   dstep : Integer;
   LUseRandomHash : boolean;
+  LRandomHasher : TRandomHashFast;
+  LDisposables : TDisposables;
 begin
   DebugStep := '----------';
   AuxStats := CT_TMinerStats_NULL;
   nonce := 0;
   dstep := 0;
+  LRandomHasher := LDisposables.AddObject( TRandomHashFast.Create ) as TRandomHashFast;
   Try
     while (Not Terminated) And (Not FCPUDeviceThread.Terminated) do begin
       Try
@@ -776,9 +779,10 @@ begin
               for i := 1 to roundsToDo do begin
                 FDigestStreamMsg.Position := FDigestStreamMsg.Size - 4;
                 FDigestStreamMsg.Write(nonce,4);
-                if LUseRandomHash then
-                  TCrypto.DoRandomHash(FDigestStreamMsg.Memory,FDigestStreamMsg.Size,resultPoW)
-                else
+                if LUseRandomHash then begin
+                  // Note if i > 1 then FDigestStreamMsg.Memory == LHasher.NextHeader (needs to be for CPU optimization to work)
+                  TCrypto.DoRandomHash(LRandomHasher,FDigestStreamMsg.Memory,FDigestStreamMsg.Size,resultPoW);
+                end else
                   TCrypto.DoDoubleSha256(FDigestStreamMsg.Memory,FDigestStreamMsg.Size,resultPoW);
                 if resultPoW < FCurrentMinerValuesForWork.target_pow then begin
                   if (Terminated) Or (FCPUDeviceThread.Terminated) then exit;
@@ -794,7 +798,9 @@ begin
                   end;
                   dstep := 8;
                 end;
-                if (nonce)<FMaxNOnce then inc(nonce) else nonce := FMinNOnce;
+                if LUseRandomHash then
+                  nonce := LRandomHasher.NextNonce
+                else if (nonce)<FMaxNOnce then inc(nonce) else nonce := FMinNOnce;
               end;
               finalHashingTC:=TPlatform.GetTickCount;
             end else begin