fixNFTokenRemint: prevent NFT re-mint: (#4406)

Without the protocol amendment introduced by this commit, an NFT ID can
be reminted in this manner:

1. Alice creates an account and mints an NFT.
2. Alice burns the NFT with an `NFTokenBurn` transaction.
3. Alice deletes her account with an `AccountDelete` transaction.
4. Alice re-creates her account.
5. Alice mints an NFT with an `NFTokenMint` transaction with params:
   `NFTokenTaxon` = 0, `Flags` = 9).

This will mint a NFT with the same `NFTokenID` as the one minted in step
1. The params that construct the NFT ID will cause a collision in
`NFTokenID` if their values are equal before and after the remint.

With the `fixNFTokenRemint` amendment, there is a new sequence number
construct which avoids this scenario:

- A new `AccountRoot` field, `FirstNFTSequence`, stays constant over
  time.
  - This field is set to the current account sequence when the account
    issues their first NFT.
  - Otherwise, it is not set.
- The sequence of a newly-minted NFT is computed by: `FirstNFTSequence +
  MintedNFTokens`.
  - `MintedNFTokens` is then incremented by 1 for each mint.

Furthermore, there is a new account deletion restriction:

- An account can only be deleted if `FirstNFTSequence + MintedNFTokens +
  256` is less than the current ledger sequence.
  - 256 was chosen because it already exists in the current account
    deletion constraint.

Without this restriction, an NFT may still be remintable. Example
scenario:

1. Alice's account sequence is at 1.
2. Bob is Alice's authorized minter.
3. Bob mints 500 NFTs for Alice. The NFTs will have sequences 1-501, as
   NFT sequence is computed by `FirstNFTokenSequence + MintedNFTokens`).
4. Alice deletes her account at ledger 257 (as required by the existing
   `AccountDelete` amendment).
5. Alice re-creates her account at ledger 258.
6. Alice mints an NFT. `FirstNFTokenSequence` initializes to her account
   sequence (258), and `MintedNFTokens` initializes as 0. This
   newly-minted NFT would have a sequence number of 258, which is a
   duplicate of what she issued through authorized minting before she
   deleted her account.

---------

Signed-off-by: Shawn Xie <shawnxie920@gmail.com>
This commit is contained in:
Shawn Xie
2023-03-20 17:47:46 -04:00
committed by GitHub
parent 9b2d563dec
commit 305c9a8d61
12 changed files with 695 additions and 43 deletions

View File

@@ -68,17 +68,26 @@ getNextID(
// Get the nftSeq from the account root of the issuer.
std::uint32_t const nftSeq = {
env.le(issuer)->at(~sfMintedNFTokens).value_or(0)};
return getID(issuer, nfTokenTaxon, nftSeq, flags, xferFee);
return token::getID(env, issuer, nfTokenTaxon, nftSeq, flags, xferFee);
}
uint256
getID(
jtx::Env const& env,
jtx::Account const& issuer,
std::uint32_t nfTokenTaxon,
std::uint32_t nftSeq,
std::uint16_t flags,
std::uint16_t xferFee)
{
if (env.current()->rules().enabled(fixNFTokenRemint))
{
// If fixNFTokenRemint is enabled, we must add issuer's
// FirstNFTokenSequence to offset the starting NFT sequence number.
nftSeq += env.le(issuer)
->at(~sfFirstNFTokenSequence)
.value_or(env.seq(issuer));
}
return ripple::NFTokenMint::createNFTokenID(
flags, xferFee, issuer, nft::toTaxon(nfTokenTaxon), nftSeq);
}