@@ -629,22 +629,27 @@ Value submitblock(const Array& params, bool fHelp)
629629 throw JSONRPCError (RPC_DESERIALIZATION_ERROR, " Block decode failed" );
630630
631631 uint256 hash = block.GetHash ();
632- BlockMap::iterator mi = mapBlockIndex.find (hash);
633- if (mi != mapBlockIndex.end ()) {
634- CBlockIndex *pindex = mi->second ;
635- if (pindex->IsValid (BLOCK_VALID_SCRIPTS))
636- return " duplicate" ;
637- if (pindex->nStatus & BLOCK_FAILED_MASK)
638- return " duplicate-invalid" ;
639- // Otherwise, we might only have the header - process the block before returning
632+ bool fBlockPresent = false ;
633+ {
634+ LOCK (cs_main);
635+ BlockMap::iterator mi = mapBlockIndex.find (hash);
636+ if (mi != mapBlockIndex.end ()) {
637+ CBlockIndex *pindex = mi->second ;
638+ if (pindex->IsValid (BLOCK_VALID_SCRIPTS))
639+ return " duplicate" ;
640+ if (pindex->nStatus & BLOCK_FAILED_MASK)
641+ return " duplicate-invalid" ;
642+ // Otherwise, we might only have the header - process the block before returning
643+ fBlockPresent = true ;
644+ }
640645 }
641646
642647 CValidationState state;
643648 submitblock_StateCatcher sc (block.GetHash ());
644649 RegisterValidationInterface (&sc);
645650 bool fAccepted = ProcessNewBlock (state, NULL , &block);
646651 UnregisterValidationInterface (&sc);
647- if (mi != mapBlockIndex. end () )
652+ if (fBlockPresent )
648653 {
649654 if (fAccepted && !sc.found )
650655 return " duplicate-inconclusive" ;
0 commit comments