gui: Fix race in WalletModel::pollBalanceChanged
gui: Fix race in WalletModel::pollBalanceChanged

Poll function was wrongly setting cached height to the current chain height
instead of the chain height at the time of polling.

This bug could cause balances to appear out of date, and was first introduced
https://github.com/bitcoin/bitcoin/pull/10244/commits/a0704a8996bb950ae3c4d5b5a30e9dfe34cde1d3#r378452145
Before that commit, there wasn't a problem because cs_main was held during the
poll update.

Currently, the problem should be rare. But if
8937d99ce81a27ae5e1012a28323c0e26d89c50b from #17954 were merged, the problem
would get worse, because the wrong cachedNumBlocks value would be set if the
wallet was polled in the interval between a block being connected and it
processing the BlockConnected notification.

MarcoFalke <falke.marco@gmail.com> also points out that a0704a8996b could lead
to GUI hangs as well, because previously the pollBalanceChanged method, which
runs on the GUI thread, would only make a nonblocking TRY_LOCK(cs_main) call,
but after could make blocking LOCK(cs_main) calls, potentially locking up the
GUI.

Thanks to John Newbery <john@johnnewbery.com> for finding this bug this while
reviewing https://github.com/bitcoin/bitcoin/pull/17954.

namecoin/namecore,bitcoin/bitcoin,midnightmagic/bitcoin,dscotese/bitcoin,rnicoll/dogecoin,yenliangl/bitcoin,kallewoof/bitcoin,DigitalPandacoin/pandacoin,dscotese/bitcoin,GroestlCoin/GroestlCoin,monacoinproject/monacoin,sipsorcery/bitcoin,bitcoin/bitcoin,alecalve/bitcoin,mitchellcash/bitcoin,Xekyo/bitcoin,jnewbery/bitcoin,jambolo/bitcoin,fanquake/bitcoin,fanquake/bitcoin,achow101/bitcoin,namecoin/namecoin-core,qtumproject/qtum,bitcoin/bitcoin,GroestlCoin/GroestlCoin,midnightmagic/bitcoin,sstone/bitcoin,dscotese/bitcoin,jamesob/bitcoin,jlopp/statoshi,MeshCollider/bitcoin,instagibbs/bitcoin,Xekyo/bitcoin,MarcoFalke/bitcoin,fujicoin/fujicoin,GroestlCoin/bitcoin,jonasschnelli/bitcoin,yenliangl/bitcoin,anditto/bitcoin,JeremyRubin/bitcoin,prusnak/bitcoin,practicalswift/bitcoin,bitcoinknots/bitcoin,peercoin/peercoin,bitcoin/bitcoin,GroestlCoin/bitcoin,sstone/bitcoin,Sjors/bitcoin,qtumproject/qtum,tecnovert/particl-core,mm-s/bitcoin,rnicoll/bitcoin,EthanHeilman/bitcoin,peercoin/peercoin,mm-s/bitcoin,fanquake/bitcoin,mitchellcash/bitcoin,apoelstra/bitcoin,mruddy/bitcoin,jambolo/bitcoin,midnightmagic/bitcoin,achow101/bitcoin,jonasschnelli/bitcoin,n1bor/bitcoin,domob1812/bitcoin,GroestlCoin/bitcoin,tecnovert/particl-core,Sjors/bitcoin,ElementsProject/elements,EthanHeilman/bitcoin,bitcoin/bitcoin,sstone/bitcoin,tecnovert/particl-core,bitcoinsSG/bitcoin,jlopp/statoshi,pataquets/namecoin-core,alecalve/bitcoin,namecoin/namecore,n1bor/bitcoin,ajtowns/bitcoin,mm-s/bitcoin,jamesob/bitcoin,cdecker/bitcoin,anditto/bitcoin,jambolo/bitcoin,DigitalPandacoin/pandacoin,kallewoof/bitcoin,n1bor/bitcoin,rnicoll/dogecoin,sipsorcery/bitcoin,lateminer/bitcoin,monacoinproject/monacoin,andreaskern/bitcoin,ajtowns/bitcoin,practicalswift/bitcoin,pataquets/namecoin-core,EthanHeilman/bitcoin,mm-s/bitcoin,monacoinproject/monacoin,jlopp/statoshi,jlopp/statoshi,prusnak/bitcoin,bitcoinknots/bitcoin,jambolo/bitcoin,pstratem/bitcoin,namecoin/namecore,lateminer/bitcoin,AkioNak/bitcoin,GroestlCoin/GroestlCoin,MeshCollider/bitcoin,namecoin/namecore,ahmedbodi/vertcoin,pataquets/namecoin-core,litecoin-project/litecoin,kallewoof/bitcoin,EthanHeilman/bitcoin,GroestlCoin/bitcoin,yenliangl/bitcoin,GroestlCoin/GroestlCoin,JeremyRubin/bitcoin,pstratem/bitcoin,vertcoin/vertcoin,kallewoof/bitcoin,rnicoll/dogecoin,yenliangl/bitcoin,ahmedbodi/vertcoin,litecoin-project/litecoin,gjhiggins/vcoincore,DigitalPandacoin/pandacoin,rnicoll/bitcoin,sstone/bitcoin,gjhiggins/vcoincore,bitcoinsSG/bitcoin,monacoinproject/monacoin,gjhiggins/vcoincore,cdecker/bitcoin,pstratem/bitcoin,particl/particl-core,andreaskern/bitcoin,fanquake/bitcoin,apoelstra/bitcoin,bitcoinsSG/bitcoin,DigitalPandacoin/pandacoin,particl/particl-core,bitcoin/bitcoin,MeshCollider/bitcoin,gjhiggins/vcoincore,mruddy/bitcoin,domob1812/namecore,particl/particl-core,lateminer/bitcoin,jlopp/statoshi,jonasschnelli/bitcoin,anditto/bitcoin,sstone/bitcoin,Xekyo/bitcoin,domob1812/namecore,rnicoll/bitcoin,monacoinproject/monacoin,anditto/bitcoin,jnewbery/bitcoin,practicalswift/bitcoin,litecoin-project/litecoin,dscotese/bitcoin,dscotese/bitcoin,mruddy/bitcoin,andreaskern/bitcoin,n1bor/bitcoin,achow101/bitcoin,mruddy/bitcoin,midnightmagic/bitcoin,ElementsProject/elements,GroestlCoin/bitcoin,MarcoFalke/bitcoin,namecoin/namecoin-core,fujicoin/fujicoin,domob1812/bitcoin,bitcoinsSG/bitcoin,domob1812/namecore,tecnovert/particl-core,bitcoinknots/bitcoin,n1bor/bitcoin,cdecker/bitcoin,fujicoin/fujicoin,litecoin-project/litecoin,pataquets/namecoin-core,namecoin/namecore,yenliangl/bitcoin,sipsorcery/bitcoin,EthanHeilman/bitcoin,domob1812/bitcoin,cdecker/bitcoin,sipsorcery/bitcoin,sipsorcery/bitcoin,rnicoll/dogecoin,prusnak/bitcoin,midnightmagic/bitcoin,ElementsProject/elements,lateminer/bitcoin,ajtowns/bitcoin,namecoin/namecoin-core,JeremyRubin/bitcoin,ahmedbodi/vertcoin,EthanHeilman/bitcoin,litecoin-project/litecoin,pataquets/namecoin-core,vertcoin/vertcoin,bitcoinsSG/bitcoin,litecoin-project/litecoin,vertcoin/vertcoin,domob1812/bitcoin,mitchellcash/bitcoin,tecnovert/particl-core,sipsorcery/bitcoin,apoelstra/bitcoin,peercoin/peercoin,rnicoll/bitcoin,MarcoFalke/bitcoin,domob1812/bitcoin,jlopp/statoshi,apoelstra/bitcoin,Sjors/bitcoin,MeshCollider/bitcoin,qtumproject/qtum,namecoin/namecoin-core,ajtowns/bitcoin,pstratem/bitcoin,ahmedbodi/vertcoin,fanquake/bitcoin,ahmedbodi/vertcoin,GroestlCoin/GroestlCoin,Xekyo/bitcoin,namecoin/namecoin-core,Xekyo/bitcoin,particl/particl-core,gjhiggins/vcoincore,vertcoin/vertcoin,ElementsProject/elements,jnewbery/bitcoin,AkioNak/bitcoin,jamesob/bitcoin,pataquets/namecoin-core,cdecker/bitcoin,MeshCollider/bitcoin,prusnak/bitcoin,ajtowns/bitcoin,jambolo/bitcoin,ajtowns/bitcoin,achow101/bitcoin,cdecker/bitcoin,instagibbs/bitcoin,JeremyRubin/bitcoin,rnicoll/bitcoin,alecalve/bitcoin,andreaskern/bitcoin,anditto/bitcoin,monacoinproject/monacoin,mruddy/bitcoin,vertcoin/vertcoin,mitchellcash/bitcoin,tecnovert/particl-core,JeremyRubin/bitcoin,jambolo/bitcoin,vertcoin/vertcoin,domob1812/namecore,AkioNak/bitcoin,bitcoinsSG/bitcoin,mm-s/bitcoin,jonasschnelli/bitcoin,rnicoll/bitcoin,pstratem/bitcoin,MarcoFalke/bitcoin,practicalswift/bitcoin,MeshCollider/bitcoin,peercoin/peercoin,sstone/bitcoin,ElementsProject/elements,GroestlCoin/GroestlCoin,namecoin/namecoin-core,peercoin/peercoin,practicalswift/bitcoin,ElementsProject/elements,fanquake/bitcoin,GroestlCoin/bitcoin,kallewoof/bitcoin,jnewbery/bitcoin,jonasschnelli/bitcoin,fujicoin/fujicoin,DigitalPandacoin/pandacoin,lateminer/bitcoin,instagibbs/bitcoin,alecalve/bitcoin,fujicoin/fujicoin,alecalve/bitcoin,prusnak/bitcoin,namecoin/namecore,kallewoof/bitcoin,mitchellcash/bitcoin,jamesob/bitcoin,ahmedbodi/vertcoin,qtumproject/qtum,peercoin/peercoin,achow101/bitcoin,AkioNak/bitcoin,instagibbs/bitcoin,domob1812/bitcoin,instagibbs/bitcoin,apoelstra/bitcoin,AkioNak/bitcoin,yenliangl/bitcoin,qtumproject/qtum,mm-s/bitcoin,qtumproject/qtum,bitcoinknots/bitcoin,mruddy/bitcoin,prusnak/bitcoin,DigitalPandacoin/pandacoin,MarcoFalke/bitcoin,particl/particl-core,midnightmagic/bitcoin,AkioNak/bitcoin,achow101/bitcoin,domob1812/namecore,Sjors/bitcoin,MarcoFalke/bitcoin,fujicoin/fujicoin,Xekyo/bitcoin,JeremyRubin/bitcoin,pstratem/bitcoin,gjhiggins/vcoincore,dscotese/bitcoin,mitchellcash/bitcoin,andreaskern/bitcoin,qtumproject/qtum,jamesob/bitcoin,domob1812/namecore,apoelstra/bitcoin,bitcoinknots/bitcoin,instagibbs/bitcoin,particl/particl-core,n1bor/bitcoin,Sjors/bitcoin,alecalve/bitcoin,jamesob/bitcoin,jnewbery/bitcoin,anditto/bitcoin,andreaskern/bitcoin,practicalswift/bitcoin,lateminer/bitcoin,rnicoll/dogecoin