Bird
Raised Fist0
Blockchain / Solidityprogramming~7 mins

Sidechains in Blockchain / Solidity

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Sidechains help blockchains work together and let you move assets between them easily. They make blockchains faster and add new features without changing the main blockchain.

You want to try new features without risking the main blockchain.
You need faster transactions than the main blockchain can handle.
You want to move tokens or coins between two different blockchains.
You want to keep the main blockchain secure but still add extra functions.
You want to reduce fees by using a smaller, connected blockchain.
Syntax
Blockchain / Solidity
Sidechain {
  main_chain: Blockchain
  side_chain: Blockchain
  peg_in: function(asset) -> lock asset on main_chain
  peg_out: function(asset) -> unlock asset on main_chain
  transfer: function(asset, from_chain, to_chain)
}

This is a simplified structure showing how sidechains connect to a main blockchain.

Functions like peg_in and peg_out handle moving assets safely between chains.

Examples
This shows how you lock an asset on the main chain and get a matching asset on the sidechain.
Blockchain / Solidity
peg_in(asset):
  lock asset on main_chain
  issue equivalent asset on side_chain
This shows how you send an asset back from the sidechain to the main chain.
Blockchain / Solidity
peg_out(asset):
  burn asset on side_chain
  unlock asset on main_chain
This function moves assets between the main chain and sidechain using peg_in and peg_out.
Blockchain / Solidity
transfer(asset, from_chain, to_chain):
  if from_chain == main_chain and to_chain == side_chain:
    peg_in(asset)
  else if from_chain == side_chain and to_chain == main_chain:
    peg_out(asset)
Sample Program

This program shows a simple example of moving coins between a main blockchain and a sidechain. It locks coins on the main chain and issues them on the sidechain, then burns coins on the sidechain and unlocks them back on the main chain.

Blockchain / Solidity
class Blockchain:
    def __init__(self, name):
        self.name = name
        self.assets = {}

    def lock_asset(self, asset, amount):
        self.assets[asset] = self.assets.get(asset, 0) - amount
        print(f"Locked {amount} {asset} on {self.name}")

    def unlock_asset(self, asset, amount):
        self.assets[asset] = self.assets.get(asset, 0) + amount
        print(f"Unlocked {amount} {asset} on {self.name}")

    def issue_asset(self, asset, amount):
        self.assets[asset] = self.assets.get(asset, 0) + amount
        print(f"Issued {amount} {asset} on {self.name}")

    def burn_asset(self, asset, amount):
        self.assets[asset] = self.assets.get(asset, 0) - amount
        print(f"Burned {amount} {asset} on {self.name}")

class Sidechain:
    def __init__(self, main_chain, side_chain):
        self.main_chain = main_chain
        self.side_chain = side_chain

    def peg_in(self, asset, amount):
        self.main_chain.lock_asset(asset, amount)
        self.side_chain.issue_asset(asset, amount)

    def peg_out(self, asset, amount):
        self.side_chain.burn_asset(asset, amount)
        self.main_chain.unlock_asset(asset, amount)

    def transfer(self, asset, amount, from_chain, to_chain):
        if from_chain == self.main_chain and to_chain == self.side_chain:
            self.peg_in(asset, amount)
        elif from_chain == self.side_chain and to_chain == self.main_chain:
            self.peg_out(asset, amount)
        else:
            print("Transfer between unsupported chains")

# Example usage
main = Blockchain("MainChain")
side = Blockchain("SideChain")

# Start with 100 coins on main chain
main.assets["COIN"] = 100

sidechain = Sidechain(main, side)

print("Before transfer:")
print(f"MainChain COIN balance: {main.assets.get('COIN', 0)}")
print(f"SideChain COIN balance: {side.assets.get('COIN', 0)}")

# Move 30 coins from main chain to sidechain
sidechain.transfer("COIN", 30, main, side)

print("After peg_in transfer:")
print(f"MainChain COIN balance: {main.assets.get('COIN', 0)}")
print(f"SideChain COIN balance: {side.assets.get('COIN', 0)}")

# Move 10 coins back from sidechain to main chain
sidechain.transfer("COIN", 10, side, main)

print("After peg_out transfer:")
print(f"MainChain COIN balance: {main.assets.get('COIN', 0)}")
print(f"SideChain COIN balance: {side.assets.get('COIN', 0)}")
OutputSuccess
Important Notes

Sidechains need a way to securely lock and unlock assets to keep everything safe.

Assets on sidechains usually represent the same value as on the main chain but can have extra features.

Sidechains help blockchains grow without slowing down the main network.

Summary

Sidechains connect to a main blockchain to move assets safely.

They let you try new things or speed up transactions without changing the main chain.

Moving assets uses locking on one chain and issuing or burning on the other.

Practice

(1/5)
1.

What is the main purpose of a sidechain in blockchain technology?

easy
A. To store user passwords securely
B. To replace the main blockchain entirely
C. To mine new cryptocurrencies faster
D. To allow assets to move between blockchains without changing the main chain

Solution

  1. Step 1: Understand sidechain function

    Sidechains connect to a main blockchain to move assets safely without altering the main chain.
  2. Step 2: Compare options

    Only To allow assets to move between blockchains without changing the main chain describes this purpose correctly; others describe unrelated functions.
  3. Final Answer:

    To allow assets to move between blockchains without changing the main chain -> Option D
  4. Quick Check:

    Sidechains move assets safely = C [OK]
Hint: Sidechains move assets without changing main chain [OK]
Common Mistakes:
  • Thinking sidechains replace main blockchain
  • Confusing sidechains with password storage
  • Assuming sidechains speed up mining
2.

Which of the following is the correct way to describe the process of moving assets from the main chain to a sidechain?

lockOnMainChain() and issueOnSidechain() are functions.

easy
A. issueOnSidechain(); lockOnMainChain();
B. lockOnMainChain(); issueOnSidechain();
C. burnOnMainChain(); issueOnSidechain();
D. issueOnSidechain(); burnOnMainChain();

Solution

  1. Step 1: Understand asset transfer steps

    Assets are locked on the main chain first, then issued on the sidechain.
  2. Step 2: Match function order

    lockOnMainChain(); issueOnSidechain(); correctly calls lockOnMainChain() before issueOnSidechain().
  3. Final Answer:

    lockOnMainChain(); issueOnSidechain(); -> Option B
  4. Quick Check:

    Lock then issue = A [OK]
Hint: Lock assets first, then issue on sidechain [OK]
Common Mistakes:
  • Issuing before locking assets
  • Using burn instead of lock for main chain
  • Mixing function order
3.

Consider this pseudocode for moving assets from a sidechain back to the main chain:

burnOnSidechain()
unlockOnMainChain()

What will be the output if burnOnSidechain() fails?

medium
A. Assets remain locked on the main chain
B. Assets are unlocked on the main chain anyway
C. Assets are burned on the main chain
D. Assets are duplicated on both chains

Solution

  1. Step 1: Analyze burn failure effect

    If burnOnSidechain() fails, assets are not removed from the sidechain.
  2. Step 2: Understand unlock condition

    unlockOnMainChain() should only run after successful burn; if burn fails, unlock does not happen.
  3. Final Answer:

    Assets remain locked on the main chain -> Option A
  4. Quick Check:

    Burn must succeed before unlock = D [OK]
Hint: Burn sidechain assets before unlocking main chain [OK]
Common Mistakes:
  • Assuming unlock happens even if burn fails
  • Thinking assets get duplicated
  • Confusing burn with lock
4.

Find the error in this pseudocode for transferring assets to a sidechain:

lockOnMainChain()
issueOnSidechain()
unlockOnMainChain()
medium
A. Unlocking main chain assets immediately after issuing is incorrect
B. Locking assets should happen after issuing
C. Issuing on sidechain should be replaced with burning
D. Unlocking main chain assets is required here

Solution

  1. Step 1: Review transfer steps

    Assets must stay locked on the main chain until they are returned from the sidechain.
  2. Step 2: Identify incorrect unlock

    Unlocking immediately after issuing breaks asset security; unlock should happen only when assets return.
  3. Final Answer:

    Unlocking main chain assets immediately after issuing is incorrect -> Option A
  4. Quick Check:

    Unlock only after return = A [OK]
Hint: Don't unlock main chain assets too early [OK]
Common Mistakes:
  • Unlocking main chain assets too soon
  • Confusing issue with burn
  • Reversing lock and issue order
5.

You want to design a sidechain system that allows fast transactions but ensures no asset duplication. Which approach best achieves this?

Choose the correct sequence of actions when moving assets from main chain to sidechain and back.

hard
A. Issue on sidechain -> Lock on main chain -> Unlock on main chain -> Burn on sidechain
B. Burn on main chain -> Issue on sidechain -> Lock on sidechain -> Unlock on main chain
C. Lock on main chain -> Issue on sidechain -> Burn on sidechain -> Unlock on main chain
D. Unlock on main chain -> Burn on sidechain -> Lock on main chain -> Issue on sidechain

Solution

  1. Step 1: Understand asset safety steps

    To avoid duplication, assets must be locked on the main chain before issuing on sidechain, and burned on sidechain before unlocking main chain.
  2. Step 2: Match correct sequence

    Lock on main chain -> Issue on sidechain -> Burn on sidechain -> Unlock on main chain follows the correct order: lock -> issue -> burn -> unlock, ensuring assets exist only in one place at a time.
  3. Final Answer:

    Lock on main chain -> Issue on sidechain -> Burn on sidechain -> Unlock on main chain -> Option C
  4. Quick Check:

    Lock, issue, burn, unlock = B [OK]
Hint: Lock before issue; burn before unlock to avoid duplicates [OK]
Common Mistakes:
  • Issuing before locking assets
  • Unlocking main chain too early
  • Burning assets on wrong chain