资源简介

Python学习区块链入门代码。区块中数据使用MerkleTree结构,包含挖矿模拟等内容。代码易读,适合初步入门学习。

资源截图

代码片段和文件信息

# -*- coding:utf-8 -*-

import hashlib
import uuid
import random
import time
from merkletree import *

class Block(object):
    def __init__(self data=None previous_hash=None):
        self.identifier = uuid.uuid4().hex  # 产生唯一标示
        self.nonce = None  # nonce值(难度值)
        self.data = data  # 区块内容
        self.timestamp = str(time.time())  # 区块标识时间戳
        self.ver = “1.0“  # 区块链版本号
        self.random_num = random.randint(10002000)  # 区块头随机数
        self.previous_hash = previous_hash  # 父节点哈希值
        if isinstance(data list):
            self.mt = merkletree(data)
            self.merkle_root =  str(self.mt.merkle_root) # 区块MT树TopHash值
        else:
            self.mt = list()
            self.merkle_root = “None“  # 区块MT树TopHash值

    def hash(self nonce=None):
        ‘‘‘
            计算区块的哈希值 拼接区块
        ‘‘‘
        message = hashlib.sha256()
        message.update(str(self.previous_hash).encode(‘utf-8‘))
        message.update(self.ver.encode(‘utf-8‘))
        message.update(self.timestamp.encode(‘utf-8‘))
        message.update(self.identifier.encode(‘utf-8‘))
        message.update(str(nonce).encode(‘utf-8‘))
        message.update(str(self.random_num).encode(‘utf-8‘))
        message.update(str(self.merkle_root).encode(‘utf-8‘))
        return message.hexdigest()

    def hash_is_valid(self the_hash):
        return the_hash.startswith(‘0000‘)

    def __str__(self):
        return ‘Block‘.format(self.hash(self.nonce) self.nonce)

    def __repr__(self):
        return ‘Block‘.format(self.hash(self.nonce) self.nonce)

    def mine(self):
        # 初始化nonce为0
        cur_nonce = self.nonce or 0
        # 循环直到生成一个有效的哈希值
        while True:
            the_hash = self.hash(nonce=cur_nonce)
            if self.hash_is_valid(the_hash):  # 如果生成的哈希值有效
                self.nonce = cur_nonce  # 保持当前nonce值
                break  # 并退出
            else:
                cur_nonce += 1  # 若当前哈希值无效,更新nonce值,进行加1操作

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2282  2018-03-16 01:39  blockchain_2_代码\block.py

     文件       1086  2018-02-28 13:44  blockchain_2_代码\blockchain.py

     文件       2707  2018-03-16 13:17  blockchain_2_代码\main.py

     文件        472  2018-03-15 23:38  blockchain_2_代码\.idea\blockchain_1.iml

     文件        276  2018-02-28 12:51  blockchain_2_代码\.idea\modules.xml

     文件      23220  2018-03-16 18:58  blockchain_2_代码\.idea\workspace.xml

     文件        226  2018-03-15 23:38  blockchain_2_代码\.idea\misc.xml

     文件       1210  2018-03-16 00:36  blockchain_2_代码\__pycache__\blockchain.cpython-36.pyc

     文件       1727  2018-03-16 01:34  blockchain_2_代码\__pycache__\merkletree.cpython-36.pyc

     文件       2011  2018-03-16 01:39  blockchain_2_代码\__pycache__\block.cpython-36.pyc

     文件       2081  2018-03-16 01:34  blockchain_2_代码\merkletree.py

     目录          0  2018-02-28 12:52  blockchain_2_代码\.idea\inspectionProfiles

     目录          0  2018-02-28 15:21  blockchain_2_代码\.idea

     目录          0  2018-02-28 13:46  blockchain_2_代码\__pycache__

     目录          0  2018-02-28 14:01  blockchain_2_代码

----------- ---------  ---------- -----  ----

                37298                    15


评论

共有 条评论