Skip to content

Commit c013bf1

Browse files
committed
[Refactoring] Add evo Notification Interface
1 parent 180a311 commit c013bf1

File tree

10 files changed

+97
-6
lines changed

10 files changed

+97
-6
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ set(COMMON_SOURCES
385385
./src/key_io.cpp
386386
./src/compressor.cpp
387387
./src/evo/deterministicmns.cpp
388+
./src/evo/evonotificationinterface.cpp
388389
./src/evo/evodb.cpp
389390
./src/evo/providertx.cpp
390391
./src/evo/specialtx.cpp

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ BITCOIN_CORE_H = \
184184
cyclingvector.h \
185185
evo/deterministicmns.h \
186186
evo/evodb.h \
187+
evo/evonotificationinterface.h \
187188
evo/providertx.h \
188189
evo/specialtx.h \
189190
pairresult.h \
@@ -330,6 +331,7 @@ libbitcoin_server_a_SOURCES = \
330331
consensus/zerocoin_verify.cpp \
331332
evo/deterministicmns.cpp \
332333
evo/evodb.cpp \
334+
evo/evonotificationinterface.cpp \
333335
evo/providertx.cpp \
334336
evo/specialtx.cpp \
335337
httprpc.cpp \

src/evo/deterministicmns.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "chainparams.h"
1010
#include "core_io.h"
1111
#include "evo/specialtx.h"
12+
#include "guiinterface.h"
1213
#include "masternode.h" // for MN_COLL_AMT, MasternodeCollateralMinConf
1314
#include "script/standard.h"
1415
#include "sync.h"
@@ -543,7 +544,11 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, const CBlockInde
543544
return _state.DoS(100, false, REJECT_INVALID, "failed-dmn-block");
544545
}
545546

546-
// !TODO: notify listeners that the mn list has changed
547+
// Don't hold cs while calling signals
548+
if (diff.HasChanges()) {
549+
GetMainSignals().NotifyMasternodeListChanged(false, oldList, diff);
550+
uiInterface.NotifyMasternodeListChanged(newList);
551+
}
547552

548553
LOCK(cs);
549554
CleanupCache(nHeight);
@@ -575,7 +580,8 @@ bool CDeterministicMNManager::UndoBlock(const CBlock& block, const CBlockIndex*
575580

576581
if (diff.HasChanges()) {
577582
auto inversedDiff = curList.BuildDiff(prevList);
578-
// !TODO: notify listeners that the masternode list has changed
583+
GetMainSignals().NotifyMasternodeListChanged(true, curList, inversedDiff);
584+
uiInterface.NotifyMasternodeListChanged(prevList);
579585
}
580586

581587
return true;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2014-2019 The Dash 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 "evo/evonotificationinterface.h"
6+
7+
#include "evo/deterministicmns.h"
8+
#include "validation.h"
9+
10+
void EvoNotificationInterface::InitializeCurrentBlockTip()
11+
{
12+
LOCK(cs_main);
13+
UpdatedBlockTip(chainActive.Tip(), nullptr, IsInitialBlockDownload());
14+
}
15+
16+
void EvoNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
17+
{
18+
deterministicMNManager->UpdatedBlockTip(pindexNew);
19+
}
20+
21+
void EvoNotificationInterface::NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff)
22+
{
23+
// !TODO
24+
}

src/evo/evonotificationinterface.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2021 The PIVX 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 EVONOTIFICATIONINTERFACE_H
6+
#define EVONOTIFICATIONINTERFACE_H
7+
8+
#include "validationinterface.h"
9+
10+
class EvoNotificationInterface : public CValidationInterface
11+
{
12+
public:
13+
explicit EvoNotificationInterface(CConnman& connmanIn): connman(connmanIn) {}
14+
virtual ~EvoNotificationInterface() = default;
15+
16+
// a small helper to initialize current block height in sub-modules on startup
17+
void InitializeCurrentBlockTip();
18+
19+
protected:
20+
// CValidationInterface
21+
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
22+
void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff) override;
23+
24+
private:
25+
CConnman& connman;
26+
};
27+
28+
#endif // EVONOTIFICATIONINTERFACE_H

src/guiinterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CBasicKeyStore;
1818
class CWallet;
1919
class uint256;
2020
class CBlockIndex;
21+
class CDeterministicMNList;
2122

2223
/** General change type (added, updated, removed). */
2324
enum ChangeType {
@@ -102,6 +103,9 @@ class CClientUIInterface
102103

103104
/** Banlist did change. */
104105
boost::signals2::signal<void (void)> BannedListChanged;
106+
107+
/** Deterministic Masternode list has changed */
108+
boost::signals2::signal<void (const CDeterministicMNList&)> NotifyMasternodeListChanged;
105109
};
106110

107111
extern CClientUIInterface uiInterface;

src/init.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "checkpoints.h"
2323
#include "compat/sanity.h"
2424
#include "consensus/upgrades.h"
25+
#include "evo/evonotificationinterface.h"
2526
#include "fs.h"
2627
#include "httpserver.h"
2728
#include "httprpc.h"
@@ -105,6 +106,8 @@ std::unique_ptr<PeerLogicValidation> peerLogic;
105106
static CZMQNotificationInterface* pzmqNotificationInterface = NULL;
106107
#endif
107108

109+
static EvoNotificationInterface* pEvoNotificationInterface = nullptr;
110+
108111
#ifdef WIN32
109112
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
110113
// accessing block files, don't count towards to fd_set size limit
@@ -307,11 +310,17 @@ void PrepareShutdown()
307310
bitdb.Flush(true);
308311
#endif
309312

313+
if (pEvoNotificationInterface) {
314+
UnregisterValidationInterface(pEvoNotificationInterface);
315+
delete pEvoNotificationInterface;
316+
pEvoNotificationInterface = nullptr;
317+
}
318+
310319
#if ENABLE_ZMQ
311320
if (pzmqNotificationInterface) {
312321
UnregisterValidationInterface(pzmqNotificationInterface);
313322
delete pzmqNotificationInterface;
314-
pzmqNotificationInterface = NULL;
323+
pzmqNotificationInterface = nullptr;
315324
}
316325
#endif
317326

@@ -719,6 +728,10 @@ void ThreadImport(const std::vector<fs::path>& vImportFiles)
719728
StartShutdown();
720729
}
721730

731+
// force UpdatedBlockTip to initialize nCachedBlockHeight for DS, MN payments and budgets
732+
// but don't call it directly to prevent triggering of other listeners like zmq etc.
733+
pEvoNotificationInterface->InitializeCurrentBlockTip();
734+
722735
if (gArgs.GetBoolArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
723736
LoadMempool(::mempool);
724737
}
@@ -1536,6 +1549,9 @@ bool AppInitMain()
15361549
}
15371550
#endif
15381551

1552+
pEvoNotificationInterface = new EvoNotificationInterface(connman);
1553+
RegisterValidationInterface(pEvoNotificationInterface);
1554+
15391555
// ********************************************************* Step 7: load block chain
15401556

15411557
fReindex = gArgs.GetBoolArg("-reindex", false);

src/validation.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,9 +2415,6 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb
24152415
// Notify ValidationInterface subscribers
24162416
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
24172417

2418-
// TODO: move to notification interface
2419-
deterministicMNManager->UpdatedBlockTip(pindexNewTip);
2420-
24212418
// Always notify the UI if a new block tip was connected
24222419
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
24232420
}

src/validationinterface.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct ValidationInterfaceConnections {
2222
boost::signals2::scoped_connection SetBestChain;
2323
boost::signals2::scoped_connection Broadcast;
2424
boost::signals2::scoped_connection BlockChecked;
25+
boost::signals2::scoped_connection NotifyMasternodeListChanged;
2526
};
2627

2728
struct MainSignalsInstance {
@@ -45,6 +46,8 @@ struct MainSignalsInstance {
4546
boost::signals2::signal<void (CConnman* connman)> Broadcast;
4647
/** Notifies listeners of a block validation result */
4748
boost::signals2::signal<void (const CBlock&, const CValidationState&)> BlockChecked;
49+
/** Notifies listeners of updated deterministic masternode list */
50+
boost::signals2::signal<void (bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff)> NotifyMasternodeListChanged;
4851

4952
std::unordered_map<CValidationInterface*, ValidationInterfaceConnections> m_connMainSignals;
5053

@@ -94,6 +97,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn)
9497
conns.SetBestChain = g_signals.m_internals->SetBestChain.connect(std::bind(&CValidationInterface::SetBestChain, pwalletIn, std::placeholders::_1));
9598
conns.Broadcast = g_signals.m_internals->Broadcast.connect(std::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, std::placeholders::_1));
9699
conns.BlockChecked = g_signals.m_internals->BlockChecked.connect(std::bind(&CValidationInterface::BlockChecked, pwalletIn, std::placeholders::_1, std::placeholders::_2));
100+
conns.NotifyMasternodeListChanged = g_signals.m_internals->NotifyMasternodeListChanged.connect(std::bind(&CValidationInterface::NotifyMasternodeListChanged, pwalletIn, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
97101
}
98102

99103
void UnregisterValidationInterface(CValidationInterface* pwalletIn)
@@ -175,3 +179,7 @@ void CMainSignals::Broadcast(CConnman* connman) {
175179
void CMainSignals::BlockChecked(const CBlock& block, const CValidationState& state) {
176180
m_internals->BlockChecked(block, state);
177181
}
182+
183+
void CMainSignals::NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff) {
184+
m_internals->NotifyMasternodeListChanged(undo, oldMNList, diff);
185+
}

src/validationinterface.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class CBlock;
1818
struct CBlockLocator;
1919
class CBlockIndex;
2020
class CConnman;
21+
class CDeterministicMNList;
22+
class CDeterministicMNListDiff;
2123
class CValidationInterface;
2224
class CValidationState;
2325
class uint256;
@@ -146,6 +148,8 @@ class CValidationInterface {
146148
friend void ::RegisterValidationInterface(CValidationInterface*);
147149
friend void ::UnregisterValidationInterface(CValidationInterface*);
148150
friend void ::UnregisterAllValidationInterfaces();
151+
/** Notifies listeners of updated deterministic masternode list */
152+
virtual void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff) {}
149153
};
150154

151155
struct MainSignalsInstance;
@@ -176,6 +180,7 @@ class CMainSignals {
176180
void SetBestChain(const CBlockLocator &);
177181
void Broadcast(CConnman* connman);
178182
void BlockChecked(const CBlock&, const CValidationState&);
183+
void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff);
179184
};
180185

181186
CMainSignals& GetMainSignals();

0 commit comments

Comments
 (0)