Browse Source

improvements

some logs updated and improved blocks/operations propagation
PascalCoin 6 years ago
parent
commit
8997079121
3 changed files with 41 additions and 27 deletions
  1. 8 6
      src/core/UBlockChain.pas
  2. 3 3
      src/core/UNetProtocol.pas
  3. 30 18
      src/core/UNode.pas

+ 8 - 6
src/core/UBlockChain.pas

@@ -2317,7 +2317,7 @@ begin
 end;
 end;
 
 
 procedure TOperationsHashTree.MarkVerifiedECDSASignatures(operationsHashTreeToMark: TOperationsHashTree);
 procedure TOperationsHashTree.MarkVerifiedECDSASignatures(operationsHashTreeToMark: TOperationsHashTree);
-var i, iPosInMyList, nMarkedAsGood : Integer;
+var i, iPosInMyList, nMarkedAsGood, nAlreadyMarked : Integer;
   opToMark, opInMyList : TPCOperation;
   opToMark, opInMyList : TPCOperation;
   myList, listToMark : TList;
   myList, listToMark : TList;
 begin
 begin
@@ -2325,6 +2325,7 @@ begin
   // Will search each "operationsHashTreeToMark" operation on my current list. If found, will set same FHasValidSignature in order to mark operation in "operationsHashTreeToMark" as verified
   // Will search each "operationsHashTreeToMark" operation on my current list. If found, will set same FHasValidSignature in order to mark operation in "operationsHashTreeToMark" as verified
   If Self=operationsHashTreeToMark then Exit;
   If Self=operationsHashTreeToMark then Exit;
   nMarkedAsGood := 0;
   nMarkedAsGood := 0;
+  nAlreadyMarked := 0;
   myList := FHashTreeOperations.LockList;
   myList := FHashTreeOperations.LockList;
   try
   try
     if myList.Count<=0 then Exit; // Nothing to search...
     if myList.Count<=0 then Exit; // Nothing to search...
@@ -2339,15 +2340,16 @@ begin
           if (iPosInMyList>=0) then begin
           if (iPosInMyList>=0) then begin
             opInMyList := POperationHashTreeReg(myList[iPosInMyList])^.Op;
             opInMyList := POperationHashTreeReg(myList[iPosInMyList])^.Op;
             if (opInMyList.FHasValidSignature) then begin
             if (opInMyList.FHasValidSignature) then begin
-              opToMark.FHasValidSignature:=True;
-              inc(nMarkedAsGood);
+              if (opToMark.FHasValidSignature) then inc(nAlreadyMarked)
+              else begin
+                opToMark.FHasValidSignature:=True;
+                inc(nMarkedAsGood);
+              end;
             end;
             end;
           end;
           end;
         end;
         end;
       end;
       end;
-      if nMarkedAsGood>0 then begin
-        TLog.NewLog(ltdebug,ClassName,Format('Marked %d/%d operations as ValidSignature from MemPool with %d operations',[nMarkedAsGood,listToMark.Count,myList.Count]));
-      end;
+      TLog.NewLog(ltdebug,ClassName,Format('Marked %d/%d operations as ValidSignature (%d before) from MemPool with %d operations',[nMarkedAsGood,listToMark.Count,nAlreadyMarked,myList.Count]));
     finally
     finally
       operationsHashTreeToMark.FHashTreeOperations.UnlockList;
       operationsHashTreeToMark.FHashTreeOperations.UnlockList;
     end;
     end;

+ 3 - 3
src/core/UNetProtocol.pas

@@ -2435,7 +2435,7 @@ end;
 destructor TNetConnection.Destroy;
 destructor TNetConnection.Destroy;
 begin
 begin
   Try
   Try
-    TLog.NewLog(ltdebug,ClassName,'Destroying '+Classname+' '+IntToHex(PtrInt(Self),8));
+    {$IFDEF HIGHLOG}TLog.NewLog(ltdebug,ClassName,'Destroying '+Classname+' '+IntToHex(PtrInt(Self),8));{$ENDIF}
 
 
     Connected := false;
     Connected := false;
 
 
@@ -4126,7 +4126,7 @@ begin
         finally
         finally
           data.Free;
           data.Free;
         end;
         end;
-      end else TLog.NewLog(ltdebug,ClassName,Format('Not sending any operations to %s (inProc:%d, Received:%d, Sent:%d)',[ClientRemoteAddr,nOpsToSend,FBufferReceivedOperationsHash.Count,FBufferToSendOperations.OperationsCount]));
+      end{$IFDEF HIGHLOG} else TLog.NewLog(ltdebug,ClassName,Format('Not sending any operations to %s (inProc:%d, Received:%d, Sent:%d)',[ClientRemoteAddr,nOpsToSend,FBufferReceivedOperationsHash.Count,FBufferToSendOperations.OperationsCount])){$ENDIF};
     finally
     finally
       FBufferLock.Release;
       FBufferLock.Release;
     end;
     end;
@@ -4396,7 +4396,7 @@ end;
 
 
 destructor TNetClient.Destroy;
 destructor TNetClient.Destroy;
 begin
 begin
-  TLog.NewLog(ltdebug,Classname,'Starting TNetClient.Destroy');
+  {$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,'Starting TNetClient.Destroy');{$ENDIF}
   FNetClientThread.OnTerminate := Nil;
   FNetClientThread.OnTerminate := Nil;
   if Not FNetClientThread.Terminated then begin
   if Not FNetClientThread.Terminated then begin
     FNetClientThread.Terminate;
     FNetClientThread.Terminate;

+ 30 - 18
src/core/UNode.pas

@@ -200,7 +200,7 @@ function TNode.AddNewBlockChain(SenderConnection: TNetConnection; NewBlockOperat
   var newBlockAccount: TBlockAccount; var errors: AnsiString): Boolean;
   var newBlockAccount: TBlockAccount; var errors: AnsiString): Boolean;
 Var i,j,maxResend : Integer;
 Var i,j,maxResend : Integer;
   nc : TNetConnection;
   nc : TNetConnection;
-  s : String;
+  s,sClientRemoteAddr : String;
   OpBlock : TOperationBlock;
   OpBlock : TOperationBlock;
   opsht : TOperationsHashTree;
   opsht : TOperationsHashTree;
   minBlockResend : Cardinal;
   minBlockResend : Cardinal;
@@ -208,9 +208,11 @@ Var i,j,maxResend : Integer;
 begin
 begin
   Result := false;
   Result := false;
   errors := '';
   errors := '';
+  if Assigned(SenderConnection) then sClientRemoteAddr := SenderConnection.ClientRemoteAddr
+  else sClientRemoteAddr:='(SELF)';
   if FDisabledsNewBlocksCount>0 then begin
   if FDisabledsNewBlocksCount>0 then begin
     TLog.NewLog(lterror,Classname,Format('Cannot Add new BlockChain due is adding disabled - Connection:%s NewBlock:%s',[
     TLog.NewLog(lterror,Classname,Format('Cannot Add new BlockChain due is adding disabled - Connection:%s NewBlock:%s',[
-    Inttohex(PtrInt(SenderConnection),8),TPCOperationsComp.OperationBlockToText(NewBlockOperations.OperationBlock)]));
+    sClientRemoteAddr,TPCOperationsComp.OperationBlockToText(NewBlockOperations.OperationBlock)]));
     errors := 'Adding blocks is disabled';
     errors := 'Adding blocks is disabled';
     exit;
     exit;
   end;
   end;
@@ -221,9 +223,9 @@ begin
       exit;
       exit;
     end;
     end;
     OpBlock := NewBlockOperations.OperationBlock;
     OpBlock := NewBlockOperations.OperationBlock;
-    TLog.NewLog(ltdebug,Classname,Format('AddNewBlockChain Connection:%s NewBlock:%s',[
-      Inttohex(PtrInt(SenderConnection),8),TPCOperationsComp.OperationBlockToText(OpBlock)]));
-    If Not TPCThread.TryProtectEnterCriticalSection(Self,2000,FLockNodeOperations) then begin
+    TLog.NewLog(ltdebug,Classname,Format('Starting AddNewBlockChain %d Operations %d from %s NewBlock:%s',[
+      OpBlock.block,NewBlockOperations.Count,sClientRemoteAddr,TPCOperationsComp.OperationBlockToText(OpBlock)]));
+    If Not TPCThread.TryProtectEnterCriticalSection(Self,5000,FLockNodeOperations) then begin
       If NewBlockOperations.OperationBlock.block<>Bank.BlocksCount then exit;
       If NewBlockOperations.OperationBlock.block<>Bank.BlocksCount then exit;
       s := 'Cannot AddNewBlockChain due blocking lock operations node';
       s := 'Cannot AddNewBlockChain due blocking lock operations node';
       TLog.NewLog(lterror,Classname,s);
       TLog.NewLog(lterror,Classname,s);
@@ -241,7 +243,7 @@ begin
       Result := Bank.AddNewBlockChainBlock(NewBlockOperations,TNetData.NetData.NetworkAdjustedTime.GetMaxAllowedTimestampForNewBlock,newBlockAccount,errors);
       Result := Bank.AddNewBlockChainBlock(NewBlockOperations,TNetData.NetData.NetworkAdjustedTime.GetMaxAllowedTimestampForNewBlock,newBlockAccount,errors);
       if Result then begin
       if Result then begin
         if Assigned(SenderConnection) then begin
         if Assigned(SenderConnection) then begin
-          FNodeLog.NotifyNewLog(ltupdate,SenderConnection.ClassName,Format(';%d;%s;%s;;%d;%d;%d;%s',[OpBlock.block,SenderConnection.ClientRemoteAddr,OpBlock.block_payload,
+          FNodeLog.NotifyNewLog(ltupdate,SenderConnection.ClassName,Format(';%d;%s;%s;;%d;%d;%d;%s',[OpBlock.block,sClientRemoteAddr,OpBlock.block_payload,
             OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
             OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
         end else begin
         end else begin
           FNodeLog.NotifyNewLog(ltupdate,ClassName,Format(';%d;%s;%s;;%d;%d;%d;%s',[OpBlock.block,'NIL',OpBlock.block_payload,
           FNodeLog.NotifyNewLog(ltupdate,ClassName,Format(';%d;%s;%s;;%d;%d;%d;%s',[OpBlock.block,'NIL',OpBlock.block_payload,
@@ -249,7 +251,7 @@ begin
         end;
         end;
       end else begin
       end else begin
         if Assigned(SenderConnection) then begin
         if Assigned(SenderConnection) then begin
-          FNodeLog.NotifyNewLog(lterror,SenderConnection.ClassName,Format(';%d;%s;%s;%s;%d;%d;%d;%s',[OpBlock.block,SenderConnection.ClientRemoteAddr,OpBlock.block_payload,errors,
+          FNodeLog.NotifyNewLog(lterror,SenderConnection.ClassName,Format(';%d;%s;%s;%s;%d;%d;%d;%s',[OpBlock.block,sClientRemoteAddr,OpBlock.block_payload,errors,
             OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
             OpBlock.timestamp,UnivDateTimeToUnix(DateTime2UnivDateTime(Now)),UnivDateTimeToUnix(DateTime2UnivDateTime(Now)) - OpBlock.timestamp,IntToHex(OpBlock.compact_target,8)]));
         end else begin
         end else begin
           FNodeLog.NotifyNewLog(lterror,ClassName,Format(';%d;%s;%s;%s;%d;%d;%d;%s',[OpBlock.block,'NIL',OpBlock.block_payload,errors,
           FNodeLog.NotifyNewLog(lterror,ClassName,Format(';%d;%s;%s;%s;%d;%d;%d;%s',[OpBlock.block,'NIL',OpBlock.block_payload,errors,
@@ -325,8 +327,8 @@ begin
       end;
       end;
     finally
     finally
       FLockNodeOperations.Release;
       FLockNodeOperations.Release;
-      TLog.NewLog(ltdebug,Classname,Format('Finalizing AddNewBlockChain Connection:%s NewBlock:%s',[
-        Inttohex(PtrInt(SenderConnection),8),TPCOperationsComp.OperationBlockToText(OpBlock) ]));
+      TLog.NewLog(ltdebug,Classname,Format('Finalizing AddNewBlockChain %d Operations %d from %s NewBlock:%s',[
+        OpBlock.block,NewBlockOperations.Count,sClientRemoteAddr,TPCOperationsComp.OperationBlockToText(OpBlock)]));
     End;
     End;
   finally
   finally
     NewBlockOperations.Unlock;
     NewBlockOperations.Unlock;
@@ -384,7 +386,7 @@ function TNode.AddOperations(SenderConnection : TNetConnection; OperationsHashTr
   end;
   end;
   {$ENDIF}
   {$ENDIF}
 Var
 Var
-  i,j : Integer;
+  i,j,nSpam,nError,nRepeated : Integer;
   valids_operations : TOperationsHashTree;
   valids_operations : TOperationsHashTree;
   nc : TNetConnection;
   nc : TNetConnection;
   e : AnsiString;
   e : AnsiString;
@@ -401,11 +403,14 @@ begin
     exit;
     exit;
   end;
   end;
   Result := 0;
   Result := 0;
+  nSpam := 0;
+  nRepeated := 0;
+  nError := 0;
   errors := '';
   errors := '';
   valids_operations := TOperationsHashTree.Create;
   valids_operations := TOperationsHashTree.Create;
   try
   try
-    TLog.NewLog(ltdebug,Classname,Format('AddOperations Connection:%s Operations:%d',[
-      Inttohex(PtrInt(SenderConnection),8),OperationsHashTree.OperationsCount]));
+    {$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperations Connection:%s Operations:%d',[
+      Inttohex(PtrInt(SenderConnection),8),OperationsHashTree.OperationsCount]));{$ENDIF}
     if Not TPCThread.TryProtectEnterCriticalSection(Self,4000,FLockNodeOperations) then begin
     if Not TPCThread.TryProtectEnterCriticalSection(Self,4000,FLockNodeOperations) then begin
       s := 'Cannot AddOperations due blocking lock operations node';
       s := 'Cannot AddOperations due blocking lock operations node';
       TLog.NewLog(lterror,Classname,s);
       TLog.NewLog(lterror,Classname,s);
@@ -421,10 +426,11 @@ begin
           // Protocol 2 limitation: In order to prevent spam of operations without Fee, will protect it
           // Protocol 2 limitation: In order to prevent spam of operations without Fee, will protect it
           If (ActOp.OperationFee=0) And (Bank.SafeBox.CurrentProtocol>=CT_PROTOCOL_2) And
           If (ActOp.OperationFee=0) And (Bank.SafeBox.CurrentProtocol>=CT_PROTOCOL_2) And
              (FOperations.OperationsHashTree.CountOperationsBySameSignerWithoutFee(ActOp.SignerAccount)>=CT_MaxAccountOperationsPerBlockWithoutFee) then begin
              (FOperations.OperationsHashTree.CountOperationsBySameSignerWithoutFee(ActOp.SignerAccount)>=CT_MaxAccountOperationsPerBlockWithoutFee) then begin
+            inc(nSpam);
             e := Format('Account %s zero fee operations per block limit:%d',[TAccountComp.AccountNumberToAccountTxtNumber(ActOp.SignerAccount),CT_MaxAccountOperationsPerBlockWithoutFee]);
             e := Format('Account %s zero fee operations per block limit:%d',[TAccountComp.AccountNumberToAccountTxtNumber(ActOp.SignerAccount),CT_MaxAccountOperationsPerBlockWithoutFee]);
             if (errors<>'') then errors := errors+' ';
             if (errors<>'') then errors := errors+' ';
             errors := errors+'Op '+IntToStr(j+1)+'/'+IntToStr(OperationsHashTree.OperationsCount)+':'+e;
             errors := errors+'Op '+IntToStr(j+1)+'/'+IntToStr(OperationsHashTree.OperationsCount)+':'+e;
-            TLog.NewLog(ltdebug,Classname,Format('AddOperation invalid/duplicated %d/%d: %s  - Error:%s',
+            TLog.NewLog(ltdebug,Classname,Format('AddOperation spam %d/%d: %s  - Error:%s',
               [(j+1),OperationsHashTree.OperationsCount,ActOp.ToString,e]));
               [(j+1),OperationsHashTree.OperationsCount,ActOp.ToString,e]));
             if Assigned(OperationsResult) then begin
             if Assigned(OperationsResult) then begin
               TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
               TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
@@ -447,10 +453,13 @@ begin
                 OperationsResult.Add(OPR);
                 OperationsResult.Add(OPR);
               end;
               end;
             end else begin
             end else begin
+              inc(nError);
               if (errors<>'') then errors := errors+' ';
               if (errors<>'') then errors := errors+' ';
               errors := errors+'Op '+IntToStr(j+1)+'/'+IntToStr(OperationsHashTree.OperationsCount)+':'+e;
               errors := errors+'Op '+IntToStr(j+1)+'/'+IntToStr(OperationsHashTree.OperationsCount)+':'+e;
+              {$IFDEF HIGHLOG}
               TLog.NewLog(ltdebug,Classname,Format('AddOperation invalid/duplicated %d/%d: %s  - Error:%s',
               TLog.NewLog(ltdebug,Classname,Format('AddOperation invalid/duplicated %d/%d: %s  - Error:%s',
                 [(j+1),OperationsHashTree.OperationsCount,ActOp.ToString,e]));
                 [(j+1),OperationsHashTree.OperationsCount,ActOp.ToString,e]));
+              {$ENDIF}
               if Assigned(OperationsResult) then begin
               if Assigned(OperationsResult) then begin
                 TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
                 TPCOperation.OperationToOperationResume(0,ActOp,True,ActOp.SignerAccount,OPR);
                 OPR.valid := false;
                 OPR.valid := false;
@@ -475,6 +484,7 @@ begin
             end;
             end;
           end;
           end;
         end else begin
         end else begin
+          inc(nRepeated);
           e := Format('AddOperation made before %d/%d: %s',[(j+1),OperationsHashTree.OperationsCount,ActOp.ToString]);
           e := Format('AddOperation made before %d/%d: %s',[(j+1),OperationsHashTree.OperationsCount,ActOp.ToString]);
           if (errors<>'') then errors := errors+' ';
           if (errors<>'') then errors := errors+' ';
           errors := errors + e;
           errors := errors + e;
@@ -496,8 +506,10 @@ begin
     finally
     finally
       FLockNodeOperations.Release;
       FLockNodeOperations.Release;
       if Result<>0 then begin
       if Result<>0 then begin
-        TLog.NewLog(ltdebug,Classname,Format('Finalizing AddOperations Connection:%s Operations:%d valids:%d',[
-          Inttohex(PtrInt(SenderConnection),8),OperationsHashTree.OperationsCount,Result ]));
+        if Assigned(SenderConnection) then begin
+          s := SenderConnection.ClientRemoteAddr;
+        end else s := '(SELF)';
+        TLog.NewLog(ltdebug,Classname,Format('Finalizing AddOperations from %s Operations:%d valids:%d spam:%d invalids:%d repeated:%d',[s,OperationsHashTree.OperationsCount,Result,nSpam,nError,nRepeated]));
       end;
       end;
     end;
     end;
     if Result=0 then exit;
     if Result=0 then exit;
@@ -1325,7 +1337,7 @@ end;
 procedure TThreadNodeNotifyNewBlock.BCExecute;
 procedure TThreadNodeNotifyNewBlock.BCExecute;
 begin
 begin
   DebugStep := 'Locking';
   DebugStep := 'Locking';
-  if TNetData.NetData.ConnectionLock(Self,FNetConnection,500) then begin
+  if TNetData.NetData.ConnectionLock(Self,FNetConnection,5000) then begin
     try
     try
       DebugStep := 'Checking connected';
       DebugStep := 'Checking connected';
       if Not FNetconnection.Connected then exit;
       if Not FNetconnection.Connected then exit;
@@ -1371,8 +1383,8 @@ end;
 
 
 procedure TThreadNodeNotifyOperations.BCExecute;
 procedure TThreadNodeNotifyOperations.BCExecute;
 begin
 begin
-  Sleep(Random(5000)); // Delay 0..5 seconds to allow receive data and don't send if not necessary
-  if TNetData.NetData.ConnectionLock(Self, FNetConnection, 500) then begin
+  Sleep(Random(3000)); // Delay 0..3 seconds to allow receive data and don't send if not necessary
+  if TNetData.NetData.ConnectionLock(Self, FNetConnection, 5000) then begin
     try
     try
       if Not FNetconnection.Connected then exit;
       if Not FNetconnection.Connected then exit;
       FNetConnection.Send_AddOperations(Nil);
       FNetConnection.Send_AddOperations(Nil);