Cross-chain bridges in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using cross-chain bridges, we want to know how the time to transfer assets grows as more transactions happen.
We ask: How does the work needed change when more users use the bridge?
Analyze the time complexity of the following code snippet.
function processBridgeTransfers(transfers) {
for (let i = 0; i < transfers.length; i++) {
verifyTransfer(transfers[i]);
updateSourceChain(transfers[i]);
updateDestinationChain(transfers[i]);
}
}
function verifyTransfer(transfer) {
// Check signatures and balances
}
function updateSourceChain(transfer) {
// Lock tokens on source chain
}
function updateDestinationChain(transfer) {
// Mint tokens on destination chain
}
This code processes a list of transfers by verifying and updating both chains for each transfer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each transfer in the list.
- How many times: Once for every transfer in the input array.
As the number of transfers grows, the work grows in a straight line with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 times the work |
| 100 | About 100 times the work |
| 1000 | About 1000 times the work |
Pattern observation: Doubling the number of transfers roughly doubles the work needed.
Time Complexity: O(n)
This means the time to process transfers grows directly with how many transfers there are.
[X] Wrong: "Processing multiple transfers takes the same time as one transfer."
[OK] Correct: Each transfer requires separate verification and updates, so more transfers mean more work.
Understanding how work grows with input size helps you explain how blockchain bridges handle many users efficiently.
"What if the verifyTransfer function itself loops over a list of validators? How would the time complexity change?"
Practice
cross-chain bridge in blockchain technology?Solution
Step 1: Understand the role of cross-chain bridges
Cross-chain bridges enable communication and asset transfers between different blockchains.Step 2: Compare options with this role
Only To connect different blockchains and allow asset transfers between them describes connecting blockchains and transferring assets, which matches the bridge's purpose.Final Answer:
To connect different blockchains and allow asset transfers between them -> Option DQuick Check:
Cross-chain bridge = connect blockchains [OK]
- Confusing bridges with mining or block creation
- Thinking bridges create new cryptocurrencies
- Assuming bridges only increase block size
Solution
Step 1: Recall how cross-chain bridges work
They lock tokens on the source chain to prevent double spending and mint equivalent tokens on the destination chain.Step 2: Match this with the options
Lock tokens on the source chain and mint equivalent tokens on the destination chain correctly describes locking on source and minting on destination, which is the standard process.Final Answer:
Lock tokens on the source chain and mint equivalent tokens on the destination chain -> Option AQuick Check:
Lock then mint = bridge step [OK]
- Mixing up minting and burning order
- Thinking tokens transfer directly without locking
- Assuming minting happens on source chain
function bridgeTransfer(amount, sourceChain, destChain) {
lockTokens(sourceChain, amount);
mintTokens(destChain, amount);
return 'Transfer complete';
}
What will be the output when calling bridgeTransfer(100, 'ChainA', 'ChainB')?Solution
Step 1: Analyze the function steps
The function calls lockTokens and mintTokens, then returns the string 'Transfer complete'.Step 2: Determine the output of the function call
Assuming lockTokens and mintTokens work correctly, the function returns 'Transfer complete'.Final Answer:
'Transfer complete' -> Option AQuick Check:
Function returns 'Transfer complete' [OK]
- Confusing function calls with return value
- Assuming intermediate functions print output
- Ignoring the return statement
function bridgeTransfer(amount, sourceChain, destChain) {
lockTokens(destChain, amount);
mintTokens(sourceChain, amount);
return 'Transfer complete';
}
What is the bug in this code?Solution
Step 1: Check the order of locking and minting
Tokens should be locked on the source chain and minted on the destination chain.Step 2: Identify the argument mismatch
The code locks tokens on destChain and mints on sourceChain, which is reversed.Final Answer:
The lockTokens and mintTokens calls have swapped chain arguments -> Option BQuick Check:
Lock on source, mint on destination [OK]
- Swapping source and destination chains
- Forgetting to return a value
- Using wrong function names
Solution
Step 1: Understand double spending prevention
Tokens must be locked on the source chain before minting on the destination chain to avoid duplicates.Step 2: Evaluate approaches for enforcing this
Using an event listener to confirm locking before minting ensures the correct order and security.Final Answer:
Use an event listener to confirm tokens are locked on source chain before minting on destination chain -> Option CQuick Check:
Confirm lock event before minting [OK]
- Minting before locking causes double spending
- Allowing mint without lock breaks security
- Skipping locking or minting steps
