## 引言
近年来,加密货币的兴起引发了全球范围内的关注,尤其是比特币和以太坊等知名币种。越来越多的人开始关注这些数字货币背后的技术,尤其是区块链技术。虽然完整的加密货币系统相对复杂,但通过使用 Python 编码可以相对简单地实现一个基础版本的区块链。在本篇文章中,我们将使用少于100行代码的 Python 脚本来实现一个简单的加密货币模型,从而帮助大家快速了解加密货币的核心概念和逻辑。
## 简单的区块链实现
下面是一个基础的区块链实现代码,只有82行,包含了创建区块、添加交易等基本功能:
```python
import hashlib
import time
class Block:
def __init__(self, index, previous_hash, timestamp, data, hash):
self.index = index
self.previous_hash = previous_hash
self.timestamp = timestamp
self.data = data
self.hash = hash
def calculate_hash(index, previous_hash, timestamp, data):
value = str(index) previous_hash str(timestamp) data
return hashlib.sha256(value.encode()).hexdigest()
def create_genesis_block():
return Block(0, "0", time.time(), "Genesis Block", calculate_hash(0, "0", time.time(), "Genesis Block"))
def create_new_block(previous_block, data):
index = previous_block.index 1
timestamp = time.time()
hash = calculate_hash(index, previous_block.hash, timestamp, data)
return Block(index, previous_block.hash, timestamp, data, hash)
blockchain = [create_genesis_block()]
current_transactions = []
def add_transaction(data):
current_transactions.append(data)
def mine_block():
if not current_transactions:
return False
last_block = blockchain[-1]
new_block = create_new_block(last_block, current_transactions)
blockchain.append(new_block)
return new_block
# Add transactions
add_transaction("Alice pays Bob 5 BTC")
add_transaction("Bob pays Charlie 2 BTC")
# Mine a new block with the current transactions
mined_block = mine_block()
print(f"New Block Mined: {mined_block.index}")
print(f"Hash of the new block: {mined_block.hash}")
print(f"Data in the block: {mined_block.data}")
```
### 代码解析
#### 区块(Block)类
在区块链中,每一个区块都包含了一个索引、前一个区块的哈希值、时间戳、存储的数据和自身的哈希值。我们的 `Block` 类封装了这些内容。
```python
class Block:
def __init__(self, index, previous_hash, timestamp, data, hash):
self.index = index
self.previous_hash = previous_hash
self.timestamp = timestamp
self.data = data
self.hash = hash
```
#### 计算哈希(calculate_hash)
哈希是区块链的核心机制之一,确保了数据的安全性与完整性。我们使用 SHA-256 算法来计算哈希值。
```python
def calculate_hash(index, previous_hash, timestamp, data):
value = str(index) previous_hash str(timestamp) data
return hashlib.sha256(value.encode()).hexdigest()
```
#### 创建创世区块(create_genesis_block)
创世区块是区块链的第一个区块,通常没有前一个区块。我们创建这个区块并生成其哈希值。
```python
def create_genesis_block():
return Block(0, "0", time.time(), "Genesis Block", calculate_hash(0, "0", time.time(), "Genesis Block"))
```
#### 创建新区块(create_new_block)
新区块的创建依赖于前一个区块的哈希值和当前的交易数据。
```python
def create_new_block(previous_block, data):
index = previous_block.index 1
timestamp = time.time()
hash = calculate_hash(index, previous_block.hash, timestamp, data)
return Block(index, previous_block.hash, timestamp, data, hash)
```
#### 交易与挖矿
我们定义了一个简单的交易系统,可以记录当前交易并在挖矿时生成新块。
```python
current_transactions = []
def add_transaction(data):
current_transactions.append(data)
def mine_block():
if not current_transactions:
return False
last_block = blockchain[-1]
new_block = create_new_block(last_block, current_transactions)
blockchain.append(new_block)
return new_block
```
## 相关问题探讨
###
