decred / dcrwallet

A secure Decred wallet daemon written in Go (golang).
https://decred.org
ISC License
216 stars 155 forks source link

wallet: Fix off-by-one in addr discovery #2319

Closed matheusd closed 9 months ago

matheusd commented 9 months ago

This fixes an off-by-one in sizing the backing array for cfilter data during address discovery.

During address discovery, the GetMainChainCFilters call to fetch cfilters from the DB uses an array sized by the caller to put the data. However, due to being an inclusive fetch, the call in the address finder is not correctly sized, missing one (i.e. the last) block.

This could cause an issue when the wallet had a single address used and that address was used on a transaction on the wallet's tip, causing the wallet to miss that address being used.

Required by #2318.

This was uncovered while debugging this test

matthawkins90 commented 9 months ago

It seems like this is such a rare edge case. Am I correct in understanding that in order for this bug to occur:

And if any other blocks were mined, it would be fine? I'm just trying to clarify for DJ how rare this situation might've been. I'm also curious if, when more blocks are mined, if a rescan would've fixed this for users.

matheusd commented 9 months ago

Your example should trigger the issue, but a simpler one is just:

A rescan would indeed fix the issue and having more blocks would also prevent the issue from being hit.

This is an issue that would likely only affect automated tests that have precise control over the entire situation (which is indeed how I found it).