@@ -2005,6 +2005,149 @@ TEST_CASE("Valid merkle proof from receipts")
20052005 ccf::crypto::openssl_sha256_shutdown ();
20062006}
20072007
2008+ TEST_CASE (" Cache size estimation" )
2009+ {
2010+ ccf::crypto::openssl_sha256_init ();
2011+ auto state = create_and_init_state ();
2012+ auto & kv_store = *state.kv_store ;
2013+
2014+ write_transactions_and_signature (kv_store, 10 );
2015+
2016+ auto ledger = construct_host_ledger (state.kv_store ->get_consensus ());
2017+
2018+ auto stub_writer = std::make_shared<StubWriter>();
2019+ ccf::historical::StateCacheImpl cache (
2020+ kv_store, state.ledger_secrets , stub_writer);
2021+
2022+ cache.set_soft_cache_limit (0 );
2023+
2024+ ccf::historical::CompoundHandle handle = {
2025+ ccf::historical::RequestNamespace::Application, 1 };
2026+
2027+ {
2028+ ccf::ds::ContiguousSet<ccf::SeqNo> seqnos;
2029+ seqnos.insert (10 );
2030+ cache.get_stores_for (handle, seqnos, std::chrono::seconds (1 ));
2031+ }
2032+
2033+ REQUIRE (cache.get_estimated_store_cache_size () == 0 );
2034+ cache.handle_ledger_entry (10 , ledger.at (10 ));
2035+ REQUIRE (cache.get_estimated_store_cache_size () == ledger.at (10 ).size ());
2036+
2037+ {
2038+ ccf::ds::ContiguousSet<ccf::SeqNo> seqnos;
2039+ seqnos.insert (5 );
2040+ cache.get_stores_for (handle, seqnos, std::chrono::seconds (1 ));
2041+ }
2042+
2043+ cache.tick (std::chrono::milliseconds (1000 ));
2044+
2045+ REQUIRE (cache.get_estimated_store_cache_size () == 0 );
2046+ ccf::crypto::openssl_sha256_shutdown ();
2047+ }
2048+
2049+ TEST_CASE (" adjust_ranges" )
2050+ {
2051+ ccf::crypto::openssl_sha256_init ();
2052+ using SeqNoSet = std::set<ccf::SeqNo>;
2053+
2054+ struct AdjustRangesAccessor : public ccf ::historical::StateCacheImpl
2055+ {
2056+ Request request;
2057+
2058+ AdjustRangesAccessor (
2059+ ccf::kv::Store& store,
2060+ const std::shared_ptr<ccf::LedgerSecrets>& secrets,
2061+ const ringbuffer::WriterPtr& host_writer) :
2062+ StateCacheImpl (store, secrets, host_writer),
2063+ request (all_stores)
2064+ {}
2065+
2066+ std::pair<SeqNoSet, SeqNoSet> adjust_ranges (const SeqNoSet& seqnos)
2067+ {
2068+ ccf::SeqNoCollection seqno_collection;
2069+ for (const auto & seqno : seqnos)
2070+ {
2071+ seqno_collection.insert (seqno);
2072+ }
2073+
2074+ auto [removed_v, added_v] =
2075+ request.adjust_ranges (seqno_collection, true , 0 );
2076+ SeqNoSet removed (removed_v.begin (), removed_v.end ());
2077+ SeqNoSet added (added_v.begin (), added_v.end ());
2078+ return {removed, added};
2079+ }
2080+ };
2081+
2082+ auto state = create_and_init_state ();
2083+ auto stub_writer = std::make_shared<StubWriter>();
2084+
2085+ {
2086+ DOCTEST_INFO (" Minimal regression test" );
2087+ AdjustRangesAccessor cache (
2088+ *state.kv_store , state.ledger_secrets , stub_writer);
2089+
2090+ auto [removed1, added1] = cache.adjust_ranges ({100 });
2091+ REQUIRE (added1.size () == 1 );
2092+ REQUIRE (added1 == SeqNoSet{100 });
2093+ REQUIRE (removed1.size () == 0 );
2094+
2095+ auto [removed2, added2] = cache.adjust_ranges ({42 });
2096+ REQUIRE (added2.size () == 1 );
2097+ REQUIRE (added2 == SeqNoSet{42 });
2098+ REQUIRE (removed2.size () == 1 );
2099+ REQUIRE (removed2 == SeqNoSet{100 });
2100+ }
2101+
2102+ {
2103+ const auto seed = time (NULL );
2104+ DOCTEST_INFO (" Random permutations, using seed: " , seed);
2105+ srand (seed);
2106+ for (size_t i = 0 ; i < 100 ; ++i)
2107+ {
2108+ DOCTEST_INFO (" Iteration #" , i);
2109+ AdjustRangesAccessor cache (
2110+ *state.kv_store , state.ledger_secrets , stub_writer);
2111+ SeqNoSet before;
2112+ for (auto j = 0 ; j < rand () % 6 ; ++j)
2113+ {
2114+ before.insert (rand () % 30 );
2115+ }
2116+
2117+ auto [removed_init, added_init] = cache.adjust_ranges (before);
2118+ REQUIRE (added_init == before);
2119+ REQUIRE (removed_init.empty ());
2120+
2121+ std::set<ccf::SeqNo> after;
2122+ for (auto j = 0 ; j < rand () % 6 ; ++j)
2123+ {
2124+ after.insert (rand () % 30 );
2125+ }
2126+
2127+ auto [actual_removed, actual_added] = cache.adjust_ranges (after);
2128+
2129+ SeqNoSet expected_added;
2130+ std::set_difference (
2131+ after.begin (),
2132+ after.end (),
2133+ before.begin (),
2134+ before.end (),
2135+ std::inserter (expected_added, expected_added.begin ()));
2136+ SeqNoSet expected_removed;
2137+ std::set_difference (
2138+ before.begin (),
2139+ before.end (),
2140+ after.begin (),
2141+ after.end (),
2142+ std::inserter (expected_removed, expected_removed.begin ()));
2143+
2144+ REQUIRE (actual_added == expected_added);
2145+ REQUIRE (actual_removed == expected_removed);
2146+ }
2147+ }
2148+ ccf::crypto::openssl_sha256_shutdown ();
2149+ }
2150+
20082151int main (int argc, char ** argv)
20092152{
20102153 threading::ThreadMessaging::init (1 );
0 commit comments