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

runtime: fix owner update for burned NFT instances #589

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

pro-wh
Copy link
Collaborator

@pro-wh pro-wh commented Dec 18, 2023

previously I tried using COALESCE(new owner, old owner) so that we can pass nil to leave the owner as it is when we don't see any transfers, but that didn't leave a way to NULL out the owner when an NFT instance gets burned. the old code turned out to try to set a non-NULL empty string owner, which the Oasis address type doesn't allow.

this PR splits up the runtime analyzer's upsert into an upsert for any NFT detected, plus a new update for when we see transfers, which sets a new owner that may be NULL and bumps the num_transfers.

@pro-wh pro-wh added bug Something isn't working analysis-layer Analysis layer-related issues. labels Dec 18, 2023
@@ -121,7 +121,11 @@ type NFTKey struct {
}

type PossibleNFT struct {
NewOwner *apiTypes.Address
// NewOwner has the latest owner if .NumTransfers is more than zero. If
// the last transfer burned this NFT instance, then it's the empty string.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, thank you for the docstrings! Can we keep it as a pointer though? Right now, to realize that the NewOwner value should be ignored, one needs to check NumTransfers == 0, which is far from obvious.

Optional: I also suggest the special value be something other than the empty string, e.g. "__BURN__" or "0". It's currently set very implicitly, via uninitialized values, and it's easy to miss it when reading the code. Also when checking the variable value in an if, "__BURN__" is a lot more informative than "".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not feeling that special string idea. I'll add a Burned bool field.

Comment on lines 716 to +730
RuntimeEVMNFTUpsert = `
INSERT INTO chain.evm_nfts AS old
(runtime, token_address, nft_id, owner, num_transfers, last_want_download_round)
(runtime, token_address, nft_id, num_transfers, last_want_download_round)
VALUES
($1, $2, $3, $4, $5, $6)
ON CONFLICT (runtime, token_address, nft_id) DO UPDATE
SET
owner = COALESCE(excluded.owner, old.owner),
num_transfers = old.num_transfers + excluded.num_transfers`
($1, $2, $3, 0, $4)
ON CONFLICT (runtime, token_address, nft_id) DO NOTHING`

RuntimeEVMNFTUpdateTransfer = `
UPDATE chain.evm_nfts SET
owner = $4,
num_transfers = num_transfers + $5
WHERE
runtime = $1 AND
token_address = $2 AND
nft_id = $3`
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to keep the single-statement setup here. It will keep the upsert a lot more similar to other upserts, both on the Go and SQL side, and thus easier to read and reason about.

We'll just need a special value for owner (or rather $4) that SQL can recognize. So in the old query, only a single line would need to change, something like

ON CONFLICT (runtime, token_address, nft_id) DO UPDATE
    SET
      owner = CASE WHEN excluded.owner = '__BURN__' THEN NULL ELSE COALESCE(excluded.owner, old.owner) END,

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

"please do this completely differently. approved"

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry if it looked weird. As explained on slack, approved to unblock you after I'm gone in my timezone.
Also, "completely differently" is ... one possible interpretation in that the whole PR is not very large, so the percentage of affected lines is large. But I agree with all the debugging work that led to the small PR, and with the semantics of the fix, which is to sometimes force the NULL into the DB. Those are the more important parts IMO, and my comment is about a technicality and code readability.
Really signing off now.

@pro-wh pro-wh closed this Dec 18, 2023
@pro-wh pro-wh deleted the pro-wh/bugfix/nftburn branch December 18, 2023 23:50
@pro-wh pro-wh restored the pro-wh/bugfix/nftburn branch December 18, 2023 23:54
@pro-wh
Copy link
Collaborator Author

pro-wh commented Dec 18, 2023

nah closing this was too rash. I'm gonna merge this as-is and do a different version tomorrow. which is rasher

@pro-wh pro-wh reopened this Dec 18, 2023
@pro-wh pro-wh enabled auto-merge December 18, 2023 23:55
@pro-wh pro-wh merged commit 7bc521f into main Dec 18, 2023
12 checks passed
@pro-wh pro-wh deleted the pro-wh/bugfix/nftburn branch December 18, 2023 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analysis-layer Analysis layer-related issues. bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants