diff --git a/src/Ledger.cpp b/src/Ledger.cpp index e76f4a78b..1b6775232 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -885,6 +885,41 @@ uint256 Ledger::getAccountRootIndex(const uint160& uAccountID) return s.getSHA512Half(); } +uint256 Ledger::getLedgerHashIndex() +{ // get the index of the node that holds the last 256 ledgers + Serializer s(2); + s.add16(spaceHashes); + return s.getSHA512Half(); +} + +uint256 Ledger::getLedgerHashIndex(uint32 desiredLedgerIndex) +{ // get the index of the node that holds the set of 256 ledgers that includes this ledger's hash + // (or the first ledger after it if it's not a multiple of 256) + Serializer s(6); + s.add16(spaceHashes); + s.add32(desiredLedgerIndex >> 16); + return s.getSHA512Half(); +} + +int Ledger::getLedgerHashOffset(uint32 ledgerIndex) +{ // get the offset for this ledger's hash (or the first one after it) in the every-256-ledger table + return (ledgerIndex >> 8) % 256; +} + +int Ledger::getLedgerHashOffset(uint32 desiredLedgerIndex, uint32 currentLedgerIndex) +{ // get the offset for this ledger's hash in the every-ledger table, -1 if not in it + if (desiredLedgerIndex >= currentLedgerIndex) + return -1; + + if (currentLedgerIndex < 256) + return desiredLedgerIndex; + + if (desiredLedgerIndex < (currentLedgerIndex - 256)) + return -1; + + return currentLedgerIndex - desiredLedgerIndex - 1; +} + uint256 Ledger::getBookBase(const uint160& uTakerPaysCurrency, const uint160& uTakerPaysIssuerID, const uint160& uTakerGetsCurrency, const uint160& uTakerGetsIssuerID) { diff --git a/src/Ledger.h b/src/Ledger.h index f04382114..1074ec9fb 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -178,6 +178,12 @@ public: uint256 getPrevLedgerIndex(const uint256& uHash); // last node begin + // Ledger hash table function + static uint256 getLedgerHashIndex(); + static uint256 getLedgerHashIndex(uint32 desiredLedgerIndex); + static int getLedgerHashOffset(uint32 desiredLedgerIndex); + static int getLedgerHashOffset(uint32 desiredLedgerIndex, uint32 currentLedgerIndex); + // index calculation functions static uint256 getAccountRootIndex(const uint160& uAccountID); diff --git a/src/LedgerFormats.cpp b/src/LedgerFormats.cpp index c187286cd..4456ebbf0 100644 --- a/src/LedgerFormats.cpp +++ b/src/LedgerFormats.cpp @@ -86,6 +86,10 @@ static bool LEFInit() << SOElement(sfHighQualityOut, SOE_OPTIONAL) ; + DECLARE_LEF(LedgerHashes, ltLEDGER_HASHES) + << SOElement(sfHashes, SOE_REQUIRED) + ; + return true; } diff --git a/src/LedgerFormats.h b/src/LedgerFormats.h index 947cb65be..cae045f23 100644 --- a/src/LedgerFormats.h +++ b/src/LedgerFormats.h @@ -14,6 +14,7 @@ enum LedgerEntryType ltNICKNAME = 'n', ltOFFER = 'o', ltCONTRACT = 'c', + ltLEDGER_HASHES = 'h', }; // Used as a prefix for computing ledger indexes (keys). @@ -28,6 +29,7 @@ enum LedgerNameSpace spaceOwnerDir = 'O', // Directory of things owned by an account. spaceBookDir = 'B', // Directory of order books. spaceContract = 'c', + spaceHashes = 'h', }; enum LedgerSpecificFlags diff --git a/src/SerializeProto.h b/src/SerializeProto.h index 2fa8dbf00..f74413ad6 100644 --- a/src/SerializeProto.h +++ b/src/SerializeProto.h @@ -122,6 +122,7 @@ // vector of 256-bit FIELD(Indexes, VECTOR256, 1) + FIELD(Hashes, VECTOR256, 2) // inner object // OBJECT/1 is reserved for end of object