0
0
Blockchain / Solidityprogramming~5 mins

Public vs private blockchains

Choose your learning style9 modes available
Introduction

Blockchains help keep data safe and shared. Public and private blockchains decide who can join and see the data.

When you want everyone to see and join the network, like for a public cryptocurrency.
When a company wants to keep data private and only allow certain people to join.
When you want fast transactions inside a trusted group, like a business team.
When you want to build a community project open to all.
When you need to control who can add or change data for security reasons.
Syntax
Blockchain / Solidity
Public Blockchain:
- Anyone can join and read data.
- Anyone can add new data (transactions).
- Examples: Bitcoin, Ethereum.

Private Blockchain:
- Only invited members can join.
- Only certain members can add data.
- Examples: Hyperledger Fabric, Corda.

Public blockchains are open and transparent.

Private blockchains are closed and controlled.

Examples
This shows who can join and use each type.
Blockchain / Solidity
Public Blockchain Example:
Anyone can download the Bitcoin software and start using it.

Private Blockchain Example:
A bank creates a blockchain only its branches can access.
Shows the control difference between public and private blockchains.
Blockchain / Solidity
Public Blockchain:
- Decentralized
- No single owner

Private Blockchain:
- Centralized control
- Owned by an organization
Sample Program

This simple program shows how public blockchains allow anyone to join and add data, while private blockchains only allow known members.

Blockchain / Solidity
class Blockchain:
    def __init__(self, is_public):
        self.is_public = is_public
        self.members = []
        self.data = []

    def join(self, member):
        if self.is_public or member in self.members:
            print(f"{member} joined the blockchain.")
            if member not in self.members:
                self.members.append(member)
        else:
            print(f"{member} cannot join this private blockchain.")

    def add_data(self, member, data):
        if self.is_public or member in self.members:
            self.data.append((member, data))
            print(f"{member} added data: {data}")
        else:
            print(f"{member} cannot add data to this private blockchain.")

# Public blockchain example
public_chain = Blockchain(is_public=True)
public_chain.join("Alice")
public_chain.add_data("Alice", "Transaction 1")

# Private blockchain example
private_chain = Blockchain(is_public=False)
private_chain.members = ["Bob", "Carol"]
private_chain.join("Bob")
private_chain.add_data("Bob", "Private Transaction")
private_chain.join("Eve")
private_chain.add_data("Eve", "Hacker Transaction")
OutputSuccess
Important Notes

Public blockchains are slower but more open.

Private blockchains are faster but less open.

Choose based on who needs access and trust level.

Summary

Public blockchains are open to everyone.

Private blockchains restrict access to trusted members.

Each type fits different needs for sharing and security.