Rework Ledger index calculation.

This commit is contained in:
Arthur Britto
2012-05-31 20:26:14 -07:00
parent e10505d2a7
commit a0009f7c6f
8 changed files with 66 additions and 47 deletions

View File

@@ -144,8 +144,7 @@ public:
static Ledger::pointer loadByHash(const uint256& ledgerHash);
// index calculation functions
static uint256 getAccountRootIndex(const uint160& account)
{ return uint160extend256(account, lnsAccounts); } // Index is accountID extended to 256 bits
static uint256 getAccountRootIndex(const uint160& uAccountID);
static uint256 getAccountRootIndex(const NewcoinAddress& account)
{ return getAccountRootIndex(account.getAccountID()); }
@@ -156,8 +155,7 @@ public:
SLE::pointer getGenerator(LedgerStateParms& parms, const uint160& uGeneratorID);
static uint256 getGeneratorIndex(const uint160& uGeneratorID)
{ return uint160extend256(uGeneratorID, lnsGenerator); } // Index is the generator ID extended to 256 bits in namespace 1
static uint256 getGeneratorIndex(const uint160& uGeneratorID);
//
// Ripple functions
@@ -169,6 +167,7 @@ public:
static uint256 getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddress& naB)
{ return getRippleStateIndex(naA, naB, uint160()); }
static uint256 getRippleDirIndex(const uint160& uAccountID);
RippleState::pointer getRippleState(const uint256& uNode);
SLE::pointer getRippleState(LedgerStateParms& parms, const uint256& uNode);
@@ -194,7 +193,7 @@ public:
// Directory functions
//
static uint256 getDirIndex(const uint256& uBase, const LedgerEntryType letKind, const uint64 uNodeDir=0);
static uint256 getDirIndex(const uint256& uBase, const uint64 uNodeDir=0);
SLE::pointer getDirRoot(LedgerStateParms& parms, const uint256& uRootIndex);
SLE::pointer getDirNode(LedgerStateParms& parms, const uint256& uNodeIndex);

View File

@@ -3,6 +3,7 @@
#include "SerializedObject.h"
// Used as the type of a transaction or the type of a ledger entry.
enum LedgerEntryType
{
ltINVALID = -1,
@@ -14,15 +15,19 @@ enum LedgerEntryType
ltNICKNAME
};
// In the format 160 + namespace + other.
// Used as a prefix for computing ledger indexes (keys).
enum LedgerNameSpace
{
lnsGenerator = -1,
lnsAccounts,
lnsRipple,
lnsBonds,
lnsInvoices,
lnsMultiSig
spaceAccount,
spaceGenerator,
spaceNickname,
spaceRipple,
spaceRippleDir,
spaceOffer,
spaceOfferDir,
spaceBond,
spaceInvoice,
spaceMultiSig,
};
enum LedgerSpecificFlags

View File

@@ -1,6 +1,16 @@
#include "Ledger.h"
uint256 Ledger::getAccountRootIndex(const uint160& uAccountID)
{
Serializer s;
s.add8(spaceAccount);
s.add160(uAccountID);
return s.getSHA512Half();
}
uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddress& naB, const uint160& uCurrency)
{
uint160 uAID = naA.getAccountID();
@@ -8,6 +18,7 @@ uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddr
bool bAltB = uAID < uBID;
Serializer s;
s.add8(spaceRipple);
s.add160(bAltB ? uAID : uBID);
s.add160(bAltB ? uBID : uAID);
s.add160(uCurrency);
@@ -15,6 +26,26 @@ uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddr
return s.getSHA512Half();
}
uint256 Ledger::getRippleDirIndex(const uint160& uAccountID)
{
Serializer s;
s.add8(spaceRippleDir);
s.add160(uAccountID);
return s.getSHA512Half();
}
uint256 Ledger::getGeneratorIndex(const uint160& uGeneratorID)
{
Serializer s;
s.add8(spaceGenerator);
s.add160(uGeneratorID);
return s.getSHA512Half();
}
uint160 Ledger::getOfferBase(const uint160& currencyIn, const uint160& accountIn,
const uint160& currencyOut, const uint160& accountOut)
{

View File

@@ -138,28 +138,18 @@ SerializedLedgerEntry::pointer Ledger::getRippleState(LedgerStateParms& parms, c
// Directory
//
uint256 Ledger::getDirIndex(const uint256& uBase, const LedgerEntryType letKind, const uint64 uNodeDir)
// For a directory entry put in the 64 bit index or quality.
uint256 Ledger::getDirIndex(const uint256& uBase, const uint64 uNodeDir)
{
// Indexes are stored in little endian format.
// The low bytes are indexed first, so when printed as a hex stream the hex is in byte order.
// Therefore, we place uNodeDir in the 8 right most bytes.
Serializer sKey;
// Indexes are stored in big endian format: they print as hex as stored.
// Most significant bytes are first. Least significant bytes repesent adjcent entries.
// We place uNodeDir in the 8 right most bytes to be adjcent.
// Want uNodeDir in big endian format so ++ goes to the next entry for indexes.
uint256 uNode(uBase);
sKey.add256(uBase);
sKey.add8(letKind);
((uint64*) uNode.end())[-1] = htobe64(uNodeDir);
uint256 uResult = sKey.getSHA512Half();
Serializer sNode; // Put in a fixed byte order: BIG. YYY
sNode.add64(uNodeDir);
// YYY SLOPPY
std::vector<unsigned char> vucData = sNode.getData();
std::copy(vucData.begin(), vucData.end(), uResult.end()-(64/8));
return uResult;
return uNode;
}
SerializedLedgerEntry::pointer Ledger::getDirRoot(LedgerStateParms& parms, const uint256& uRootIndex)

View File

@@ -172,11 +172,10 @@ SLE::pointer NetworkOPs::getGenerator(const uint256& uLedger, const uint160& uGe
bool NetworkOPs::getDirInfo(
const uint256& uLedger,
const uint256& uBase,
const LedgerEntryType letKind,
uint256& uDirLineNodeFirst,
uint256& uDirLineNodeLast)
{
uint256 uRootIndex = Ledger::getDirIndex(uBase, letKind);
uint256 uRootIndex = Ledger::getDirIndex(uBase, 0);
LedgerStateParms lspRoot = lepNONE;
SLE::pointer sleRoot = mLedgerMaster->getLedgerByHash(uLedger)->getDirRoot(lspRoot, uRootIndex);
@@ -186,8 +185,8 @@ bool NetworkOPs::getDirInfo(
std::cerr << "getDirInfo: first: " << strHex(sleRoot->getIFieldU64(sfFirstNode)) << std::endl;
std::cerr << "getDirInfo: last: " << strHex(sleRoot->getIFieldU64(sfLastNode)) << std::endl;
uDirLineNodeFirst = Ledger::getDirIndex(uBase, letKind, sleRoot->getIFieldU64(sfFirstNode));
uDirLineNodeLast = Ledger::getDirIndex(uBase, letKind, sleRoot->getIFieldU64(sfLastNode));
uDirLineNodeFirst = Ledger::getDirIndex(uBase, sleRoot->getIFieldU64(sfFirstNode));
uDirLineNodeLast = Ledger::getDirIndex(uBase, sleRoot->getIFieldU64(sfLastNode));
std::cerr << "getDirInfo: first: " << uDirLineNodeFirst.ToString() << std::endl;
std::cerr << "getDirInfo: last: " << uDirLineNodeLast.ToString() << std::endl;

View File

@@ -73,7 +73,6 @@ public:
//
bool getDirInfo(const uint256& uLedger, const uint256& uBase,
const LedgerEntryType letKind,
uint256& uDirNodeFirst, uint256& uDirNodeLast);
STVector256 getDirNode(const uint256& uLedger, const uint256& uDirLineNode);
@@ -82,7 +81,7 @@ public:
//
bool getDirLineInfo(const uint256& uLedger, const NewcoinAddress& naAccount, uint256& uDirLineNodeFirst, uint256& uDirLineNodeLast)
{ return getDirInfo(uLedger, uint160extend256(naAccount.getAccountID(), 0), ltRIPPLE_STATE, uDirLineNodeFirst, uDirLineNodeLast); }
{ return getDirInfo(uLedger, Ledger::getRippleDirIndex(naAccount.getAccountID()), uDirLineNodeFirst, uDirLineNodeLast); }
RippleState::pointer getRippleState(const uint256& uLedger, const uint256& uIndex);

View File

@@ -14,12 +14,11 @@ typedef SerializedLedgerEntry SLE;
TransactionEngineResult TransactionEngine::dirAdd(
std::vector<AffectedAccount>& accounts,
uint64& uNodeDir,
const LedgerEntryType letKind,
const uint256& uBase,
const uint256& uLedgerIndex)
{
// Get the root.
uint256 uRootIndex = Ledger::getDirIndex(uBase, letKind);
uint256 uRootIndex = Ledger::getDirIndex(uBase, 0);
LedgerStateParms lspRoot = lepNONE;
SLE::pointer sleRoot = mLedger->getDirRoot(lspRoot, uRootIndex);
bool bRootNew;
@@ -50,7 +49,7 @@ TransactionEngineResult TransactionEngine::dirAdd(
}
// Get the last node.
uint256 uNodeIndex = Ledger::getDirIndex(uBase, letKind, uNodeDir);
uint256 uNodeIndex = Ledger::getDirIndex(uBase, uNodeDir);
LedgerStateParms lspNode = lepNONE;
SLE::pointer sleNode = bRootNew ? SLE::pointer() : mLedger->getDirNode(lspNode, uNodeIndex);
@@ -111,12 +110,11 @@ TransactionEngineResult TransactionEngine::dirAdd(
TransactionEngineResult TransactionEngine::dirDelete(
std::vector<AffectedAccount>& accounts,
const uint64& uNodeDir,
const LedgerEntryType letKind,
const uint256& uBase,
const uint256& uLedgerIndex)
{
uint64 uNodeCur = uNodeDir;
uint256 uNodeIndex = Ledger::getDirIndex(uBase, letKind, uNodeCur);
uint256 uNodeIndex = Ledger::getDirIndex(uBase, uNodeCur);
LedgerStateParms lspNode = lepNONE;
SLE::pointer sleNode = mLedger->getDirNode(lspNode, uNodeIndex);
@@ -141,7 +139,7 @@ TransactionEngineResult TransactionEngine::dirDelete(
{
// Get root information
LedgerStateParms lspRoot = lepNONE;
SLE::pointer sleRoot = mLedger->getDirRoot(lspRoot, Ledger::getDirIndex(uBase, letKind));
SLE::pointer sleRoot = mLedger->getDirRoot(lspRoot, Ledger::getDirIndex(uBase, 0));
if (!sleRoot)
{
@@ -197,7 +195,7 @@ TransactionEngineResult TransactionEngine::dirDelete(
// Get replacement node.
lspNode = lepNONE;
sleNode = mLedger->getDirNode(lspNode, Ledger::getDirIndex(uBase, letKind, uNodeCur));
sleNode = mLedger->getDirNode(lspNode, Ledger::getDirIndex(uBase, uNodeCur));
svIndexes = sleNode->getIFieldV256(sfIndexes);
}
}
@@ -640,7 +638,7 @@ TransactionEngineResult TransactionEngine::doCreditSet(const SerializedTransacti
// XXX Verify extend is passing the right bits, not the zero bits.
// XXX Make dirAdd more flexiable to take vector.
terResult = dirAdd(accounts, uSrcRef, ltRIPPLE_STATE, uint160extend256(uSrcAccountID, 0), sleRippleState->getIndex());
terResult = dirAdd(accounts, uSrcRef, Ledger::getRippleDirIndex(uSrcAccountID), sleRippleState->getIndex());
}
std::cerr << "doCreditSet<" << std::endl;

View File

@@ -75,14 +75,12 @@ private:
TransactionEngineResult dirAdd(
std::vector<AffectedAccount>& accounts,
uint64& uNodeDir, // Node of entry.
const LedgerEntryType letKind,
const uint256& uBase,
const uint256& uLedgerIndex);
TransactionEngineResult dirDelete(
std::vector<AffectedAccount>& accounts,
const uint64& uNodeDir, // Node item is mentioned in.
const LedgerEntryType letKind,
const uint256& uBase, // Key of item.
const uint256& uLedgerIndex); // Item being deleted