0
0
Blockchain / Solidityprogramming~10 mins

Common vulnerability patterns 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 prevent reentrancy by using a mutex lock.

Blockchain / Solidity
bool locked = false;

function withdraw(uint amount) public {
    require(![1], "Reentrant call detected");
    locked = true;
    // send funds
    locked = false;
}
Drag options to blanks, or click blank then click option'
Alocked
Btrue
Camount
Dmsg.sender
Attempts:
3 left
💡 Hint
Common Mistakes
Using a constant true value in the require statement.
Checking the amount or sender instead of the lock variable.
2fill in blank
medium

Complete the code to check for integer overflow before addition.

Blockchain / Solidity
function add(uint a, uint b) public pure returns (uint) {
    uint c = a + b;
    require(c [1] a, "Overflow detected");
    return c;
}
Drag options to blanks, or click blank then click option'
A<
B!=
C==
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < which would allow overflow.
Using equality which does not detect overflow.
3fill in blank
hard

Fix the error in the access control modifier to restrict function calls to the owner only.

Blockchain / Solidity
address owner;

modifier onlyOwner() {
    require(msg.sender [1] owner, "Not owner");
    _;
}
Drag options to blanks, or click blank then click option'
A!=
B==
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality operator which blocks the owner.
Using greater or less than which is invalid for addresses.
4fill in blank
hard

Fill both blanks to safely update a mapping after checking conditions.

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

function updateBalance(address user, uint amount) public {
    require(balances[user] [1] 0, "No balance");
    balances[user] [2] amount;
}
Drag options to blanks, or click blank then click option'
A>
B=
C<
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than in the require which allows zero balances.
Using += which adds instead of replacing the balance.
5fill in blank
hard

Fill all three blanks to implement a safe withdrawal function avoiding reentrancy and updating state correctly.

Blockchain / Solidity
bool locked = false;

function safeWithdraw(uint amount) public {
    require(![1], "Reentrant call");
    locked = true;
    require(balances[msg.sender] [2] amount, "Insufficient balance");
    balances[msg.sender] [3] balances[msg.sender] - amount;
    // send funds
    locked = false;
}
Drag options to blanks, or click blank then click option'
Alocked
B>=
C=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= which may cause underflow in older Solidity versions.
Not checking balance before withdrawal.
Not using the lock variable correctly.