Introduction
Blockchains help keep data safe and shared. Public and private blockchains decide who can join and see the data.
Blockchains help keep data safe and shared. Public and private blockchains decide who can join and see the data.
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.
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.Public Blockchain: - Decentralized - No single owner Private Blockchain: - Centralized control - Owned by an organization
This simple program shows how public blockchains allow anyone to join and add data, while private blockchains only allow known members.
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")
Public blockchains are slower but more open.
Private blockchains are faster but less open.
Choose based on who needs access and trust level.
Public blockchains are open to everyone.
Private blockchains restrict access to trusted members.
Each type fits different needs for sharing and security.
def can_access(user, blockchain_type):
if blockchain_type == 'public':
return True
elif blockchain_type == 'private':
return user in ['Alice', 'Bob']
else:
return False
print(can_access('Alice', 'public'))
print(can_access('Eve', 'private'))
print(can_access('Bob', 'private'))
What is the output?def check_access(user, blockchain):
if blockchain == 'private':
if user == ['Alice', 'Bob']:
return True
else:
return False
else:
return True
print(check_access('Alice', 'private'))Pass the test and this pattern moves unknown