Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cap 0027: first class multiplexed accounts #2498

Merged
merged 2 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ TEST_FILES = $(TESTDATA_DIR)/stellar-core_example.cfg $(TESTDATA_DIR)/stellar-co

BUILT_SOURCES = $(SRC_X_FILES:.x=.h) main/StellarCoreVersion.cpp $(TEST_FILES)

$(SRC_X_FILES:.x=.h): $(XDRC)
SUFFIXES = .x .h
.x.h: $(XDRC)
.x.h:
$(XDRC) -hh -o $@ $<

$(srcdir)/src.mk: $(top_srcdir)/make-mks
Expand Down
4 changes: 2 additions & 2 deletions src/main/test/CommandHandlerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST_CASE("transaction envelope bridge", "[commandhandler]")
auto root = TestAccount::createRoot(*app);

Transaction tx;
tx.sourceAccount = root;
tx.sourceAccount = toMuxedAccount(root);
tx.fee = baseFee;
tx.seqNum = root.nextSequenceNumber();
tx.operations.emplace_back(payment(root, 1));
Expand Down Expand Up @@ -87,7 +87,7 @@ TEST_CASE("transaction envelope bridge", "[commandhandler]")

TransactionEnvelope env(ENVELOPE_TYPE_TX);
auto& tx = env.v1().tx;
tx.sourceAccount = root;
tx.sourceAccount = toMuxedAccount(root);
tx.fee = baseFee;
tx.seqNum = root.nextSequenceNumber();
tx.operations.emplace_back(payment(root, 1));
Expand Down
2 changes: 1 addition & 1 deletion src/test/TestAccount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TestAccount::tx(std::vector<Operation> const& ops, SequenceNumber sn)
Operation
TestAccount::op(Operation operation)
{
operation.sourceAccount.activate() = getPublicKey();
operation.sourceAccount.activate() = toMuxedAccount(getPublicKey());
return operation;
}

Expand Down
28 changes: 22 additions & 6 deletions src/test/TxTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,22 @@ getAccountSigners(PublicKey const& k, Application& app)
return account.current().data.account().signers;
}

MuxedAccount
toMuxedAccount(AccountID const& a)
{
MuxedAccount ret(static_cast<CryptoKeyType>(a.type()));
switch (a.type())
{
case PUBLIC_KEY_TYPE_ED25519:
ret.ed25519() = a.ed25519();
break;
default:
// this would be a bug
abort();
}
return ret;
}

TransactionFramePtr
transactionFromOperationsV0(Application& app, SecretKey const& from,
SequenceNumber seq,
Expand Down Expand Up @@ -445,7 +461,7 @@ transactionFromOperationsV1(Application& app, SecretKey const& from,
const std::vector<Operation>& ops, int fee)
{
TransactionEnvelope e(ENVELOPE_TYPE_TX);
e.v1().tx.sourceAccount = from.getPublicKey();
e.v1().tx.sourceAccount = toMuxedAccount(from.getPublicKey());
e.v1().tx.fee =
fee != 0 ? fee
: static_cast<uint32_t>(
Expand Down Expand Up @@ -520,7 +536,7 @@ payment(PublicKey const& to, int64_t amount)
Operation op;
op.body.type(PAYMENT);
op.body.paymentOp().amount = amount;
op.body.paymentOp().destination = to;
op.body.paymentOp().destination = toMuxedAccount(to);
op.body.paymentOp().asset.type(ASSET_TYPE_NATIVE);
return op;
}
Expand All @@ -531,7 +547,7 @@ payment(PublicKey const& to, Asset const& asset, int64_t amount)
Operation op;
op.body.type(PAYMENT);
op.body.paymentOp().amount = amount;
op.body.paymentOp().destination = to;
op.body.paymentOp().destination = toMuxedAccount(to);
op.body.paymentOp().asset = asset;
return op;
}
Expand Down Expand Up @@ -590,7 +606,7 @@ pathPayment(PublicKey const& to, Asset const& sendCur, int64_t sendMax,
ppop.sendMax = sendMax;
ppop.destAsset = destCur;
ppop.destAmount = destAmount;
ppop.destination = to;
ppop.destination = toMuxedAccount(to);
std::copy(std::begin(path), std::end(path), std::back_inserter(ppop.path));

return op;
Expand All @@ -608,7 +624,7 @@ pathPaymentStrictSend(PublicKey const& to, Asset const& sendCur,
ppop.sendAmount = sendAmount;
ppop.destAsset = destCur;
ppop.destMin = destMin;
ppop.destination = to;
ppop.destination = toMuxedAccount(to);
std::copy(std::begin(path), std::end(path), std::back_inserter(ppop.path));

return op;
Expand Down Expand Up @@ -1036,7 +1052,7 @@ accountMerge(PublicKey const& dest)
{
Operation op;
op.body.type(ACCOUNT_MERGE);
op.body.destination() = dest;
op.body.destination() = toMuxedAccount(dest);
return op;
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/TxTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ bool doesAccountExist(Application& app, PublicKey const& k);
xdr::xvector<Signer, 20> getAccountSigners(PublicKey const& k,
Application& app);

MuxedAccount toMuxedAccount(AccountID const& a);

TransactionFramePtr
transactionFromOperationsV0(Application& app, SecretKey const& from,
SequenceNumber seq,
Expand Down
4 changes: 2 additions & 2 deletions src/transactions/MergeOpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ MergeOpFrame::doApply(AbstractLedgerTxn& ltx)
auto header = ltx.loadHeader();

auto otherAccount =
stellar::loadAccount(ltx, mOperation.body.destination());
stellar::loadAccount(ltx, toAccountID(mOperation.body.destination()));
if (!otherAccount)
{
innerResult().code(ACCOUNT_MERGE_NO_ACCOUNT);
Expand Down Expand Up @@ -125,7 +125,7 @@ bool
MergeOpFrame::doCheckValid(uint32_t ledgerVersion)
{
// makes sure not merging into self
if (getSourceID() == mOperation.body.destination())
if (getSourceID() == toAccountID(mOperation.body.destination()))
{
innerResult().code(ACCOUNT_MERGE_MALFORMED);
return false;
Expand Down
7 changes: 4 additions & 3 deletions src/transactions/OperationFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "transactions/PaymentOpFrame.h"
#include "transactions/SetOptionsOpFrame.h"
#include "transactions/TransactionFrame.h"
#include "transactions/TransactionUtils.h"
#include "util/Logging.h"

#include <xdrpp/printer.h>
Expand Down Expand Up @@ -150,8 +151,8 @@ OperationFrame::checkSignature(SignatureChecker& signatureChecker,
return false;
}

if (!mParentTx.checkSignatureNoAccount(signatureChecker,
*mOperation.sourceAccount))
if (!mParentTx.checkSignatureNoAccount(
signatureChecker, toAccountID(*mOperation.sourceAccount)))
{
mResult.code(opBAD_AUTH);
return false;
Expand All @@ -164,7 +165,7 @@ OperationFrame::checkSignature(SignatureChecker& signatureChecker,
AccountID
OperationFrame::getSourceID() const
{
return mOperation.sourceAccount ? *mOperation.sourceAccount
return mOperation.sourceAccount ? toAccountID(*mOperation.sourceAccount)
: mParentTx.getSourceID();
}

Expand Down
16 changes: 12 additions & 4 deletions src/transactions/PathPaymentOpFrameBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ PathPaymentOpFrameBase::PathPaymentOpFrameBase(Operation const& op,
{
}

AccountID
PathPaymentOpFrameBase::getDestID() const
{
return toAccountID(getDestMuxedAccount());
}

void
PathPaymentOpFrameBase::insertLedgerKeysToPrefetch(
std::unordered_set<LedgerKey>& keys) const
{
keys.emplace(accountKey(getDestID()));
auto destID = getDestID();
keys.emplace(accountKey(destID));

if (getDestAsset().type() != ASSET_TYPE_NATIVE)
{
keys.emplace(trustlineKey(getDestID(), getDestAsset()));
keys.emplace(trustlineKey(destID, getDestAsset()));
}
if (getSourceAsset().type() != ASSET_TYPE_NATIVE)
{
Expand Down Expand Up @@ -194,11 +201,12 @@ PathPaymentOpFrameBase::updateDestBalance(AbstractLedgerTxn& ltx,
int64_t amount,
bool bypassIssuerCheck)
{
auto destID = getDestID();
auto const& asset = getDestAsset();

if (asset.type() == ASSET_TYPE_NATIVE)
{
auto destination = stellar::loadAccount(ltx, getDestID());
auto destination = stellar::loadAccount(ltx, destID);
if (!addBalance(ltx.loadHeader(), destination, amount))
{
if (ltx.loadHeader().current().ledgerVersion >= 11)
Expand All @@ -219,7 +227,7 @@ PathPaymentOpFrameBase::updateDestBalance(AbstractLedgerTxn& ltx,
return false;
}

auto destLine = stellar::loadTrustLine(ltx, getDestID(), asset);
auto destLine = stellar::loadTrustLine(ltx, destID, asset);
if (!destLine)
{
setResultDestNoTrust();
Expand Down
3 changes: 2 additions & 1 deletion src/transactions/PathPaymentOpFrameBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class PathPaymentOpFrameBase : public OperationFrame

virtual Asset const& getSourceAsset() const = 0;
virtual Asset const& getDestAsset() const = 0;
virtual AccountID const& getDestID() const = 0;
AccountID getDestID() const;
virtual MuxedAccount const& getDestMuxedAccount() const = 0;
virtual xdr::xvector<Asset, 5> const& getPath() const = 0;

virtual void setResultSuccess() = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/transactions/PathPaymentStrictReceiveOpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ PathPaymentStrictReceiveOpFrame::getDestAsset() const
return mPathPayment.destAsset;
}

AccountID const&
PathPaymentStrictReceiveOpFrame::getDestID() const
MuxedAccount const&
PathPaymentStrictReceiveOpFrame::getDestMuxedAccount() const
{
return mPathPayment.destination;
}
Expand Down
2 changes: 1 addition & 1 deletion src/transactions/PathPaymentStrictReceiveOpFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PathPaymentStrictReceiveOpFrame : public PathPaymentOpFrameBase

Asset const& getSourceAsset() const override;
Asset const& getDestAsset() const override;
AccountID const& getDestID() const override;
MuxedAccount const& getDestMuxedAccount() const override;
xdr::xvector<Asset, 5> const& getPath() const override;

void setResultSuccess() override;
Expand Down
4 changes: 2 additions & 2 deletions src/transactions/PathPaymentStrictSendOpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ PathPaymentStrictSendOpFrame::getDestAsset() const
return mPathPayment.destAsset;
}

AccountID const&
PathPaymentStrictSendOpFrame::getDestID() const
MuxedAccount const&
PathPaymentStrictSendOpFrame::getDestMuxedAccount() const
{
return mPathPayment.destination;
}
Expand Down
2 changes: 1 addition & 1 deletion src/transactions/PathPaymentStrictSendOpFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PathPaymentStrictSendOpFrame : public PathPaymentOpFrameBase

Asset const& getSourceAsset() const override;
Asset const& getDestAsset() const override;
AccountID const& getDestID() const override;
MuxedAccount const& getDestMuxedAccount() const override;
xdr::xvector<Asset, 5> const& getPath() const override;

void setResultSuccess() override;
Expand Down
10 changes: 6 additions & 4 deletions src/transactions/PaymentOpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ PaymentOpFrame::doApply(AbstractLedgerTxn& ltx)
// least to check trustlines
// in ledger version 2 it would work for any asset type
auto ledgerVersion = ltx.loadHeader().current().ledgerVersion;
auto destID = toAccountID(mPayment.destination);
auto instantSuccess = ledgerVersion > 2
? mPayment.destination == getSourceID() &&
? destID == getSourceID() &&
mPayment.asset.type() == ASSET_TYPE_NATIVE
: mPayment.destination == getSourceID();
: destID == getSourceID();
if (instantSuccess)
{
innerResult().code(PAYMENT_SUCCESS);
Expand Down Expand Up @@ -128,13 +129,14 @@ void
PaymentOpFrame::insertLedgerKeysToPrefetch(
std::unordered_set<LedgerKey>& keys) const
{
keys.emplace(accountKey(mPayment.destination));
auto destID = toAccountID(mPayment.destination);
keys.emplace(accountKey(destID));

// Prefetch issuer for non-native assets
if (mPayment.asset.type() != ASSET_TYPE_NATIVE)
{
// These are *maybe* needed; For now, we load everything
keys.emplace(trustlineKey(mPayment.destination, mPayment.asset));
keys.emplace(trustlineKey(destID, mPayment.asset));
keys.emplace(trustlineKey(getSourceID(), mPayment.asset));
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/transactions/TransactionFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ TransactionFrame::getSourceID() const
res.ed25519() = mEnvelope.v0().tx.sourceAccountEd25519;
return res;
}
return mEnvelope.v1().tx.sourceAccount;
return toAccountID(mEnvelope.v1().tx.sourceAccount);
}

uint32_t
Expand Down Expand Up @@ -321,7 +321,8 @@ TransactionFrame::commonValidPreSeqNum(AbstractLedgerTxn& ltx, bool chargeFee)
// (stay true regardless of other side effects)
auto header = ltx.loadHeader();
uint32_t ledgerVersion = header.current().ledgerVersion;
if ((ledgerVersion < 13 && mEnvelope.type() == ENVELOPE_TYPE_TX) ||
if ((ledgerVersion < 13 && (mEnvelope.type() == ENVELOPE_TYPE_TX ||
hasMuxedAccount(mEnvelope))) ||
(ledgerVersion >= 13 && mEnvelope.type() == ENVELOPE_TYPE_TX_V0))
{
getResult().result.code(txNOT_SUPPORTED);
Expand Down
64 changes: 64 additions & 0 deletions src/transactions/TransactionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,73 @@ trustLineFlagIsValid(uint32_t flag, uint32_t ledgerVersion)
}
}

AccountID
toAccountID(MuxedAccount const& m)
{
AccountID ret(static_cast<PublicKeyType>(m.type() & 0xff));
switch (m.type())
{
case KEY_TYPE_ED25519:
ret.ed25519() = m.ed25519();
break;
case KEY_TYPE_MUXED_ED25519:
ret.ed25519() = m.med25519().ed25519;
break;
default:
// this would be a bug
abort();
}
return ret;
}

bool
trustLineFlagIsValid(uint32_t flag, LedgerTxnHeader const& header)
{
return trustLineFlagIsValid(flag, header.current().ledgerVersion);
}

namespace detail
{
struct MuxChecker
{
bool mHasMuxedAccount{false};

void
operator()(stellar::MuxedAccount const& t)
{
// checks if this is a multiplexed account,
// such as KEY_TYPE_MUXED_ED25519
if ((t.type() & 0x100) != 0)
{
mHasMuxedAccount = true;
}
}

template <typename T>
std::enable_if_t<(xdr::xdr_traits<T>::is_container ||
xdr::xdr_traits<T>::is_class)>
operator()(T const& t)
{
if (!mHasMuxedAccount)
{
xdr::xdr_traits<T>::save(*this, t);
}
}

template <typename T>
std::enable_if_t<!(xdr::xdr_traits<T>::is_container ||
xdr::xdr_traits<T>::is_class)>
operator()(T const& t)
{
}
};
} // namespace detail

bool
hasMuxedAccount(TransactionEnvelope const& e)
{
detail::MuxChecker c;
c(e);
return c.mHasMuxedAccount;
}
} // namespace stellar
Loading