UCTRLSyncronization.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. unit UCTRLSyncronization;
  2. { Copyright (c) 2018 by Herman Schoenfeld
  3. Distributed under the MIT software license, see the accompanying file LICENSE
  4. or visit http://www.opensource.org/licenses/mit-license.php.
  5. This unit is a part of the PascalCoin Project, an infinitely scalable
  6. cryptocurrency. Find us here:
  7. Web: https://www.pascalcoin.org
  8. Source: https://github.com/PascalCoin/PascalCoin
  9. THIS LICENSE HEADER MUST NOT BE REMOVED.
  10. }
  11. {$mode delphi}
  12. interface
  13. {$I ..\config.inc}
  14. uses
  15. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  16. StdCtrls, ComCtrls, Buttons, UCommon.UI, UNetProtocol, UBaseTypes;
  17. type
  18. { TCTRLSyncronization }
  19. TCTRLSyncronization = class(TApplicationForm)
  20. btnBack: TSpeedButton;
  21. GroupBox1: TGroupBox;
  22. imgSplash: TImage;
  23. Label16: TLabel;
  24. Label4: TLabel;
  25. Label8: TLabel;
  26. lblBlockAgeLabel: TLabel;
  27. lblBlockAgeValue: TLabel;
  28. lblBlocksFound: TLabel;
  29. lblBlockTargetLabel: TLabel;
  30. lblBlockTargetValue: TLabel;
  31. lblCurrentDifficultyCaption1: TLabel;
  32. lblCurrentDifficultyCaption2: TLabel;
  33. lblMinersClientsValue: TLabel;
  34. lblMiningStatusCaption: TLabel;
  35. lblNetProtocolVersion: TLabel;
  36. lblNodeStatus: TLabel;
  37. lblPendingOperationsLabel: TLabel;
  38. lblPendingOperationsValue: TLabel;
  39. lblProtocolVersion: TLabel;
  40. lblReceivedMessages: TLabel;
  41. lblTimeAverage: TLabel;
  42. lblTimeAverageAux: TLabel;
  43. lblTotalAccountsLabel: TLabel;
  44. lblTotalAccountsValue: TLabel;
  45. lblTotalBlocksLabel: TLabel;
  46. lblTotalBlocksValue: TLabel;
  47. paSplash: TPanel;
  48. paSync: TPanel;
  49. procedure btnBackClick(Sender: TObject);
  50. procedure FormCreate(Sender: TObject);
  51. procedure FormDestroy(Sender: TObject);
  52. procedure lblReceivedMessagesClick(Sender:TObject);
  53. private
  54. FMinedBlocksCount: Integer;
  55. FShowSplash : boolean;
  56. FMessagesUnreadCount : Integer;
  57. procedure OnLoading(Sender: TObject; const message : AnsiString; curPos, totalCount : Int64);
  58. procedure OnLoaded(Sender: TObject);
  59. procedure SetMinedBlocksCount(const Value: Integer);
  60. procedure SetShowSplash(ABool : boolean);
  61. procedure SetMessagesNotificationText(const text : AnsiString);
  62. function GetMessagesNotificationText : AnsiString;
  63. procedure SetStatusText(AColour: TColor; AText: String);
  64. procedure UpdateNodeStatus;
  65. procedure UpdateBlockChainState;
  66. procedure OnAppStarted(Sender: TObject);
  67. procedure OnBlocksChanged(Sender: TObject);
  68. procedure OnUIRefreshTimer(Sender: TObject);
  69. procedure OnNodeMessageEvent(NetConnection: TNetConnection; MessageData: TRawBytes);
  70. protected
  71. procedure ActivateFirstTime; override;
  72. public
  73. property MinedBlocksCount : Integer read FMinedBlocksCount write SetMinedBlocksCount;
  74. property ShowSplash : boolean read FShowSplash write SetShowSplash;
  75. property MessagesNotificationText : AnsiString read GetMessagesNotificationText write SetMessagesNotificationText;
  76. end;
  77. implementation
  78. {$R *.lfm}
  79. uses UCommon, UTime, UConst, UUserInterface, UAccounts, UNode;
  80. procedure TCTRLSyncronization.FormCreate(Sender: TObject);
  81. begin
  82. TUserInterface.AppStarted.Add(OnAppStarted);
  83. TUserInterface.Loading.Add(OnLoading);
  84. TUserInterface.BlocksChanged.Add(OnBlocksChanged);
  85. TUserInterface.UIRefreshTimer.Add(OnUIRefreshTimer);
  86. TUserInterface.NodeMessageEvent.Add(OnNodeMessageEvent);
  87. FMessagesUnreadCount := 0;
  88. end;
  89. procedure TCTRLSyncronization.FormDestroy(Sender: TObject);
  90. begin
  91. TUserInterface.AppStarted.Remove(OnAppStarted);
  92. TUserInterface.Loading.Remove (OnLoading);
  93. TUserInterface.BlocksChanged.Remove(OnBlocksChanged);
  94. TUserInterface.UIRefreshTimer.Remove(OnUIRefreshTimer);
  95. TUserInterface.NodeMessageEvent.Remove(OnNodeMessageEvent);
  96. end;
  97. procedure TCTRLSyncronization.ActivateFirstTime;
  98. begin
  99. ShowSplash := true;
  100. end;
  101. procedure TCTRLSyncronization.OnAppStarted(Sender: TObject);
  102. begin
  103. UpdateBlockChainState;
  104. end;
  105. procedure TCTRLSyncronization.OnLoading(Sender : TObject; const message : AnsiString; curPos, totalCount : Int64);
  106. var LPercent : String;
  107. begin
  108. if (totalCount>0) then
  109. LPercent := Format('%.1f',[curPos*100/totalCount])+'%'
  110. else
  111. LPercent := '';
  112. SetStatusText(clGreen, message+' '+LPercent);
  113. end;
  114. procedure TCTRLSyncronization.OnLoaded(Sender: TObject);
  115. begin
  116. btnBack.Enabled:=true;
  117. TUserInterface.ShowWallet;
  118. end;
  119. procedure TCTRLSyncronization.OnBlocksChanged(Sender: TObject);
  120. begin
  121. UpdateBlockChainState;
  122. end;
  123. procedure TCTRLSyncronization.OnUIRefreshTimer(Sender: TObject);
  124. begin
  125. UpdateBlockChainState;
  126. UpdateNodeStatus;
  127. end;
  128. procedure TCTRLSyncronization.OnNodeMessageEvent(NetConnection: TNetConnection; MessageData: TRawBytes);
  129. begin
  130. inc(FMessagesUnreadCount);
  131. if FMessagesUnreadCount>1 then
  132. MessagesNotificationText := Format('You have received %d messages',[FMessagesUnreadCount])
  133. else
  134. MessagesNotificationText := 'You have received 1 message';
  135. end;
  136. procedure TCTRLSyncronization.SetShowSplash(ABool : boolean);
  137. begin
  138. if ABool = FShowSplash then exit;
  139. FShowSplash := ABool;
  140. if FShowSplash then begin
  141. TUserInterface.Enabled := false;
  142. paSplash.Visible:= true;
  143. paSync.Visible := false;
  144. end else begin
  145. TUserInterface.Enabled := true;
  146. paSplash.Visible:= false;
  147. paSync.Visible := true;
  148. end;
  149. end;
  150. function TCTRLSyncronization.GetMessagesNotificationText : AnsiString;
  151. begin
  152. Result := lblReceivedMessages.Caption;
  153. end;
  154. procedure TCTRLSyncronization.SetMessagesNotificationText(const text : AnsiString);
  155. begin
  156. if (text = '') then lblReceivedMessages.Visible := false;
  157. lblReceivedMessages.Caption := text;
  158. end;
  159. procedure TCTRLSyncronization.SetStatusText(AColour: TColor; AText: String);
  160. begin
  161. lblNodeStatus.Font.Color := AColour;
  162. lblNodeStatus.Caption := AText;
  163. end;
  164. procedure TCTRLSyncronization.UpdateNodeStatus;
  165. Var status : AnsiString;
  166. begin
  167. if not TUserInterface.Started then exit;
  168. // State text
  169. case TUserInterface.State of
  170. uisLoading: ShowSplash := false; // text set by OnLoading
  171. uisLoaded: SetStatusText(clGreen, 'Loaded');
  172. uisDiscoveringPeers: SetStatusText(clGreen, 'Discovering Peers');
  173. uisSyncronizingBlockchain: SetStatusText(clGreen, 'Syncronizing');
  174. uisActive: SetStatusText(clGreen, 'Active');
  175. uisIsolated: SetStatusText(clRed, 'Isolated');
  176. uisError: SetStatusText(clRed, Format('Error: %s', [TUserInterface.StateText]));
  177. end;
  178. // Protocol labels
  179. lblProtocolVersion.Caption := Format('%d (%d)', [TUserInterface.Node.Bank.SafeBox.CurrentProtocol,CT_BlockChain_Protocol_Available]);
  180. lblNetProtocolVersion.Caption := Format('%d (%d)', [CT_NetProtocol_Version, CT_NetProtocol_Available]);
  181. end;
  182. procedure TCTRLSyncronization.UpdateBlockChainState;
  183. Var
  184. f, favg : real;
  185. begin
  186. TUserInterface.Node.Operations.Lock;
  187. try
  188. if not TUserInterface.Started then exit;
  189. UpdateNodeStatus;
  190. if Assigned(TUserInterface.Node) then begin
  191. if TUserInterface.Node.Bank.BlocksCount>0 then begin
  192. lblTotalBlocksValue.Caption := Inttostr(TUserInterface.Node.Bank.BlocksCount)+' (0..'+Inttostr(TUserInterface.Node.Bank.BlocksCount-1)+')'; ;
  193. end else lblTotalBlocksValue.Caption := '(none)';
  194. lblTotalAccountsValue.Caption := Inttostr(TUserInterface.Node.Bank.AccountsCount);
  195. lblBlockAgeValue.Caption := UnixTimeToLocalElapsedTime(TUserInterface.Node.Bank.LastOperationBlock.timestamp);
  196. lblPendingOperationsValue.Caption := Inttostr(TUserInterface.Node.Operations.Count);
  197. lblBlockTargetValue.Caption := InttoHex(TUserInterface.Node.Operations.OperationBlock.compact_target,8);
  198. favg := TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage);
  199. f := (CT_NewLineSecondsAvg - favg) / CT_NewLineSecondsAvg;
  200. lblTimeAverage.Caption := 'Last '+Inttostr(CT_CalcNewTargetBlocksAverage)+': '+FormatFloat('0.0',favg)+' sec. (Optimal '+Inttostr(CT_NewLineSecondsAvg)+'s) Deviation '+FormatFloat('0.00%',f*100);
  201. if favg>=CT_NewLineSecondsAvg then begin
  202. lblTimeAverage.Font.Color := clNavy;
  203. end else begin
  204. lblTimeAverage.Font.Color := clOlive;
  205. end;
  206. lblTimeAverageAux.Caption := Format('Last %d: %s sec. - %d: %s sec. - %d: %s sec. - %d: %s sec. - %d: %s sec.',[
  207. CT_CalcNewTargetBlocksAverage * 2 ,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage * 2)),
  208. ((CT_CalcNewTargetBlocksAverage * 3) DIV 2) ,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage((CT_CalcNewTargetBlocksAverage * 3) DIV 2)),
  209. ((CT_CalcNewTargetBlocksAverage DIV 4)*3),FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(((CT_CalcNewTargetBlocksAverage DIV 4)*3))),
  210. CT_CalcNewTargetBlocksAverage DIV 2,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage DIV 2)),
  211. CT_CalcNewTargetBlocksAverage DIV 4,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage DIV 4))]);
  212. end else begin
  213. lblTotalBlocksValue.Caption := '';
  214. lblTotalAccountsValue.Caption := '';
  215. lblBlockAgeValue.Caption := '';
  216. lblPendingOperationsValue.Caption := '';
  217. lblBlockTargetValue.Caption := '';
  218. lblTimeAverage.Caption := '';
  219. lblTimeAverageAux.Caption := '';
  220. end;
  221. if (Assigned(TUserInterface.PoolMiningServer)) And (TUserInterface.PoolMiningServer.Active) then begin
  222. If TUserInterface.PoolMiningServer.ClientsCount>0 then begin
  223. lblMinersClientsValue.Caption := IntToStr(TUserInterface.PoolMiningServer.ClientsCount)+' connected JSON-RPC clients';
  224. lblMinersClientsValue.Font.Color := clNavy;
  225. end else begin
  226. lblMinersClientsValue.Caption := 'No JSON-RPC clients';
  227. lblMinersClientsValue.Font.Color := clDkGray;
  228. end;
  229. MinedBlocksCount := TUserInterface.PoolMiningServer.ClientsWins;
  230. end else begin
  231. MinedBlocksCount := 0;
  232. lblMinersClientsValue.Caption := 'JSON-RPC server not active';
  233. lblMinersClientsValue.Font.Color := clRed;
  234. end;
  235. finally
  236. TUserInterface.Node.Operations.Unlock;
  237. end;
  238. end;
  239. procedure TCTRLSyncronization.SetMinedBlocksCount(const Value: Integer);
  240. begin
  241. FMinedBlocksCount := Value;
  242. lblBlocksFound.Caption := Inttostr(Value);
  243. if Value>0 then lblBlocksFound.Font.Color := clGreen
  244. else lblBlocksFound.Font.Color := clDkGray;
  245. end;
  246. procedure TCTRLSyncronization.lblReceivedMessagesClick(Sender:TObject);
  247. begin
  248. lblReceivedMessages.Visible := false;
  249. lblReceivedMessages.Caption := text;
  250. TUserInterface.ShowMessagesForm;
  251. end;
  252. procedure TCTRLSyncronization.btnBackClick(Sender: TObject);
  253. begin
  254. TUserInterface.ShowWallet;
  255. end;
  256. end.