1#include <xrpl/tx/transactors/bridge/XChainBridge.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/core/ServiceRegistry.h>
9#include <xrpl/ledger/ApplyView.h>
10#include <xrpl/ledger/PaymentSandbox.h>
11#include <xrpl/ledger/RawView.h>
12#include <xrpl/ledger/ReadView.h>
13#include <xrpl/ledger/helpers/AccountRootHelpers.h>
14#include <xrpl/ledger/helpers/DirectoryHelpers.h>
15#include <xrpl/protocol/AccountID.h>
16#include <xrpl/protocol/Feature.h>
17#include <xrpl/protocol/Indexes.h>
18#include <xrpl/protocol/Issue.h>
19#include <xrpl/protocol/KeyType.h>
20#include <xrpl/protocol/Keylet.h>
21#include <xrpl/protocol/LedgerFormats.h>
22#include <xrpl/protocol/PublicKey.h>
23#include <xrpl/protocol/SField.h>
24#include <xrpl/protocol/STAmount.h>
25#include <xrpl/protocol/STLedgerEntry.h>
26#include <xrpl/protocol/STObject.h>
27#include <xrpl/protocol/STTx.h>
28#include <xrpl/protocol/STXChainBridge.h>
29#include <xrpl/protocol/SecretKey.h>
30#include <xrpl/protocol/Seed.h>
31#include <xrpl/protocol/TER.h>
32#include <xrpl/protocol/TxFlags.h>
33#include <xrpl/protocol/XChainAttestations.h>
34#include <xrpl/protocol/XRPAmount.h>
35#include <xrpl/tx/ApplyContext.h>
36#include <xrpl/tx/SignerEntries.h>
37#include <xrpl/tx/Transactor.h>
38#include <xrpl/tx/paths/Flow.h>
39#include <xrpl/tx/paths/detail/Steps.h>
114checkAttestationPublicKey(
116 std::unordered_map<AccountID, std::uint32_t>
const& signersList,
117 AccountID const& attestationSignerAccount,
121 if (!signersList.
contains(attestationSignerAccount))
128 if (
auto const sleAttestationSigningAccount =
131 if (accountFromPK == attestationSignerAccount)
134 if (sleAttestationSigningAccount->isFlag(lsfDisableMaster))
136 JLOG(j.
trace()) <<
"Attempt to add an attestation with "
137 "disabled master key.";
144 if (std::optional<AccountID>
const regularKey =
145 (*sleAttestationSigningAccount)[~sfRegularKey];
146 regularKey != accountFromPK)
150 JLOG(j.
trace()) <<
"Attempt to add an attestation with "
151 "account present and non-present regular key.";
155 JLOG(j.
trace()) <<
"Attempt to add an attestation with "
156 "account present and mismatched "
157 "regular key/public key.";
168 JLOG(j.
trace()) <<
"Attempt to add an attestation with non-existant account "
169 "and mismatched pk/account pair.";
188enum class CheckDst {
Check, Ignore };
189template <
class TAttestation>
190std::expected<std::vector<AccountID>,
TER>
194 typename TAttestation::MatchFields
const& toMatch,
196 std::uint32_t quorum,
197 std::unordered_map<AccountID, std::uint32_t>
const& signersList,
204 return checkAttestationPublicKey(view, signersList, a.keyAccount, a.publicKey, j) !=
209 std::vector<AccountID> rewardAccounts;
211 std::uint32_t weight = 0;
214 auto const matchR = a.match(toMatch);
221 auto i = signersList.
find(a.keyAccount);
222 if (i == signersList.
end())
225 UNREACHABLE(
"xrpl::claimHelper : invalid inputs");
231 rewardAccounts.
push_back(a.rewardAccount);
234 if (weight >= quorum)
235 return rewardAccounts;
271struct OnNewAttestationResult
273 std::optional<std::vector<AccountID>> rewardAccounts;
279template <
class TAttestation>
280[[nodiscard]] OnNewAttestationResult
284 typename TAttestation::TSignedAttestation
const* attBegin,
285 typename TAttestation::TSignedAttestation
const* attEnd,
286 std::uint32_t quorum,
287 std::unordered_map<AccountID, std::uint32_t>
const& signersList,
290 bool changed =
false;
291 for (
auto att = attBegin; att != attEnd; ++att)
293 auto const ter = checkAttestationPublicKey(
294 view, signersList, att->attestationSignerAccount, att->publicKey, j);
304 auto const& claimSigningAccount = att->attestationSignerAccount;
306 attestations, [&](
auto const& a) {
return a.keyAccount == claimSigningAccount; });
311 *i = TAttestation{*att};
321 auto r = claimHelper(
324 typename TAttestation::MatchFields{*attBegin},
331 return {.rewardAccounts = std::nullopt, .changed = changed};
333 return {std::move(r.value()), changed};
339std::expected<std::vector<AccountID>,
TER>
344 bool wasLockingChainSend,
345 std::uint32_t quorum,
346 std::unordered_map<AccountID, std::uint32_t>
const& signersList,
350 sendingAmount, wasLockingChainSend, std::nullopt};
351 return claimHelper(
attestations, view, toMatch, CheckDst::Ignore, quorum, signersList, j);
354enum class CanCreateDstPolicy {
No,
Yes };
356enum class DepositAuthPolicy {
Normal, DstCanBypass };
360struct TransferHelperSubmittingAccountInfo
363 STAmount preFeeBalance;
364 STAmount postFeeBalance;
395 std::optional<std::uint32_t>
const& dstTag,
396 std::optional<AccountID>
const& claimOwner,
398 CanCreateDstPolicy canCreate,
399 DepositAuthPolicy depositAuthPolicy,
400 std::optional<TransferHelperSubmittingAccountInfo>
const& submittingAccountInfo,
407 if (
auto sleDst = psb.read(dstK))
411 if (sleDst->isFlag(lsfRequireDestTag) && !dstTag)
417 bool const canBypassDepositAuth =
418 dst == claimOwner && depositAuthPolicy == DepositAuthPolicy::DstCanBypass;
420 if (!canBypassDepositAuth && sleDst->isFlag(lsfDepositAuth) &&
426 else if (!amt.native() || canCreate == CanCreateDstPolicy::No)
434 XRPL_ASSERT(sleSrc,
"xrpl::transferHelper : non-null source account");
441 auto const availableBalance = [&]() ->
STAmount {
442 STAmount curBal = (*sleSrc)[sfBalance];
446 if (!submittingAccountInfo || submittingAccountInfo->account != src ||
447 submittingAccountInfo->postFeeBalance != curBal)
449 return submittingAccountInfo->preFeeBalance;
452 if (availableBalance < amt + reserve)
458 auto sleDst = psb.peek(dstK);
461 if (canCreate == CanCreateDstPolicy::No)
466 if (amt < psb.fees().reserve)
468 JLOG(j.
trace()) <<
"Insufficient payment to create account.";
474 sleDst->setAccountID(sfAccount, dst);
475 sleDst->setFieldU32(sfSequence, psb.seq());
480 (*sleSrc)[sfBalance] = (*sleSrc)[sfBalance] - amt;
481 (*sleDst)[sfBalance] = (*sleDst)[sfBalance] + amt;
488 auto const result =
flow(
514enum class OnTransferFail {
525struct FinalizeClaimHelperResult
530 std::optional<TER> mainFundsTer;
532 std::optional<TER> rewardTer;
534 std::optional<TER> rmSleTer;
555 return *mainFundsTer;
564 return *mainFundsTer;
602FinalizeClaimHelperResult
607 std::optional<std::uint32_t>
const& dstTag,
612 std::vector<AccountID>
const& rewardAccounts,
614 Keylet const& claimIDKeylet,
615 OnTransferFail onTransferFail,
616 DepositAuthPolicy depositAuthPolicy,
619 FinalizeClaimHelperResult result;
622 STAmount const thisChainAmount = [&] {
624 r.
setIssue(bridgeSpec.issue(dstChain));
627 auto const& thisDoor = bridgeSpec.door(dstChain);
641 result.mainFundsTer = transferHelper(
648 CanCreateDstPolicy::Yes,
653 if (!
isTesSuccess(*result.mainFundsTer) && onTransferFail == OnTransferFail::KeepClaim)
659 result.rewardTer = [&]() ->
TER {
660 if (rewardAccounts.
empty())
667 auto const roundMode = innerSb.rules().enabled(fixXChainRewardRounding)
673 return divide(rewardPool, den, rewardPool.asset());
676 for (
auto const& rewardAccount : rewardAccounts)
678 auto const thTer = transferHelper(
686 CanCreateDstPolicy::No,
687 DepositAuthPolicy::Normal,
695 distributed += share;
701 if (distributed > rewardPool)
708 (onTransferFail == OnTransferFail::KeepClaim || *result.rewardTer ==
tecINTERNAL))
718 innerSb.apply(outerSb);
722 if (
auto const sleClaimID = outerSb.peek(claimIDKeylet))
724 auto const cidOwner = (*sleClaimID)[sfAccount];
728 auto const page = (*sleClaimID)[sfOwnerNode];
729 if (!outerSb.dirRemove(
keylet::ownerDir(cidOwner), page, sleClaimID->key(),
true))
731 JLOG(j.
fatal()) <<
"Unable to delete xchain seq number from owner.";
738 outerSb.erase(sleClaimID);
755std::tuple<std::unordered_map<AccountID, std::uint32_t>, std::uint32_t,
TER>
756getSignersListAndQuorum(
ReadView const& view,
SLE const& sleBridge, beast::Journal j)
758 std::unordered_map<AccountID, std::uint32_t> r;
761 AccountID const thisDoor = sleBridge[sfAccount];
762 auto const sleDoor = [&] {
return view.read(
keylet::account(thisDoor)); }();
774 q = (*sleS)[sfSignerQuorum];
783 for (
auto const& as : *accountSigners)
785 r[as.account] = as.weight;
791template <
class R,
class F>
796 if (
auto r = getter(bridgeSpec, ct))
798 if ((*r)[sfXChainBridge] == bridgeSpec)
811 return readOrpeekBridge<SLE>(
821 return readOrpeekBridge<SLE const>(
830template <
class TIter>
832applyClaimAttestations(
839 std::unordered_map<AccountID, std::uint32_t>
const& signersList,
840 std::uint32_t quorum,
843 if (attBegin == attEnd)
852 OnNewAttestationResult newAttResult;
853 STAmount rewardAmount;
857 auto const scopeResult = [&]() -> std::expected<ScopeResult, TER> {
862 auto const sleClaimID = psb.peek(claimIDKeylet);
867 std::vector<attestations::AttestationClaim> atts;
869 for (
auto att = attBegin; att != attEnd; ++att)
871 if (!signersList.
contains(att->attestationSignerAccount))
881 AccountID const otherChainSource = (*sleClaimID)[sfOtherChainSource];
882 if (attBegin->sendingAccount != otherChainSource)
893 if (attDstChain != dstChain)
901 auto const newAttResult = onNewAttestations(
905 &atts[0] + atts.
size(),
911 sleClaimID->setFieldArray(sfXChainClaimAttestations, curAtts.toSTArray());
912 psb.update(sleClaimID);
915 newAttResult, (*sleClaimID)[sfSignatureReward], (*sleClaimID)[sfAccount]};
918 if (!scopeResult.has_value())
919 return scopeResult.error();
921 auto const& [newAttResult, rewardAmount, cidOwner] = scopeResult.value();
922 auto const& [rewardAccounts, attListChanged] = newAttResult;
923 if (rewardAccounts && attBegin->dst)
925 auto const r = finalizeClaimHelper(
931 attBegin->sendingAmount,
937 OnTransferFail::KeepClaim,
938 DepositAuthPolicy::Normal,
941 auto const rTer = r.ter();
953template <
class TIter>
955applyCreateAccountAttestations(
965 std::unordered_map<AccountID, std::uint32_t>
const& signersList,
966 std::uint32_t quorum,
969 if (attBegin == attEnd)
974 auto const claimCountResult = [&]() -> std::expected<std::uint64_t, TER> {
975 auto const sleBridge = psb.peek(bridgeK);
979 return (*sleBridge)[sfXChainAccountClaimCount];
982 if (!claimCountResult.has_value())
983 return claimCountResult.error();
985 std::uint64_t
const claimCount = claimCountResult.value();
987 if (attBegin->createCount <= claimCount)
1003 if (attDstChain != dstChain)
1009 auto const claimIDKeylet =
1014 OnNewAttestationResult newAttResult;
1016 XChainCreateAccountAttestations curAtts;
1019 auto const scopeResult = [&]() -> std::expected<ScopeResult, TER> {
1027 auto const sleClaimID = psb.peek(claimIDKeylet);
1028 bool createCID =
false;
1033 auto const sleDoor = psb.peek(doorK);
1038 auto const balance = (*sleDoor)[sfBalance];
1039 auto const reserve =
accountReserve(psb, sleDoor, j, {.ownerCountDelta = 1});
1041 if (balance < reserve)
1045 std::vector<attestations::AttestationCreateAccount> atts;
1047 for (
auto att = attBegin; att != attEnd; ++att)
1049 if (!signersList.
contains(att->attestationSignerAccount))
1062 sleClaimID->getFieldArray(sfXChainCreateAccountAttestations)};
1067 auto const newAttResult = onNewAttestations(
1071 &atts[0] + atts.
size(),
1082 sleClaimID->setFieldArray(sfXChainCreateAccountAttestations, curAtts.toSTArray());
1083 psb.update(sleClaimID);
1085 return ScopeResult{newAttResult, createCID, curAtts};
1088 if (!scopeResult.has_value())
1089 return scopeResult.error();
1091 auto const& [attResult, createCID, curAtts] = scopeResult.value();
1092 auto const& [rewardAccounts, attListChanged] = attResult;
1095 if (rewardAccounts && claimCount + 1 == attBegin->createCount)
1097 auto const r = finalizeClaimHelper(
1103 attBegin->sendingAmount,
1105 attBegin->rewardAmount,
1109 OnTransferFail::RemoveClaim,
1110 DepositAuthPolicy::Normal,
1113 auto const rTer = r.ter();
1122 auto const sleBridge = psb.peek(bridgeK);
1125 (*sleBridge)[sfXChainAccountClaimCount] = attBegin->createCount;
1126 psb.update(sleBridge);
1131 (*createdSleClaimID)[sfAccount] = doorAccount;
1132 (*createdSleClaimID)[sfXChainBridge] = bridgeSpec;
1133 (*createdSleClaimID)[sfXChainAccountCreateCount] = attBegin->createCount;
1134 createdSleClaimID->setFieldArray(sfXChainCreateAccountAttestations, curAtts.toSTArray());
1137 auto const page = psb.dirInsert(
1141 (*createdSleClaimID)[sfOwnerNode] = *
page;
1143 auto const sleDoor = psb.peek(doorK);
1149 psb.insert(createdSleClaimID);
1150 psb.update(sleDoor);
1158template <
class TAttestation>
1159std::optional<TAttestation>
1160toClaim(
STTx const& tx)
1174 return TAttestation(o);
1178 return std::nullopt;
1182template <
class TAttestation>
1189 auto const att = toClaim<TAttestation>(ctx.tx);
1194 if (!att->verify(bridgeSpec))
1196 if (!att->validAmounts())
1199 if (att->sendingAmount.signum() <= 0)
1202 if (att->sendingAmount.asset() != expectedIssue)
1208template <
class TAttestation>
1212 auto const att = toClaim<TAttestation>(ctx.tx);
1218 auto const sleBridge = readBridge(ctx.view, bridgeSpec);
1224 AccountID const attestationSignerAccount{ctx.tx[sfAttestationSignerAccount]};
1225 PublicKey const pk{ctx.tx[sfPublicKey]};
1228 auto const [signersList, quorum, slTer] = getSignersListAndQuorum(ctx.view, *sleBridge, ctx.j);
1233 return checkAttestationPublicKey(ctx.view, signersList, attestationSignerAccount, pk, ctx.j);
1236template <
class TAttestation>
1240 auto const att = toClaim<TAttestation>(ctx.tx);
1251 STXChainBridge::ChainType srcChain = STXChainBridge::ChainType::Locking;
1252 std::unordered_map<AccountID, std::uint32_t> signersList;
1253 std::uint32_t quorum{};
1258 auto const scopeResult = [&]() -> std::expected<ScopeResult, TER> {
1263 auto sleBridge = readBridge(ctx.view(), bridgeSpec);
1268 Keylet const bridgeK{ltBRIDGE, sleBridge->key()};
1269 AccountID const thisDoor = (*sleBridge)[sfAccount];
1273 if (thisDoor == bridgeSpec.lockingChainDoor())
1277 else if (thisDoor == bridgeSpec.issuingChainDoor())
1289 auto [signersList, quorum, slTer] =
1290 getSignersListAndQuorum(ctx.view(), *sleBridge, ctx.journal);
1295 return ScopeResult{srcChain, std::move(signersList), quorum, thisDoor, bridgeK};
1298 if (!scopeResult.has_value())
1299 return scopeResult.error();
1301 auto const& [srcChain, signersList, quorum, thisDoor, bridgeK] = scopeResult.value();
1309 return applyClaimAttestations(
1322 return applyCreateAccountAttestations(
1344 auto const account = ctx.
tx[sfAccount];
1345 auto const reward = ctx.
tx[sfSignatureReward];
1346 auto const minAccountCreate = ctx.
tx[~sfMinAccountCreateAmount];
1347 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1349 if (bridgeSpec.lockingChainDoor() == bridgeSpec.issuingChainDoor())
1354 if (bridgeSpec.lockingChainDoor() != account && bridgeSpec.issuingChainDoor() != account)
1359 if (
isXRP(bridgeSpec.lockingChainIssue()) !=
isXRP(bridgeSpec.issuingChainIssue()))
1366 if (!
isXRP(reward) || reward.signum() < 0)
1371 if (minAccountCreate &&
1372 ((!
isXRP(*minAccountCreate) || minAccountCreate->signum() <= 0) ||
1373 !
isXRP(bridgeSpec.lockingChainIssue()) || !
isXRP(bridgeSpec.issuingChainIssue())))
1378 if (
isXRP(bridgeSpec.issuingChainIssue()))
1385 if (bridgeSpec.issuingChainDoor() != kRootAccount)
1394 if (bridgeSpec.issuingChainDoor() != bridgeSpec.issuingChainIssue().account)
1400 if (bridgeSpec.lockingChainDoor() == bridgeSpec.lockingChainIssue().account)
1413 auto const account = ctx.
tx[sfAccount];
1414 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1430 if (!
isXRP(bridgeSpec.issue(chainType)))
1439 if (sleIssuer->isFlag(lsfAllowTrustLineClawback))
1449 auto const balance = (*sleAcc)[sfBalance];
1452 if (balance < reserve)
1462 auto const account =
ctx_.tx[sfAccount];
1463 auto const bridgeSpec =
ctx_.tx[sfXChainBridge];
1464 auto const reward =
ctx_.tx[sfSignatureReward];
1465 auto const minAccountCreate =
ctx_.tx[~sfMinAccountCreateAmount];
1477 (*sleBridge)[sfAccount] = account;
1478 (*sleBridge)[sfSignatureReward] = reward;
1479 if (minAccountCreate)
1480 (*sleBridge)[sfMinAccountCreateAmount] = *minAccountCreate;
1481 (*sleBridge)[sfXChainBridge] = bridgeSpec;
1482 (*sleBridge)[sfXChainClaimID] = 0;
1483 (*sleBridge)[sfXChainAccountCreateCount] = 0;
1484 (*sleBridge)[sfXChainAccountClaimCount] = 0;
1488 auto const page =
ctx_.view().dirInsert(
1492 (*sleBridge)[sfOwnerNode] = *page;
1497 ctx_.view().insert(sleBridge);
1498 ctx_.view().update(sleAcct);
1508 return tfXChainModifyBridgeMask;
1514 auto const account = ctx.
tx[sfAccount];
1515 auto const reward = ctx.
tx[~sfSignatureReward];
1516 auto const minAccountCreate = ctx.
tx[~sfMinAccountCreateAmount];
1517 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1518 bool const clearAccountCreate = ctx.
tx.
isFlag(tfClearAccountCreateAmount);
1520 if (!reward && !minAccountCreate && !clearAccountCreate)
1526 if (minAccountCreate && clearAccountCreate)
1532 if (bridgeSpec.lockingChainDoor() != account && bridgeSpec.issuingChainDoor() != account)
1537 if (reward && (!
isXRP(*reward) || reward->signum() < 0))
1542 if (minAccountCreate &&
1543 ((!
isXRP(*minAccountCreate) || minAccountCreate->signum() <= 0) ||
1544 !
isXRP(bridgeSpec.lockingChainIssue()) || !
isXRP(bridgeSpec.issuingChainIssue())))
1555 auto const account = ctx.
tx[sfAccount];
1556 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1572 auto const account =
ctx_.tx[sfAccount];
1573 auto const bridgeSpec =
ctx_.tx[sfXChainBridge];
1574 auto const reward =
ctx_.tx[~sfSignatureReward];
1575 auto const minAccountCreate =
ctx_.tx[~sfMinAccountCreateAmount];
1576 bool const clearAccountCreate =
ctx_.tx.isFlag(tfClearAccountCreateAmount);
1590 (*sleBridge)[sfSignatureReward] = *reward;
1591 if (minAccountCreate)
1593 (*sleBridge)[sfMinAccountCreateAmount] = *minAccountCreate;
1595 if (clearAccountCreate && sleBridge->isFieldPresent(sfMinAccountCreateAmount))
1597 sleBridge->makeFieldAbsent(sfMinAccountCreateAmount);
1599 ctx_.view().update(sleBridge);
1610 auto const amount = ctx.
tx[sfAmount];
1612 if (amount.signum() <= 0 ||
1627 STAmount const& thisChainAmount = ctx.
tx[sfAmount];
1628 auto const claimID = ctx.
tx[sfXChainClaimID];
1630 auto const sleBridge = readBridge(ctx.
view, bridgeSpec);
1641 auto const thisDoor = (*sleBridge)[sfAccount];
1642 bool isLockingChain =
false;
1646 isLockingChain =
true;
1650 isLockingChain =
false;
1681 auto const otherChainAmount = [&]() ->
STAmount {
1703 if ((*sleClaimID)[sfAccount] != account)
1720 auto const dst =
ctx_.tx[sfDestination];
1723 auto const claimID =
ctx_.tx[sfXChainClaimID];
1735 auto const scopeResult = [&]() -> std::expected<ScopeResult, TER> {
1742 auto const sleBridge = peekBridge(psb, bridgeSpec);
1743 auto const sleClaimID = psb.
peek(claimIDKeylet);
1745 if (!(sleBridge && sleClaimID && sleAcct))
1748 AccountID const thisDoor = (*sleBridge)[sfAccount];
1767 auto const sendingAmount = [&]() ->
STAmount {
1773 auto const [signersList, quorum, slTer] =
1774 getSignersListAndQuorum(
ctx_.view(), *sleBridge,
ctx_.journal);
1781 auto const claimR = onClaim(
1789 if (!claimR.has_value())
1793 .rewardAccounts = claimR.value(),
1794 .rewardPoolSrc = (*sleClaimID)[sfAccount],
1795 .sendingAmount = sendingAmount,
1796 .srcChain = srcChain,
1797 .signatureReward = (*sleClaimID)[sfSignatureReward],
1801 if (!scopeResult.has_value())
1802 return scopeResult.error();
1804 auto const& [rewardAccounts, rewardPoolSrc, sendingAmount, srcChain, signatureReward] =
1805 scopeResult.
value();
1808 auto const r = finalizeClaimHelper(
1820 OnTransferFail::KeepClaim,
1821 DepositAuthPolicy::DstCanBypass,
1823 if (!r.isTesSuccess())
1836 auto const maxSpend = [&] {
1837 auto const amount = ctx.
tx[sfAmount];
1838 if (amount.native() && amount.signum() > 0)
1839 return amount.xrp();
1849 auto const amount = ctx.
tx[sfAmount];
1850 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1852 if (amount.signum() <= 0 || !
isLegalNet(amount))
1855 if (amount.asset() != bridgeSpec.lockingChainIssue() &&
1856 amount.asset() != bridgeSpec.issuingChainIssue())
1865 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1866 auto const amount = ctx.
tx[sfAmount];
1868 auto const sleBridge = readBridge(ctx.
view, bridgeSpec);
1874 AccountID const thisDoor = (*sleBridge)[sfAccount];
1877 if (thisDoor == account)
1883 bool isLockingChain =
false;
1885 if (thisDoor == bridgeSpec.lockingChainDoor())
1887 isLockingChain =
true;
1889 else if (thisDoor == bridgeSpec.issuingChainDoor())
1891 isLockingChain =
false;
1901 if (bridgeSpec.lockingChainIssue() != ctx.
tx[sfAmount].asset())
1906 if (bridgeSpec.issuingChainIssue() != ctx.
tx[sfAmount].asset())
1918 auto const account =
ctx_.tx[sfAccount];
1919 auto const amount =
ctx_.tx[sfAmount];
1920 auto const bridgeSpec =
ctx_.tx[sfXChainBridge];
1926 auto const sleBridge = readBridge(psb, bridgeSpec);
1930 auto const dst = (*sleBridge)[sfAccount];
1933 TransferHelperSubmittingAccountInfo submittingAccountInfo{
1936 .postFeeBalance = (*sleAccount)[sfBalance]};
1938 auto const thTer = transferHelper(
1945 CanCreateDstPolicy::No,
1946 DepositAuthPolicy::Normal,
1947 submittingAccountInfo,
1963 auto const reward = ctx.
tx[sfSignatureReward];
1974 auto const account = ctx.
tx[sfAccount];
1975 auto const bridgeSpec = ctx.
tx[sfXChainBridge];
1976 auto const sleBridge = readBridge(ctx.
view, bridgeSpec);
1984 auto const reward = ctx.
tx[sfSignatureReward];
1986 if (reward != (*sleBridge)[sfSignatureReward])
1997 auto const balance = (*sleAcc)[sfBalance];
1999 if (balance < reserve)
2009 auto const account =
ctx_.tx[sfAccount];
2010 auto const bridgeSpec =
ctx_.tx[sfXChainBridge];
2011 auto const reward =
ctx_.tx[sfSignatureReward];
2012 auto const otherChainSrc =
ctx_.tx[sfOtherChainSource];
2018 auto const sleBridge = peekBridge(
ctx_.view(), bridgeSpec);
2022 std::uint32_t const claimID = (*sleBridge)[sfXChainClaimID] + 1;
2029 (*sleBridge)[sfXChainClaimID] = claimID;
2032 if (
ctx_.view().exists(claimIDKeylet))
2040 (*sleClaimID)[sfAccount] = account;
2041 (*sleClaimID)[sfXChainBridge] = bridgeSpec;
2042 (*sleClaimID)[sfXChainClaimID] = claimID;
2043 (*sleClaimID)[sfOtherChainSource] = otherChainSrc;
2044 (*sleClaimID)[sfSignatureReward] = reward;
2045 sleClaimID->setFieldArray(sfXChainClaimAttestations,
STArray{sfXChainClaimAttestations});
2049 auto const page =
ctx_.view().dirInsert(
2053 (*sleClaimID)[sfOwnerNode] = *page;
2058 ctx_.view().insert(sleClaimID);
2059 ctx_.view().update(sleBridge);
2060 ctx_.view().update(sleAcct);
2070 return attestationPreflight<attestations::AttestationClaim>(ctx);
2076 return attestationPreclaim<attestations::AttestationClaim>(ctx);
2082 return attestationDoApply<attestations::AttestationClaim>(
ctx_);
2090 return attestationPreflight<attestations::AttestationCreateAccount>(ctx);
2096 return attestationPreclaim<attestations::AttestationCreateAccount>(ctx);
2102 return attestationDoApply<attestations::AttestationCreateAccount>(
ctx_);
2110 auto const amount = ctx.
tx[sfAmount];
2112 if (amount.signum() <= 0 || !amount.native())
2115 auto const reward = ctx.
tx[sfSignatureReward];
2116 if (reward.signum() < 0 || !reward.native())
2119 if (reward.asset() != amount.asset())
2130 STAmount const reward = ctx.
tx[sfSignatureReward];
2132 auto const sleBridge = readBridge(ctx.
view, bridgeSpec);
2138 if (reward != (*sleBridge)[sfSignatureReward])
2145 if (!minCreateAmount)
2148 if (amount < *minCreateAmount)
2151 if (minCreateAmount->asset() != amount.
asset())
2154 AccountID const thisDoor = (*sleBridge)[sfAccount];
2156 if (thisDoor == account)
2179 if (bridgeSpec.
issue(srcChain) != ctx.
tx[sfAmount].asset())
2202 auto const sleBridge = peekBridge(psb, bridge);
2206 auto const dst = (*sleBridge)[sfAccount];
2209 TransferHelperSubmittingAccountInfo submittingAccountInfo{
2212 .postFeeBalance = (*sle)[sfBalance]};
2213 STAmount const toTransfer = amount + reward;
2214 auto const thTer = transferHelper(
2221 CanCreateDstPolicy::Yes,
2222 DepositAuthPolicy::Normal,
2223 submittingAccountInfo,
2229 (*sleBridge)[sfXChainAccountCreateCount] = (*sleBridge)[sfXChainAccountCreateCount] + 1;
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
State information when applying a tx.
Writeable view to a ledger, for applying a transaction.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static std::uint32_t getFlagsMask(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static RoundingMode setround(RoundingMode inMode)
static RoundingMode getround()
A wrapper which makes credits unavailable to balances.
void apply(RawView &to)
Apply changes to base view.
Interface for ledger entry changes.
virtual bool exists(Keylet const &k) const =0
Determine if a state item exists.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
void setIssue(Asset const &asset)
Set the Issue for this amount.
STAmount zeroed() const
Returns a zero value with the same issuer and currency.
Asset const & asset() const
STAmount const & value() const noexcept
std::shared_ptr< STLedgerEntry > pointer
std::shared_ptr< STLedgerEntry const > const & const_ref
std::shared_ptr< STLedgerEntry const > const_pointer
bool isFlag(std::uint32_t) const
void setAccountID(SField const &field, AccountID const &)
AccountID const & issuingChainDoor() const
static ChainType dstChain(bool wasLockingChainSend)
AccountID const & lockingChainDoor() const
static ChainType srcChain(bool wasLockingChainSend)
Issue const & issue(ChainType ct) const
Issue const & issuingChainIssue() const
static ChainType otherChain(ChainType ct)
Issue const & lockingChainIssue() const
static std::expected< std::vector< SignerEntry >, NotTEC > deserialize(STObject const &obj, beast::Journal journal, std::string_view annotation)
AccountID const accountID_
Class describing the consequences to the account of applying a transaction if the transaction consume...
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TxConsequences makeTxConsequences(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
SLE::pointer peek(Keylet const &k) override
Prepare to modify the SLE associated with key.
void update(SLE::ref sle) override
Indicate changes to a peeked SLE.
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Keylet depositPreauth(AccountID const &owner, AccountID const &preauthorized) noexcept
A DepositPreauth.
Keylet bridge(STXChainBridge const &bridge, STXChainBridge::ChainType chainType)
Keylet signerList(AccountID const &account) noexcept
A SignerList.
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Keylet page(uint256 const &root, std::uint64_t const index=0) noexcept
A page in a directory.
Keylet account(AccountID const &id) noexcept
AccountID root.
Keylet xChainClaimID(STXChainBridge const &bridge, std::uint64_t const seq)
Keylet xChainCreateAccountClaimID(STXChainBridge const &bridge, std::uint64_t const seq)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
STAmount divide(STAmount const &amount, Rate const &rate)
constexpr size_t kXbridgeMaxAccountCreateClaims
bool isTerRetry(TER x) noexcept
void decreaseOwnerCountForObject(ApplyView &view, SLE::ref accountSle, SLE::ref objectSle, std::uint32_t count, beast::Journal j)
Decrease owner-count fields for an existing ledger object.
bool isXRP(AccountID const &c)
void increaseOwnerCount(ApplyView &view, SLE::ref accountSle, SLE::ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
bool isLegalNet(STAmount const &value)
Seed generateSeed(std::string const &passPhrase)
Generate a seed deterministically.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
@ Yes
We have consensus along with the network.
@ No
We do not have consensus.
TERSubset< CanCvtToNotTEC > NotTEC
bool isTefFailure(TER x) noexcept
StrandResult< TInAmt, TOutAmt > flow(PaymentSandbox const &baseView, Strand const &strand, std::optional< TInAmt > const &maxIn, TOutAmt const &out, beast::Journal j)
Request out amount from a strand.
std::function< void(SLE::ref)> describeOwnerDir(AccountID const &account)
Returns a function that sets the owner on a directory SLE.
AccountID calcAccountID(PublicKey const &pk)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
@ temXCHAIN_BRIDGE_BAD_MIN_ACCOUNT_CREATE_AMOUNT
@ temXCHAIN_BRIDGE_NONDOOR_OWNER
@ temXCHAIN_BRIDGE_BAD_ISSUES
@ temXCHAIN_BRIDGE_BAD_REWARD_AMOUNT
@ temXCHAIN_EQUAL_DOOR_ACCOUNTS
bool isTesSuccess(TER x) noexcept
TERSubset< CanCvtToTER > TER
@ tecXCHAIN_INSUFF_CREATE_AMOUNT
@ tecXCHAIN_NO_SIGNERS_LIST
@ tecXCHAIN_SENDING_ACCOUNT_MISMATCH
@ tecXCHAIN_BAD_TRANSFER_ISSUE
@ tecXCHAIN_PROOF_UNKNOWN_KEY
@ tecXCHAIN_ACCOUNT_CREATE_PAST
@ tecXCHAIN_PAYMENT_FAILED
@ tecXCHAIN_ACCOUNT_CREATE_TOO_MANY
@ tecXCHAIN_CREATE_ACCOUNT_NONXRP_ISSUE
@ tecXCHAIN_BAD_PUBLIC_KEY_ACCOUNT_PAIR
@ tecXCHAIN_CREATE_ACCOUNT_DISABLED
@ tecINSUFFICIENT_RESERVE
@ tecXCHAIN_CLAIM_NO_QUORUM
@ tecXCHAIN_REWARD_MISMATCH
bool isTecClaim(TER x) noexcept
XRPAmount accountReserve(ReadView const &view, SLE::const_ref sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
A pair of SHAMap key and LedgerEntryType.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.