/
LedgerHandler.cpp
321 lines (290 loc) · 12.4 KB
/
LedgerHandler.cpp
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
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2014 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <ripple/app/ledger/LedgerToJson.h>
#include <ripple/app/main/Application.h>
#include <ripple/app/misc/LoadFeeTrack.h>
#include <ripple/json/Object.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/jss.h>
#include <ripple/resource/Fees.h>
#include <ripple/rpc/GRPCHandlers.h>
#include <ripple/rpc/Role.h>
#include <ripple/rpc/handlers/LedgerHandler.h>
namespace ripple {
namespace RPC {
LedgerHandler::LedgerHandler(JsonContext& context) : context_(context)
{
}
Status
LedgerHandler::check()
{
auto const& params = context_.params;
bool needsLedger = params.isMember(jss::ledger) ||
params.isMember(jss::ledger_hash) ||
params.isMember(jss::ledger_index) || context_.app.config().reporting();
if (!needsLedger)
return Status::OK;
if (auto s = lookupLedger(ledger_, context_, result_))
return s;
bool const full = params[jss::full].asBool();
bool const transactions = params[jss::transactions].asBool();
bool const accounts = params[jss::accounts].asBool();
bool const expand = params[jss::expand].asBool();
bool const binary = params[jss::binary].asBool();
bool const owner_funds = params[jss::owner_funds].asBool();
bool const queue = params[jss::queue].asBool();
auto type = chooseLedgerEntryType(params);
if (type.first)
return type.first;
type_ = type.second;
options_ = (full ? LedgerFill::full : 0) |
(expand ? LedgerFill::expand : 0) |
(transactions ? LedgerFill::dumpTxrp : 0) |
(accounts ? LedgerFill::dumpState : 0) |
(binary ? LedgerFill::binary : 0) |
(owner_funds ? LedgerFill::ownerFunds : 0) |
(queue ? LedgerFill::dumpQueue : 0);
if (full || accounts)
{
// Until some sane way to get full ledgers has been implemented,
// disallow retrieving all state nodes.
if (!isUnlimited(context_.role))
return rpcNO_PERMISSION;
if (context_.app.getFeeTrack().isLoadedLocal() &&
!isUnlimited(context_.role))
{
return rpcTOO_BUSY;
}
context_.loadType =
binary ? Resource::feeMediumBurdenRPC : Resource::feeHighBurdenRPC;
}
if (queue)
{
if (!ledger_ || !ledger_->open())
{
// It doesn't make sense to request the queue
// with a non-existent or closed/validated ledger.
return rpcINVALID_PARAMS;
}
queueTxs_ = context_.app.getTxQ().getTxs();
}
return Status::OK;
}
} // namespace RPC
std::pair<org::xrpl::rpc::v1::GetLedgerResponse, grpc::Status>
doLedgerGrpc(RPC::GRPCContext<org::xrpl::rpc::v1::GetLedgerRequest>& context)
{
auto begin = std::chrono::system_clock::now();
org::xrpl::rpc::v1::GetLedgerRequest& request = context.params;
org::xrpl::rpc::v1::GetLedgerResponse response;
grpc::Status status = grpc::Status::OK;
std::shared_ptr<ReadView const> ledger;
if (auto status = RPC::ledgerFromRequest(ledger, context))
{
grpc::Status errorStatus;
if (status.toErrorCode() == rpcINVALID_PARAMS)
{
errorStatus = grpc::Status(
grpc::StatusCode::INVALID_ARGUMENT, status.message());
}
else
{
errorStatus =
grpc::Status(grpc::StatusCode::NOT_FOUND, status.message());
}
return {response, errorStatus};
}
Serializer s;
addRaw(ledger->info(), s, true);
response.set_ledger_header(s.peekData().data(), s.getLength());
if (request.transactions())
{
try
{
for (auto& i : ledger->txs)
{
assert(i.first);
if (request.expand())
{
auto txn = response.mutable_transactions_list()
->add_transactions();
Serializer sTxn = i.first->getSerializer();
txn->set_transaction_blob(sTxn.data(), sTxn.getLength());
if (i.second)
{
Serializer sMeta = i.second->getSerializer();
txn->set_metadata_blob(sMeta.data(), sMeta.getLength());
}
}
else
{
auto const& hash = i.first->getTransactionID();
response.mutable_hashes_list()->add_hashes(
hash.data(), hash.size());
}
}
}
catch (std::exception const& e)
{
JLOG(context.j.error())
<< __func__ << " - Error deserializing transaction in ledger "
<< ledger->info().seq
<< " . skipping transaction and following transactions. You "
"should look into this further";
}
}
if (request.get_objects())
{
std::shared_ptr<ReadView const> parent =
context.app.getLedgerMaster().getLedgerBySeq(ledger->seq() - 1);
std::shared_ptr<Ledger const> base =
std::dynamic_pointer_cast<Ledger const>(parent);
if (!base)
{
grpc::Status errorStatus{
grpc::StatusCode::NOT_FOUND, "parent ledger not validated"};
return {response, errorStatus};
}
std::shared_ptr<Ledger const> desired =
std::dynamic_pointer_cast<Ledger const>(ledger);
if (!desired)
{
grpc::Status errorStatus{
grpc::StatusCode::NOT_FOUND, "ledger not validated"};
return {response, errorStatus};
}
SHAMap::Delta differences;
int maxDifferences = std::numeric_limits<int>::max();
bool res = base->stateMap().compare(
desired->stateMap(), differences, maxDifferences);
if (!res)
{
grpc::Status errorStatus{
grpc::StatusCode::RESOURCE_EXHAUSTED,
"too many differences between specified ledgers"};
return {response, errorStatus};
}
for (auto& [k, v] : differences)
{
auto obj = response.mutable_ledger_objects()->add_objects();
auto inBase = v.first;
auto inDesired = v.second;
obj->set_key(k.data(), k.size());
if (inDesired)
{
assert(inDesired->size() > 0);
obj->set_data(inDesired->data(), inDesired->size());
}
if (inBase && inDesired)
obj->set_mod_type(
org::xrpl::rpc::v1::RawLedgerObject::MODIFIED);
else if (inBase && !inDesired)
obj->set_mod_type(org::xrpl::rpc::v1::RawLedgerObject::DELETED);
else
obj->set_mod_type(org::xrpl::rpc::v1::RawLedgerObject::CREATED);
auto const blob = inDesired ? inDesired->slice() : inBase->slice();
auto const objectType =
static_cast<LedgerEntryType>(blob[1] << 8 | blob[2]);
if (request.get_object_neighbors())
{
if (!(inBase && inDesired))
{
auto lb = desired->stateMap().lower_bound(k);
auto ub = desired->stateMap().upper_bound(k);
if (lb != desired->stateMap().end())
obj->set_predecessor(
lb->key().data(), lb->key().size());
if (ub != desired->stateMap().end())
obj->set_successor(ub->key().data(), ub->key().size());
if (objectType == ltDIR_NODE)
{
auto sle = std::make_shared<SLE>(SerialIter{blob}, k);
if (!sle->isFieldPresent(sfOwner))
{
auto bookBase = keylet::quality({ltDIR_NODE, k}, 0);
if (!inBase && inDesired)
{
auto firstBook =
desired->stateMap().upper_bound(
bookBase.key);
if (firstBook != desired->stateMap().end() &&
firstBook->key() <
getQualityNext(bookBase.key) &&
firstBook->key() == k)
{
auto succ = response.add_book_successors();
succ->set_book_base(
bookBase.key.data(),
bookBase.key.size());
succ->set_first_book(
firstBook->key().data(),
firstBook->key().size());
}
}
if (inBase && !inDesired)
{
auto oldFirstBook =
base->stateMap().upper_bound(bookBase.key);
if (oldFirstBook != base->stateMap().end() &&
oldFirstBook->key() <
getQualityNext(bookBase.key) &&
oldFirstBook->key() == k)
{
auto succ = response.add_book_successors();
succ->set_book_base(
bookBase.key.data(),
bookBase.key.size());
auto newFirstBook =
desired->stateMap().upper_bound(
bookBase.key);
if (newFirstBook !=
desired->stateMap().end() &&
newFirstBook->key() <
getQualityNext(bookBase.key))
{
succ->set_first_book(
newFirstBook->key().data(),
newFirstBook->key().size());
}
}
}
}
}
}
}
}
response.set_objects_included(true);
response.set_object_neighbors_included(request.get_object_neighbors());
response.set_skiplist_included(true);
}
response.set_validated(context.ledgerMaster.isValidated(*ledger));
auto end = std::chrono::system_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin)
.count() *
1.0;
JLOG(context.j.warn())
<< __func__ << " - Extract time = " << duration
<< " - num objects = " << response.ledger_objects().objects_size()
<< " - num txns = " << response.transactions_list().transactions_size()
<< " - ms per obj "
<< duration / response.ledger_objects().objects_size()
<< " - ms per txn "
<< duration / response.transactions_list().transactions_size();
return {response, status};
}
} // namespace ripple