Skip to content

Commit

Permalink
wallet: Avoid use of Chain::Lock in importmulti
Browse files Browse the repository at this point in the history
This is a step toward removing the Chain::Lock class and reducing cs_main
locking.

This change only affects behavior in the case where wallet last block processed
falls behind the chain tip, in which case it may use a more accurate rescan
time.
  • Loading branch information
ryanofsky committed Mar 31, 2020
1 parent 25a9fcf commit bc96a9b
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 19 deletions.
7 changes: 0 additions & 7 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex>
assert(block != nullptr);
return block->GetBlockTime();
}
int64_t getBlockMedianTimePast(int height) override
{
LockAssertion lock(::cs_main);
CBlockIndex* block = ::ChainActive()[height];
assert(block != nullptr);
return block->GetMedianTimePast();
}
bool haveBlockOnDisk(int height) override
{
LockAssertion lock(::cs_main);
Expand Down
4 changes: 0 additions & 4 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ class Chain
//! Get block time. Height must be valid or this function will abort.
virtual int64_t getBlockTime(int height) = 0;

//! Get block median time past. Height must be valid or this function
//! will abort.
virtual int64_t getBlockMedianTimePast(int height) = 0;

//! Check that the block is available on disk (i.e. has not been
//! pruned), and contains transactions.
virtual bool haveBlockOnDisk(int height) = 0;
Expand Down
9 changes: 1 addition & 8 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1379,20 +1379,13 @@ UniValue importmulti(const JSONRPCRequest& mainRequest)
EnsureWalletIsUnlocked(pwallet);

// Verify all timestamps are present before importing any keys.
const Optional<int> tip_height = locked_chain->getHeight();
now = tip_height ? locked_chain->getBlockMedianTimePast(*tip_height) : 0;
CHECK_NONFATAL(pwallet->chain().findBlock(pwallet->GetLastBlockHash(), FoundBlock().time(nLowestTimestamp).mtpTime(now)));
for (const UniValue& data : requests.getValues()) {
GetImportTimestamp(data, now);
}

const int64_t minimumTimestamp = 1;

if (fRescan && tip_height) {
nLowestTimestamp = locked_chain->getBlockTime(*tip_height);
} else {
fRescan = false;
}

for (const UniValue& data : requests.getValues()) {
const int64_t timestamp = std::max(GetImportTimestamp(data, now), minimumTimestamp);
const UniValue result = ProcessImport(pwallet, data, timestamp);
Expand Down
1 change: 1 addition & 0 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup)
{
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(chain.get(), WalletLocation(), WalletDatabase::CreateDummy());
wallet->SetupLegacyScriptPubKeyMan();
WITH_LOCK(wallet->cs_wallet, wallet->SetLastBlockProcessed(newTip->nHeight, newTip->GetBlockHash()));
AddWallet(wallet);
UniValue keys;
keys.setArray();
Expand Down

0 comments on commit bc96a9b

Please sign in to comment.