-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Reentrancy Locks test contract add update StorageContract.sol (#851)
Co-authored-by: Kyon <[email protected]>
- Loading branch information
Showing
4 changed files
with
101 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
50 changes: 50 additions & 0 deletions
50
src/main/resources/contract/solidity/0.8.26/DoubleBufferContract.sol
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
contract DoubleBufferContract { | ||
uint[] bufferA; | ||
uint[] bufferB; | ||
|
||
modifier nonreentrant(bytes32 key) { | ||
assembly { | ||
if tload(key) {revert(0, 0)} | ||
tstore(key, 1) | ||
} | ||
_; | ||
assembly { | ||
tstore(key, 0) | ||
} | ||
} | ||
|
||
bytes32 constant A_LOCK = keccak256("a"); | ||
bytes32 constant B_LOCK = keccak256("b"); | ||
|
||
function pushA() nonreentrant(A_LOCK) public payable { | ||
bufferA.push(msg.value); | ||
} | ||
|
||
function popA() nonreentrant(A_LOCK) public { | ||
require(bufferA.length > 0); | ||
|
||
(bool success,) = msg.sender.call{value: bufferA[bufferA.length - 1]}(""); | ||
require(success); | ||
bufferA.pop(); | ||
} | ||
|
||
function pushB() nonreentrant(B_LOCK) public payable { | ||
bufferB.push(msg.value); | ||
} | ||
|
||
function popB() nonreentrant(B_LOCK) public { | ||
require(bufferB.length > 0); | ||
|
||
(bool success,) = msg.sender.call{value: bufferB[bufferB.length - 1]}(""); | ||
require(success); | ||
bufferB.pop(); | ||
} | ||
|
||
function getBufferA() public view returns (uint256[] memory) { | ||
return bufferA; | ||
} | ||
|
||
function getBufferB() public view returns (uint256[] memory) { | ||
return bufferB; | ||
} | ||
} |
This file contains 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
27 changes: 27 additions & 0 deletions
27
src/main/resources/contract/solidity/0.8.26/TestDoubleBufferContract.sol
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
import "./DoubleBufferContract.sol"; | ||
|
||
contract TestDoubleBufferContract { | ||
DoubleBufferContract public doubleBufferContract; | ||
|
||
constructor() { | ||
doubleBufferContract = new DoubleBufferContract(); | ||
} | ||
|
||
function testReentrancyA() public { | ||
doubleBufferContract.pushA{value: 1 ether}(); | ||
// 尝试再次调用pushA函数,应该被阻止 | ||
doubleBufferContract.pushA{value: 1 ether}(); | ||
} | ||
|
||
function testReentrancyB() public { | ||
doubleBufferContract.pushB{value: 1 ether}(); | ||
// 尝试再次调用pushB函数,应该被阻止 | ||
doubleBufferContract.pushB{value: 1 ether}(); | ||
} | ||
|
||
receive() external payable { | ||
doubleBufferContract.pushA{value: 1 ether}(); | ||
} | ||
} |