Event testing in Blockchain / Solidity - Time & Space Complexity
When testing events in blockchain code, we want to know how the time needed grows as we check more events.
We ask: How does the work increase when the number of events grows?
Analyze the time complexity of the following code snippet.
function testEvents(events) {
for (let i = 0; i < events.length; i++) {
if (events[i].name === 'Transfer') {
console.log('Transfer event found');
}
}
}
This code checks each event in a list to find all events named 'Transfer'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the events array once.
- How many times: Once for each event in the list.
As the number of events grows, the code checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of events.
Time Complexity: O(n)
This means the time to test events grows in a straight line with the number of events.
[X] Wrong: "Checking events is always fast and constant time."
[OK] Correct: Because the code looks at each event one by one, more events mean more work.
Understanding how event testing time grows helps you explain your code's efficiency clearly and confidently.
"What if we stopped checking after finding the first 'Transfer' event? How would the time complexity change?"
Practice
What is the main purpose of event testing in blockchain smart contracts?
Solution
Step 1: Understand event testing purpose
Event testing focuses on confirming that smart contracts emit the right signals (events) when certain actions happen.Step 2: Compare options with event testing goal
Only To verify that the contract emits the correct events during execution matches this purpose, as it talks about verifying emitted events.Final Answer:
To verify that the contract emits the correct events during execution -> Option AQuick Check:
Event testing = Verify emitted events [OK]
- Confusing event testing with gas optimization
- Thinking event testing checks UI elements
- Assuming event testing measures storage use
Which of the following is the correct syntax to listen for an event named Transfer in a Solidity test using JavaScript?
contractInstance.Transfer({fromBlock: 0, toBlock: 'latest'}).???Solution
Step 1: Recall event listening syntax in Solidity tests
In older Solidity test frameworks,watchis used to listen for events on contract instances.Step 2: Match options with correct method
Onlywatchfits the syntax shown;on,emit, andlistenare incorrect here.Final Answer:
watch -> Option BQuick Check:
Event listening method = watch [OK]
- Using 'emit' which triggers events, not listens
- Confusing 'on' with event listening in other frameworks
- Using 'listen' which is not a Solidity test method
Given this Solidity event and test snippet, what will be the output of console.log(events.length) after calling transfer() once?
event Transfer(address indexed from, address indexed to, uint amount);
// In test:
const events = await contractInstance.getPastEvents('Transfer', {fromBlock: 0, toBlock: 'latest'});
await contractInstance.transfer(receiver, 100);
console.log(events.length);Solution
Step 1: Understand event fetching timing
ThegetPastEventscall happens beforetransfer(), so it fetches events before the transfer.Step 2: Analyze event count after transfer
Since events are fetched before the transfer,events.lengthwill be 0 because no Transfer event happened yet.Final Answer:
0 -> Option CQuick Check:
Events fetched before transfer = 0 [OK]
- Assuming events array updates automatically
- Ignoring async order of calls
- Expecting event count before event occurs
Identify the error in this test code snippet that listens for an event Approval:
contractInstance.Approval().watch((error, event) => {
if (!error) {
console.log('Approval event:', event);
}
});Solution
Step 1: Check event listening syntax
The event constructor requires a filter object parameter (e.g., {fromBlock: 0}) to specify the event scope.Step 2: Identify correct usage
The correct syntax iscontractInstance.Approval({fromBlock: 0}).watch(...); the code uses emptyApproval().Final Answer:
Missing event filter parameters in the call -> Option DQuick Check:
Event filter params required [OK]
- Omitting filter parameters in event call
- Confusing callback parameters
- Mixing event listening methods
You want to test that a Deposit event is emitted only when the deposit amount is greater than zero. Which test approach correctly verifies this?
1. Call deposit(0) and check no event emitted 2. Call deposit(100) and check event emitted with amount 100 3. Call deposit(-50) and check event emitted 4. Call deposit(100) and check event emitted with amount 0
Solution
Step 1: Understand event emission condition
The event should emit only if deposit amount > 0, so deposit(0) should emit no event, deposit(100) should emit event with amount 100.Step 2: Evaluate test steps
Step 3 calls deposit(-50), which is invalid (negative deposit), so event emission is not expected. Step 4 expects event with amount 0, which contradicts the condition.Final Answer:
Only steps 1 and 2 are correct test cases -> Option AQuick Check:
Test event only for positive deposits [OK]
- Testing negative amounts which are invalid
- Expecting event with zero amount
- Assuming all deposits emit events
Ready to prove it?
Pass the test and this pattern moves unknown
