From 1c5481bad7b168b142a1d89fc9b19412a9db8ed5 Mon Sep 17 00:00:00 2001 From: jed Date: Mon, 20 Aug 2012 11:53:43 -0700 Subject: [PATCH] forgot some functions --- src/Ledger.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/src/Ledger.cpp b/src/Ledger.cpp index 99ce3986de..739947d933 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -694,6 +694,146 @@ SLE::pointer Ledger::getRippleState(LedgerStateParms& parms, const uint256& uNod return getASNode(parms, uNode, ltRIPPLE_STATE); } +// For an entry put in the 64 bit index or quality. +uint256 Ledger::getQualityIndex(const uint256& uBase, const uint64 uNodeDir) +{ + // Indexes are stored in big endian format: they print as hex as stored. + // Most significant bytes are first. Least significant bytes represent adjacent entries. + // We place uNodeDir in the 8 right most bytes to be adjacent. + // Want uNodeDir in big endian format so ++ goes to the next entry for indexes. + uint256 uNode(uBase); + + ((uint64*) uNode.end())[-1] = htobe64(uNodeDir); + + return uNode; +} + +// Return the last 64 bits. +uint64 Ledger::getQuality(const uint256& uBase) +{ + return be64toh(((uint64*) uBase.end())[-1]); +} + +uint256 Ledger::getQualityNext(const uint256& uBase) +{ + static uint256 uNext("10000000000000000"); + + uint256 uResult = uBase; + + uResult += uNext; + + return uResult; +} + +uint256 Ledger::getAccountRootIndex(const uint160& uAccountID) +{ + Serializer s(22); + + s.add16(spaceAccount); // 2 + s.add160(uAccountID); // 20 + + return s.getSHA512Half(); +} + +uint256 Ledger::getBookBase(const uint160& uTakerPaysCurrency, const uint160& uTakerPaysIssuerID, + const uint160& uTakerGetsCurrency, const uint160& uTakerGetsIssuerID) +{ + bool bInNative = uTakerPaysCurrency.isZero(); + bool bOutNative = uTakerGetsCurrency.isZero(); + + assert(!bInNative || !bOutNative); // Stamps to stamps not allowed. + assert(bInNative == uTakerPaysIssuerID.isZero()); // Make sure issuer is specified as needed. + assert(bOutNative == uTakerGetsIssuerID.isZero()); // Make sure issuer is specified as needed. + assert(uTakerPaysCurrency != uTakerGetsCurrency || uTakerPaysIssuerID != uTakerGetsIssuerID); // Currencies or accounts must differ. + + Serializer s(82); + + s.add16(spaceBookDir); // 2 + s.add160(uTakerPaysCurrency); // 20 + s.add160(uTakerGetsCurrency); // 20 + s.add160(uTakerPaysIssuerID); // 20 + s.add160(uTakerGetsIssuerID); // 20 + + return getQualityIndex(s.getSHA512Half()); // Return with quality 0. +} + +uint256 Ledger::getDirNodeIndex(const uint256& uDirRoot, const uint64 uNodeIndex) +{ + if (uNodeIndex) + { + Serializer s(42); + + s.add16(spaceDirNode); // 2 + s.add256(uDirRoot); // 32 + s.add64(uNodeIndex); // 8 + + return s.getSHA512Half(); + } + else + { + return uDirRoot; + } +} + +uint256 Ledger::getGeneratorIndex(const uint160& uGeneratorID) +{ + Serializer s(22); + + s.add16(spaceGenerator); // 2 + s.add160(uGeneratorID); // 20 + + return s.getSHA512Half(); +} + +// What is important: +// --> uNickname: is a Sha256 +// <-- SHA512/2: for consistency and speed in generating indexes. +uint256 Ledger::getNicknameIndex(const uint256& uNickname) +{ + Serializer s(34); + + s.add16(spaceNickname); // 2 + s.add256(uNickname); // 32 + + return s.getSHA512Half(); +} + +uint256 Ledger::getOfferIndex(const uint160& uAccountID, uint32 uSequence) +{ + Serializer s(26); + + s.add16(spaceOffer); // 2 + s.add160(uAccountID); // 20 + s.add32(uSequence); // 4 + + return s.getSHA512Half(); +} + +uint256 Ledger::getOwnerDirIndex(const uint160& uAccountID) +{ + Serializer s(22); + + s.add16(spaceOwnerDir); // 2 + s.add160(uAccountID); // 20 + + return s.getSHA512Half(); +} + +uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddress& naB, const uint160& uCurrency) +{ + uint160 uAID = naA.getAccountID(); + uint160 uBID = naB.getAccountID(); + bool bAltB = uAID < uBID; + Serializer s(62); + + s.add16(spaceRipple); // 2 + s.add160(bAltB ? uAID : uBID); // 20 + s.add160(bAltB ? uBID : uAID); // 20 + s.add160(uCurrency); // 20 + + return s.getSHA512Half(); +} + // vim:ts=4