lightninglabs / taproot-assets

A layer 1 daemon, for the Taproot Assets Protocol specification, written in Go (golang)
MIT License
439 stars 106 forks source link

tapdb: remove duplicate assets before adding unique index #980

Open guggero opened 4 days ago

guggero commented 4 days ago

This commit first removes duplicate assets that were mistakenly created due to self transfers in previous versions. After removing the duplicates, the new unique index should be applied without issues.

guggero commented 4 days ago

There are four tables with a foreign key reference to assets:

* `asset_witnesses`

* `asset_proofs`

* `passive_assets`

* `addr_events`

This PR addresses asset_witnesses but ignores the other three tables. Are we sure we can safely do so?

Yeah, good point. In the example database I was given there were no entries in the asset_proofs, passive_assets and addr_events table. So I think due to how the other insert/upsert queries are structured, only the asset with the lowest ID would get an entry in those tables. So by keeping the asset with the lowest ID when removing duplicates should work. But I'll look into some more safe queries with the help of LLMs.

guggero commented 3 days ago

@gijswijs I added some more involved logic to properly account for what to do with the asset_witnesses and asset_proofs table. Those should now be dealt with correctly, I also added test code for that.

That leaves the passive_assets and addr_events tables. I thought hard about the code that produced the duplicates and am 95% sure that we won't ever have entries in those two tables for the duplicate entries. And if for some reason there would be, then for those users the migration would fail and we could manually intervene.