Tidy up TxFormat and create TxFormats, TxFlags

This commit is contained in:
Vinnie Falco
2013-06-15 07:18:14 -07:00
parent 17e370918b
commit 8aab3645cb
56 changed files with 1883 additions and 1599 deletions

View File

@@ -0,0 +1,59 @@
#ifndef RIPPLE_HASHPREFIX_H
#define RIPPLE_HASHPREFIX_H
/** Prefix for hashing functions.
These prefixes are inserted before the source material used to
generate various hashes. This is done to put each hash in its own
"space." This way, two different types of objects with the
same binary data will produce different hashes.
Each prefix is a 4-byte value with the last byte set to zero
and the first three bytes formed from the ASCII equivalent of
some arbitrary string. For example "TXN".
@note Hash prefixes are part of the Ripple protocol.
@ingroup protocol
*/
struct HashPrefix
{
// VFALCO TODO Make these Doxygen comments and expand the
// description to complete, concise sentences.
//
// transaction plus signature to give transaction ID
static uint32 const transactionID = 0x54584E00; // 'TXN'
// transaction plus metadata
static uint32 const txNode = 0x534E4400; // 'TND'
// account state
static uint32 const leafNode = 0x4D4C4E00; // 'MLN'
// inner node in tree
static uint32 const innerNode = 0x4D494E00; // 'MIN'
// ledger master data for signing
static uint32 const ledgerMaster = 0x4C575200; // 'LGR'
// inner transaction to sign
static uint32 const txSign = 0x53545800; // 'STX'
// validation for signing
static uint32 const validation = 0x56414C00; // 'VAL'
// proposal for signing
static uint32 const proposal = 0x50525000; // 'PRP'
// inner transaction to sign (TESTNET)
static uint32 const txSignTestnet = 0x73747800; // 'stx'
// validation for signing (TESTNET)
static uint32 const validationTestnet = 0x76616C00; // 'val'
// proposal for signing (TESTNET)
static uint32 const proposalTestnet = 0x70727000; // 'prp'
};
#endif

View File

@@ -1,5 +1,26 @@
#ifndef RIPPLE_PROTOCOLTYPES_H
#define RIPPLE_PROTOCOLTYPES_H
#ifndef RIPPLE_PROTOCOL_H
#define RIPPLE_PROTOCOL_H
/** Protocol specific constants, types, and data.
This information is part of the Ripple protocol. Specifically,
it is required for peers to be able to communicate with each other.
@note Changing these will create a hard fork.
@ingroup protocol
@defgroup protocol
*/
struct Protocol
{
/** Smallest legal byte size of a transaction.
*/
static int const txMinSizeBytes = 32;
/** Largest legal byte size of a transaction.
*/
static int const txMaxSizeBytes = 1024 * 1024; // 1048576
};
/*
Hashes are used to uniquely identify objects like

View File

@@ -1262,12 +1262,12 @@ UPTR_T<STObject> STObject::parseJson (const Json::Value& object, SField::ref inN
{
if (field == sfTransactionType)
{
TransactionFormat* f = TransactionFormat::getTxnFormat (strValue);
TxFormat* f = TxFormats::getInstance ().findByName (strValue);
if (!f)
throw std::runtime_error ("Unknown transaction type");
data.push_back (new STUInt16 (field, static_cast<uint16> (f->t_type)));
data.push_back (new STUInt16 (field, static_cast<uint16> (f->getType ())));
if (*name == sfGeneric)
name = &sfTransaction;

View File

@@ -124,10 +124,10 @@ std::string STUInt16::getText () const
if (getFName () == sfTransactionType)
{
TransactionFormat* f = TransactionFormat::getTxnFormat (value);
TxFormat* f = TxFormats::getInstance ().findByType (static_cast <TransactionType> (value));
if (f != NULL)
return f->t_name;
return f->getName ();
}
return boost::lexical_cast<std::string> (value);
@@ -145,10 +145,10 @@ Json::Value STUInt16::getJson (int) const
if (getFName () == sfTransactionType)
{
TransactionFormat* f = TransactionFormat::getTxnFormat (value);
TxFormat* f = TxFormats::getInstance ().findByType (static_cast <TransactionType> (value));
if (f != NULL)
return f->t_name;
return f->getName ();
}
return value;

View File

@@ -11,6 +11,8 @@
// operator to copy the field name breaks the use of copy assignment just to copy values,
// which is used in the transaction engine code.
// VFALCO TODO Remove this unused enum
/*
enum PathFlags
{
PF_END = 0x00, // End of current path & path list.
@@ -24,6 +26,7 @@ enum PathFlags
PF_REDEEM = 0x40,
PF_ISSUE = 0x80,
};
*/
// VFALCO TODO make these non static or otherwise clean constants.
static const uint160 u160_zero (0), u160_one (1);
@@ -43,6 +46,9 @@ static inline const uint160& get_u160_one ()
#define ACCOUNT_XRP get_u160_zero()
#define ACCOUNT_ONE get_u160_one() // Used as a place holder.
// VFALCO TODO Document this as it looks like a central class.
// STObject is derived from it
//
class SerializedType
{
public:

View File

@@ -1,88 +0,0 @@
#ifndef RIPPLE_TRANSACTIONFORMAT_H
#define RIPPLE_TRANSACTIONFORMAT_H
enum TransactionType
{
ttINVALID = -1,
ttPAYMENT = 0,
ttCLAIM = 1, // open
ttWALLET_ADD = 2,
ttACCOUNT_SET = 3,
ttPASSWORD_FUND = 4, // open
ttREGULAR_KEY_SET = 5,
ttNICKNAME_SET = 6, // open
ttOFFER_CREATE = 7,
ttOFFER_CANCEL = 8,
ttCONTRACT = 9,
ttCONTRACT_REMOVE = 10, // can we use the same msg as offer cancel
ttTRUST_SET = 20,
ttFEATURE = 100,
ttFEE = 101,
};
class TransactionFormat
{
public:
std::string t_name;
TransactionType t_type;
SOTemplate elements;
static std::map<int, TransactionFormat*> byType;
static std::map<std::string, TransactionFormat*> byName;
TransactionFormat (const char* name, TransactionType type) : t_name (name), t_type (type)
{
byName[name] = this;
byType[type] = this;
}
TransactionFormat& operator<< (const SOElement& el)
{
elements.push_back (el);
return *this;
}
static TransactionFormat* getTxnFormat (TransactionType t);
static TransactionFormat* getTxnFormat (const std::string& t);
static TransactionFormat* getTxnFormat (int t);
};
const int TransactionMinLen = 32;
const int TransactionMaxLen = 1048576;
//
// Transaction flags.
//
// AccountSet flags:
const uint32 tfRequireDestTag = 0x00010000;
const uint32 tfOptionalDestTag = 0x00020000;
const uint32 tfRequireAuth = 0x00040000;
const uint32 tfOptionalAuth = 0x00080000;
const uint32 tfDisallowXRP = 0x00100000;
const uint32 tfAllowXRP = 0x00200000;
const uint32 tfAccountSetMask = ~ (tfRequireDestTag | tfOptionalDestTag
| tfRequireAuth | tfOptionalAuth
| tfDisallowXRP | tfAllowXRP);
// OfferCreate flags:
const uint32 tfPassive = 0x00010000;
const uint32 tfImmediateOrCancel = 0x00020000;
const uint32 tfFillOrKill = 0x00040000;
const uint32 tfSell = 0x00080000;
const uint32 tfOfferCreateMask = ~ (tfPassive | tfImmediateOrCancel | tfFillOrKill | tfSell);
// Payment flags:
const uint32 tfNoRippleDirect = 0x00010000;
const uint32 tfPartialPayment = 0x00020000;
const uint32 tfLimitQuality = 0x00040000;
const uint32 tfPaymentMask = ~ (tfPartialPayment | tfLimitQuality | tfNoRippleDirect);
// TrustSet flags:
const uint32 tfSetfAuth = 0x00010000;
const uint32 tfTrustSetMask = ~ (tfSetfAuth);
#endif
// vim:ts=4

View File

@@ -0,0 +1,51 @@
#ifndef RIPPLE_TXFLAGS_H
#define RIPPLE_TXFLAGS_H
//
// Transaction flags.
//
/** Transaction flags.
These flags modify the behavior of an operation.
@note Changing these will create a hard fork
@ingroup protocol
*/
class TxFlag
{
public:
static uint32 const requireDestTag = 0x00010000;
};
// VFALCO TODO Move all flags into this container after some study.
// AccountSet flags:
// VFALCO TODO Javadoc comment every one of these constants
//const uint32 TxFlag::requireDestTag = 0x00010000;
const uint32 tfOptionalDestTag = 0x00020000;
const uint32 tfRequireAuth = 0x00040000;
const uint32 tfOptionalAuth = 0x00080000;
const uint32 tfDisallowXRP = 0x00100000;
const uint32 tfAllowXRP = 0x00200000;
const uint32 tfAccountSetMask = ~ (TxFlag::requireDestTag | tfOptionalDestTag
| tfRequireAuth | tfOptionalAuth
| tfDisallowXRP | tfAllowXRP);
// OfferCreate flags:
const uint32 tfPassive = 0x00010000;
const uint32 tfImmediateOrCancel = 0x00020000;
const uint32 tfFillOrKill = 0x00040000;
const uint32 tfSell = 0x00080000;
const uint32 tfOfferCreateMask = ~ (tfPassive | tfImmediateOrCancel | tfFillOrKill | tfSell);
// Payment flags:
const uint32 tfNoRippleDirect = 0x00010000;
const uint32 tfPartialPayment = 0x00020000;
const uint32 tfLimitQuality = 0x00040000;
const uint32 tfPaymentMask = ~ (tfPartialPayment | tfLimitQuality | tfNoRippleDirect);
// TrustSet flags:
const uint32 tfSetfAuth = 0x00010000;
const uint32 tfTrustSetMask = ~ (tfSetfAuth);
#endif

View File

@@ -1,10 +1,7 @@
std::map<int, TransactionFormat*> TransactionFormat::byType;
// VFALCO TODO Find a way to not use macros. inline function?
std::map<std::string, TransactionFormat*> TransactionFormat::byName;
// VFALCO TODO surely we can think of a better way than to use macros??
#define TF_BASE \
#define TF_BASE \
<< SOElement(sfTransactionType, SOE_REQUIRED) \
<< SOElement(sfFlags, SOE_OPTIONAL) \
<< SOElement(sfSourceTag, SOE_OPTIONAL) \
@@ -16,11 +13,11 @@ std::map<std::string, TransactionFormat*> TransactionFormat::byName;
<< SOElement(sfSigningPubKey, SOE_REQUIRED) \
<< SOElement(sfTxnSignature, SOE_OPTIONAL)
#define DECLARE_TF(name, type) tf = new TransactionFormat(#name, type); (*tf) TF_BASE
#define DECLARE_TF(name, type) tf = TxFormats::getInstance().add (new TxFormat(#name, type)); (*tf) TF_BASE
void TFInit ()
{
TransactionFormat* tf;
TxFormat* tf;
DECLARE_TF (AccountSet, ttACCOUNT_SET)
<< SOElement (sfEmailHash, SOE_OPTIONAL)
@@ -86,35 +83,3 @@ void TFInit ()
<< SOElement (sfReserveIncrement, SOE_REQUIRED)
;
}
TransactionFormat* TransactionFormat::getTxnFormat (TransactionType t)
{
std::map<int, TransactionFormat*>::iterator it = byType.find (static_cast<int> (t));
if (it == byType.end ())
return NULL;
return it->second;
}
TransactionFormat* TransactionFormat::getTxnFormat (int t)
{
std::map<int, TransactionFormat*>::iterator it = byType.find ((t));
if (it == byType.end ())
return NULL;
return it->second;
}
TransactionFormat* TransactionFormat::getTxnFormat (const std::string& t)
{
std::map<std::string, TransactionFormat*>::iterator it = byName.find ((t));
if (it == byName.end ())
return NULL;
return it->second;
}
// vim:ts=4

View File

@@ -0,0 +1,70 @@
#ifndef RIPPLE_TXFORMAT_H
#define RIPPLE_TXFORMAT_H
// VFALCO TODO Rename to TxType
// Be aware there are some strings "TransactionType"
// And also we have TransactionType in ripple_SerializeDeclarations.h
//
/** Transaction type identifiers.
These are part of the binary message format.
@ingroup protocol
*/
enum TransactionType
{
ttINVALID = -1,
ttPAYMENT = 0,
ttCLAIM = 1, // open
ttWALLET_ADD = 2,
ttACCOUNT_SET = 3,
ttPASSWORD_FUND = 4, // open
ttREGULAR_KEY_SET = 5,
ttNICKNAME_SET = 6, // open
ttOFFER_CREATE = 7,
ttOFFER_CANCEL = 8,
ttCONTRACT = 9,
ttCONTRACT_REMOVE = 10, // can we use the same msg as offer cancel
ttTRUST_SET = 20,
ttFEATURE = 100,
ttFEE = 101,
};
class TxFormat
{
public:
TxFormat (char const* name, TransactionType type)
: m_name (name)
, m_type (type)
{
}
TxFormat& operator<< (SOElement const& el)
{
elements.push_back (el);
return *this;
}
/** Retrieve the name of the format.
*/
std::string const& getName () const { return m_name; }
/** Retrieve the transaction type this format represents.
*/
TransactionType getType () const { return m_type; }
public:
// VFALCO TODO make an accessor for this
SOTemplate elements;
private:
std::string const m_name;
TransactionType const m_type;
};
#endif
// vim:ts=4

View File

@@ -0,0 +1,49 @@
TxFormats& TxFormats::getInstance ()
{
static TxFormats instance;
return instance;
}
TxFormat* TxFormats::add (TxFormat* txFormat)
{
// VFALCO TODO Figure out when and how to delete the TxFormat objects later?
m_types [txFormat->getType ()] = txFormat;
m_names [txFormat->getName ()] = txFormat;
return txFormat;
}
TxFormat* TxFormats::findByType (TransactionType type)
{
TxFormat* result = NULL;
TypeMap::iterator const iter = m_types.find (type);
if (iter != m_types.end ())
{
result = iter->second;
}
return result;
}
TxFormat* TxFormats::findByName (std::string const& name)
{
TxFormat* result = NULL; // VFALCO TODO replace all NULL with nullptr
NameMap::iterator const iter = m_names.find (name);
if (iter != m_names.end ())
{
result = iter->second;
}
return result;
}
TxFormats::TxFormats ()
{
}

View File

@@ -0,0 +1,40 @@
#ifndef RIPPLE_TXFORMATS_H
#define RIPPLE_TXFORMATS_H
/** Manages the list of known transaction formats.
*/
class TxFormats
{
public:
// VFALCO TODO Make this a member of the Application object instead of a singleton?
static TxFormats& getInstance ();
/** Add a format.
The caller is responsible for freeing the memory.
@return The passed format.
*/
TxFormat* add (TxFormat* txFormat);
/** Retrieve a format based on its transaction type.
*/
TxFormat* findByType (TransactionType type);
/** Retrieve a format based on its name.
*/
TxFormat* findByName (std::string const& name);
private:
TxFormats ();
private:
typedef std::map <std::string, TxFormat*> NameMap;
typedef std::map <TransactionType, TxFormat*> TypeMap;
NameMap m_names;
TypeMap m_types;
};
#endif
// vim:ts=4