From 700c42b85d20e624bef4228eef062c93084efab5 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Thu, 27 Jul 2017 10:08:31 -0400 Subject: Add height, depth, and hash methods to the Chain interface And use them to remove uses of chainActive and mapBlockIndex in wallet code This commit does not change behavior. Co-authored-by: Ben Woosley --- src/interfaces/chain.cpp | 30 ++++++++++++++++++++++++++++++ src/interfaces/chain.h | 20 ++++++++++++++++++++ src/interfaces/wallet.cpp | 6 +++--- 3 files changed, 53 insertions(+), 3 deletions(-) (limited to 'src/interfaces') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 2571a91031..138d1bfd5f 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -4,7 +4,9 @@ #include +#include #include +#include #include #include @@ -16,6 +18,34 @@ namespace { class LockImpl : public Chain::Lock { + Optional getHeight() override + { + int height = ::chainActive.Height(); + if (height >= 0) { + return height; + } + return nullopt; + } + Optional getBlockHeight(const uint256& hash) override + { + CBlockIndex* block = LookupBlockIndex(hash); + if (block && ::chainActive.Contains(block)) { + return block->nHeight; + } + return nullopt; + } + int getBlockDepth(const uint256& hash) override + { + const Optional tip_height = getHeight(); + const Optional height = getBlockHeight(hash); + return tip_height && height ? *tip_height - *height + 1 : 0; + } + uint256 getBlockHash(int height) override + { + CBlockIndex* block = ::chainActive[height]; + assert(block != nullptr); + return block->GetBlockHash(); + } }; class LockingStateImpl : public LockImpl, public UniqueLock diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index fe5658de4b..928b8af1f4 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -5,11 +5,14 @@ #ifndef BITCOIN_INTERFACES_CHAIN_H #define BITCOIN_INTERFACES_CHAIN_H +#include + #include #include #include class CScheduler; +class uint256; namespace interfaces { @@ -28,6 +31,23 @@ public: { public: virtual ~Lock() {} + + //! Get current chain height, not including genesis block (returns 0 if + //! chain only contains genesis block, nullopt if chain does not contain + //! any blocks). + virtual Optional getHeight() = 0; + + //! Get block height above genesis block. Returns 0 for genesis block, + //! 1 for following block, and so on. Returns nullopt for a block not + //! included in the current chain. + virtual Optional getBlockHeight(const uint256& hash) = 0; + + //! Get block depth. Returns 1 for chain tip, 2 for preceding block, and + //! so on. Returns 0 for a block not included in the current chain. + virtual int getBlockDepth(const uint256& hash) = 0; + + //! Get block hash. Height must be valid or this function will abort. + virtual uint256 getBlockHash(int height) = 0; }; //! Return Lock interface. Chain is locked when this is called, and diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 8db34ed759..489cf0981c 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -333,7 +333,7 @@ public: if (mi == m_wallet.mapWallet.end()) { return false; } - num_blocks = ::chainActive.Height(); + num_blocks = locked_chain->getHeight().value_or(-1); block_time = ::chainActive.Tip()->GetBlockTime(); tx_status = MakeWalletTxStatus(*locked_chain, mi->second); return true; @@ -348,7 +348,7 @@ public: LOCK(m_wallet.cs_wallet); auto mi = m_wallet.mapWallet.find(txid); if (mi != m_wallet.mapWallet.end()) { - num_blocks = ::chainActive.Height(); + num_blocks = locked_chain->getHeight().value_or(-1); in_mempool = mi->second.InMempool(); order_form = mi->second.vOrderForm; tx_status = MakeWalletTxStatus(*locked_chain, mi->second); @@ -379,7 +379,7 @@ public: return false; } balances = getBalances(); - num_blocks = ::chainActive.Height(); + num_blocks = locked_chain->getHeight().value_or(-1); return true; } CAmount getBalance() override { return m_wallet.GetBalance(); } -- cgit v1.2.3 From d93c4c1d6e0aab5f32306ecd7c1237257b26940d Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Mon, 7 Jan 2019 22:35:47 -0800 Subject: Add time methods to the Chain interface And use them to remove uses of chainActive and mapBlockIndex in wallet code This commit does not change behavior. Co-authored-by: Ben Woosley --- src/interfaces/chain.cpp | 12 ++++++++++++ src/interfaces/chain.h | 8 ++++++++ src/interfaces/wallet.cpp | 9 +++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) (limited to 'src/interfaces') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 138d1bfd5f..ca2533bb1d 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -46,6 +46,18 @@ class LockImpl : public Chain::Lock assert(block != nullptr); return block->GetBlockHash(); } + int64_t getBlockTime(int height) override + { + CBlockIndex* block = ::chainActive[height]; + assert(block != nullptr); + return block->GetBlockTime(); + } + int64_t getBlockMedianTimePast(int height) override + { + CBlockIndex* block = ::chainActive[height]; + assert(block != nullptr); + return block->GetMedianTimePast(); + } }; class LockingStateImpl : public LockImpl, public UniqueLock diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 928b8af1f4..1db9d3861d 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -48,6 +49,13 @@ public: //! Get block hash. Height must be valid or this function will abort. virtual uint256 getBlockHash(int height) = 0; + + //! Get block time. Height must be valid or this function will abort. + virtual int64_t getBlockTime(int height) = 0; + + //! Get block median time past. Height must be valid or this function + //! will abort. + virtual int64_t getBlockMedianTimePast(int height) = 0; }; //! Return Lock interface. Chain is locked when this is called, and diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 489cf0981c..88c5db73a1 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -333,8 +333,13 @@ public: if (mi == m_wallet.mapWallet.end()) { return false; } - num_blocks = locked_chain->getHeight().value_or(-1); - block_time = ::chainActive.Tip()->GetBlockTime(); + if (Optional height = locked_chain->getHeight()) { + num_blocks = *height; + block_time = locked_chain->getBlockTime(*height); + } else { + num_blocks = -1; + block_time = -1; + } tx_status = MakeWalletTxStatus(*locked_chain, mi->second); return true; } -- cgit v1.2.3 From 2ffb07929ef480bd114defdc10b6a84463f222be Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Mon, 7 Jan 2019 22:56:46 -0800 Subject: Add findFork and findBlock to the Chain interface And use them to remove uses of chainActive and mapBlockIndex in wallet code This commit does not change behavior. Co-authored-by: Ben Woosley --- src/interfaces/chain.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/interfaces/chain.h | 19 +++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'src/interfaces') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index ca2533bb1d..6e2224bc3e 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include #include @@ -58,6 +60,22 @@ class LockImpl : public Chain::Lock assert(block != nullptr); return block->GetMedianTimePast(); } + Optional findFork(const uint256& hash, Optional* height) override + { + const CBlockIndex* block = LookupBlockIndex(hash); + const CBlockIndex* fork = block ? ::chainActive.FindFork(block) : nullptr; + if (height) { + if (block) { + *height = block->nHeight; + } else { + height->reset(); + } + } + if (fork) { + return fork->nHeight; + } + return nullopt; + } }; class LockingStateImpl : public LockImpl, public UniqueLock @@ -77,6 +95,27 @@ public: return std::move(result); } std::unique_ptr assumeLocked() override { return MakeUnique(); } + bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override + { + CBlockIndex* index; + { + LOCK(cs_main); + index = LookupBlockIndex(hash); + if (!index) { + return false; + } + if (time) { + *time = index->GetBlockTime(); + } + if (time_max) { + *time_max = index->GetBlockTimeMax(); + } + } + if (block && !ReadBlockFromDisk(*block, index, Params().GetConsensus())) { + block->SetNull(); + } + return true; + } }; } // namespace diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 1db9d3861d..8c7bd8c6ea 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -12,6 +12,7 @@ #include #include +class CBlock; class CScheduler; class uint256; @@ -56,6 +57,13 @@ public: //! Get block median time past. Height must be valid or this function //! will abort. virtual int64_t getBlockMedianTimePast(int height) = 0; + + //! Return height of the highest block on the chain that is an ancestor + //! of the specified block, or nullopt if no common ancestor is found. + //! Also return the height of the specified block as an optional output + //! parameter (to avoid the cost of a second hash lookup in case this + //! information is desired). + virtual Optional findFork(const uint256& hash, Optional* height) = 0; }; //! Return Lock interface. Chain is locked when this is called, and @@ -66,6 +74,17 @@ public: //! method is temporary and is only used in a few places to avoid changing //! behavior while code is transitioned to use the Chain::Lock interface. virtual std::unique_ptr assumeLocked() = 0; + + //! Return whether node has the block and optionally return block metadata + //! or contents. + //! + //! If a block pointer is provided to retrieve the block contents, and the + //! block exists but doesn't have data (for example due to pruning), the + //! block will be empty and all fields set to null. + virtual bool findBlock(const uint256& hash, + CBlock* block = nullptr, + int64_t* time = nullptr, + int64_t* max_time = nullptr) = 0; }; //! Interface to let node manage chain clients (wallets, or maybe tools for -- cgit v1.2.3 From db21f0264855406c9b6ec4c084a15988c5c71b32 Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Mon, 7 Jan 2019 23:38:53 -0800 Subject: Convert CWallet::ScanForWalletTransactions and SyncTransaction to the new Chain apis Only change in behavior is "Rescan started from block " message replaced by "Rescan started from block " message in ScanForWalletTransactions. Co-authored-by: Ben Woosley --- src/interfaces/chain.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/interfaces/chain.h | 24 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+) (limited to 'src/interfaces') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 6e2224bc3e..1f39e650dc 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -60,6 +60,42 @@ class LockImpl : public Chain::Lock assert(block != nullptr); return block->GetMedianTimePast(); } + Optional findFirstBlockWithTime(int64_t time, uint256* hash) override + { + CBlockIndex* block = ::chainActive.FindEarliestAtLeast(time); + if (block) { + if (hash) *hash = block->GetBlockHash(); + return block->nHeight; + } + return nullopt; + } + Optional findFirstBlockWithTimeAndHeight(int64_t time, int height) override + { + // TODO: Could update CChain::FindEarliestAtLeast() to take a height + // parameter and use it with std::lower_bound() to make this + // implementation more efficient and allow combining + // findFirstBlockWithTime and findFirstBlockWithTimeAndHeight into one + // method. + for (CBlockIndex* block = ::chainActive[height]; block; block = ::chainActive.Next(block)) { + if (block->GetBlockTime() >= time) { + return block->nHeight; + } + } + return nullopt; + } + Optional findPruned(int start_height, Optional stop_height) override + { + if (::fPruneMode) { + CBlockIndex* block = stop_height ? ::chainActive[*stop_height] : ::chainActive.Tip(); + while (block && block->nHeight >= start_height) { + if ((block->nStatus & BLOCK_HAVE_DATA) == 0) { + return block->nHeight; + } + block = block->pprev; + } + } + return nullopt; + } Optional findFork(const uint256& hash, Optional* height) override { const CBlockIndex* block = LookupBlockIndex(hash); @@ -116,6 +152,11 @@ public: } return true; } + double guessVerificationProgress(const uint256& block_hash) override + { + LOCK(cs_main); + return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash)); + } }; } // namespace diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index 8c7bd8c6ea..aef81675e0 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -58,6 +58,26 @@ public: //! will abort. virtual int64_t getBlockMedianTimePast(int height) = 0; + //! Return height of the first block in the chain with timestamp equal + //! or greater than the given time, or nullopt if there is no block with + //! a high enough timestamp. Also return the block hash as an optional + //! output parameter (to avoid the cost of a second lookup in case this + //! information is needed.) + virtual Optional findFirstBlockWithTime(int64_t time, uint256* hash) = 0; + + //! Return height of the first block in the chain with timestamp equal + //! or greater than the given time and height equal or greater than the + //! given height, or nullopt if there is no such block. + //! + //! Calling this with height 0 is equivalent to calling + //! findFirstBlockWithTime, but less efficient because it requires a + //! linear instead of a binary search. + virtual Optional findFirstBlockWithTimeAndHeight(int64_t time, int height) = 0; + + //! Return height of last block in the specified range which is pruned, or + //! nullopt if no block in the range is pruned. Range is inclusive. + virtual Optional findPruned(int start_height = 0, Optional stop_height = nullopt) = 0; + //! Return height of the highest block on the chain that is an ancestor //! of the specified block, or nullopt if no common ancestor is found. //! Also return the height of the specified block as an optional output @@ -85,6 +105,10 @@ public: CBlock* block = nullptr, int64_t* time = nullptr, int64_t* max_time = nullptr) = 0; + + //! Estimate fraction of total transactions verified if blocks up to + //! the specified block hash are verified. + virtual double guessVerificationProgress(const uint256& block_hash) = 0; }; //! Interface to let node manage chain clients (wallets, or maybe tools for -- cgit v1.2.3 From 44de1561aaf7556bb8ae8b582c233742ff76767d Mon Sep 17 00:00:00 2001 From: Russell Yanofsky Date: Tue, 8 Jan 2019 00:06:24 -0800 Subject: Remove remaining chainActive references from CWallet This commit does not change behavior. Co-authored-by: Ben Woosley --- src/interfaces/chain.cpp | 20 ++++++++++++++++++++ src/interfaces/chain.h | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'src/interfaces') diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 1f39e650dc..38888be8a3 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -60,6 +60,11 @@ class LockImpl : public Chain::Lock assert(block != nullptr); return block->GetMedianTimePast(); } + bool haveBlockOnDisk(int height) override + { + CBlockIndex* block = ::chainActive[height]; + return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; + } Optional findFirstBlockWithTime(int64_t time, uint256* hash) override { CBlockIndex* block = ::chainActive.FindEarliestAtLeast(time); @@ -112,6 +117,21 @@ class LockImpl : public Chain::Lock } return nullopt; } + bool isPotentialTip(const uint256& hash) override + { + if (::chainActive.Tip()->GetBlockHash() == hash) return true; + CBlockIndex* block = LookupBlockIndex(hash); + return block && block->GetAncestor(::chainActive.Height()) == ::chainActive.Tip(); + } + CBlockLocator getLocator() override { return ::chainActive.GetLocator(); } + Optional findLocatorFork(const CBlockLocator& locator) override + { + LockAnnotation lock(::cs_main); + if (CBlockIndex* fork = FindForkInGlobalIndex(::chainActive, locator)) { + return fork->nHeight; + } + return nullopt; + } }; class LockingStateImpl : public LockImpl, public UniqueLock diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index aef81675e0..735d5b60df 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -15,6 +15,7 @@ class CBlock; class CScheduler; class uint256; +struct CBlockLocator; namespace interfaces { @@ -58,6 +59,10 @@ public: //! will abort. virtual int64_t getBlockMedianTimePast(int height) = 0; + //! Check that the block is available on disk (i.e. has not been + //! pruned), and contains transactions. + virtual bool haveBlockOnDisk(int height) = 0; + //! Return height of the first block in the chain with timestamp equal //! or greater than the given time, or nullopt if there is no block with //! a high enough timestamp. Also return the block hash as an optional @@ -84,6 +89,19 @@ public: //! parameter (to avoid the cost of a second hash lookup in case this //! information is desired). virtual Optional findFork(const uint256& hash, Optional* height) = 0; + + //! Return true if block hash points to the current chain tip, or to a + //! possible descendant of the current chain tip that isn't currently + //! connected. + virtual bool isPotentialTip(const uint256& hash) = 0; + + //! Get locator for the current chain tip. + virtual CBlockLocator getLocator() = 0; + + //! Return height of the latest block common to locator and chain, which + //! is guaranteed to be an ancestor of the block used to create the + //! locator. + virtual Optional findLocatorFork(const CBlockLocator& locator) = 0; }; //! Return Lock interface. Chain is locked when this is called, and -- cgit v1.2.3