Skip to content

Commit a0b5b4a

Browse files
committed
interfaces, refactor: Add more block information to block connected notifications
Add new interfaces::BlockInfo struct to be able to pass extra block information (file and undo information) to indexes which they are updated to use high level interfaces::Chain notifications. This commit does not change behavior in any way.
1 parent 8d4a058 commit a0b5b4a

File tree

8 files changed

+94
-21
lines changed

8 files changed

+94
-21
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ BITCOIN_CORE_H = \
171171
interfaces/ipc.h \
172172
interfaces/node.h \
173173
interfaces/wallet.h \
174+
kernel/chain.h \
174175
kernel/chainstatemanager_opts.h \
175176
kernel/checks.h \
176177
kernel/coinstats.h \
@@ -365,6 +366,7 @@ libbitcoin_node_a_SOURCES = \
365366
index/coinstatsindex.cpp \
366367
index/txindex.cpp \
367368
init.cpp \
369+
kernel/chain.cpp \
368370
kernel/checks.cpp \
369371
kernel/coinstats.cpp \
370372
kernel/context.cpp \

src/interfaces/chain.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
class ArgsManager;
2020
class CBlock;
21+
class CBlockUndo;
2122
class CFeeRate;
2223
class CRPCCommand;
2324
class CScheduler;
@@ -67,6 +68,19 @@ class FoundBlock
6768
mutable bool found = false;
6869
};
6970

71+
//! Block data sent with blockConnected, blockDisconnected notifications.
72+
struct BlockInfo {
73+
const uint256& hash;
74+
const uint256* prev_hash = nullptr;
75+
int height = -1;
76+
int file_number = -1;
77+
unsigned data_pos = 0;
78+
const CBlock* data = nullptr;
79+
const CBlockUndo* undo_data = nullptr;
80+
81+
BlockInfo(const uint256& hash LIFETIMEBOUND) : hash(hash) {}
82+
};
83+
7084
//! Interface giving clients (wallet processes, maybe other analysis tools in
7185
//! the future) ability to access to the chain state, receive notifications,
7286
//! estimate fees, and submit transactions.
@@ -239,8 +253,8 @@ class Chain
239253
virtual ~Notifications() {}
240254
virtual void transactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
241255
virtual void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
242-
virtual void blockConnected(const CBlock& block, int height) {}
243-
virtual void blockDisconnected(const CBlock& block, int height) {}
256+
virtual void blockConnected(const BlockInfo& block) {}
257+
virtual void blockDisconnected(const BlockInfo& block) {}
244258
virtual void updatedBlockTip() {}
245259
virtual void chainStateFlushed(const CBlockLocator& locator) {}
246260
};

src/kernel/chain.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <chain.h>
6+
#include <interfaces/chain.h>
7+
#include <sync.h>
8+
#include <uint256.h>
9+
10+
class CBlock;
11+
12+
namespace kernel {
13+
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex* index, const CBlock* data)
14+
{
15+
interfaces::BlockInfo info{index ? *index->phashBlock : uint256::ZERO};
16+
if (index) {
17+
info.prev_hash = index->pprev ? index->pprev->phashBlock : nullptr;
18+
info.height = index->nHeight;
19+
LOCK(::cs_main);
20+
info.file_number = index->nFile;
21+
info.data_pos = index->nDataPos;
22+
}
23+
info.data = data;
24+
return info;
25+
}
26+
} // namespace kernel

src/kernel/chain.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_KERNEL_CHAIN_H
6+
#define BITCOIN_KERNEL_CHAIN_H
7+
8+
class CBlock;
9+
class CBlockIndex;
10+
namespace interfaces {
11+
struct BlockInfo;
12+
} // namespace interfaces
13+
14+
namespace kernel {
15+
//! Return data from block index.
16+
interfaces::BlockInfo MakeBlockInfo(const CBlockIndex* block_index, const CBlock* data = nullptr);
17+
} // namespace kernel
18+
19+
#endif // BITCOIN_KERNEL_CHAIN_H

src/node/interfaces.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <netaddress.h>
2020
#include <netbase.h>
2121
#include <node/blockstorage.h>
22+
#include <kernel/chain.h>
2223
#include <node/coin.h>
2324
#include <node/context.h>
2425
#include <node/transaction.h>
@@ -426,11 +427,11 @@ class NotificationsProxy : public CValidationInterface
426427
}
427428
void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
428429
{
429-
m_notifications->blockConnected(*block, index->nHeight);
430+
m_notifications->blockConnected(kernel::MakeBlockInfo(index, block.get()));
430431
}
431432
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* index) override
432433
{
433-
m_notifications->blockDisconnected(*block, index->nHeight);
434+
m_notifications->blockDisconnected(kernel::MakeBlockInfo(index, block.get()));
434435
}
435436
void UpdatedBlockTip(const CBlockIndex* index, const CBlockIndex* fork_index, bool is_ibd) override
436437
{

src/wallet/test/fuzz/notifications.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,13 @@ FUZZ_TARGET_INIT(wallet_notifications, initialize_setup)
137137
block.vtx.emplace_back(MakeTransactionRef(tx));
138138
}
139139
// Mine block
140-
a.wallet->blockConnected(block, chain.size());
141-
b.wallet->blockConnected(block, chain.size());
140+
const uint256& hash = block.GetHash();
141+
interfaces::BlockInfo info{hash};
142+
info.prev_hash = &block.hashPrevBlock;
143+
info.height = chain.size();
144+
info.data = &block;
145+
a.wallet->blockConnected(info);
146+
b.wallet->blockConnected(info);
142147
// Store the coins for the next block
143148
Coins coins_new;
144149
for (const auto& tx : block.vtx) {
@@ -154,8 +159,13 @@ FUZZ_TARGET_INIT(wallet_notifications, initialize_setup)
154159
auto& [coins, block]{chain.back()};
155160
if (block.vtx.empty()) return; // Can only disconnect if the block was submitted first
156161
// Disconnect block
157-
a.wallet->blockDisconnected(block, chain.size() - 1);
158-
b.wallet->blockDisconnected(block, chain.size() - 1);
162+
const uint256& hash = block.GetHash();
163+
interfaces::BlockInfo info{hash};
164+
info.prev_hash = &block.hashPrevBlock;
165+
info.height = chain.size() - 1;
166+
info.data = &block;
167+
a.wallet->blockDisconnected(info);
168+
b.wallet->blockDisconnected(info);
159169
chain.pop_back();
160170
});
161171
auto& [coins, first_block]{chain.front()};

src/wallet/wallet.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,30 +1314,31 @@ void CWallet::transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRe
13141314
}
13151315
}
13161316

1317-
void CWallet::blockConnected(const CBlock& block, int height)
1317+
void CWallet::blockConnected(const interfaces::BlockInfo& block)
13181318
{
1319-
const uint256& block_hash = block.GetHash();
1319+
assert(block.data);
13201320
LOCK(cs_wallet);
13211321

1322-
m_last_block_processed_height = height;
1323-
m_last_block_processed = block_hash;
1324-
for (size_t index = 0; index < block.vtx.size(); index++) {
1325-
SyncTransaction(block.vtx[index], TxStateConfirmed{block_hash, height, static_cast<int>(index)});
1326-
transactionRemovedFromMempool(block.vtx[index], MemPoolRemovalReason::BLOCK, 0 /* mempool_sequence */);
1322+
m_last_block_processed_height = block.height;
1323+
m_last_block_processed = block.hash;
1324+
for (size_t index = 0; index < block.data->vtx.size(); index++) {
1325+
SyncTransaction(block.data->vtx[index], TxStateConfirmed{block.hash, block.height, static_cast<int>(index)});
1326+
transactionRemovedFromMempool(block.data->vtx[index], MemPoolRemovalReason::BLOCK, 0 /* mempool_sequence */);
13271327
}
13281328
}
13291329

1330-
void CWallet::blockDisconnected(const CBlock& block, int height)
1330+
void CWallet::blockDisconnected(const interfaces::BlockInfo& block)
13311331
{
1332+
assert(block.data);
13321333
LOCK(cs_wallet);
13331334

13341335
// At block disconnection, this will change an abandoned transaction to
13351336
// be unconfirmed, whether or not the transaction is added back to the mempool.
13361337
// User may have to call abandontransaction again. It may be addressed in the
13371338
// future with a stickier abandoned state or even removing abandontransaction call.
1338-
m_last_block_processed_height = height - 1;
1339-
m_last_block_processed = block.hashPrevBlock;
1340-
for (const CTransactionRef& ptx : block.vtx) {
1339+
m_last_block_processed_height = block.height - 1;
1340+
m_last_block_processed = *Assert(block.prev_hash);
1341+
for (const CTransactionRef& ptx : Assert(block.data)->vtx) {
13411342
SyncTransaction(ptx, TxStateInactive{});
13421343
}
13431344
}

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,8 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
508508
CWalletTx* AddToWallet(CTransactionRef tx, const TxState& state, const UpdateWalletTxFn& update_wtx=nullptr, bool fFlushOnClose=true, bool rescanning_old_block = false);
509509
bool LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
510510
void transactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) override;
511-
void blockConnected(const CBlock& block, int height) override;
512-
void blockDisconnected(const CBlock& block, int height) override;
511+
void blockConnected(const interfaces::BlockInfo& block) override;
512+
void blockDisconnected(const interfaces::BlockInfo& block) override;
513513
void updatedBlockTip() override;
514514
int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
515515

0 commit comments

Comments
 (0)