forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpgrades.cpp
765 lines (702 loc) · 24.5 KB
/
Upgrades.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// Copyright 2017 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "herder/Upgrades.h"
#include "database/Database.h"
#include "database/DatabaseUtils.h"
#include "ledger/LedgerTxn.h"
#include "ledger/LedgerTxnEntry.h"
#include "ledger/LedgerTxnHeader.h"
#include "ledger/TrustLineWrapper.h"
#include "main/Config.h"
#include "transactions/OfferExchange.h"
#include "transactions/TransactionUtils.h"
#include "util/Decoder.h"
#include "util/Logging.h"
#include "util/Timer.h"
#include "util/types.h"
#include <cereal/archives/json.hpp>
#include <cereal/cereal.hpp>
#include <lib/util/format.h>
#include <xdrpp/marshal.h>
namespace cereal
{
template <class Archive>
void
save(Archive& ar, stellar::Upgrades::UpgradeParameters const& p)
{
ar(make_nvp("time", stellar::VirtualClock::to_time_t(p.mUpgradeTime)));
ar(make_nvp("version", p.mProtocolVersion));
ar(make_nvp("fee", p.mBaseFee));
ar(make_nvp("maxtxsize", p.mMaxTxSize));
ar(make_nvp("reserve", p.mBaseReserve));
}
template <class Archive>
void
load(Archive& ar, stellar::Upgrades::UpgradeParameters& o)
{
time_t t;
ar(make_nvp("time", t));
o.mUpgradeTime = stellar::VirtualClock::from_time_t(t);
ar(make_nvp("version", o.mProtocolVersion));
ar(make_nvp("fee", o.mBaseFee));
ar(make_nvp("maxtxsize", o.mMaxTxSize));
ar(make_nvp("reserve", o.mBaseReserve));
}
} // namespace cereal
namespace stellar
{
std::string
Upgrades::UpgradeParameters::toJson() const
{
std::ostringstream out;
{
cereal::JSONOutputArchive ar(out);
cereal::save(ar, *this);
}
return out.str();
}
void
Upgrades::UpgradeParameters::fromJson(std::string const& s)
{
std::istringstream in(s);
{
cereal::JSONInputArchive ar(in);
cereal::load(ar, *this);
}
}
Upgrades::Upgrades(UpgradeParameters const& params) : mParams(params)
{
}
void
Upgrades::setParameters(UpgradeParameters const& params, Config const& cfg)
{
if (params.mProtocolVersion &&
*params.mProtocolVersion > cfg.LEDGER_PROTOCOL_VERSION)
{
throw std::invalid_argument(fmt::format(
"Protocol version error: supported is up to {}, passed is {}",
cfg.LEDGER_PROTOCOL_VERSION, *params.mProtocolVersion));
}
mParams = params;
}
Upgrades::UpgradeParameters const&
Upgrades::getParameters() const
{
return mParams;
}
std::vector<LedgerUpgrade>
Upgrades::createUpgradesFor(LedgerHeader const& header) const
{
auto result = std::vector<LedgerUpgrade>{};
if (!timeForUpgrade(header.scpValue.closeTime))
{
return result;
}
if (mParams.mProtocolVersion &&
(header.ledgerVersion != *mParams.mProtocolVersion))
{
result.emplace_back(LEDGER_UPGRADE_VERSION);
result.back().newLedgerVersion() = *mParams.mProtocolVersion;
}
if (mParams.mBaseFee && (header.baseFee != *mParams.mBaseFee))
{
result.emplace_back(LEDGER_UPGRADE_BASE_FEE);
result.back().newBaseFee() = *mParams.mBaseFee;
}
if (mParams.mMaxTxSize && (header.maxTxSetSize != *mParams.mMaxTxSize))
{
result.emplace_back(LEDGER_UPGRADE_MAX_TX_SET_SIZE);
result.back().newMaxTxSetSize() = *mParams.mMaxTxSize;
}
if (mParams.mBaseReserve && (header.baseReserve != *mParams.mBaseReserve))
{
result.emplace_back(LEDGER_UPGRADE_BASE_RESERVE);
result.back().newBaseReserve() = *mParams.mBaseReserve;
}
return result;
}
void
Upgrades::applyTo(LedgerUpgrade const& upgrade, AbstractLedgerTxn& ltx)
{
switch (upgrade.type())
{
case LEDGER_UPGRADE_VERSION:
applyVersionUpgrade(ltx, upgrade.newLedgerVersion());
break;
case LEDGER_UPGRADE_BASE_FEE:
ltx.loadHeader().current().baseFee = upgrade.newBaseFee();
break;
case LEDGER_UPGRADE_MAX_TX_SET_SIZE:
ltx.loadHeader().current().maxTxSetSize = upgrade.newMaxTxSetSize();
break;
case LEDGER_UPGRADE_BASE_RESERVE:
applyReserveUpgrade(ltx, upgrade.newBaseReserve());
break;
default:
{
auto s = fmt::format("Unknown upgrade type: {0}", upgrade.type());
throw std::runtime_error(s);
}
}
}
std::string
Upgrades::toString(LedgerUpgrade const& upgrade)
{
switch (upgrade.type())
{
case LEDGER_UPGRADE_VERSION:
return fmt::format("protocolversion={0}", upgrade.newLedgerVersion());
case LEDGER_UPGRADE_BASE_FEE:
return fmt::format("basefee={0}", upgrade.newBaseFee());
case LEDGER_UPGRADE_MAX_TX_SET_SIZE:
return fmt::format("maxtxsetsize={0}", upgrade.newMaxTxSetSize());
case LEDGER_UPGRADE_BASE_RESERVE:
return fmt::format("basereserve={0}", upgrade.newBaseReserve());
default:
return "<unsupported>";
}
}
std::string
Upgrades::toString() const
{
fmt::MemoryWriter r;
auto appendInfo = [&](std::string const& s, optional<uint32> const& o) {
if (o)
{
if (!r.size())
{
r << "upgradetime="
<< VirtualClock::pointToISOString(mParams.mUpgradeTime);
}
r << ", " << s << "=" << *o;
}
};
appendInfo("protocolversion", mParams.mProtocolVersion);
appendInfo("basefee", mParams.mBaseFee);
appendInfo("basereserve", mParams.mBaseReserve);
appendInfo("maxtxsize", mParams.mMaxTxSize);
return r.str();
}
Upgrades::UpgradeParameters
Upgrades::removeUpgrades(std::vector<UpgradeType>::const_iterator beginUpdates,
std::vector<UpgradeType>::const_iterator endUpdates,
bool& updated)
{
updated = false;
UpgradeParameters res = mParams;
auto resetParam = [&](optional<uint32>& o, uint32 v) {
if (o && *o == v)
{
o.reset();
updated = true;
}
};
for (auto it = beginUpdates; it != endUpdates; it++)
{
auto& u = *it;
LedgerUpgrade lu;
try
{
xdr::xdr_from_opaque(u, lu);
}
catch (xdr::xdr_runtime_error&)
{
continue;
}
switch (lu.type())
{
case LEDGER_UPGRADE_VERSION:
resetParam(res.mProtocolVersion, lu.newLedgerVersion());
break;
case LEDGER_UPGRADE_BASE_FEE:
resetParam(res.mBaseFee, lu.newBaseFee());
break;
case LEDGER_UPGRADE_MAX_TX_SET_SIZE:
resetParam(res.mMaxTxSize, lu.newMaxTxSetSize());
break;
case LEDGER_UPGRADE_BASE_RESERVE:
resetParam(res.mBaseReserve, lu.newBaseReserve());
break;
default:
// skip unknown
break;
}
}
return res;
}
Upgrades::UpgradeValidity
Upgrades::isValidForApply(UpgradeType const& opaqueUpgrade,
LedgerUpgrade& upgrade, LedgerHeader const& header,
uint32_t maxLedgerVersion)
{
try
{
xdr::xdr_from_opaque(opaqueUpgrade, upgrade);
}
catch (xdr::xdr_runtime_error&)
{
return UpgradeValidity::XDR_INVALID;
}
bool res = true;
switch (upgrade.type())
{
case LEDGER_UPGRADE_VERSION:
{
uint32 newVersion = upgrade.newLedgerVersion();
// only allow upgrades to a supported version of the protocol
res = res && (newVersion <= maxLedgerVersion);
// and enforce versions to be strictly monotonic
res = res && (newVersion > header.ledgerVersion);
}
break;
case LEDGER_UPGRADE_BASE_FEE:
res = res && (upgrade.newBaseFee() != 0);
break;
case LEDGER_UPGRADE_MAX_TX_SET_SIZE:
res = res && (upgrade.newMaxTxSetSize() != 0);
break;
case LEDGER_UPGRADE_BASE_RESERVE:
res = res && (upgrade.newBaseReserve() != 0);
break;
default:
res = false;
}
return res ? UpgradeValidity::VALID : UpgradeValidity::INVALID;
}
bool
Upgrades::isValidForNomination(LedgerUpgrade const& upgrade,
LedgerHeader const& header) const
{
if (!timeForUpgrade(header.scpValue.closeTime))
{
return false;
}
switch (upgrade.type())
{
case LEDGER_UPGRADE_VERSION:
return mParams.mProtocolVersion &&
(upgrade.newLedgerVersion() == *mParams.mProtocolVersion);
case LEDGER_UPGRADE_BASE_FEE:
return mParams.mBaseFee && (upgrade.newBaseFee() == *mParams.mBaseFee);
case LEDGER_UPGRADE_MAX_TX_SET_SIZE:
return mParams.mMaxTxSize &&
(upgrade.newMaxTxSetSize() == *mParams.mMaxTxSize);
case LEDGER_UPGRADE_BASE_RESERVE:
return mParams.mBaseReserve &&
(upgrade.newBaseReserve() == *mParams.mBaseReserve);
default:
return false;
}
}
bool
Upgrades::isValid(UpgradeType const& upgrade, LedgerUpgradeType& upgradeType,
bool nomination, Config const& cfg,
LedgerHeader const& header) const
{
LedgerUpgrade lupgrade;
bool res =
isValidForApply(upgrade, lupgrade, header,
cfg.LEDGER_PROTOCOL_VERSION) == UpgradeValidity::VALID;
if (nomination)
{
res = res && isValidForNomination(lupgrade, header);
}
if (res)
{
upgradeType = lupgrade.type();
}
return res;
}
bool
Upgrades::timeForUpgrade(uint64_t time) const
{
return mParams.mUpgradeTime <= VirtualClock::from_time_t(time);
}
void
Upgrades::dropAll(Database& db)
{
db.getSession() << "DROP TABLE IF EXISTS upgradehistory";
db.getSession() << "CREATE TABLE upgradehistory ("
"ledgerseq INT NOT NULL CHECK (ledgerseq >= 0), "
"upgradeindex INT NOT NULL, "
"upgrade TEXT NOT NULL, "
"changes TEXT NOT NULL, "
"PRIMARY KEY (ledgerseq, upgradeindex)"
")";
db.getSession()
<< "CREATE INDEX upgradehistbyseq ON upgradehistory (ledgerseq);";
}
void
Upgrades::storeUpgradeHistory(Database& db, uint32_t ledgerSeq,
LedgerUpgrade const& upgrade,
LedgerEntryChanges const& changes, int index)
{
xdr::opaque_vec<> upgradeContent(xdr::xdr_to_opaque(upgrade));
std::string upgradeContent64 = decoder::encode_b64(upgradeContent);
xdr::opaque_vec<> upgradeChanges(xdr::xdr_to_opaque(changes));
std::string upgradeChanges64 = decoder::encode_b64(upgradeChanges);
auto prep = db.getPreparedStatement(
"INSERT INTO upgradehistory "
"(ledgerseq, upgradeindex, upgrade, changes) VALUES "
"(:seq, :upgradeindex, :upgrade, :changes)");
auto& st = prep.statement();
st.exchange(soci::use(ledgerSeq));
st.exchange(soci::use(index));
st.exchange(soci::use(upgradeContent64));
st.exchange(soci::use(upgradeChanges64));
st.define_and_bind();
{
auto timer = db.getInsertTimer("upgradehistory");
st.execute(true);
}
if (st.get_affected_rows() != 1)
{
throw std::runtime_error("Could not update data in SQL");
}
}
void
Upgrades::deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count)
{
DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count,
"upgradehistory", "ledgerseq");
}
static void
addLiabilities(std::map<Asset, std::unique_ptr<int64_t>>& liabilities,
AccountID const& accountID, Asset const& asset, int64_t delta)
{
auto iter =
liabilities.insert(std::make_pair(asset, std::make_unique<int64_t>(0)))
.first;
if (asset.type() != ASSET_TYPE_NATIVE && accountID == getIssuer(asset))
{
return;
}
if (iter->second)
{
if (!stellar::addBalance(*iter->second, delta))
{
iter->second.reset();
}
}
}
static int64_t
getAvailableBalanceExcludingLiabilities(AccountID const& accountID,
Asset const& asset,
int64_t balanceAboveReserve,
AbstractLedgerTxn& ltx)
{
if (asset.type() == ASSET_TYPE_NATIVE)
{
return balanceAboveReserve;
}
if (accountID == getIssuer(asset))
{
return INT64_MAX;
}
else
{
auto trust = stellar::loadTrustLineWithoutRecord(ltx, accountID, asset);
if (trust && trust.isAuthorized())
{
return trust.getBalance();
}
else
{
return 0;
}
}
}
static int64_t
getAvailableLimitExcludingLiabilities(AccountID const& accountID,
Asset const& asset, int64_t balance,
AbstractLedgerTxn& ltx)
{
if (asset.type() == ASSET_TYPE_NATIVE)
{
return INT64_MAX - balance;
}
if (accountID == getIssuer(asset))
{
return INT64_MAX;
}
else
{
LedgerKey key(TRUSTLINE);
key.trustLine().accountID = accountID;
key.trustLine().asset = asset;
auto trust = ltx.loadWithoutRecord(key);
if (trust && isAuthorized(trust))
{
auto const& tl = trust.current().data.trustLine();
return tl.limit - tl.balance;
}
else
{
return 0;
}
}
}
static bool
shouldDeleteOffer(Asset const& asset, int64_t effectiveBalance,
std::map<Asset, std::unique_ptr<int64_t>> const& liabilities,
std::function<int64_t(Asset const&, int64_t)> getCap)
{
auto iter = liabilities.find(asset);
if (iter == liabilities.end())
{
throw std::runtime_error("liabilities were not calculated");
}
// Offers should be deleted if liabilities exceed INT64_MAX (nullptr) or if
// there are excess liabilities.
return iter->second ? *iter->second > getCap(asset, effectiveBalance)
: true;
}
enum class UpdateOfferResult
{
Unchanged,
Adjusted,
AdjustedToZero,
Erased
};
static UpdateOfferResult
updateOffer(
LedgerTxnEntry& offerEntry, int64_t balance, int64_t balanceAboveReserve,
std::map<Asset, Liabilities>& liabilities,
std::map<Asset, std::unique_ptr<int64_t>> const& initialBuyingLiabilities,
std::map<Asset, std::unique_ptr<int64_t>> const& initialSellingLiabilities,
AbstractLedgerTxn& ltx, LedgerTxnHeader const& header)
{
using namespace std::placeholders;
auto& offer = offerEntry.current().data.offer();
auto availableBalanceBind =
std::bind(getAvailableBalanceExcludingLiabilities, offer.sellerID, _1,
_2, std::ref(ltx));
auto availableLimitBind = std::bind(getAvailableLimitExcludingLiabilities,
offer.sellerID, _1, _2, std::ref(ltx));
bool erase =
shouldDeleteOffer(offer.selling, balanceAboveReserve,
initialSellingLiabilities, availableBalanceBind);
erase = erase ||
shouldDeleteOffer(offer.buying, balance, initialBuyingLiabilities,
availableLimitBind);
UpdateOfferResult res =
erase ? UpdateOfferResult::Erased : UpdateOfferResult::Unchanged;
// If erase == false then we know that the total buying liabilities
// of the buying asset do not exceed its available limit, and the
// total selling liabilities of the selling asset do not exceed its
// available balance. This implies that there are no excess
// liabilities for this offer, so the only applicable limit is the
// offer amount. We then use adjustOffer to check that it will
// satisfy thresholds.
if (!erase && adjustOffer(offer.price, offer.amount, INT64_MAX) == 0)
{
erase = true;
res = UpdateOfferResult::AdjustedToZero;
}
if (erase)
{
offerEntry.erase();
}
else
{
// The same logic for adjustOffer discussed above applies here,
// except that we now actually update the offer to reflect the
// adjustment.
auto adjAmount = adjustOffer(offer.price, offer.amount, INT64_MAX);
if (adjAmount != offer.amount)
{
offer.amount = adjAmount;
res = UpdateOfferResult::Adjusted;
}
if (offer.buying.type() == ASSET_TYPE_NATIVE ||
!(offer.sellerID == getIssuer(offer.buying)))
{
if (!stellar::addBalance(
liabilities[offer.buying].buying,
getOfferBuyingLiabilities(header, offerEntry)))
{
throw std::runtime_error("could not add buying "
"liabilities");
}
}
if (offer.selling.type() == ASSET_TYPE_NATIVE ||
!(offer.sellerID == getIssuer(offer.selling)))
{
if (!stellar::addBalance(
liabilities[offer.selling].selling,
getOfferSellingLiabilities(header, offerEntry)))
{
throw std::runtime_error("could not add selling "
"liabilities");
}
}
}
return res;
}
// This function is used to bring offers and liabilities into a valid state.
// For every account that has offers,
// 1. Calculate total liabilities for each asset
// 2. For every asset with excess buying liabilities according to (1), erase
// all offers buying that asset. For every asset with excess selling
// liabilities according to (1), erase all offers selling that asset.
// 3. Update liabilities to reflect offers remaining in the book.
// It is essential to note that the excess liabilities are determined only
// using the initial result of step (1), so it does not matter what order the
// offers are processed.
static void
prepareLiabilities(AbstractLedgerTxn& ltx, LedgerTxnHeader const& header)
{
CLOG(INFO, "Ledger") << "Starting prepareLiabilities";
auto offersByAccount = ltx.loadAllOffers();
uint64_t nChangedAccounts = 0;
uint64_t nChangedTrustLines = 0;
std::map<UpdateOfferResult, uint64_t> nUpdatedOffers;
for (auto& accountOffers : offersByAccount)
{
// The purpose of std::unique_ptr here is to have a special value
// (nullptr) to indicate that an integer overflow would have occured.
// Overflow is possible here because existing offers were not
// constrainted to have int64_t liabilities. This must be carefully
// handled in what follows.
std::map<Asset, std::unique_ptr<int64_t>> initialBuyingLiabilities;
std::map<Asset, std::unique_ptr<int64_t>> initialSellingLiabilities;
for (auto const& offerEntry : accountOffers.second)
{
auto const& offer = offerEntry.current().data.offer();
addLiabilities(initialBuyingLiabilities, offer.sellerID,
offer.buying,
getOfferBuyingLiabilities(header, offerEntry));
addLiabilities(initialSellingLiabilities, offer.sellerID,
offer.selling,
getOfferSellingLiabilities(header, offerEntry));
}
auto accountEntry = stellar::loadAccount(ltx, accountOffers.first);
if (!accountEntry)
{
throw std::runtime_error("account does not exist");
}
auto const& acc = accountEntry.current().data.account();
AccountEntry const accountBefore = acc;
// balanceAboveReserve must exclude native selling liabilities, since
// these are in the process of being recalculated from scratch.
int64_t balance = acc.balance;
int64_t minBalance = getMinBalance(header, acc.numSubEntries);
int64_t balanceAboveReserve = balance - minBalance;
std::map<Asset, Liabilities> liabilities;
for (auto& offerEntry : accountOffers.second)
{
auto offerID = offerEntry.current().data.offer().offerID;
auto res = updateOffer(offerEntry, balance, balanceAboveReserve,
liabilities, initialBuyingLiabilities,
initialSellingLiabilities, ltx, header);
if (res == UpdateOfferResult::AdjustedToZero ||
res == UpdateOfferResult::Erased)
{
stellar::addNumEntries(header, accountEntry, -1);
}
++nUpdatedOffers[res];
if (res != UpdateOfferResult::Unchanged)
{
std::string message;
switch (res)
{
case UpdateOfferResult::Adjusted:
message = " was adjusted";
break;
case UpdateOfferResult::AdjustedToZero:
message = " was adjusted to zero";
break;
case UpdateOfferResult::Erased:
message = " was erased";
break;
default:
throw std::runtime_error("Unknown UpdateOfferResult");
}
CLOG(DEBUG, "Ledger")
<< "Offer with offerID=" << offerID << message;
}
}
for (auto const& assetLiabilities : liabilities)
{
Asset const& asset = assetLiabilities.first;
Liabilities const& liab = assetLiabilities.second;
if (asset.type() == ASSET_TYPE_NATIVE)
{
int64_t deltaSelling =
liab.selling - getSellingLiabilities(header, accountEntry);
int64_t deltaBuying =
liab.buying - getBuyingLiabilities(header, accountEntry);
if (!addSellingLiabilities(header, accountEntry, deltaSelling))
{
throw std::runtime_error("invalid selling liabilities "
"during upgrade");
}
if (!addBuyingLiabilities(header, accountEntry, deltaBuying))
{
throw std::runtime_error("invalid buying liabilities "
"during upgrade");
}
}
else
{
auto trustEntry =
stellar::loadTrustLine(ltx, accountOffers.first, asset);
int64_t deltaSelling =
liab.selling - trustEntry.getSellingLiabilities(header);
int64_t deltaBuying =
liab.buying - trustEntry.getBuyingLiabilities(header);
if (deltaSelling != 0 || deltaBuying != 0)
{
++nChangedTrustLines;
}
if (!trustEntry.addSellingLiabilities(header, deltaSelling))
{
throw std::runtime_error("invalid selling liabilities "
"during upgrade");
}
if (!trustEntry.addBuyingLiabilities(header, deltaBuying))
{
throw std::runtime_error("invalid buying liabilities "
"during upgrade");
}
}
}
if (!(acc == accountBefore))
{
++nChangedAccounts;
}
}
CLOG(INFO, "Ledger") << "prepareLiabilities completed with "
<< nChangedAccounts << " accounts modified, "
<< nChangedTrustLines << " trustlines modified, "
<< nUpdatedOffers[UpdateOfferResult::Adjusted]
<< " offers adjusted, "
<< nUpdatedOffers[UpdateOfferResult::AdjustedToZero]
<< " offers adjusted to zero, and "
<< nUpdatedOffers[UpdateOfferResult::Erased]
<< " offers erased";
}
void
Upgrades::applyVersionUpgrade(AbstractLedgerTxn& ltx, uint32_t newVersion)
{
auto header = ltx.loadHeader();
uint32_t prevVersion = header.current().ledgerVersion;
header.current().ledgerVersion = newVersion;
if (header.current().ledgerVersion >= 10 && prevVersion < 10)
{
prepareLiabilities(ltx, header);
}
}
void
Upgrades::applyReserveUpgrade(AbstractLedgerTxn& ltx, uint32_t newReserve)
{
auto header = ltx.loadHeader();
bool didReserveIncrease = newReserve > header.current().baseReserve;
header.current().baseReserve = newReserve;
if (header.current().ledgerVersion >= 10 && didReserveIncrease)
{
prepareLiabilities(ltx, header);
}
}
}