Changeset View
Changeset View
Standalone View
Standalone View
src/test/txvalidationcache_tests.cpp
Show First 20 Lines • Show All 425 Lines • ▼ Show 20 Lines | // TODO: add tests for remaining script flags | ||||
STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
txdata, &scriptchecks)); | txdata, &scriptchecks)); | ||||
// Should get 2 script checks back -- caching is on a whole-transaction | // Should get 2 script checks back -- caching is on a whole-transaction | ||||
// basis. | // basis. | ||||
BOOST_CHECK_EQUAL(scriptchecks.size(), 2U); | BOOST_CHECK_EQUAL(scriptchecks.size(), 2U); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(scriptcache_values) { | |||||
// Test insertion and querying of keys&values from the script cache. | |||||
InitScriptExecutionCache(); | |||||
// construct four distinct keys from very slightly different data | |||||
CMutableTransaction tx1; | |||||
tx1.nVersion = 1; | |||||
CMutableTransaction tx2; | |||||
tx2.nVersion = 2; | |||||
uint32_t flagsA = 0x7fffffff; | |||||
uint32_t flagsB = 0xffffffff; | |||||
ScriptCacheKey key1A = GetScriptCacheKey(CTransaction(tx1), flagsA); | |||||
ScriptCacheKey key1B = GetScriptCacheKey(CTransaction(tx1), flagsB); | |||||
ScriptCacheKey key2A = GetScriptCacheKey(CTransaction(tx2), flagsA); | |||||
ScriptCacheKey key2B = GetScriptCacheKey(CTransaction(tx2), flagsB); | |||||
BOOST_CHECK(key1A == key1A); | |||||
BOOST_CHECK(!(key1A == key1B)); | |||||
BOOST_CHECK(!(key1A == key2A)); | |||||
BOOST_CHECK(!(key1A == key2B)); | |||||
BOOST_CHECK(key1B == key1B); | |||||
BOOST_CHECK(!(key1B == key2A)); | |||||
BOOST_CHECK(!(key1B == key2B)); | |||||
BOOST_CHECK(key2A == key2A); | |||||
BOOST_CHECK(!(key2A == key2B)); | |||||
BOOST_CHECK(key2B == key2B); | |||||
int nSigChecks = 0xfeed; | |||||
// key is not yet inserted | |||||
BOOST_CHECK(!IsKeyInScriptCache(key1A, false, nSigChecks)); | |||||
// should not modify value if not found | |||||
BOOST_CHECK(nSigChecks == 0xfeed); | |||||
deadalnix: I'm not sure this is really something you want your API to guarantee. That seems like an… | |||||
// add the key and check it worked | |||||
AddKeyInScriptCache(key1A, 42); | |||||
BOOST_CHECK(IsKeyInScriptCache(key1A, false, nSigChecks)); | |||||
BOOST_CHECK(nSigChecks == 42); | |||||
BOOST_CHECK(!IsKeyInScriptCache(key1B, false, nSigChecks)); | |||||
BOOST_CHECK(!IsKeyInScriptCache(key2A, false, nSigChecks)); | |||||
BOOST_CHECK(!IsKeyInScriptCache(key2B, false, nSigChecks)); | |||||
// add a couple more keys with value the limit of storeable range | |||||
AddKeyInScriptCache(key1B, 0); | |||||
AddKeyInScriptCache(key2A, 0xffff); | |||||
// try adding an unstoreable value | |||||
BOOST_CHECK_THROW(AddKeyInScriptCache(key2B, 0x10000), std::range_error); | |||||
BOOST_CHECK_THROW(AddKeyInScriptCache(key2B, -1), std::range_error); | |||||
// read out values again | |||||
BOOST_CHECK(IsKeyInScriptCache(key1A, false, nSigChecks)); | |||||
BOOST_CHECK(nSigChecks == 42); | |||||
deadalnixUnsubmitted Done Inline ActionsProblem here is that you don't know if the value was left alone or set to 42, because it was already 42. You are better off using a fresh int every time for this kind of tests, so they don't accidentally depend on other tests. deadalnix: Problem here is that you don't know if the value was left alone or set to 42, because it was… | |||||
BOOST_CHECK(IsKeyInScriptCache(key1B, false, nSigChecks)); | |||||
BOOST_CHECK(nSigChecks == 0); | |||||
BOOST_CHECK(IsKeyInScriptCache(key2A, false, nSigChecks)); | |||||
BOOST_CHECK(nSigChecks == 0xffff); | |||||
BOOST_CHECK(!IsKeyInScriptCache(key2B, false, nSigChecks)); | |||||
// try overwriting an existing entry with different value (should never | |||||
// happen in practice but see what happens) | |||||
AddKeyInScriptCache(key1A, 99); | |||||
BOOST_CHECK(IsKeyInScriptCache(key1A, false, nSigChecks)); | |||||
// currently, no replacement is done | |||||
BOOST_CHECK(nSigChecks == 42); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |
I'm not sure this is really something you want your API to guarantee. That seems like an implementation detail.