/
Import.cpp
1338 lines (1142 loc) · 43.4 KB
/
Import.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
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
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 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/misc/Manifest.h>
#include <ripple/app/tx/impl/Import.h>
#include <ripple/app/tx/impl/SetSignerList.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/base64.h>
#include <ripple/json/json_reader.h>
#include <ripple/json/json_value.h>
#include <ripple/ledger/View.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Import.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/STTx.h>
#include <algorithm>
#include <charconv>
#include <iostream>
#include <vector>
namespace ripple {
TxConsequences
Import::makeTxConsequences(PreflightContext const& ctx)
{
auto calculate = [](PreflightContext const& ctx) -> XRPAmount {
auto const [inner, meta] = getInnerTxn(ctx.tx, ctx.j);
if (!inner || !inner->isFieldPresent(sfFee))
return beast::zero;
if (!meta || !meta->isFieldPresent(sfTransactionResult))
return beast::zero;
auto const result = meta->getFieldU8(sfTransactionResult);
if (result != tesSUCCESS &&
!(result >= tecCLAIM && result <= tecLAST_POSSIBLE_ENTRY))
return beast::zero;
STAmount const innerFee = inner->getFieldAmount(sfFee);
if (!isXRP(innerFee))
return beast::zero;
return innerFee.xrp();
};
return TxConsequences{ctx.tx, calculate(ctx)};
}
std::pair<
std::unique_ptr<STTx const>, // txn
std::unique_ptr<STObject const>> // meta
Import::getInnerTxn(
STTx const& outer,
beast::Journal const& j,
Json::Value const* xpop)
{
// parse blob as json
std::optional<Json::Value> xpop_storage;
if (!xpop && outer.isFieldPresent(sfBlob))
{
xpop_storage = syntaxCheckXPOP(outer.getFieldVL(sfBlob), j);
xpop = &(*xpop_storage);
}
if (!xpop)
return {};
// extract the transaction for which this xpop is a proof
auto rawTx = strUnHex((*xpop)[jss::transaction][jss::blob].asString());
auto meta = strUnHex((*xpop)[jss::transaction][jss::meta].asString());
if (!rawTx)
{
JLOG(j.warn()) << "Import: failed to deserialize tx blob inside xpop "
"(invalid hex) "
<< outer.getTransactionID();
return {};
}
if (!meta)
{
JLOG(j.warn()) << "Import: failed to deserialize tx meta inside xpop "
"(invalid hex) "
<< outer.getTransactionID();
return {};
}
try
{
return {
std::make_unique<STTx const>(
SerialIter{rawTx->data(), rawTx->size()}),
std::make_unique<STObject const>(
SerialIter(meta->data(), meta->size()), sfMetadata)};
}
catch (std::exception& e)
{
JLOG(j.warn())
<< "Import: failed to deserialize tx blob/meta inside xpop ("
<< e.what() << ") outer txid: " << outer.getTransactionID();
return {};
}
}
NotTEC
Import::preflight(PreflightContext const& ctx)
{
if (!ctx.rules.enabled(featureImport))
return temDISABLED;
if (!ctx.rules.enabled(featureHooksUpdate1) &&
ctx.tx.isFieldPresent(sfIssuer))
return temDISABLED;
if (ctx.tx.isFieldPresent(sfIssuer) &&
ctx.tx.getAccountID(sfIssuer) == ctx.tx.getAccountID(sfAccount))
{
JLOG(ctx.j.warn()) << "Import: Issuer cannot be the source account.";
return temMALFORMED;
}
if (auto const ret = preflight1(ctx); !isTesSuccess(ret))
return ret;
auto& tx = ctx.tx;
if (!tx.isFieldPresent(sfBlob))
{
JLOG(ctx.j.warn())
<< "Import: sfBlob was missing (should be impossible) "
<< tx.getTransactionID();
return temMALFORMED;
}
if (tx.getFieldVL(sfBlob).size() > (512 * 1024))
{
JLOG(ctx.j.warn()) << "Import: blob was more than 512kib "
<< tx.getTransactionID();
return temMALFORMED;
}
// parse blob as json
auto const xpop = syntaxCheckXPOP(tx.getFieldVL(sfBlob), ctx.j);
if (!xpop)
return temMALFORMED;
// we will check if we recognise the vl key in preclaim because it may be
// from on-ledger object
std::optional<PublicKey> masterVLKey;
{
std::string strPk =
(*xpop)[jss::validation][jss::unl][jss::public_key].asString();
auto pkHex = strUnHex(strPk);
if (!pkHex)
{
JLOG(ctx.j.warn())
<< "Import: validation.unl.public_key was not valid hex.";
return temMALFORMED;
}
auto const pkType = publicKeyType(makeSlice(*pkHex));
if (!pkType)
{
JLOG(ctx.j.warn()) << "Import: validation.unl.public_key was not a "
"recognised public key type.";
return temMALFORMED;
}
masterVLKey = PublicKey(makeSlice(*pkHex));
}
auto const [stpTrans, meta] = getInnerTxn(tx, ctx.j, &(*xpop));
if (!stpTrans || !meta)
return temMALFORMED;
if (stpTrans->isFieldPresent(sfTicketSequence))
{
JLOG(ctx.j.warn()) << "Import: cannot use TicketSequence XPOP.";
return temMALFORMED;
}
// check if txn is emitted or a psuedo
if (isPseudoTx(*stpTrans) || stpTrans->isFieldPresent(sfEmitDetails))
{
JLOG(ctx.j.warn()) << "Import: attempted to import xpop containing an "
"emitted or pseudo txn. "
<< tx.getTransactionID();
return temMALFORMED;
}
// ensure that the txn was tesSUCCESS / tec
if (!meta->isFieldPresent(sfTransactionResult))
{
JLOG(ctx.j.warn()) << "Import: inner txn lacked transaction result... "
<< tx.getTransactionID();
return temMALFORMED;
}
else
{
uint8_t innerResult = meta->getFieldU8(sfTransactionResult);
if (innerResult == tesSUCCESS)
{
// pass
}
else if (
innerResult >= tecCLAIM && innerResult <= tecLAST_POSSIBLE_ENTRY)
{
// pass : proof of burn on account set can be done with a tec code
}
else
{
JLOG(ctx.j.warn())
<< "Import: inner txn did not have a tesSUCCESS or tec result "
<< tx.getTransactionID();
return temMALFORMED;
}
}
// check if the account matches the account in the xpop, if not bail early
if (stpTrans->getAccountID(sfAccount) != tx.getAccountID(sfAccount))
{
JLOG(ctx.j.warn()) << "Import: import and txn inside xpop must be "
"signed by the same account "
<< tx.getTransactionID();
return temMALFORMED;
}
// ensure inner txn is for networkid = 0 (network id must therefore be
// missing)
if (stpTrans->isFieldPresent(sfNetworkID))
{
JLOG(ctx.j.warn()) << "Import: attempted to import xpop containing a "
"txn with a sfNetworkID field. "
<< tx.getTransactionID();
return temMALFORMED;
}
// ensure inner txn is destined for the network we're on, this is according
// to OperationLimit field
if (!stpTrans->isFieldPresent(sfOperationLimit))
{
JLOG(ctx.j.warn()) << "Import: OperationLimit missing from inner xpop "
"txn. outer txid: "
<< tx.getTransactionID();
return temMALFORMED;
}
if (stpTrans->getFieldU32(sfOperationLimit) != ctx.app.config().NETWORK_ID)
{
JLOG(ctx.j.warn()) << "Import: Wrong network ID for OperationLimit in "
"inner txn. outer txid: "
<< tx.getTransactionID();
return telWRONG_NETWORK;
}
// check if the inner transaction is signed using the same keying as the
// outer txn
{
auto outer = tx.getSigningPubKey();
auto inner = stpTrans->getSigningPubKey();
if (outer.empty() && inner.empty())
{
// check signer list
bool const outerHasSigners = tx.isFieldPresent(sfSigners);
bool const innerHasSigners = stpTrans->isFieldPresent(sfSigners);
if (outerHasSigners && innerHasSigners)
{
auto const& outerSigners = tx.getFieldArray(sfSigners);
auto const& innerSigners = stpTrans->getFieldArray(sfSigners);
bool ok = outerSigners.size() == innerSigners.size() &&
innerSigners.size() > 1;
for (uint64_t i = 0; ok && i < outerSigners.size(); ++i)
{
if (outerSigners[i].getAccountID(sfAccount) !=
innerSigners[i].getAccountID(sfAccount) ||
outerSigners[i].getFieldVL(sfSigningPubKey) !=
innerSigners[i].getFieldVL(sfSigningPubKey))
ok = false;
}
if (!ok)
{
JLOG(ctx.j.warn()) << "Import: outer and inner txns were "
"(multi) signed with different keys. "
<< tx.getTransactionID();
return temMALFORMED;
}
}
else
{
JLOG(ctx.j.warn())
<< "Import: outer or inner txn was missing signers. "
<< tx.getTransactionID();
return temMALFORMED;
}
}
else if (outer != inner)
{
JLOG(ctx.j.warn()) << "Import: outer and inner txns were signed "
"with different keys. "
<< tx.getTransactionID();
return temMALFORMED;
}
}
// check inner txns signature
// we do this with a custom ruleset which should be kept up to date with
// network 0's signing rules
const std::unordered_set<uint256, beast::uhash<>> rulesFeatures{
featureExpandedSignerList};
if (!stpTrans->checkSign(
STTx::RequireFullyCanonicalSig::yes, Rules(rulesFeatures)))
{
JLOG(ctx.j.warn()) << "Import: inner txn signature verify failed "
<< tx.getTransactionID();
return temMALFORMED;
}
// execution to here means that:
// 1. the proof is for the same account that submitted the proof
// 2. the inner txn has been checked for validity
// 3. if the xpop itself is proven then the txn can go to preclaim
//
// XPOP verify
//
// check it was used to sign over the manifest
auto const m = deserializeManifest(base64_decode(
(*xpop)[jss::validation][jss::unl][jss::manifest].asString()));
if (!m)
{
JLOG(ctx.j.warn()) << "Import: failed to deserialize manifest on txid "
<< tx.getTransactionID();
return temMALFORMED;
}
// we will check the master key matches a known one in preclaim, because the
// import vl key might be from the on-ledger object
if (m->masterKey != masterVLKey)
{
JLOG(ctx.j.warn()) << "Import: manifest master key did not match top "
"level master key in unl section of xpop "
<< tx.getTransactionID();
return temMALFORMED;
}
if (!m->verify())
{
JLOG(ctx.j.warn()) << "Import: manifest signature invalid "
<< tx.getTransactionID();
return temMALFORMED;
}
// manifest signing (ephemeral) key
auto const signingKey = m->signingKey;
// decode blob
auto const data =
base64_decode((*xpop)[jss::validation][jss::unl][jss::blob].asString());
Json::Reader r;
Json::Value list;
if (!r.parse(data, list))
{
JLOG(ctx.j.warn())
<< "Import: unl blob was not valid json (after base64 decoding) "
<< tx.getTransactionID();
return temMALFORMED;
}
if (!list.isMember(jss::sequence) || !list[jss::sequence].isInt())
{
JLOG(ctx.j.warn()) << "Import: unl blob json (after base64 decoding) "
"lacked required field (sequence) and/or types "
<< tx.getTransactionID();
return temMALFORMED;
}
if (!list.isMember(jss::expiration) || !list[jss::expiration].isInt())
{
JLOG(ctx.j.warn()) << "Import: unl blob json (after base64 decoding) "
"lacked required field (expiration) and/or types "
<< tx.getTransactionID();
return temMALFORMED;
}
if (list.isMember(jss::effective) && !list[jss::effective].isInt())
{
JLOG(ctx.j.warn()) << "Import: unl blob json (after base64 decoding) "
"lacked required field (effective) and/or types "
<< tx.getTransactionID();
return temMALFORMED;
}
if (!list.isMember(jss::validators) || !list[jss::validators].isArray())
{
JLOG(ctx.j.warn()) << "Import: unl blob json (after base64 decoding) "
"lacked required field (validators) and/or types "
<< tx.getTransactionID();
return temMALFORMED;
}
auto const validFrom = TimeKeeper::time_point{TimeKeeper::duration{
list.isMember(jss::effective) ? list[jss::effective].asUInt() : 0}};
auto const validUntil = TimeKeeper::time_point{
TimeKeeper::duration{list[jss::expiration].asUInt()}};
auto const now = ctx.app.timeKeeper().now();
if (validUntil <= validFrom)
{
JLOG(ctx.j.warn()) << "Import: unl blob validUntil <= validFrom "
<< tx.getTransactionID();
return temMALFORMED;
}
if (validUntil <= now)
{
JLOG(ctx.j.warn()) << "Import: unl blob expired "
<< tx.getTransactionID();
return temMALFORMED;
}
if (validFrom > now)
{
JLOG(ctx.j.warn()) << "Import: unl blob not yet valid "
<< tx.getTransactionID();
return temMALFORMED;
}
auto const sig =
strUnHex((*xpop)[jss::validation][jss::unl][jss::signature].asString());
if (!sig || !ripple::verify(signingKey, makeSlice(data), makeSlice(*sig)))
{
JLOG(ctx.j.warn()) << "Import: unl blob not signed correctly "
<< tx.getTransactionID();
return temMALFORMED;
}
auto const tx_hash =
stpTrans->getTransactionID(); // sha512Half(HashPrefix::transactionID,
// *rawTx);
JLOG(ctx.j.trace()) << "tx_hash (computed): " << tx_hash;
auto rawTx = strUnHex((*xpop)[jss::transaction][jss::blob].asString());
auto const tx_meta =
strUnHex((*xpop)[jss::transaction][jss::meta].asString());
Serializer s(rawTx->size() + tx_meta->size() + 40);
s.addVL(*rawTx);
s.addVL(*tx_meta);
s.addBitString(tx_hash);
uint256 const computed_tx_hash_and_meta =
sha512Half(HashPrefix::txNode, s.slice());
// check if the proof is inside the proof tree/list
if (!([](Json::Value const& proof, std::string hash) -> bool {
auto const proofContains = [](Json::Value const* proof,
std::string hash,
int depth = 0,
auto proofContains =
nullptr) -> bool {
if (depth > 32)
return false;
if (!proof->isObject() && !proof->isArray())
return false;
if (proof->isMember(jss::children))
proof = &((*proof)[jss::children]);
for (int x = 0; x < 16; ++x)
{
Json::Value const* entry = proof->isObject()
? &((*proof)[std::string(1, "0123456789ABCDEF"[x])])
: &((*proof)[x]);
if (entry->isNull())
continue;
if ((entry->isString() && entry->asString() == hash) ||
(entry->isObject() && entry->isMember(jss::hash) &&
(*entry)[jss::hash] == hash) ||
proofContains(entry, hash, depth + 1, proofContains))
return true;
}
return false;
};
return proofContains(&proof, hash, 0, proofContains);
})((*xpop)[jss::transaction][jss::proof],
strHex(computed_tx_hash_and_meta)))
{
JLOG(ctx.j.warn())
<< "Import: xpop proof did not contain the specified txn hash "
<< strHex(computed_tx_hash_and_meta) << " submitted in Import TXN "
<< tx.getTransactionID();
return temMALFORMED;
}
// compute the merkel root over the proof
uint256 const computedTxRoot = ([](Json::Value const& proof) -> uint256 {
auto hashProof = [](Json::Value const& proof,
int depth = 0,
auto const& hashProof = nullptr) -> uint256 {
const uint256 nullhash;
if (depth > 32)
return nullhash;
if (!proof.isObject() && !proof.isArray())
return nullhash;
sha512_half_hasher h;
using beast::hash_append;
hash_append(h, ripple::HashPrefix::innerNode);
if (proof.isArray())
{
for (const auto& entry : proof)
{
if (entry.isString())
{
uint256 hash;
if (hash.parseHex(entry.asString()))
hash_append(h, hash);
}
else
hash_append(h, hashProof(entry, depth + 1, hashProof));
}
}
else if (proof.isObject())
{
for (int x = 0; x < 16; ++x)
{
// Duplicate / Sanity
std::string const nibble(1, "0123456789ABCDEF"[x]);
if (!proof[jss::children].isMember(nibble))
hash_append(h, nullhash);
else if (
proof[jss::children][nibble][jss::children].size() ==
0u)
{
uint256 hash;
if (hash.parseHex(
proof[jss::children][nibble][jss::hash]
.asString()))
hash_append(h, hash);
}
else
hash_append(
h,
hashProof(
proof[jss::children][nibble],
depth + 1,
hashProof));
}
}
return static_cast<uint256>(h);
};
return hashProof(proof, 0, hashProof);
})((*xpop)[jss::transaction][jss::proof]);
auto const& lgr = (*xpop)[jss::ledger];
if (strHex(computedTxRoot) != lgr[jss::txroot])
{
JLOG(ctx.j.warn()) << "Import: computed txroot does not match xpop "
"txroot, invalid xpop. "
<< tx.getTransactionID();
return temMALFORMED;
}
auto coins = parse_uint64(lgr[jss::coins].asString());
uint256 phash, acroot;
// Duplicate / Sanity - syntaxCheckXPOP
if (!coins || !phash.parseHex(lgr[jss::phash].asString()) ||
!acroot.parseHex(lgr[jss::acroot].asString()))
{
JLOG(ctx.j.warn()) << "Import: error parsing coins | phash | acroot in "
"the ledger section of XPOP. "
<< tx.getTransactionID();
return temMALFORMED;
}
// compute ledger
uint256 computedLedgerHash = sha512Half(
HashPrefix::ledgerMaster,
std::uint32_t(lgr[jss::index].asUInt()),
*coins,
phash,
computedTxRoot,
acroot,
std::uint32_t(lgr[jss::pclose].asUInt()),
std::uint32_t(lgr[jss::close].asUInt()),
std::uint8_t(lgr[jss::cres].asUInt()),
std::uint8_t(lgr[jss::flags].asUInt()));
//
// validation section
//
std::map<std::string /* nodepub */, std::string /* public key */>
validators;
std::map<std::string /* master pubkey */, std::string /* nodepub */>
validatorsMaster;
uint64_t totalValidatorCount{0};
// parse the validator list
for (auto const& val : list[jss::validators])
{
totalValidatorCount++;
if (!val.isObject() || !val.isMember(jss::validation_public_key) ||
!val[jss::validation_public_key].isString() ||
!val.isMember(jss::manifest) || !val[jss::manifest].isString())
{
JLOG(ctx.j.warn()) << "Import: unl blob contained invalid "
"validator entry, skipping "
<< tx.getTransactionID();
continue;
}
std::optional<Blob> const ret =
strUnHex(val[jss::validation_public_key].asString());
if (!ret || !publicKeyType(makeSlice(*ret)))
{
JLOG(ctx.j.warn()) << "Import: unl blob contained an invalid "
"validator key, skipping "
<< val[jss::validation_public_key].asString()
<< " " << tx.getTransactionID();
continue;
}
auto const m =
deserializeManifest(base64_decode(val[jss::manifest].asString()));
if (!m)
{
JLOG(ctx.j.warn())
<< "Import: unl blob contained an invalid manifest, skipping "
<< tx.getTransactionID();
continue;
}
if (strHex(m->masterKey) != val[jss::validation_public_key])
{
JLOG(ctx.j.warn()) << "Import: unl blob list entry manifest master "
"key did not match master key, skipping "
<< tx.getTransactionID();
continue;
}
if (!m->verify())
{
JLOG(ctx.j.warn()) << "Import: unl blob list entry manifest "
"signature invalid, skipping "
<< tx.getTransactionID();
continue;
}
std::string const nodepub =
toBase58(TokenType::NodePublic, m->signingKey);
std::string const nodemaster =
toBase58(TokenType::NodePublic, m->masterKey);
validators[nodepub] = strHex(m->signingKey);
validatorsMaster[nodemaster] = nodepub;
}
JLOG(ctx.j.trace()) << "totalValidatorCount: " << totalValidatorCount;
uint64_t quorum = totalValidatorCount * 0.8;
if (quorum == 0)
{
JLOG(ctx.j.warn()) << "Import: resetting quorum to 1. "
<< tx.getTransactionID();
quorum = 1;
}
// count how many validations this ledger hash has
uint64_t validationCount{0};
{
auto const& data = (*xpop)[jss::validation][jss::data];
std::set<std::string> used_key;
for (const auto& key : data.getMemberNames())
{
auto nodepub = key;
auto const datakey = nodepub;
// if the specified node address (nodepub) is in the master address
// => regular address list then make a note and replace it with the
// regular address
auto regular = validatorsMaster.find(nodepub);
if (regular != validatorsMaster.end() &&
validators.find(regular->second) != validators.end())
{
used_key.emplace(nodepub);
nodepub = regular->second;
}
auto const signingKey = validators.find(nodepub);
if (signingKey == validators.end())
{
JLOG(ctx.j.trace()) << "Import: validator nodepub " << nodepub
<< " did not appear in validator list but "
"did appear in data section "
<< tx.getTransactionID();
continue;
}
if (used_key.find(nodepub) != used_key.end())
{
JLOG(ctx.j.trace())
<< "Import: validator nodepub " << nodepub
<< " key appears more than once in data section "
<< tx.getTransactionID();
continue;
}
used_key.emplace(nodepub);
// process the validation message
try
{
std::unique_ptr<STValidation> val;
auto const valBlob = strUnHex(data[datakey].asString());
if (!valBlob)
{
JLOG(ctx.j.warn())
<< "Import: validation inside xpop was not valid hex "
<< "nodepub: " << nodepub
<< " txid: " << tx.getTransactionID();
continue;
}
SerialIter sit(makeSlice(*valBlob));
val = std::make_unique<STValidation>(
std::ref(sit),
[](PublicKey const& pk) { return calcNodeID(pk); },
false);
if (val->getLedgerHash() != computedLedgerHash)
{
JLOG(ctx.j.warn()) << "Import: validation message was not "
"for computed ledger hash "
<< computedLedgerHash << " it was for "
<< val->getLedgerHash();
continue;
}
if (!(strHex(val->getSignerPublic()) == signingKey->second))
{
JLOG(ctx.j.warn())
<< "Import: validation inside xpop was not signed with "
"a signing key we recognise "
<< "despite being listed against a nodepub we "
"recognise. "
<< "nodepub: " << nodepub
<< ". txid: " << tx.getTransactionID();
continue;
}
// signature check is expensive hence done after checking
// everything else
if (!val->isValid())
{
JLOG(ctx.j.warn()) << "Import: validation inside xpop was "
"not correctly signed "
<< "nodepub: " << nodepub
<< " txid: " << tx.getTransactionID();
continue;
}
validationCount++;
}
catch (...)
{
JLOG(ctx.j.warn()) << "Import: validation inside xpop was not "
"able to be parsed "
<< "nodepub: " << nodepub
<< " txid: " << tx.getTransactionID();
continue;
}
}
}
JLOG(ctx.j.trace()) << "quorum: " << quorum
<< " validation count: " << validationCount;
// check if the validation count is adequate
auto hasInsufficientQuorum =
[&ctx](uint64_t quorum, uint64_t validationCount) {
if (ctx.rules.enabled(fixXahauV1))
{
return quorum > validationCount;
}
else
{
return quorum >= validationCount;
}
};
if (hasInsufficientQuorum(quorum, validationCount))
{
JLOG(ctx.j.warn()) << "Import: xpop did not contain an 80% quorum for "
"the txn it purports to prove. "
<< tx.getTransactionID();
return temMALFORMED;
}
// Duplicate / Sanity
if (!stpTrans->isFieldPresent(sfSequence) ||
!stpTrans->isFieldPresent(sfFee) ||
!isXRP(stpTrans->getFieldAmount(sfFee)))
{
JLOG(ctx.j.warn()) << "Import: xpop inner txn did not contain a "
"sequence number or fee. "
<< tx.getTransactionID();
return temMALFORMED;
}
if (stpTrans->getFieldAmount(sfFee) < beast::zero)
{
JLOG(ctx.j.warn()) << "Import: xpop contained negative fee. "
<< tx.getTransactionID();
return temBAD_FEE;
}
return preflight2(ctx);
}
TER
Import::preclaim(PreclaimContext const& ctx)
{
if (!ctx.view.rules().enabled(featureImport))
return temDISABLED;
if (!ctx.tx.isFieldPresent(sfBlob))
return tefINTERNAL;
// parse blob as json
auto const xpop = syntaxCheckXPOP(ctx.tx.getFieldVL(sfBlob), ctx.j);
if (!xpop)
{
JLOG(ctx.j.warn())
<< "Import: during preclaim could not parse xpop, bailing.";
return tefINTERNAL;
}
auto const [stpTrans, meta] = getInnerTxn(ctx.tx, ctx.j, &(*xpop));
if (!stpTrans || !meta || !stpTrans->isFieldPresent(sfSequence))
{
JLOG(ctx.j.warn()) << "Import: during preclaim could not find "
"importSequence, bailing.";
return tefINTERNAL;
}
auto const& sle = ctx.view.read(keylet::account(ctx.tx[sfAccount]));
if (sle && sle->isFieldPresent(sfImportSequence))
{
uint32_t sleImportSequence = sle->getFieldU32(sfImportSequence);
// replay attempt
if (sleImportSequence >= stpTrans->getFieldU32(sfSequence))
return tefPAST_IMPORT_SEQ;
}
// when importing for the first time the fee must be zero
if (!sle && ctx.tx.getFieldAmount(sfFee) != beast::zero)
return temBAD_FEE;
auto const vlInfo = getVLInfo(*xpop, ctx.j);
if (!vlInfo)
{
JLOG(ctx.j.warn())
<< "Import: during preclaim could not parse vlInfo, bailing.";
return tefINTERNAL;
}
auto const& sleVL = ctx.view.read(keylet::import_vlseq(vlInfo->second));
if (sleVL && sleVL->getFieldU32(sfImportSequence) > vlInfo->first)
{
JLOG(ctx.j.warn())
<< "Import: import vl sequence already used, bailing.";
return tefPAST_IMPORT_VL_SEQ;
}
// check master VL key
std::string strPk =
(*xpop)[jss::validation][jss::unl][jss::public_key].asString();
if (auto const& found = ctx.app.config().IMPORT_VL_KEYS.find(strPk);
found != ctx.app.config().IMPORT_VL_KEYS.end())
return tesSUCCESS;
auto pkHex = strUnHex(strPk);
if (!pkHex)
return tefINTERNAL;
auto const pkType = publicKeyType(makeSlice(*pkHex));
if (!pkType)
return tefINTERNAL;
PublicKey const pk(makeSlice(*pkHex));
// check on ledger
if (auto const unlRep = ctx.view.read(keylet::UNLReport()); unlRep)
{
auto const& vlKeys = unlRep->getFieldArray(sfImportVLKeys);
for (auto const& k : vlKeys)
if (PublicKey(k[sfPublicKey]) == pk)
return tesSUCCESS;
}
JLOG(ctx.j.warn()) << "Import: import vl key not recognized, bailing.";
return telIMPORT_VL_KEY_NOT_RECOGNISED;
}
void
Import::doSignerList(std::shared_ptr<SLE>& sle, STTx const& stpTrans)
{
AccountID id = stpTrans.getAccountID(sfAccount);
JLOG(ctx_.journal.trace()) << "Import: doSignerList acc: " << id;
if (!stpTrans.isFieldPresent(sfSignerQuorum))
{
JLOG(ctx_.journal.warn())
<< "Import: acc " << id
<< " tried to import signerlist without sfSignerQuorum, skipping";
return;
}
Sandbox sb(&view());
uint32_t quorum = stpTrans.getFieldU32(sfSignerQuorum);
if (quorum == 0)
{
// delete operation
TER result =
SetSignerList::removeFromLedger(ctx_.app, sb, id, ctx_.journal);
if (result == tesSUCCESS)
{
JLOG(ctx_.journal.warn())
<< "Import: successful destroy SignerListSet";
sb.apply(ctx_.rawView());
}
else
{
JLOG(ctx_.journal.warn())
<< "Import: SetSignerList destroy failed with code " << result
<< " acc: " << id;
}
return;
}
if (!stpTrans.isFieldPresent(sfSignerEntries) ||
stpTrans.getFieldArray(sfSignerEntries).empty())
{
JLOG(ctx_.journal.warn())
<< "Import: SetSignerList lacked populated array and quorum was "
"non-zero. Ignoring. acc: "
<< id;
return;
}
//