Skip to content

Commit ef73ed0

Browse files
committed
fixup: for collateral age change
1 parent 2549f6b commit ef73ed0

File tree

2 files changed

+62
-51
lines changed

2 files changed

+62
-51
lines changed

divi/src/masternode.cpp

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ CMasternode::CMasternode()
122122
activeState = MASTERNODE_ENABLED;
123123
sigTime = GetAdjustedTime();
124124
lastPing = CMasternodePing();
125+
collateralBlock.SetNull();
125126
allowFreeTx = true;
126127
nActiveState = MASTERNODE_ENABLED;
127128
protocolVersion = PROTOCOL_VERSION;
@@ -142,7 +143,7 @@ CMasternode::CMasternode(const CMasternode& other)
142143
activeState = other.activeState;
143144
sigTime = other.sigTime;
144145
lastPing = other.lastPing;
145-
collateralHeight = other.collateralHeight;
146+
collateralBlock = other.collateralBlock;
146147
allowFreeTx = other.allowFreeTx;
147148
nActiveState = MASTERNODE_ENABLED;
148149
protocolVersion = other.protocolVersion;
@@ -163,7 +164,7 @@ CMasternode::CMasternode(const CMasternodeBroadcast& mnb)
163164
activeState = MASTERNODE_ENABLED;
164165
sigTime = mnb.sigTime;
165166
lastPing = mnb.lastPing;
166-
collateralHeight = mnb.collateralHeight;
167+
collateralBlock = mnb.collateralBlock;
167168
allowFreeTx = true;
168169
nActiveState = MASTERNODE_ENABLED;
169170
protocolVersion = mnb.protocolVersion;
@@ -188,7 +189,7 @@ void CMasternode::swap(CMasternode& first, CMasternode& second) // nothrow
188189
swap(first.activeState, second.activeState);
189190
swap(first.sigTime, second.sigTime);
190191
swap(first.lastPing, second.lastPing);
191-
swap(first.collateralHeight, second.collateralHeight);
192+
swap(first.collateralBlock, second.collateralBlock);
192193
swap(first.allowFreeTx, second.allowFreeTx);
193194
swap(first.protocolVersion, second.protocolVersion);
194195
swap(first.nScanningErrorCount, second.nScanningErrorCount);
@@ -239,21 +240,18 @@ bool CMasternode::IsEnabled() const
239240

240241
int CMasternode::GetMasternodeInputAge() const
241242
{
242-
if (collateralHeight == 0)
243+
LOCK(cs_main);
244+
245+
const auto* pindex = GetCollateralBlock();
246+
if (pindex == nullptr)
243247
return 0;
244248

245-
unsigned tipHeight;
246-
{
247-
LOCK(cs_main);
248-
if (chainActive.Tip() == nullptr)
249-
return 0;
250-
tipHeight = chainActive.Tip()->nHeight;
251-
}
249+
assert(chainActive.Contains(pindex));
252250

253-
if (tipHeight < collateralHeight)
254-
return 0;
251+
const unsigned tipHeight = chainActive.Height();
252+
assert(tipHeight >= pindex->nHeight);
255253

256-
return tipHeight - collateralHeight + 1;
254+
return tipHeight - pindex->nHeight + 1;
257255
}
258256

259257
std::string CMasternode::Status() const
@@ -745,9 +743,8 @@ CMasternodePing createDelayedMasternodePing(const CMasternodeBroadcast& mnb)
745743
CMasternodePing ping;
746744
const int64_t offsetTimeBy45BlocksInSeconds = 60 * 45;
747745
ping.vin = mnb.vin;
748-
const auto* confBlock = mnb.GetCollateralConfirmationBlock();
749746
const int tipHeight = chainActive.Height();
750-
const int depthOfTx = (confBlock == nullptr ? 0 : tipHeight - confBlock->nHeight);
747+
const int depthOfTx = mnb.GetMasternodeInputAge();
751748
const int offset = std::min( std::max(0, depthOfTx), 12 );
752749
const auto* block = chainActive[tipHeight - offset];
753750
ping.blockHash = block->GetBlockHash();
@@ -872,30 +869,36 @@ bool CMasternodeBroadcast::CheckAndUpdate(int& nDos)
872869
return true;
873870
}
874871

875-
const CBlockIndex* CMasternodeBroadcast::GetCollateralConfirmationBlock() const
872+
const CBlockIndex* CMasternode::GetCollateralBlock() const
876873
{
874+
LOCK(cs_main);
875+
876+
if (!collateralBlock.IsNull()) {
877+
const auto mi = mapBlockIndex.find(collateralBlock);
878+
if (mi != mapBlockIndex.end() && chainActive.Contains(mi->second))
879+
return mi->second;
880+
}
881+
877882
uint256 hashBlock;
878883
CTransaction tx;
879-
if (!GetTransaction(vin.prevout.hash, tx, hashBlock, true))
884+
if (!GetTransaction(vin.prevout.hash, tx, hashBlock, true)) {
885+
collateralBlock.SetNull();
880886
return nullptr;
887+
}
881888

882889
const auto mi = mapBlockIndex.find(hashBlock);
883-
if (mi == mapBlockIndex.end() || mi->second == nullptr)
890+
if (mi == mapBlockIndex.end() || mi->second == nullptr) {
891+
collateralBlock.SetNull();
884892
return nullptr;
885-
886-
const CBlockIndex* conf;
887-
{
888-
LOCK(cs_main);
889-
if (!chainActive.Contains(mi->second))
890-
return nullptr;
891-
conf = chainActive[mi->second->nHeight + MASTERNODE_MIN_CONFIRMATIONS - 1];
892893
}
893894

894-
if (conf == nullptr)
895+
if (!chainActive.Contains(mi->second)) {
896+
collateralBlock.SetNull();
895897
return nullptr;
898+
}
896899

897-
assert(conf->GetAncestor(mi->second->nHeight) == mi->second);
898-
return conf;
900+
collateralBlock = hashBlock;
901+
return mi->second;
899902
}
900903

901904
bool CMasternodeBroadcast::CheckInputs(int& nDoS) const
@@ -919,18 +922,29 @@ bool CMasternodeBroadcast::CheckInputs(int& nDoS) const
919922

920923
LogPrint("masternode", "mnb - Accepted Masternode entry\n");
921924

922-
const auto* confBlock = GetCollateralConfirmationBlock();
923-
if (confBlock == nullptr) {
925+
const CBlockIndex* pindexConf;
926+
{
927+
LOCK(cs_main);
928+
const auto* pindexCollateral = GetCollateralBlock();
929+
if (pindexCollateral == nullptr)
930+
pindexConf = nullptr;
931+
else {
932+
assert(chainActive.Contains(pindexCollateral));
933+
pindexConf = chainActive[pindexCollateral->nHeight + MASTERNODE_MIN_CONFIRMATIONS - 1];
934+
assert(pindexConf == nullptr || pindexConf->GetAncestor(pindexCollateral->nHeight) == pindexCollateral);
935+
}
936+
}
937+
938+
if (pindexConf == nullptr) {
924939
LogPrint("masternode","mnb - Input must have at least %d confirmations\n", MASTERNODE_MIN_CONFIRMATIONS);
925940
return false;
926941
}
927-
collateralHeight = confBlock->nHeight - MASTERNODE_MIN_CONFIRMATIONS + 1;
928942

929943
// verify that sig time is legit in past
930944
// should be at least not earlier than block when 1000 PIV tx got MASTERNODE_MIN_CONFIRMATIONS
931-
if (confBlock->GetBlockTime() > sigTime) {
945+
if (pindexConf->GetBlockTime() > sigTime) {
932946
LogPrint("masternode","mnb - Bad sigTime %d for Masternode %s (%i conf block is at %d)\n",
933-
sigTime, vin.prevout.hash.ToString(), MASTERNODE_MIN_CONFIRMATIONS, confBlock->GetBlockTime());
947+
sigTime, vin.prevout.hash.ToString(), MASTERNODE_MIN_CONFIRMATIONS, pindexConf->GetBlockTime());
934948
return false;
935949
}
936950

divi/src/masternode.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,16 @@ class CMasternode
126126
mutable CCriticalSection cs;
127127
int64_t lastTimeChecked;
128128

129-
mutable int cacheInputAge;
130-
mutable int cacheInputAgeBlock;
129+
protected:
130+
131+
/** Cached block hash of where the collateral output of this
132+
* masternode got included. */
133+
mutable uint256 collateralBlock;
134+
135+
/** Looks up and returns the block index when the collateral got
136+
* included in the currently active chain. If it is not yet confirmed
137+
* then this returns nullptr. */
138+
const CBlockIndex* GetCollateralBlock() const;
131139

132140
public:
133141
enum state {
@@ -157,15 +165,10 @@ class CMasternode
157165
MasternodeTier nTier;
158166
CMasternodePing lastPing;
159167

160-
/** Height of the collateral UTXO. This is set when checking
161-
* the collateral in CheckInputs for a broadcast. */
162-
mutable int collateralHeight = 0;
163-
164168
CMasternode();
165169
CMasternode(const CMasternode& other);
166170
CMasternode(const CMasternodeBroadcast& mnb);
167171

168-
169172
void swap(CMasternode& first, CMasternode& second); // nothrow
170173

171174
CMasternode& operator=(CMasternode from);
@@ -200,7 +203,7 @@ class CMasternode
200203
READWRITE(protocolVersion);
201204
READWRITE(activeState);
202205
READWRITE(lastPing);
203-
READWRITE(collateralHeight);
206+
READWRITE(collateralBlock);
204207
READWRITE(allowFreeTx);
205208
READWRITE(nScanningErrorCount);
206209
READWRITE(nLastScanningErrorBlockHeight);
@@ -262,14 +265,6 @@ class CMasternodeBroadcast : public CMasternode
262265
int protocolVersionIn);
263266
CMasternodeBroadcast(const CMasternode& mn);
264267

265-
/** Looks up and returns the block index when the collateral got its
266-
* minimum confirmation. That block's time is part of the condition
267-
* for verifying the signature time.
268-
*
269-
* If the collateral does not yet have sufficient confirmations (i.e.
270-
* that block does not exist), then the function returns nullptr. */
271-
const CBlockIndex* GetCollateralConfirmationBlock() const;
272-
273268
bool CheckAndUpdate(int& nDoS);
274269
bool CheckInputs(int& nDos) const;
275270
bool Sign(CKey& keyCollateralAddress, bool updateTimeBeforeSigning = true);
@@ -294,8 +289,10 @@ class CMasternodeBroadcast : public CMasternode
294289
if (!ser_action.ForRead ())
295290
tier = static_cast<int> (nTier);
296291
READWRITE(tier);
297-
if (ser_action.ForRead ())
292+
if (ser_action.ForRead ()) {
298293
nTier = static_cast<MasternodeTier> (tier);
294+
collateralBlock.SetNull();
295+
}
299296
}
300297

301298
uint256 GetHash() const

0 commit comments

Comments
 (0)