Skip to content

Fix/804 #817

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

Merged
merged 3 commits into from
Oct 22, 2021
Merged

Fix/804 #817

merged 3 commits into from
Oct 22, 2021

Conversation

zone117x
Copy link
Member

@zone117x zone117x commented Oct 21, 2021

Closes #804

This PR fixes a couple issues with microblock re-org handling.

It also likely fixes #624 -- confirmed transactions showing up in mempool as pending -- still need to finish testing this one.

The serious bug here resulted in the API not showing certain transactions in rare scenarios involving both a micro-reorg and anchor-block-reorg. This also resulted in other queries like account balances to be incorrect.

Only around ~30 transactions in total appear to have been affected (and incorrectly appearing as orphaned).

This was caused by a bug similar to the one addressed in #686. Immediately non-canonical anchor blocks (referring to /new_block payloads received by the API for blocks that already have a canonical block at the same height) would end up marking transactions as micro-orphaned, and in very rare re-org scenarios, these transactions were not restored after a re-org, i.e. they incorrectly remained orphaned.

The fix for this involved A) prevent immediately-non-canonical new blocks from marking microblock data as micro-orphaned because they could be potentially shared with other blocks at the same height, and B) reverse the order of re-org handling so that txs in a given block are marked orphaned before the canonical txs are restored, fixing an issue where anchor block forks clobber over each other's microblock pointers.

So far I've tested the fix by running a query to return txs that are orphaned, and comparing the results from the old db and a db populated using these changes. This query does the job:

select DISTINCT ON (tx_id) tx_id, canonical, microblock_canonical, block_height from (
  select * from txs a
  where not exists (select 1 from txs b where a.tx_id=b.tx_id and canonical=true and microblock_canonical=true)
) orphaned
where block_height <= 34425
order by tx_id, block_height desc

Then run through the txs that are missing from the old db, find ones that include a STX transfer, then use the https://stacks-node-api.mainnet.stacks.co/v2/accounts/<address> core-node API to verify balances are correct.


A different, less serious bug resulted in an incorrect/partial list of microblock hashes in the /block response object for the microblocks_accepted: [] and microblocks_streamed: [] arrays. It looks like this could cause display issues in the Explorer's "recent block" list during rare situations involving both a micro-reorg and anchor-block-reorg.

This was fixed by removing an UPDATE query which was incorrectly identifying rows in the microblocks table by the cached index_block_hash column:

UPDATE microblocks
SET canonical = $2
WHERE index_block_hash = $1 AND canonical != $2
RETURNING microblock_hash

@vercel
Copy link

vercel bot commented Oct 21, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/blockstack/stacks-blockchain-api/FVmpJsuRubSoUkYP732fipsSxNDi
✅ Preview: https://stacks-blockchain-api-git-fix-804-blockstack.vercel.app

@codecov
Copy link

codecov bot commented Oct 21, 2021

Codecov Report

Merging #817 (d21ff14) into develop (4ade5ee) will increase coverage by 0.01%.
The diff coverage is 80.43%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #817      +/-   ##
===========================================
+ Coverage    65.39%   65.40%   +0.01%     
===========================================
  Files           90       90              
  Lines         9122     9142      +20     
  Branches      1459     1461       +2     
===========================================
+ Hits          5965     5979      +14     
- Misses        3152     3158       +6     
  Partials         5        5              
Impacted Files Coverage Δ
src/datastore/postgres-store.ts 83.26% <80.43%> (-0.16%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4ade5ee...d21ff14. Read the comment docs.

@zone117x zone117x linked an issue Oct 21, 2021 that may be closed by this pull request
@zone117x zone117x requested a review from rafaelcr October 21, 2021 22:11
@zone117x zone117x marked this pull request as ready for review October 21, 2021 22:32
@kyranjamie
Copy link
Contributor

Is this an issue we think might be often be a problem. The wallet could help here, right? Displaying a warning if API returns a different balance from the node?

@zone117x
Copy link
Member Author

zone117x commented Oct 22, 2021

Is this an issue we think might be often be a problem. The wallet could help here, right? Displaying a warning if API returns a different balance from the node?

Historically that would result in more false positive alerts due to sync issues between the node pool and the API. The client would need to fetch the balance for a given (current) block height using ?tip=<block> (e.g. https://stacks-node-api.mainnet.stacks.co/v2/accounts/SPYQW63PYXA8DFQS34GRMT5DPNRA6WR0TN19RY9F?proof=0&tip=ccf77c96f5ba4093b8ddcc92f372752d5fc20926723a2ce0a396c9fe634663cd) -- but that code is bugged and requires an index_block_hash rather than a normal block hash/height, and the index_block_hash is not readily available to clients.

@rafaelcr is working on a script we can run internally that can perform this kind of testing/validation #819

Copy link
Collaborator

@rafaelcr rafaelcr left a comment

Choose a reason for hiding this comment

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

LGTM

@zone117x zone117x linked an issue Oct 22, 2021 that may be closed by this pull request
@zone117x zone117x merged commit bae619d into develop Oct 22, 2021
@zone117x zone117x deleted the fix/804 branch October 22, 2021 19:38
@blockstack-devops
Copy link

🎉 This PR is included in version 0.70.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Contract and Balance Discrepancies in Block #32861 Incorrect STX balance
4 participants