-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathLedgerReplay_test.cpp
More file actions
1508 lines (1352 loc) · 45.7 KB
/
LedgerReplay_test.cpp
File metadata and controls
1508 lines (1352 loc) · 45.7 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
#include <test/jtx.h>
#include <test/jtx/envconfig.h>
#include <xrpld/app/ledger/BuildLedger.h>
#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/app/ledger/LedgerReplay.h>
#include <xrpld/app/ledger/LedgerReplayTask.h>
#include <xrpld/app/ledger/LedgerReplayer.h>
#include <xrpld/app/ledger/detail/LedgerDeltaAcquire.h>
#include <xrpld/app/ledger/detail/LedgerReplayMsgHandler.h>
#include <xrpld/app/ledger/detail/SkipListAcquire.h>
#include <xrpld/overlay/PeerSet.h>
#include <xrpld/overlay/detail/PeerImp.h>
#include <xrpl/basics/Slice.h>
#include <chrono>
#include <thread>
namespace xrpl {
namespace test {
struct LedgerReplay_test : public beast::unit_test::suite
{
void
run() override
{
testcase("Replay ledger");
using namespace jtx;
// Build a ledger normally
auto const alice = Account("alice");
auto const bob = Account("bob");
Env env(*this);
env.fund(XRP(100000), alice, bob);
env.close();
LedgerMaster& ledgerMaster = env.app().getLedgerMaster();
auto const lastClosed = ledgerMaster.getClosedLedger();
auto const lastClosedParent = ledgerMaster.getLedgerByHash(lastClosed->header().parentHash);
auto const replayed = buildLedger(
LedgerReplay(lastClosedParent, lastClosed), tapNONE, env.app(), env.journal);
BEAST_EXPECT(replayed->header().hash == lastClosed->header().hash);
}
};
enum class InboundLedgersBehavior {
Good,
DropAll,
};
/**
* Simulate a network InboundLedgers.
* Depending on the configured InboundLedgersBehavior,
* it either provides the ledger or not
*/
class MagicInboundLedgers : public InboundLedgers
{
public:
MagicInboundLedgers(
LedgerMaster& ledgerSource,
LedgerMaster& ledgerSink,
InboundLedgersBehavior bhvr)
: ledgerSource(ledgerSource), ledgerSink(ledgerSink), bhvr(bhvr)
{
}
virtual ~MagicInboundLedgers() = default;
virtual std::shared_ptr<Ledger const>
acquire(uint256 const& hash, std::uint32_t seq, InboundLedger::Reason) override
{
if (bhvr == InboundLedgersBehavior::DropAll)
return {};
if (auto l = ledgerSource.getLedgerByHash(hash); l)
{
ledgerSink.storeLedger(l);
return l;
}
return {};
}
virtual void
acquireAsync(uint256 const& hash, std::uint32_t seq, InboundLedger::Reason reason) override
{
}
virtual std::shared_ptr<InboundLedger>
find(LedgerHash const& hash) override
{
return {};
}
virtual bool
gotLedgerData(
LedgerHash const& ledgerHash,
std::shared_ptr<Peer>,
std::shared_ptr<protocol::TMLedgerData>) override
{
return false;
}
virtual void
gotStaleData(std::shared_ptr<protocol::TMLedgerData> packet) override
{
}
virtual void
logFailure(uint256 const& h, std::uint32_t seq) override
{
}
virtual bool
isFailure(uint256 const& h) override
{
return false;
}
virtual void
clearFailures() override
{
}
virtual Json::Value
getInfo() override
{
return {};
}
virtual std::size_t
fetchRate() override
{
return 0;
}
virtual void
onLedgerFetched() override
{
}
virtual void
gotFetchPack() override
{
}
virtual void
sweep() override
{
}
virtual void
stop() override
{
}
virtual size_t
cacheSize() override
{
return 0;
}
LedgerMaster& ledgerSource;
LedgerMaster& ledgerSink;
InboundLedgersBehavior bhvr;
};
enum class PeerFeature {
LedgerReplayEnabled,
None,
};
/**
* Simulate a network peer.
* Depending on the configured PeerFeature,
* it either supports the ProtocolFeature::LedgerReplay or not
*/
class TestPeer : public Peer
{
public:
TestPeer(bool enableLedgerReplay)
: ledgerReplayEnabled_(enableLedgerReplay)
, nodePublicKey_(derivePublicKey(KeyType::ed25519, randomSecretKey()))
{
}
void
send(std::shared_ptr<Message> const& m) override
{
}
beast::IP::Endpoint
getRemoteAddress() const override
{
return {};
}
void
charge(Resource::Charge const& fee, std::string const& context = {}) override
{
}
id_t
id() const override
{
return 1234;
}
bool
cluster() const override
{
return false;
}
bool
isHighLatency() const override
{
return false;
}
int
getScore(bool) const override
{
return 0;
}
PublicKey const&
getNodePublic() const override
{
return nodePublicKey_;
}
Json::Value
json() override
{
return {};
}
bool
supportsFeature(ProtocolFeature f) const override
{
return f == ProtocolFeature::LedgerReplay && ledgerReplayEnabled_;
}
std::optional<std::size_t>
publisherListSequence(PublicKey const&) const override
{
return {};
}
void
setPublisherListSequence(PublicKey const&, std::size_t const) override
{
}
uint256 const&
getClosedLedgerHash() const override
{
static uint256 hash{};
return hash;
}
bool
hasLedger(uint256 const& hash, std::uint32_t seq) const override
{
return true;
}
void
ledgerRange(std::uint32_t& minSeq, std::uint32_t& maxSeq) const override
{
}
bool
hasTxSet(uint256 const& hash) const override
{
return false;
}
void
cycleStatus() override
{
}
bool
hasRange(std::uint32_t uMin, std::uint32_t uMax) override
{
return false;
}
bool
compressionEnabled() const override
{
return false;
}
void
sendTxQueue() override
{
}
void
addTxQueue(uint256 const&) override
{
}
void
removeTxQueue(uint256 const&) override
{
}
bool
txReduceRelayEnabled() const override
{
return false;
}
std::string const&
fingerprint() const override
{
return fingerprint_;
}
std::string fingerprint_;
bool ledgerReplayEnabled_;
PublicKey nodePublicKey_;
};
enum class PeerSetBehavior {
Good,
Drop50,
DropAll,
DropSkipListReply,
DropLedgerDeltaReply,
Repeat,
};
/**
* Simulate a peerSet that supplies peers to ledger replay subtasks.
* It connects the ledger replay client side and server side message handlers.
* Depending on the configured PeerSetBehavior,
* it may drop or repeat some of the messages.
*/
struct TestPeerSet : public PeerSet
{
TestPeerSet(
LedgerReplayMsgHandler& me,
LedgerReplayMsgHandler& other,
PeerSetBehavior bhvr,
bool enableLedgerReplay)
: local(me)
, remote(other)
, dummyPeer(std::make_shared<TestPeer>(enableLedgerReplay))
, behavior(bhvr)
{
}
void
addPeers(
std::size_t limit,
std::function<bool(std::shared_ptr<Peer> const&)> hasItem,
std::function<void(std::shared_ptr<Peer> const&)> onPeerAdded) override
{
hasItem(dummyPeer);
onPeerAdded(dummyPeer);
}
void
sendRequest(
::google::protobuf::Message const& msg,
protocol::MessageType type,
std::shared_ptr<Peer> const& peer) override
{
int dropRate = 0;
if (behavior == PeerSetBehavior::Drop50)
{
dropRate = 50;
}
else if (behavior == PeerSetBehavior::DropAll)
{
dropRate = 100;
}
if (((rand() % 100) + 1) <= dropRate)
return;
switch (type)
{
case protocol::mtPROOF_PATH_REQ: {
if (behavior == PeerSetBehavior::DropSkipListReply)
return;
auto request = std::make_shared<protocol::TMProofPathRequest>(
dynamic_cast<protocol::TMProofPathRequest const&>(msg));
auto reply = std::make_shared<protocol::TMProofPathResponse>(
remote.processProofPathRequest(request));
local.processProofPathResponse(reply);
if (behavior == PeerSetBehavior::Repeat)
local.processProofPathResponse(reply);
break;
}
case protocol::mtREPLAY_DELTA_REQ: {
if (behavior == PeerSetBehavior::DropLedgerDeltaReply)
return;
auto request = std::make_shared<protocol::TMReplayDeltaRequest>(
dynamic_cast<protocol::TMReplayDeltaRequest const&>(msg));
auto reply = std::make_shared<protocol::TMReplayDeltaResponse>(
remote.processReplayDeltaRequest(request));
local.processReplayDeltaResponse(reply);
if (behavior == PeerSetBehavior::Repeat)
local.processReplayDeltaResponse(reply);
break;
}
default:
return;
}
}
std::set<Peer::id_t> const&
getPeerIds() const override
{
static std::set<Peer::id_t> emptyPeers;
return emptyPeers;
}
LedgerReplayMsgHandler& local;
LedgerReplayMsgHandler& remote;
std::shared_ptr<TestPeer> dummyPeer;
PeerSetBehavior behavior;
};
/**
* Build the TestPeerSet.
*/
class TestPeerSetBuilder : public PeerSetBuilder
{
public:
TestPeerSetBuilder(
LedgerReplayMsgHandler& me,
LedgerReplayMsgHandler& other,
PeerSetBehavior bhvr,
PeerFeature peerFeature)
: local(me)
, remote(other)
, behavior(bhvr)
, enableLedgerReplay(peerFeature == PeerFeature::LedgerReplayEnabled)
{
}
std::unique_ptr<PeerSet>
build() override
{
return std::make_unique<TestPeerSet>(local, remote, behavior, enableLedgerReplay);
}
private:
LedgerReplayMsgHandler& local;
LedgerReplayMsgHandler& remote;
PeerSetBehavior behavior;
bool enableLedgerReplay;
};
/**
* Utility class for (1) creating ledgers with txns and
* (2) providing the ledgers via the ledgerMaster
*/
struct LedgerServer
{
struct Parameter
{
int initLedgers;
int initAccounts = 10;
int initAmount = 1'000'000;
int numTxPerLedger = 10;
int txAmount = 10;
};
LedgerServer(beast::unit_test::suite& suite, Parameter const& p)
: env(suite)
, app(env.app())
, ledgerMaster(env.app().getLedgerMaster())
, msgHandler(env.app(), env.app().getLedgerReplayer())
, param(p)
{
assert(param.initLedgers > 0);
createAccounts(param.initAccounts);
createLedgerHistory();
app.getLogs().threshold(beast::severities::kWarning);
}
/**
* @note close a ledger
*/
void
createAccounts(int newAccounts)
{
auto fundedAccounts = accounts.size();
for (int i = 0; i < newAccounts; ++i)
{
accounts.emplace_back("alice_" + std::to_string(fundedAccounts + i));
env.fund(jtx::XRP(param.initAmount), accounts.back());
}
env.close();
}
/**
* @note close a ledger
*/
void
sendPayments(int newTxes)
{
int fundedAccounts = accounts.size();
assert(fundedAccounts >= newTxes);
std::unordered_set<int> senders;
// somewhat random but reproducible
int r = ledgerMaster.getClosedLedger()->seq() * 7;
int fromIdx = 0;
int toIdx = 0;
auto updateIdx = [&]() {
assert(fundedAccounts > senders.size());
fromIdx = (fromIdx + r) % fundedAccounts;
while (senders.contains(fromIdx))
fromIdx = (fromIdx + 1) % fundedAccounts;
senders.insert(fromIdx);
toIdx = (toIdx + r * 2) % fundedAccounts;
if (toIdx == fromIdx)
toIdx = (toIdx + 1) % fundedAccounts;
};
for (int i = 0; i < newTxes; ++i)
{
updateIdx();
env(pay(accounts[fromIdx],
accounts[toIdx],
jtx::drops(ledgerMaster.getClosedLedger()->fees().base) +
jtx::XRP(param.txAmount)),
jtx::seq(jtx::autofill),
jtx::fee(jtx::autofill),
jtx::sig(jtx::autofill));
}
env.close();
}
/**
* create ledger history
*/
void
createLedgerHistory()
{
for (int i = 0; i < param.initLedgers - 1; ++i)
{
sendPayments(param.numTxPerLedger);
}
}
jtx::Env env;
Application& app;
LedgerMaster& ledgerMaster;
LedgerReplayMsgHandler msgHandler;
Parameter param;
std::vector<jtx::Account> accounts;
};
enum class TaskStatus {
Failed,
Completed,
NotDone,
NotExist,
};
/**
* Ledger replay client side.
* It creates the LedgerReplayer which has the client side logic.
* The client side and server side message handlers are connect via
* the peerSet to pass the requests and responses.
* It also has utility functions for checking task status
*/
class LedgerReplayClient
{
public:
LedgerReplayClient(
beast::unit_test::suite& suite,
LedgerServer& server,
PeerSetBehavior behavior = PeerSetBehavior::Good,
InboundLedgersBehavior inboundBhvr = InboundLedgersBehavior::Good,
PeerFeature peerFeature = PeerFeature::LedgerReplayEnabled)
: env(suite, jtx::envconfig(), nullptr, beast::severities::kDisabled)
, app(env.app())
, ledgerMaster(env.app().getLedgerMaster())
, inboundLedgers(server.app.getLedgerMaster(), ledgerMaster, inboundBhvr)
, serverMsgHandler(server.app, server.app.getLedgerReplayer())
, clientMsgHandler(env.app(), replayer)
, replayer(
env.app(),
inboundLedgers,
std::make_unique<TestPeerSetBuilder>(
clientMsgHandler,
serverMsgHandler,
behavior,
peerFeature))
{
}
void
addLedger(std::shared_ptr<Ledger const> const& l)
{
ledgerMaster.storeLedger(l);
}
bool
haveLedgers(uint256 const& finishLedgerHash, int totalReplay)
{
uint256 hash = finishLedgerHash;
int i = 0;
for (; i < totalReplay; ++i)
{
auto const l = ledgerMaster.getLedgerByHash(hash);
if (!l)
return false;
hash = l->header().parentHash;
}
return true;
}
bool
waitForLedgers(uint256 const& finishLedgerHash, int totalReplay)
{
int totalRound = 100;
for (int i = 0; i < totalRound; ++i)
{
if (haveLedgers(finishLedgerHash, totalReplay))
return true;
if (i < totalRound - 1)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return false;
}
bool
waitForDone()
{
int totalRound = 100;
for (int i = 0; i < totalRound; ++i)
{
bool allDone = true;
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
for (auto const& t : replayer.tasks_)
{
if (!t->finished())
{
allDone = false;
break;
}
}
}
if (allDone)
return true;
if (i < totalRound - 1)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
return false;
}
std::vector<std::shared_ptr<LedgerReplayTask>>
getTasks()
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
return replayer.tasks_;
}
std::shared_ptr<LedgerReplayTask>
findTask(uint256 const& hash, int totalReplay)
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
auto i = std::find_if(replayer.tasks_.begin(), replayer.tasks_.end(), [&](auto const& t) {
return t->parameter_.finishHash_ == hash && t->parameter_.totalLedgers_ == totalReplay;
});
if (i == replayer.tasks_.end())
return {};
return *i;
}
std::size_t
countDeltas()
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
return replayer.deltas_.size();
}
std::size_t
countSkipLists()
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
return replayer.skipLists_.size();
}
bool
countsAsExpected(std::size_t tasks, std::size_t skipLists, std::size_t deltas)
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
return replayer.tasks_.size() == tasks && replayer.skipLists_.size() == skipLists &&
replayer.deltas_.size() == deltas;
}
std::shared_ptr<SkipListAcquire>
findSkipListAcquire(uint256 const& hash)
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
auto i = replayer.skipLists_.find(hash);
if (i == replayer.skipLists_.end())
return {};
return i->second.lock();
}
std::shared_ptr<LedgerDeltaAcquire>
findLedgerDeltaAcquire(uint256 const& hash)
{
std::unique_lock<std::mutex> lock(replayer.mtx_);
auto i = replayer.deltas_.find(hash);
if (i == replayer.deltas_.end())
return {};
return i->second.lock();
}
template <typename T>
TaskStatus
taskStatus(std::shared_ptr<T> const& t)
{
if (t->failed_)
return TaskStatus::Failed;
if (t->complete_)
return TaskStatus::Completed;
return TaskStatus::NotDone;
}
bool
asExpected(
std::shared_ptr<LedgerReplayTask> const& task,
TaskStatus taskExpect,
TaskStatus skiplistExpect,
std::vector<TaskStatus> const& deltaExpects)
{
if (taskStatus(task) == taskExpect)
{
if (taskStatus(task->skipListAcquirer_) == skiplistExpect)
{
if (task->deltas_.size() == deltaExpects.size())
{
for (int i = 0; i < deltaExpects.size(); ++i)
{
if (taskStatus(task->deltas_[i]) != deltaExpects[i])
return false;
}
return true;
}
}
}
return false;
}
bool
asExpected(
uint256 const& hash,
int totalReplay,
TaskStatus taskExpect,
TaskStatus skiplistExpect,
std::vector<TaskStatus> const& deltaExpects)
{
auto t = findTask(hash, totalReplay);
if (!t)
{
return taskExpect == TaskStatus::NotExist;
}
return asExpected(t, taskExpect, skiplistExpect, deltaExpects);
}
bool
checkStatus(
uint256 const& hash,
int totalReplay,
TaskStatus taskExpect,
TaskStatus skiplistExpect,
std::vector<TaskStatus> const& deltaExpects)
{
auto t = findTask(hash, totalReplay);
if (!t)
{
return taskExpect == TaskStatus::NotExist;
}
return asExpected(t, taskExpect, skiplistExpect, deltaExpects);
}
bool
waitAndCheckStatus(
uint256 const& hash,
int totalReplay,
TaskStatus taskExpect,
TaskStatus skiplistExpect,
std::vector<TaskStatus> const& deltaExpects)
{
if (!waitForDone())
return false;
return checkStatus(hash, totalReplay, taskExpect, skiplistExpect, deltaExpects);
}
jtx::Env env;
Application& app;
LedgerMaster& ledgerMaster;
MagicInboundLedgers inboundLedgers;
LedgerReplayMsgHandler serverMsgHandler;
LedgerReplayMsgHandler clientMsgHandler;
LedgerReplayer replayer;
};
using namespace beast::severities;
void
logAll(
LedgerServer& server,
LedgerReplayClient& client,
beast::severities::Severity level = Severity::kTrace)
{
server.app.getLogs().threshold(level);
client.app.getLogs().threshold(level);
}
// logAll(net.server, net.client);
/*
* Create a LedgerServer and a LedgerReplayClient
*/
struct NetworkOfTwo
{
NetworkOfTwo(
beast::unit_test::suite& suite,
LedgerServer::Parameter const& param,
PeerSetBehavior behavior = PeerSetBehavior::Good,
InboundLedgersBehavior inboundBhvr = InboundLedgersBehavior::Good,
PeerFeature peerFeature = PeerFeature::LedgerReplayEnabled)
: server(suite, param), client(suite, server, behavior, inboundBhvr, peerFeature)
{
// logAll(server, client);
}
LedgerServer server;
LedgerReplayClient client;
};
/**
* Test cases:
* LedgerReplayer_test:
* -- process TMProofPathRequest and TMProofPathResponse
* -- process TMReplayDeltaRequest and TMReplayDeltaResponse
* -- update and merge LedgerReplayTask::TaskParameter
* -- process [ledger_replay] section in config
* -- peer handshake
* -- replay a range of ledgers that the local node already has
* -- replay a range of ledgers and fallback to InboundLedgers because
* peers do not support ProtocolFeature::LedgerReplay
* -- replay a range of ledgers and the network drops or repeats messages
* -- call stop() and the tasks and subtasks are removed
* -- process a bad skip list
* -- process a bad ledger delta
* -- replay ledger ranges with different overlaps
*
* LedgerReplayerTimeout_test:
* -- timeouts of SkipListAcquire
* -- timeouts of LedgerDeltaAcquire
*
* LedgerReplayerLong_test: (MANUAL)
* -- call replayer.replay() 4 times to replay 1000 ledgers
*/
struct LedgerReplayer_test : public beast::unit_test::suite
{
void
testProofPath()
{
testcase("ProofPath");
LedgerServer server(*this, {1});
auto const l = server.ledgerMaster.getClosedLedger();
{
// request, missing key
auto request = std::make_shared<protocol::TMProofPathRequest>();
request->set_ledgerhash(l->header().hash.data(), l->header().hash.size());
request->set_type(protocol::TMLedgerMapType::lmACCOUNT_STATE);
auto reply = std::make_shared<protocol::TMProofPathResponse>(
server.msgHandler.processProofPathRequest(request));
BEAST_EXPECT(reply->has_error());
BEAST_EXPECT(!server.msgHandler.processProofPathResponse(reply));
}
{
// request, wrong hash
auto request = std::make_shared<protocol::TMProofPathRequest>();
request->set_type(protocol::TMLedgerMapType::lmACCOUNT_STATE);
request->set_key(keylet::skip().key.data(), keylet::skip().key.size());
uint256 hash(1234567);
request->set_ledgerhash(hash.data(), hash.size());
auto reply = std::make_shared<protocol::TMProofPathResponse>(
server.msgHandler.processProofPathRequest(request));
BEAST_EXPECT(reply->has_error());
}
{
// good request
auto request = std::make_shared<protocol::TMProofPathRequest>();
request->set_ledgerhash(l->header().hash.data(), l->header().hash.size());
request->set_type(protocol::TMLedgerMapType::lmACCOUNT_STATE);
request->set_key(keylet::skip().key.data(), keylet::skip().key.size());
// generate response
auto reply = std::make_shared<protocol::TMProofPathResponse>(
server.msgHandler.processProofPathRequest(request));
BEAST_EXPECT(!reply->has_error());
BEAST_EXPECT(server.msgHandler.processProofPathResponse(reply));
{
// bad reply
// bad header
std::string r(reply->ledgerheader());
r.back()--;
reply->set_ledgerheader(r);
BEAST_EXPECT(!server.msgHandler.processProofPathResponse(reply));
r.back()++;
reply->set_ledgerheader(r);
BEAST_EXPECT(server.msgHandler.processProofPathResponse(reply));
// bad proof path
reply->mutable_path()->RemoveLast();
BEAST_EXPECT(!server.msgHandler.processProofPathResponse(reply));
}
}
}
void
testReplayDelta()
{
testcase("ReplayDelta");
LedgerServer server(*this, {1});
auto const l = server.ledgerMaster.getClosedLedger();
{
// request, missing hash
auto request = std::make_shared<protocol::TMReplayDeltaRequest>();
auto reply = std::make_shared<protocol::TMReplayDeltaResponse>(
server.msgHandler.processReplayDeltaRequest(request));
BEAST_EXPECT(reply->has_error());
BEAST_EXPECT(!server.msgHandler.processReplayDeltaResponse(reply));
// request, wrong hash
uint256 hash(1234567);
request->set_ledgerhash(hash.data(), hash.size());
reply = std::make_shared<protocol::TMReplayDeltaResponse>(
server.msgHandler.processReplayDeltaRequest(request));
BEAST_EXPECT(reply->has_error());
BEAST_EXPECT(!server.msgHandler.processReplayDeltaResponse(reply));
}
{
// good request
auto request = std::make_shared<protocol::TMReplayDeltaRequest>();
request->set_ledgerhash(l->header().hash.data(), l->header().hash.size());
auto reply = std::make_shared<protocol::TMReplayDeltaResponse>(
server.msgHandler.processReplayDeltaRequest(request));
BEAST_EXPECT(!reply->has_error());
BEAST_EXPECT(server.msgHandler.processReplayDeltaResponse(reply));
{
// bad reply
// bad header
std::string r(reply->ledgerheader());
r.back()--;
reply->set_ledgerheader(r);
BEAST_EXPECT(!server.msgHandler.processReplayDeltaResponse(reply));
r.back()++;
reply->set_ledgerheader(r);
BEAST_EXPECT(server.msgHandler.processReplayDeltaResponse(reply));
// bad txns
reply->mutable_transaction()->RemoveLast();
BEAST_EXPECT(!server.msgHandler.processReplayDeltaResponse(reply));
}
}
}
void
testTaskParameter()
{
testcase("TaskParameter");
auto makeSkipList = [](int count) -> std::vector<uint256> {
std::vector<uint256> sList;
sList.reserve(count);
for (int i = 0; i < count; ++i)
sList.emplace_back(i);
return sList;
};
LedgerReplayTask::TaskParameter tp10(InboundLedger::Reason::GENERIC, uint256(10), 10);
BEAST_EXPECT(!tp10.update(uint256(777), 5, makeSkipList(10)));
BEAST_EXPECT(!tp10.update(uint256(10), 5, makeSkipList(8)));
BEAST_EXPECT(tp10.update(uint256(10), 10, makeSkipList(10)));
// can merge to self
BEAST_EXPECT(tp10.canMergeInto(tp10));
// smaller task
LedgerReplayTask::TaskParameter tp9(InboundLedger::Reason::GENERIC, uint256(9), 9);
BEAST_EXPECT(tp9.canMergeInto(tp10));
BEAST_EXPECT(!tp10.canMergeInto(tp9));
tp9.totalLedgers_++;
BEAST_EXPECT(!tp9.canMergeInto(tp10));
tp9.totalLedgers_--;
BEAST_EXPECT(tp9.canMergeInto(tp10));
tp9.reason_ = InboundLedger::Reason::CONSENSUS;
BEAST_EXPECT(!tp9.canMergeInto(tp10));
tp9.reason_ = InboundLedger::Reason::GENERIC;
BEAST_EXPECT(tp9.canMergeInto(tp10));