-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathUPCRPCFileUtils.pas
More file actions
180 lines (156 loc) · 6.33 KB
/
Copy pathUPCRPCFileUtils.pas
File metadata and controls
180 lines (156 loc) · 6.33 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
unit UPCRPCFileUtils;
{ Copyright (c) 2020 by PascalCoin developers, orignal code by Albert Molina
Distributed under the MIT software license, see the accompanying file LICENSE
or visit http://www.opensource.org/licenses/mit-license.php.
This unit is a part of the PascalCoin Project, an infinitely scalable
cryptocurrency. Find us here:
Web: https://www.pascalcoin.org
Source: https://github.com/PascalCoin/PascalCoin
If you like it, consider a donation using Bitcoin:
16K3HCZRhFUtM8GdWRcfKeaa6KsuyxZaYk
THIS LICENSE HEADER MUST NOT BE REMOVED.
}
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
{$I ./../config.inc}
Uses classes, SysUtils,
UJSONFunctions, URPC, UCrypto, ULog,
{$IFDEF USE_ABSTRACTMEM}
UPCAbstractMem, UPCAbstractMemAccountKeys,
{$ENDIF}
{$IFNDEF FPC}System.Generics.Collections{$ELSE}Generics.Collections{$ENDIF},
UBlockChain, UPCOrderedLists;
Type
{ TRPCFileUtils }
TRPCFileUtils = Class
private
public
class function SaveAsSafeboxStream(const ASender : TRPCProcess; const AMethodName : String; AInputParams, AJSONResponse : TPCJSONObject; var AErrorNum : Integer; var AErrorDesc : String) : Boolean;
class function GenerateNewAbstractMemSafebox(const ASender : TRPCProcess; const AMethodName : String; AInputParams, AJSONResponse : TPCJSONObject; var AErrorNum : Integer; var AErrorDesc : String) : Boolean;
class function AbstractMemStats(const ASender : TRPCProcess; const AMethodName : String; AInputParams, AJSONResponse : TPCJSONObject; var AErrorNum : Integer; var AErrorDesc : String) : Boolean;
End;
implementation
uses UPCDataTypes, UFileStorage, UNode;
{ TRPCFileUtils }
class function TRPCFileUtils.GenerateNewAbstractMemSafebox(
const ASender: TRPCProcess; const AMethodName: String; AInputParams,
AJSONResponse: TPCJSONObject; var AErrorNum: Integer;
var AErrorDesc: String): Boolean;
{$IFDEF USE_ABSTRACTMEM}
var
LFileName : String;
{$ENDIF}
begin
if Not ASender.RPCServer.AllowUsePrivateKeys then begin
AErrorNum := CT_RPC_ErrNum_NotAllowedCall;
Exit(False);
end;
{$IFDEF USE_ABSTRACTMEM}
LFileName := AInputParams.AsString('filename', '').Trim;
if (LFileName='') then begin
LFileName := TPCBank.GetSafeboxCheckpointingFileName(TNode.Node.Bank.GetStorageFolder(''),TNode.Node.Bank.BlocksCount);
end;
TNode.Node.Bank.SafeBox.SaveCheckpointing(LFileName);
AJSONResponse.GetAsObject('result').GetAsVariant('filename').Value := LFileName;
AErrorNum := 0;
AErrorDesc := '';
Result := True;
{$ELSE}
AErrorNum := CT_RPC_ErrNum_NotImplemented;
AErrorDesc := 'AbstractMem library is not available in this build';
Result := False;
{$ENDIF}
end;
class function TRPCFileUtils.AbstractMemStats(const ASender: TRPCProcess;
const AMethodName: String; AInputParams, AJSONResponse: TPCJSONObject;
var AErrorNum: Integer; var AErrorDesc: String): Boolean;
var LStrings, LReport : TStrings;
LTotalUsedSize, LTotalUsedBlocksCount, LTotalLeaksSize, LTotalLeaksBlocksCount : Int64;
i, nMax : Integer;
Lobj : TPCJSONObject;
Larray : TPCJSONArray;
begin
if Not ASender.RPCServer.AllowUsePrivateKeys then begin
AErrorNum := CT_RPC_ErrNum_NotAllowedCall;
Exit(False);
end;
{$IFDEF USE_ABSTRACTMEM}
LStrings := TStringList.Create;
Try
if AInputParams.GetAsVariant('report').AsBoolean(False) then LReport := LStrings
else LReport := Nil;
Lobj := AJSONResponse.GetAsObject('result').GetAsObject('abstractmem');
if TNode.Node.Bank.SafeBox.PCAbstractMem.AbstractMem.CheckConsistency(LReport,Nil, LTotalUsedSize, LTotalUsedBlocksCount, LTotalLeaksSize, LTotalLeaksBlocksCount) then begin
Lobj.GetAsVariant('checkconsistency').Value := True;
end else begin
Lobj.GetAsVariant('checkconsistency').Value := False;
end;
Lobj.GetAsVariant('total_used_size').Value := LTotalUsedSize;
Lobj.GetAsVariant('total_used_blocks_count').Value := LTotalUsedBlocksCount;
Lobj.GetAsVariant('total_leaks_size').Value := LTotalLeaksSize;
Lobj.GetAsVariant('total_leaks_blocks_count').Value := LTotalLeaksBlocksCount;
if Assigned(LReport) then begin
Larray := Lobj.GetAsArray('report');
i := AInputParams.GetAsVariant('report_start').AsInteger(0);
nMax := AInputParams.GetAsVariant('report_max').AsInteger(100);
while (nMax>0) and (i>=0) and (i<LStrings.Count-1) do begin
Larray.GetAsVariant(Larray.Count).Value := LStrings[i];
inc(i);
dec(nMax);
end;
end;
Result := True;
Finally
LStrings.Free;
end;
{$ELSE}
AErrorNum := CT_RPC_ErrNum_NotImplemented;
AErrorDesc := 'AbstractMem library is not available in this build';
Result := False;
{$ENDIF}
//
end;
class function TRPCFileUtils.SaveAsSafeboxStream(const ASender: TRPCProcess;
const AMethodName: String; AInputParams, AJSONResponse: TPCJSONObject;
var AErrorNum: Integer; var AErrorDesc: String): Boolean;
var LFileName : String;
LFs : TFileStream;
LStart,LEnd : Integer;
begin
if Not ASender.RPCServer.AllowUsePrivateKeys then begin
AErrorNum := CT_RPC_ErrNum_NotAllowedCall;
Exit(False);
end;
LFileName := AInputParams.AsString('filename', '').Trim;
if (LFileName='') then begin
LFileName := TPCBank.GetSafeboxCheckpointingFileName(TNode.Node.Bank.GetStorageFolder(''),TNode.Node.Bank.BlocksCount);
LFileName := ChangeFileExt(LFileName,'.safebox');
end;
LFs := TFileStream.Create(LFileName,fmCreate);
try
LFs.Size := 0;
LFs.Position := 0;
LStart := 0;
LEnd := TNode.Node.Bank.BlocksCount-1;
TNode.Node.Bank.SafeBox.SaveSafeBoxToAStream(LFs,LStart,LEnd);
finally
LFs.Free;
end;
AJSONResponse.GetAsObject('result').GetAsVariant('filename').Value := LFileName;
AJSONResponse.GetAsObject('result').GetAsVariant('start').Value := LStart;
AJSONResponse.GetAsObject('result').GetAsVariant('end').Value := LEnd;
AErrorNum := 0;
AErrorDesc := '';
Result := True;
end;
initialization
TRPCProcess.RegisterProcessMethod('save-safebox-stream',TRPCFileUtils.SaveAsSafeboxStream);
TRPCProcess.RegisterProcessMethod('save-safebox-abstractmem',TRPCFileUtils.GenerateNewAbstractMemSafebox);
TRPCProcess.RegisterProcessMethod('abstractmem-stats',TRPCFileUtils.AbstractMemStats);
finalization
TRPCProcess.UnregisterProcessMethod('save-safebox-stream');
TRPCProcess.UnregisterProcessMethod('save-safebox-abstractmem');
TRPCProcess.UnregisterProcessMethod('abstractmem-stats');
end.