diff --git a/doc/release-notes.md b/doc/release-notes.md index 339250382..84de37e8b 100644 --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -1,9 +1,14 @@ # Bitcoin ABC 0.25.8 Release Notes Bitcoin ABC version 0.25.8 is now available from: This release includes the following features and fixes: - Users can start their node with the option `-coinstatsindex` which syncs an - index of coin statistics in the background. + index of coin statistics in the background. After the index is synced the user + can use `gettxoutsetinfo` with hash_type=none or hash_type=muhash and will get + the response instantly out of the index +- Users can specify a height or block hash when calling `gettxoutsetinfo` to + see coin statistics at a specific block height when they use the `-coinstatsindex` + option. diff --git a/src/node/coinstats.cpp b/src/node/coinstats.cpp index 468f30f9a..a9e4f9ea3 100644 --- a/src/node/coinstats.cpp +++ b/src/node/coinstats.cpp @@ -1,184 +1,186 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include uint64_t GetBogoSize(const CScript &script_pub_key) { return 32 /* txid */ + 4 /* vout index */ + 4 /* height + coinbase */ + 8 /* amount */ + 2 /* scriptPubKey len */ + script_pub_key.size() /* scriptPubKey */; } CDataStream TxOutSer(const COutPoint &outpoint, const Coin &coin) { CDataStream ss(SER_DISK, PROTOCOL_VERSION); ss << outpoint; ss << static_cast(coin.GetHeight() * 2 + coin.IsCoinBase()); ss << coin.GetTxOut(); return ss; } //! Warning: be very careful when changing this! assumeutxo and UTXO snapshot //! validation commitments are reliant on the hash constructed by this //! function. //! //! If the construction of this hash is changed, it will invalidate //! existing UTXO snapshots. This will not result in any kind of consensus //! failure, but it will force clients that were expecting to make use of //! assumeutxo to do traditional IBD instead. //! //! It is also possible, though very unlikely, that a change in this //! construction could cause a previously invalid (and potentially malicious) //! UTXO snapshot to be considered valid. static void ApplyHash(CHashWriter &ss, const TxId &txid, const std::map &outputs) { for (auto it = outputs.begin(); it != outputs.end(); ++it) { if (it == outputs.begin()) { ss << txid; ss << VARINT(it->second.GetHeight() * 2 + it->second.IsCoinBase()); } ss << VARINT(it->first + 1); ss << it->second.GetTxOut().scriptPubKey; ss << VARINT_MODE(it->second.GetTxOut().nValue / SATOSHI, VarIntMode::NONNEGATIVE_SIGNED); if (it == std::prev(outputs.end())) { ss << VARINT(0u); } } } static void ApplyHash(std::nullptr_t, const TxId &txid, const std::map &outputs) {} static void ApplyHash(MuHash3072 &muhash, const TxId &txid, const std::map &outputs) { for (auto it = outputs.begin(); it != outputs.end(); ++it) { COutPoint outpoint = COutPoint(txid, it->first); Coin coin = it->second; muhash.Insert(MakeUCharSpan(TxOutSer(outpoint, coin))); } } static void ApplyStats(CCoinsStats &stats, const TxId &txid, const std::map &outputs) { assert(!outputs.empty()); stats.nTransactions++; for (auto it = outputs.begin(); it != outputs.end(); ++it) { stats.nTransactionOutputs++; stats.nTotalAmount += it->second.GetTxOut().nValue; stats.nBogoSize += GetBogoSize(it->second.GetTxOut().scriptPubKey); } } //! Calculate statistics about the unspent transaction output set template static bool GetUTXOStats(CCoinsView *view, BlockManager &blockman, CCoinsStats &stats, T hash_obj, - const std::function &interruption_point) { + const std::function &interruption_point, + const CBlockIndex *pindex) { std::unique_ptr pcursor(view->Cursor()); assert(pcursor); - stats.hashBlock = pcursor->GetBestBlock(); - const CBlockIndex *pindex; - { + if (!pindex) { LOCK(cs_main); assert(std::addressof(g_chainman.m_blockman) == std::addressof(blockman)); - - pindex = blockman.LookupBlockIndex(stats.hashBlock); - stats.nHeight = Assert(pindex)->nHeight; + pindex = blockman.LookupBlockIndex(view->GetBestBlock()); } + stats.nHeight = Assert(pindex)->nHeight; + stats.hashBlock = pindex->GetBlockHash(); // Use CoinStatsIndex if it is available and a hash_type of Muhash or None // was requested if ((stats.m_hash_type == CoinStatsHashType::MUHASH || stats.m_hash_type == CoinStatsHashType::NONE) && g_coin_stats_index) { + stats.from_index = true; return g_coin_stats_index->LookUpStats(pindex, stats); } PrepareHash(hash_obj, stats); TxId prevkey; std::map outputs; while (pcursor->Valid()) { interruption_point(); COutPoint key; Coin coin; if (pcursor->GetKey(key) && pcursor->GetValue(coin)) { if (!outputs.empty() && key.GetTxId() != prevkey) { ApplyStats(stats, prevkey, outputs); ApplyHash(hash_obj, prevkey, outputs); outputs.clear(); } prevkey = key.GetTxId(); outputs[key.GetN()] = std::move(coin); stats.coins_count++; } else { return error("%s: unable to read value", __func__); } pcursor->Next(); } if (!outputs.empty()) { ApplyStats(stats, prevkey, outputs); ApplyHash(hash_obj, prevkey, outputs); } FinalizeHash(hash_obj, stats); stats.nDiskSize = view->EstimateSize(); return true; } bool GetUTXOStats(CCoinsView *view, BlockManager &blockman, CCoinsStats &stats, - const std::function &interruption_point) { + const std::function &interruption_point, + const CBlockIndex *pindex) { switch (stats.m_hash_type) { case (CoinStatsHashType::HASH_SERIALIZED): { CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION); - return GetUTXOStats(view, blockman, stats, ss, interruption_point); + return GetUTXOStats(view, blockman, stats, ss, interruption_point, + pindex); } case (CoinStatsHashType::MUHASH): { MuHash3072 muhash; return GetUTXOStats(view, blockman, stats, muhash, - interruption_point); + interruption_point, pindex); } case (CoinStatsHashType::NONE): { return GetUTXOStats(view, blockman, stats, nullptr, - interruption_point); + interruption_point, pindex); } } // no default case, so the compiler can warn about missing cases assert(false); } // The legacy hash serializes the hashBlock static void PrepareHash(CHashWriter &ss, const CCoinsStats &stats) { ss << stats.hashBlock; } // MuHash does not need the prepare step static void PrepareHash(MuHash3072 &muhash, CCoinsStats &stats) {} static void PrepareHash(std::nullptr_t, CCoinsStats &stats) {} static void FinalizeHash(CHashWriter &ss, CCoinsStats &stats) { stats.hashSerialized = ss.GetHash(); } static void FinalizeHash(MuHash3072 &muhash, CCoinsStats &stats) { uint256 out; muhash.Finalize(out); stats.hashSerialized = out; } static void FinalizeHash(std::nullptr_t, CCoinsStats &stats) {} diff --git a/src/node/coinstats.h b/src/node/coinstats.h index bf47b9eae..55c072e00 100644 --- a/src/node/coinstats.h +++ b/src/node/coinstats.h @@ -1,53 +1,56 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_NODE_COINSTATS_H #define BITCOIN_NODE_COINSTATS_H #include #include #include #include #include #include #include #include class BlockManager; class CCoinsView; enum class CoinStatsHashType { HASH_SERIALIZED, MUHASH, NONE, }; struct CCoinsStats { CoinStatsHashType m_hash_type; int nHeight{0}; BlockHash hashBlock{}; uint64_t nTransactions{0}; uint64_t nTransactionOutputs{0}; uint64_t nBogoSize{0}; uint256 hashSerialized{}; uint64_t nDiskSize{0}; Amount nTotalAmount{Amount::zero()}; //! The number of coins contained. uint64_t coins_count{0}; + bool from_index{false}; + CCoinsStats(CoinStatsHashType hash_type) : m_hash_type(hash_type) {} }; //! Calculate statistics about the unspent transaction output set bool GetUTXOStats(CCoinsView *view, BlockManager &blockman, CCoinsStats &stats, - const std::function &interruption_point = {}); + const std::function &interruption_point = {}, + const CBlockIndex *pindex = nullptr); uint64_t GetBogoSize(const CScript &script_pub_key); CDataStream TxOutSer(const COutPoint &outpoint, const Coin &coin); #endif // BITCOIN_NODE_COINSTATS_H diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index cbbd45023..30f87fd9a 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1,3205 +1,3244 @@ // Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include