This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
nl_disk.pas
459 lines (417 loc) · 11.6 KB
/
nl_disk.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
unit nl_disk;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, nl_data, nl_cripto, nl_language, dialogs, nl_functions, fileutil,
Zipper, splashform,nosonosocfg,nosodebug;
Procedure VerifyFilesStructure();
Procedure CreateNewWallet();
Procedure LoadWallet();
Procedure SaveWallet();
Procedure CreateTrashWallet();
Procedure MoveAddressToTrash(Address:WalletData);
Procedure SaveOptions();
Procedure LoadOptions();
Procedure CreateSumary();
Procedure LoadSumary();
Procedure UnZipSumary();
Procedure CreateMNsFile();
Procedure LoadMNsFromFile();
Procedure FillArrayNodes();
Procedure SaveMnsToFile(LineText:String);
Procedure SaveCFGToFile(LineText:String);
Function GetVerificators(LineText:String):String;
// GVTs
Procedure CreateGVTsFile();
Procedure LoadGVTsFile();
// Labels
Procedure CreateLabelsFile();
Procedure LoadLabelsFile();
Procedure SaveLabelsToDisk();
// Start log
Procedure CreateStartLog();
Procedure ToStartLog(TextLine:String);
implementation
// verify the app files structure
Procedure VerifyFilesStructure();
Begin
if not directoryexists(WalletDirectory) then CreateDir(WalletDirectory);
if not directoryexists(DataDirectory) then CreateDir(DataDirectory);
if not FileExists(WalletFileName) then CreateNewWallet() else LoadWallet();
if not FileExists(TrashFilename) then CreateTrashWallet();
if not FileExists(OptionsFilename) then SaveOptions() else LoadOptions();
if not FileExists(SumaryFilename) then CreateSumary() else LoadSumary();
if not FileExists(MNsFilename) then CreateMNsFile() else LoadMNsFromFile();
if not FileExists(GVTFilename) then CreateGVTsFile() else LoadGVTsFile();
if not FileExists(LabelsFilename) then CreateLabelsFile() else LoadLabelsFile();
CreateStartLog();
SetCFGFilename(DataDirectory+CFGFilename);
End;
//******************************************************************************
// WALLET
//******************************************************************************
// Creates a new wallet
Procedure CreateNewWallet();
Begin
if not fileexists(WalletFileName) then
begin
setlength(ARRAY_Addresses,1);
TRY
assignfile(FILE_Wallet,WalletFileName);
rewrite(FILE_Wallet);
ARRAY_Addresses[0] := CreateNewAddress();
seek(FILE_Wallet,0);
write(FILE_Wallet,ARRAY_Addresses[0]);
closefile(FILE_Wallet);
EXCEPT on E:Exception do
begin
ToLog('main',format(rsError0001,[e.Message]));
end;
END;
end;
End;
// Loads the wallet from disk
Procedure LoadWallet();
var
counter : integer = 0;
Begin
TRY
assignfile(FILE_Wallet,WalletFileName);
reset(FILE_Wallet);
setlength(ARRAY_Addresses,filesize(FILE_Wallet));
for counter := 0 to length(ARRAY_Addresses)-1 do
begin
seek(FILE_Wallet,counter);
Read(FILE_Wallet,ARRAY_Addresses[counter]);
ARRAY_Addresses[counter].Pending:=0;
end;
closefile(FILE_Wallet);
EXCEPT on E:Exception do
begin
ToLog('main',format(rsError0002,[e.Message]));
end;
END{Try};
End;
// Save wallet file to disk
Procedure SaveWallet();
var
Counter:integer;
Previous : int64;
Begin
copyfile (WalletFileName,WalletFileName+'.bak');
assignfile(FILE_Wallet,WalletFileName);
reset(FILE_Wallet);
EnterCriticalSection(CS_ARRAY_Addresses);
TRY
For Counter := 0 to length(ARRAY_Addresses)-1 do
begin
seek(FILE_Wallet,Counter);
Previous := ARRAY_Addresses[Counter].Pending;
ARRAY_Addresses[Counter].Pending := 0;
write(FILE_Wallet,ARRAY_Addresses[Counter]);
ARRAY_Addresses[Counter].Pending := Previous;
end;
Truncate(FILE_Wallet);
EXCEPT on E:Exception do
begin
ToLog('main',Format(rsError0004,[E.Message]))
end;
END{Try};
LeaveCriticalSection(CS_ARRAY_Addresses);
SAVE_Wallet := false;
closefile(FILE_Wallet);
End;
// Creates the trash wallet file
Procedure CreateTrashWallet();
Begin
assignfile(FILE_Trash,TrashFilename);
rewrite(FILE_Trash);
closefile(FILE_Trash);
End;
// Moves an address to the trash wallet
Procedure MoveAddressToTrash(Address:WalletData);
Begin
assignfile(FILE_Trash,TrashFilename);
reset(FILE_Trash);
seek(FILE_Trash,FileSize(FILE_Trash));
write(FILE_Trash,Address);
closefile(FILE_Trash);
End;
//******************************************************************************
// OPTIONS
//******************************************************************************
Procedure SaveOptions();
Begin
TRY
Assignfile(FILE_Options, OptionsFilename);
rewrite(FILE_Options);
writeln(FILE_Options,'block '+WO_LastBlock.ToString);
writeln(FILE_Options,'sumary '+WO_LastSumary);
writeln(FILE_Options,'multisend '+BoolToStr(WO_MultiSend,true));
writeln(FILE_Options,'liqpoolhost '+LiqPoolHost);
writeln(FILE_Options,'liqpoolport '+LiqPoolPort.ToString);
CloseFile(FILE_Options);
EXCEPT on E:Exception do
begin
end;
END{Try};
End;
// Load the options file
Procedure LoadOptions();
var
LLine : string;
Thisapp : appData;
Begin
TRY
Assignfile(FILE_Options, OptionsFilename);
reset(FILE_Options);
while not eof(FILE_Options) do
begin
readln(FILE_Options,LLine);
if parameter(LLine,0) ='block' then WO_LastBlock:=Parameter(LLine,1).ToInteger();
if parameter(LLine,0) ='sumary' then WO_LastSumary:=Parameter(LLine,1);
if parameter(LLine,0) ='multisend' then WO_Multisend:=StrToBool(Parameter(LLine,1));
if parameter(LLine,0) ='liqpoolhost' then LiqPoolHost:=Parameter(LLine,1);
if parameter(LLine,0) ='liqpoolport' then LiqPoolPort:=StrToIntDef(Parameter(LLine,1),0);
end;
CloseFile(FILE_Options);
EXCEPT on E:Exception do
begin
end;
END{Try};
End;
//******************************************************************************
// SUMARY
//******************************************************************************
// Creates a empty sumary file
Procedure CreateSumary();
Begin
SetLength(ARRAY_Sumary,0);
assignfile(FILE_Sumary,SumaryFilename);
Rewrite(FILE_Sumary);
CloseFile(FILE_Sumary);
End;
// Loads a sumary to memory
Procedure LoadSumary();
var
Counter : integer = 0;
Begin
TRY
SetLength(ARRAY_Sumary,0);
assignfile(FILE_Sumary,SumaryFilename);
Reset(FILE_Sumary);
SetLength(ARRAY_Sumary,fileSize(FILE_Sumary));
for Counter := 0 to Filesize(FILE_Sumary)-1 do
Begin
seek(FILE_Sumary,Counter);
read(FILE_Sumary,ARRAY_Sumary[Counter]);
end;
CloseFile(FILE_Sumary);
UpdateWalletFromSumary();
EXCEPT on E:Exception do
begin
end
END{Try};
End;
// Unzip the received sumary file
Procedure UnZipSumary();
var
UnZipper: TUnZipper;
Begin
TRY
UnZipper := TUnZipper.Create;
TRY
UnZipper.FileName := ZipSumaryFilename;
UnZipper.OutputPath := '';
UnZipper.Examine;
UnZipper.UnZipAllFiles;
FINALLY
UnZipper.Free;
END;
EXCEPT on E:Exception do
begin
tolog ('main','Error unzipping block file');
end;
END{Try};
End;
//******************************************************************************
// MASTERNODES
//******************************************************************************
// Creates a new default empty masternodes file
Procedure CreateMNsFile();
Begin
assignfile(FILE_MNs,MNsFilename);
Rewrite(FILE_MNs);
write(FILE_MNs,STR_SeedNodes);
CloseFile(FILE_MNs);
LoadMNsFromFile;
End;
Procedure LoadMNsFromFile();
var
LineText : String;
Begin
assignfile(FILE_MNs,MNsFilename);
Reset(FILE_MNs);
ReadLn(FILE_MNs,LineText);
CloseFile(FILE_MNs);
SetMasternodes(LineText);
FillArrayNodes;
End;
Procedure FillArrayNodes();
Begin
if WO_UseSeedNodes then LoadSeedNodes(Parameter(NosoCFGString,1))
else LoadSeedNodes(GetVerificators(GetMasterNodes));
End;
Procedure SaveMnsToFile(LineText:String);
Begin
assignfile(FILE_MNs,MNsFilename);
Rewrite(FILE_MNs);
SetMasternodes(LineText);
write(FILE_MNs,LineText);
CloseFile(FILE_MNs);
End;
Procedure SaveCFGToFile(LineText:String);
Begin
assignfile(FILE_CFG,CFGFilename);
Rewrite(FILE_CFG);
write(FILE_CFG,LineText);
CloseFile(FILE_CFG);
End;
Function GetVerificators(LineText:String):String;
var
counter : integer = 1;
count2 : integer;
ThisParam : string;
ThisMN : TMnData;
Ip : string;
Port : integer;
Address : string;
Count : integer;
ArrNodes : array of TMnData;
Added : boolean;
VersCount : integer;
Begin
Result := MasternodesLastBlock.ToString+' ';
SetLEngth(ArrNodes,0);
repeat
thisParam := Parameter(LineText,counter);
if ThisParam <>'' then
begin
Added := false;
ThisParam := StringReplace(ThisParam,':',' ',[rfReplaceAll, rfIgnoreCase]);
ThisParam := StringReplace(ThisParam,';',' ',[rfReplaceAll, rfIgnoreCase]);
ThisMN.Ip := Parameter(ThisParam,0);
ThisMN.Port := StrToIntDef(Parameter(ThisParam,1),8080);
ThisMN.Address := Parameter(ThisParam,2);
ThisMN.Count := StrToIntDef(Parameter(ThisParam,3),1);
if Length(ArrNodes) = 0 then Insert(ThisMN,ArrNodes,0)
else
begin
for count2 := 0 to length(ArrNodes)-1 do
begin
if ThisMN.count > ArrNodes[count2].count then
begin
Insert(ThisMN,ArrNodes,count2);
Added := true;
Break;
end;
end;
if not Added then Insert(ThisMN,ArrNodes,length(ArrNodes));
end;
end;
Inc(Counter);
until thisParam = '';
VersCount := (length(ArrNodes) div 10)+3;
Delete(ArrNodes,VersCount,Length(ArrNodes));
for counter := 0 to length(ArrNodes)-1 do
begin
Result := Result+ ArrNodes[counter].ip+';'+IntToStr(ArrNodes[counter].port)+':'+ArrNodes[counter].address+':'+
IntToStr(ArrNodes[counter].count)+' ';
end;
Result := Trim(Result);
End;
//******************************************************************************
// GVTs
//******************************************************************************
// Creates a new GVTs File
Procedure CreateGVTsFile();
Begin
assignfile(FILE_GVTs,GVTFilename);
Rewrite(FILE_GVTs);
CloseFile(FILE_GVTs);
LoadGVTsFile();
End;
// Load GVTS from file
Procedure LoadGVTsFile();
var
LineText : String;
Counter : integer;
ThisGVT : TGVT;
Begin
assignfile(FILE_GVTs,GVTFilename);
Reset(FILE_GVTs);
SetLength(ARRAY_GVTs,Filesize(FILE_GVTs));
For Counter := 0 to Filesize(FILE_GVTs)-1 do
begin
seek(FILE_GVTs,counter);
Read(FILE_GVTs,ThisGVT);
ARRAY_GVTs[counter] := ThisGVT;
end;
CloseFile(FILE_GVTs);
End;
//******************************************************************************
// LABELS
//******************************************************************************
Procedure CreateLabelsFile();
Begin
assignfile(FILE_Labels,LabelsFilename);
Rewrite(FILE_Labels);
CloseFile(FILE_Labels);
LoadGVTsFile();
End;
Procedure LoadLabelsFile();
var
ThisLine : string;
ThisData : TypeLabel;
Begin
assignfile(FILE_Labels,LabelsFilename);
Reset(FILE_Labels);
SetLength(ARRAY_Labels,0);
While not eof(FILE_Labels) do
begin
ReadLn(FILE_Labels,ThisLine);
ThisData.Address:=Parameter(ThisLine,0);
ThisData.LabelSt:=Parameter(ThisLine,1);
Insert(ThisData,ARRAY_Labels,LEngth(ARRAY_Labels));
end;
CloseFile(FILE_Labels);
End;
Procedure SaveLabelsToDisk();
var
Counter : integer;
Begin
assignfile(FILE_Labels,LabelsFilename);
Rewrite(FILE_Labels);
For counter := 0 to length(ARRAY_Labels)-1 do
writeln(FILE_Labels,ARRAY_Labels[counter].Address+' '+ARRAY_Labels[counter].LabelSt);
CloseFile(FILE_Labels);
End;
//******************************************************************************
// START LOG
//******************************************************************************
Procedure CreateStartLog();
Begin
assignfile(FILE_StartLog,StartLogFilename);
Rewrite(FILE_StartLog);
CloseFile(FILE_StartLog);
End;
Procedure ToStartLog(TextLine:String);
Begin
assignfile(FILE_StartLog,StartLogFilename);
Append(FILE_StartLog);
WriteLn(FILE_StartLog,TextLine);
CloseFile(FILE_StartLog);
form4.LabelSplash.Caption:=form4.LabelSplash.Caption+TextLine+slinebreak;
form4.LabelSplash.Update;
End;
END. // END UNIT