0
0
Blockchain / Solidityprogramming~10 mins

Reentrancy attacks in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a payable fallback function in Solidity.

Blockchain / Solidity
fallback() external [1] {}
Drag options to blanks, or click blank then click option'
Ainternal
Bview
Cpure
Dpayable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'view' or 'pure' which do not allow Ether reception.
Forgetting to mark fallback as payable.
2fill in blank
medium

Complete the code to update the user's balance after withdrawal.

Blockchain / Solidity
balances[msg.sender] [1] 0;
Drag options to blanks, or click blank then click option'
A=
B==
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which is a comparison, not assignment.
Using '+=' or '-=' which modify the value instead of setting it.
3fill in blank
hard

Fix the error in the withdrawal function to prevent reentrancy by updating state before sending Ether.

Blockchain / Solidity
function withdraw() public {
    uint amount = balances[msg.sender];
    require(amount > 0, "No balance");
    [1];
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Failed to send Ether");
}
Drag options to blanks, or click blank then click option'
Abalances[msg.sender] -= amount
Bbalances[msg.sender] += amount
Cbalances[msg.sender] = 0
Damount = 0
Attempts:
3 left
💡 Hint
Common Mistakes
Updating balance after sending Ether.
Using '+=' or '-=' which do not reset the balance properly.
4fill in blank
hard

Fill both blanks to create a mapping and a function that checks the balance of an address.

Blockchain / Solidity
mapping(address => uint) public [1];

function getBalance(address user) public view returns (uint) {
    return [2][user];
}
Drag options to blanks, or click blank then click option'
Abalances
Cusers
Daccounts
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for mapping and return statement.
Using unrelated names like 'users' or 'accounts'.
5fill in blank
hard

Fill all three blanks to implement a safe withdrawal pattern using a mutex lock.

Blockchain / Solidity
bool private [1];

function withdraw() public {
    require(![2], "Reentrant call");
    [3] = true;
    uint amount = balances[msg.sender];
    require(amount > 0, "No balance");
    balances[msg.sender] = 0;
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Failed to send Ether");
    [3] = false;
}
Drag options to blanks, or click blank then click option'
Alocked
Dactive
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the lock.
Not resetting the lock to false after withdrawal.