33
33
#include " wallet/fees.h"
34
34
35
35
#include < assert.h>
36
+ #include < future>
36
37
37
38
#include < boost/algorithm/string/replace.hpp>
38
39
#include < boost/thread.hpp>
@@ -1232,6 +1233,8 @@ void CWallet::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const
1232
1233
for (size_t i = 0 ; i < pblock->vtx .size (); i++) {
1233
1234
SyncTransaction (pblock->vtx [i], pindex, i);
1234
1235
}
1236
+
1237
+ m_last_block_processed = pindex;
1235
1238
}
1236
1239
1237
1240
void CWallet::BlockDisconnected (const std::shared_ptr<const CBlock>& pblock) {
@@ -1244,6 +1247,36 @@ void CWallet::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock) {
1244
1247
1245
1248
1246
1249
1250
+ void CWallet::BlockUntilSyncedToCurrentChain () {
1251
+ AssertLockNotHeld (cs_main);
1252
+ AssertLockNotHeld (cs_wallet);
1253
+
1254
+ {
1255
+ // Skip the queue-draining stuff if we know we're caught up with
1256
+ // chainActive.Tip()...
1257
+ // We could also take cs_wallet here, and call m_last_block_processed
1258
+ // protected by cs_wallet instead of cs_main, but as long as we need
1259
+ // cs_main here anyway, its easier to just call it cs_main-protected.
1260
+ LOCK (cs_main);
1261
+ const CBlockIndex* initialChainTip = chainActive.Tip ();
1262
+
1263
+ if (m_last_block_processed->GetAncestor (initialChainTip->nHeight ) == initialChainTip) {
1264
+ return ;
1265
+ }
1266
+ }
1267
+
1268
+ // ...otherwise put a callback in the validation interface queue and wait
1269
+ // for the queue to drain enough to execute it (indicating we are caught up
1270
+ // at least with the time we entered this function).
1271
+
1272
+ std::promise<void > promise;
1273
+ CallFunctionInValidationInterfaceQueue ([&promise] {
1274
+ promise.set_value ();
1275
+ });
1276
+ promise.get_future ().wait ();
1277
+ }
1278
+
1279
+
1247
1280
isminetype CWallet::IsMine (const CTxIn &txin) const
1248
1281
{
1249
1282
{
@@ -3900,8 +3933,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
3900
3933
3901
3934
LogPrintf (" wallet %15dms\n " , GetTimeMillis () - nStart);
3902
3935
3903
- RegisterValidationInterface (walletInstance);
3904
-
3905
3936
// Try to top up keypool. No-op if the wallet is locked.
3906
3937
walletInstance->TopUpKeyPool ();
3907
3938
@@ -3913,6 +3944,10 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
3913
3944
if (walletdb.ReadBestBlock (locator))
3914
3945
pindexRescan = FindForkInGlobalIndex (chainActive, locator);
3915
3946
}
3947
+
3948
+ walletInstance->m_last_block_processed = chainActive.Tip ();
3949
+ RegisterValidationInterface (walletInstance);
3950
+
3916
3951
if (chainActive.Tip () && chainActive.Tip () != pindexRescan)
3917
3952
{
3918
3953
// We can't rescan beyond non-pruned blocks, stop and throw an error
0 commit comments