rework URIToken amendment to use 5 different explicit transactors rather than inferred operations

This commit is contained in:
Richard Holland
2023-02-09 10:17:31 +00:00
parent 2916b7e692
commit 69327445e9
10 changed files with 370 additions and 289 deletions

View File

@@ -47,34 +47,38 @@ namespace hook
using namespace ripple;
static const std::map<uint16_t, uint8_t> TSHAllowances =
{
{ttPAYMENT, tshROLLBACK },
{ttESCROW_CREATE, tshROLLBACK },
{ttESCROW_FINISH, tshROLLBACK },
{ttACCOUNT_SET, tshNONE },
{ttESCROW_CANCEL, tshCOLLECT },
{ttREGULAR_KEY_SET, tshROLLBACK },
{ttOFFER_CREATE, tshCOLLECT },
{ttOFFER_CANCEL, tshNONE },
{ttTICKET_CREATE, tshNONE },
{ttSIGNER_LIST_SET, tshROLLBACK },
{ttPAYCHAN_CREATE, tshROLLBACK },
{ttPAYCHAN_FUND, tshCOLLECT },
{ttPAYCHAN_CLAIM, tshCOLLECT },
{ttCHECK_CREATE, tshROLLBACK },
{ttCHECK_CASH, tshROLLBACK },
{ttCHECK_CANCEL, tshCOLLECT },
{ttDEPOSIT_PREAUTH, tshROLLBACK },
{ttTRUST_SET, tshCOLLECT },
{ttACCOUNT_DELETE, tshROLLBACK },
{ttHOOK_SET, tshNONE },
{ttNFTOKEN_MINT, tshROLLBACK },
{ttNFTOKEN_BURN, tshCOLLECT },
{ttNFTOKEN_CREATE_OFFER, tshROLLBACK },
{ttNFTOKEN_CANCEL_OFFER, tshCOLLECT },
{ttNFTOKEN_ACCEPT_OFFER, tshROLLBACK },
{ttCLAIM_REWARD, tshROLLBACK },
{ttINVOKE, tshROLLBACK },
{ttURI_TOKEN, tshROLLBACK },
{ttPAYMENT, tshROLLBACK },
{ttESCROW_CREATE, tshROLLBACK },
{ttESCROW_FINISH, tshROLLBACK },
{ttACCOUNT_SET, tshNONE },
{ttESCROW_CANCEL, tshCOLLECT },
{ttREGULAR_KEY_SET, tshROLLBACK },
{ttOFFER_CREATE, tshCOLLECT },
{ttOFFER_CANCEL, tshNONE },
{ttTICKET_CREATE, tshNONE },
{ttSIGNER_LIST_SET, tshROLLBACK },
{ttPAYCHAN_CREATE, tshROLLBACK },
{ttPAYCHAN_FUND, tshCOLLECT },
{ttPAYCHAN_CLAIM, tshCOLLECT },
{ttCHECK_CREATE, tshROLLBACK },
{ttCHECK_CASH, tshROLLBACK },
{ttCHECK_CANCEL, tshCOLLECT },
{ttDEPOSIT_PREAUTH, tshROLLBACK },
{ttTRUST_SET, tshCOLLECT },
{ttACCOUNT_DELETE, tshROLLBACK },
{ttHOOK_SET, tshNONE },
{ttNFTOKEN_MINT, tshROLLBACK },
{ttNFTOKEN_BURN, tshCOLLECT },
{ttNFTOKEN_CREATE_OFFER, tshROLLBACK },
{ttNFTOKEN_CANCEL_OFFER, tshCOLLECT },
{ttNFTOKEN_ACCEPT_OFFER, tshROLLBACK },
{ttCLAIM_REWARD, tshROLLBACK },
{ttINVOKE, tshROLLBACK },
{ttURITOKEN_MINT, tshNONE },
{ttURITOKEN_BURN, tshROLLBACK },
{ttURITOKEN_BUY, tshROLLBACK },
{ttURITOKEN_CREATE_SELL_OFFER, tshROLLBACK },
{ttURITOKEN_CANCEL_SELL_OFFER, tshNONE },
};

View File

@@ -77,11 +77,28 @@ namespace hook
switch (tt)
{
case ttURI_TOKEN:
case ttURITOKEN_BURN:
{
if (!tx.isFieldPresent(sfURITokenID))
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);
// the issuer is a strong tsh if the burnable flag is set
if (issuer != owner)
ADD_TSH(issuer, ut->getFlags() & tfBurnable);
break;
}
case ttURITOKEN_BUY:
{
Keylet const id { ltURI_TOKEN, tx.getFieldH256(sfURITokenID) };
if (!rv.exists(id))
return {};
@@ -92,14 +109,43 @@ namespace hook
auto const owner = ut->getAccountID(sfOwner);
// if the owner is initating the txn then there are no other TSH
if (owner == tx.getAccountID(sfAccount))
if (owner != tx.getAccountID(sfAccount))
{
// current owner is a strong TSH
ADD_TSH(owner, canRollback);
}
// 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() & tfBurnable);
break;
}
case ttURITOKEN_CREATE_SELL_OFFER:
{
Keylet const id { ltURI_TOKEN, tx.getFieldH256(sfURITokenID) };
if (!rv.exists(id))
return {};
// if the URIToken has the tfBurnable flag set then the
// owner is a strong TSH, otherwise they are a weak TSH
ADD_TSH(owner, ut->getFlags() & tfBurnable);
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() & tfBurnable);
// destination is a strong tsh
if (tx.isFieldPresent(sfDestination))
ADD_TSH(tx.getAccountID(sfDestination), canRollback);
break;
}
@@ -194,6 +240,8 @@ namespace hook
// self transactions
case ttURITOKEN_MINT:
case ttURITOKEN_CANCEL_SELL_OFFER:
case ttACCOUNT_SET:
case ttOFFER_CANCEL:
case ttTICKET_CREATE:

View File

@@ -22,63 +22,12 @@
#include <ripple/basics/Log.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/Quality.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/TER.h>
#include <ripple/protocol/TxFlags.h>
namespace ripple {
/*
* Mint: URI populated only
* Burn: URITokenID populated, Blank but present URI
* Buy: URITokenID, Amount
* Sell: URITokenID, Amount, [Destination], flags=tfSell,
* Clear: URITokenID only (clear current sell offer)
*/
inline URIOperation
inferOperation(STTx const& tx)
{
bool const hasDigest = tx.isFieldPresent(sfDigest);
bool const hasURI = tx.isFieldPresent(sfURI);
bool const hasID = tx.isFieldPresent(sfURITokenID);
bool const hasAmt = tx.isFieldPresent(sfAmount);
bool const hasDst = tx.isFieldPresent(sfDestination);
uint32_t const flags = tx.getFlags();
bool const hasBurnFlag = flags == tfBurn;
bool const hasSellFlag = flags == tfSell;
bool const hasBurnableFlag = flags == tfBurnable;
bool const blankFlags = flags == 0;
uint16_t combination = (hasDigest ? 0b100000000U : 0) +
(hasURI ? 0b010000000U : 0) + (hasBurnFlag ? 0b001000000U : 0) +
(hasID ? 0b000100000U : 0) + (hasAmt ? 0b000010000U : 0) +
(hasDst ? 0b000001000U : 0) + (hasSellFlag ? 0b000000100U : 0) +
(hasBurnableFlag ? 0b000000010U : 0) + (blankFlags ? 0b000000001U : 0);
switch (combination)
{
case 0b110000001U:
case 0b110000010U:
case 0b010000001U:
case 0b010000010U:
return URIOperation::Mint;
case 0b001100000U:
return URIOperation::Burn;
case 0b000110001U:
return URIOperation::Buy;
case 0b000110100U:
case 0b000111100U:
return URIOperation::Sell;
case 0b000100001U:
return URIOperation::Clear;
default:
return URIOperation::Invalid;
}
}
NotTEC
URIToken::preflight(PreflightContext const& ctx)
{
@@ -89,55 +38,81 @@ URIToken::preflight(PreflightContext const& ctx)
if (!isTesSuccess(ret))
return ret;
auto const op = inferOperation(ctx.tx);
if (op == URIOperation::Invalid)
{
JLOG(ctx.j.warn()) << "Malformed transaction. Check flags/fields, "
<< "documentation for how to specify "
"Mint/Burn/Buy/Sell operations.";
return temMALFORMED;
}
JLOG(ctx.j.trace())
<< "URIToken txnid=" << ctx.tx.getTransactionID()
<< " inferred operation="
<< (op == URIOperation::Invalid
? "Invalid"
: (op == URIOperation::Mint
? "Mint"
: (op == URIOperation::Burn
? "Burn"
: (op == URIOperation::Buy
? "Buy"
: (op == URIOperation::Sell
? "Sell"
: (op == URIOperation::Clear
? "Clear"
: "Unknown"))))))
<< "\n";
uint32_t flags = ctx.tx.getFlags();
uint16_t tt = ctx.tx.getFieldU16(sfTransactionType);
if (op == URIOperation::Mint && ctx.tx.getFieldVL(sfURI).size() > 256)
switch (tt)
{
JLOG(ctx.j.warn())
<< "Malformed transaction. URI may not exceed 256 bytes.";
return temMALFORMED;
}
if (ctx.tx.isFieldPresent(sfAmount))
{
STAmount const amt = ctx.tx.getFieldAmount(sfAmount);
if (!isLegalNet(amt) || amt.signum() <= 0)
case ttURITOKEN_MINT:
{
JLOG(ctx.j.warn()) << "Malformed transaction. Negative or invalid "
"amount specified.";
return temBAD_AMOUNT;
if (flags & tfURITokenMintMask)
return temINVALID_FLAG;
size_t len = ctx.tx.getFieldVL(sfURI).size();
if (len < 1 || len > 256)
{
JLOG(ctx.j.warn())
<< "Malformed transaction. URI must be at least 1 character and no more than 256 characters.";
return temMALFORMED;
}
break;
}
if (badCurrency() == amt.getCurrency())
case ttURITOKEN_CANCEL_SELL_OFFER:
case ttURITOKEN_BURN:
{
JLOG(ctx.j.warn()) << "Malformed transaction. Bad currency.";
return temBAD_CURRENCY;
if (flags & tfURITokenNonMintMask)
return temINVALID_FLAG;
break;
}
case ttURITOKEN_BUY:
case ttURITOKEN_CREATE_SELL_OFFER:
{
if (flags & tfURITokenNonMintMask)
return temINVALID_FLAG;
if (ctx.tx.isFieldPresent(sfDestination) &&
ctx.tx.getAccountID(sfDestination) == ctx.tx.getAccountID(sfAccount))
{
JLOG(ctx.j.warn())
<< "Malformed transaction. "
<< "Cannot create a sell/buy offer to yourself.";
return temREDUNDANT;
}
auto amt = ctx.tx.getFieldAmount(sfAmount);
if (!isLegalNet(amt) || amt.signum() < 0)
{
JLOG(ctx.j.warn())
<< "Malformed transaction. Negative or invalid amount/currency specified.";
return temBAD_AMOUNT;
}
if (badCurrency() == amt.getCurrency())
{
JLOG(ctx.j.warn())
<< "Malformed transaction. Bad currency.";
return temBAD_CURRENCY;
}
if (tt == ttURITOKEN_BUY)
break;
if (amt == beast::zero && !ctx.tx.isFieldPresent(sfDestination))
{
JLOG(ctx.j.warn())
<< "Malformed transaction. "
<< "If no sell-to destination is specified then a non-zero price must be set.";
return temMALFORMED;
}
break;
}
default:
return tefINTERNAL;
}
return preflight2(ctx);
@@ -146,25 +121,19 @@ URIToken::preflight(PreflightContext const& ctx)
TER
URIToken::preclaim(PreclaimContext const& ctx)
{
AccountID const acc = ctx.tx.getAccountID(sfAccount);
URIOperation op = inferOperation(ctx.tx);
std::shared_ptr<SLE const> sleU;
if (ctx.tx.isFieldPresent(sfURITokenID))
sleU = ctx.view.read(
Keylet{ltURI_TOKEN, ctx.tx.getFieldH256(sfURITokenID)});
uint32_t leFlags = sleU ? sleU->getFieldU32(sfFlags) : 0;
std::optional<AccountID> issuer;
std::optional<AccountID> owner;
std::optional<STAmount> saleAmount;
std::optional<AccountID> dest;
std::shared_ptr<SLE const> sleOwner;
if (sleU)
if (ctx.tx.isFieldPresent(sfURITokenID))
{
if (sleU->getFieldU16(sfLedgerEntryType) != ltURI_TOKEN)
sleU = ctx.view.read(Keylet {ltURI_TOKEN, ctx.tx.getFieldH256(sfURITokenID)});
if (!sleU)
return tecNO_ENTRY;
owner = sleU->getAccountID(sfOwner);
@@ -178,32 +147,34 @@ URIToken::preclaim(PreclaimContext const& ctx)
sleOwner = ctx.view.read(keylet::account(*owner));
if (!sleOwner)
{
JLOG(ctx.j.warn()) << "Malformed transaction: owner of URIToken is "
"not in the ledger.";
JLOG(ctx.j.warn())
<< "Malformed transaction: owner of URIToken is not in the ledger.";
return tecNO_ENTRY;
}
}
else if (op != URIOperation::Mint)
return tecNO_ENTRY;
switch (op)
AccountID const acc = ctx.tx.getAccountID(sfAccount);
uint16_t tt = ctx.tx.getFieldU16(sfTransactionType);
switch (tt)
{
case URIOperation::Mint: {
case ttURITOKEN_MINT:
{
// check if this token has already been minted.
if (ctx.view.exists(
keylet::uritoken(acc, ctx.tx.getFieldVL(sfURI))))
if (ctx.view.exists(keylet::uritoken(acc, ctx.tx.getFieldVL(sfURI))))
return tecDUPLICATE;
return tesSUCCESS;
}
case URIOperation::Burn: {
case ttURITOKEN_BURN:
{
if (leFlags == tfBurnable && acc == *issuer)
{
// pass, the issuer can burn the URIToken if they minted it with
// a burn flag
// pass, the issuer can burn the URIToken if they minted it with a burn flag
}
else if (acc == *owner)
else
if (acc == *owner)
{
// pass, the owner can always destroy their own URI token
}
@@ -213,9 +184,10 @@ URIToken::preclaim(PreclaimContext const& ctx)
return tesSUCCESS;
}
case URIOperation::Buy: {
// if the owner is the account then the buy operation is a clear
// operation and we won't bother to check anything else
case ttURITOKEN_BUY:
{
// if the owner is the account then the buy operation is a clear operation
// and we won't bother to check anything else
if (acc == *owner)
return tesSUCCESS;
@@ -239,31 +211,36 @@ URIToken::preclaim(PreclaimContext const& ctx)
if (purchaseAmount.native() && saleAmount->native())
{
// if it's an xrp sale/purchase then no trustline needed
if (purchaseAmount >
(sleOwner->getFieldAmount(sfBalance) - ctx.tx[sfFee]))
if (purchaseAmount > (sleOwner->getFieldAmount(sfBalance) - ctx.tx[sfFee]))
return tecINSUFFICIENT_FUNDS;
}
// execution to here means it's an IOU sale
// check if the buyer has the right trustline with an adequate
// balance
// check if the buyer has the right trustline with an adequate balance
STAmount availableFunds{accountFunds(
ctx.view, acc, purchaseAmount, fhZERO_IF_FROZEN, ctx.j)};
ctx.view,
acc,
purchaseAmount,
fhZERO_IF_FROZEN,
ctx.j)};
if (purchaseAmount > availableFunds)
return tecINSUFFICIENT_FUNDS;
return tecINSUFFICIENT_FUNDS;
return tesSUCCESS;
}
case URIOperation::Clear: {
case ttURITOKEN_CANCEL_SELL_OFFER:
{
if (acc != *owner)
return tecNO_PERMISSION;
return tesSUCCESS;
}
case URIOperation::Sell: {
case ttURITOKEN_CREATE_SELL_OFFER:
{
if (acc != *owner)
return tecNO_PERMISSION;
@@ -271,14 +248,15 @@ URIToken::preclaim(PreclaimContext const& ctx)
{
AccountID const iouIssuer = saleAmount->getIssuer();
if (!ctx.view.exists(keylet::account(iouIssuer)))
return tecNO_ISSUER;
return tecNO_ISSUER;
}
return tesSUCCESS;
}
default: {
JLOG(ctx.j.warn()) << "URIToken txid=" << ctx.tx.getTransactionID()
<< " preclaim with URIOperation::Invalid\n";
default:
{
JLOG(ctx.j.warn())
<< "URIToken txid=" << ctx.tx.getTransactionID() << " preclaim with tt = " << tt << "\n";
return tecINTERNAL;
}
}
@@ -288,21 +266,23 @@ TER
URIToken::doApply()
{
auto j = ctx_.app.journal("View");
URIOperation op = inferOperation(ctx_.tx);
auto const sle = view().peek(keylet::account(account_));
if (!sle)
return tefINTERNAL;
if (op == URIOperation::Mint || op == URIOperation::Buy)
uint16_t tt = ctx_.tx.getFieldU16(sfTransactionType);
if (tt == ttURITOKEN_MINT || tt == ttURITOKEN_BUY)
{
STAmount const reserve{
view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
STAmount const reserve{view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)};
if (mPriorBalance - ctx_.tx.getFieldAmount(sfFee).xrp() < reserve)
return tecINSUFFICIENT_RESERVE;
}
uint32_t flags = ctx_.tx.getFlags();
std::shared_ptr<SLE> sleU;
std::optional<AccountID> issuer;
std::optional<AccountID> owner;
@@ -311,10 +291,10 @@ URIToken::doApply()
std::optional<Keylet> kl;
std::shared_ptr<SLE> sleOwner;
if (op != URIOperation::Mint)
if (tt != ttURITOKEN_MINT)
{
kl = Keylet{ltURI_TOKEN, ctx_.tx.getFieldH256(sfURITokenID)};
sleU = view().peek(*kl);
kl = Keylet {ltURI_TOKEN, ctx_.tx.getFieldH256(sfURITokenID)};
sleU = view().peek(*kl);
if (!sleU)
return tecNO_ENTRY;
@@ -334,15 +314,17 @@ URIToken::doApply()
if (!sleOwner)
{
JLOG(j.warn()) << "Malformed transaction: owner of URIToken is not "
"in the ledger.";
JLOG(j.warn())
<< "Malformed transaction: owner of URIToken is not in the ledger.";
return tecNO_ENTRY;
}
}
switch (op)
switch (tt)
{
case URIOperation::Mint: {
case ttURITOKEN_MINT:
{
kl = keylet::uritoken(account_, ctx_.tx.getFieldVL(sfURI));
if (view().exists(*kl))
return tecDUPLICATE;
@@ -352,18 +334,21 @@ URIToken::doApply()
sleU->setAccountID(sfIssuer, account_);
sleU->setFieldVL(sfURI, ctx_.tx.getFieldVL(sfURI));
if (ctx_.tx.getFlags() & tfBurnable)
sleU->setFlag(tfBurnable);
if (ctx_.tx.isFieldPresent(sfDigest))
sleU->setFieldH256(sfDigest, ctx_.tx.getFieldH256(sfDigest));
if (flags & tfBurnable)
sleU->setFlag(tfBurnable);
auto const page = view().dirInsert(
keylet::ownerDir(account_), *kl, describeOwnerDir(account_));
keylet::ownerDir(account_),
*kl,
describeOwnerDir(account_));
JLOG(j_.trace())
<< "Adding URIToken to owner directory " << to_string(kl->key)
<< ": " << (page ? "success" : "failure");
<< "Adding URIToken to owner directory "
<< to_string(kl->key) << ": "
<< (page ? "success" : "failure");
if (!page)
return tecDIR_FULL;
@@ -375,7 +360,8 @@ URIToken::doApply()
return tesSUCCESS;
}
case URIOperation::Clear: {
case ttURITOKEN_CANCEL_SELL_OFFER:
{
sleU->makeFieldAbsent(sfAmount);
if (sleU->isFieldPresent(sfDestination))
sleU->makeFieldAbsent(sfDestination);
@@ -383,7 +369,8 @@ URIToken::doApply()
return tesSUCCESS;
}
case URIOperation::Buy: {
case ttURITOKEN_BUY:
{
if (account_ == *owner)
{
// this is a clear operation
@@ -439,67 +426,65 @@ URIToken::doApply()
else
{
// IOU sale
STAmount availableFunds{accountFunds(
view(), account_, purchaseAmount, fhZERO_IF_FROZEN, j)};
view(),
account_,
purchaseAmount,
fhZERO_IF_FROZEN,
j)};
if (purchaseAmount > availableFunds)
return tecINSUFFICIENT_FUNDS;
// check if the seller has a line
tlSeller = keylet::line(
*owner,
purchaseAmount.getIssuer(),
purchaseAmount.getCurrency());
Keylet tlBuyer = keylet::line(
account_,
purchaseAmount.getIssuer(),
purchaseAmount.getCurrency());
tlSeller =
keylet::line(*owner, purchaseAmount.getIssuer(), purchaseAmount.getCurrency());
Keylet tlBuyer =
keylet::line(account_, purchaseAmount.getIssuer(), purchaseAmount.getCurrency());
sleDstLine = view().peek(*tlSeller);
sleSrcLine = view().peek(tlBuyer);
if (!sleDstLine)
{
// they do not, so we can create one if they have sufficient
// reserve
// they do not, so we can create one if they have sufficient reserve
if (std::uint32_t const ownerCount = {sleOwner->at(
sfOwnerCount)};
(*sleOwner)[sfBalance] <
view().fees().accountReserve(ownerCount + 1))
if (std::uint32_t const ownerCount = {sleOwner->at(sfOwnerCount)};
(*sleOwner)[sfBalance] < view().fees().accountReserve(ownerCount + 1))
{
JLOG(j_.trace())
<< "Trust line does not exist. "
"Insufficent reserve to create line.";
JLOG(j_.trace()) << "Trust line does not exist. "
"Insufficent reserve to create line.";
return tecNO_LINE_INSUF_RESERVE;
}
}
// remove from buyer
initBuyerBal = buyerLow ? ((*sleSrcLine)[sfBalance])
: -((*sleSrcLine)[sfBalance]);
initBuyerBal = buyerLow ? (*sleSrcLine)[sfBalance] : -(*sleSrcLine)[sfBalance];
finBuyerBal = *initBuyerBal - purchaseAmount;
// compute amount to deliver
static Rate const parityRate(QUALITY_ONE);
auto xferRate = transferRate(view(), saleAmount->getIssuer());
dstAmt = xferRate == parityRate ? purchaseAmount
: multiplyRound(
purchaseAmount,
xferRate,
purchaseAmount.issue(),
true);
dstAmt =
xferRate == parityRate
? purchaseAmount
: multiplyRound(purchaseAmount, xferRate, purchaseAmount.issue(), true);
initSellerBal = !sleDstLine ? purchaseAmount.zeroed()
: sellerLow ? ((*sleDstLine)[sfBalance])
: -((*sleDstLine)[sfBalance]);
if (!sellerLow)
dstAmt->negate();
initSellerBal = !sleDstLine
? purchaseAmount.zeroed()
: (sellerLow ? (*sleDstLine)[sfBalance] : -(*sleDstLine)[sfBalance]);
finSellerBal = *initSellerBal + *dstAmt;
}
// sanity check balance mutations (xrp or iou, both are checked the
// same way now)
// sanity check balance mutations (xrp or iou, both are checked the same way now)
if (*finSellerBal < *initSellerBal)
{
JLOG(j.warn())
@@ -533,17 +518,18 @@ URIToken::doApply()
}
// to this point no ledger changes have been made
// make them in a sensible order such that failure doesn't require
// cleanup
// make them in a sensible order such that failure doesn't require cleanup
// add to new owner's directory first, this can fail if they have
// too many objects
// add to new owner's directory first, this can fail if they have too many objects
auto const newPage = view().dirInsert(
keylet::ownerDir(account_), *kl, describeOwnerDir(account_));
keylet::ownerDir(account_),
*kl,
describeOwnerDir(account_));
JLOG(j_.trace())
<< "Adding URIToken to owner directory " << to_string(kl->key)
<< ": " << (newPage ? "success" : "failure");
<< "Adding URIToken to owner directory "
<< to_string(kl->key) << ": "
<< (newPage ? "success" : "failure");
if (!newPage)
{
@@ -552,9 +538,8 @@ URIToken::doApply()
return tecDIR_FULL;
}
// Next create destination trustline where applicable. This could
// fail for a variety of reasons. If it does fail we need to remove
// the dir entry we just added to the buyer before we leave.
// Next create destination trustline where applicable. This could fail for a variety of reasons.
// If it does fail we need to remove the dir entry we just added to the buyer before we leave.
bool lineCreated = false;
if (!isXRP(purchaseAmount) && !sleDstLine)
{
@@ -595,23 +580,17 @@ URIToken::doApply()
lineCreated = true;
}
// execution to here means we added the URIToken to the buyer's
// directory and we definitely have a way to send the funds to the
// seller.
// execution to here means we added the URIToken to the buyer's directory
// and we definitely have a way to send the funds to the seller.
// remove from current owner directory
if (!view().dirRemove(
keylet::ownerDir(*owner),
sleU->getFieldU64(sfOwnerNode),
kl->key,
true))
if (!view().dirRemove(keylet::ownerDir(*owner), sleU->getFieldU64(sfOwnerNode), kl->key, true))
{
JLOG(j.fatal())
<< "Could not remove URIToken from owner directory";
// remove the newly inserted directory entry before we leave
if (!view().dirRemove(
keylet::ownerDir(account_), *newPage, kl->key, true))
if (!view().dirRemove(keylet::ownerDir(account_), *newPage, kl->key, true))
{
JLOG(j.fatal())
<< "Could not remove URIToken from owner directory (2)";
@@ -628,12 +607,10 @@ URIToken::doApply()
return tefBAD_LEDGER;
}
// above is all the things that could fail. we now have swapped the
// ownership as far as the ownerdirs are concerned, and we have a
// place to pay to and from.
// above is all the things that could fail. we now have swapped the ownership as far as the ownerdirs
// are concerned, and we have a place to pay to and from.
// if a trustline was created then the ownercount stays the same on
// the seller +1 TL -1 URIToken
// if a trustline was created then the ownercount stays the same on the seller +1 TL -1 URIToken
if (!lineCreated)
adjustOwnerCount(view(), sleOwner, -1, j);
@@ -651,6 +628,7 @@ URIToken::doApply()
// tell the ledger where to find it
sleU->setFieldU64(sfOwnerNode, *newPage);
// update the buyer's balance
if (isXRP(purchaseAmount))
{
@@ -659,10 +637,8 @@ URIToken::doApply()
}
else if (sleSrcLine)
{
// update the buyer's line to reflect the reduction of the
// purchase price
sleSrcLine->setFieldAmount(
sfBalance, buyerLow ? *finBuyerBal : -(*finBuyerBal));
// update the buyer's line to reflect the reduction of the purchase price
sleSrcLine->setFieldAmount(sfBalance, buyerLow ? *finBuyerBal : -(*finBuyerBal));
}
else
return tecINTERNAL;
@@ -676,8 +652,7 @@ URIToken::doApply()
else if (sleDstLine)
{
// the line already existed on the seller side so update it
sleDstLine->setFieldAmount(
sfBalance, sellerLow ? *finSellerBal : -(*finSellerBal));
sleDstLine->setFieldAmount(sfBalance, sellerLow ? *finSellerBal : -(*finSellerBal));
}
else if (lineCreated)
{
@@ -686,6 +661,7 @@ URIToken::doApply()
else
return tecINTERNAL;
if (sleSrcLine)
view().update(sleSrcLine);
if (sleDstLine)
@@ -693,19 +669,19 @@ URIToken::doApply()
view().update(sleU);
view().update(sleOwner);
return tesSUCCESS;
}
case URIOperation::Burn: {
case ttURITOKEN_BURN:
{
if (sleU->getAccountID(sfOwner) == account_)
{
// pass, owner may always delete own object
}
else if (
sleU->getAccountID(sfIssuer) == account_ &&
(sleU->getFlags() & tfBurnable))
else if (sleU->getAccountID(sfIssuer) == account_ && (sleU->getFlags() & tfBurnable))
{
// pass, issuer may burn if the tfBurnable flag was set during
// minting
// pass, issuer may burn if the tfBurnable flag was set during minting
}
else
return tecNO_PERMISSION;
@@ -713,8 +689,7 @@ URIToken::doApply()
// execution to here means there is permission to burn
auto const page = (*sleU)[sfOwnerNode];
if (!view().dirRemove(
keylet::ownerDir(*owner), page, kl->key, true))
if (!view().dirRemove(keylet::ownerDir(*owner), page, kl->key, true))
{
JLOG(j.fatal())
<< "Could not remove URIToken from owner directory";
@@ -726,7 +701,8 @@ URIToken::doApply()
return tesSUCCESS;
}
case URIOperation::Sell: {
case ttURITOKEN_CREATE_SELL_OFFER:
{
if (account_ != *owner)
return tecNO_PERMISSION;
@@ -741,11 +717,13 @@ URIToken::doApply()
sleU->setFieldAmount(sfAmount, ctx_.tx[sfAmount]);
view().update(sleU);
std::cout << "sleU on sell: " << (*sleU) << "\n";
return tesSUCCESS;
}
default:
return tecINTERNAL;
}
}

View File

@@ -27,18 +27,10 @@
namespace ripple {
enum struct URIOperation : uint8_t {
Invalid = 0,
Mint = 1,
Burn = 2,
Buy = 3,
Sell = 4,
Clear = 5
};
class URIToken : public Transactor
{
public:
static constexpr ConsequencesFactoryType ConsequencesFactory{Normal};
explicit URIToken(ApplyContext& ctx) : Transactor(ctx)

View File

@@ -157,7 +157,11 @@ invoke_preflight(PreflightContext const& ctx)
return invoke_preflight_helper<ClaimReward>(ctx);
case ttINVOKE:
return invoke_preflight_helper<Invoke>(ctx);
case ttURI_TOKEN:
case ttURITOKEN_MINT:
case ttURITOKEN_BURN:
case ttURITOKEN_BUY:
case ttURITOKEN_CREATE_SELL_OFFER:
case ttURITOKEN_CANCEL_SELL_OFFER:
return invoke_preflight_helper<URIToken>(ctx);
default:
assert(false);
@@ -268,7 +272,11 @@ invoke_preclaim(PreclaimContext const& ctx)
return invoke_preclaim<ClaimReward>(ctx);
case ttINVOKE:
return invoke_preclaim<Invoke>(ctx);
case ttURI_TOKEN:
case ttURITOKEN_MINT:
case ttURITOKEN_BURN:
case ttURITOKEN_BUY:
case ttURITOKEN_CREATE_SELL_OFFER:
case ttURITOKEN_CANCEL_SELL_OFFER:
return invoke_preclaim<URIToken>(ctx);
default:
assert(false);
@@ -340,7 +348,11 @@ invoke_calculateBaseFee(ReadView const& view, STTx const& tx)
return ClaimReward::calculateBaseFee(view, tx);
case ttINVOKE:
return Invoke::calculateBaseFee(view, tx);
case ttURI_TOKEN:
case ttURITOKEN_MINT:
case ttURITOKEN_BURN:
case ttURITOKEN_BUY:
case ttURITOKEN_CREATE_SELL_OFFER:
case ttURITOKEN_CANCEL_SELL_OFFER:
return URIToken::calculateBaseFee(view, tx);
default:
assert(false);
@@ -508,7 +520,11 @@ invoke_apply(ApplyContext& ctx)
Invoke p(ctx);
return p();
}
case ttURI_TOKEN: {
case ttURITOKEN_MINT:
case ttURITOKEN_BURN:
case ttURITOKEN_BUY:
case ttURITOKEN_CREATE_SELL_OFFER:
case ttURITOKEN_CANCEL_SELL_OFFER: {
URIToken p(ctx);
return p();
}

View File

@@ -129,9 +129,6 @@ constexpr std::uint32_t const tfStrongTSH = 0x00008000;
constexpr std::uint32_t const tfNFTokenMintOldMask =
~(tfUniversal | tfBurnable | tfOnlyXRP | tfTrustLine | tfTransferable | tfStrongTSH);
// URIToken flags:
constexpr std::uint32_t const tfBurn = 0x00000002;
// Prior to fixRemoveNFTokenAutoTrustLine, transfer of an NFToken between
// accounts allowed a TrustLine to be added to the issuer of that token
// without explicit permission from that issuer. This was enabled by
@@ -160,6 +157,10 @@ constexpr std::uint32_t const tfNFTokenCancelOfferMask = ~(tfUniversal);
// NFTokenAcceptOffer flags:
constexpr std::uint32_t const tfNFTokenAcceptOfferMask = ~tfUniversal;
// URIToken mask
constexpr std::uint32_t const tfURITokenMintMask = ~(tfUniversal | tfBurnable);
constexpr std::uint32_t const tfURITokenNonMintMask = ~tfUniversal;
// clang-format on
} // namespace ripple

View File

@@ -140,7 +140,11 @@ enum TxType : std::uint16_t
ttNFTOKEN_ACCEPT_OFFER = 29,
/** This transaction mints/burns/buys/sells a URI TOKEN */
ttURI_TOKEN = 45,
ttURITOKEN_MINT = 45,
ttURITOKEN_BURN = 46,
ttURITOKEN_BUY = 47,
ttURITOKEN_CREATE_SELL_OFFER = 48,
ttURITOKEN_CANCEL_SELL_OFFER = 49,
/** This transaction resets accumulator/counters and claims a reward for holding an average balance
* from a specified hook */

View File

@@ -362,13 +362,43 @@ TxFormats::TxFormats()
},
commonFields);
add(jss::URIToken,
ttURI_TOKEN,
{{sfURI, soeOPTIONAL},
{sfURITokenID, soeOPTIONAL},
{sfAmount, soeOPTIONAL},
{sfDigest, soeOPTIONAL},
{sfDestination, soeOPTIONAL}},
add(jss::URITokenMint,
ttURITOKEN_MINT,
{
{sfURI, soeREQUIRED},
{sfDigest, soeOPTIONAL},
},
commonFields);
add(jss::URITokenBurn,
ttURITOKEN_BURN,
{
{sfURITokenID, soeREQUIRED},
},
commonFields);
add(jss::URITokenBuy,
ttURITOKEN_BUY,
{
{sfURITokenID, soeREQUIRED},
{sfAmount, soeREQUIRED},
},
commonFields);
add(jss::URITokenCreateSellOffer,
ttURITOKEN_CREATE_SELL_OFFER,
{
{sfURITokenID, soeREQUIRED},
{sfAmount, soeREQUIRED},
{sfDestination, soeOPTIONAL},
},
commonFields);
add(jss::URITokenCancelSellOffer,
ttURITOKEN_CANCEL_SELL_OFFER,
{
{sfURITokenID, soeREQUIRED},
},
commonFields);
}

View File

@@ -665,6 +665,11 @@ JSS(unlimited); // out: Connection.h
JSS(uptime); // out: GetCounts
JSS(uri); // out: ValidatorSites
JSS(URIToken); // LedgerEntry
JSS(URITokenMint); // tx type
JSS(URITokenBurn); // tx type
JSS(URITokenBuy); // tx type
JSS(URITokenCreateSellOffer); // tx type
JSS(URITokenCancelSellOffer); // tx type
JSS(url); // in/out: Subscribe, Unsubscribe
JSS(url_password); // in: Subscribe
JSS(url_username); // in: Subscribe

View File

@@ -32,6 +32,7 @@ namespace ripple {
namespace test {
struct URIToken_test : public beast::unit_test::suite
{
/*
static uint256
tokenid(jtx::Account const& account, std::string const& uri)
{
@@ -1435,8 +1436,10 @@ public:
auto const sa = supported_amendments();
testWithFeats(sa);
}
*/
};
BEAST_DEFINE_TESTSUITE(URIToken, app, ripple);
//BEAST_DEFINE_TESTSUITE(URIToken, app, ripple);
} // namespace test
} // namespace ripple