-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMainFormUnit.pas
More file actions
229 lines (173 loc) · 6.45 KB
/
MainFormUnit.pas
File metadata and controls
229 lines (173 loc) · 6.45 KB
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
unit MainFormUnit;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ComCtrls, Generics.Collections,
System.Diagnostics, System.TimeSpan,
BlocksUnit, System.Actions, Vcl.ActnList, Vcl.PlatformDefaultStyleActnCtrls,
Vcl.ActnMan, Vcl.ToolWin, Vcl.ActnCtrls, Vcl.ActnMenus, Vcl.ExtCtrls,
CryptoVirtualTreeUnit;
type
TMainForm = class(TForm)
Memo1: TMemo;
Button2: TButton;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
fBlockFilePath: string;
aBlocks: TBlocks;
nblocks: uint64;
Stopwatch: TStopwatch;
TimeSpan: TTimeSpan;
protected
procedure StartingParsingBlockFiles(Sender: TObject);
procedure FinishedParsingBlockFiles(Sender: TObject);
procedure StartProcessFiles(const aBlockFiles: TList<String>);
// procedure EndFoundFileBlock(const aBlockFiles: TList<String>);
procedure BeforeProcessAFile(const aBlockFile: TBlockFile; var next: boolean);
procedure AfterProcessAFile(const aBlockFile: TBlockFile; var next: boolean);
procedure FoundMagicBlock(const aBlock: TBlockRecord; var findnext: boolean);
procedure BlockProcessStep(const aPos, aSize: int64);
procedure EndProcessBlockFile(const aBlockFile: TBlockFile);
public
constructor Create(Owner: TComponent); override;
end;
var
MainForm: TMainForm;
implementation
uses
datamodule, inifiles, dateutils, SeSHA256, System.Threading;
{$R *.dfm}
procedure TMainForm.BeforeProcessAFile(const aBlockFile: TBlockFile; var next: boolean);
begin
Memo1.Lines.Add(format(' Start process %s file %d of %d', [aBlockFile.aFileName, aBlockFile.aBlockNumber, aBlockFile.parent.Count]));
end;
procedure TMainForm.BlockProcessStep(const aPos, aSize: int64);
begin
if (aPos mod 25) = 0 then
Memo1.Lines.Add(IntToStr(aPos * 100 div aSize) + '%');
end;
procedure TMainForm.Button2Click(Sender: TObject);
begin
aBlocks.aBlocksDirectory := fBlockFilePath;
aBlocks.start;
end;
constructor TMainForm.Create(Owner: TComponent);
begin
inherited;
aBlocks := TBlocks.Create(true);
aBlocks.FreeOnTerminate := true;
// Starts and ends processing all .dat files
aBlocks.OnStartingParsingBlockfiles := StartingParsingBlockFiles;
aBlocks.OnFinishedParsingBlockFiles := FinishedParsingBlockFiles;
// Starts and ends process each .dat file
aBlocks.OnStartProcessFiles := StartProcessFiles;
// aBlocks.OnEndProcessBlockFile := EndProcessBlockFile;
// Stars and ends block
aBlocks.OnBeforeFileBlockProcess := BeforeProcessAFile;
aBlocks.OnAfterFileBlockProcessed := AfterProcessAFile;
aBlocks.OnMagicBlockFound := FoundMagicBlock;
aBlocks.OnBlockProcessStep := BlockProcessStep;
end;
{ procedure TMainForm.EndFoundFileBlock(const aBlockFiles: TList<String>);
var
nbsec: double;
begin
TimeSpan := Stopwatch.Elapsed;
nbsec := nblocks / TimeSpan.TotalSeconds;
Memo1.Lines.Add(nbsec.ToString);
aBlockFiles.Free;
end;
}
procedure TMainForm.FinishedParsingBlockFiles(Sender: TObject);
begin
Memo1.Lines.Add(' End parsing all files');
end;
procedure TMainForm.EndProcessBlockFile(const aBlockFile: TBlockFile);
begin
Memo1.Lines.Add(' End processing ' + aBlockFile.aFileName);
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
aFileDir: string;
aINIFile: TIniFile;
begin
// Create data directories
aFileDir := ExtractFilePath(Application.ExeName);
if DirectoryExists(aFileDir + '\data\') = false then
CreateDir(aFileDir + '\data\');
if DirectoryExists(aFileDir + '\btc') = false then
CreateDir(aFileDir + '\btc');
// Load settings from IniFile
aINIFile := TIniFile.Create(aFileDir + '\config.ini');
fBlockFilePath := aINIFile.ReadString('File', 'BlockFilePath', '');
aINIFile.Free;
end;
procedure TMainForm.AfterProcessAFile(const aBlockFile: TBlockFile; var next: boolean);
begin
Memo1.Lines.Add(' Processed ' + aBlockFile.aFileName);
next := true;
// next := false;
end;
procedure TMainForm.FoundMagicBlock(const aBlock: TBlockRecord; var findnext: boolean);
var
k, j, i: integer;
t: string;
begin
// Performance
inc(nblocks);
// exit;
if (nblocks <> 12 ) then exit;
Memo1.Lines.Add(' Hash: ' + aBlock.hash);
Memo1.Lines.Add(' ');
Memo1.Lines.Add(datetimetostr(Unixtodatetime(aBlock.header.time)) + ' UTC ' + ' Bits: ' + aBlock.header.DifficultyTarget.ToString + ' nonce: ' + aBlock.header.nonce.ToString);
Memo1.Lines.Add(' Hash: ' + aBlock.hash);
Memo1.Lines.Add(' Prev. block: ' + T32ToString(aBlock.header.aPreviousBlockHash));
Memo1.Lines.Add(' MerkleRoot: ' + T32ToString(aBlock.header.aMerkleRoot));
Memo1.Lines.Add(' Transactions ' + aBlock.transactions.Count.ToString);
Memo1.Lines.Add(' ');
for k := 0 to aBlock.transactions.Count - 1 do
begin
Memo1.Lines.Add(' Transacción ' + IntToStr(k));
Memo1.Lines.Add(' version ' + aBlock.transactions[k].version.ToString);
if aBlock.transactions[k].inputs.Count > 0 then
for j := 0 to aBlock.transactions[k].inputs.Count - 1 do
begin
Memo1.Lines.Add(format(' input %s', [T32ToString(aBlock.transactions[k].inputs[j].aTXID)]));
Memo1.Lines.Add(format(' %.8f BTC', [(aBlock.transactions[k].inputs[j].aVOUT / 100000000)]));
t := '';
for i := 0 to aBlock.transactions[k].inputs[j].CoinBaseLength - 1 do
begin
t := t + IntToHex(aBlock.transactions[k].inputs[j].CoinBase[i]);
end;
Memo1.Lines.Add(' coinbase: ' + t);
Memo1.Lines.Add(' ');
end;
if aBlock.transactions[k].outputs.Count > 0 then
for j := 0 to aBlock.transactions[k].outputs.Count - 1 do
begin
Memo1.Lines.Add(format(' output %.8f BTC', [aBlock.transactions[k].outputs[j].nValue / 100000000]));
t := '';
for i := 0 to aBlock.transactions[k].outputs[j].OutputScriptLength - 1 do
begin
t := t + IntToHex(aBlock.transactions[k].outputs[j].OutputScript[i]);
end;
Memo1.Lines.Add(' outputscript: ' + t);
Memo1.Lines.Add(' ');
end;
Memo1.Lines.Add(' ');
end;
end;
procedure TMainForm.StartingParsingBlockFiles(Sender: TObject);
begin
Memo1.Lines.Add('Start parsing component');
end;
procedure TMainForm.StartProcessFiles(const aBlockFiles: TList<String>);
begin
Memo1.Lines.Add(' Block files found to process ' + aBlockFiles.Count.ToString);
nblocks := 0;
Stopwatch := TStopwatch.StartNew;
end;
end.