0
0
Blockchain / Solidityprogramming~10 mins

Reentrancy guard pattern 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 boolean variable named 'locked' initialized to false.

Blockchain / Solidity
bool [1] = false;
Drag options to blanks, or click blank then click option'
AisLocked
Block
Cstatus
Dlocked
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name like 'lock' or 'status' which is not standard.
2fill in blank
medium

Complete the modifier to check if the contract is not locked before running the function.

Blockchain / Solidity
modifier noReentrant() {
    require(![1], "Reentrant call");
    _;
}
Drag options to blanks, or click blank then click option'
Alocked
BisLocked
Clock
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the wrong variable or missing the negation operator.
3fill in blank
hard

Fix the error in the modifier to set the locked state correctly before and after the function runs.

Blockchain / Solidity
modifier noReentrant() {
    require(!locked, "Reentrant call");
    locked = [1];
    _;
    locked = false;
}
Drag options to blanks, or click blank then click option'
Atrue
Bstatus
Clocked
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting locked to false before the function runs, which disables the guard.
4fill in blank
hard

Fill both blanks to complete the safe withdraw function using the noReentrant modifier and resetting the locked state.

Blockchain / Solidity
function withdraw(uint amount) public noReentrant {
    require(balances[msg.sender] >= amount, "Insufficient balance");
    balances[msg.sender] -= amount;
    (bool success, ) = msg.sender.call{value: amount}([1]);
    require(success, "Transfer failed");
    locked = [2];
}
Drag options to blanks, or click blank then click option'
Agas(2300)
Bfalse
Ctrue
Dgas()
Attempts:
3 left
💡 Hint
Common Mistakes
Using gas(2300) which limits gas and can cause failures.
Not resetting locked to false.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps addresses to their balances only if balance is greater than zero.

Blockchain / Solidity
mapping(address => uint) public balances;

function positiveBalances(address[] memory users) public view returns (mapping(address => uint) memory) {
    return {user: balances[user] for user in users if balances[user] [1] 0};
}

modifier noReentrant() {
    require(!locked, "Reentrant call");
    locked = [2];
    _;
    locked = [3];
}
Drag options to blanks, or click blank then click option'
A>
Btrue
Cfalse
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or wrong boolean values in the modifier.