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

txdb lock balance accounting fixes. #780

Closed

Conversation

pinheadmz
Copy link
Member

The assertions were only there in the first place because of weird covenant accounting: for some covenants like REVEAL, we dig up the corresponding BID from the database and we MUST HAVE THAT DATA or we can't compute the change to the wallet's locked balance.

The new approach is: we process all inputs and all outputs of every TX, meaning that the BID in the input of the transaction is applied to our balance mere moments before we process the REVEAL in the output of the same transaction. This simplifies balance locking really well (we eliminate DB lookups and the functions are no longer async/await).

The assertion errors we've been getting almost daily from users occurs when the BID is not in the database yet because we don't recognize our own address because it's a wallet recovery and we haven't generated the address yet.

What this PR does is "not care": if we see a REVEAL before we see the BID, then we have a negative locked balance. Yep that really sucks but it can be fixed with a rescan or two... and no one's node has to crash.

There is one other drawback to this, which is why I'm pausing here and opening as draft:

TODO:

  • class Balance in txdb needs to allow negative values. Remove assert >= 0 and switch all writeU64 to writeI64 which interestingly, because it's javascript, I think means we don't need a migration ... ?!

@coveralls
Copy link

coveralls commented Nov 16, 2022

Coverage Status

Coverage decreased (-0.03%) to 67.755% when pulling e74ac10 on pinheadmz:txdb-unlock-balances into e07ba54 on handshake-org:master.

@nodech nodech force-pushed the txdb-unlock-balances branch from b26bbd0 to 13e0a19 Compare November 27, 2022 12:45
lib/wallet/layout.js Outdated Show resolved Hide resolved
@nodech nodech force-pushed the txdb-unlock-balances branch 2 times, most recently from c1c0741 to 137534e Compare November 29, 2022 12:20
@nodech nodech changed the title txdb lockBalances assertion errors MUST DIE ☠️ txdb lock balance accounting fixes. Nov 29, 2022
@pinheadmz pinheadmz added this to the hsd 5.0.0 milestone Nov 29, 2022
lib/wallet/account.js Outdated Show resolved Hide resolved
@nodech nodech force-pushed the txdb-unlock-balances branch 3 times, most recently from d19b487 to 77a57f8 Compare December 1, 2022 12:03
const GRIND_NAME_LEN = 10;
const hardFee = 1e4;

// TODO: Add insert w/ block only tests(no unconfirmed step).
// TODO: Add claim test.
// TODO: (Re)Move and merge wallet-test lock balance checks.
// TODO: Add double spend and double spend/erase of chained txs.
// - Test when we have spent tracking(layout.s) but not spendCoin(layoud.d)
Copy link
Member Author

Choose a reason for hiding this comment

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

typo: "layout"

@pinheadmz pinheadmz force-pushed the txdb-unlock-balances branch from 86c5627 to e74ac10 Compare December 5, 2022 22:17
@pinheadmz
Copy link
Member Author

@nodech rebased on master and discovered another failed test from #781

I don't know if this issue is recoverable. I'm using these extra log statements to monitor the un/lock balance activity:

https://gist.github.com/pinheadmz/9af90d54e7ac934a7aa331ccd6501f28

Comment on lines +27 to +30
/**
* @typedef {import('./walletdb')} WalletDB
*/

Copy link
Member Author

Choose a reason for hiding this comment

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

CI didn't like this.


ERROR: Unable to parse a tag's type expression for source file /home/runner/work/hsd/hsd/lib/wallet/txdb.js in line 27 with tag title "typedef" and text "{import('./walletdb')} WalletDB": Invalid type expression "import('./walletdb')": Expected "!", "=", "?", "[]", "|", or end of input but "(" found.
Error: Process completed with exit code 1.

Comment on lines +1354 to +1357
if (!credit) {
credit = Credit.fromTX(tx, i, height);
await this.removeCredit(b, credit, path);
continue;
Copy link
Member Author

Choose a reason for hiding this comment

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

The continue here makes sense, but we already know there is no Credit, so I don't understand the purpose of removeCredit() ? All tests pass without it

@pinheadmz pinheadmz closed this Dec 13, 2022
@nodech nodech mentioned this pull request Dec 16, 2022
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.

3 participants