rpc: Optimize serialization disk space of dumptxoutset

Co-authored-by: Aurèle Oulès <aurele@oules.com>
Co-authored-by: TheCharlatan <seb.kung@gmail.com>
This commit is contained in:
committed 2024-05-21 13:38:07 +02:00
1 parent eb0bdbdd75
commit de95953d87
5 files changed
+66 -24

No files matched your search

+29 -4
View File
@@ -34,6 +34,7 @@
#include <rpc/server_util.h> #include <rpc/server_util.h>
#include <rpc/util.h> #include <rpc/util.h>
#include <script/descriptor.h> #include <script/descriptor.h>
#include <serialize.h>
#include <streams.h> #include <streams.h>
#include <sync.h> #include <sync.h>
#include <txdb.h> #include <txdb.h>
@@ -2695,24 +2696,48 @@ UniValue CreateUTXOSnapshot(
afile << metadata; afile << metadata;
COutPoint key; COutPoint key;
Txid last_hash;
Coin coin; Coin coin;
unsigned int iter{0}; unsigned int iter{0};
size_t written_coins_count{0};
std::vector<std::pair<uint32_t, Coin>> coins;
auto write_coins_to_file = [&](AutoFile& afile, const Txid& last_hash, const std::vector<std::pair<uint32_t, Coin>>& coins, size_t& written_coins_count) {
afile << last_hash;
WriteCompactSize(afile, coins.size());
for (const auto& [n, coin] : coins) {
WriteCompactSize(afile, n);
afile << coin;
++written_coins_count;
}
};
pcursor->GetKey(key);
last_hash = key.hash;
while (pcursor->Valid()) { while (pcursor->Valid()) {
if (iter % 5000 == 0) node.rpc_interruption_point(); if (iter % 5000 == 0) node.rpc_interruption_point();
++iter; ++iter;
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) { if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
afile << key; if (key.hash != last_hash) {
afile << coin; write_coins_to_file(afile, last_hash, coins, written_coins_count);
last_hash = key.hash;
coins.clear();
}
coins.emplace_back(key.n, coin);
} }
pcursor->Next(); pcursor->Next();
} }
if (!coins.empty()) {
write_coins_to_file(afile, last_hash, coins, written_coins_count);
}
CHECK_NONFATAL(written_coins_count == maybe_stats->coins_count);
afile.fclose(); afile.fclose();
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
result.pushKV("coins_written", maybe_stats->coins_count); result.pushKV("coins_written", written_coins_count);
result.pushKV("base_hash", tip->GetBlockHash().ToString()); result.pushKV("base_hash", tip->GetBlockHash().ToString());
result.pushKV("base_height", tip->nHeight); result.pushKV("base_height", tip->nHeight);
result.pushKV("path", path.utf8string()); result.pushKV("path", path.utf8string());
@@ -226,10 +226,13 @@ struct SnapshotTestSetup : TestChain100Setup {
// A UTXO is missing but count is correct // A UTXO is missing but count is correct
metadata.m_coins_count -= 1; metadata.m_coins_count -= 1;
COutPoint outpoint; Txid txid;
auto_infile >> txid;
// coins size
(void)ReadCompactSize(auto_infile);
// vout index
(void)ReadCompactSize(auto_infile);
Coin coin; Coin coin;
auto_infile >> outpoint;
auto_infile >> coin; auto_infile >> coin;
})); }));
+23 -10
View File
@@ -5660,23 +5660,30 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
return false; return false;
} }
COutPoint outpoint;
Coin coin;
const uint64_t coins_count = metadata.m_coins_count; const uint64_t coins_count = metadata.m_coins_count;
uint64_t coins_left = metadata.m_coins_count; uint64_t coins_left = metadata.m_coins_count;
LogPrintf("[snapshot] loading coins from snapshot %s\n", base_blockhash.ToString()); LogPrintf("[snapshot] loading %d coins from snapshot %s\n", coins_left, base_blockhash.ToString());
int64_t coins_processed{0}; int64_t coins_processed{0};
while (coins_left > 0) { while (coins_left > 0) {
try { try {
coins_file >> outpoint; Txid txid;
coins_file >> coin; coins_file >> txid;
} catch (const std::ios_base::failure&) { size_t coins_per_txid{0};
LogPrintf("[snapshot] bad snapshot format or truncated snapshot after deserializing %d coins\n", coins_per_txid = ReadCompactSize(coins_file);
coins_count - coins_left);
if (coins_per_txid > coins_left) {
LogPrintf("[snapshot] mismatch in coins count in snapshot metadata and actual snapshot data\n");
return false; return false;
} }
for (size_t i = 0; i < coins_per_txid; i++) {
COutPoint outpoint;
Coin coin;
outpoint.n = static_cast<uint32_t>(ReadCompactSize(coins_file));
outpoint.hash = txid;
coins_file >> coin;
if (coin.nHeight > base_height || if (coin.nHeight > base_height ||
outpoint.n >= std::numeric_limits<decltype(outpoint.n)>::max() // Avoid integer wrap-around in coinstats.cpp:ApplyHash outpoint.n >= std::numeric_limits<decltype(outpoint.n)>::max() // Avoid integer wrap-around in coinstats.cpp:ApplyHash
) { ) {
@@ -5689,7 +5696,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
coins_count - coins_left); coins_count - coins_left);
return false; return false;
} }
coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin)); coins_cache.EmplaceCoinInternalDANGER(std::move(outpoint), std::move(coin));
--coins_left; --coins_left;
@@ -5725,6 +5731,12 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
} }
} }
} }
} catch (const std::ios_base::failure&) {
LogPrintf("[snapshot] bad snapshot format or truncated snapshot after deserializing %d coins\n",
coins_processed);
return false;
}
}
// Important that we set this. This and the coins_cache accesses above are // Important that we set this. This and the coins_cache accesses above are
// sort of a layer violation, but either we reach into the innards of // sort of a layer violation, but either we reach into the innards of
@@ -5735,7 +5747,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
bool out_of_coins{false}; bool out_of_coins{false};
try { try {
coins_file >> outpoint; Txid txid;
coins_file >> txid;
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
// We expect an exception since we should be out of coins. // We expect an exception since we should be out of coins.
out_of_coins = true; out_of_coins = true;
+7 -6
View File
@@ -94,15 +94,16 @@ class AssumeutxoTest(BitcoinTestFramework):
f.write(valid_snapshot_contents[32 + 8:]) f.write(valid_snapshot_contents[32 + 8:])
expected_error(log_msg=f"bad snapshot - coins left over after deserializing 298 coins" if off == -1 else f"bad snapshot format or truncated snapshot after deserializing 299 coins") expected_error(log_msg=f"bad snapshot - coins left over after deserializing 298 coins" if off == -1 else f"bad snapshot format or truncated snapshot after deserializing 299 coins")
self.log.info(" - snapshot file with alternated UTXO data")
self.log.info(" - snapshot file with alternated but parsable UTXO data results in different hash")
cases = [ cases = [
# (content, offset, wrong_hash, custom_message) # (content, offset, wrong_hash, custom_message)
[b"\xff" * 32, 0, "7d52155c9a9fdc4525b637ef6170568e5dad6fabd0b1fdbb9432010b8453095b", None], # wrong outpoint hash [b"\xff" * 32, 0, "7d52155c9a9fdc4525b637ef6170568e5dad6fabd0b1fdbb9432010b8453095b", None], # wrong outpoint hash
[(1).to_bytes(4, "little"), 32, "9f4d897031ab8547665b4153317ae2fdbf0130c7840b66427ebc48b881cb80ad", None], # wrong outpoint index [b"\x01", 33, "9f4d897031ab8547665b4153317ae2fdbf0130c7840b66427ebc48b881cb80ad", None], # wrong outpoint index
[b"\x81", 36, "3da966ba9826fb6d2604260e01607b55ba44e1a5de298606b08704bc62570ea8", None], # wrong coin code VARINT [b"\x81", 34, "3da966ba9826fb6d2604260e01607b55ba44e1a5de298606b08704bc62570ea8", None], # wrong coin code VARINT
[b"\x80", 36, "091e893b3ccb4334378709578025356c8bcb0a623f37c7c4e493133c988648e5", None], # another wrong coin code [b"\x80", 34, "091e893b3ccb4334378709578025356c8bcb0a623f37c7c4e493133c988648e5", None], # another wrong coin code
[b"\x84\x58", 36, None, "[snapshot] bad snapshot data after deserializing 0 coins"], # wrong coin case with height 364 and coinbase 0 [b"\x84\x58", 34, None, "[snapshot] bad snapshot data after deserializing 0 coins"], # wrong coin case with height 364 and coinbase 0
[b"\xCA\xD2\x8F\x5A", 41, None, "[snapshot] bad snapshot data after deserializing 0 coins - bad tx out value"], # Amount exceeds MAX_MONEY [b"\xCA\xD2\x8F\x5A", 39, None, "[snapshot] bad snapshot data after deserializing 0 coins - bad tx out value"], # Amount exceeds MAX_MONEY
] ]
for content, offset, wrong_hash, custom_message in cases: for content, offset, wrong_hash, custom_message in cases:
+1 -1
View File
@@ -43,7 +43,7 @@ class DumptxoutsetTest(BitcoinTestFramework):
# UTXO snapshot hash should be deterministic based on mocked time. # UTXO snapshot hash should be deterministic based on mocked time.
assert_equal( assert_equal(
sha256sum_file(str(expected_path)).hex(), sha256sum_file(str(expected_path)).hex(),
'b1bacb602eacf5fbc9a7c2ef6eeb0d229c04e98bdf0c2ea5929012cd0eae3830') '3263fc0311ea46415b85513a59ad8fe67806b3cdce66147175ecb9da768d4a99')
assert_equal( assert_equal(
out['txoutset_hash'], 'a0b7baa3bf5ccbd3279728f230d7ca0c44a76e9923fca8f32dbfd08d65ea496a') out['txoutset_hash'], 'a0b7baa3bf5ccbd3279728f230d7ca0c44a76e9923fca8f32dbfd08d65ea496a')