-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLedgerManagerImpl.cpp
More file actions
3066 lines (2741 loc) · 107 KB
/
LedgerManagerImpl.cpp
File metadata and controls
3066 lines (2741 loc) · 107 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
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 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 "ledger/LedgerManagerImpl.h"
#include "bucket/BucketManager.h"
#include "bucket/BucketSnapshotManager.h"
#include "bucket/HotArchiveBucketList.h"
#include "bucket/LiveBucketList.h"
#include "catchup/AssumeStateWork.h"
#include "crypto/Hex.h"
#include "crypto/KeyUtils.h"
#include "crypto/SHA.h"
#include "crypto/SecretKey.h"
#include "database/Database.h"
#include "herder/Herder.h"
#include "herder/HerderPersistence.h"
#include "herder/LedgerCloseData.h"
#include "herder/TxSetFrame.h"
#include "herder/Upgrades.h"
#include "history/HistoryManager.h"
#include "invariant/InvariantDoesNotHold.h"
#include "invariant/InvariantManager.h"
#include "ledger/FlushAndRotateMetaDebugWork.h"
#include "ledger/LedgerEntryScope.h"
#include "ledger/LedgerHeaderUtils.h"
#include "ledger/LedgerManager.h"
#include "ledger/LedgerTxn.h"
#include "ledger/LedgerTxnEntry.h"
#include "ledger/LedgerTxnHeader.h"
#include "ledger/LedgerTypeUtils.h"
#include "ledger/P23HotArchiveBug.h"
#include "ledger/SharedModuleCacheCompiler.h"
#include "main/Application.h"
#include "main/Config.h"
#include "main/ErrorMessages.h"
#include "rust/RustBridge.h"
#include "transactions/MutableTransactionResult.h"
#include "transactions/OperationFrame.h"
#include "transactions/ParallelApplyUtils.h"
#include "transactions/TransactionFrameBase.h"
#include "transactions/TransactionMeta.h"
#include "transactions/TransactionUtils.h"
#include "util/DebugMetaUtils.h"
#include "util/Decoder.h"
#include "util/Fs.h"
#include "util/GlobalChecks.h"
#include "util/JitterInjection.h"
#include "util/LogSlowExecution.h"
#include "util/Logging.h"
#include "util/MetricsRegistry.h"
#include "util/ProtocolVersion.h"
#include "util/XDRCereal.h"
#include "util/XDRStream.h"
#include "util/types.h"
#include "work/WorkScheduler.h"
#include "xdr/Stellar-ledger-entries.h"
#include "xdrpp/printer.h"
#include <cstdint>
#include <fmt/format.h>
#ifdef BUILD_TESTS
#include "test/TxTests.h"
#endif
#include "xdr/Stellar-ledger-entries.h"
#include "xdr/Stellar-ledger.h"
#include "xdr/Stellar-transaction.h"
#include "xdrpp/types.h"
#include "medida/buckets.h"
#include "medida/counter.h"
#include "medida/meter.h"
#include "medida/timer.h"
#include <Tracy.hpp>
#include "LedgerManagerImpl.h"
#include <chrono>
#include <memory>
#include <mutex>
#include <optional>
#include <regex>
#include <sstream>
#include <stdexcept>
#include <thread>
/*
The ledger module:
1) gets the externalized tx set
2) applies this set to the last closed ledger
3) sends the changed entries to the BucketList
4) saves the changed entries to SQL
5) saves the ledger hash and header to SQL
6) sends the new ledger hash and the tx set to the history
7) sends the new ledger hash and header to the Herder
catching up to network:
1) Wait for SCP to tell us what the network is on now
2) Pull history log or static deltas from history archive
3) Replay or force-apply deltas, depending on catchup mode
*/
using namespace std;
namespace stellar
{
uint32_t const LedgerManager::GENESIS_LEDGER_SEQ = 1;
uint32_t const LedgerManager::GENESIS_LEDGER_VERSION = 0;
uint32_t const LedgerManager::GENESIS_LEDGER_BASE_FEE = 100;
uint32_t const LedgerManager::GENESIS_LEDGER_BASE_RESERVE = 100000000;
uint32_t const LedgerManager::GENESIS_LEDGER_MAX_TX_SIZE = 100;
int64_t const LedgerManager::GENESIS_LEDGER_TOTAL_COINS = 1000000000000000000;
namespace
{
std::vector<uint32_t>
getModuleCacheProtocols()
{
std::vector<uint32_t> ledgerVersions;
for (uint32_t i = (uint32_t)REUSABLE_SOROBAN_MODULE_CACHE_PROTOCOL_VERSION;
i <= Config::CURRENT_LEDGER_PROTOCOL_VERSION; i++)
{
ledgerVersions.push_back(i);
}
auto extra = getenv("SOROBAN_TEST_EXTRA_PROTOCOL");
if (extra)
{
uint32_t proto = static_cast<uint32_t>(atoi(extra));
if (proto > 0)
{
ledgerVersions.push_back(proto);
}
}
return ledgerVersions;
}
void
setLedgerTxnHeader(LedgerHeader const& lh, Application& app)
{
LedgerTxn ltx(app.getLedgerTxnRoot());
ltx.loadHeader().current() = lh;
ltx.commit();
}
bool
mergeOpInTx(std::vector<Operation> const& ops)
{
for (auto const& op : ops)
{
if (op.body.type() == ACCOUNT_MERGE)
{
return true;
}
}
return false;
}
}
std::unique_ptr<LedgerManager>
LedgerManager::create(Application& app)
{
return std::make_unique<LedgerManagerImpl>(app);
}
std::string
LedgerManager::ledgerAbbrev(LedgerHeader const& header)
{
return ledgerAbbrev(header, xdrSha256(header));
}
std::string
LedgerManager::ledgerAbbrev(uint32_t seq, uint256 const& hash)
{
std::ostringstream oss;
oss << "[seq=" << seq << ", hash=" << hexAbbrev(hash) << "]";
return oss.str();
}
std::string
LedgerManager::ledgerAbbrev(LedgerHeader const& header, uint256 const& hash)
{
return ledgerAbbrev(header.ledgerSeq, hash);
}
std::string
LedgerManager::ledgerAbbrev(LedgerHeaderHistoryEntry const& he)
{
return ledgerAbbrev(he.header, he.hash);
}
LedgerManagerImpl::LedgerApplyMetrics::LedgerApplyMetrics(
MetricsRegistry& registry)
: mSorobanMetrics(registry)
, mTransactionApply(registry.NewTimer({"ledger", "transaction", "apply"}))
, mTotalTxApply(registry.NewTimer({"ledger", "transaction", "total-apply"}))
, mTransactionCount(
registry.NewHistogram({"ledger", "transaction", "count"}))
, mOperationCount(registry.NewHistogram({"ledger", "operation", "count"}))
, mPrefetchHitRate(
registry.NewHistogram({"ledger", "prefetch", "hit-rate"}))
, mLedgerClose(registry.NewTimer({"ledger", "ledger", "close"}))
, mLedgerAgeClosed(registry.NewBuckets({"ledger", "age", "closed"},
{5000.0, 7000.0, 10000.0, 20000.0}))
, mLedgerAge(registry.NewCounter({"ledger", "age", "current-seconds"}))
, mTransactionApplySucceeded(
registry.NewCounter({"ledger", "apply", "success"}))
, mTransactionApplyFailed(
registry.NewCounter({"ledger", "apply", "failure"}))
, mSorobanTransactionApplySucceeded(
registry.NewCounter({"ledger", "apply-soroban", "success"}))
, mSorobanTransactionApplyFailed(
registry.NewCounter({"ledger", "apply-soroban", "failure"}))
, mMaxClustersPerLedger(
registry.NewCounter({"ledger", "apply-soroban", "max-clusters"}))
, mStagesPerLedger(
registry.NewCounter({"ledger", "apply-soroban", "stages"}))
, mMetaStreamBytes(
registry.NewMeter({"ledger", "metastream", "bytes"}, "byte"))
, mMetaStreamWriteTime(registry.NewTimer({"ledger", "metastream", "write"}))
{
}
LedgerManagerImpl::ApplyState::ApplyState(Application& app)
: mMetrics(app.getMetrics())
, mAppConnector(app.getAppConnector())
, mModuleCache(::rust_bridge::new_module_cache())
, mModuleCacheProtocols(getModuleCacheProtocols())
, mNumCompilationThreads(app.getConfig().COMPILATION_THREADS)
{
}
LedgerManagerImpl::LedgerApplyMetrics&
LedgerManagerImpl::ApplyState::getMetrics()
{
return mMetrics;
}
InMemorySorobanState const&
LedgerManagerImpl::ApplyState::getInMemorySorobanState() const
{
releaseAssert(mPhase == Phase::APPLYING ||
mPhase == Phase::SETTING_UP_STATE ||
mPhase == Phase::READY_TO_APPLY);
return mInMemorySorobanState;
}
#ifdef BUILD_TESTS
InMemorySorobanState&
LedgerManagerImpl::ApplyState::getInMemorySorobanStateForTesting()
{
return mInMemorySorobanState;
}
::rust::Box<rust_bridge::SorobanModuleCache> const&
LedgerManagerImpl::ApplyState::getModuleCacheForTesting()
{
return mModuleCache;
}
uint64_t
LedgerManagerImpl::ApplyState::getSorobanInMemoryStateSizeForTesting() const
{
return mInMemorySorobanState.getSize();
}
#endif
void
LedgerManagerImpl::ApplyState::threadInvariant() const
{
if (mAppConnector.getConfig().parallelLedgerClose())
{
releaseAssert(threadIsMain() || mAppConnector.threadIsType(
Application::ThreadType::APPLY));
}
else
{
releaseAssert(threadIsMain());
}
}
::rust::Box<rust_bridge::SorobanModuleCache> const&
LedgerManagerImpl::ApplyState::getModuleCache() const
{
releaseAssert(mPhase == Phase::APPLYING);
return mModuleCache;
}
void
LedgerManagerImpl::markApplyStateReset()
{
mApplyState.resetToSetupPhase();
}
bool
LedgerManagerImpl::ApplyState::isCompilationRunning() const
{
return static_cast<bool>(mCompiler);
}
void
LedgerManagerImpl::ApplyState::updateInMemorySorobanState(
std::vector<LedgerEntry> const& initEntries,
std::vector<LedgerEntry> const& liveEntries,
std::vector<LedgerKey> const& deadEntries, LedgerHeader const& lh,
std::optional<SorobanNetworkConfig const> const& sorobanConfig)
{
assertWritablePhase();
mInMemorySorobanState.updateState(initEntries, liveEntries, deadEntries, lh,
sorobanConfig,
getMetrics().mSorobanMetrics);
}
uint64_t
LedgerManagerImpl::ApplyState::getSorobanInMemoryStateSize() const
{
// This assert is not strictly necessary, but we don't really want to
// access the state size outside of the snapshotting process during the
// LEDGER_CLOSE or SETTING_UP_STATE phase.
assertWritablePhase();
return mInMemorySorobanState.getSize();
}
void
LedgerManagerImpl::ApplyState::manuallyAdvanceLedgerHeader(
LedgerHeader const& lh)
{
assertCommittingPhase();
mInMemorySorobanState.manuallyAdvanceLedgerHeader(lh);
}
LedgerManagerImpl::LedgerManagerImpl(Application& app)
: mApp(app)
, mApplyState(app)
, mLastClosedLedgerState(std::make_shared<CompleteConstLedgerState>(
nullptr, nullptr, LedgerHeaderHistoryEntry(), HistoryArchiveState()))
, mLastClose(mApp.getClock().now())
, mCatchupDuration(
app.getMetrics().NewTimer({"ledger", "catchup", "duration"}))
, mState(LM_BOOTING_STATE)
{
setupLedgerCloseMetaStream();
}
void
LedgerManagerImpl::moveToSynced()
{
setState(LM_SYNCED_STATE);
}
void
LedgerManagerImpl::beginApply()
{
releaseAssert(threadIsMain());
// Go into "applying" state, this will prevent catchup from starting
mCurrentlyApplyingLedger = true;
}
void
LedgerManagerImpl::setState(State s)
{
releaseAssert(threadIsMain());
if (s != getState())
{
std::string oldState = getStateHuman();
mState = s;
mApp.syncOwnMetrics();
CLOG_INFO(Ledger, "Changing state {} -> {}", oldState, getStateHuman());
if (mState != LM_CATCHING_UP_STATE)
{
mApp.getLedgerApplyManager().logAndUpdateCatchupStatus(true);
}
}
}
LedgerManager::State
LedgerManagerImpl::getState() const
{
return mState;
}
std::string
LedgerManagerImpl::getStateHuman() const
{
static std::array<char const*, LM_NUM_STATE> stateStrings = std::array{
"LM_BOOTING_STATE", "LM_SYNCED_STATE", "LM_CATCHING_UP_STATE"};
return std::string(stateStrings[getState()]);
}
LedgerHeader
LedgerManager::genesisLedger()
{
LedgerHeader result;
// all fields are initialized by default to 0
// set the ones that are not 0
result.ledgerVersion = GENESIS_LEDGER_VERSION;
result.baseFee = GENESIS_LEDGER_BASE_FEE;
result.baseReserve = GENESIS_LEDGER_BASE_RESERVE;
result.maxTxSetSize = GENESIS_LEDGER_MAX_TX_SIZE;
result.totalCoins = GENESIS_LEDGER_TOTAL_COINS;
result.ledgerSeq = GENESIS_LEDGER_SEQ;
return result;
}
void
LedgerManagerImpl::startNewLedger(LedgerHeader const& genesisLedger)
{
mApplyState.assertSetupPhase();
auto ledgerTime = mApplyState.getMetrics().mLedgerClose.TimeScope();
SecretKey skey = SecretKey::fromSeed(mApp.getNetworkID());
LedgerTxn ltx(mApp.getLedgerTxnRoot(), false);
auto const& cfg = mApp.getConfig();
ltx.loadHeader().current() = genesisLedger;
if (cfg.USE_CONFIG_FOR_GENESIS)
{
SorobanNetworkConfig::initializeGenesisLedgerForTesting(
cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION, ltx, mApp);
}
LedgerEntry rootEntry;
rootEntry.lastModifiedLedgerSeq = 1;
rootEntry.data.type(ACCOUNT);
auto& rootAccount = rootEntry.data.account();
rootAccount.accountID = skey.getPublicKey();
rootAccount.thresholds[0] = 1;
rootAccount.balance = genesisLedger.totalCoins;
#ifdef BUILD_TESTS
// If test account creation is enabled, create additional accounts
if (cfg.GENESIS_TEST_ACCOUNT_COUNT > 0)
{
CLOG_INFO(Ledger, "Creating {} test accounts for genesis ledger",
cfg.GENESIS_TEST_ACCOUNT_COUNT);
// Split totalCoins evenly among all accounts (root + test accounts)
uint32_t totalAccounts =
cfg.GENESIS_TEST_ACCOUNT_COUNT + 1; // +1 for root account
int64_t baseAccountBalance = genesisLedger.totalCoins / totalAccounts;
int64_t remainder = genesisLedger.totalCoins % totalAccounts;
// Set root account balance to equal share plus any remainder
// to ensure we don't lose any coins due to rounding
rootAccount.balance = baseAccountBalance + remainder;
// Create accounts using similar approach as TxGenerator::createAccounts
for (uint32_t i = 0; i < cfg.GENESIS_TEST_ACCOUNT_COUNT; i++)
{
auto name = "TestAccount-" + std::to_string(i);
auto account = txtest::getAccount(name.c_str());
LedgerEntry testEntry;
testEntry.lastModifiedLedgerSeq = 1;
testEntry.data.type(ACCOUNT);
auto& testAccount = testEntry.data.account();
testAccount.accountID = account.getPublicKey();
testAccount.thresholds[0] = 1;
testAccount.balance = baseAccountBalance;
ltx.create(testEntry);
}
}
#endif
ltx.create(rootEntry);
CLOG_INFO(Ledger, "Established genesis ledger, closing");
CLOG_INFO(Ledger, "Root account: {}", skey.getStrKeyPublic());
CLOG_INFO(Ledger, "Root account seed: {}", skey.getStrKeySeed().value);
auto& appConnector = mApp.getAppConnector();
auto output = sealLedgerTxnAndStoreInBucketsAndDB(
appConnector.copySearchableLiveBucketListSnapshot(),
appConnector.copySearchableHotArchiveBucketListSnapshot(), ltx,
/*ledgerCloseMeta*/ nullptr,
/*initialLedgerVers*/ 0);
advanceLastClosedLedgerState(output);
ltx.commit();
// Note: We're still not done with LedgerManager initialization here, as we
// still need to call setLastClosedLedger to properly initialize
// LedgerManager after creating the genesis ledger.
}
void
LedgerManagerImpl::startNewLedger()
{
auto ledger = genesisLedger();
auto const& cfg = mApp.getConfig();
if (cfg.USE_CONFIG_FOR_GENESIS)
{
ledger.ledgerVersion = cfg.TESTING_UPGRADE_LEDGER_PROTOCOL_VERSION;
ledger.baseFee = cfg.TESTING_UPGRADE_DESIRED_FEE;
ledger.baseReserve = cfg.TESTING_UPGRADE_RESERVE;
ledger.maxTxSetSize = cfg.TESTING_UPGRADE_MAX_TX_SET_SIZE;
}
startNewLedger(ledger);
}
void
LedgerManagerImpl::loadLastKnownLedgerInternal(bool skipBuildingFullState)
{
ZoneScoped;
mApplyState.assertSetupPhase();
// Step 1. Load LCL state from the DB
HistoryArchiveState has;
has.fromString(mApp.getPersistentState().getState(
PersistentState::kHistoryArchiveState,
mApp.getDatabase().getSession()));
// Step 2. Restore LedgerHeader from storestate
std::optional<LedgerHeader> latestLedgerHeader;
std::string headerEncoded = mApp.getPersistentState().getState(
PersistentState::kLastClosedLedgerHeader,
mApp.getDatabase().getSession());
if (headerEncoded.empty())
{
throw std::runtime_error("Could not load ledger header from database");
}
auto currentLedger = std::make_shared<LedgerHeader>(
LedgerHeaderUtils::decodeFromData(headerEncoded));
if (currentLedger->ledgerSeq != has.currentLedger)
{
throw std::runtime_error("Invalid database state: last known "
"ledger does not agree with HAS");
}
CLOG_INFO(Ledger, "Loaded LCL header from database: {}",
ledgerAbbrev(*currentLedger));
setLedgerTxnHeader(*currentLedger, mApp);
latestLedgerHeader = *currentLedger;
releaseAssert(latestLedgerHeader.has_value());
auto missing = mApp.getBucketManager().checkForMissingBucketsFiles(has);
auto pubmissing =
mApp.getHistoryManager().getMissingBucketsReferencedByPublishQueue();
missing.insert(missing.end(), pubmissing.begin(), pubmissing.end());
if (!missing.empty())
{
CLOG_ERROR(Ledger, "{} buckets are missing from bucket directory '{}'",
missing.size(), mApp.getBucketManager().getBucketDir());
throw std::runtime_error("Bucket directory is corrupt");
}
// Only restart merges in full startup mode. Many modes in core
// (standalone offline commands, in-memory setup) do not need to
// spin up expensive merge processes.
auto assumeStateWork = mApp.getWorkScheduler().executeWork<AssumeStateWork>(
has, latestLedgerHeader->ledgerVersion,
/* restartMerges */ !skipBuildingFullState);
if (assumeStateWork->getState() == BasicWork::State::WORK_SUCCESS)
{
CLOG_INFO(Ledger, "Assumed bucket-state for LCL: {}",
ledgerAbbrev(*latestLedgerHeader));
}
else
{
// Work should only fail during graceful shutdown
releaseAssertOrThrow(mApp.isStopping());
}
// Step 4. Restore LedgerManager's LCL state
advanceLastClosedLedgerState(
advanceBucketListSnapshotAndMakeLedgerState(*latestLedgerHeader, has));
// Maybe truncate checkpoint files if we're restarting after a crash
// in applyLedger (in which case any modifications to the ledger state have
// been rolled back)
mApp.getHistoryManager().restoreCheckpoint(latestLedgerHeader->ledgerSeq);
// Prime module cache with LCL state, not apply-state. This is acceptable
// here because we just started and there is no apply-state yet and no apply
// thread to hold such state.
auto const& snapshot = mLastClosedLedgerState->getBucketSnapshot();
if (!skipBuildingFullState)
{
mApplyState.compileAllContractsInLedger(
snapshot, latestLedgerHeader->ledgerVersion);
mApplyState.populateInMemorySorobanState(
snapshot, latestLedgerHeader->ledgerVersion);
}
if (!skipBuildingFullState)
{
maybeRunSnapshotInvariantFromLedgerState(
mLastClosedLedgerState, maybeCopySorobanStateForInvariant(),
/* runInParallel */ false);
}
mApplyState.markEndOfSetupPhase();
}
void
LedgerManagerImpl::loadLastKnownLedger()
{
loadLastKnownLedgerInternal(/* skipBuildingFullState */ false);
}
void
LedgerManagerImpl::partiallyLoadLastKnownLedgerForUtils()
{
loadLastKnownLedgerInternal(/* skipBuildingFullState */ true);
}
Database&
LedgerManagerImpl::getDatabase()
{
return mApp.getDatabase();
}
uint32_t
LedgerManagerImpl::getLastMaxTxSetSize() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->getLastClosedLedgerHeader()
.header.maxTxSetSize;
}
uint32_t
LedgerManagerImpl::getLastMaxTxSetSizeOps() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
auto n =
mLastClosedLedgerState->getLastClosedLedgerHeader().header.maxTxSetSize;
return protocolVersionStartsFrom(
mLastClosedLedgerState->getLastClosedLedgerHeader()
.header.ledgerVersion,
ProtocolVersion::V_11)
? n
: (n * MAX_OPS_PER_TX);
}
Resource
LedgerManagerImpl::maxLedgerResources(bool isSoroban)
{
ZoneScoped;
if (isSoroban)
{
return getLastClosedSorobanNetworkConfig().maxLedgerResources();
}
else
{
uint32_t maxOpsLedger = getLastMaxTxSetSizeOps();
return Resource(maxOpsLedger);
}
}
Resource
LedgerManagerImpl::maxSorobanTransactionResources()
{
ZoneScoped;
auto const& conf =
mApp.getLedgerManager().getLastClosedSorobanNetworkConfig();
int64_t const opCount = 1;
std::vector<int64_t> limits = {opCount,
conf.txMaxInstructions(),
conf.txMaxSizeBytes(),
conf.txMaxDiskReadBytes(),
conf.txMaxWriteBytes(),
conf.txMaxDiskReadEntries(),
conf.txMaxWriteLedgerEntries()};
return Resource(limits);
}
int64_t
LedgerManagerImpl::getLastMinBalance(uint32_t ownerCount) const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
auto const& lh = mLastClosedLedgerState->getLastClosedLedgerHeader().header;
if (protocolVersionIsBefore(lh.ledgerVersion, ProtocolVersion::V_9))
return (2 + ownerCount) * lh.baseReserve;
else
return (2LL + ownerCount) * int64_t(lh.baseReserve);
}
uint32_t
LedgerManagerImpl::getLastReserve() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->getLastClosedLedgerHeader()
.header.baseReserve;
}
uint32_t
LedgerManagerImpl::getLastTxFee() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->getLastClosedLedgerHeader().header.baseFee;
}
LedgerHeaderHistoryEntry const&
LedgerManagerImpl::getLastClosedLedgerHeader() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->getLastClosedLedgerHeader();
}
HistoryArchiveState
LedgerManagerImpl::getLastClosedLedgerHAS() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->getLastClosedHistoryArchiveState();
}
uint32_t
LedgerManagerImpl::getLastClosedLedgerNum() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->getLastClosedLedgerHeader().header.ledgerSeq;
}
std::shared_ptr<InMemorySorobanState const>
LedgerManagerImpl::maybeCopySorobanStateForInvariant()
{
std::shared_ptr<InMemorySorobanState const> inMemorySnapshotForInvariant =
nullptr;
if (mApp.getInvariantManager().shouldRunInvariantSnapshot())
{
// The in memory state copy is expensive, so we need to mark
// that start of the invariant scan here, not in the callback, to ensure
// we don't trigger a race condition that creates two copies.
mApp.getInvariantManager().markStartOfInvariantSnapshot();
inMemorySnapshotForInvariant =
std::make_shared<InMemorySorobanState const>(
mApplyState.getInMemorySorobanState());
}
return inMemorySnapshotForInvariant;
}
void
LedgerManagerImpl::maybeRunSnapshotInvariantFromLedgerState(
CompleteConstLedgerStatePtr const& ledgerState,
std::shared_ptr<InMemorySorobanState const> inMemorySnapshotForInvariant,
bool runInParallel) const
{
releaseAssert(threadIsMain());
if (!inMemorySnapshotForInvariant ||
!mApp.getConfig().INVARIANT_EXTRA_CHECKS || mApp.isStopping())
{
return;
}
// Verify consistency of all snapshot state.
auto ledgerSeq = ledgerState->getLastClosedLedgerHeader().header.ledgerSeq;
inMemorySnapshotForInvariant->assertLastClosedLedger(ledgerSeq);
// Copy snapshots from ledgerState to ensure consistency with the
// in-memory Soroban state
auto liveSnapshotCopy =
BucketSnapshotManager::copySearchableLiveBucketListSnapshot(
ledgerState->getBucketSnapshot(), mApp.getMetrics());
auto hotArchiveSnapshotCopy =
BucketSnapshotManager::copySearchableHotArchiveBucketListSnapshot(
ledgerState->getHotArchiveSnapshot(), mApp.getMetrics());
releaseAssertOrThrow(liveSnapshotCopy->getLedgerSeq() == ledgerSeq);
releaseAssertOrThrow(hotArchiveSnapshotCopy->getLedgerSeq() == ledgerSeq);
// Note: No race condition acquiring app by reference, as all worker
// threads are joined before application destruction.
auto cb = [liveSnapshot = liveSnapshotCopy,
hotArchiveSnapshot = hotArchiveSnapshotCopy, &app = mApp,
inMemorySnapshotForInvariant]() {
app.getInvariantManager().runStateSnapshotInvariant(
liveSnapshot, hotArchiveSnapshot, *inMemorySnapshotForInvariant,
[&app]() { return app.isStopping(); });
};
if (runInParallel)
{
mApp.postOnBackgroundThread(std::move(cb), "checkSnapshot");
}
else
{
cb();
}
}
SorobanNetworkConfig const&
LedgerManagerImpl::getLastClosedSorobanNetworkConfig() const
{
releaseAssert(threadIsMain());
releaseAssert(hasLastClosedSorobanNetworkConfig());
return mLastClosedLedgerState->getSorobanConfig();
}
bool
LedgerManagerImpl::hasLastClosedSorobanNetworkConfig() const
{
releaseAssert(threadIsMain());
releaseAssert(mLastClosedLedgerState);
return mLastClosedLedgerState->hasSorobanConfig();
}
std::chrono::milliseconds
LedgerManagerImpl::getExpectedLedgerCloseTime() const
{
releaseAssert(threadIsMain());
auto const& cfg = mApp.getConfig();
if (auto overrideOp = cfg.getExpectedLedgerCloseTimeTestingOverride();
overrideOp.has_value())
{
return *overrideOp;
}
auto const& lcl = getLastClosedLedgerHeader();
if (protocolVersionStartsFrom(lcl.header.ledgerVersion,
ProtocolVersion::V_23))
{
auto const& networkConfig = getLastClosedSorobanNetworkConfig();
return std::chrono::milliseconds(
networkConfig.ledgerTargetCloseTimeMilliseconds());
}
return Herder::TARGET_LEDGER_CLOSE_TIME_BEFORE_PROTOCOL_VERSION_23_MS;
}
#ifdef BUILD_TESTS
std::vector<TransactionMetaFrame> const&
LedgerManagerImpl::getLastClosedLedgerTxMeta()
{
return mLastLedgerTxMeta;
}
std::optional<LedgerCloseMetaFrame> const&
LedgerManagerImpl::getLastClosedLedgerCloseMeta()
{
return mLastLedgerCloseMeta;
}
void
LedgerManagerImpl::storeCurrentLedgerForTest(LedgerHeader const& header)
{
storePersistentStateAndLedgerHeaderInDB(header, true);
}
InMemorySorobanState const&
LedgerManagerImpl::getInMemorySorobanStateForTesting()
{
return mApplyState.getInMemorySorobanStateForTesting();
}
CompleteConstLedgerStatePtr
LedgerManagerImpl::getLastClosedLedgerStateForTesting()
{
return mLastClosedLedgerState;
}
void
LedgerManagerImpl::rebuildInMemorySorobanStateForTesting(uint32_t ledgerVersion)
{
mApplyState.resetToSetupPhase();
mApplyState.getInMemorySorobanStateForTesting().clearForTesting();
mApplyState.populateInMemorySorobanState(
mLastClosedLedgerState->getBucketSnapshot(), ledgerVersion);
mApplyState.markEndOfSetupPhase();
}
::rust::Box<rust_bridge::SorobanModuleCache>
LedgerManagerImpl::getModuleCacheForTesting()
{
releaseAssert(!mApplyState.isCompilationRunning());
return mApplyState.getModuleCacheForTesting()->shallow_clone();
}
uint64_t
LedgerManagerImpl::getSorobanInMemoryStateSizeForTesting()
{
return mApplyState.getSorobanInMemoryStateSizeForTesting();
}
#endif
SorobanMetrics&
LedgerManagerImpl::getSorobanMetrics()
{
return mApplyState.getMetrics().mSorobanMetrics;
}
std::unique_ptr<LedgerTxnRoot>
LedgerManagerImpl::createLedgerTxnRoot(Application& app, size_t entryCacheSize,
size_t prefetchBatchSize
#ifdef BEST_OFFER_DEBUGGING
,
bool bestOfferDebuggingEnabled
#endif
)
{
return std::make_unique<LedgerTxnRoot>(
app, mApplyState.getInMemorySorobanState(), entryCacheSize,
prefetchBatchSize
#ifdef BEST_OFFER_DEBUGGING
,
bestOfferDebuggingEnabled
#endif
);
}
::rust::Box<rust_bridge::SorobanModuleCache>
LedgerManagerImpl::getModuleCache()
{
// There should not be any compilation running when
// anyone calls this function. It is accessed from
// transactions during apply only.
releaseAssert(!mApplyState.isCompilationRunning());
return mApplyState.getModuleCache()->shallow_clone();
}
void
LedgerManagerImpl::handleUpgradeAffectingSorobanInMemoryStateSize(
AbstractLedgerTxn& upgradeLtx)
{
mApplyState.handleUpgradeAffectingSorobanInMemoryStateSize(upgradeLtx);
}
void
LedgerManagerImpl::ApplyState::handleUpgradeAffectingSorobanInMemoryStateSize(
AbstractLedgerTxn& upgradeLtx)
{
assertCommittingPhase();
// Load the current network from the ledger. It might be in some
// intermediate state, which is fine, because we call this only after
// a relevant section has been upgraded and all the remaining sections
// are not relevant for the size computation.
auto currentConfig = SorobanNetworkConfig::loadFromLedger(upgradeLtx);
auto upgradeLedgerVersion = upgradeLtx.loadHeader().current().ledgerVersion;
mInMemorySorobanState.recomputeContractCodeSize(currentConfig,
upgradeLedgerVersion);
// We need to record the updated size, but only when we're in p23+, as
// before that we store BL size instead.
if (protocolVersionStartsFrom(upgradeLedgerVersion, ProtocolVersion::V_23))
{
SorobanNetworkConfig::updateRecomputedSorobanStateSize(
mInMemorySorobanState.getSize(), upgradeLtx);
}
}
void
LedgerManagerImpl::ApplyState::finishPendingCompilation()
{
assertWritablePhase();
releaseAssert(mCompiler);
auto newCache = mCompiler->wait();
getMetrics().mSorobanMetrics.mModuleCacheRebuildBytes.set_count(
(int64)mCompiler->getBytesCompiled());
getMetrics().mSorobanMetrics.mModuleCacheNumEntries.set_count(
(int64)mCompiler->getContractsCompiled());
getMetrics().mSorobanMetrics.mModuleCacheRebuildTime.Update(
mCompiler->getCompileTime());
mModuleCache.swap(newCache);
mCompiler.reset();
}
void
LedgerManagerImpl::ApplyState::compileAllContractsInLedger(
SearchableSnapshotConstPtr snap, uint32_t minLedgerVersion)
{
assertSetupPhase();
startCompilingAllContracts(snap, minLedgerVersion);
finishPendingCompilation();
}
void
LedgerManagerImpl::ApplyState::populateInMemorySorobanState(
SearchableSnapshotConstPtr snap, uint32_t ledgerVersion)
{
assertSetupPhase();
mInMemorySorobanState.initializeStateFromSnapshot(snap, ledgerVersion);
}
void
LedgerManagerImpl::ApplyState::assertCommittingPhase() const
{
threadInvariant();
releaseAssert(mPhase == Phase::COMMITTING);
}
void
LedgerManagerImpl::ApplyState::markStartOfApplying()
{
threadInvariant();