Python中如何学习区块链技术

最初认识到区块链技术是在 17 年初,是从国内一家做 ICO 资讯 挖矿监控的公司中面试中了解到的。 当时面试的岗位是 iOS,遗憾最后没能顺利加入,在二面挂掉。当时由于对区块链的知识过于肤浅, 包括自己的 iOS 技术达不到要求等。

17 年底也正是 Btc 涨到顶峰的时候, 才开始意识到是不是应该好好学习一下区块链领域的知识等等。 最近在网上找了些资料准备抽工作之余时间入门。

学习资料链接: 区块链技术入门,涉及哪些编程语言?

社区:EOS github、BTC github、ETH github、中国 ETH。

希望有接触这块领域的大佬可以小弟分享分享学习心得, 资源链接等等,带小弟一把😆。

以及喜欢区块链想入门学习或炒币的朋友, 也可以一起交流交流哈。


Python中如何学习区块链技术

6 回复

要学区块链,Python确实是个好起点。直接给你个能跑的区块链核心代码,跑起来就懂了。

import hashlib
import json
from time import time
from typing import List, Dict, Any

class Block:
    def __init__(self, index: int, transactions: List[Dict], timestamp: float, previous_hash: str):
        self.index = index
        self.transactions = transactions
        self.timestamp = timestamp
        self.previous_hash = previous_hash
        self.nonce = 0
        self.hash = self.calculate_hash()
    
    def calculate_hash(self) -> str:
        block_string = json.dumps({
            "index": self.index,
            "transactions": self.transactions,
            "timestamp": self.timestamp,
            "previous_hash": self.previous_hash,
            "nonce": self.nonce
        }, sort_keys=True).encode()
        return hashlib.sha256(block_string).hexdigest()
    
    def mine_block(self, difficulty: int):
        while self.hash[:difficulty] != "0" * difficulty:
            self.nonce += 1
            self.hash = self.calculate_hash()

class Blockchain:
    def __init__(self):
        self.chain: List[Block] = []
        self.difficulty = 4
        self.pending_transactions: List[Dict] = []
        self.create_genesis_block()
    
    def create_genesis_block(self):
        genesis_block = Block(0, [], time(), "0")
        genesis_block.mine_block(self.difficulty)
        self.chain.append(genesis_block)
    
    def get_latest_block(self) -> Block:
        return self.chain[-1]
    
    def add_transaction(self, sender: str, recipient: str, amount: float):
        self.pending_transactions.append({
            "sender": sender,
            "recipient": recipient,
            "amount": amount
        })
    
    def mine_pending_transactions(self, miner_address: str):
        block = Block(
            len(self.chain),
            self.pending_transactions,
            time(),
            self.get_latest_block().hash
        )
        block.mine_block(self.difficulty)
        self.chain.append(block)
        self.pending_transactions = []
    
    def is_chain_valid(self) -> bool:
        for i in range(1, len(self.chain)):
            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

# 使用示例
if __name__ == "__main__":
    my_blockchain = Blockchain()
    
    # 添加交易
    my_blockchain.add_transaction("Alice", "Bob", 10)
    my_blockchain.add_transaction("Bob", "Charlie", 5)
    
    # 挖矿
    print("开始挖矿...")
    my_blockchain.mine_pending_transactions("miner_address")
    print(f"区块 #{my_blockchain.get_latest_block().index} 已挖出")
    print(f"哈希: {my_blockchain.get_latest_block().hash}")
    
    # 验证链
    print(f"区块链有效: {my_blockchain.is_chain_valid()}")
    
    # 查看链信息
    print(f"\n区块链长度: {len(my_blockchain.chain)}")
    for block in my_blockchain.chain:
        print(f"区块 #{block.index}: {block.hash}")

这个代码实现了区块链的核心:区块结构、哈希计算、工作量证明(挖矿)、交易处理和链验证。跑起来看看每个区块怎么生成的,哈希怎么算的,链怎么连起来的。

学区块链别光看理论,先把这个代码跑通,然后自己改改:试试改难度值看挖矿时间变化,加个交易验证逻辑,或者实现个简单的P2P网络。理解了这些基础概念,再去看智能合约(用web3.py)、以太坊这些就顺了。

一句话建议:从实现基础区块链开始,边写代码边理解核心概念。

尴尬了 。。。
学习资料链接: [区块链技术入门,涉及哪些编程语言?]( https://www.zhihu.com/question/46729645)
社区:[EOS github]( https://github.com/EOSIO/eos)、[BTC github]( https://github.com/bitcoin/bitcoin)、[ETH github]( https://github.com/ethereum)、[中国 ETH]( https://ethfans.org/topics)。

要不组个区块链学习小分队吧.本人目前 java 党

可以啊、 我重新这这个专题上发个帖 -.-。 这个添加和回复的格式 233333

我也在学习。。零基础,求组队。。

回到顶部