/
applyHook.cpp
6352 lines (5227 loc) · 179 KB
/
applyHook.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
#include <ripple/app/hook/applyHook.h>
#include <ripple/app/ledger/OpenLedger.h>
#include <ripple/app/ledger/TransactionMaster.h>
#include <ripple/app/misc/HashRouter.h>
#include <ripple/app/misc/NetworkOPs.h>
#include <ripple/app/misc/Transaction.h>
#include <ripple/app/misc/TxQ.h>
#include <ripple/app/tx/impl/Import.h>
#include <ripple/app/tx/impl/details/NFTokenUtils.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/Slice.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/protocol/st.h>
#include <ripple/protocol/tokens.h>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <any>
#include <cfenv>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include <wasmedge/wasmedge.h>
using namespace ripple;
namespace hook {
std::vector<std::pair<AccountID, bool>>
getTransactionalStakeHolders(STTx const& tx, ReadView const& rv)
{
if (!rv.rules().enabled(featureHooks))
return {};
if (!tx.isFieldPresent(sfAccount))
return {};
std::optional<AccountID> destAcc = tx.at(~sfDestination);
std::optional<AccountID> otxnAcc = tx.at(~sfAccount);
if (!otxnAcc)
return {};
uint16_t tt = tx.getFieldU16(sfTransactionType);
std::map<AccountID, std::pair<int, bool>> tshEntries;
int upto = 0;
auto const ADD_TSH = [&otxnAcc, &tshEntries, &upto](
const AccountID& acc_r, bool rb) {
if (acc_r != *otxnAcc)
{
if (tshEntries.find(acc_r) != tshEntries.end())
tshEntries[acc_r].second |= rb;
else
tshEntries.emplace(acc_r, std::make_pair(upto++, rb));
}
};
bool const tshSTRONG = true; // tshROLLBACK
bool const tshWEAK = false; // tshCOLLECT
auto const getNFTOffer =
[](std::optional<uint256> id,
ReadView const& rv) -> std::shared_ptr<const SLE> {
if (!id || *id == beast::zero)
return nullptr;
return rv.read(keylet::nftoffer(*id));
};
bool const fixV1 = rv.rules().enabled(fixXahauV1);
bool const fixV2 = rv.rules().enabled(fixXahauV2);
switch (tt)
{
case ttREMIT: {
if (destAcc)
ADD_TSH(*destAcc, tshSTRONG);
if (tx.isFieldPresent(sfInform))
{
auto const inform = tx.getAccountID(sfInform);
if (*otxnAcc != inform && *destAcc != inform)
ADD_TSH(inform, tshWEAK);
}
if (tx.isFieldPresent(sfURITokenIDs))
{
STVector256 tokenIds = tx.getFieldV256(sfURITokenIDs);
for (uint256 const klRaw : tokenIds)
{
Keylet const id{ltURI_TOKEN, klRaw};
if (!rv.exists(id))
continue;
auto const ut = rv.read(id);
if (!ut ||
ut->getFieldU16(sfLedgerEntryType) != ltURI_TOKEN)
continue;
auto const owner = ut->getAccountID(sfOwner);
auto const issuer = ut->getAccountID(sfIssuer);
if (issuer != owner && issuer != *destAcc)
{
ADD_TSH(
issuer,
(ut->getFlags() & lsfBurnable) ? tshSTRONG
: tshWEAK);
}
}
}
break;
}
case ttIMPORT: {
if (tx.isFieldPresent(sfIssuer))
ADD_TSH(tx.getAccountID(sfIssuer), fixV2 ? tshWEAK : tshSTRONG);
break;
}
case ttURITOKEN_BURN: {
Keylet const id{ltURI_TOKEN, tx.getFieldH256(sfURITokenID)};
if (!rv.exists(id))
return {};
auto const ut = rv.read(id);
if (!ut || ut->getFieldU16(sfLedgerEntryType) != ltURI_TOKEN)
return {};
auto const owner = ut->getAccountID(sfOwner);
auto const issuer = ut->getAccountID(sfIssuer);
// three possible burn scenarios:
// the burner is the owner and issuer of the token
// the burner is the owner and not the issuer of the token
// the burner is the issuer and not the owner of the token
if (issuer == owner)
break;
// pass, already a TSH
// new logic
if (fixV1)
{
// the owner burns their token, and the issuer is a weak TSH
if (*otxnAcc == owner && rv.exists(keylet::account(issuer)))
ADD_TSH(issuer, tshWEAK);
// the issuer burns the owner's token, and the owner is a weak
// TSH
else if (rv.exists(keylet::account(owner)))
ADD_TSH(owner, tshWEAK);
break;
}
// old logic
{
if (*otxnAcc == owner)
{
// the owner burns their token, and the issuer is a weak TSH
ADD_TSH(issuer, tshSTRONG);
}
else
{
// the issuer burns the owner's token, and the owner is a
// weak TSH
ADD_TSH(owner, tshSTRONG);
}
}
break;
}
case ttURITOKEN_BUY: {
Keylet const id{ltURI_TOKEN, tx.getFieldH256(sfURITokenID)};
if (!rv.exists(id))
return {};
auto const ut = rv.read(id);
if (!ut || ut->getFieldU16(sfLedgerEntryType) != ltURI_TOKEN)
return {};
auto const owner = ut->getAccountID(sfOwner);
if (owner != tx.getAccountID(sfAccount))
{
// current owner is a strong TSH
ADD_TSH(owner, tshSTRONG);
}
// issuer is also a strong TSH if the burnable flag is set
auto const issuer = ut->getAccountID(sfIssuer);
if (issuer != owner)
ADD_TSH(
issuer,
(ut->getFlags() & lsfBurnable) ? tshSTRONG : tshWEAK);
break;
}
case ttURITOKEN_MINT: {
// destination is a strong tsh
if (fixV2 && tx.isFieldPresent(sfDestination))
ADD_TSH(tx.getAccountID(sfDestination), tshSTRONG);
break;
}
case ttURITOKEN_CANCEL_SELL_OFFER: {
if (!fixV2)
break;
Keylet const id{ltURI_TOKEN, tx.getFieldH256(sfURITokenID)};
if (!rv.exists(id))
return {};
auto const ut = rv.read(id);
if (!ut || ut->getFieldU16(sfLedgerEntryType) != ltURI_TOKEN)
return {};
if (ut->isFieldPresent(sfDestination))
{
auto const dest = ut->getAccountID(sfDestination);
ADD_TSH(dest, tshWEAK);
}
break;
}
case ttURITOKEN_CREATE_SELL_OFFER: {
Keylet const id{ltURI_TOKEN, tx.getFieldH256(sfURITokenID)};
if (!rv.exists(id))
return {};
auto const ut = rv.read(id);
if (!ut || ut->getFieldU16(sfLedgerEntryType) != ltURI_TOKEN)
return {};
auto const owner = ut->getAccountID(sfOwner);
auto const issuer = ut->getAccountID(sfIssuer);
// issuer is a strong TSH if the burnable flag is set
if (issuer != owner)
ADD_TSH(
issuer,
(ut->getFlags() & lsfBurnable) ? tshSTRONG : tshWEAK);
// destination is a strong tsh
if (tx.isFieldPresent(sfDestination))
ADD_TSH(tx.getAccountID(sfDestination), tshSTRONG);
break;
}
// NFT
case ttNFTOKEN_MINT:
case ttCLAIM_REWARD: {
if (tx.isFieldPresent(sfIssuer))
ADD_TSH(tx.getAccountID(sfIssuer), tshSTRONG);
break;
};
case ttNFTOKEN_BURN:
case ttNFTOKEN_CREATE_OFFER: {
if (!tx.isFieldPresent(sfNFTokenID) ||
!tx.isFieldPresent(sfAccount))
return {};
uint256 nid = tx.getFieldH256(sfNFTokenID);
bool hasOwner = tx.isFieldPresent(sfOwner);
AccountID owner = tx.getAccountID(hasOwner ? sfOwner : sfAccount);
if (!nft::findToken(rv, owner, nid))
return {};
auto const issuer = nft::getIssuer(nid);
bool issuerCanRollback = nft::getFlags(nid) & tfStrongTSH;
ADD_TSH(issuer, issuerCanRollback);
if (hasOwner)
ADD_TSH(owner, tshWEAK);
break;
}
case ttNFTOKEN_ACCEPT_OFFER: {
auto const bo = getNFTOffer(tx[~sfNFTokenBuyOffer], rv);
auto const so = getNFTOffer(tx[~sfNFTokenSellOffer], rv);
if (!bo && !so)
return {};
// issuer only has rollback ability if NFT specifies it in flags
uint256 nid = (bo ? bo : so)->getFieldH256(sfNFTokenID);
auto const issuer = nft::getIssuer(nid);
bool issuerCanRollback = nft::getFlags(nid) & tfStrongTSH;
ADD_TSH(issuer, issuerCanRollback);
if (bo)
{
ADD_TSH(bo->getAccountID(sfOwner), tshSTRONG);
if (bo->isFieldPresent(sfDestination))
ADD_TSH(bo->getAccountID(sfDestination), tshSTRONG);
}
if (so)
{
ADD_TSH(so->getAccountID(sfOwner), tshSTRONG);
if (so->isFieldPresent(sfDestination))
ADD_TSH(so->getAccountID(sfDestination), tshSTRONG);
}
break;
}
case ttNFTOKEN_CANCEL_OFFER: {
if (!tx.isFieldPresent(sfNFTokenOffers))
return {};
auto const& offerVec = tx.getFieldV256(sfNFTokenOffers);
for (auto const& offerID : offerVec)
{
auto const offer = getNFTOffer(offerID, rv);
if (offer)
{
ADD_TSH(offer->getAccountID(sfOwner), tshWEAK);
if (offer->isFieldPresent(sfDestination))
ADD_TSH(offer->getAccountID(sfDestination), tshWEAK);
// issuer can't stop people canceling their offers, but can
// get weak executions
uint256 nid = offer->getFieldH256(sfNFTokenID);
auto const issuer = nft::getIssuer(nid);
ADD_TSH(issuer, tshWEAK);
}
}
break;
}
// self transactions
case ttACCOUNT_SET:
case ttOFFER_CANCEL:
case ttTICKET_CREATE:
case ttHOOK_SET:
case ttOFFER_CREATE: // this is handled seperately
{
break;
}
case ttREGULAR_KEY_SET: {
if (!tx.isFieldPresent(sfRegularKey))
return {};
ADD_TSH(tx.getAccountID(sfRegularKey), tshSTRONG);
break;
}
case ttDEPOSIT_PREAUTH: {
if (!tx.isFieldPresent(sfAuthorize))
return {};
ADD_TSH(tx.getAccountID(sfAuthorize), tshSTRONG);
break;
}
// simple two party transactions
case ttPAYMENT:
case ttESCROW_CREATE:
case ttCHECK_CREATE:
case ttACCOUNT_DELETE:
case ttPAYCHAN_CREATE:
case ttINVOKE: {
if (destAcc)
ADD_TSH(*destAcc, tshSTRONG);
break;
}
case ttTRUST_SET: {
if (!tx.isFieldPresent(sfLimitAmount))
return {};
auto const& lim = tx.getFieldAmount(sfLimitAmount);
AccountID const& issuer = lim.getIssuer();
ADD_TSH(issuer, tshWEAK);
break;
}
case ttESCROW_CANCEL:
case ttESCROW_FINISH: {
// new logic
if (fixV1)
{
if (!tx.isFieldPresent(sfOwner))
return {};
AccountID const owner = tx.getAccountID(sfOwner);
bool const hasSeq = tx.isFieldPresent(sfOfferSequence);
bool const hasID = tx.isFieldPresent(sfEscrowID);
if (!hasSeq && !hasID)
return {};
Keylet kl = hasSeq
? keylet::escrow(owner, tx.getFieldU32(sfOfferSequence))
: Keylet(ltESCROW, tx.getFieldH256(sfEscrowID));
auto escrow = rv.read(kl);
if (!escrow ||
escrow->getFieldU16(sfLedgerEntryType) != ltESCROW)
return {};
// this should always be the same as owner, but defensively...
AccountID const src = escrow->getAccountID(sfAccount);
AccountID const dst = escrow->getAccountID(sfDestination);
// the source account is a strong transacitonal stakeholder for
// fin and can
ADD_TSH(src, tshSTRONG);
// the dest acc is a strong tsh for fin and weak for can
if (src != dst)
ADD_TSH(dst, tt == ttESCROW_FINISH ? tshSTRONG : tshWEAK);
break;
}
// old logic
{
if (!tx.isFieldPresent(sfOwner) ||
!tx.isFieldPresent(sfOfferSequence))
return {};
auto escrow = rv.read(keylet::escrow(
tx.getAccountID(sfOwner), tx.getFieldU32(sfOfferSequence)));
if (!escrow)
return {};
ADD_TSH(escrow->getAccountID(sfAccount), tshSTRONG);
ADD_TSH(
escrow->getAccountID(sfDestination),
tt == ttESCROW_FINISH ? tshSTRONG : tshWEAK);
break;
}
}
case ttPAYCHAN_FUND:
case ttPAYCHAN_CLAIM: {
if (!tx.isFieldPresent(sfChannel))
return {};
auto chan = rv.read(Keylet{ltPAYCHAN, tx.getFieldH256(sfChannel)});
if (!chan)
return {};
ADD_TSH(chan->getAccountID(sfAccount), tshSTRONG);
ADD_TSH(chan->getAccountID(sfDestination), tshWEAK);
break;
}
case ttCHECK_CASH:
case ttCHECK_CANCEL: {
if (!tx.isFieldPresent(sfCheckID))
return {};
auto check = rv.read(Keylet{ltCHECK, tx.getFieldH256(sfCheckID)});
if (!check)
return {};
ADD_TSH(check->getAccountID(sfAccount), tshSTRONG);
ADD_TSH(check->getAccountID(sfDestination), tshWEAK);
break;
}
// the owners of accounts whose keys appear on a signer list are
// entitled to prevent their inclusion
case ttSIGNER_LIST_SET: {
STArray const& signerEntries = tx.getFieldArray(sfSignerEntries);
for (auto const& entryObj : signerEntries)
if (entryObj.isFieldPresent(sfAccount))
ADD_TSH(entryObj.getAccountID(sfAccount), tshSTRONG);
break;
}
case ttGENESIS_MINT: {
if (tx.isFieldPresent(sfGenesisMints))
{
auto const& mints = tx.getFieldArray(sfGenesisMints);
for (auto const& mint : mints)
{
if (mint.isFieldPresent(sfDestination))
{
ADD_TSH(mint.getAccountID(sfDestination), tshWEAK);
}
}
}
break;
}
default:
return {};
}
std::vector<std::pair<AccountID, bool>> ret{tshEntries.size()};
for (auto& [a, e] : tshEntries)
ret[e.first] = std::pair<AccountID, bool>{a, e.second};
return ret;
}
} // namespace hook
namespace hook_float {
// power of 10 LUT for fast integer math
static int64_t power_of_ten[19] = {
1LL,
10LL,
100LL,
1000LL,
10000LL,
100000LL,
1000000LL,
10000000LL,
100000000LL,
1000000000LL,
10000000000LL,
100000000000LL,
1000000000000LL,
10000000000000LL,
100000000000000LL,
1000000000000000LL, // 15
10000000000000000LL,
100000000000000000LL,
1000000000000000000LL,
};
using namespace hook_api;
static int64_t const minMantissa = 1000000000000000ull;
static int64_t const maxMantissa = 9999999999999999ull;
static int32_t const minExponent = -96;
static int32_t const maxExponent = 80;
inline int32_t
get_exponent(int64_t float1)
{
if (float1 < 0)
return INVALID_FLOAT;
if (float1 == 0)
return 0;
uint64_t float_in = (uint64_t)float1;
float_in >>= 54U;
float_in &= 0xFFU;
return ((int32_t)float_in) - 97;
}
inline int64_t
get_mantissa(int64_t float1)
{
if (float1 < 0)
return INVALID_FLOAT;
if (float1 == 0)
return 0;
float1 -= ((((uint64_t)float1) >> 54U) << 54U);
return float1;
}
inline bool
is_negative(int64_t float1)
{
return ((float1 >> 62U) & 1ULL) == 0;
}
inline int64_t
invert_sign(int64_t float1)
{
int64_t r = (int64_t)(((uint64_t)float1) ^ (1ULL << 62U));
return r;
}
inline int64_t
set_sign(int64_t float1, bool set_negative)
{
bool neg = is_negative(float1);
if ((neg && set_negative) || (!neg && !set_negative))
return float1;
return invert_sign(float1);
}
inline int64_t
set_mantissa(int64_t float1, uint64_t mantissa)
{
if (mantissa > maxMantissa)
return MANTISSA_OVERSIZED;
if (mantissa < minMantissa)
return MANTISSA_UNDERSIZED;
return float1 - get_mantissa(float1) + mantissa;
}
inline int64_t
set_exponent(int64_t float1, int32_t exponent)
{
if (exponent > maxExponent)
return EXPONENT_OVERSIZED;
if (exponent < minExponent)
return EXPONENT_UNDERSIZED;
uint64_t exp = (exponent + 97);
exp <<= 54U;
float1 &= ~(0xFFLL << 54);
float1 += (int64_t)exp;
return float1;
}
inline int64_t
make_float(ripple::IOUAmount& amt)
{
int64_t man_out = amt.mantissa();
int64_t float_out = 0;
bool neg = man_out < 0;
if (neg)
man_out *= -1;
float_out = set_sign(float_out, neg);
float_out = set_mantissa(float_out, (uint64_t)man_out);
float_out = set_exponent(float_out, amt.exponent());
return float_out;
}
inline int64_t
make_float(uint64_t mantissa, int32_t exponent, bool neg)
{
if (mantissa == 0)
return 0;
if (mantissa > maxMantissa)
return MANTISSA_OVERSIZED;
if (mantissa < minMantissa)
return MANTISSA_UNDERSIZED;
if (exponent > maxExponent)
return EXPONENT_OVERSIZED;
if (exponent < minExponent)
return EXPONENT_UNDERSIZED;
int64_t out = 0;
out = set_mantissa(out, mantissa);
out = set_exponent(out, exponent);
out = set_sign(out, neg);
return out;
}
/**
* This function normalizes the mantissa and exponent passed, if it can.
* It returns the XFL and mutates the supplied manitssa and exponent.
* If a negative mantissa is provided then the returned XFL has the negative
* flag set. If there is an overflow error return XFL_OVERFLOW. On underflow
* returns canonical 0
*/
template <typename T>
inline int64_t
normalize_xfl(T& man, int32_t& exp, bool neg = false)
{
if (man == 0)
return 0;
if (man == std::numeric_limits<int64_t>::min())
man++;
constexpr bool sman = std::is_same<T, int64_t>::value;
static_assert(sman || std::is_same<T, uint64_t>());
if constexpr (sman)
{
if (man < 0)
{
man *= -1LL;
neg = true;
}
}
// mantissa order
std::feclearexcept(FE_ALL_EXCEPT);
int32_t mo = log10(man);
// defensively ensure log10 produces a sane result; we'll borrow the
// overflow error code if it didn't
if (std::fetestexcept(FE_INVALID))
return XFL_OVERFLOW;
int32_t adjust = 15 - mo;
if (adjust > 0)
{
// defensive check
if (adjust > 18)
return 0;
man *= power_of_ten[adjust];
exp -= adjust;
}
else if (adjust < 0)
{
// defensive check
if (-adjust > 18)
return XFL_OVERFLOW;
man /= power_of_ten[-adjust];
exp -= adjust;
}
if (man == 0)
{
exp = 0;
return 0;
}
// even after adjustment the mantissa can be outside the range by one place
// improving the math above would probably alleviate the need for these
// branches
if (man < minMantissa)
{
if (man == minMantissa - 1LL)
man += 1LL;
else
{
man *= 10LL;
exp--;
}
}
if (man > maxMantissa)
{
if (man == maxMantissa + 1LL)
man -= 1LL;
else
{
man /= 10LL;
exp++;
}
}
if (exp < minExponent)
{
man = 0;
exp = 0;
return 0;
}
if (man == 0)
{
exp = 0;
return 0;
}
if (exp > maxExponent)
return XFL_OVERFLOW;
int64_t ret = make_float((uint64_t)man, exp, neg);
if constexpr (sman)
{
if (neg)
man *= -1LL;
}
return ret;
}
} // namespace hook_float
using namespace hook_float;
inline int32_t
no_free_slots(hook::HookContext& hookCtx)
{
return hook_api::max_slots - hookCtx.slot.size() <= 0;
}
inline std::optional<int32_t>
get_free_slot(hook::HookContext& hookCtx)
{
// allocate a slot
int32_t slot_into = 0;
if (hookCtx.slot_free.size() > 0)
{
slot_into = hookCtx.slot_free.front();
hookCtx.slot_free.pop();
return slot_into;
}
// no slots were available in the queue so increment slot counter until we
// find a free slot usually this will be the next available but the hook
// developer may have allocated any slot ahead of when the counter gets
// there
do
{
slot_into = ++hookCtx.slot_counter;
} while (hookCtx.slot.find(slot_into) != hookCtx.slot.end() &&
// this condition should always be met, if for some reason, somehow
// it is not then we will return the final slot every time.
hookCtx.slot_counter <= hook_api::max_slots);
if (hookCtx.slot_counter > hook_api::max_slots)
return {};
return slot_into;
}
// cu_ptr is a pointer into memory, bounds check is assumed to have already
// happened
inline std::optional<Currency>
parseCurrency(uint8_t* cu_ptr, uint32_t cu_len)
{
if (cu_len == 20)
{
// normal 20 byte currency
return Currency::fromVoid(cu_ptr);
}
else if (cu_len == 3)
{
// 3 byte ascii currency
// need to check what data is in these three bytes, to ensure ISO4217
// compliance
auto const validateChar = [](uint8_t c) -> bool {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') || c == '?' || c == '!' || c == '@' ||
c == '#' || c == '$' || c == '%' || c == '^' || c == '&' ||
c == '*' || c == '<' || c == '>' || c == '(' || c == ')' ||
c == '{' || c == '}' || c == '[' || c == ']' || c == '|';
};
if (!validateChar(*((uint8_t*)(cu_ptr + 0U))) ||
!validateChar(*((uint8_t*)(cu_ptr + 1U))) ||
!validateChar(*((uint8_t*)(cu_ptr + 2U))))
return {};
uint8_t cur_buf[20] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
*((uint8_t*)(cu_ptr + 0U)),
*((uint8_t*)(cu_ptr + 1U)),
*((uint8_t*)(cu_ptr + 2U)),
0,
0,
0,
0,
0};
return Currency::fromVoid(cur_buf);
}
else
return {};
}
uint32_t
hook::computeHookStateOwnerCount(uint32_t hookStateCount)
{
return hookStateCount;
}
inline int64_t
serialize_keylet(
ripple::Keylet& kl,
uint8_t* memory,
uint32_t write_ptr,
uint32_t write_len)
{
if (write_len < 34)
return hook_api::TOO_SMALL;
memory[write_ptr + 0] = (kl.type >> 8) & 0xFFU;
memory[write_ptr + 1] = (kl.type >> 0) & 0xFFU;
for (int i = 0; i < 32; ++i)
memory[write_ptr + 2 + i] = kl.key.data()[i];
return 34;
}
std::optional<ripple::Keylet>
unserialize_keylet(uint8_t* ptr, uint32_t len)
{
if (len != 34)
return {};
uint16_t ktype = ((uint16_t)ptr[0] << 8) + ((uint16_t)ptr[1]);
return ripple::Keylet{
static_cast<LedgerEntryType>(ktype),
ripple::uint256::fromVoid(ptr + 2)};
}
bool
hook::isEmittedTxn(ripple::STTx const& tx)
{
return tx.isFieldPresent(ripple::sfEmitDetails);
}
int64_t
hook::computeExecutionFee(uint64_t instructionCount)
{
int64_t fee = (int64_t)instructionCount;
if (fee < instructionCount)
return 0x7FFFFFFFFFFFFFFFLL;
return fee;
}
int64_t
hook::computeCreationFee(uint64_t byteCount)
{
int64_t fee = ((int64_t)byteCount) * 500ULL;
if (fee < byteCount)
return 0x7FFFFFFFFFFFFFFFLL;
return fee;
}
// many datatypes can be encoded into an int64_t
inline int64_t
data_as_int64(void const* ptr_raw, uint32_t len)
{
if (len > 8)
return hook_api::hook_return_code::TOO_BIG;
uint8_t const* ptr = reinterpret_cast<uint8_t const*>(ptr_raw);
uint64_t output = 0;
for (int i = 0, j = (len - 1) * 8; i < len; ++i, j -= 8)
output += (((uint64_t)ptr[i]) << j);
if ((1ULL << 63U) & output)
return hook_api::hook_return_code::TOO_BIG;
return (int64_t)output;
}
/* returns true iff every even char is ascii and every odd char is 00
* only a hueristic, may be inaccurate in edgecases */
inline bool
is_UTF16LE(const uint8_t* buffer, size_t len)
{
if (len % 2 != 0 || len == 0)
return false;
for (int i = 0; i < len; i += 2)
if (buffer[i + 0] == 0 || buffer[i + 1] != 0)
return false;
return true;
}
// return true if sleAccount has been modified as a result of the call
bool
hook::addHookNamespaceEntry(ripple::SLE& sleAccount, ripple::uint256 ns)
{
STVector256 vec = sleAccount.getFieldV256(sfHookNamespaces);
for (auto u : vec.value())
if (u == ns)
return false;
vec.push_back(ns);
sleAccount.setFieldV256(sfHookNamespaces, vec);
return true;
}
// return true if sleAccount has been modified as a result of the call
bool
hook::removeHookNamespaceEntry(ripple::SLE& sleAccount, ripple::uint256 ns)
{
if (sleAccount.isFieldPresent(sfHookNamespaces))
{
STVector256 const& vec = sleAccount.getFieldV256(sfHookNamespaces);
if (vec.size() == 0)
{
// clean up structure if it's present but empty
sleAccount.makeFieldAbsent(sfHookNamespaces);
return true;
}
else
{
// defensively ensure the uniqueness of the namespace array
std::set<uint256> spaces;
for (auto u : vec.value())
if (u != ns)
spaces.emplace(u);
// drop through if it wasn't present (see comment block 20 lines
// above)
if (spaces.size() != vec.size())
{
if (spaces.size() == 0)
sleAccount.makeFieldAbsent(sfHookNamespaces);
else
{
std::vector<uint256> nv;
nv.reserve(spaces.size());
for (auto u : spaces)
nv.push_back(u);
sleAccount.setFieldV256(