Encode issuer in STAmount. Breaking protocol and db version change.

This commit is contained in:
Arthur Britto
2012-08-23 14:55:03 -07:00
parent acb4502a04
commit e4b8d874e7
7 changed files with 68 additions and 105 deletions

View File

@@ -288,6 +288,7 @@ void STAmount::add(Serializer& s) const
s.add64(mValue | (static_cast<uint64>(mOffset + 512 + 256 + 97) << (64 - 10)));
s.add160(mCurrency);
s.add160(mIssuer);
}
}
@@ -334,25 +335,38 @@ STAmount* STAmount::construct(SerializerIterator& sit, const char *name)
return new STAmount(name, value, true); // negative
}
uint160 currency = sit.get160();
if (!currency)
uint160 uCurrencyID = sit.get160();
if (!uCurrencyID)
throw std::runtime_error("invalid native currency");
uint160 uIssuerID = sit.get160();
int offset = static_cast<int>(value >> (64 - 10)); // 10 bits for the offset, sign and "not native" flag
value &= ~(1023ull << (64-10));
if (value == 0)
STAmount* sapResult;
if (value)
{
bool isNegative = (offset & 256) == 0;
offset = (offset & 255) - 97; // center the range
if ((value < cMinValue) || (value > cMaxValue) || (offset < cMinOffset) || (offset > cMaxOffset))
throw std::runtime_error("invalid currency value");
sapResult = new STAmount(name, uCurrencyID, value, offset, isNegative);
}
else
{
if (offset != 512)
throw std::runtime_error("invalid currency value");
return new STAmount(name, currency);
sapResult = new STAmount(name, uCurrencyID);
}
bool isNegative = (offset & 256) == 0;
offset = (offset & 255) - 97; // center the range
if ((value < cMinValue) || (value > cMaxValue) || (offset < cMinOffset) || (offset > cMaxOffset))
throw std::runtime_error("invalid currency value");
return new STAmount(name, currency, value, offset, isNegative);
if (sapResult)
sapResult->setIssuer(uIssuerID);
return sapResult;
}
int64 STAmount::getSNValue() const

View File

@@ -57,9 +57,7 @@ LedgerEntryFormat LedgerFormats[]=
{ S_FIELD(OwnerNode), STI_UINT64, SOE_REQUIRED, 0 },
{ S_FIELD(LastTxnID), STI_HASH256, SOE_REQUIRED, 0 },
{ S_FIELD(LastTxnSeq), STI_UINT32, SOE_REQUIRED, 0 },
{ S_FIELD(PaysIssuer), STI_ACCOUNT, SOE_IFFLAG, 1 },
{ S_FIELD(GetsIssuer), STI_ACCOUNT, SOE_IFFLAG, 2 },
{ S_FIELD(Expiration), STI_UINT32, SOE_IFFLAG, 4 },
{ S_FIELD(Expiration), STI_UINT32, SOE_IFFLAG, 1 },
{ S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x01000000 },
{ sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } }
},

View File

@@ -50,7 +50,6 @@ enum SOE_Field
sfFlags,
sfGenerator,
sfGeneratorID,
sfGetsIssuer,
sfHash,
sfHighID,
sfHighLimit,
@@ -84,7 +83,6 @@ enum SOE_Field
sfOfferSequence,
sfOwnerNode,
sfPaths,
sfPaysIssuer,
sfPubKey,
sfPublishHash,
sfPublishSize,

View File

@@ -362,12 +362,6 @@ Transaction::pointer Transaction::setOfferCreate(
mTransaction->setITFieldAmount(sfTakerPays, saTakerPays);
mTransaction->setITFieldAmount(sfTakerGets, saTakerGets);
if (!saTakerPays.isNative())
mTransaction->setITFieldAccount(sfPaysIssuer, saTakerPays.getIssuer());
if (!saTakerGets.isNative())
mTransaction->setITFieldAccount(sfGetsIssuer, saTakerGets.getIssuer());
if (uExpiration)
mTransaction->setITFieldU32(sfExpiration, uExpiration);

View File

@@ -251,7 +251,7 @@ STAmount TransactionEngine::accountHolds(const uint160& uAccountID, const uint16
{
STAmount saAmount;
if (uCurrencyID.isZero())
if (!uCurrencyID)
{
SLE::pointer sleAccount = entryCache(ltACCOUNT_ROOT, Ledger::getAccountRootIndex(uAccountID));
@@ -397,7 +397,7 @@ STAmount TransactionEngine::rippleSend(const uint160& uSenderID, const uint160&
STAmount saTransitFee = rippleTransferFee(uSenderID, uReceiverID, uIssuerID, saAmount);
saActual = saTransitFee.isZero() ? saAmount : saAmount+saTransitFee;
saActual = !saTransitFee ? saAmount : saAmount+saTransitFee;
saActual.setIssuer(uIssuerID); // XXX Make sure this done in + above.
@@ -833,7 +833,7 @@ SLE::pointer TransactionEngine::entryCache(LedgerEntryType letType, const uint25
{
SLE::pointer sleEntry;
if (!uIndex.isZero())
if (!!uIndex)
{
LedgerEntryAction action;
sleEntry = mNodes.getEntry(uIndex, action);
@@ -852,7 +852,7 @@ SLE::pointer TransactionEngine::entryCache(LedgerEntryType letType, const uint25
SLE::pointer TransactionEngine::entryCreate(LedgerEntryType letType, const uint256& uIndex)
{
assert(!uIndex.isZero());
assert(!!uIndex);
SLE::pointer sleNew = boost::make_shared<SerializedLedgerEntry>(letType);
sleNew->setIndex(uIndex);
@@ -1030,7 +1030,7 @@ TransactionEngineResult TransactionEngine::applyTransaction(const SerializedTran
if (terSUCCESS == terResult && (params & tepNO_CHECK_FEE) == tepNONE)
{
if (!saCost.isZero())
if (!!saCost)
{
if (saPaid < saCost)
{
@@ -1041,7 +1041,7 @@ TransactionEngineResult TransactionEngine::applyTransaction(const SerializedTran
}
else
{
if (!saPaid.isZero())
if (!!saPaid)
{
// Transaction is malformed.
Log(lsWARNING) << "applyTransaction: fee not allowed";
@@ -1168,7 +1168,7 @@ TransactionEngineResult TransactionEngine::applyTransaction(const SerializedTran
// Deduct the fee, so it's not available during the transaction.
// Will only write the account back, if the transaction succeeds.
if (terSUCCESS != terResult || saCost.isZero())
if (terSUCCESS != terResult || !saCost)
{
nothing();
}
@@ -1191,7 +1191,7 @@ TransactionEngineResult TransactionEngine::applyTransaction(const SerializedTran
{
nothing();
}
else if (!saCost.isZero())
else if (!!saCost)
{
uint32 a_seq = mTxnAccount->getIFieldU32(sfSequence);
@@ -1340,7 +1340,7 @@ TransactionEngineResult TransactionEngine::doAccountSet(const SerializedTransact
{
uint128 uHash = txn.getITFieldH128(sfEmailHash);
if (uHash.isZero())
if (!uHash)
{
Log(lsINFO) << "doAccountSet: unset email hash";
@@ -1362,7 +1362,7 @@ TransactionEngineResult TransactionEngine::doAccountSet(const SerializedTransact
{
uint256 uHash = txn.getITFieldH256(sfWalletLocator);
if (uHash.isZero())
if (!uHash)
{
Log(lsINFO) << "doAccountSet: unset wallet locator";
@@ -1459,7 +1459,7 @@ TransactionEngineResult TransactionEngine::doAccountSet(const SerializedTransact
uint256 uHash = txn.getITFieldH256(sfPublishHash);
uint32 uSize = txn.getITFieldU32(sfPublishSize);
if (uHash.isZero())
if (!uHash)
{
Log(lsINFO) << "doAccountSet: unset publish";
@@ -1535,16 +1535,16 @@ TransactionEngineResult TransactionEngine::doCreditSet(const SerializedTransacti
{
// A line exists in one or more directions.
#if 0
if (saLimitAmount.isZero())
if (!saLimitAmount)
{
// Zeroing line.
uint160 uLowID = sleRippleState->getIValueFieldAccount(sfLowID).getAccountID();
uint160 uHighID = sleRippleState->getIValueFieldAccount(sfHighID).getAccountID();
bool bLow = uLowID == uSrcAccountID;
bool bHigh = uLowID == uDstAccountID;
bool bBalanceZero = sleRippleState->getIValueFieldAmount(sfBalance).isZero();
bool bBalanceZero = !sleRippleState->getIValueFieldAmount(sfBalance);
STAmount saDstLimit = sleRippleState->getIValueFieldAmount(bSendLow ? sfLowLimit : sfHighLimit);
bool bDstLimitZero = saDstLimit.isZero();
bool bDstLimitZero = !saDstLimit;
assert(bLow || bHigh);
@@ -1595,7 +1595,7 @@ TransactionEngineResult TransactionEngine::doCreditSet(const SerializedTransacti
Log(lsINFO) << "doCreditSet: Modifying ripple line: bDelIndex=" << bDelIndex;
}
// Line does not exist.
else if (saLimitAmount.isZero())
else if (!saLimitAmount)
{
Log(lsINFO) << "doCreditSet: Redundant: Setting non-existant ripple line to 0.";
@@ -1829,12 +1829,6 @@ void TransactionEngine::calcOfferBridgeNext(
STAmount saOfferPays = sleOffer->getIValueFieldAmount(sfTakerGets);
STAmount saOfferGets = sleOffer->getIValueFieldAmount(sfTakerPays);
if (sleOffer->getIFieldPresent(sfGetsIssuer))
saOfferPays.setIssuer(sleOffer->getIValueFieldAccount(sfGetsIssuer).getAccountID());
if (sleOffer->getIFieldPresent(sfPaysIssuer))
saOfferGets.setIssuer(sleOffer->getIValueFieldAccount(sfPaysIssuer).getAccountID());
if (sleOffer->getIFieldPresent(sfExpiration) && sleOffer->getIFieldU32(sfExpiration) <= mLedger->getParentCloseTimeNC())
{
// Offer is expired.
@@ -1986,14 +1980,8 @@ bool TransactionEngine::calcNodeOfferRev(
SLE::pointer sleCurOfr = entryCache(ltOFFER, uCurIndex);
uint160 uCurOfrAccountID = sleCurOfr->getIValueFieldAccount(sfAccount).getAccountID();
STAmount saCurOfrOutReq = sleCurOfr->getIValueFieldAmount(sfTakerGets);
STAmount saCurOfrIn = sleCurOfr->getIValueFieldAmount(sfTakerPays);
// XXX Move issuer into STAmount
if (sleCurOfr->getIFieldPresent(sfGetsIssuer))
saCurOfrOutReq.setIssuer(sleCurOfr->getIValueFieldAccount(sfGetsIssuer).getAccountID());
if (sleCurOfr->getIFieldPresent(sfPaysIssuer))
saCurOfrIn.setIssuer(sleCurOfr->getIValueFieldAccount(sfPaysIssuer).getAccountID());
const STAmount& saCurOfrOutReq = sleCurOfr->getIValueFieldAmount(sfTakerGets);
// UNUSED? const STAmount& saCurOfrIn = sleCurOfr->getIValueFieldAmount(sfTakerPays);
STAmount saCurOfrFunds = accountFunds(uCurOfrAccountID, saCurOfrOutReq); // Funds left.
@@ -2070,10 +2058,7 @@ bool TransactionEngine::calcNodeOfferRev(
// YYY This could combine offers with the same fee before doing math.
SLE::pointer sleNxtOfr = entryCache(ltOFFER, uNxtIndex);
uint160 uNxtOfrAccountID = sleNxtOfr->getIValueFieldAccount(sfAccount).getAccountID();
STAmount saNxtOfrIn = sleNxtOfr->getIValueFieldAmount(sfTakerPays);
// XXX Move issuer into STAmount
if (sleNxtOfr->getIFieldPresent(sfPaysIssuer))
saNxtOfrIn.setIssuer(sleCurOfr->getIValueFieldAccount(sfPaysIssuer).getAccountID());
const STAmount& saNxtOfrIn = sleNxtOfr->getIValueFieldAmount(sfTakerPays);
STAmount saFeeRate = uCurOfrAccountID == uCurIssuerID || uNxtOfrAccountID == uCurIssuerID
? saOne
@@ -2183,18 +2168,11 @@ bool TransactionEngine::calcNodeOfferFwd(
{
SLE::pointer sleCurOfr = entryCache(ltOFFER, uCurIndex);
uint160 uCurOfrAccountID = sleCurOfr->getIValueFieldAccount(sfAccount).getAccountID();
STAmount saCurOfrOutReq = sleCurOfr->getIValueFieldAmount(sfTakerGets);
STAmount saCurOfrInReq = sleCurOfr->getIValueFieldAmount(sfTakerPays);
// XXX Move issuer into STAmount
if (sleCurOfr->getIFieldPresent(sfGetsIssuer))
saCurOfrOutReq.setIssuer(sleCurOfr->getIValueFieldAccount(sfGetsIssuer).getAccountID());
if (sleCurOfr->getIFieldPresent(sfPaysIssuer))
saCurOfrInReq.setIssuer(sleCurOfr->getIValueFieldAccount(sfPaysIssuer).getAccountID());
const STAmount& saCurOfrOutReq = sleCurOfr->getIValueFieldAmount(sfTakerGets);
const STAmount& saCurOfrInReq = sleCurOfr->getIValueFieldAmount(sfTakerPays);
STAmount saCurOfrInAct;
STAmount saCurOfrFunds = accountFunds(uCurOfrAccountID, saCurOfrOutReq); // Funds left.
saCurOfrInReq = MIN(saCurOfrInReq, saPrvDlvReq-saPrvDlvAct);
STAmount saCurOfrInMax = MIN(saCurOfrInReq, saPrvDlvReq-saPrvDlvAct);
if (!!uNxtAccountID)
{
@@ -2202,10 +2180,10 @@ bool TransactionEngine::calcNodeOfferFwd(
const STAmount saFeeRate = uCurOfrAccountID == uCurIssuerID || uNxtAccountID == uCurIssuerID
? saOne
: saTransferRate;
: saTransferRate;
const bool bFee = saFeeRate != saOne;
const STAmount saOutPass = STAmount::divide(saCurOfrInReq, saOfrRate, uCurCurrencyID);
const STAmount saOutPass = STAmount::divide(saCurOfrInMax, saOfrRate, uCurCurrencyID);
const STAmount saOutBase = MIN(saCurOfrOutReq, saOutPass); // Limit offer out by needed.
const STAmount saOutCost = MIN(
bFee
@@ -2255,17 +2233,14 @@ bool TransactionEngine::calcNodeOfferFwd(
// YYY This could combine offers with the same fee before doing math.
SLE::pointer sleNxtOfr = entryCache(ltOFFER, uNxtIndex);
const uint160 uNxtOfrAccountID = sleNxtOfr->getIValueFieldAccount(sfAccount).getAccountID();
STAmount saNxtOfrIn = sleNxtOfr->getIValueFieldAmount(sfTakerPays);
// XXX Move issuer into STAmount
if (sleNxtOfr->getIFieldPresent(sfPaysIssuer))
saNxtOfrIn.setIssuer(sleCurOfr->getIValueFieldAccount(sfPaysIssuer).getAccountID());
const STAmount& saNxtOfrIn = sleNxtOfr->getIValueFieldAmount(sfTakerPays);
const STAmount saFeeRate = uCurOfrAccountID == uCurIssuerID || uNxtOfrAccountID == uCurIssuerID
? saOne
: saTransferRate;
const bool bFee = saFeeRate != saOne;
const STAmount saInBase = saCurOfrInReq-saCurOfrInAct;
const STAmount saInBase = saCurOfrInMax-saCurOfrInAct;
const STAmount saOutPass = STAmount::divide(saInBase, saOfrRate, uCurCurrencyID);
STAmount saOutBase = MIN(saCurOfrOutReq, saOutPass); // Limit offer out by needed.
saOutBase = MIN(saOutBase, saNxtOfrIn); // Limit offer out by supplying offer.
@@ -3772,7 +3747,7 @@ TransactionEngineResult TransactionEngine::doPayment(const SerializedTransaction
return terOVER_LIMIT;
}
if (saDstBalance.isZero())
if (!saDstBalance)
{
// XXX May be able to delete indexes for credit limits which are zero.
nothing();
@@ -3916,7 +3891,7 @@ TransactionEngineResult TransactionEngine::doPayment(const SerializedTransaction
// Partial payment not allowed.
terResult = terPATH_PARTIAL; // XXX No effect, except unfunded and charge fee.
}
else if (saPaid.isZero())
else if (!saPaid)
{
// Nothing claimed.
terResult = terPATH_EMPTY; // XXX No effect except unfundeds and charge fee.
@@ -4022,7 +3997,7 @@ TransactionEngineResult TransactionEngine::takeOffers(
STAmount& saTakerPaid,
STAmount& saTakerGot)
{
assert(!saTakerPays.isZero() && !saTakerGets.isZero());
assert(!!saTakerPays && !!saTakerGets);
Log(lsINFO) << "takeOffers: against book: " << uBookBase.ToString();
@@ -4091,12 +4066,6 @@ TransactionEngineResult TransactionEngine::takeOffers(
STAmount saOfferPays = sleOffer->getIValueFieldAmount(sfTakerGets);
STAmount saOfferGets = sleOffer->getIValueFieldAmount(sfTakerPays);
if (sleOffer->getIFieldPresent(sfGetsIssuer))
saOfferPays.setIssuer(sleOffer->getIValueFieldAccount(sfGetsIssuer).getAccountID());
if (sleOffer->getIFieldPresent(sfPaysIssuer))
saOfferGets.setIssuer(sleOffer->getIValueFieldAccount(sfPaysIssuer).getAccountID());
if (sleOffer->getIFieldPresent(sfExpiration) && sleOffer->getIFieldU32(sfExpiration) <= mLedger->getParentCloseTimeNC())
{
// Offer is expired. Delete it.
@@ -4210,14 +4179,12 @@ TransactionEngineResult TransactionEngine::doOfferCreate(const SerializedTransac
Log(lsWARNING) << "doOfferCreate> " << txn.getJson(0);
const uint32 txFlags = txn.getFlags();
const bool bPassive = !!(txFlags & tfPassive);
const uint160 uPaysIssuerID = txn.getITFieldAccount(sfPaysIssuer);
const uint160 uGetsIssuerID = txn.getITFieldAccount(sfGetsIssuer);
STAmount saTakerPays = txn.getITFieldAmount(sfTakerPays);
saTakerPays.setIssuer(uPaysIssuerID);
Log(lsWARNING) << "doOfferCreate: saTakerPays=" << saTakerPays.getFullText();
STAmount saTakerPays = txn.getITFieldAmount(sfTakerPays);
STAmount saTakerGets = txn.getITFieldAmount(sfTakerGets);
saTakerGets.setIssuer(uGetsIssuerID);
Log(lsWARNING) << "doOfferCreate: saTakerPays=" << saTakerPays.getFullText();
Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
const uint160 uPaysIssuerID = saTakerPays.getIssuer();
const uint160 uGetsIssuerID = saTakerGets.getIssuer();
const uint32 uExpiration = txn.getITFieldU32(sfExpiration);
const bool bHaveExpiration = txn.getITFieldPresent(sfExpiration);
const uint32 uSequence = txn.getSequence();
@@ -4254,7 +4221,7 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
terResult = tenBAD_OFFER;
}
else if (saTakerPays.isZero() || saTakerGets.isZero())
else if (!saTakerPays || !saTakerGets)
{
Log(lsWARNING) << "doOfferCreate: Malformed offer: bad amount";
@@ -4266,7 +4233,7 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
terResult = tenREDUNDANT;
}
else if (saTakerPays.isNative() != uPaysIssuerID.isZero() || saTakerGets.isNative() != uGetsIssuerID.isZero())
else if (saTakerPays.isNative() != !uPaysIssuerID || saTakerGets.isNative() != !uGetsIssuerID)
{
Log(lsWARNING) << "doOfferCreate: Malformed offer: bad issuer";
@@ -4339,8 +4306,8 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
// Log(lsWARNING) << "doOfferCreate: takeOffers: uGetsIssuerID=" << NewcoinAddress::createHumanAccountID(uGetsIssuerID);
if (terSUCCESS == terResult
&& !saTakerPays.isZero() // Still wanting something.
&& !saTakerGets.isZero() // Still offering something.
&& !!saTakerPays // Still wanting something.
&& !!saTakerGets // Still offering something.
&& accountFunds(mTxnAccountID, saTakerGets).isPositive()) // Still funded.
{
// We need to place the remainder of the offer into its order book.
@@ -4382,12 +4349,6 @@ Log(lsWARNING) << "doOfferCreate: saTakerGets=" << saTakerGets.getFullText();
sleOffer->setIFieldU64(sfOwnerNode, uOwnerNode);
sleOffer->setIFieldU64(sfBookNode, uBookNode);
if (!saTakerPays.isNative())
sleOffer->setIFieldAccount(sfPaysIssuer, uPaysIssuerID);
if (!saTakerGets.isNative())
sleOffer->setIFieldAccount(sfGetsIssuer, uGetsIssuerID);
if (uExpiration)
sleOffer->setIFieldU32(sfExpiration, uExpiration);

View File

@@ -61,9 +61,7 @@ TransactionFormat InnerTxnFormats[]=
{ S_FIELD(TakerPays), STI_AMOUNT, SOE_REQUIRED, 0 },
{ S_FIELD(TakerGets), STI_AMOUNT, SOE_REQUIRED, 0 },
{ S_FIELD(SourceTag), STI_UINT32, SOE_IFFLAG, 1 },
{ S_FIELD(PaysIssuer), STI_ACCOUNT, SOE_IFFLAG, 2 },
{ S_FIELD(GetsIssuer), STI_ACCOUNT, SOE_IFFLAG, 4 },
{ S_FIELD(Expiration), STI_UINT32, SOE_IFFLAG, 8 },
{ S_FIELD(Expiration), STI_UINT32, SOE_IFFLAG, 2 },
{ S_FIELD(Extensions), STI_TL, SOE_IFFLAG, 0x02000000 },
{ sfInvalid, NULL, STI_DONE, SOE_NEVER, -1 } }
},

View File

@@ -4,10 +4,10 @@
// Versions
//
#define SERVER_VERSION_MAJOR 0
#define SERVER_VERSION_MINOR 4
#define SERVER_VERSION_SUB "-a"
#define SERVER_NAME "NewCoin"
#define SERVER_VERSION_MAJOR 0
#define SERVER_VERSION_MINOR 4
#define SERVER_VERSION_SUB "-a"
#define SERVER_NAME "NewCoin"
#define SV_STRINGIZE(x) SV_STRINGIZE2(x)
#define SV_STRINGIZE2(x) #x
@@ -16,11 +16,11 @@
// Version we prefer to speak:
#define PROTO_VERSION_MAJOR 0
#define PROTO_VERSION_MINOR 5
#define PROTO_VERSION_MINOR 6
// Version we wil speak to:
// Version we will speak to:
#define MIN_PROTO_MAJOR 0
#define MIN_PROTO_MINOR 5
#define MIN_PROTO_MINOR 6
#define MAKE_VERSION_INT(maj,min) ((maj << 16) | min)
#define GET_VERSION_MAJOR(ver) (ver >> 16)