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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions analyzer/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,13 +715,19 @@ var (

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`
Comment on lines 716 to +730
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.


RuntimeEVMNFTAnalysisStale = `
SELECT
Expand Down
8 changes: 6 additions & 2 deletions analyzer/runtime/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

NewOwner apiTypes.Address
// NumTransfers is how many times we saw it transferred. If it's more than
// zero, the new owner is in .NewOwner.
NumTransfers int
}

Expand Down Expand Up @@ -270,7 +274,7 @@ func registerNFTExist(nftChanges map[NFTKey]*PossibleNFT, contractAddr apiTypes.
func registerNFTTransfer(nftChanges map[NFTKey]*PossibleNFT, contractAddr apiTypes.Address, tokenID *big.Int, newOwner apiTypes.Address) {
possibleNFT := findPossibleNFT(nftChanges, contractAddr, tokenID)
possibleNFT.NumTransfers++
possibleNFT.NewOwner = &newOwner
possibleNFT.NewOwner = newOwner
}

func findTokenChange(tokenChanges map[TokenChangeKey]*big.Int, contractAddr apiTypes.Address, accountAddr apiTypes.Address) *big.Int {
Expand Down
17 changes: 15 additions & 2 deletions analyzer/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/oasisprotocol/nexus/analyzer/queries"
evm "github.com/oasisprotocol/nexus/analyzer/runtime/evm"
uncategorized "github.com/oasisprotocol/nexus/analyzer/uncategorized"
apiTypes "github.com/oasisprotocol/nexus/api/v1/types"
"github.com/oasisprotocol/nexus/common"
"github.com/oasisprotocol/nexus/config"
"github.com/oasisprotocol/nexus/log"
Expand Down Expand Up @@ -399,9 +400,21 @@ func (m *processor) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {
m.runtime,
key.TokenAddress,
key.TokenID,
possibleNFT.NewOwner,
possibleNFT.NumTransfers,
data.Header.Round,
)
if possibleNFT.NumTransfers > 0 {
var newOwner *apiTypes.Address
if possibleNFT.NewOwner != "" {
newOwner = &possibleNFT.NewOwner
}
batch.Queue(
queries.RuntimeEVMNFTUpdateTransfer,
m.runtime,
key.TokenAddress,
key.TokenID,
newOwner,
possibleNFT.NumTransfers,
)
}
}
}
Loading