Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

waddrmgr: Put client request first in recovery. #979

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JoeGruffins
Copy link
Contributor

@JoeGruffins JoeGruffins commented Feb 25, 2025

closes #978

I'm unsure if this is the best solution but it gets me past the block in my issue. It looks to me like this should always hit when restoring with a birthday, but it does not. Does btcwallet not download all headers anyway?

@JoeGruffins JoeGruffins marked this pull request as draft February 25, 2025 08:20
@JoeGruffins
Copy link
Contributor Author

Oh wait, got stuck a second time at another block 2025-02-25 17:19:30.554 [ERR] BTCW: Unable to synchronize wallet to chain, trying again in 5s: unable to perform wallet recovery: failed to store sync information 00000000000000000000b9e6cc0b5be06c02f56506fee5c7897edf6cafc4e7cc: failed to fetch block hash for height 870852: block not found

@JoeGruffins
Copy link
Contributor Author

If there was an error connecting to a client inside an update in recovery, the tx is rolled back but the in memory sync point is not. this fixes that https://github.com/btcsuite/btcwallet/compare/40d5f25cd330609e350818655632b18fb1a360c8..6935a129d7e68f106895c7dd0c761d8e09b1fdd9

@JoeGruffins JoeGruffins marked this pull request as ready for review February 26, 2025 07:08
@JoeGruffins JoeGruffins changed the title waddrmgr: Don't get block hash before birthday. waddrmgr: Roll back in memory sync point on error. Feb 26, 2025
@JoeGruffins
Copy link
Contributor Author

The birthday thing was apparently a coincidence.

Copy link

@dev-warrior777 dev-warrior777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one

wallet/wallet.go Outdated
@@ -802,6 +803,7 @@ func (w *Wallet) recovery(chainClient chain.Interface,
)
})
if err != nil {
w.Manager.SetSyncedToMemory(&rollbackStamp)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so what was the initial error in the first place that got this out of sync? Sounds like all the failed to fetch block hash for height 870852: block not found errors are follow-up errors.
Would be nice to know that. But this fix definitely makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the causing error in the issue for one instance.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, fetching a block failed in a Neutrino setup.

Great catch, I'm quite certain this is indeed the cause for the errors you saw.

I wonder how we should deal with all the other cases where SetSyncedTo() is called within a database transaction that might be rolled back and cause the DB and in-memory state to de-synced...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the out-of-synced state will make things worse. It looks like recoverScopedAddresses should be performed first before calling SetSyncedTo. Also it needs some refactor to move the non-db operations out so we don't need to wrap them in one giant tx walletdb.Update.

Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this approach works - it's hacky and creates inconsistent states.

wallet/wallet.go Outdated
@@ -802,6 +803,7 @@ func (w *Wallet) recovery(chainClient chain.Interface,
)
})
if err != nil {
w.Manager.SetSyncedToMemory(&rollbackStamp)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the out-of-synced state will make things worse. It looks like recoverScopedAddresses should be performed first before calling SetSyncedTo. Also it needs some refactor to move the non-db operations out so we don't need to wrap them in one giant tx walletdb.Update.

@JoeGruffins
Copy link
Contributor Author

JoeGruffins commented Feb 27, 2025

I think the out-of-synced state will make things worse.

I'll look again but I think all of the db functions never happen, and this was the only thing not rolled back. I'll do whatever though because this bug is affecting a few of our projects.

@JoeGruffins
Copy link
Contributor Author

JoeGruffins commented Feb 27, 2025

@yyforyongyu I've looked through, and there is alot, but I can't find any values that are not rolled back. Could you point them out?

Also, with no fix, if the bug is hit, then the db would be borked anyway if what you say is correct. Even if a huge refactor is needed, a fix for the moment would be good to have. I can try to write a test I guess?

@JoeGruffins JoeGruffins changed the title waddrmgr: Roll back in memory sync point on error. waddrmgr: Put client request first in recovery. Feb 27, 2025
@JoeGruffins
Copy link
Contributor Author

JoeGruffins commented Feb 27, 2025

@yyforyongyu Is this current version ok for a patch? It only swaps the order as you suggested. Because the client request for headers happens in the first part, and this is where a connection error can happen, it won't continue to update the internal sync point. This also fixes the immediate problem for us. https://github.com/btcsuite/btcwallet/compare/469713a564568f734e51e8bd87e7a2ac2885b754..06463724d4c69dc3e09f2191de442f5635e9c606

@JoeGruffins
Copy link
Contributor Author

Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update! LGTM 🙏

@JoeGruffins JoeGruffins force-pushed the dontfetchbeforebday branch from 7509f6b to fe45f81 Compare March 6, 2025 06:32
@JoeGruffins
Copy link
Contributor Author

@guggero guggero self-requested a review March 6, 2025 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sync: failed to fetch block hash for height ####: block not found
4 participants