Added best block hash to the NotifyHeaderTip and NotifyBlockTip signals.

[ClientModel] best header/block hash cached.
This commit is contained in:
furszy
2020-01-23 19:31:16 -03:00
parent cfe22a5f9e
commit 2f867203b0
11 changed files with 57 additions and 34 deletions

View File

@@ -187,6 +187,11 @@ public:
LOCK(::cs_main);
return ::ChainActive().Height();
}
uint256 getBestBlockHash() override
{
const CBlockIndex* tip = WITH_LOCK(::cs_main, return ::ChainActive().Tip());
return tip ? tip->GetBlockHash() : Params().GenesisBlock().GetHash();
}
int64_t getLastBlockTime() override
{
LOCK(::cs_main);
@@ -310,7 +315,7 @@ public:
std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) override
{
return MakeHandler(::uiInterface.NotifyBlockTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
fn(sync_state, block->nHeight, block->GetBlockTime(),
fn(sync_state, block->GetBlockHash(), block->nHeight, block->GetBlockTime(),
GuessVerificationProgress(Params().TxData(), block));
}));
}
@@ -318,7 +323,7 @@ public:
{
return MakeHandler(
::uiInterface.NotifyHeaderTip_connect([fn](SynchronizationState sync_state, const CBlockIndex* block) {
fn(sync_state, block->nHeight, block->GetBlockTime(),
fn(sync_state, block->GetBlockHash(), block->nHeight, block->GetBlockTime(),
/* verification progress is unused when a header was received */ 0);
}));
}

View File

@@ -149,6 +149,9 @@ public:
//! Get num blocks.
virtual int getNumBlocks() = 0;
//! Get best block hash.
virtual uint256 getBestBlockHash() = 0;
//! Get last block time.
virtual int64_t getLastBlockTime() = 0;
@@ -250,12 +253,12 @@ public:
//! Register handler for block tip messages.
using NotifyBlockTipFn =
std::function<void(SynchronizationState, int height, int64_t block_time, double verification_progress)>;
std::function<void(SynchronizationState, const uint256& block_hash, int height, int64_t block_time, double verification_progress)>;
virtual std::unique_ptr<Handler> handleNotifyBlockTip(NotifyBlockTipFn fn) = 0;
//! Register handler for header tip messages.
using NotifyHeaderTipFn =
std::function<void(SynchronizationState, int height, int64_t block_time, double verification_progress)>;
std::function<void(SynchronizationState, const uint256& block_hash, int height, int64_t block_time, double verification_progress)>;
virtual std::unique_ptr<Handler> handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0;
//! Return pointer to internal chain interface, useful for testing.

View File

@@ -351,13 +351,13 @@ public:
}
return result;
}
bool tryGetBalances(WalletBalances& balances, int& num_blocks) override
bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override
{
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
if (!locked_wallet) {
return false;
}
num_blocks = m_wallet->GetLastBlockHeight();
block_hash = m_wallet->GetLastBlockHash();
balances = getBalances();
return true;
}

View File

@@ -203,7 +203,7 @@ public:
virtual WalletBalances getBalances() = 0;
//! Get balances if possible without blocking.
virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
virtual bool tryGetBalances(WalletBalances& balances, uint256& block_hash) = 0;
//! Get balance.
virtual CAmount getBalance() = 0;