@@ -221,6 +221,12 @@ UniValue importaddress(const JSONRPCRequest& request)
221221 const std::string strLabel = (request.params .size () > 1 ? request.params [1 ].get_str () : " " );
222222 // Whether to perform rescan after import
223223 const bool fRescan = (request.params .size () > 2 ? request.params [2 ].get_bool () : true );
224+
225+ WalletRescanReserver reserver (pwalletMain);
226+ if (fRescan && !reserver.reserve ()) {
227+ throw JSONRPCError (RPC_WALLET_ERROR, " Wallet is currently rescanning. Abort existing rescan or wait." );
228+ }
229+
224230 // Whether to import a p2sh version, too
225231 const bool fP2SH = (request.params .size () > 3 ? request.params [3 ].get_bool () : false );
226232
@@ -276,6 +282,11 @@ UniValue importpubkey(const JSONRPCRequest& request)
276282 // Whether to perform rescan after import
277283 const bool fRescan = (request.params .size () > 2 ? request.params [2 ].get_bool () : true );
278284
285+ WalletRescanReserver reserver (pwalletMain);
286+ if (fRescan && !reserver.reserve ()) {
287+ throw JSONRPCError (RPC_WALLET_ERROR, " Wallet is currently rescanning. Abort existing rescan or wait." );
288+ }
289+
279290 if (!IsHex (request.params [0 ].get_str ()))
280291 throw JSONRPCError (RPC_INVALID_ADDRESS_OR_KEY, " Pubkey must be a hex string" );
281292 std::vector<unsigned char > data (ParseHex (request.params [0 ].get_str ()));
@@ -317,19 +328,23 @@ UniValue importwallet(const JSONRPCRequest& request)
317328 " \n Import using the json rpc call\n " +
318329 HelpExampleRpc (" importwallet" , " \" test\" " ));
319330
320-
321331 std::ifstream file;
322332 file.open (request.params [0 ].get_str ().c_str (), std::ios::in | std::ios::ate);
323333 if (!file.is_open ())
324334 throw JSONRPCError (RPC_INVALID_PARAMETER, " Cannot open wallet dump file" );
325335
326- int64_t nTimeBegin = chainActive.Tip ()->GetBlockTime ();
336+ WalletRescanReserver reserver (pwalletMain);
337+ if (!reserver.reserve ()) {
338+ throw JSONRPCError (RPC_WALLET_ERROR, " Wallet is currently rescanning. Abort existing rescan or wait." );
339+ }
327340
341+ int64_t nTimeBegin = 0 ;
328342 bool fGood = true ;
329343 {
330344 LOCK2 (cs_main, pwalletMain->cs_wallet );
331345 EnsureWalletIsUnlocked ();
332346
347+ nTimeBegin = chainActive.Tip ()->GetBlockTime ();
333348 int64_t nFilesize = std::max ((int64_t )1 , (int64_t )file.tellg ());
334349 file.seekg (0 , file.beg );
335350
@@ -973,6 +988,11 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
973988 }
974989 }
975990
991+ WalletRescanReserver reserver (pwalletMain);
992+ if (fRescan && !reserver.reserve ()) {
993+ throw JSONRPCError (RPC_WALLET_ERROR, " Wallet is currently rescanning. Abort existing rescan or wait." );
994+ }
995+
976996 int64_t now = 0 ;
977997 bool fRunScan = false ;
978998 int64_t nLowestTimestamp = 0 ;
0 commit comments