Enforce Checks-Effects-Interactions pattern in NFTSwap purchase and revoke#896
Open
daatsuka wants to merge 1 commit intoAmazingAng:mainfrom
Open
Enforce Checks-Effects-Interactions pattern in NFTSwap purchase and revoke#896daatsuka wants to merge 1 commit intoAmazingAng:mainfrom
daatsuka wants to merge 1 commit intoAmazingAng:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this (这是什么类型的PR)
Which issue(s) this PR fixes(Optional) (这个PR 修复了什么问题 (可选择))
What this PR does / why we need it (这个PR 做了什么/ 我们为什么需要这个PR)
The
purchase()andrevoke()functions in38_NFTSwap/NFTSwap.sol(and its English counterpart atLanguages/en/38_NFTSwap_en/NFTSwap.sol) were deleting thenftListmapping entry after performing external calls —safeTransferFromandtransfer. This ordering violates the Checks-Effects-Interactions (CEI) pattern and opens the door to reentrancy: a maliciousonERC721Receivedcallback could re-enterpurchase()before the order is cleared, draining the contract. The recompiledout/artifacts reflect the updated bytecode produced by the Solidity 0.8.34 toolchain that was bumped in the prior commit onmain.In
purchase(), I now cache_order.ownerand_order.priceinto local memory variables, thendelete nftList[_nftAddr][_tokenId]immediately after therequirechecks and before any external interaction. The subsequentsafeTransferFrom,transfer, and refund logic all reference the cached locals instead of reading from storage that no longer exists. The same state-before-interaction reordering is applied torevoke(), where thedeletenow precedes thesafeTransferFromcall that returns the NFT to the seller. Both the Chinese and English source files carry identical logic changes so the tutorial stays consistent across languages.The design intent is straightforward: state mutations must settle before the contract hands control to an external address. This is the canonical CEI discipline recommended by the Solidity docs and auditing firms alike, and it eliminates the reentrancy vector without introducing a mutex lock or any additional gas overhead. I verified the change locally by compiling with
forge build— all contracts compile cleanly with zero warnings, and the generated artifacts inout/match the expected output for the updated source.Closes #895