diff options
author | William Casarin <jb55@jb55.com> | 2020-05-01 17:31:38 -0700 |
---|---|---|
committer | William Casarin <jb55@jb55.com> | 2020-05-15 15:26:54 -0700 |
commit | 83a425d25af033086744c1c8c892015014ed46bd (patch) | |
tree | 1fd1094b3c5c37aa900b49b496e0d1c40468b7b6 /src/test | |
parent | 844d2070a2c0106bb7a54be5cad7d4da4d9cd55e (diff) | |
download | bitcoin-83a425d25af033086744c1c8c892015014ed46bd.tar.xz |
compressor: use a prevector in compressed script serialization
Use a prevector for stack allocation instead of heap allocation during
script compression and decompression. These functions were doing
millions of unnecessary heap allocations during IBD.
We introduce a CompressedScript type alias for this prevector. It is
size 33 as that is the maximum size of a compressed script.
Fix the DecompressScript header to match the variable name from
compressor.cpp
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/compress_tests.cpp | 8 | ||||
-rw-r--r-- | src/test/fuzz/script.cpp | 8 |
2 files changed, 9 insertions, 7 deletions
diff --git a/src/test/compress_tests.cpp b/src/test/compress_tests.cpp index df1a119d79..4bc301f583 100644 --- a/src/test/compress_tests.cpp +++ b/src/test/compress_tests.cpp @@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_ckey_id) CScript script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(pubkey.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG; BOOST_CHECK_EQUAL(script.size(), 25); - std::vector<unsigned char> out; + CompressedScript out; bool done = CompressScript(script, out); BOOST_CHECK_EQUAL(done, true); @@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_cscript_id) script << OP_HASH160 << ToByteVector(CScriptID(redeemScript)) << OP_EQUAL; BOOST_CHECK_EQUAL(script.size(), 23); - std::vector<unsigned char> out; + CompressedScript out; bool done = CompressScript(script, out); BOOST_CHECK_EQUAL(done, true); @@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_compressed_pubkey_id) CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // COMPRESSED_PUBLIC_KEY_SIZE (33) BOOST_CHECK_EQUAL(script.size(), 35); - std::vector<unsigned char> out; + CompressedScript out; bool done = CompressScript(script, out); BOOST_CHECK_EQUAL(done, true); @@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(compress_script_to_uncompressed_pubkey_id) CScript script = CScript() << ToByteVector(key.GetPubKey()) << OP_CHECKSIG; // PUBLIC_KEY_SIZE (65) BOOST_CHECK_EQUAL(script.size(), 67); // 1 char code + 65 char pubkey + OP_CHECKSIG - std::vector<unsigned char> out; + CompressedScript out; bool done = CompressScript(script, out); BOOST_CHECK_EQUAL(done, true); diff --git a/src/test/fuzz/script.cpp b/src/test/fuzz/script.cpp index de82122dd6..63fff7d2ba 100644 --- a/src/test/fuzz/script.cpp +++ b/src/test/fuzz/script.cpp @@ -36,7 +36,7 @@ void test_one_input(const std::vector<uint8_t>& buffer) if (!script_opt) return; const CScript script{*script_opt}; - std::vector<unsigned char> compressed; + CompressedScript compressed; if (CompressScript(script, compressed)) { const unsigned int size = compressed[0]; compressed.erase(compressed.begin()); @@ -94,10 +94,12 @@ void test_one_input(const std::vector<uint8_t>& buffer) { const std::vector<uint8_t> bytes = ConsumeRandomLengthByteVector(fuzzed_data_provider); + CompressedScript compressed_script; + compressed_script.assign(bytes.begin(), bytes.end()); // DecompressScript(..., ..., bytes) is not guaranteed to be defined if the bytes vector is too short - if (bytes.size() >= 32) { + if (compressed_script.size() >= 32) { CScript decompressed_script; - DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), bytes); + DecompressScript(decompressed_script, fuzzed_data_provider.ConsumeIntegral<unsigned int>(), compressed_script); } } |