Transaction context (tx.origin vs msg.sender) in Blockchain / Solidity - Performance Comparison
When working with blockchain transactions, it is important to understand how the code checks who started or called a transaction.
We want to see how the number of steps changes when the transaction involves multiple calls.
Analyze the time complexity of the following Solidity code snippet.
contract Example {
function checkOrigin() public view returns (address) {
return tx.origin;
}
function checkSender() public view returns (address) {
return msg.sender;
}
}
This code returns the address of the original transaction sender and the immediate caller.
In this snippet, there are no loops or recursion.
- Primary operation: Accessing transaction context variables.
- How many times: Once per function call.
Since the functions only read a value, the number of steps stays the same no matter how many calls happen.
| Call Depth (n) | Approx. Operations |
|---|---|
| 1 | 1 |
| 10 | 1 |
| 100 | 1 |
Pattern observation: The work remains constant regardless of the number of calls or call depth.
Time Complexity: O(1)
This means each function call takes the same small amount of time, no matter what.
[X] Wrong: "Using tx.origin or msg.sender will slow down the contract as calls get deeper."
[OK] Correct: Accessing these variables is a simple read operation and does not depend on call depth or input size.
Understanding how transaction context variables work helps you reason about security and performance in smart contracts, a key skill for blockchain developers.
"What if the function called another contract before returning tx.origin? How would that affect the time complexity?"