Library contracts in Blockchain / Solidity - Time & Space Complexity
When using library contracts in blockchain, it's important to see how the cost of running code changes as the input grows.
We want to know how the number of steps grows when the library functions are called many times.
Analyze the time complexity of the following code snippet.
library MathLib {
function sum(uint[] memory numbers) internal pure returns (uint) {
uint total = 0;
for (uint i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
}
}
contract Calculator {
function calculateSum(uint[] memory data) public pure returns (uint) {
return MathLib.sum(data);
}
}
This code uses a library contract to sum all numbers in an array passed to the Calculator contract.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside the library function that adds each number.
- How many times: It runs once for every element in the input array.
As the array gets bigger, the number of additions grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows evenly as the input size grows.
Time Complexity: O(n)
This means the time to run the sum grows in a straight line with the number of items.
[X] Wrong: "Using a library makes the code run faster regardless of input size."
[OK] Correct: The library just organizes code; the time depends on how many items you process, not on using a library.
Understanding how library contracts affect time helps you write efficient blockchain code and explain your choices clearly.
"What if the library function used nested loops to compare each number with every other number? How would the time complexity change?"
Practice
library contract in blockchain development?Solution
Step 1: Understand the role of library contracts
Library contracts contain reusable code that can be called by other contracts to avoid duplication.Step 2: Compare options with library purpose
Only To reuse code across multiple smart contracts correctly describes code reuse, while others describe unrelated blockchain functions.Final Answer:
To reuse code across multiple smart contracts -> Option AQuick Check:
Library = Code reuse [OK]
- Confusing libraries with data storage contracts
- Thinking libraries create tokens
- Assuming libraries mine blocks
Solution
Step 1: Recall Solidity keywords for libraries
Solidity uses the keywordlibraryto declare a library contract.Step 2: Check each option's keyword
Only library MyLib {} useslibrary. Others declare contract, interface, or struct, which are different constructs.Final Answer:
library MyLib {} -> Option AQuick Check:
Keyword for library = library [OK]
- Using 'contract' instead of 'library'
- Confusing interfaces with libraries
- Trying to declare libraries as structs
test.getDouble(5)?
library MathLib {
function double(uint x) internal pure returns (uint) {
return x * 2;
}
}
contract Test {
using MathLib for uint;
function getDouble(uint value) public pure returns (uint) {
return value.double();
}
}Solution
Step 1: Understand the library function call
The library MathLib defines a functiondoublethat multiplies input by 2.Step 2: Analyze the usage in contract Test
Usingusing MathLib for uint;allows callingdouble()on uint variables. Sovalue.double()returns 5 * 2 = 10.Final Answer:
10 -> Option BQuick Check:
5 * 2 = 10 [OK]
- Expecting the original value instead of doubled
- Thinking 'using for' is optional for calling library functions
- Confusing internal and public function visibility
library Utils {
function increment(uint x) public pure returns (uint) {
return x + 1;
}
}
contract Counter {
using Utils for uint;
uint count;
function increase() public {
count = count.increment();
}
}Solution
Step 1: Check library function visibility
Library functions used with 'using for' must be internal, not public.Step 2: Verify other options
No constructor is needed here, 'using for' works with uint, and increase modifies state so cannot be view.Final Answer:
Library function must be internal, not public -> Option CQuick Check:
Library function visibility = internal [OK]
- Declaring library functions as public
- Thinking 'using for' can't be used with uint
- Marking state-changing functions as view
using for in other contracts?Solution
Step 1: Check function visibility and usage
Library functions used with 'using for' must be internal. library SafeMath { function safeAdd(uint a, uint b) public pure returns (uint) { return a + b; } } uses public (invalid), D uses view which is incorrect for pure functions. library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } uses internal pure.Step 2: Verify overflow check and Solidity 0.8+ behavior
Solidity 0.8+ has built-in overflow checks, so unchecked block in C disables protection and returns wrapped value on overflow. library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } correctly relies on built-in checks with additional require guard and no unchecked block.Final Answer:
library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } -> Option DQuick Check:
Internal pure function with overflow check = library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } [OK]
- Using public visibility in library functions
- Using view instead of pure for math functions
- Unnecessary unchecked blocks in Solidity 0.8+
Ready to prove it?
Pass the test and this pattern moves unknown
