Browse Source

Code: minor refactoring

Herman Schoenfeld 7 years ago
parent
commit
f98e25b2ef
3 changed files with 103 additions and 105 deletions
  1. 99 101
      src/core.utils/UCoreUtils.pas
  2. 1 1
      src/core.utils/UDataSources.pas
  3. 3 3
      src/gui/UCTRLWallet.pas

+ 99 - 101
src/core.utils/UCoreUtils.pas

@@ -60,35 +60,23 @@ type
   { TCoreTool }
   { TCoreTool }
 
 
   TCoreTool = class
   TCoreTool = class
+  private
+    class function GetBalanceInternal(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary;
   public
   public
     class function GetSignerCandidates(ANumOps: integer; ASingleOperationFee: int64; const ACandidates: array of TAccount): TArray<TAccount>; static;
     class function GetSignerCandidates(ANumOps: integer; ASingleOperationFee: int64; const ACandidates: array of TAccount): TArray<TAccount>; static;
     class function GetOperationShortText(const OpType, OpSubType: DWord): ansistring; static; inline;
     class function GetOperationShortText(const OpType, OpSubType: DWord): ansistring; static; inline;
-  end;
-
-  { TOrderedAccountKeysListHelper }
-
-  TOrderedAccountKeysListHelper = class helper for TOrderedAccountKeysList
-  public
-    function GetBalance(IncludePending: boolean = False): TBalanceSummary;
-    function GetAccounts(IncludePending: boolean = False): TArray<TAccount>;
-    function GetAccountNumbers: TArray<cardinal>;
-  end;
-
-  { TSafeBoxHelper }
-
-  TSafeBoxHelper = class helper for TPCSafeBox
-  private
-    function GetBalanceInternal(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary;
-  public
-    function GetModifiedAccounts(const AAccounts: array of TAccount): TArray<TAccount>;
-    function GetBalance(const AKey: TAccountKey; IncludePending: boolean = False): TBalanceSummary; overload;
-    function GetBalance(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary; overload;
+    class function GetUserBalance(IncludePending: boolean = False): TBalanceSummary;
+    class function GetUserAccounts(IncludePending: boolean = False): TArray<TAccount>;
+    class function GetUserAccountNumbers: TArray<cardinal>;
+    class function GetModifiedAccounts(const AAccounts: array of TAccount; IncludePending : boolean = false): TArray<TAccount>;
+    class function GetBalance(const AKey: TAccountKey; IncludePending: boolean = False): TBalanceSummary; overload;
+    class function GetBalance(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary; overload;
   end;
   end;
 
 
   { TNodeHelper }
   { TNodeHelper }
 
 
   TNodeHelper = class helper for TNode
   TNodeHelper = class helper for TNode
-    function HasBestKnownBlockchainTip: boolean;
+   function HasBestKnownBlockchainTip: boolean;
   end;
   end;
 
 
   { TAccountHelper }
   { TAccountHelper }
@@ -119,7 +107,6 @@ implementation
 uses
 uses
   UMemory, UConst, UWallet, UECIES, UAES;
   UMemory, UConst, UWallet, UECIES, UAES;
 
 
-
 { TCoreTool }
 { TCoreTool }
 
 
 class function TCoreTool.GetSignerCandidates(ANumOps: integer; ASingleOperationFee: int64; const ACandidates: array of TAccount): TArray<TAccount>;
 class function TCoreTool.GetSignerCandidates(ANumOps: integer; ASingleOperationFee: int64; const ACandidates: array of TAccount): TArray<TAccount>;
@@ -195,31 +182,85 @@ begin
   end;
   end;
 end;
 end;
 
 
+class function TCoreTool.GetModifiedAccounts(const AAccounts: array of TAccount; IncludePending : boolean = false): TArray<TAccount>;
+var
+  i: integer;
+  LChanged: TList<TAccount>;
+  LAcc: TAccount;
+  GC: TDisposables;
+begin
+  LChanged := GC.AddObject(TList<TAccount>.Create) as TList<TAccount>;
+  for i := Low(AAccounts) to High(AAccounts) do
+  begin
+    if IncludePending then
+      LAcc := TNode.Node.Operations.SafeBoxTransaction.Account(AAccounts[i].account)
+    else begin
+      TNode.Node.Bank.SafeBox.StartThreadSafe;
+      LAcc := TNode.Node.Bank.SafeBox.Account(AAccounts[i].account);
+      TNode.Node.Bank.SafeBox.EndThreadSave;
+    end;
+    if (LAcc.n_Operation <> AAccounts[i].n_operation) or (LAcc.Balance <> AAccounts[i].balance) then
+      LChanged.Add(LAcc);
+  end;
+  Result := LChanged.ToArray;
+end;
 
 
-{ TNodeHelper }
+class function TCoreTool.GetBalance(const AKey: TAccountKey; IncludePending: boolean = False): TBalanceSummary;
+begin
+  Result := GetBalance([AKey], IncludePending);
+end;
 
 
-function TNodeHelper.HasBestKnownBlockchainTip: boolean;
+class function TCoreTool.GetBalance(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary;
+begin
+  Result := GetBalanceInternal(AKeys, IncludePending);
+end;
+
+class function TCoreTool.GetBalanceInternal(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary;
 var
 var
-  LReady: boolean;
-  LMsg: ansistring;
-  LDestBlock : Cardinal;
+  i: integer;
+  LAcc: TAccount;
+  LAccs: THashSet<TAccount>;
+  LKeys: THashSet<TAccountKey>;
+  GC: TDisposables;
 begin
 begin
-  LReady := Self.Bank.IsReady(LMsg);
-  if LReady and TNetData.NetData.IsGettingNewBlockChainFromClient then begin
-    LDestBlock := TNetData.NetData.MaxRemoteOperationBlock.block;
-    Result := Self.Bank.BlocksCount = TNetData.NetData.MaxRemoteOperationBlock.block;
+  // Setup local collections
+  LAccs := GC.AddObject(THashSet<TAccount>.Create(TAccountEqualityComparer.Create)) as THashSet<TAccount>;
+  LKeys := GC.AddObject(THashSet<TAccountKey>.Create(TAccountKeyEqualityComparer.Create)) as THashSet<TAccountKey>;
+
+  // Gather all keys into hashset
+  for i := Low(AKeys) to High(AKeys) do
+    LKeys.Add(AKeys[i]);
+
+  // Gather all referenced accounts
+  for i := 0 to TNode.Node.Bank.SafeBox.AccountsCount - 1 do
+  begin
+    if IncludePending then
+      LAcc := TNode.Node.Operations.SafeBoxTransaction.Account(i)
+    else begin
+      TNode.Node.Bank.SafeBox.StartThreadSafe;
+      LAcc := TNode.Node.Bank.SafeBox.Account(i);
+      TNode.Node.Bank.SafeBox.EndThreadSave;
+    end;
+    if LKeys.Contains(LAcc.accountInfo.accountKey) then
+      LAccs.Add(LAcc);
   end;
   end;
-end;
 
 
-{ TOrderedAccountKeysListHelper }
+  // Build the results
+  Result := CT_BalanceSummary_Nil;
+  for LAcc in LAccs do
+  begin
+    Inc(Result.TotalPASA);
+    Inc(Result.TotalPASC, LAcc.Balance);
+  end;
+end;
 
 
-function TOrderedAccountKeysListHelper.GetBalance(IncludePending: boolean = False): TBalanceSummary;
+class function TCoreTool.GetUserBalance(IncludePending: boolean = False): TBalanceSummary;
 var
 var
   i: integer;
   i: integer;
   LAccs: TArray<TAccount>;
   LAccs: TArray<TAccount>;
 begin
 begin
   Result := CT_BalanceSummary_Nil;
   Result := CT_BalanceSummary_Nil;
-  LAccs := Self.GetAccounts(IncludePending);
+  LAccs := TCoreTool.GetUserAccounts(IncludePending);
   for i := Low(LAccs) to High(LAccs) do
   for i := Low(LAccs) to High(LAccs) do
   begin
   begin
     Inc(Result.TotalPASA);
     Inc(Result.TotalPASA);
@@ -227,7 +268,7 @@ begin
   end;
   end;
 end;
 end;
 
 
-function TOrderedAccountKeysListHelper.GetAccounts(IncludePending: boolean = False): TArray<TAccount>;
+class function TCoreTool.GetUserAccounts(IncludePending: boolean = False): TArray<TAccount>;
 var
 var
   i, j: integer;
   i, j: integer;
   LAccs: TList<TAccount>;
   LAccs: TList<TAccount>;
@@ -236,100 +277,57 @@ var
   Disposables: TDisposables;
   Disposables: TDisposables;
 begin
 begin
   LAccs := Disposables.AddObject(TList<TAccount>.Create) as TList<TAccount>;
   LAccs := Disposables.AddObject(TList<TAccount>.Create) as TList<TAccount>;
-  for i := 0 to Self.Count - 1 do
+  for i := 0 to TWallet.Keys.Count - 1 do
   begin
   begin
-    LList := Self.AccountKeyList[i];
-    for j := 0 to LList.Count - 1 do
-    begin
+    LList := TWallet.Keys.AccountsKeyList.AccountKeyList[i];
+    for j := 0 to LList.Count - 1 do begin
       if IncludePending then
       if IncludePending then
         LAcc := TNode.Node.Operations.SafeBoxTransaction.Account(j)
         LAcc := TNode.Node.Operations.SafeBoxTransaction.Account(j)
-      else
+      else begin
+        TNode.Node.Bank.SafeBox.StartThreadSafe;
         LAcc := TNode.Node.Bank.SafeBox.Account(LList.Get(j));
         LAcc := TNode.Node.Bank.SafeBox.Account(LList.Get(j));
+        TNode.Node.Bank.SafeBox.EndThreadSave;
+      end;
       LAccs.Add(LAcc);
       LAccs.Add(LAcc);
     end;
     end;
   end;
   end;
   Result := LAccs.ToArray;
   Result := LAccs.ToArray;
 end;
 end;
 
 
-function TOrderedAccountKeysListHelper.GetAccountNumbers: TArray<cardinal>;
+class function TCoreTool.GetUserAccountNumbers: TArray<cardinal>;
 var
 var
   i, j: integer;
   i, j: integer;
-  LAccs: TList<cardinal>;
+  LAccs: TSortedList<cardinal>;
   LList: TOrderedCardinalList;
   LList: TOrderedCardinalList;
   Disposables: TDisposables;
   Disposables: TDisposables;
 begin
 begin
-  LAccs := Disposables.AddObject(TList<cardinal>.Create) as TList<cardinal>;
-  for i := 0 to Self.Count - 1 do
+  LAccs := Disposables.AddObject(TSortedList<cardinal>.Create) as TSortedList<cardinal>;
+  for i := 0 to TWallet.Keys.AccountsKeyList.Count - 1 do
   begin
   begin
-    LList := Self.AccountKeyList[i];
+    LList := TWallet.Keys.AccountsKeyList.AccountKeyList[i];
     for j := 0 to LList.Count - 1 do
     for j := 0 to LList.Count - 1 do
       LAccs.Add(j);
       LAccs.Add(j);
   end;
   end;
   Result := LAccs.ToArray;
   Result := LAccs.ToArray;
 end;
 end;
 
 
-{ TSafeBoxHelper }
 
 
-function TSafeBoxHelper.GetModifiedAccounts(const AAccounts: array of TAccount): TArray<TAccount>;
-var
-  i: integer;
-  LChanged: TList<TAccount>;
-  LAcc: TAccount;
-  GC: TDisposables;
-begin
-  LChanged := GC.AddObject(TList<TAccount>.Create) as TList<TAccount>;
-  for i := Low(AAccounts) to High(AAccounts) do
-  begin
-    LAcc := Self.Account(AAccounts[i].account);
-    if (LAcc.n_Operation <> AAccounts[i].n_operation) or (LAcc.Balance <> AAccounts[i].balance) then
-      LChanged.Add(LAcc);
-  end;
-  Result := LChanged.ToArray;
-end;
-
-function TSafeBoxHelper.GetBalance(const AKey: TAccountKey; IncludePending: boolean = False): TBalanceSummary;
-begin
-  Result := GetBalance([AKey], IncludePending);
-end;
-
-function TSafeBoxHelper.GetBalance(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary;
-begin
-  Result := GetBalanceInternal(AKeys, IncludePending);
-end;
+{ TNodeHelper }
 
 
-function TSafeBoxHelper.GetBalanceInternal(const AKeys: array of TAccountKey; IncludePending: boolean = False): TBalanceSummary;
+function TNodeHelper.HasBestKnownBlockchainTip: boolean;
 var
 var
-  i: integer;
-  LAcc: TAccount;
-  LAccs: THashSet<TAccount>;
-  LKeys: THashSet<TAccountKey>;
-  GC: TDisposables;
+  LReady: boolean;
+  LMsg: ansistring;
+  LDestBlock : Cardinal;
 begin
 begin
-  // Setup local collections
-  LAccs := GC.AddObject(THashSet<TAccount>.Create(TAccountEqualityComparer.Create)) as THashSet<TAccount>;
-  LKeys := GC.AddObject(THashSet<TAccountKey>.Create(TAccountKeyEqualityComparer.Create)) as THashSet<TAccountKey>;
-
-  // Gather all keys into hashset
-  for i := Low(AKeys) to High(AKeys) do
-    LKeys.Add(AKeys[i]);
-
-  // Gather all referenced accounts
-  for i := 0 to Self.AccountsCount - 1 do
-  begin
-    LAcc := Self.Account(i);
-    if LKeys.Contains(LAcc.accountInfo.accountKey) then
-      LAccs.Add(LAcc);
-  end;
-
-  // Build the results
-  Result := CT_BalanceSummary_Nil;
-  for LAcc in LAccs do
-  begin
-    Inc(Result.TotalPASA);
-    Inc(Result.TotalPASC, LAcc.Balance);
+  LReady := Self.Bank.IsReady(LMsg);
+  if LReady and TNetData.NetData.IsGettingNewBlockChainFromClient then begin
+    LDestBlock := TNetData.NetData.MaxRemoteOperationBlock.block;
+    Result := Self.Bank.BlocksCount = TNetData.NetData.MaxRemoteOperationBlock.block;
   end;
   end;
 end;
 end;
 
 
+
 { TAccountComparer }
 { TAccountComparer }
 
 
 function TAccountComparer.Compare(constref ALeft, ARight: TAccount): integer;
 function TAccountComparer.Compare(constref ALeft, ARight: TAccount): integer;

+ 1 - 1
src/core.utils/UDataSources.pas

@@ -250,7 +250,7 @@ var
   i: integer;
   i: integer;
   LAccs: TArray<TAccount>;
   LAccs: TArray<TAccount>;
 begin
 begin
-  LAccs := TWallet.Keys.AccountsKeyList.GetAccounts(FIncludePending);
+  LAccs := TCoreTool.GetUserAccounts(FIncludePending);
   if FKeys.Count > 0 then begin
   if FKeys.Count > 0 then begin
     for i := Low(LAccs) to High(LAccs) do
     for i := Low(LAccs) to High(LAccs) do
       if FKeys.Contains(LAccs[i].accountInfo.accountKey) then
       if FKeys.Contains(LAccs[i].accountInfo.accountKey) then

+ 3 - 3
src/gui/UCTRLWallet.pas

@@ -125,7 +125,7 @@ begin
   // fields
   // fields
   FAccountsDataSource := TMyAccountsDataSource.Create(Self);
   FAccountsDataSource := TMyAccountsDataSource.Create(Self);
   FOperationsDataSource := TAccountsOperationsDataSource.Create(Self);
   FOperationsDataSource := TAccountsOperationsDataSource.Create(Self);
-  FOperationsDataSource.Accounts := TWallet.Keys.AccountsKeyList.GetAccountNumbers;
+  FOperationsDataSource.Accounts := TCoreTool.GetUserAccountNumbers;
   FOperationsHistory := woh7Days;
   FOperationsHistory := woh7Days;
   FOperationsMode:= womAllAccounts;
   FOperationsMode:= womAllAccounts;
   FAccountsMode := wamMyAccounts;
   FAccountsMode := wamMyAccounts;
@@ -305,7 +305,7 @@ end;
 procedure TCTRLWallet.RefreshTotals;
 procedure TCTRLWallet.RefreshTotals;
 var LBalance : TBalanceSummary;
 var LBalance : TBalanceSummary;
 begin
 begin
-  LBalance := TWallet.Keys.AccountsKeyList.GetBalance(true);
+  LBalance := TCoretool.GetUserBalance(true);
   lblTotalPASC.Caption := TAccountComp.FormatMoney(LBalance.TotalPASC);
   lblTotalPASC.Caption := TAccountComp.FormatMoney(LBalance.TotalPASC);
   lblTotalPASA.Caption := Format('%d', [LBalance.TotalPASA]);
   lblTotalPASA.Caption := Format('%d', [LBalance.TotalPASA]);
 end;
 end;
@@ -411,7 +411,7 @@ begin
   case FOperationsMode of
   case FOperationsMode of
     womAllAccounts: begin
     womAllAccounts: begin
       FOperationsGrid.Caption.Text := '';
       FOperationsGrid.Caption.Text := '';
-      FOperationsDataSource.Accounts := TWallet.Keys.AccountsKeyList.GetAccountNumbers;
+      FOperationsDataSource.Accounts := TCoreTool.GetUserAccountNumbers;
     end;
     end;
     womSelectedAccounts:
     womSelectedAccounts:
     begin
     begin