UCTRLSyncronization.pas 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. unit UCTRLSyncronization;
  2. {$mode delphi}
  3. { Copyright (c) 2018 Sphere 10 Software
  4. Distributed under the MIT software license, see the accompanying file LICENSE
  5. or visit http://www.opensource.org/licenses/mit-license.php.
  6. Acknowledgements:
  7. - Herman Schoenfeld: unit creator, implementation
  8. }
  9. interface
  10. {$I ..\config.inc}
  11. uses
  12. Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  13. StdCtrls, ComCtrls, Buttons, UCommon.UI;
  14. type
  15. { TSyncMode }
  16. TSyncMode = (smUnset, smInitialising, smReady);
  17. { TCTRLSyncronization }
  18. TCTRLSyncronization = class(TApplicationForm)
  19. btnBack: TSpeedButton;
  20. GroupBox1: TGroupBox;
  21. imgSplash: TImage;
  22. Label16: TLabel;
  23. Label4: TLabel;
  24. Label8: TLabel;
  25. lblBlockAgeLabel: TLabel;
  26. lblBlockAgeValue: TLabel;
  27. lblBlocksFound: TLabel;
  28. lblBlockTargetLabel: TLabel;
  29. lblBlockTargetValue: TLabel;
  30. lblCurrentDifficultyCaption1: TLabel;
  31. lblCurrentDifficultyCaption2: TLabel;
  32. lblMinersClientsValue: TLabel;
  33. lblMiningStatusCaption: TLabel;
  34. lblNetProtocolVersion: TLabel;
  35. lblNodeStatus: TLabel;
  36. lblPendingOperationsLabel: TLabel;
  37. lblPendingOperationsValue: TLabel;
  38. lblProtocolVersion: TLabel;
  39. lblReceivedMessages: TLabel;
  40. lblTimeAverage: TLabel;
  41. lblTimeAverageAux: TLabel;
  42. lblTotalAccountsLabel: TLabel;
  43. lblTotalAccountsValue: TLabel;
  44. lblTotalBlocksLabel: TLabel;
  45. lblTotalBlocksValue: TLabel;
  46. paSplash: TPanel;
  47. paSync: TPanel;
  48. procedure btnBackClick(Sender: TObject);
  49. procedure lblReceivedMessagesClick(Sender:TObject);
  50. private
  51. FMinedBlocksCount: Integer;
  52. FMode : TSyncMode;
  53. procedure SetMinedBlocksCount(const Value: Integer);
  54. procedure SetSyncMode(AMode : TSyncMode);
  55. protected
  56. procedure ActivateFirstTime; override;
  57. public
  58. property MinedBlocksCount : Integer read FMinedBlocksCount write SetMinedBlocksCount;
  59. property SyncMode : TSyncMode read FMode write SetSyncMode;
  60. procedure UpdateNodeStatus;
  61. procedure UpdateBlockChainState;
  62. procedure OnFinishedLoadingDatabase;
  63. end;
  64. implementation
  65. {$R *.lfm}
  66. uses UNetProtocol,UTime,UConst, UUserInterface;
  67. procedure TCTRLSyncronization.ActivateFirstTime;
  68. begin
  69. FMode := smInitialising;
  70. paSplash.Visible:= true;
  71. paSync.Visible := false;
  72. end;
  73. procedure TCTRLSyncronization.SetSyncMode(AMode : TSyncMode);
  74. begin
  75. if FMode = AMode then exit;
  76. case AMode of
  77. smInitialising: begin
  78. TUserInterface.Enabled := false;
  79. paSplash.Visible:= true;
  80. paSync.Visible := false;
  81. end;
  82. smReady: begin
  83. TUserInterface.Enabled := true;
  84. paSplash.Visible:= false;
  85. paSync.Visible := true;
  86. end;
  87. end;
  88. end;
  89. procedure TCTRLSyncronization.UpdateNodeStatus;
  90. Var status : AnsiString;
  91. begin
  92. if not TUserInterface.Started then exit;
  93. If Not Assigned(TUserInterface.Node) then begin
  94. lblNodeStatus.Font.Color := clRed;
  95. lblNodeStatus.Caption := 'Initializing...';
  96. end else begin
  97. SyncMode:=smReady;
  98. If TUserInterface.Node.IsReady(status) then begin
  99. if TNetData.NetData.NetStatistics.ActiveConnections>0 then begin
  100. lblNodeStatus.Font.Color := clGreen;
  101. if TNetData.NetData.IsDiscoveringServers then begin
  102. lblNodeStatus.Caption := 'Discovering servers';
  103. end else if TNetData.NetData.IsGettingNewBlockChainFromClient then begin
  104. lblNodeStatus.Caption := 'Obtaining new blockchain';
  105. end else begin
  106. lblNodeStatus.Caption := 'Running';
  107. end;
  108. end else begin
  109. lblNodeStatus.Font.Color := clRed;
  110. lblNodeStatus.Caption := 'Alone in the world...';
  111. end;
  112. end else begin
  113. lblNodeStatus.Font.Color := clRed;
  114. lblNodeStatus.Caption := status;
  115. end;
  116. end;
  117. lblProtocolVersion.Caption := Format('%d (%d)', [TUserInterface.Node.Bank.SafeBox.CurrentProtocol,CT_BlockChain_Protocol_Available]);
  118. lblNetProtocolVersion.Caption := Format('%d (%d)', [CT_NetProtocol_Version, CT_NetProtocol_Available]);
  119. if NOT btnBack.Enabled then begin
  120. lblNodeStatus.Caption := 'Please wait until finished - ' + lblNodeStatus.Caption;
  121. end;
  122. end;
  123. procedure TCTRLSyncronization.UpdateBlockChainState;
  124. Var
  125. f, favg : real;
  126. begin
  127. if not TUserInterface.Started then exit;
  128. UpdateNodeStatus;
  129. if Assigned(TUserInterface.Node) then begin
  130. if TUserInterface.Node.Bank.BlocksCount>0 then begin
  131. lblTotalBlocksValue.Caption := Inttostr(TUserInterface.Node.Bank.BlocksCount)+' (0..'+Inttostr(TUserInterface.Node.Bank.BlocksCount-1)+')'; ;
  132. end else lblTotalBlocksValue.Caption := '(none)';
  133. lblTotalAccountsValue.Caption := Inttostr(TUserInterface.Node.Bank.AccountsCount);
  134. lblBlockAgeValue.Caption := UnixTimeToLocalElapsedTime(TUserInterface.Node.Bank.LastOperationBlock.timestamp);
  135. lblPendingOperationsValue.Caption := Inttostr(TUserInterface.Node.Operations.Count);
  136. lblBlockTargetValue.Caption := InttoHex(TUserInterface.Node.Operations.OperationBlock.compact_target,8);
  137. favg := TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage);
  138. f := (CT_NewLineSecondsAvg - favg) / CT_NewLineSecondsAvg;
  139. lblTimeAverage.Caption := 'Last '+Inttostr(CT_CalcNewTargetBlocksAverage)+': '+FormatFloat('0.0',favg)+' sec. (Optimal '+Inttostr(CT_NewLineSecondsAvg)+'s) Deviation '+FormatFloat('0.00%',f*100);
  140. if favg>=CT_NewLineSecondsAvg then begin
  141. lblTimeAverage.Font.Color := clNavy;
  142. end else begin
  143. lblTimeAverage.Font.Color := clOlive;
  144. end;
  145. lblTimeAverageAux.Caption := Format('Last %d: %s sec. - %d: %s sec. - %d: %s sec. - %d: %s sec. - %d: %s sec.',[
  146. CT_CalcNewTargetBlocksAverage * 2 ,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage * 2)),
  147. ((CT_CalcNewTargetBlocksAverage * 3) DIV 2) ,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage((CT_CalcNewTargetBlocksAverage * 3) DIV 2)),
  148. ((CT_CalcNewTargetBlocksAverage DIV 4)*3),FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(((CT_CalcNewTargetBlocksAverage DIV 4)*3))),
  149. CT_CalcNewTargetBlocksAverage DIV 2,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage DIV 2)),
  150. CT_CalcNewTargetBlocksAverage DIV 4,FormatFloat('0.0',TUserInterface.Node.Bank.GetActualTargetSecondsAverage(CT_CalcNewTargetBlocksAverage DIV 4))]);
  151. end else begin
  152. lblTotalBlocksValue.Caption := '';
  153. lblTotalAccountsValue.Caption := '';
  154. lblBlockAgeValue.Caption := '';
  155. lblPendingOperationsValue.Caption := '';
  156. lblBlockTargetValue.Caption := '';
  157. lblTimeAverage.Caption := '';
  158. lblTimeAverageAux.Caption := '';
  159. end;
  160. if (Assigned(TUserInterface.PoolMiningServer)) And (TUserInterface.PoolMiningServer.Active) then begin
  161. If TUserInterface.PoolMiningServer.ClientsCount>0 then begin
  162. lblMinersClientsValue.Caption := IntToStr(TUserInterface.PoolMiningServer.ClientsCount)+' connected JSON-RPC clients';
  163. lblMinersClientsValue.Font.Color := clNavy;
  164. end else begin
  165. lblMinersClientsValue.Caption := 'No JSON-RPC clients';
  166. lblMinersClientsValue.Font.Color := clDkGray;
  167. end;
  168. MinedBlocksCount := TUserInterface.PoolMiningServer.ClientsWins;
  169. end else begin
  170. MinedBlocksCount := 0;
  171. lblMinersClientsValue.Caption := 'JSON-RPC server not active';
  172. lblMinersClientsValue.Font.Color := clRed;
  173. end;
  174. end;
  175. procedure TCTRLSyncronization.SetMinedBlocksCount(const Value: Integer);
  176. begin
  177. FMinedBlocksCount := Value;
  178. lblBlocksFound.Caption := Inttostr(Value);
  179. if Value>0 then lblBlocksFound.Font.Color := clGreen
  180. else lblBlocksFound.Font.Color := clDkGray;
  181. end;
  182. procedure TCTRLSyncronization.OnFinishedLoadingDatabase;
  183. begin
  184. btnBack.Enabled:=true;
  185. TUserInterface.ShowWallet;
  186. end;
  187. procedure TCTRLSyncronization.lblReceivedMessagesClick(Sender:TObject);
  188. begin
  189. TUserInterface.ShowMessagesForm;
  190. end;
  191. procedure TCTRLSyncronization.btnBackClick(Sender: TObject);
  192. begin
  193. TUserInterface.ShowWallet;
  194. end;
  195. end.