pascalcoin_miner.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. program PascalCoinMiner;
  2. {$mode delphi}{$H+}
  3. {$DEFINE UseCThreads}
  4. {$I config.inc}
  5. { Copyright (c) 2017 by Albert Molina
  6. Distributed under the MIT software license, see the accompanying file LICENSE
  7. or visit http://www.opensource.org/licenses/mit-license.php.
  8. This unit is a part of Pascal Coin, a P2P crypto currency without need of
  9. historical operations.
  10. If you like it, consider a donation using BitCoin:
  11. 16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
  12. }
  13. {$I config.inc}
  14. uses
  15. {$IFDEF UNIX}{$IFDEF UseCThreads}
  16. cthreads,
  17. {$ENDIF}{$ENDIF}
  18. Classes, SysUtils, CustApp, crt, SyncObjs,
  19. UBlockChain, UPoolMinerThreads, UGPUMining,
  20. UPoolMining, ULog, UThread, UAccounts, UCrypto,
  21. UConst, UTime, UNode, UNetProtocol, USha256,
  22. UOpenSSL, UBaseTypes, UCommon,
  23. DelphiCL;
  24. type
  25. { TPascalMinerApp }
  26. TPascalMinerApp = class(TCustomApplication)
  27. FLastLogs : TStringList;
  28. procedure ShowGPUDrivers;
  29. procedure OnConnectionStateChanged(Sender : TObject);
  30. procedure OnDeviceStateChanged(Sender : TObject);
  31. procedure OnMinerValuesChanged(Sender : TObject);
  32. procedure OnFoundNOnce(Sender : TCustomMinerDeviceThread; Timestamp, nOnce : Cardinal);
  33. procedure WriteLine(nline : Integer; txt : String);
  34. procedure OnInThreadNewLog(logtype : TLogType; Time : TDateTime; ThreadID : TThreadID; Const sender, logtext : AnsiString);
  35. protected
  36. FWindow32X1,FWindow32Y1,FWindow32X2,FWindow32Y2: DWord;
  37. FLock : TCriticalSection;
  38. FPrivateKey : TECPrivateKey;
  39. FPoolMinerThread : TPoolMinerThread;
  40. FDeviceThreads : TList;
  41. FAppStartTime : TDateTime;
  42. procedure DoRun; override;
  43. public
  44. constructor Create(TheOwner: TComponent); override;
  45. destructor Destroy; override;
  46. procedure WriteHelp; virtual;
  47. end;
  48. Const
  49. CT_MINER_VERSION = {$IFDEF PRODUCTION}'4.0'{$ELSE}{$IFDEF TESTNET}'4.0 TESTNET'{$ELSE}ERROR{$ENDIF}{$ENDIF};
  50. CT_Line_DeviceStatus = 3;
  51. CT_Line_ConnectionStatus = 4;
  52. CT_Line_MinerValues = 7;
  53. CT_Line_MiningStatus = 10;
  54. CT_Line_LastFound = 12;
  55. CT_Line_Logs = 15;
  56. CT_MaxLogs = 10;
  57. CT_OpenCL_FileName = 'pascalsha.cl';
  58. { TPascalMinerApp }
  59. procedure TPascalMinerApp.ShowGPUDrivers;
  60. Var i,j,n : Integer;
  61. dev : TDCLDevice;
  62. begin
  63. n := 0;
  64. If Not TGPUDriver.GPUDriver.HasOpenCL then WriteLn('No GPU driver found')
  65. else begin
  66. Writeln('');
  67. WriteLn('** Platforms (Total ',TGPUDriver.GPUDriver.Platforms.PlatformCount,')');
  68. for i:=0 to TGPUDriver.GPUDriver.Platforms.PlatformCount-1 do begin
  69. WriteLn('Platform ',i,' Name:',Trim(TGPUDriver.GPUDriver.Platforms.Platforms[i]^.Name),
  70. ' Version:',Trim(TGPUDriver.GPUDriver.Platforms.Platforms[i]^.Version),
  71. ' Vendor:',Trim(TGPUDriver.GPUDriver.Platforms.Platforms[i]^.Vendor),
  72. ' CPU''s:',TGPUDriver.GPUDriver.Platforms.Platforms[i]^.CPUCount,
  73. ' GPU''s:',TGPUDriver.GPUDriver.Platforms.Platforms[i]^.GPUCount,
  74. ' Devices: ',TGPUDriver.GPUDriver.Platforms.Platforms[i]^.DeviceCount
  75. );
  76. inc(n,TGPUDriver.GPUDriver.Platforms.Platforms[i]^.DeviceCount);
  77. end;
  78. Writeln('');
  79. Writeln('** Platforms and devices available: (Total ',n,')');
  80. for i:=0 to TGPUDriver.GPUDriver.Platforms.PlatformCount-1 do begin
  81. for j:=0 to TGPUDriver.GPUDriver.Platforms.Platforms[i]^.DeviceCount-1 do begin
  82. dev := TGPUDriver.GPUDriver.Platforms.Platforms[i]^.Devices[j]^;
  83. Writeln('-p ',i,' -d ',j,' Name:',Trim(dev.Name),' Compute Units:',dev.MaxComputeUnits,' Max Freq.:',dev.MaxClockFrequency);
  84. end;
  85. end;
  86. end;
  87. end;
  88. procedure TPascalMinerApp.OnConnectionStateChanged(Sender: TObject);
  89. Const CT_state : Array[boolean] of String = ('Disconnected','Connected');
  90. var i : Integer;
  91. s : String;
  92. begin
  93. If FPoolMinerThread.PoolMinerClient.PoolType=ptNone then s:='MINING'
  94. else s:='POOL MINING USER "'+FPoolMinerThread.PoolMinerClient.UserName+'"';
  95. If FPoolMinerThread.PoolMinerClient.Connected then begin
  96. WriteLine(CT_Line_ConnectionStatus,s + ' server: '+FPoolMinerThread.PoolMinerClient.ClientRemoteAddr);
  97. For i:=0 to FDeviceThreads.Count-1 do begin
  98. TCustomMinerDeviceThread(FDeviceThreads[i]).Paused:=false;
  99. end;
  100. end else begin
  101. For i:=0 to FDeviceThreads.Count-1 do begin
  102. TCustomMinerDeviceThread(FDeviceThreads[i]).Paused:=true;
  103. end;
  104. WriteLine(CT_Line_ConnectionStatus,'** NOT CONNECTED '+s + ' Connecting to '+FPoolMinerThread.PoolMinerClient.ClientRemoteAddr);
  105. end;
  106. end;
  107. procedure TPascalMinerApp.OnDeviceStateChanged(Sender: TObject);
  108. Var i : Integer;
  109. s : String;
  110. begin
  111. If Sender is TCustomMinerDeviceThread then begin
  112. If TCustomMinerDeviceThread(Sender).IsMining then WriteLine(CT_Line_DeviceStatus,'') // clear line
  113. else WriteLine(CT_Line_DeviceStatus,'*** Not mining ***');
  114. end;
  115. end;
  116. procedure TPascalMinerApp.OnMinerValuesChanged(Sender: TObject);
  117. begin
  118. If Sender is TCustomMinerDeviceThread then begin
  119. If TCustomMinerDeviceThread(Sender).MinerValuesForWork.block>0 then begin
  120. WriteLine(CT_Line_MinerValues,Format('Current block: %d Wallet Name: "%s" Target: %s',
  121. [TCustomMinerDeviceThread(Sender).MinerValuesForWork.block,
  122. FPoolMinerThread.GlobalMinerValuesForWork.payload_start.ToPrintable,
  123. IntToHex(TCustomMinerDeviceThread(Sender).MinerValuesForWork.target,8)
  124. ]));
  125. end;
  126. end;
  127. end;
  128. procedure TPascalMinerApp.OnFoundNOnce(Sender: TCustomMinerDeviceThread; Timestamp, nOnce: Cardinal);
  129. begin
  130. WriteLine(CT_Line_LastFound + FDeviceThreads.Count,FormatDateTime('hh:nn:ss',now)+' Block:'+IntToStr(Sender.MinerValuesForWork.block)+' NOnce:'+Inttostr(nOnce)+
  131. ' Timestamp:'+inttostr(Timestamp)+' Miner:'+Sender.MinerValuesForWork.payload_start.ToPrintable);
  132. end;
  133. procedure TPascalMinerApp.WriteLine(nline: Integer; txt: String);
  134. Var i : Integer;
  135. begin
  136. FLock.Acquire;
  137. try
  138. i := length(txt);
  139. if i<=(FWindow32X2-FWindow32X1+1) then begin
  140. setlength(txt,FWindow32X2-FWindow32X1+1);
  141. fillchar(txt[i+1],FWindow32X2-FWindow32X1+1-i,' ');
  142. end else begin
  143. txt := copy(txt,1,FWindow32X2-FWindow32X1+1);
  144. end;
  145. if (nline<=(FWindow32Y2-FWindow32Y1)) then begin
  146. GotoXY(FWindow32X1,nline);
  147. write(txt);
  148. end;
  149. finally
  150. FLock.Release;
  151. end;
  152. end;
  153. procedure TPascalMinerApp.OnInThreadNewLog(logtype: TLogType; Time: TDateTime;
  154. ThreadID: TThreadID; const sender, logtext: AnsiString);
  155. var msg : String;
  156. i,nline : Integer;
  157. begin
  158. If logtype=ltdebug then exit;
  159. FLock.Acquire;
  160. try
  161. msg := formatdatetime('hh:nn:ss',now)+' '+CT_LogType[logtype]+' '+logtext;
  162. // TODO - test logtype is properly casted/stored/retrieved/accessed.
  163. // Confirm casting doesn't lose bits in 32/64 bit archs
  164. // OLD: FLastLogs.AddObject(msg,TObject(PtrInt(logtype)));
  165. FLastLogs.AddObject(msg,Pointer(logtype));
  166. i := FLastLogs.Count-CT_MaxLogs;
  167. if (i<0) then i:=0;
  168. nline := CT_Line_Logs+FDeviceThreads.Count;
  169. while (i<FLastLogs.Count) do begin
  170. WriteLine(nline,FLastLogs[i]);
  171. inc(nline); inc(i);
  172. end;
  173. if FLastLogs.Count>(CT_MaxLogs*2) then begin
  174. for i:=1 to CT_MaxLogs do FLastLogs.Delete(0);
  175. end;
  176. Finally
  177. FLock.Release;
  178. end;
  179. end;
  180. procedure TPascalMinerApp.DoRun;
  181. var
  182. ErrorMsg: String;
  183. s : String;
  184. nsarr : TNodeServerAddressArray;
  185. Function AddMiners : Boolean;
  186. var p,d,c,i : Integer;
  187. strl : TStringList;
  188. devt : TCustomMinerDeviceThread;
  189. begin
  190. Result := false;
  191. if (Not HasOption('p','platform')) And (Not HasOption('d','device')) And (Not HasOption('c','cpu')) then begin
  192. Writeln('Need to specify -p X and -d Y for GPU mining or -c N for CPU mining. See -h for more info');
  193. ShowGPUDrivers;
  194. Terminate;
  195. Exit;
  196. end;
  197. if HasOption('c','cpu') then begin
  198. c := StrToIntDef(GetOptionValue('c','cpu'),-1);
  199. if (c<=0) or (c>TCPUTool.GetLogicalCPUCount()) then begin
  200. WriteLn('Invalid cpu value ',c,'. Valid values: 1..',TCPUTool.GetLogicalCPUCount());
  201. Terminate;
  202. exit;
  203. end;
  204. devt:= TCPUDeviceThread.Create(FPoolMinerThread,CT_TMinerValuesForWork_NULL);
  205. devt.OnStateChanged:=OnDeviceStateChanged;
  206. devt.OnMinerValuesChanged:=OnMinerValuesChanged;
  207. devt.OnFoundNOnce:=OnFoundNOnce;
  208. TCPUDeviceThread(devt).CPUs:=c;
  209. devt.Paused:=true;
  210. FDeviceThreads.Add(devt);
  211. end else begin
  212. p := StrToIntDef(GetOptionValue('p','platform'),-1);
  213. d := StrToIntDef(GetOptionValue('d','device'),-1);
  214. if (p<0) or (p>=TGPUDriver.GPUDriver.Platforms.PlatformCount) then begin
  215. WriteLn('Invalid Platform ',p,'. Valid values: 0..',TGPUDriver.GPUDriver.Platforms.PlatformCount-1);
  216. Terminate;
  217. exit;
  218. end;
  219. strl := TStringList.Create;
  220. try
  221. if (d<0) then begin
  222. // Is a value separated by commas?
  223. strl.Delimiter:=',';
  224. strl.DelimitedText:=GetOptionValue('d','device');
  225. end else strl.Text:=inttostr(d);
  226. for i:=0 to strl.Count-1 do begin
  227. d := StrToIntDef(strl[i],-1);
  228. if (d<0) or (d>=TGPUDriver.GPUDriver.Platforms.Platforms[p]^.DeviceCount) then begin
  229. WriteLn('Invalid device ',d,'. Valid values: 0..',TGPUDriver.GPUDriver.Platforms.Platforms[p]^.DeviceCount-1);
  230. Terminate;
  231. exit;
  232. end;
  233. //
  234. devt := TGPUDeviceThread.Create(FPoolMinerThread,CT_TMinerValuesForWork_NULL);
  235. devt.OnStateChanged:=OnDeviceStateChanged;
  236. devt.OnMinerValuesChanged:=OnMinerValuesChanged;
  237. devt.OnFoundNOnce:=OnFoundNOnce;
  238. TGPUDeviceThread(devt).Platform:=p;
  239. TGPUDeviceThread(devt).Device:=d;
  240. TGPUDeviceThread(devt).ProgramFileName:=ExtractFileDir(ExeName)+PathDelim+CT_OpenCL_FileName;
  241. devt.Paused:=true;
  242. FDeviceThreads.Add(devt);
  243. end;
  244. finally
  245. strl.Free;
  246. end;
  247. end;
  248. Result := true;
  249. end;
  250. Procedure DoWaitAndLog;
  251. Var tc : TTickCount;
  252. gs,ms : TMinerStats;
  253. hrReal,hrHashing, glhrHashing, glhrReal : Real;
  254. i : Integer;
  255. devt : TCustomMinerDeviceThread;
  256. s : String;
  257. kpressed : Char;
  258. Begin
  259. tc := TPlatform.GetTickCount;
  260. repeat
  261. If FPoolMinerThread.PoolMinerClient.Connected then begin
  262. for i:=0 to FDeviceThreads.Count-1 do begin
  263. TCustomMinerDeviceThread(FDeviceThreads[i]).Paused:=false;
  264. end;
  265. end;
  266. while (Not Terminated) do begin
  267. sleep(100);
  268. If TPlatform.GetElapsedMilliseconds(tc)>1000 then begin
  269. tc := TPlatform.GetTickCount;
  270. For i:=0 to FDeviceThreads.Count-1 do begin
  271. devt := TCustomMinerDeviceThread(FDeviceThreads[i]);
  272. ms := devt.DeviceStats;
  273. if ((devt.MinerValuesForWork.version>=CT_PROTOCOL_4) AND (CT_ACTIVATE_RANDOMHASH_V4)) then begin
  274. if ms.WorkingMillisecondsHashing>0 then hrHashing := (((ms.RoundsCount / (ms.WorkingMillisecondsHashing/1000))))
  275. else hrHashing := 0;
  276. gs := devt.GlobalDeviceStats;
  277. If ms.RoundsCount>0 then begin
  278. s := FormatDateTime('hh:nn:ss',now)+Format(' Miner:"%s" at %0.1f H/s - Rounds: %0.2f K Found: %d',[devt.MinerValuesForWork.payload_start.ToString,hrHashing, gs.RoundsCount/1000, gs.WinsCount]);
  279. If (gs.Invalids>0) then s := s +' '+inttostr(gs.Invalids)+' ERRORS!';
  280. WriteLine(CT_Line_MiningStatus+i,s);
  281. end else begin
  282. If gs.RoundsCount>0 then begin
  283. s := FormatDateTime('hh:nn:ss',now)+Format(' Miner:"%s" **NOT MINING** - Rounds: %0.2f K Found: %d',[devt.MinerValuesForWork.payload_start.ToString,gs.RoundsCount/1000, gs.WinsCount]);
  284. If (gs.Invalids>0) then s := s +' '+inttostr(gs.Invalids)+' ERRORS!';
  285. end else begin
  286. s := FormatDateTime('hh:nn:ss',now)+' Not mining...';
  287. end;
  288. WriteLine(CT_Line_MiningStatus+i,s);
  289. end;
  290. end else begin
  291. if ms.WorkingMillisecondsHashing>0 then hrHashing := (((ms.RoundsCount DIV Int64(ms.WorkingMillisecondsHashing)))/(1000))
  292. else hrHashing := 0;
  293. gs := devt.GlobalDeviceStats;
  294. If ms.RoundsCount>0 then begin
  295. s := FormatDateTime('hh:nn:ss',now)+Format(' Miner:"%s" at %0.2f MH/s - Rounds: %0.2f G Found: %d',[devt.MinerValuesForWork.payload_start.ToString,hrHashing, gs.RoundsCount/1000000000, gs.WinsCount]);
  296. If (gs.Invalids>0) then s := s +' '+inttostr(gs.Invalids)+' ERRORS!';
  297. WriteLine(CT_Line_MiningStatus+i,s);
  298. end else begin
  299. If gs.RoundsCount>0 then begin
  300. s := FormatDateTime('hh:nn:ss',now)+Format(' Miner:"%s" **NOT MINING** - Rounds: %0.2f G Found: %d',[devt.MinerValuesForWork.payload_start.ToString,gs.RoundsCount/1000000000, gs.WinsCount]);
  301. If (gs.Invalids>0) then s := s +' '+inttostr(gs.Invalids)+' ERRORS!';
  302. end else begin
  303. s := FormatDateTime('hh:nn:ss',now)+' Not mining...';
  304. end;
  305. WriteLine(CT_Line_MiningStatus+i,s);
  306. end;
  307. end;
  308. end;
  309. WriteLine(CT_Line_LastFound+FDeviceThreads.Count-1,'MY VALID BLOCKS FOUND: '+IntToStr(gs.WinsCount) +' Working time: '+IntToStr(Trunc(now - FAppStartTime))+'d '+FormatDateTime('hh:nn:ss',Now-FAppStartTime) );
  310. end;
  311. If KeyPressed then begin
  312. kpressed := ReadKey;
  313. If kpressed in ['c','C','q','Q'] then begin
  314. TLog.NewLog(ltinfo,ClassName,'Finalizing by keypressing '+kpressed);
  315. WriteLine(CT_Line_Logs+FDeviceThreads.Count+CT_MaxLogs,'Finalizing...');
  316. terminate;
  317. end;
  318. end;
  319. end;
  320. until (Terminated) Or (FPoolMinerThread.Terminated);
  321. end;
  322. Procedure DoVisualprocess(minerName, UserName, Password : String);
  323. Var sc : tcrtcoord;
  324. Flog : TLog;
  325. devt : TCustomMinerDeviceThread;
  326. i : Integer;
  327. Begin
  328. FPoolMinerThread := TPoolMinerThread.Create(nsarr[0].ip,nsarr[0].port,FPrivateKey.PublicKey);
  329. try
  330. If (UserName<>'') then begin
  331. FPoolMinerThread.PoolMinerClient.PoolType:=ptIdentify;
  332. FPoolMinerThread.PoolMinerClient.UserName:=UserName;
  333. FPoolMinerThread.PoolMinerClient.Password:=Password;
  334. end;
  335. If Not AddMiners then exit;
  336. if HasOption('t','testmode') then begin
  337. i := StrToIntDef(GetOptionValue('t','testmode'),-1);
  338. if (i>=0) And (i<=32) then begin
  339. FPoolMinerThread.TestingPoWLeftBits:=i;
  340. end else begin
  341. WriteLn('Invalid bits for testing mode. value ',i,'. Valid values: 0..32 (0=No testing mode)');
  342. Terminate;
  343. exit;
  344. end;
  345. end;
  346. //
  347. cursoroff;
  348. try
  349. clrscr;
  350. FWindow32X1:=WindMinX;
  351. FWindow32X2:=WindMaxX;
  352. FWindow32Y1:=WindMinY;
  353. FWindow32Y2:=WindMaxY;
  354. WriteLine(1,'** PascalCoin miner ** Version: '+CT_MINER_VERSION);
  355. WriteLine(CT_Line_MinerValues-1,'MINER VALUES:');
  356. WriteLine(CT_Line_MiningStatus-1,'MINING STATUS:');
  357. WriteLine(CT_Line_LastFound+FDeviceThreads.Count-1,'MY VALID BLOCKS FOUND: 0');
  358. WriteLine(CT_Line_Logs+FDeviceThreads.Count-1,'LOGS:');
  359. FPoolMinerThread.MinerAddName:=minerName;
  360. WriteLine(CT_Line_MinerValues-1,'MINER VALUES: (My miner name="'+minerName+'")');
  361. FPoolMinerThread.OnConnectionStateChanged:=OnConnectionStateChanged;
  362. OnConnectionStateChanged(FPoolMinerThread);
  363. If (FDeviceThreads.Count)=1 then begin
  364. devt := TCustomMinerDeviceThread(FDeviceThreads[0]);
  365. WriteLine(2,devt.MinerDeviceName);
  366. end else begin
  367. WriteLine(2,'Mining using '+IntToStr(FDeviceThreads.Count)+' devices');
  368. end;
  369. Flog := TLog.Create(Nil);
  370. try
  371. Flog.OnInThreadNewLog:=OnInThreadNewLog;
  372. DoWaitAndLog;
  373. finally
  374. FLog.free;
  375. end;
  376. finally
  377. cursoron;
  378. end;
  379. Finally
  380. FPoolMinerThread.Terminate;
  381. FPoolMinerThread.Free;
  382. end;
  383. end;
  384. Var username,password : String;
  385. begin
  386. FLastLogs := TStringList.Create;
  387. FLock := TCriticalSection.Create;
  388. Try
  389. // quick check parameters
  390. ErrorMsg:=CheckOptions('hp:d:s::c:n::t:u::x::', 'help platform device server cpu minername testmode user pwd');
  391. if ErrorMsg<>'' then begin
  392. //ShowException(Exception.Create(ErrorMsg));
  393. WriteLn(ErrorMsg);
  394. Exit;
  395. end;
  396. // parse parameters
  397. if HasOption('h', 'help') then begin
  398. WriteHelp;
  399. Exit;
  400. end;
  401. if (Not HasOption('p','platform')) And (Not HasOption('d','device')) And (Not HasOption('c','cpu:')) then begin
  402. Writeln('Need to specify -p X and -d Y for GPU mining or -c N for CPU mining');
  403. Writeln('Execute ',ExtractFileName(ExeName),' -h for more info');
  404. ShowGPUDrivers;
  405. Exit;
  406. end;
  407. If Not FileExists(ExtractFileDir(ExeName)+PathDelim+CT_OpenCL_FileName) then begin
  408. Writeln('**********************');
  409. Writeln('OpenCL file not found!');
  410. Writeln('File: ',CT_OpenCL_FileName);
  411. Exit;
  412. end;
  413. If HasOption('s','server') then begin
  414. s := Trim(GetOptionValue('s','server'));
  415. if (s='') then s := 'localhost:'+inttostr(CT_JSONRPCMinerServer_Port);
  416. end else s:='';
  417. if (s='') then begin
  418. WriteLn('Input server name (default is localhost:',CT_JSONRPCMinerServer_Port,'):');
  419. Readln(s);
  420. trim(s);
  421. if (s='') then s := 'localhost:'+inttostr(CT_JSONRPCMinerServer_Port);
  422. end;
  423. if (pos(':',s)=0) then begin
  424. s := trim(s) + ':'+inttostr(CT_JSONRPCMinerServer_Port);
  425. end;
  426. TNode.DecodeIpStringToNodeServerAddressArray(s,nsarr);
  427. if (length(nsarr)<>1) then begin
  428. Writeln('INVALID SERVER VALUE ',s);
  429. WriteHelp;
  430. Exit;
  431. end;
  432. If (Not HasOption('n','minername')) then begin
  433. WriteLn('Input miner name that will be added to server miner name:');
  434. Readln(s);
  435. end else s:=GetOptionValue('n','minername');
  436. Try
  437. TCrypto.InitCrypto;
  438. Except
  439. On E:Exception do begin
  440. Writeln('**************************');
  441. Writeln('Error initializing library '+SSL_C_LIB+' (Not found or not valid)');
  442. Writeln('Error message: '+E.Message);
  443. Writeln('**************************');
  444. Exit;
  445. end;
  446. end;
  447. username:='';
  448. password:='';
  449. If (HasOption('u','user')) Or (HasOption('x','pwd')) then begin
  450. username:=trim(GetOptionValue('u','user'));
  451. password:=trim(GetOptionValue('x','pwd'));
  452. if (username='') then begin
  453. WriteLn('Input Pool username (or empty for non pool connection):');
  454. Readln(username);
  455. end;
  456. if (password='') And (username<>'') then begin
  457. WriteLn('Input Pool password for user ',username,':');
  458. Readln(password);
  459. end;
  460. end;
  461. FPrivateKey := TECPrivateKey.Create;
  462. Try
  463. FPrivateKey.GenerateRandomPrivateKey(CT_Default_EC_OpenSSL_NID);
  464. DoVisualprocess(s,username,password);
  465. finally
  466. FreeAndNil(FPrivateKey);
  467. end;
  468. finally
  469. FreeAndNil(FLock);
  470. FreeAndNil(FLastLogs);
  471. if not terminated then
  472. Terminate;
  473. end;
  474. end;
  475. constructor TPascalMinerApp.Create(TheOwner: TComponent);
  476. Var FLog : TLog;
  477. begin
  478. inherited Create(TheOwner);
  479. FDeviceThreads := TList.Create;
  480. StopOnException:=True;
  481. FAppStartTime := Now;
  482. FLog := TLog.Create(self);
  483. FLog.SaveTypes:=CT_TLogTypes_DEFAULT;
  484. FLog.FileName:=ExtractFileDir(ExeName)+PathDelim+'PascalCoinMiner.log';
  485. end;
  486. destructor TPascalMinerApp.Destroy;
  487. begin
  488. FreeAndNil(FDeviceThreads);
  489. inherited Destroy;
  490. end;
  491. procedure TPascalMinerApp.WriteHelp;
  492. begin
  493. { add your help code here }
  494. writeln('PascalCoin Miner - Version: ',CT_MINER_VERSION);
  495. writeln('Usage: ', ExtractFileName(ExeName), ' -h -s S -p X -d Y -c N -n MYNAME');
  496. writeln(' -h for help');
  497. writeln(' -s S (S is PascalCoin server:port where default value is localhost:',CT_JSONRPCMinerServer_Port,')');
  498. writeln(' -p X (X is GPU platform)');
  499. writeln(' -d Y (Y is GPU device for platform)');
  500. writeln(' Y can be multiple devices. Example -d 0,2,3 Will use devices 0, 2 and 3');
  501. writeln(' -c N (For CPU mining, where N is CPU''s to use. Activating this disable GPU mining)');
  502. writeln(' -n MYNAME (Will add MYNAME value to miner name assigned by server)');
  503. writeln(' ** POOL IDENTIFICATION PROTOCOL **');
  504. writeln(' (Not needed for PascalCoin core, only some third party pools)');
  505. writeln(' -u USERNAME');
  506. writeln(' -x PASSWORD');
  507. writeln('');
  508. writeln('Basic example GPU mining over multiple devices: ');
  509. writeln(' ',ExtractFileName(ExeName),' -p 0 -d 0,1,2,3 -s -n ABC');
  510. writeln(' (Devices 0,1,2,3 at server localhost:',CT_JSONRPCMinerServer_Port,' miner name ABC)');
  511. writeln('');
  512. ShowGPUDrivers;
  513. end;
  514. var
  515. Application: TPascalMinerApp;
  516. begin
  517. Application:=TPascalMinerApp.Create(nil);
  518. Application.Title:='Pascal Miner';
  519. Application.Run;
  520. Application.Free;
  521. end.