3.区块链、区块、交易简单实现

作者: 浮云发发 | 来源:发表于2018-04-17 14:01 被阅读33次

简单的区块链、区块、交易逻辑结构与代码实现。

1.区块链逻辑结构

区块链的逻辑结构

block_chain是一个区块链,block是一个区块,message是消息,transcation是交易。
一个区块链是多个区块的连接;一个区块包含多个消息;一个消息有一个交易。

2.区块链类图

区块链类图

3.代码

以下四个类可直接执行看效果,感受区块链、区块、消息、交易的数据结构。

Transcation.py

import datetime

class Transcation:
    def __init__(self,payer,recer,money):
        self.payer = payer
        self.recer = recer
        self.money = money
        self.timestamp = datetime.datetime.now()

    def __repr__(self):
        return str(self.payer) + " pay " + str(self.recer) + " " + str(self.money) + " " + str(self.timestamp)

if __name__ == "__main__":
    t1 = Transcation("I","him",10)
    print(t1)

NiuMessage.py

import hashlib
import datetime
from Transcation import Transcation

class NiuMessage:

    def __init__(self,transcation):
        self.transcation = transcation
        self.hash = None
        self.pre_hash = None
        self.timestamp = datetime.datetime.now()
        self.payload_hash = self._hash_payload() #交易后的hash

    def _hash_payload(self): #对交易时间与数据进行hash
        return hashlib.sha256( (str(self.timestamp) + str(self.transcation)).encode("utf-8") ).hexdigest()

    def _hash_message(self): #将前一个hash与交易后的hash,再hash
        return hashlib.sha256( (str(self.pre_hash) + str(self._hash_payload())).encode("utf-8") ).hexdigest()

    def seal(self): #就是将交易hash,设置为现在的hash,这里为什么不能赋值在构造器,因为构造器不能调用两个函数
        self.hash = self._hash_message()

    def __repr__(self):
        mystr = "hash:{} , pre_hash:{} , data:{}".format(self.hash,self.pre_hash,self.transcation)
        return mystr

    def validate(self,Message): #上个交易的hash与数据要能对上
        if (Message.hash != self.pre_hash):
            raise InvalidateMessage("交易hash被篡改 ," + str(self))
        if(Message.hash != Message._hash_message()):
            raise InvalidateMessage("交易数据被篡改 ,"+str(self))

    def link(self,Message):
        self.pre_hash = Message.hash

class InvalidateMessage(Exception):
    def __init__(self,*args,**kwargs):
        Exception.__init__(self,*args,**kwargs)

if __name__ == "__main__":
    t1 = Transcation("I", "him", 10)
    t2 = Transcation("him", "it", 10)
    t3 = Transcation("it", "her", 10)

    m1 = NiuMessage(t1)
    m1.seal()

    m2 = NiuMessage(t2)
    m2.link(m1)
    m2.seal()
    m2.validate(m1)

    m3 = NiuMessage(t3)
    m3.link(m2)
    m3.seal()
    m3.validate(m2)

    print(m1)
    print(m2)
    print(m3)

    #m2.hash="33"  '3448aa15999c2f73e61e5bfc4a72286823a6b52d23bf571d9fcbb7ba631bed97'
    #m2.transcation = "fda"
    #m3.validate(m2)

NiuBlock.py

import hashlib
import datetime
from NiuMessage import NiuMessage
from Transcation import Transcation
from NiuMessage import InvalidateMessage

class NiuBlock:
    def __init__(self,*args):
        self.hash = None
        self.pre_hash = None
        self.timestamp = None
        self.message_list = [] #存储多个交易记录
        if args:
            for arg in args:
                self.add_message(arg) #一次打包多个交易进区块

    def add_message(self,message): #将message放进区块的message列表中
        if(len(self.message_list)>0):
            message.link(self.message_list[-1]) #message与上一个message相连
        message.seal() #这是message的seal,不是block的seal
        if (len(self.message_list) > 0):
            message.validate(self.message_list[-1]) #这是message的validate,不是block的
        self.message_list.append(message)

    def link(self,block):
        self.pre_hash = block.hash

    def seal(self):
        self.timestamp = datetime.datetime.now()
        self.hash = self.hash_block()

    def hash_block(self):
        return hashlib.sha256( (str(self.pre_hash) + \
                                str(self.timestamp) + \
                                str(self.message_list[-1].hash)).encode("utf-8") ).hexdigest()

    def validate(self,block):#验证block
        if (block.hash != self.pre_hash):
            raise InvalidBlockException("区块hash被篡改 ," + str(self))

    def validate_message_list(self):#验证消息列表
        for i,message in enumerate(self.message_list):
            if i>0 :
                message.validate(self.message_list[i-1])
        return str(self) + " ,数据ok"

    def __repr__(self):
        return "money block=hash:{},pre_hash:{},len:{},timestamp:{}".\
            format(self.hash,self.pre_hash,len(self.message_list),self.timestamp)

class InvalidBlockException(Exception):
    def __init__(self,*args,**kwargs):
        Exception.__init__(self,*args,**kwargs)

if __name__ == "__main__":
    try:
        t1 = Transcation("I", "him", 10)
        t2 = Transcation("him", "it", 10)
        t3 = Transcation("it", "her", 10)

        m1 = NiuMessage(t1)
        m2 = NiuMessage(t2)
        m3 = NiuMessage(t3)

        niu = NiuBlock(m1,m2,m3)
        niu.seal()

        #m2.hash = "hehe"

        #niu.message_list[1] = m3

        print(niu.validate_message_list()) #仅是校验message,还不是校验区块之间的连接
    except InvalidateMessage as e:
        print(e)
    except InvalidBlockException as e:
        print(e)

NiuBlockChain.py

from NiuMessage import NiuMessage
from Transcation import Transcation
from NiuMessage import InvalidateMessage
from NiuBlock import InvalidBlockException
from NiuBlock import NiuBlock

class NiuBlockChain:
    def __init__(self):
        self.block_list = []

    def __repr__(self):
        mystr = "block chain:\n"
        for i,block in enumerate(blockchain.block_list):
            mystr += "block {} , {}  \n".format(i,block)
            for j,message in enumerate(block.message_list):
                mystr += "  message {}\n".format(message)
        return mystr

    def add_block(self,block):
        if(len(self.block_list) > 0):
            block.link(self.block_list[-1])
        block.seal()
        if (len(self.block_list) > 0):
            block.validate(self.block_list[-1])
        self.block_list.append(block)

    def validate_block_list(self):
        for i,block in enumerate(self.block_list):
            if i>0 :
                try:
                    block.validate(self.block_list[i-1])
                except InvalidateBlockChainException as e:
                    raise InvalidateBlockChainException("无效blockchain,区块被修改")
        return str(self) + " ,数据ok"

class InvalidateBlockChainException(Exception):
        def __init__(self, *args, **kwargs):
            Exception.__init__(self, *args, **kwargs)

if __name__ == "__main__":
    try:

        blockchain = NiuBlockChain()

        t1 = Transcation("I", "him", 10)
        t2 = Transcation("him", "it", 10)
        t3 = Transcation("it", "her", 10)
        t4 = Transcation("her", "mm", 10)
        t5 = Transcation("mm", "gg", 10)
        t6 = Transcation("gg", "pp", 10)

        m1 = NiuMessage(t1)
        m2 = NiuMessage(t2)
        m3 = NiuMessage(t3)
        m4 = NiuMessage(t4)
        m5 = NiuMessage(t5)
        m6 = NiuMessage(t6)

        b1 = NiuBlock(m1,m2)
        blockchain.add_block(b1)

        b2 = NiuBlock(m3)
        blockchain.add_block(b2)

        b3 = NiuBlock(m4,m5,m6)
        blockchain.add_block(b3)

        #blockchain.add_block(b2) 测试区块被修改

        print(blockchain.validate_block_list())

    except InvalidateMessage as e:
        print(e)
    except InvalidBlockException as e:
        print(e)
    except InvalidateBlockChainException as e:
        print(e)
NiuBlockChain执行结果

可以看出区块链区块之间由hash连接,区块中交易的消息也是由hash连接,一旦任何数据被修改,hash之间就连接不上了。

相关文章

  • 3.区块链、区块、交易简单实现

    简单的区块链、区块、交易逻辑结构与代码实现。 1.区块链逻辑结构 block_chain是一个区块链,block是...

  • 5.区块链挖矿与交易实现

    本段代码将实现:1.区块链挖矿;2.挖矿后获得记账权;3.将链中的交易打包进区块。 NiuCoinBlockCha...

  • 区块链之dew分析

    DEW是全球区块链交易所,主要业务涵盖区块链资产交易、区块链指数衍生品交易、区块链全球的证券衍生品、区块链商品衍生...

  • BitMEX交易所上币流程是什么

    BitMEX交易所上币流程是什么?区块链交易所开发,区块链商城系统开发,区块链应用技术开发,区块链钱包开发,区块链...

  • Bithumb交易所上币条件是什么

    Bithumb交易所上币条件是什么,区块链交易所开发,区块链商城系统开发,区块链应用技术开发,区块链钱包开发,区块...

  • Python实现一个新币种

    简单区块链模型,实现了链式结构,创建区块,添加交易数据,共识算法(POW挖矿),分叉选择,工作量证明,多节点数据一...

  • 解析区块链最常见的误区,你应该有所了解!

    在现在,比特币交易和区块链技术已经成为了全球性公众类话题,区块链技术可以实现点对点的即时性交易,而广受喜爱。 区块...

  • 1.区块链概念及特点

    区块链 = 区块 + 链 区块就是所有交易记录的打包形成的交易单。每个区块记录了上一个区块的地址,根据这个地址区块...

  • 金融区块链交易所开发,宝安币币交易所系统开发

    金融区块链交易所开发,宝安币币交易所系统开发 区块链交易所开发,区块链币币交易所系统搭建,区块链技术将应用于金融行...

  • 区块链入门

    区块链入门 区块链 区块链是不可破坏的交易记录的数字账本,对于参与交易的各方来说,他们共享区块链上的数据,而且各方...

网友评论

  • 智博区块链:请在表头写上下文,有qyb奖励:
    本文由【区块链研习社】优质内容计划支持,更多关于区块链的深度好文,请点击区块链研习社传送门

本文标题:3.区块链、区块、交易简单实现

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