# Simple-Blockchain **Repository Path**: rwzsjfm/Simple-Blockchain ## Basic Information - **Project Name**: Simple-Blockchain - **Description**: A simple blockchain demo by python. - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-19 - **Last Updated**: 2026-02-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README
class Block:
"""
区块类
"""
def __init__(self, index, timestamp, data, previous_hash=' '):
"""
初始化函数
"""
self.index = index
self.timestamp = timestamp
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
"""
计算hash
"""
return hashlib.sha256(str(self.index) + self.previous_hash + self.timestamp + json.dumps(self.data)).hexdigest()
def print_block(self):
"""
输出区块信息
"""
print "Block #" + str(self.index)
print "Data " + str(self.data)
print "Block Hash: " + str(self.hash)
print "Block Previous: " + str(self.previous_hash)
class BlockChain:
"""
区块链类
"""
def __init__(self):
"""
初始化函数
"""
self.chain = [self.create_genesis_block()]
def create_genesis_block(self):
"""
创建创世块
"""
return Block(0, "29/10/2018", "Genesis Block", "0")
def get_last_block(self):
"""
获取最近的一个区块
"""
return self.chain[len(self.chain) - 1]
def add_block(self, new_block):
"""
添加区块
"""
new_block.previous_hash = self.get_last_block().hash
new_block.hash = new_block.calculate_hash()
self.chain.append(new_block)
def is_chain_valid(self):
"""
校验区块链是否合法
"""
chain_len = len(self.chain)
for i in range(1, chain_len):
current_block = self.chain[i]
previous_block = self.chain[i-1]
# 校验数据是否被篡改
if current_block.hash != current_block.calculate_hash():
return False
if current_block.previous_hash != previous_block.hash:
return False
return True
def print_block_chain(self):
"""
输出区块链
"""
chain_len = len(self.chain)
for i in range(1, chain_len):
self.chain[i].print_block()
if __name__ == "__main__":
my_coin = BlockChain()
my_coin.add_block(Block(1, "23/10/2018", {"account": "DoubleQ", "amount": 25, "action": "buy"}))
my_coin.add_block(Block(2, "24/10/2018", {"account": "Eric", "amount": 10, "action": "buy"}))
my_coin.add_block(Block(3, "25/10/2018", {"account": "Winky", "amount": 20, "action": "buy"}))
my_coin.add_block(Block(4, "26/10/2018", {"account": "Xxz", "amount": 4, "action": "buy"}))
my_coin.print_block_chain()
print "Chain valid? " + str(my_coin.is_chain_valid())
my_coin.chain[1].data = {"account": "DoubleQ", "amount": 100, "action": "buy"}
print "Chain valid? " + str(my_coin.is_chain_valid())
输出如下:
Block #1
Data {'action': 'buy', 'account': 'DoubleQ', 'amount': 25}
Block Hash: 20d59499ef38bda24de15df140fd195b2627a9707453c6afd12458866ab5eba9
Block Previous: c3f4fe865ecc110ea0ed13fb3602beb2468eb2fdedc7a9aeb9eec245dd414763
Block #2
Data {'action': 'buy', 'account': 'Eric', 'amount': 10}
Block Hash: 0797ecc35e15f4758b635ee4053006bbb9a61ca9cba8876fbfa6d9a2c186a882
Block Previous: 20d59499ef38bda24de15df140fd195b2627a9707453c6afd12458866ab5eba9
Block #3
Data {'action': 'buy', 'account': 'Winky', 'amount': 20}
Block Hash: 28bf741c567028c4a3f3975aacb6f7e23561d240f06608cd1241d308fe40557b
Block Previous: 0797ecc35e15f4758b635ee4053006bbb9a61ca9cba8876fbfa6d9a2c186a882
Block #4
Data {'action': 'buy', 'account': 'Xxz', 'amount': 4}
Block Hash: 29eae18d6f72bd2203c7a3fdce3f688178aae8b371fd530ddffa67a042be41c1
Block Previous: 28bf741c567028c4a3f3975aacb6f7e23561d240f06608cd1241d308fe40557b
Chain valid? True
Chain valid? False