From dc3571184aa1627660ae940c64fa691e9eaa3ac7 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 28 Nov 2016 15:56:08 -0800 Subject: [PATCH] Add a flag to the ledger for SHAMapV2 This will allow code that looks at the ledger header to know what version the SHAMap uses. This is helpful for code that rebuilds ledger binary structures from the leaves. --- Builds/VisualStudio2015/RippleD.vcxproj | 4 ++ .../VisualStudio2015/RippleD.vcxproj.filters | 3 + src/ripple/app/ledger/Ledger.cpp | 13 +++- src/ripple/ledger/ReadView.h | 11 ++- src/test/ledger/SHAMapV2_test.cpp | 71 +++++++++++++++++++ src/unity/ledger_test_unity.cpp | 3 +- 6 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 src/test/ledger/SHAMapV2_test.cpp diff --git a/Builds/VisualStudio2015/RippleD.vcxproj b/Builds/VisualStudio2015/RippleD.vcxproj index 145c44a454..97fe368547 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj +++ b/Builds/VisualStudio2015/RippleD.vcxproj @@ -4628,6 +4628,10 @@ True True + + True + True + True True diff --git a/Builds/VisualStudio2015/RippleD.vcxproj.filters b/Builds/VisualStudio2015/RippleD.vcxproj.filters index c921aae40a..f7bf7fa95d 100644 --- a/Builds/VisualStudio2015/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2015/RippleD.vcxproj.filters @@ -5352,6 +5352,9 @@ test\ledger + + test\ledger + test\ledger diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index aec258d189..4e77b31e21 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -217,9 +217,11 @@ Ledger::Ledger ( beast::Journal j) : mImmutable (true) , txMap_ (std::make_shared (SHAMapType::TRANSACTION, - info.txHash, family, SHAMap::version{1})) + info.txHash, family, + SHAMap::version{getSHAMapV2(info) ? 2 : 1})) , stateMap_ (std::make_shared (SHAMapType::STATE, - info.accountHash, family, SHAMap::version{1})) + info.accountHash, family, + SHAMap::version{getSHAMapV2(info) ? 2 : 1})) , info_ (info) { loaded = true; @@ -273,6 +275,12 @@ Ledger::Ledger (Ledger const& prevLedger, info_.closeTimeResolution = getNextLedgerTimeResolution( prevLedger.info_.closeTimeResolution, getCloseAgree(prevLedger.info()), info_.seq); + + if (stateMap_->get_version() == SHAMap::version{2}) + { + info_.closeFlags |= sLCF_SHAMapV2; + } + if (prevLedger.info_.closeTime == NetClock::time_point{}) { info_.closeTime = roundCloseTime(closeTime, info_.closeTimeResolution); @@ -1053,6 +1061,7 @@ Ledger::make_v2() info_.accountHash = stateMap_->getHash ().as_uint256(); info_.txHash = txMap_->getHash ().as_uint256(); info_.hash = calculateLedgerHash (info_); + info_.closeFlags |= sLCF_SHAMapV2; } void diff --git a/src/ripple/ledger/ReadView.h b/src/ripple/ledger/ReadView.h index b313bce1fe..3ad05b4f60 100644 --- a/src/ripple/ledger/ReadView.h +++ b/src/ripple/ledger/ReadView.h @@ -416,7 +416,10 @@ public: // ledger close flags static -std::uint32_t const sLCF_NoConsensusTime = 1; +std::uint32_t const sLCF_NoConsensusTime = 0x01; + +static +std::uint32_t const sLCF_SHAMapV2 = 0x02; inline bool getCloseAgree (LedgerInfo const& info) @@ -424,6 +427,12 @@ bool getCloseAgree (LedgerInfo const& info) return (info.closeFlags & sLCF_NoConsensusTime) == 0; } +inline +bool getSHAMapV2 (LedgerInfo const& info) +{ + return (info.closeFlags & sLCF_SHAMapV2) != 0; +} + void addRaw (LedgerInfo const&, Serializer&); } // ripple diff --git a/src/test/ledger/SHAMapV2_test.cpp b/src/test/ledger/SHAMapV2_test.cpp new file mode 100644 index 0000000000..740453c71f --- /dev/null +++ b/src/test/ledger/SHAMapV2_test.cpp @@ -0,0 +1,71 @@ +//----------------------------------------------------------------------------- +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2015 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include +#include +#include +#include + +namespace ripple { +namespace test { + +// Test that converting a ledger to SHAMapV2 +// works as expected + +class SHAMapV2_test : public beast::unit_test::suite +{ + void + testSHAMapV2() + { + jtx::Env env(*this); + Config config; + + auto ledger = + std::make_shared(create_genesis, config, env.app().family()); + BEAST_EXPECT(! getSHAMapV2 (ledger->info())); + BEAST_EXPECT(ledger->stateMap().get_version() == SHAMap::version{1}); + BEAST_EXPECT(ledger->txMap().get_version() == SHAMap::version{1}); + + ledger = + std::make_shared(*ledger, NetClock::time_point{}); + BEAST_EXPECT(! getSHAMapV2 (ledger->info())); + BEAST_EXPECT(ledger->stateMap().get_version() == SHAMap::version{1}); + BEAST_EXPECT(ledger->txMap().get_version() == SHAMap::version{1}); + + ledger->make_v2(); + BEAST_EXPECT(getSHAMapV2 (ledger->info())); + BEAST_EXPECT(ledger->stateMap().get_version() == SHAMap::version{2}); + BEAST_EXPECT(ledger->txMap().get_version() == SHAMap::version{2}); + + ledger = std::make_shared(*ledger, NetClock::time_point{}); + BEAST_EXPECT(getSHAMapV2 (ledger->info())); + BEAST_EXPECT(ledger->stateMap().get_version() == SHAMap::version{2}); + BEAST_EXPECT(ledger->txMap().get_version() == SHAMap::version{2}); + } + + void run() + { + testSHAMapV2(); + } +}; + +BEAST_DEFINE_TESTSUITE(SHAMapV2,ledger,ripple); + +} // test +} // ripple diff --git a/src/unity/ledger_test_unity.cpp b/src/unity/ledger_test_unity.cpp index 7c569159ce..9496f523fd 100644 --- a/src/unity/ledger_test_unity.cpp +++ b/src/unity/ledger_test_unity.cpp @@ -22,5 +22,6 @@ #include #include #include +#include #include -#include \ No newline at end of file +#include