美文网首页
区块链简易实现

区块链简易实现

作者: 楠昭 | 来源:发表于2019-12-06 21:22 被阅读0次

浩哥小课堂的实验,比较有收获的点就是工作量证明以及共识机制的设计,完整代码参考https://github.com/Ryuchen/BlockChain

一、总体框架

定义BlockChain类:
    1. 初始化函数:__init__(self)
    2. 添加新区块的函数:new_block(self)
    3. 添加新交易的函数:new_transaction(self)
    4. 生成区块哈希值的函数:hash(block)
    5. 返回最后一个区块函数:last_block(self)
    6. 工作量证明实现函数:proof_of_work(self, last_proof)
    7. 检查一个链是否有效的函数:valid_chain() 
    8. 遍历所有邻居节点找到最长有效链条:resolve_conflicts()
利用flask在网页中进行模拟挖矿
    1. 计算工作量证明 PoW
    2. 通过新增一个交易授予矿工(自己)一个币
    3. 构造新区块并将其添加到链中

二、工作量证明算法

  • 如何保证你的区块能被其他人认可?--->共识机制---->工作量证明算法(POW:proof of work)
  • 拜占庭将军问题--->解决方案:Raft算法
  • 简易方案及代码实现
    • 思路:找到一个数字 P ,使得它与前一个区块的 proof 拼接成的字符串的 Hash 值以 4个零开头
    • 具体实现代码
    @staticmethod
    def valid_proof(last_proof, proof):
        """
        Validates the Proof: Does hash(last_proof, proof) contain 4 leading zeroes?
        :param last_proof: <int> Previous Proof
        :param proof: <int> Current Proof
        :return: <bool> True if correct, False if not.
        """
        guess = f'{last_proof}{proof}'.encode()
        guess_hash = hashlib.sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"

    def proof_of_work(self, last_proof):
        """
        Simple Proof of Work Algorithm:
        - Find a number p' such that hash(pp') contains leading 4 zeroes, where p is the previous p'
        - p is the previous proof, and p' is the new proof
        :param last_proof: <int>
        :return: <int>
        """
        proof = 0
        while self.valid_proof(last_proof, proof) is False:
            proof += 1
        return proof

三、共识机制设计

共识就是指节点之间要知道对方的存在,因此每个节点都需要保存一份包含网络中其它节点的记录。另外为了解决冲突问题,制定最长的有效链条是最权威的规则。

# 第一个方法 valid_chain() 负责检查一个链是否有效,方法是遍历每个块并验证散列和证明。
# 第二个方法 resolve_conflicts() 是一个遍历我们所有邻居节点的方法,下载它们的链并使用上面的方法验证它们。如果找到一个长度大于我们的有效链条,我们就取代我们的链条。

    def valid_chain(self, chain):
        """
        Determine if a given blockchain is valid
        :param chain: <list> A blockchain
        :return: <bool> True if valid, False if not
        """
        last_block = chain[0]
        current_index = 1
        while current_index < len(chain):
            block = chain[current_index]

            print(f'{last_block}')
            print(f'{block}')
            print("\n-----------\n")

            # Check that the hash of the block is correct
            if block['previous_hash'] != self.hash(last_block):
                return False
            # Check that the Proof of Work is correct
            if not self.valid_proof(last_block['proof'], block['proof']):
                return False

            last_block = block
            current_index += 1
        return True

    def resolve_conflicts(self):
        """
        This is our Consensus Algorithm, it resolves conflicts
        by replacing our chain with the longest one in the network.
        :return: <bool> True if our chain was replaced, False if not
        """
        neighbours = self.nodes
        new_chain = None
        # We're only looking for chains longer than ours
        max_length = len(self.chain)
        # Grab and verify the chains from all the nodes in our network
        for node in neighbours:
            response = requests.get(f'http://{node}/chain')
            if response.status_code == 200:
                length = response.json()['length']
                chain = response.json()['chain']

                # Check if the length is longer and the chain is valid
                if length > max_length and self.valid_chain(chain):
                    max_length = length
                    new_chain = chain
            # Replace our chain if we discovered a new, valid chain longer than ours
            if new_chain:
                self.chain = new_chain
                return True
        return False

相关文章

  • python 区块链的简易实现

    区块链技术因为比特币的火爆而广受关注,我们这里用 python 代码来聊一下区块链技术: 区块链实现原理的简易描述...

  • 区块链简易实现

    浩哥小课堂的实验,比较有收获的点就是工作量证明以及共识机制的设计,完整代码参考https://github.com...

  • 使用go语言实现简易版的区块链

    使用go语言实现简易版的区块链 区块链概念 区块链(Blockchain),是比特币的一个重要概念,它本质上是一个...

  • NodeJS实现简易区块链

    之前由于课程要求,基于Nodejs做了一个实现简易区块链。要求非常简单,结构体记录区块结构,顺便能向链中插入新的区...

  • Java简易实现区块链

      区块链露脸率越来越大,我也很好奇这是什么。找了些视频,发现有个很不错,是用JavaScript实现的一个简易区...

  • 区块链开发:持久化 #C12

    在之前的版本中,我们已经实现了区块链的基本模型,包括区块链的基本结构、不同的共识机制与简易的P2P网络。但是仍有一...

  • 在 iOS 中实现区块链

    在 iOS 中实现区块链 在 iOS 中实现区块链

  • 接下来,多关注链改项目——3.17日BTC/ETH/EOS/XR

    虽然区块链经过了10年的发展,但是区块链依然停留在虚拟层面,如何实现区块链实现实体落地,如何实现传统产业与区块链相...

  • 区块链市场---进入区块链第一款APP

    区块链市场目的实现“深耕中国,完善布局”的发展战略规划,并以区块链行业为核心, 扩展全球区块链应用市场。实现“深耕...

  • 侧链,再见;子链,您好!(GBCAX链交所)

    墨客率先提出并实现子链技术,引领区块链进入3.0时代 墨客区块链首次提出并实现了子链技术,在区块链技术领域迈出了坚...

网友评论

      本文标题:区块链简易实现

      本文链接:https://www.haomeiwen.com/subject/owxygctx.html