PascalCoin 7 years ago
parent
commit
3346db7a22
3 changed files with 37 additions and 9 deletions
  1. 3 0
      README.md
  2. 12 0
      src/core/UNode.pas
  3. 22 9
      src/core/URPC.pas

+ 3 - 0
README.md

@@ -167,6 +167,9 @@ Also, consider a donation at PascalCoin development account: "0-10"
       - "operations" : Integer
       - "amount" : PASCURRENCY
       - "fee" : PASCURRENCY  
+  - Updated method "getpendings" : Added params "start" (0..N) default=0 and "max" default=100 (0 = ALL)
+    - Also fixed bug #86 : https://github.com/PascalCoin/PascalCoin/issues/86
+  - New method "getpendingscount" : Returns pending operations count
 - Daemon:
   - Allow to force max block read from Blockchain when started using "-b MAX_BLOCK_NUMBER" param. Example "nohup ./pascalcoin_daemon -r -b 12345 &"
 - Protections against invalid nodes (scammers):

+ 12 - 0
src/core/UNode.pas

@@ -96,6 +96,8 @@ Type
     Procedure EnableNewBlocks;
     Property NodeLogFilename : AnsiString read GetNodeLogFilename write SetNodeLogFilename;
     Property OperationSequenceLock : TPCCriticalSection read FOperationSequenceLock;
+    function TryLockNode(MaxWaitMilliseconds : Cardinal) : Boolean;
+    procedure UnlockNode;
   End;
 
   TNodeNotifyEvents = Class;
@@ -621,6 +623,16 @@ begin
   dec(FDisabledsNewBlocksCount);
 end;
 
+function TNode.TryLockNode(MaxWaitMilliseconds: Cardinal): Boolean;
+begin
+  Result := TPCThread.TryProtectEnterCriticalSection(Self,MaxWaitMilliseconds,FLockNodeOperations);
+end;
+
+procedure TNode.UnlockNode;
+begin
+  FLockNodeOperations.Release;
+end;
+
 class function TNode.EncodeNodeServerAddressArrayToIpString(
   const NodeServerAddressArray: TNodeServerAddressArray): AnsiString;
 var i : Integer;

+ 22 - 9
src/core/URPC.pas

@@ -2927,17 +2927,30 @@ begin
   end else if (method='getpendings') then begin
     // Returns all the operations pending to be included in a block in "Operation resume format" as an array
     // Create result
-    GetResultArray;
-    for i:=FNode.Operations.Count-1 downto 0 do begin
-      if not TPCOperation.OperationToOperationResume(0,FNode.Operations.Operation[i],True,FNode.Operations.Operation[i].SignerAccount,opr) then begin
-        ErrorNum := CT_RPC_ErrNum_InternalError;
-        ErrorDesc := 'Error converting data';
-        exit;
+    k := params.AsInteger('max',100);
+    j := params.AsInteger('start',0);
+    If FNode.TryLockNode(5000) then begin
+      Try
+        jsonarr := GetResultArray;
+        for i := j to FNode.Operations.Count-1 do begin
+          If TPCOperation.OperationToOperationResume(0,FNode.Operations.Operation[i],True,FNode.Operations.Operation[i].SignerAccount,opr) then begin
+            opr.NOpInsideBlock:=i;
+            opr.time:=pcops.OperationBlock.timestamp;
+            opr.Balance := -1; // Don't include!
+            FillOperationResumeToJSONObject(opr,jsonarr.GetAsObject(jsonarr.Count));
+          end;
+          if (k>0) And (jsonarr.Count>=k) then break;
+        end;
+      finally
+        FNode.UnlockNode;
       end;
-      opr.NOpInsideBlock:=i;
-      opr.Balance := FNode.Operations.SafeBoxTransaction.Account(FNode.Operations.Operation[i].SignerAccount).balance;
-      FillOperationResumeToJSONObject(opr,GetResultArray.GetAsObject( FNode.Operations.Count-1-i ));
+      Result := true;
+    end else begin
+      ErrorNum := CT_RPC_ErrNum_InternalError;
+      ErrorDesc := 'Node is busy';
     end;
+  end else if (method='getpendingscount') then begin
+    jsonresponse.GetAsVariant('result').Value := FNode.Operations.Count;
     Result := true;
   end else if (method='decodeophash') then begin
     // Search for an operation based on "ophash"