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 1
/
nosoheaders.pas
278 lines (250 loc) · 6.97 KB
/
nosoheaders.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
unit NosoHeaders;
{
nosoHeaders 1.1
Jan 31th, 2024
Stand alone unit to control headers file.
}
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Nosodebug,nosocrypto, nosogeneral;
Type
ResumenData = Packed Record
block : integer;
blockhash : string[32];
SumHash : String[32];
end;
Procedure SetResumenHash();
function GetResumenHash():String;
Function SetHeadersFileName(Filename:String):Boolean;
Function CreateHeadersFile():boolean;
Function AddRecordToHeaders(BlockNumber:int64;BlockHash,SumHash:String):boolean;
Function RemoveHeadersLastRecord():Boolean;
Function GetHeadersHeigth():integer;
Function GetHeadersLastBlock():Integer;
Function GetHeadersAsMemStream(var LMs:TMemoryStream):int64;
Function SaveStreamAsHeaders(var LStream:TMemoryStream):Boolean;
Function LastHeadersString(FromBlock:integer):String;
var
FileResumen : file of ResumenData;
MyResumenHash : String = '';
ResumenFilename : string = {'NOSODATA'+DirectorySeparator+}'blchhead.nos';
CS_HeadersFile : TRTLCriticalSection;
IMPLEMENTATION
{$REGION Headers file access}
// Sets the hash value
Procedure SetResumenHash();
Begin
EnterCriticalSection(CS_HeadersFile);
MyResumenHash := HashMD5File(ResumenFilename);
LeaveCriticalSection(CS_HeadersFile);
End;
// Returns the hash of the file
function GetResumenHash():String;
Begin
EnterCriticalSection(CS_HeadersFile);
result := MyResumenHash;
LeaveCriticalSection(CS_HeadersFile);
End;
// Sets the headers file path and name
Function SetHeadersFileName(Filename:String):Boolean;
Begin
Result := true;
ResumenFilename := Filename;
assignfile(FileResumen,ResumenFilename);
if not Fileexists(ResumenFilename) then CreateEmptyFile(ResumenFilename);
SetResumenHash();
End;
// Creates a headers file. If it already exists, rewrite a new empty one.
Function CreateHeadersFile():boolean;
var
MyStream : TMemoryStream;
Begin
Result := True;
MyStream := TMemoryStream.Create;
EnterCriticalSection(CS_HeadersFile);
TRY
MYStream.SaveToFile(ResumenFilename);
EXCEPT ON E:EXCEPTION DO
begin
Result := false;
ToDeepDeb('NosoHeaders,CreateHeadersFile,'+E.Message);
end;
END;
LeaveCriticalSection(CS_HeadersFile);
MyStream.Free;
End;
Function AddRecordToHeaders(BlockNumber:int64;BlockHash,SumHash:String):boolean;
var
NewData : ResumenData;
Opened : boolean = false;
PorperlyClosed : boolean = false;
Begin
Result := true;
NewData := Default(ResumenData);
NewData.block := BlockNumber;
NewData.blockhash := BlockHash;
NewData.SumHash := SumHash;
filemode := 2;
EnterCriticalSection(CS_HeadersFile);
TRY
reset(FileResumen);
Opened := true;
seek(fileResumen,filesize(fileResumen));
write(fileResumen,NewData);
closefile(FileResumen);
PorperlyClosed := true;
EXCEPT ON E:EXCEPTION DO
begin
ToDeepDeb('NosoHeaders,AddRecordToHeaders,'+E.Message);
Result := false;
end;
END;
if ( (opened) and (not PorperlyClosed) ) then closefile(FileResumen);
LeaveCriticalSection(CS_HeadersFile);
End;
Function RemoveHeadersLastRecord():Boolean;
var
Opened : boolean = false;
PorperlyClosed : boolean = false;
Begin
Result := true;
filemode := 2;
EnterCriticalSection(CS_HeadersFile);
TRY
reset(FileResumen);
Opened := true;
seek(fileResumen,filesize(fileResumen)-1);
truncate(fileResumen);
closefile(FileResumen);
PorperlyClosed := true;
EXCEPT ON E:EXCEPTION DO
begin
ToDeepDeb('NosoHeaders,RemoveHeadersLastRecord,'+E.Message);
Result := false;
end;
END;
if ( (opened) and (not PorperlyClosed) ) then closefile(FileResumen);
LeaveCriticalSection(CS_HeadersFile);
End;
Function GetHeadersHeigth():integer;
var
Opened : boolean = false;
PorperlyClosed : boolean = false;
Begin
Result := -1;
EnterCriticalSection(CS_HeadersFile);
TRY
reset(FileResumen);
Opened := true;
Result := filesize(fileResumen)-1;
closefile(FileResumen);
PorperlyClosed := true;
EXCEPT on E:Exception do
begin
ToDeepDeb('NosoHeaders,GetHeadersHeigth,'+E.Message);
end;
END;
if ( (opened) and (not PorperlyClosed) ) then closefile(FileResumen);
LeaveCriticalSection(CS_HeadersFile);
End;
// Returns the block number of the last record on headers
Function GetHeadersLastBlock():Integer;
var
ThisData : ResumenData;
Opened : boolean = false;
PorperlyClosed : boolean = false;
Begin
Result := 0;
ThisData := Default(ResumenData);
EnterCriticalSection(CS_HeadersFile);
TRY
reset(FileResumen);
Opened := true;
if filesize(FileResumen)>0 then
begin
seek(fileResumen,filesize(FileResumen)-1);
Read(fileResumen,ThisData);
result := ThisData.block;
end;
CloseFile(FileResumen);
PorperlyClosed := true;
EXCEPT on E:Exception do
begin
ToDeepDeb('NosoHeaders,GetHeadersLastBlock,'+E.Message);
end;
END;
if ( (opened) and (not PorperlyClosed) ) then closefile(FileResumen);
LeaveCriticalSection(CS_HeadersFile);
End;
// Returns the headers file as a STREAM
Function GetHeadersAsMemStream(var LMs:TMemoryStream):int64;
Begin
Result := 0;
BeginPerformance('GetHeadersAsMemStream');
EnterCriticalSection(CS_HeadersFile);
TRY
LMs.LoadFromFile(ResumenFilename);
result:= LMs.Size;
LMs.Position:=0;
EXCEPT ON E:Exception do
begin
ToDeepDeb('NosoHeaders,GetHeadersAsMemStream,'+E.Message);
end;
END;
LeaveCriticalSection(CS_HeadersFile);
EndPerformance('GetHeadersAsMemStream');
End;
// Save a provided stream as the headers file
Function SaveStreamAsHeaders(var LStream:TMemoryStream):Boolean;
Begin
result := false;
EnterCriticalSection(CS_HeadersFile);
TRY
LStream.SaveToFile(ResumenFilename);
Result := true;
EXCEPT ON E:Exception do
begin
ToDeepDeb('NosoHeaders,SaveStreamAsHeaders,'+E.Message);
end;
END{Try};
LeaveCriticalSection(CS_HeadersFile);
End;
// Returns the string for headers updates
Function LastHeadersString(FromBlock:integer):String;
var
ThisData : ResumenData;
Opened : boolean = false;
PorperlyClosed : boolean = false;
Begin
result := '';
if FromBlock<GetHeadersLastBlock-1008 then exit;
EnterCriticalSection(CS_HeadersFile);
TRY
reset(FileResumen);
Opened := true;
ThisData := Default(ResumenData);
seek(fileResumen,FromBlock-100);
While not Eof(fileResumen) do
begin
Read(fileResumen,ThisData);
Result := Result+ThisData.block.ToString+':'+ThisData.blockhash+':'+ThisData.SumHash+' ';
end;
closefile(FileResumen);
PorperlyClosed := true;
EXCEPT on E:Exception do
begin
ToDeepDeb('NosoHeaders,LastHeadersString,'+E.Message);
end;
END;
//if ( (opened) and (not PorperlyClosed) ) then closefile(FileResumen);
LeaveCriticalSection(CS_HeadersFile);
Result := Trim(Result);
End;
{$ENDREGION}
INITIALIZATION
InitCriticalSection(CS_HeadersFile);
assignfile(FileResumen,ResumenFilename);
FINALIZATION
DoneCriticalSection(CS_HeadersFile);
END.