Format first-party source according to .clang-format

This commit is contained in:
Pretty Printer
2020-04-17 09:56:34 -05:00
committed by manojsdoshi
parent 65dfc5d19e
commit 50760c6935
1076 changed files with 86161 additions and 77449 deletions

View File

@@ -17,22 +17,22 @@
*/
//==============================================================================
#include <ripple/protocol/STTx.h>
#include <ripple/basics/contract.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/basics/StringUtilities.h>
#include <ripple/basics/contract.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/json/to_string.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/HashPrefix.h>
#include <ripple/protocol/jss.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/protocol/PublicKey.h>
#include <ripple/protocol/Sign.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/STArray.h>
#include <ripple/protocol/STTx.h>
#include <ripple/protocol/Sign.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/protocol/UintTypes.h>
#include <ripple/json/to_string.h>
#include <ripple/protocol/jss.h>
#include <boost/format.hpp>
#include <array>
#include <memory>
@@ -41,95 +41,90 @@
namespace ripple {
static
auto getTxFormat (TxType type)
static auto
getTxFormat(TxType type)
{
auto format = TxFormats::getInstance().findByType (type);
auto format = TxFormats::getInstance().findByType(type);
if (format == nullptr)
{
Throw<std::runtime_error> (
Throw<std::runtime_error>(
"Invalid transaction type " +
std::to_string (
safe_cast<std::underlying_type_t<TxType>>(type)));
std::to_string(safe_cast<std::underlying_type_t<TxType>>(type)));
}
return format;
}
STTx::STTx (STObject&& object) noexcept (false)
: STObject (std::move (object))
STTx::STTx(STObject&& object) noexcept(false) : STObject(std::move(object))
{
tx_type_ = safe_cast<TxType> (getFieldU16 (sfTransactionType));
applyTemplate (getTxFormat (tx_type_)->getSOTemplate()); // may throw
tx_type_ = safe_cast<TxType>(getFieldU16(sfTransactionType));
applyTemplate(getTxFormat(tx_type_)->getSOTemplate()); // may throw
tid_ = getHash(HashPrefix::transactionID);
}
STTx::STTx (SerialIter& sit) noexcept (false)
: STObject (sfTransaction)
STTx::STTx(SerialIter& sit) noexcept(false) : STObject(sfTransaction)
{
int length = sit.getBytesLeft ();
int length = sit.getBytesLeft();
if ((length < txMinSizeBytes) || (length > txMaxSizeBytes))
Throw<std::runtime_error> ("Transaction length invalid");
Throw<std::runtime_error>("Transaction length invalid");
if (set (sit))
Throw<std::runtime_error> ("Transaction contains an object terminator");
if (set(sit))
Throw<std::runtime_error>("Transaction contains an object terminator");
tx_type_ = safe_cast<TxType> (getFieldU16 (sfTransactionType));
tx_type_ = safe_cast<TxType>(getFieldU16(sfTransactionType));
applyTemplate (getTxFormat (tx_type_)->getSOTemplate()); // May throw
applyTemplate(getTxFormat(tx_type_)->getSOTemplate()); // May throw
tid_ = getHash(HashPrefix::transactionID);
}
STTx::STTx (
TxType type,
std::function<void(STObject&)> assembler)
: STObject (sfTransaction)
STTx::STTx(TxType type, std::function<void(STObject&)> assembler)
: STObject(sfTransaction)
{
auto format = getTxFormat (type);
auto format = getTxFormat(type);
set (format->getSOTemplate());
setFieldU16 (sfTransactionType, format->getType ());
set(format->getSOTemplate());
setFieldU16(sfTransactionType, format->getType());
assembler (*this);
assembler(*this);
tx_type_ = safe_cast<TxType>(getFieldU16 (sfTransactionType));
tx_type_ = safe_cast<TxType>(getFieldU16(sfTransactionType));
if (tx_type_ != type)
LogicError ("Transaction type was mutated during assembly");
LogicError("Transaction type was mutated during assembly");
tid_ = getHash(HashPrefix::transactionID);
}
std::string
STTx::getFullText () const
STTx::getFullText() const
{
std::string ret = "\"";
ret += to_string (getTransactionID ());
ret += to_string(getTransactionID());
ret += "\" = {";
ret += STObject::getFullText ();
ret += STObject::getFullText();
ret += "}";
return ret;
}
boost::container::flat_set<AccountID>
STTx::getMentionedAccounts () const
STTx::getMentionedAccounts() const
{
boost::container::flat_set<AccountID> list;
for (auto const& it : *this)
{
if (auto sacc = dynamic_cast<STAccount const*> (&it))
if (auto sacc = dynamic_cast<STAccount const*>(&it))
{
assert(! sacc->isDefault());
if (! sacc->isDefault())
assert(!sacc->isDefault());
if (!sacc->isDefault())
list.insert(sacc->value());
}
else if (auto samt = dynamic_cast<STAmount const*> (&it))
else if (auto samt = dynamic_cast<STAmount const*>(&it))
{
auto const& issuer = samt->getIssuer ();
if (! isXRP (issuer))
auto const& issuer = samt->getIssuer();
if (!isXRP(issuer))
list.insert(issuer);
}
}
@@ -137,60 +132,57 @@ STTx::getMentionedAccounts () const
return list;
}
static Blob getSigningData (STTx const& that)
static Blob
getSigningData(STTx const& that)
{
Serializer s;
s.add32 (HashPrefix::txSign);
that.addWithoutSigningFields (s);
s.add32(HashPrefix::txSign);
that.addWithoutSigningFields(s);
return s.getData();
}
uint256
STTx::getSigningHash () const
STTx::getSigningHash() const
{
return STObject::getSigningHash (HashPrefix::txSign);
return STObject::getSigningHash(HashPrefix::txSign);
}
Blob STTx::getSignature () const
Blob
STTx::getSignature() const
{
try
{
return getFieldVL (sfTxnSignature);
return getFieldVL(sfTxnSignature);
}
catch (std::exception const&)
{
return Blob ();
return Blob();
}
}
void STTx::sign (
PublicKey const& publicKey,
SecretKey const& secretKey)
void
STTx::sign(PublicKey const& publicKey, SecretKey const& secretKey)
{
auto const data = getSigningData (*this);
auto const data = getSigningData(*this);
auto const sig = ripple::sign (
publicKey,
secretKey,
makeSlice(data));
auto const sig = ripple::sign(publicKey, secretKey, makeSlice(data));
setFieldVL (sfTxnSignature, sig);
setFieldVL(sfTxnSignature, sig);
tid_ = getHash(HashPrefix::transactionID);
}
std::pair<bool, std::string>
STTx::checkSign(RequireFullyCanonicalSig requireCanonicalSig) const
{
std::pair<bool, std::string> ret {false, ""};
std::pair<bool, std::string> ret{false, ""};
try
{
// Determine whether we're single- or multi-signing by looking
// at the SigningPubKey. It it's empty we must be
// multi-signing. Otherwise we're single-signing.
Blob const& signingPubKey = getFieldVL (sfSigningPubKey);
ret = signingPubKey.empty () ?
checkMultiSign (requireCanonicalSig) :
checkSingleSign (requireCanonicalSig);
Blob const& signingPubKey = getFieldVL(sfSigningPubKey);
ret = signingPubKey.empty() ? checkMultiSign(requireCanonicalSig)
: checkSingleSign(requireCanonicalSig);
}
catch (std::exception const&)
{
@@ -199,86 +191,93 @@ STTx::checkSign(RequireFullyCanonicalSig requireCanonicalSig) const
return ret;
}
Json::Value STTx::getJson (JsonOptions) const
Json::Value STTx::getJson(JsonOptions) const
{
Json::Value ret = STObject::getJson (JsonOptions::none);
ret[jss::hash] = to_string (getTransactionID ());
Json::Value ret = STObject::getJson(JsonOptions::none);
ret[jss::hash] = to_string(getTransactionID());
return ret;
}
Json::Value STTx::getJson (JsonOptions options, bool binary) const
Json::Value
STTx::getJson(JsonOptions options, bool binary) const
{
if (binary)
{
Json::Value ret;
Serializer s = STObject::getSerializer ();
ret[jss::tx] = strHex (s.peekData ());
ret[jss::hash] = to_string (getTransactionID ());
Serializer s = STObject::getSerializer();
ret[jss::tx] = strHex(s.peekData());
ret[jss::hash] = to_string(getTransactionID());
return ret;
}
return getJson(options);
}
std::string const&
STTx::getMetaSQLInsertReplaceHeader ()
STTx::getMetaSQLInsertReplaceHeader()
{
static std::string const sql = "INSERT OR REPLACE INTO Transactions "
"(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, TxnMeta)"
static std::string const sql =
"INSERT OR REPLACE INTO Transactions "
"(TransID, TransType, FromAcct, FromSeq, LedgerSeq, Status, RawTxn, "
"TxnMeta)"
" VALUES ";
return sql;
}
std::string STTx::getMetaSQL (std::uint32_t inLedger,
std::string const& escapedMetaData) const
std::string
STTx::getMetaSQL(std::uint32_t inLedger, std::string const& escapedMetaData)
const
{
Serializer s;
add (s);
return getMetaSQL (s, inLedger, txnSqlValidated, escapedMetaData);
add(s);
return getMetaSQL(s, inLedger, txnSqlValidated, escapedMetaData);
}
// VFALCO This could be a free function elsewhere
std::string
STTx::getMetaSQL (Serializer rawTxn,
std::uint32_t inLedger, char status, std::string const& escapedMetaData) const
STTx::getMetaSQL(
Serializer rawTxn,
std::uint32_t inLedger,
char status,
std::string const& escapedMetaData) const
{
static boost::format bfTrans ("('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
std::string rTxn = sqlEscape (rawTxn.peekData ());
static boost::format bfTrans(
"('%s', '%s', '%s', '%d', '%d', '%c', %s, %s)");
std::string rTxn = sqlEscape(rawTxn.peekData());
auto format = TxFormats::getInstance().findByType (tx_type_);
assert (format != nullptr);
auto format = TxFormats::getInstance().findByType(tx_type_);
assert(format != nullptr);
return str (boost::format (bfTrans)
% to_string (getTransactionID ()) % format->getName ()
% toBase58(getAccountID(sfAccount))
% getSequence () % inLedger % status % rTxn % escapedMetaData);
return str(
boost::format(bfTrans) % to_string(getTransactionID()) %
format->getName() % toBase58(getAccountID(sfAccount)) % getSequence() %
inLedger % status % rTxn % escapedMetaData);
}
std::pair<bool, std::string>
STTx::checkSingleSign (RequireFullyCanonicalSig requireCanonicalSig) const
STTx::checkSingleSign(RequireFullyCanonicalSig requireCanonicalSig) const
{
// We don't allow both a non-empty sfSigningPubKey and an sfSigners.
// That would allow the transaction to be signed two ways. So if both
// fields are present the signature is invalid.
if (isFieldPresent (sfSigners))
if (isFieldPresent(sfSigners))
return {false, "Cannot both single- and multi-sign."};
bool validSig = false;
try
{
bool const fullyCanonical =
(getFlags() & tfFullyCanonicalSig) ||
bool const fullyCanonical = (getFlags() & tfFullyCanonicalSig) ||
(requireCanonicalSig == RequireFullyCanonicalSig::yes);
auto const spk = getFieldVL (sfSigningPubKey);
auto const spk = getFieldVL(sfSigningPubKey);
if (publicKeyType (makeSlice(spk)))
if (publicKeyType(makeSlice(spk)))
{
Blob const signature = getFieldVL (sfTxnSignature);
Blob const data = getSigningData (*this);
Blob const signature = getFieldVL(sfTxnSignature);
Blob const data = getSigningData(*this);
validSig = verify (
PublicKey (makeSlice(spk)),
validSig = verify(
PublicKey(makeSlice(spk)),
makeSlice(data),
makeSlice(signature),
fullyCanonical);
@@ -296,19 +295,19 @@ STTx::checkSingleSign (RequireFullyCanonicalSig requireCanonicalSig) const
}
std::pair<bool, std::string>
STTx::checkMultiSign (RequireFullyCanonicalSig requireCanonicalSig) const
STTx::checkMultiSign(RequireFullyCanonicalSig requireCanonicalSig) const
{
// Make sure the MultiSigners are present. Otherwise they are not
// attempting multi-signing and we just have a bad SigningPubKey.
if (!isFieldPresent (sfSigners))
if (!isFieldPresent(sfSigners))
return {false, "Empty SigningPubKey."};
// We don't allow both an sfSigners and an sfTxnSignature. Both fields
// being present would indicate that the transaction is signed both ways.
if (isFieldPresent (sfTxnSignature))
if (isFieldPresent(sfTxnSignature))
return {false, "Cannot both single- and multi-sign."};
STArray const& signers {getFieldArray (sfSigners)};
STArray const& signers{getFieldArray(sfSigners)};
// There are well known bounds that the number of signers must be within.
if (signers.size() < minMultiSigners || signers.size() > maxMultiSigners)
@@ -317,22 +316,21 @@ STTx::checkMultiSign (RequireFullyCanonicalSig requireCanonicalSig) const
// We can ease the computational load inside the loop a bit by
// pre-constructing part of the data that we hash. Fill a Serializer
// with the stuff that stays constant from signature to signature.
Serializer const dataStart {startMultiSigningData (*this)};
Serializer const dataStart{startMultiSigningData(*this)};
// We also use the sfAccount field inside the loop. Get it once.
auto const txnAccountID = getAccountID (sfAccount);
auto const txnAccountID = getAccountID(sfAccount);
// Determine whether signatures must be full canonical.
bool const fullyCanonical =
(getFlags() & tfFullyCanonicalSig) ||
bool const fullyCanonical = (getFlags() & tfFullyCanonicalSig) ||
(requireCanonicalSig == RequireFullyCanonicalSig::yes);
// Signers must be in sorted order by AccountID.
AccountID lastAccountID (beast::zero);
AccountID lastAccountID(beast::zero);
for (auto const& signer : signers)
{
auto const accountID = signer.getAccountID (sfAccount);
auto const accountID = signer.getAccountID(sfAccount);
// The account owner may not multisign for themselves.
if (accountID == txnAccountID)
@@ -354,17 +352,16 @@ STTx::checkMultiSign (RequireFullyCanonicalSig requireCanonicalSig) const
try
{
Serializer s = dataStart;
finishMultiSigningData (accountID, s);
finishMultiSigningData(accountID, s);
auto spk = signer.getFieldVL (sfSigningPubKey);
auto spk = signer.getFieldVL(sfSigningPubKey);
if (publicKeyType (makeSlice(spk)))
if (publicKeyType(makeSlice(spk)))
{
Blob const signature =
signer.getFieldVL (sfTxnSignature);
Blob const signature = signer.getFieldVL(sfTxnSignature);
validSig = verify (
PublicKey (makeSlice(spk)),
validSig = verify(
PublicKey(makeSlice(spk)),
s.slice(),
makeSlice(signature),
fullyCanonical);
@@ -376,8 +373,10 @@ STTx::checkMultiSign (RequireFullyCanonicalSig requireCanonicalSig) const
validSig = false;
}
if (!validSig)
return {false, std::string("Invalid signature on account ") +
toBase58(accountID) + "."};
return {
false,
std::string("Invalid signature on account ") +
toBase58(accountID) + "."};
}
// All signatures verified.
@@ -386,22 +385,21 @@ STTx::checkMultiSign (RequireFullyCanonicalSig requireCanonicalSig) const
//------------------------------------------------------------------------------
static
bool
isMemoOkay (STObject const& st, std::string& reason)
static bool
isMemoOkay(STObject const& st, std::string& reason)
{
if (!st.isFieldPresent (sfMemos))
if (!st.isFieldPresent(sfMemos))
return true;
auto const& memos = st.getFieldArray (sfMemos);
auto const& memos = st.getFieldArray(sfMemos);
// The number 2048 is a preallocation hint, not a hard limit
// to avoid allocate/copy/free's
Serializer s (2048);
memos.add (s);
Serializer s(2048);
memos.add(s);
// FIXME move the memo limit into a config tunable
if (s.getDataLength () > 1024)
if (s.getDataLength() > 1024)
{
reason = "The memo exceeds the maximum allowed size.";
return false;
@@ -409,7 +407,7 @@ isMemoOkay (STObject const& st, std::string& reason)
for (auto const& memo : memos)
{
auto memoObj = dynamic_cast <STObject const*> (&memo);
auto memoObj = dynamic_cast<STObject const*>(&memo);
if (!memoObj || (memoObj->getFName() != sfMemo))
{
@@ -421,22 +419,23 @@ isMemoOkay (STObject const& st, std::string& reason)
{
auto const& name = memoElement.getFName();
if (name != sfMemoType &&
name != sfMemoData &&
if (name != sfMemoType && name != sfMemoData &&
name != sfMemoFormat)
{
reason = "A memo may contain only MemoType, MemoData or "
"MemoFormat fields.";
reason =
"A memo may contain only MemoType, MemoData or "
"MemoFormat fields.";
return false;
}
// The raw data is stored as hex-octets, which we want to decode.
auto optData = strUnHex (memoElement.getText ());
auto optData = strUnHex(memoElement.getText());
if (!optData)
{
reason = "The MemoType, MemoData and MemoFormat fields may "
"only contain hex-encoded data.";
reason =
"The MemoType, MemoData and MemoFormat fields may "
"only contain hex-encoded data.";
return false;
}
@@ -446,18 +445,17 @@ isMemoOkay (STObject const& st, std::string& reason)
// The only allowed characters for MemoType and MemoFormat are the
// characters allowed in URLs per RFC 3986: alphanumerics and the
// following symbols: -._~:/?#[]@!$&'()*+,;=%
static std::array<char, 256> const allowedSymbols = []
{
static std::array<char, 256> const allowedSymbols = [] {
std::array<char, 256> a;
a.fill(0);
std::string symbols (
std::string symbols(
"0123456789"
"-._~:/?#[]@!$&'()*+,;=%"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz");
for(char c : symbols)
for (char c : symbols)
a[c] = 1;
return a;
}();
@@ -466,9 +464,10 @@ isMemoOkay (STObject const& st, std::string& reason)
{
if (!allowedSymbols[c])
{
reason = "The MemoType and MemoFormat fields may only "
"contain characters that are allowed in URLs "
"under RFC 3986.";
reason =
"The MemoType and MemoFormat fields may only "
"contain characters that are allowed in URLs "
"under RFC 3986.";
return false;
}
}
@@ -479,26 +478,26 @@ isMemoOkay (STObject const& st, std::string& reason)
}
// Ensure all account fields are 160-bits
static
bool
isAccountFieldOkay (STObject const& st)
static bool
isAccountFieldOkay(STObject const& st)
{
for (int i = 0; i < st.getCount(); ++i)
{
auto t = dynamic_cast<STAccount const*>(st.peekAtPIndex (i));
if (t && t->isDefault ())
auto t = dynamic_cast<STAccount const*>(st.peekAtPIndex(i));
if (t && t->isDefault())
return false;
}
return true;
}
bool passesLocalChecks (STObject const& st, std::string& reason)
bool
passesLocalChecks(STObject const& st, std::string& reason)
{
if (!isMemoOkay (st, reason))
if (!isMemoOkay(st, reason))
return false;
if (!isAccountFieldOkay (st))
if (!isAccountFieldOkay(st))
{
reason = "An account field is invalid.";
return false;
@@ -513,7 +512,7 @@ bool passesLocalChecks (STObject const& st, std::string& reason)
}
std::shared_ptr<STTx const>
sterilize (STTx const& stx)
sterilize(STTx const& stx)
{
Serializer s;
stx.add(s);
@@ -531,4 +530,4 @@ isPseudoTx(STObject const& tx)
return tt == ttAMENDMENT || tt == ttFEE;
}
} // ripple
} // namespace ripple