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 17
/
mpcoin.pas
478 lines (447 loc) · 15.5 KB
/
mpcoin.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
unit mpCoin;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,MasterPaskalForm,mpgui,Clipbrd, strutils, nosodebug,nosogeneral,
nosocrypto, nosounit,nosotime,nosopsos,nosowallcon, nosoblock,nosonetwork;
function GetAddressAvailable(address:string):int64;
function GetAddressPendingPays(Address:string):int64;
function GetAddressIncomingpays(Address:string):int64;
//function TranxAlreadyPending(TrxHash:string):boolean;
//function TrxExistsInLastBlock(trfrhash:String):boolean;
//function GetLastPendingTime():int64;
//function AddArrayPoolTXs(order:TOrderData):boolean;
Procedure VerifyIfPendingIsMine(order:Torderdata);
function AddressAlreadyCustomized(address:string):boolean;
Function GVTAlreadyTransfered(NumberStr:String):boolean;
function AliasAlreadyExists(Addalias:string):boolean;
//function GetFee(monto:int64):Int64;
Function SendFundsFromAddress(Origen, Destino:String; monto, comision:int64; reference,
ordertime:String;linea:integer):TOrderData;
Procedure CheckForMyPending();
//function GetMaximunToSend(monto:int64):int64;
function GetCurrentStatus(mode:integer):String;
function GetBlockHeaders(numberblock:integer):string;
function ValidRPCHost(hoststr:string):boolean;
function PendingRawInfo(ForRPC : boolean = true):String;
{
Function GetPendingCount():integer;
Procedure ClearAllPending();
}
implementation
Uses
mpblock, Mpred, mpparser,mpdisk, mpProtocol;
function GetAddressAvailable(address:string):int64;
Begin
result := GetAddressBalanceIndexed(address)-GetAddressPendingPays(address);
End;
// Returns the balance an address already have committed to be paid.
function GetAddressPendingPays(Address:string):int64;
var
cont : integer;
CopyPendings : array of Torderdata;
Begin
Result := 0;
if Address = '' then exit;
if GetPendingCount>0 then
begin
EnterCriticalSection(CSPending);
SetLength(CopyPendings,0);
CopyPendings := copy(ArrayPoolTXs,0,length(ArrayPoolTXs));
LeaveCriticalSection(CSPending);
for cont := 0 to length(CopyPendings)-1 do
begin
if address = CopyPendings[cont].address then
result := result+CopyPendings[cont].AmmountFee+CopyPendings[cont].AmmountTrf;
end;
end;
if MyLastBlock >= Update050Block then
if IsLockedMN(Address) then Inc(Result,1050000000000);
End;
// Returns the pending incomings for the specified address**ONLY HASH ACCEPTED
function GetAddressIncomingpays(Address:string):int64;
var
cont : integer;
CopyPendings : array of Torderdata;
Begin
result := 0;
if GetPendingCount>0 then
begin
EnterCriticalSection(CSPending);
SetLength(CopyPendings,0);
CopyPendings := copy(ArrayPoolTXs,0,length(ArrayPoolTXs));
LeaveCriticalSection(CSPending);
for cont := 0 to length(CopyPendings)-1 do
begin
if address = ArrayPoolTXs[cont].receiver then
result := result+ArrayPoolTXs[cont].AmmountTrf;
end;
end;
End;
{
//Devuelve si una transaccion ya se encuentra pendiente
function TranxAlreadyPending(TrxHash:string):boolean;
var
cont : integer;
Begin
Result := false;
for cont := 0 to GetPendingCount-1 do
begin
if TrxHash = ArrayPoolTXs[cont].TrfrID then
begin
result := true;
break;
end;
end;
End;
}
{
// Devuelve si una transaccion existe en el ultimo bloque
function TrxExistsInLastBlock(trfrhash:String):boolean;
var
ArrayLastBlockTrxs : TBlockOrdersArray;
cont : integer;
Begin
Result := false;
ArrayLastBlockTrxs := Default(TBlockOrdersArray);
ArrayLastBlockTrxs := GetBlockTrxs(MyLastBlock);
for cont := 0 to length(ArrayLastBlockTrxs)-1 do
begin
if ArrayLastBlockTrxs[cont].TrfrID = trfrhash then
begin
result := true ;
break
end;
end;
SetLength(ArrayLastBlockTrxs,0);
End;
}
{
function GetLastPendingTime():int64;
Begin
result := 0;
EnterCriticalSection(CSPending);
if length(ArrayPoolTXs) > 0 then result := ArrayPoolTXs[length(ArrayPoolTXs)-1].TimeStamp;
LeaveCriticalSection(CSPending);
End;
}
{
// Añade la transaccion pendiente en su lugar
function AddArrayPoolTXs(order:TOrderData):boolean;
var
cont : integer = 0;
insertar : boolean = false;
resultado : integer = 0;
Begin
BeginPerformance('AddArrayPoolTXs');
//if order.OrderType='FEE' then exit;
if order.TimeStamp < LastBlockData.TimeStart then exit;
if TrxExistsInLastBlock(order.TrfrID) then exit;
if ((BlockAge>585) and (order.TimeStamp < LastBlockData.TimeStart+540) ) then exit;
if not TranxAlreadyPending(order.TrfrID) then
begin
EnterCriticalSection(CSPending);
while cont < length(ArrayPoolTXs) do
begin
if order.TimeStamp < ArrayPoolTXs[cont].TimeStamp then
begin
insertar := true;
resultado := cont;
break;
end
else if order.TimeStamp = ArrayPoolTXs[cont].TimeStamp then
begin
if order.OrderID < ArrayPoolTXs[cont].OrderID then
begin
insertar := true;
resultado := cont;
break;
end
else if order.OrderID = ArrayPoolTXs[cont].OrderID then
begin
if order.TrxLine < ArrayPoolTXs[cont].TrxLine then
begin
insertar := true;
resultado := cont;
break;
end;
end;
end;
cont := cont+1;
end;
if not insertar then resultado := length(ArrayPoolTXs);
Insert(order,ArrayPoolTXs,resultado);
LeaveCriticalSection(CSPending);
result := true;
VerifyIfPendingIsMine(order);
end;
EndPerformance('AddArrayPoolTXs');
End;
}
// Verifica si una orden especifica es del usuario
Procedure VerifyIfPendingIsMine(order:Torderdata);
var
DireccionEnvia: string;
SendIndex : integer;
Begin
DireccionEnvia := order.address;
SendIndex := WallAddIndex(DireccionEnvia);
if SendIndex>=0 then
begin
SetPendingForAddress(SendIndex,GetWallArrIndex(SendIndex).Pending+Order.AmmountFee+order.AmmountTrf);
montooutgoing := montooutgoing+Order.AmmountFee+order.AmmountTrf;
if not form1.ImageOut.Visible then form1.ImageOut.Visible:= true;
end;
if WallAddIndex(Order.Receiver)>=0 then
begin
montoincoming := montoincoming+order.AmmountTrf;
if not form1.ImageInc.Visible then form1.ImageInc.Visible:= true;
end;
U_DirPanel := true;
End;
// Devuelve si una direccion ya posee un alias
function AddressAlreadyCustomized(address:string):boolean;
var
cont : integer;
Begin
Result := false;
if GetAddressAlias(address) <> '' then Exit(True);
for cont := 0 to GetPendingCount-1 do
if ((ArrayPoolTXs[cont].Address=address) and (ArrayPoolTXs[cont].OrderType = 'CUSTOM')) then
exit(true);
End;
Function GVTAlreadyTransfered(NumberStr:String):boolean;
var
number : integer;
counter : integer;
Begin
result := false;
Number := StrToIntDef(NumberStr,-1);
if number < 0 then
begin
result := true;
exit;
end;
for counter := 0 to GetPendingCount-1 do
begin
if ((ArrayPoolTXs[counter].reference=NumberStr) and (ArrayPoolTXs[counter].OrderType = 'SNDGVT')) then
begin
result := true;
break;
end;
end;
End;
// verify if an alias is already registered
function AliasAlreadyExists(Addalias:string):boolean;
var
cont : integer;
LRecord : TSummaryData;
Begin
Result := false;
if GetIndexPosition(AddAlias,LRecord,True) >= 0 then Exit(True);
for cont := 0 to GetPendingCount-1 do
if ((ArrayPoolTXs[cont].OrderType='CUSTOM') and (ArrayPoolTXs[cont].Receiver = Addalias)) then
Exit(True);
End;
// Devuelve la comision por un monto
function GetFee(monto:int64):Int64;
Begin
Result := monto div Comisiontrfr;
if result < MinimunFee then result := 1000000;//MinimunFee;
End;
// Obtiene una orden de envio de fondos desde una direccion
Function SendFundsFromAddress(Origen, Destino:String; monto, comision:int64; reference,
ordertime:String; linea:integer):TOrderData;
var
MontoDisponible, Montotrfr, comisionTrfr : int64;
OrderInfo : Torderdata;
Begin
BeginPerformance('SendFundsFromAddress');
MontoDisponible := GetAddressBalanceIndexed(GetWallArrIndex(WallAddIndex(origen)).Hash)-GetAddressPendingPays(Origen);
if MontoDisponible>comision then ComisionTrfr := Comision
else comisiontrfr := montodisponible;
if montodisponible>monto+comision then montotrfr := monto
else montotrfr := montodisponible-comision;
if montotrfr <0 then montotrfr := 0;
OrderInfo := Default(TOrderData);
OrderInfo.OrderID := '';
OrderInfo.OrderLines := 1;
OrderInfo.OrderType := 'TRFR';
OrderInfo.TimeStamp := StrToInt64(OrderTime);
OrderInfo.reference := reference;
OrderInfo.TrxLine := linea;
OrderInfo.sender := GetWallArrIndex(WallAddIndex(origen)).PublicKey;
OrderInfo.Address := GetWallArrIndex(WallAddIndex(origen)).Hash;
OrderInfo.Receiver := Destino;
OrderInfo.AmmountFee := ComisionTrfr;
OrderInfo.AmmountTrf := montotrfr;
OrderInfo.Signature := GetStringSigned(ordertime+origen+destino+IntToStr(montotrfr)+
IntToStr(comisiontrfr)+IntToStr(linea),
GetWallArrIndex(WallAddIndex(origen)).PrivateKey);
OrderInfo.TrfrID := GetTransferHash(ordertime+origen+destino+IntToStr(monto)+IntToStr(MyLastblock));
Result := OrderInfo;
EndPerformance('SendFundsFromAddress');
End;
// verifica si en las transaccione pendientes hay alguna de nuestra cartera
Procedure CheckForMyPending();
var
counter : integer = 0;
DireccionEnvia : string;
AddIndex : integer;
Begin
MontoIncoming := 0;
MontoOutgoing := 0;
if GetPendingCount = 0 then
begin
form1.ImageInc.Visible:=false;
form1.ImageOut.Visible:=false;
end
else
begin
for counter := 0 to GetPendingCount-1 do
begin
DireccionEnvia := ArrayPoolTXs[counter].Address;
AddIndex := WallAddIndex(DireccionEnvia);
if AddIndex >= 0 then
begin
MontoOutgoing := MontoOutgoing+ArrayPoolTXs[counter].AmmountFee+ArrayPoolTXs[counter].AmmountTrf;
SetPendingForAddress(AddIndex,GetWallArrIndex(AdDIndex).Pending+ArrayPoolTXs[counter].AmmountFee+ArrayPoolTXs[counter].AmmountTrf);
//WalletArray[WallAddIndex(DireccionEnvia)].Pending:=WalletArray[WallAddIndex(DireccionEnvia)].Pending+ArrayPoolTXs[counter].AmmountFee+ArrayPoolTXs[counter].AmmountTrf;
end;
If WallAddIndex(ArrayPoolTXs[counter].Receiver)>=0 then
MontoIncoming := MontoIncoming+ArrayPoolTXs[counter].AmmountTrf;
end;
if MontoIncoming>0 then form1.ImageInc.Visible := true else form1.ImageInc.Visible:= false;
if MontoOutgoing>0 then form1.ImageOut.Visible := true else form1.ImageOut.Visible:= false;
U_DirPanel := true;
end;
End;
// Retorna cuanto es lo maximo que se puede enviar
function GetMaximunToSend(monto:int64):int64;
var
Disponible : int64;
maximo : int64;
comision : int64;
Envio : int64;
Diferencia : int64;
Begin
Disponible := monto;
if ( (disponible < 1000000{MinimunFee}) or (Disponible<0) ) then
begin
result := 0;
exit;
end;
maximo := (Disponible * Comisiontrfr) div (Comisiontrfr+1);
comision := maximo div Comisiontrfr;
if Comision < 1000000{MinimunFee} then Comision := 1000000{MinimunFee};
Envio := maximo + comision;
Diferencia := Disponible-envio;
result := maximo+diferencia;
End;
function GetCurrentStatus(mode:integer):String;
var
Resultado : string = '';
Begin
resultado := resultado+ 'ServerON : '+BoolToStr(Form1.Server.Active,true)+' ';
if mode = 1 then
begin
resultado := resultado+'Date : '+FormatDateTime('dd MMMM YYYY HH:MM:SS.zzz', Now)+slinebreak;
resultado := resultado+'MyConStatus : '+IntToStr(myConStatus)+slinebreak;
Resultado := resultado+'OS : '+OSVersion +slinebreak;
Resultado := resultado+'WalletVer : '+MainnetVersion+NodeRelease+slinebreak;
end;
result := resultado;
End;
function GetBlockHeaders(numberblock:integer):string;
var
Header : BlockHeaderData;
blockhash : string;
Begin
Header := default (BlockHeaderData);
if fileexists(BlockDirectory+IntToStr(numberblock)+'.blk') then
begin
Header := LoadBlockDataHeader(numberblock);
blockhash := HashMD5File(BlockDirectory+IntToStr(numberblock)+'.blk');
Header.Solution := StringReplace(Header.Solution,' ',#0,[rfReplaceAll, rfIgnoreCase]);
Header.LastBlockHash := StringReplace(Header.LastBlockHash,' ','',[rfReplaceAll, rfIgnoreCase]);
result :=(format('%d'#127'%d'#127'%d'#127'%d'#127'%d'#127'%d'#127'%d'#127'%s'#127'%s'#127'%s'#127'%d'#127'%s'#127'%d'#127'%d'#127'%s',
[Header.Number,Header.TimeStart,Header.TimeEnd,Header.TimeTotal,
Header.TimeLast20,Header.TrxTotales,Header.Difficult,Header.TargetHash,
Header.Solution,Header.LastBlockHash,Header.NxtBlkDiff,Header.AccountMiner,
Header.MinerFee,Header.Reward,blockhash]));
end
End;
function ValidRPCHost(hoststr:string):boolean;
var
HostIP : string;
whitelisted : string;
thiswhitelist : string;
counter : integer = 0;
Begin
result := false;
HostIP := StringReplace(hoststr,':',' ',[rfReplaceAll, rfIgnoreCase]);
HostIP := parameter(HostIP,0);
whitelisted := StringReplace(RPCWhiteList,',',' ',[rfReplaceAll, rfIgnoreCase]);
repeat
thiswhitelist := parameter(whitelisted,counter);
if thiswhitelist = HostIP then result := true;
counter+=1;
until thiswhitelist = '';
End;
// Returns the basic info of the pending orders
function PendingRawInfo(ForRPC : boolean = true):String;
var
CopyArrayPoolTXs : Array of TOrderData;
counter : integer;
ThisPending : string;
Begin
result := '';
if Length(ArrayPoolTXs) > 0 then
begin
EnterCriticalSection(CSPending);
SetLength(CopyArrayPoolTXs,0);
CopyArrayPoolTXs := copy(ArrayPoolTXs,0,length(ArrayPoolTXs));
LeaveCriticalSection(CSPending);
for counter := 0 to Length(CopyArrayPoolTXs)-1 do
begin
if ForRPC then
begin
ThisPending:=CopyArrayPoolTXs[counter].OrderID+','+
CopyArrayPoolTXs[counter].TimeStamp.ToString+','+
CopyArrayPoolTXs[counter].OrderType+','+
CopyArrayPoolTXs[counter].Address+','+
CopyArrayPoolTXs[counter].Receiver+','+
CopyArrayPoolTXs[counter].AmmountTrf.ToString+','+
CopyArrayPoolTXs[counter].AmmountFee.ToString+','+
CopyArrayPoolTXs[counter].Reference{+','+CopyArrayPoolTXs[counter].TimeStamp.ToString};
end
else
begin
ThisPending:=CopyArrayPoolTXs[counter].OrderType+','+
CopyArrayPoolTXs[counter].Address+','+
CopyArrayPoolTXs[counter].Receiver+','+
CopyArrayPoolTXs[counter].AmmountTrf.ToString+','+
CopyArrayPoolTXs[counter].AmmountFee.ToString{+','+CopyArrayPoolTXs[counter].TimeStamp.ToString};
end;
result := result+ThisPending+' ';
end;
Trim(result);
end;
End;
{
// Returns the length of the pending transactions array safely
Function GetPendingCount():integer;
Begin
EnterCriticalSection(CSPending);
result := Length(ArrayPoolTXs);
LeaveCriticalSection(CSPending);
End;
// Clear the pending transactions array safely
Procedure ClearAllPending();
Begin
EnterCriticalSection(CSPending);
SetLength(ArrayPoolTXs,0);
LeaveCriticalSection(CSPending);
End;
}
END. // END UNIT