浏览代码

Fast propagation optimizations

Pascal Coin 3 年之前
父节点
当前提交
e00607f12a
共有 2 个文件被更改,包括 21 次插入5 次删除
  1. 5 1
      src/core/UBlockChain.pas
  2. 16 4
      src/core/UNetProtocol.pas

+ 5 - 1
src/core/UBlockChain.pas

@@ -337,6 +337,7 @@ Type
     FTotalAmount : Int64;
     FTotalFee : Int64;
     FMax0feeOperationsBySigner : Integer;
+    FHasOpRecoverOperations : Boolean;
     function InternalCanAddOperationToHashTree(lockedThreadList : TList<Pointer>; op : TPCOperation) : Boolean;
     function InternalAddOperationToHashTree(list : TList<Pointer>; op : TPCOperation; CalcNewHashTree : Boolean) : Boolean;
     Function FindOrderedByOpReference(lockedThreadList : TList<Pointer>; const Value: TOpReference; var Index: Integer): Boolean;
@@ -367,7 +368,7 @@ Type
     Property OnChanged : TNotifyEvent read FOnChanged write FOnChanged;
     Property Max0feeOperationsBySigner : Integer Read FMax0feeOperationsBySigner write SetMax0feeOperationsBySigner;
     procedure MarkVerifiedECDSASignatures(operationsHashTreeToMark : TOperationsHashTree);
-
+    Property HasOpRecoverOperations : Boolean read FHasOpRecoverOperations;
     // Will add all operations of the HashTree to then end of AList without removing previous objects
     function GetOperationsList(AList : TList<TPCOperation>; AAddOnlyOperationsWithoutNotVerifiedSignature : Boolean) : Integer;
   End;
@@ -2332,6 +2333,7 @@ begin
       FListOrderedByAccountsData.Clear;
       FListOrderedByOpReference.Clear;
       FHashTree:=Nil;
+      FHasOpRecoverOperations := False;
     End;
     If Assigned(FOnChanged) then FOnChanged(Self);
   finally
@@ -2387,6 +2389,7 @@ begin
   FHashTree := Nil;
   FMax0feeOperationsBySigner := -1; // Unlimited by default
   FHashTreeOperations := TPCThreadList<Pointer>.Create('TOperationsHashTree_HashTreeOperations');
+  FHasOpRecoverOperations := False;
 end;
 
 procedure TOperationsHashTree.Delete(index: Integer);
@@ -2617,6 +2620,7 @@ begin
     Result := False;
     Exit;
   end else Result := True; // Will add:
+    if (op is TOpRecoverFounds) then FHasOpRecoverOperations := True;
     New(P);
     if Not _PCOperationsStorage.FindPCOperationAndIncCounterIfFound(op) then begin
       msCopy := TMemoryStream.Create;

+ 16 - 4
src/core/UNetProtocol.pas

@@ -512,7 +512,7 @@ implementation
 
 uses
   UConst, ULog, UNode, UTime, UPCEncryption, UChunk,
-  UPCOperationsBlockValidator, UPCOperationsSignatureValidator,
+  UPCOperationsBlockValidator, UPCOperationsSignatureValidator, UOpTransaction,
   UPCTemporalFileStream;
 
 Const
@@ -3925,8 +3925,13 @@ var operationsComp : TPCOperationsComp;
     If Not TAccountComp.EqualOperationBlocks(operationsComp.OperationBlock,original_OperationBlock) then begin
       // This can happen when a OpReference in my MEMPOOL is different to an OpReference in the miner, causing different OperationsHash value
       // This means a possible double spend found
-      TLog.NewLog(lterror,ClassName,Format('Constructed a distinct FAST PROPAGATION block with my mempool operations. Received: %s Constructed: %s',
+      if Not operationsComp.OperationsHashTree.HasOpRecoverOperations then begin
+        TLog.NewLog(lterror,ClassName,Format('Constructed a distinct FAST PROPAGATION block with my mempool operations. Received: %s Constructed: %s',
         [TPCOperationsComp.OperationBlockToText(original_OperationBlock),TPCOperationsComp.OperationBlockToText(operationsComp.OperationBlock)]));
+      end else begin
+        TLog.NewLog(ltdebug,ClassName,Format('Constructed a distinct FAST PROPAGATION block with my mempool operations. Received: %s Constructed: %s',
+        [TPCOperationsComp.OperationBlockToText(original_OperationBlock),TPCOperationsComp.OperationBlockToText(operationsComp.OperationBlock)]));
+      end;
       if Not TPCSafeBox.IsValidOperationBlock(original_OperationBlock,errors) then begin
         // This means a scammer!
         DoDisconnect := True;
@@ -4637,8 +4642,12 @@ begin
     data := TMemoryStream.Create;
     try
       request_id := TNetData.NetData.NewRequestId;
-      // Will send a FAST PROPAGATION BLOCK as described at PIP-0015
-      netOp := CT_NetOp_NewBlock_Fast_Propagation;
+      if (NewBlock.OperationsHashTree.HasOpRecoverOperations) then begin
+        netOp := CT_NetOp_NewBlock;
+      end else begin
+        // Will send a FAST PROPAGATION BLOCK as described at PIP-0015
+        netOp := CT_NetOp_NewBlock_Fast_Propagation;
+      end;
       NewBlock.SaveBlockToStream(netOp = CT_NetOp_NewBlock_Fast_Propagation,data); // Will save all only if not FAST PROPAGATION
       // Send Aggregated Hashsrate based on network protocol available version
       if FNetProtocolVersion.protocol_available>=CT_MIN_NetProtocol_Use_Aggregated_Hashrate then begin
@@ -4658,6 +4667,9 @@ begin
             data.Write(opRef,SizeOf(opRef));
           end;
         end;
+        TLog.NewLog(ltdebug,ClassName,Format('Sending NEW FAST PROPAGATION BLOCK %d with %d operations in %d bytes to %s',[NewBlock.OperationBlock.block,c,data.Size,ClientRemoteAddr]));
+      end else begin
+        TLog.NewLog(ltdebug,ClassName,Format('Sending NEW BLOCK %d with %d operations in %d bytes to %s',[NewBlock.OperationBlock.block,NewBlock.Count,data.Size,ClientRemoteAddr]));
       end;
       Send(ntp_autosend,netOp,0,request_id,data);
     finally