python开发区块链

50行Python代码构建小型区块链DogCoin(狗币)

本文介绍了如何使用python构建一个小型的区块链技术,使用Python2实现,代码不到50行。

这个简单的区块链。这将被称为DogCoin(狗币)。

(一)定义区块链的块结构。

像比特币一样,我们的DogCoin每个区块将包括作为区块的索引的哈希值,时间戳,数据以及前一个块的哈希值和新的哈希值。如下图:

index

timestamp

data

previous_hash

hash

Python代码:

import hashlib as hasher

class Block:

def __init__(self, index, timestamp, data, previous_hash):

self.index = index

self.timestamp = timestamp

self.data = data

self.previous_hash = previous_hash

self.hash = self.hash_block()

def hash_block(self):

sha = hasher.sha256()

sha.update(str(self.index) +

str(self.timestamp) +

str(self.data) +

str(self.previous_hash))

return sha.hexdigest()

(二)创建创世区块。

如前所述,每个块都需要上一个块的信息。但是这就出现了一个问题:区块链中的第一个区块,也就是“创世区块”,如何创建?

按照模块化原则,我们将创建一个函数,只需返回一个创世区块,该区块的索引为0,它在“previous hash”参数中具有任意数据值和任意值。

如下图:

Index:0

timestamp

Data:Genesis Block

previous_hash

hash

Python代码:

import datetime as date

def create_genesis_block():

# Manually construct a block with

# index zero and arbitrary previous hash

return Block(0, date.datetime.now(), “Genesis Block”, “0”)

(三) 创建后续区块。

我们需要一个新的函数来生成区块链中的后续区块。该函数将链中的前一个区块作为参数,创建要生成的区块的数据,并返回具有其相应数据的新块。新产生的区块会存储先前区块中的哈希值,区块链的完整性随着每个新的区块而增加。这个哈希链作为加密证明,有助于确保一旦新区块被添加到区块链中,它不能被替换或删除。

如下图:

Index:2

timestamp

Data:Hey! I’m block 2

previous_hash

hash

Python代码:

def next_block(last_block):

this_index = last_block.index + 1

this_timestamp = date.datetime.now()

this_data = “Hey! I’m block ” + str(this_index)

this_hash = last_block.hash

return Block(this_index, this_timestamp, this_data, this_hash)

(四)创建(组装)我们的区块链DogCoin。

区块链其实就是一个将普通指针换成哈希指针的链表,所以每个区块不仅包含了上一个区块的地址,还包括了上一个区块的哈希值 。

列表的第一个元素是创世区块,然后需要添加后续区块。本例中,我们只添加了10个新的块,用for循环搞定。

如下图:

后续区块(4)
后续区块(3)
后续区块(2)
创世区块(1)

Python代码:

# Create the blockchain and add the genesis block

blockchain = [create_genesis_block()]

previous_block = blockchain[0]

# How many blocks should we add to the chain

# after the genesis block

num_of_blocks_to_add = 20

# Add blocks to the chain

for i in range(0, num_of_blocks_to_add):

block_to_add = next_block(previous_block)

blockchain.append(block_to_add)

previous_block = block_to_add

# Tell everyone about it!

print “Block #{} has been added to blockchain!”.format(block_to_add.index)

print “Hash: {}n”.format(block_to_add.hash)

(五)打印区块链DogCoin的每个块!

我们的区块链生效了!

完整Python.代码如下:(python 2.7)

import hashlib as hasher

class Block:

def __init__(self, index, timestamp, data, previous_hash):

self.index = index

self.timestamp = timestamp

self.data = data

self.previous_hash = previous_hash

self.hash = self.hash_block()

def hash_block(self):

sha = hasher.sha256()

sha.update(str(self.index) +

str(self.timestamp) +

str(self.data) +

str(self.previous_hash))

return sha.hexdigest()

import datetime as date

def create_genesis_block():

# Manually construct a block with

# index zero and arbitrary previous hash

return Block(0, date.datetime.now(), “Genesis Block”, “0”)

def next_block(last_block):

this_index = last_block.index + 1

this_timestamp = date.datetime.now()

this_data = “Hey! I’m block ” + str(this_index)

this_hash = last_block.hash

return Block(this_index, this_timestamp, this_data, this_hash)

# Create the blockchain and add the genesis block

blockchain = [create_genesis_block()]

previous_block = blockchain[0]

#Print genesis block

print “DogCoin”

print “—————“

print “block index: {}”.format(blockchain[0].index)

print “timestamp: {}”.format(blockchain[0]. timestamp)

print “data: {}”.format(blockchain[0]. data)

print “previous_hash: {}”.format(blockchain[0]. previous_hash)

print “hash: {}n”.format(blockchain[0].hash)

print “—————“

# How many blocks should we add to the chain

# after the genesis block

num_of_blocks_to_add = 10

# Add blocks to the chain

for i in range(0, num_of_blocks_to_add):

block_to_add = next_block(previous_block)

blockchain.append(block_to_add)

previous_block = block_to_add

# Tell everyone about it!

#print”Block {} has been added to blockchain!”.format(block_to_add.index)

#print “Hash: {}n”.format(block_to_add.hash)

print “DogCoin”

print “—————“

print “block index: {}”.format(block_to_add.index)

print “timestamp: {}”.format(block_to_add. timestamp)

print “data: {}”.format(block_to_add. data)

print “previous_hash: {}”.format(block_to_add. previous_hash)

print “hash: {}n”.format(block_to_add.hash)

print “—————“

演示输出:

DogCoin

—————

block index: 0

timestamp: 2018-03-15 13:59:12.750000

data: Genesis Block

previous_hash: 0

hash: 978123c9edd1b41d5e35ce5126169cbd9f6f63efbc538b4d9ca63bb26da1a9b1

—————

DogCoin

—————

block index: 1

timestamp: 2018-03-15 13:59:12.828000

data: Hey! I’m block 1

previous_hash: 978123c9edd1b41d5e35ce5126169cbd9f6f63efbc538b4d9ca63bb26da1a9b1

hash: 0de84484d23722cde7987a9dbe4ed0af9e415679888328fc3b9c069827727cfa

—————

DogCoin

—————

block index: 2

timestamp: 2018-03-15 13:59:12.906000

data: Hey! I’m block 2

previous_hash: 0de84484d23722cde7987a9dbe4ed0af9e415679888328fc3b9c069827727cfa

hash: 7c0ce5d65626e4af2907ef6c8acfe05e9b8c39dcd5229583c0d4bc4f3d66a775

—————

DogCoin

—————

block index: 3

timestamp: 2018-03-15 13:59:12.984000

data: Hey! I’m block 3

previous_hash: 7c0ce5d65626e4af2907ef6c8acfe05e9b8c39dcd5229583c0d4bc4f3d66a775

hash: c1b7d949eea10af88659713b6145bd5069221d8fd49b337be8a8780684e0a16f

—————

DogCoin

—————

block index: 4

timestamp: 2018-03-15 13:59:13.078000

data: Hey! I’m block 4

previous_hash: c1b7d949eea10af88659713b6145bd5069221d8fd49b337be8a8780684e0a16f

hash: 4915c65e6dc42d18cdd5489f458181f746aefafd7bc8b43e8c47c68dd0c90998

—————

DogCoin

—————

block index: 5

timestamp: 2018-03-15 13:59:13.156000

data: Hey! I’m block 5

previous_hash: 4915c65e6dc42d18cdd5489f458181f746aefafd7bc8b43e8c47c68dd0c90998

hash: 95f61d76cabcf80e0ee3549ae88aa96f7b40e76f487ab1baa4ce0dee79fefbe8

—————

DogCoin

—————

block index: 6

timestamp: 2018-03-15 13:59:13.250000

data: Hey! I’m block 6

previous_hash: 95f61d76cabcf80e0ee3549ae88aa96f7b40e76f487ab1baa4ce0dee79fefbe8

hash: 3206120a85d02a7b2b3ab85188913db304e6929141b0e4956a5ab65e21e508fe

—————

DogCoin

—————

block index: 7

timestamp: 2018-03-15 13:59:13.343000

data: Hey! I’m block 7

previous_hash: 3206120a85d02a7b2b3ab85188913db304e6929141b0e4956a5ab65e21e508fe

hash: 54c4229a20dbe5e014b930eb31438c84bc6732240d023c8016dc17ad7b1b0287

—————

DogCoin

—————

block index: 8

timestamp: 2018-03-15 13:59:13.421000

data: Hey! I’m block 8

previous_hash: 54c4229a20dbe5e014b930eb31438c84bc6732240d023c8016dc17ad7b1b0287

hash: 27d4561bd21fd36a794926139de39a3ea32ced70ac19e644544884ea923d418f

—————

DogCoin

—————

block index: 9

timestamp: 2018-03-15 13:59:13.500000

data: Hey! I’m block 9

previous_hash: 27d4561bd21fd36a794926139de39a3ea32ced70ac19e644544884ea923d418f

hash: 143821446e2ccc4d7695242a591e0cd69f689a4b5f3c4c861c736deb8e5bee52

—————

DogCoin

—————

block index: 10

timestamp: 2018-03-15 13:59:13.578000

data: Hey! I’m block 10

previous_hash: 143821446e2ccc4d7695242a591e0cd69f689a4b5f3c4c861c736deb8e5bee52

hash: 1a66358fb32db223a2f998061e3bc90354b0ddb8d2c3a013cc86c63060c5c547

—————

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:dandanxi6@qq.com

(0)
上一篇 2023年 3月 23日 上午8:59
下一篇 2023年 3月 23日 上午9:05

相关推荐

  • 虾圈投票,虾圈速递最近活动

    第一组: 2022中国上市公司口碑榜 海外,投狗子十票: ***/A6K2fDiu 大消费产业,投青岛、倍轻松、石头共十票,看情况端水: ***/A6KAYWIp 倍轻松董秘,**…

    2023年 5月 6日
  • 麦克风话筒怎么连接音响(单体麦克风怎么用)

    麦克风根据使用的场景不同,对应的设置会有一些改变。下面雷萌科技小编带您了解一下麦克风对不同的配件如何连接设置吧! 麦克风怎么连接电脑需要使用驱动的麦克风? 1、正确将麦克风与电脑连…

    2023年 3月 3日
  • 中秋节放假通知图片模板(中秋节贺卡文案图片大全)

    中秋节有很多习俗一直到现在还是很流行的,比如祭月赏月拜月,品尝月饼,观赏菊花等,这些习俗从唐朝一直流传到现在,这里和大家分享的是漂亮的中秋节放假通知,以及电子贺卡的制作教程,大家可…

    2022年 12月 27日
  • 百度网盘里面的文件怎么用迅雷下

      平时喜欢追各种国外影视剧,或者是经常使用一些在线云盘的小伙伴们,对于迅雷和百度网盘这两个名字一定都不陌生。因为我们经常会因为一个个人的需要或者是工作的需要,需要在线下载超大的影…

    2023年 3月 4日
  • uv与pv的区别,pvuv计算公式

    UV(Unique visitor) 是指通过互联网访问、浏览这个网页的自然人。访问您网站的一台电脑客户端为一个访客。00:00-24:00内相同的客户端只被计算一次。 一天内同个…

    2023年 7月 20日
  • iphone浏览器超实用干货

    iPhone自带的一款浏览器,是叫做Safari的App。 它不嵌套任何广告和插件,只是单纯提供用户浏览功能,而且操作简便,提供了很多强大的功能给用户使用。 许多朋友可能只是使用它…

    2023年 4月 16日
  • 搜索营销不花钱如何获取搜索流量

    对于电商商家来说,流量是店铺的根本。没有流量更不要谈转化率。 01 如何获取流量呢?各平台玩法不同,但核心无非就是流量=站内流量+站外流量,然后通过这个公式无限拆解流量来源,最终找…

    2023年 4月 11日
  • 日本电子钱包pay

    概况 LINE Pay于2014年12月推出,是日本流行的电子钱包,由当地超级社交应用LINE提供,类似于国内微信应用程序上的微信支付。 LINE Pay提供二维码收付款、线下购物…

    2023年 7月 21日
  • 12306出行防疫保

    掌上春城讯2022年春运将于1月17日正式启动。今天(1月15日)开始至1月17日,铁路春运火车票销售进入高峰期,期间,可购买1月29日(腊月二十七)至1月31日(除夕)的火车票。…

    2023年 1月 7日
  • 百度贴吧转型合伙人,找到变现金矿的人

    不久前,百度贴吧举办了一个贴吧合伙人沙龙,邀请了最具代表性和知名度的各个垂类企业,如包括汽车、旅游、教育、婚嫁、餐饮等各个行业领导者成为首批贴吧合伙人,授予各战略合作伙伴管理行业贴…

    2023年 9月 10日