@@ -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
240241int 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
259257std::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
901904bool 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
0 commit comments