Achieve a simple block chain

Because the leaders of the speech, this technology becomes hot block chain up to the center seems to have become the trend.

Or late at night every day before going to bed brush B station, look up the main dynamic animation or technology, or a number of scientific reports, suddenly to brush up on the main unconventional series of video blocks chain, listening to really block chain an understanding, so we plan to follow chiefs also use your favorite language to achieve a simple block chain.
Here Insert Picture Description
Video links point here
, I think, if not specialize in this area, but on a very hot topic of technology still have to have a certain understanding, a word to learn more is always no harm.

Gangster js code is written, GitHub link here , I have to realize what python

#首先导库,导入sha256加密算法
import hashlib


#区块
class Block:
    def __init__(self,data,previousHash):
        self.data=data
        self.previousHash=previousHash
        self.hash=self.computeHash()
    #计算hash值
    def computeHash(self):
        return hashlib.sha256((self.data+self.previousHash).encode("utf-8")).hexdigest()
    
    
#链
class Chain:
    def __init__(self):
        self.chain=[self.bigBang()]
    #第一个区块
    def bigBang(self):
        genesisBlock=Block("我是祖先","")
        return genesisBlock
    
    #最后的区块
    def getLastestBlock(self):
        return self.chain[len(self.chain)-1]
    
    #添加区块到区块链上
    def addBlockToChain(self,newBlock):
        newBlock.previousHash=self.getLastestBlock().hash
        newBlock.hash=newBlock.computeHash()
        self.chain.append(newBlock)
        print("添加新区块成功")
        
    #验证这个当前的区块链是否合法
    #当前的数据有没有被篡改
    #我们要验证区块的previousHash是否等于previous区块的hash
    def validateChain(self):
        if(len(self.chain)==1):
            if(self.chain[0].hash!=self.chain[0].computeHash()):
                return False
            return True
        else:
            # self.chain[1] 是第二个区块
            # 我们从第二个区块开始 验证
            # 验证到最后一个区块 this.chain.length -1 
            for i in range(1,len(self.chain)):
                blockToValidate=self.chain[i]
                #当前的数据有没有被篡改
                if(blockToValidate.hash!=blockToValidate.computeHash()):
                    print("数据篡改")
                    return False
                #我们要验证区块的previousHash是否等于previous区块的hash
                previousBlock=self.chain[i-1]
                if(blockToValidate.previousHash!=previousBlock.hash):
                    print("前后区块链接断裂")
                    return False
            else:
                return True
            
#初始化一个区块链           
mychain=Chain()
print(mychain.validateChain())

#添加两个新区块
block1=Block("转账十元","")
mychain.addBlockToChain(block1)
block2=Block("转账一百元","")
mychain.addBlockToChain(block2)
print(mychain.validateChain())

#查看区块的数据
print(mychain.chain[0].data,mychain.chain[0].previousHash,mychain.chain[0].hash)
print(mychain.chain[1].data,mychain.chain[1].previousHash,mychain.chain[1].hash)
print(mychain.chain[2].data,mychain.chain[2].previousHash,mychain.chain[2].hash)

#尝试篡改区块链信息
mychain.chain[1].data="转账二元"
mychain.chain[1].hash=mychain.chain[1].computeHash()
print(mychain.chain[1].data)
print(mychain.validateChain())

Here Insert Picture Description
Gangster video also said to encourage use your favorite language to implement it, so a few days also intend to go with is learning to write a try, probably it is so, then the code free pass github.

Published 85 original articles · won praise 55 · views 20000 +

Guess you like

Origin blog.csdn.net/shelgi/article/details/103525307