Skip to content

Commit

Permalink
MPT code with no direct pay and hacked STAmount
Browse files Browse the repository at this point in the history
Co-Authored-By: Gregory Tsipenyuk <[email protected]>
  • Loading branch information
shawnxie999 and gregtatcam committed Feb 25, 2024
1 parent d7d15a9 commit b91ed89
Show file tree
Hide file tree
Showing 59 changed files with 3,814 additions and 170 deletions.
7 changes: 7 additions & 0 deletions Builds/CMake/RippledCore.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ target_sources (rippled PRIVATE
src/ripple/app/tx/impl/AMMWithdraw.cpp
src/ripple/app/tx/impl/ApplyContext.cpp
src/ripple/app/tx/impl/BookTip.cpp
src/ripple/app/tx/impl/MPTokenIssuanceCreate.cpp
src/ripple/app/tx/impl/MPTokenIssuanceDestroy.cpp
src/ripple/app/tx/impl/MPTokenAuthorize.cpp
src/ripple/app/tx/impl/MPTokenIssuanceSet.cpp
src/ripple/app/tx/impl/CancelCheck.cpp
src/ripple/app/tx/impl/CancelOffer.cpp
src/ripple/app/tx/impl/CashCheck.cpp
Expand Down Expand Up @@ -765,6 +769,7 @@ target_sources (rippled PRIVATE
src/ripple/rpc/handlers/ValidatorListSites.cpp
src/ripple/rpc/handlers/Validators.cpp
src/ripple/rpc/handlers/WalletPropose.cpp
src/ripple/rpc/impl/MPTokenIssuanceID.cpp
src/ripple/rpc/impl/DeliveredAmount.cpp
src/ripple/rpc/impl/Handler.cpp
src/ripple/rpc/impl/LegacyPathFind.cpp
Expand Down Expand Up @@ -814,6 +819,7 @@ if (tests)
src/test/app/AMM_test.cpp
src/test/app/AMMCalc_test.cpp
src/test/app/AMMExtended_test.cpp
src/test/app/MPToken_test.cpp
src/test/app/Check_test.cpp
src/test/app/Clawback_test.cpp
src/test/app/CrossingLimits_test.cpp
Expand Down Expand Up @@ -962,6 +968,7 @@ if (tests)
src/test/jtx/impl/Account.cpp
src/test/jtx/impl/AMM.cpp
src/test/jtx/impl/AMMTest.cpp
src/test/jtx/impl/mpt.cpp
src/test/jtx/impl/Env.cpp
src/test/jtx/impl/JSONRPCClient.cpp
src/test/jtx/impl/TestHelpers.cpp
Expand Down
13 changes: 13 additions & 0 deletions src/ripple/app/ledger/impl/LedgerToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ripple/protocol/jss.h>
#include <ripple/rpc/Context.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/MPTokenIssuanceID.h>
#include <ripple/rpc/impl/RPCHelpers.h>

namespace ripple {
Expand Down Expand Up @@ -157,6 +158,12 @@ fillJsonTx(
fill.ledger,
txn,
{txn->getTransactionID(), fill.ledger.seq(), *stMeta});

// If applicable, insert mpt issuance id
RPC::insertMPTokenIssuanceID(
txJson[jss::meta],
txn,
{txn->getTransactionID(), fill.ledger.seq(), *stMeta});
}

if (!fill.ledger.open())
Expand Down Expand Up @@ -190,6 +197,12 @@ fillJsonTx(
fill.ledger,
txn,
{txn->getTransactionID(), fill.ledger.seq(), *stMeta});

// If applicable, insert mpt issuance id
RPC::insertMPTokenIssuanceID(
txJson[jss::metaData],
txn,
{txn->getTransactionID(), fill.ledger.seq(), *stMeta});
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/ripple/app/misc/NetworkOPs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include <ripple/resource/ResourceManager.h>
#include <ripple/rpc/BookChanges.h>
#include <ripple/rpc/DeliveredAmount.h>
#include <ripple/rpc/MPTokenIssuanceID.h>
#include <ripple/rpc/ServerHandler.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <boost/asio/ip/host_name.hpp>
Expand Down Expand Up @@ -3120,6 +3121,8 @@ NetworkOPsImp::transJson(
jvObj[jss::meta] = meta->get().getJson(JsonOptions::none);
RPC::insertDeliveredAmount(
jvObj[jss::meta], *ledger, transaction, meta->get());
RPC::insertMPTokenIssuanceID(
jvObj[jss::meta], transaction, meta->get());
}

if (!ledger->open())
Expand Down
168 changes: 168 additions & 0 deletions src/ripple/app/tx/impl/InvariantCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ LedgerEntryTypesMatch::visitEntry(
case ltXCHAIN_OWNED_CLAIM_ID:
case ltXCHAIN_OWNED_CREATE_ACCOUNT_CLAIM_ID:
case ltDID:
case ltMPTOKEN_ISSUANCE:
case ltMPTOKEN:
break;
default:
invalidTypeAdded_ = true;
Expand Down Expand Up @@ -799,4 +801,170 @@ ValidClawback::finalize(
return true;
}

//------------------------------------------------------------------------------

void
ValidMPTIssuance::visitEntry(
bool isDelete,
std::shared_ptr<SLE const> const& before,
std::shared_ptr<SLE const> const& after)
{
if (after && after->getType() == ltMPTOKEN_ISSUANCE)
{
if (isDelete)
mptIssuancesDeleted_++;
else if (!before)
mptIssuancesCreated_++;
}

if (after && after->getType() == ltMPTOKEN)
{
if (isDelete)
mptokensDeleted_++;
else if (!before)
mptokensCreated_++;
}
}

bool
ValidMPTIssuance::finalize(
STTx const& tx,
TER const result,
XRPAmount const _fee,
ReadView const& _view,
beast::Journal const& j)
{
if (result == tesSUCCESS)
{
if (tx.getTxnType() == ttMPTOKEN_ISSUANCE_CREATE)
{
if (mptIssuancesCreated_ == 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance creation "
"succeeded without creating a MPT issuance";
}
else if (mptIssuancesDeleted_ != 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance creation "
"succeeded while removing MPT issuances";
}
else if (mptIssuancesCreated_ > 1)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance creation "
"succeeded but created multiple issuances";
}

return mptIssuancesCreated_ == 1 && mptIssuancesDeleted_ == 0;
}

if (tx.getTxnType() == ttMPTOKEN_ISSUANCE_DESTROY)
{
if (mptIssuancesDeleted_ == 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance deletion "
"succeeded without removing a MPT issuance";
}
else if (mptIssuancesCreated_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance deletion "
"succeeded while creating MPT issuances";
}
else if (mptIssuancesDeleted_ > 1)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance deletion "
"succeeded but deleted multiple issuances";
}

return mptIssuancesCreated_ == 0 && mptIssuancesDeleted_ == 1;
}

if (tx.getTxnType() == ttMPTOKEN_AUTHORIZE)
{
bool const submittedByIssuer = tx.isFieldPresent(sfMPTokenHolder);

if (mptIssuancesCreated_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"succeeded but created MPT issuances";
return false;
}
else if (mptIssuancesDeleted_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT authorize "
"succeeded but deleted issuances";
return false;
}
else if (
submittedByIssuer &&
(mptokensCreated_ > 0 || mptokensDeleted_ > 0))
{
JLOG(j.fatal())
<< "Invariant failed: MPT authorize submitted by issuer "
"succeeded but created/deleted mptokens";
return false;
}
else if (
!submittedByIssuer &&
(mptokensCreated_ + mptokensDeleted_ != 1))
{
// if the holder submitted this tx, then a mptoken must be
// either created or deleted.
JLOG(j.fatal())
<< "Invariant failed: MPT authorize submitted by holder "
"succeeded but created/deleted bad number of mptokens";
return false;
}

return true;
}

if (tx.getTxnType() == ttMPTOKEN_ISSUANCE_SET)
{
if (mptIssuancesDeleted_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance set "
"succeeded while removing MPT issuances";
}
else if (mptIssuancesCreated_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance set "
"succeeded while creating MPT issuances";
}
else if (mptokensDeleted_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance set "
"succeeded while removing MPTokens";
}
else if (mptokensCreated_ > 0)
{
JLOG(j.fatal()) << "Invariant failed: MPT issuance set "
"succeeded while creating MPTokens";
}

return mptIssuancesCreated_ == 0 && mptIssuancesDeleted_ == 0 &&
mptokensCreated_ == 0 && mptokensDeleted_ == 0;
}
}

if (mptIssuancesCreated_ != 0)
{
JLOG(j.fatal()) << "Invariant failed: a MPT issuance was created";
}
else if (mptIssuancesDeleted_ != 0)
{
JLOG(j.fatal()) << "Invariant failed: a MPT issuance was deleted";
}
else if (mptokensCreated_ != 0)
{
JLOG(j.fatal()) << "Invariant failed: a MPToken was created";
}
else if (mptokensDeleted_ != 0)
{
JLOG(j.fatal()) << "Invariant failed: a MPToken was deleted";
}

return mptIssuancesCreated_ == 0 && mptIssuancesDeleted_ == 0 &&
mptokensCreated_ == 0 && mptokensDeleted_ == 0;
}

} // namespace ripple
27 changes: 26 additions & 1 deletion src/ripple/app/tx/impl/InvariantCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,30 @@ class ValidClawback
beast::Journal const&);
};

class ValidMPTIssuance
{
std::uint32_t mptIssuancesCreated_ = 0;
std::uint32_t mptIssuancesDeleted_ = 0;

std::uint32_t mptokensCreated_ = 0;
std::uint32_t mptokensDeleted_ = 0;

public:
void
visitEntry(
bool,
std::shared_ptr<SLE const> const&,
std::shared_ptr<SLE const> const&);

bool
finalize(
STTx const&,
TER const,
XRPAmount const,
ReadView const&,
beast::Journal const&);
};

// additional invariant checks can be declared above and then added to this
// tuple
using InvariantChecks = std::tuple<
Expand All @@ -432,7 +456,8 @@ using InvariantChecks = std::tuple<
ValidNewAccountRoot,
ValidNFTokenPage,
NFTokenCountTracking,
ValidClawback>;
ValidClawback,
ValidMPTIssuance>;

/**
* @brief get a tuple of all invariant checks
Expand Down
Loading

0 comments on commit b91ed89

Please sign in to comment.