当前位置: 首页 > news >正文

网站开发的理解/杭州百度推广代理商

网站开发的理解,杭州百度推广代理商,p2p网站功能,网站内部链接是怎么做的背景 目前区块链的各种教程很容易让人似懂非懂,真正想理解,不如直接敲一遍,把每一步的逻辑理解清楚才是最实在的。 环境 测试环境:Google Colab 1. 先定义好需要的工具有 #flask 提供了一个基本的Web功能 #requests 做接口测…

背景

  • 目前区块链的各种教程很容易让人似懂非懂,真正想理解,不如直接敲一遍,把每一步的逻辑理解清楚才是最实在的。

环境

  • 测试环境:Google Colab

1. 先定义好需要的工具有

#flask 提供了一个基本的Web功能
#requests 做接口测试可以用
!pip install Flask==0.12.2 requests==2.18.4
#API接口测试工具
!pip install cURL

2. 开始书写Blockchain类代码

  • 我们要明确blockchain它有的功能。
    • 首先在开始创建的时候,我们必须要有一个chain来完成链里面所有区块的记录。
    • 那么链里面的区块是怎么来的,当然是通过new_block这样的函数新建而来。
    • new_block这样的函数新建出来的区块需要包含哪些基本信息呢?
      • 实际上需要有的是index,timestamp,transcations,proof以及previous_hash这样的一些信息。
      • 但如果搭建过以太坊的就会很困惑,因为以太坊里的创始区块不止有这几个字段,这是因为它新增了很多和安全与应用相关的功能。
    • 那么transcations中记录了什么呢?
      • 它记录了一份包含着sender,recipientamount的字段。
      • 需要注意的是它记录的是一个交易的列表,而并非一次的交易,所以每一个区块都会有这个链过往交易的所有列表。
  • 这边我们列举一个区块
block = {'index': 1,'timestamp': 1506057125.900785,'transactions': [{'sender': "8527147fe1f5426f9dd545de4b27ee00",'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",'amount': 5,}],'proof': 324984774000,'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}
  • 直观的理解,indextimestamp以及transcations都好懂,但是proof的原理是什么呢?
    • 首先,对于区块链有基础了解,就很容易理解记账的人会获得奖励的概念,所以大家就会用自己的节点拼命的记账。
    • 而谁才能够算记账成功并且获得奖励呢,他们设计了一套机制。
    • 首先,给定一个整数x,其次是要求记账的节点生成另一个整数y,这个整数y会使得hash(x*y)的末尾出现k0。例如,当k=1时,就只需要hash(x*y)=xxx...xxx0即完成记账了。
    • 当然,如果要求的是开始的部分出现k个0,那么检查的时候就会更加方便,所以我们现在就可以为我们接下来的Blockchain提一个需求,即寻找数字p,要求哈希开头是4个0。

2.1. 定义基础的数据结构

class Blockchain(object):def __init__(self):self.chain = []self.current_transactions = []def new_block(self):# Creates a new Block and adds it to the chainpassdef new_transaction(self):# Adds a new transaction to the list of transactionspass@staticmethoddef hash(block):# Hashes a Blockpass@propertydef last_block(self):# Returns the last Block in the chainpass

2.2. 在区块中添加交易信息

class Blockchain(object):...def new_transaction(self, sender, recipient, amount):"""Creates a new transaction to go into the next mined Block:param sender: <str> Address of the Sender:param recipient: <str> Address of the Recipient:param amount: <int> Amount:return: <int> The index of the Block that will hold this transaction"""self.current_transactions.append({'sender': sender,'recipient': recipient,'amount': amount,})return self.last_block['index'] + 1
  • 注意,在列表中添加新交易之后,会返回该交易被加到的区块的索引,也就是指向下一个要挖的区块

2.3. 创世区块和工作量证明

  • 实例化Blockchain类之后,还需要新建一个创始区块。此外还要加入证明。
import hashlib
import json
from time import timeclass Blockchain(object):def __init__(self):self.current_transactions = []self.chain = []# Create the genesis blockself.new_block(previous_hash=1, proof=100)def new_block(self, proof, previous_hash=None):"""Create a new Block in the Blockchain:param proof: <int> The proof given by the Proof of Work algorithm:param previous_hash: (Optional) <str> Hash of previous Block:return: <dict> New Block"""block = {'index': len(self.chain) + 1,'timestamp': time(),'transactions': self.current_transactions,'proof': proof,'previous_hash': previous_hash or self.hash(self.chain[-1]),}# Reset the current list of transactionsself.current_transactions = []self.chain.append(block)return blockdef new_transaction(self, sender, recipient, amount):"""Creates a new transaction to go into the next mined Block:param sender: <str> Address of the Sender:param recipient: <str> Address of the Recipient:param amount: <int> Amount:return: <int> The index of the Block that will hold this transaction"""self.current_transactions.append({'sender': sender,'recipient': recipient,'amount': amount,})return self.last_block['index'] + 1@propertydef last_block(self):return self.chain[-1]@staticmethoddef hash(block):"""Creates a SHA-256 hash of a Block:param block: <dict> Block:return: <str>"""# We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashesblock_string = json.dumps(block, sort_keys=True).encode()return hashlib.sha256(block_string).hexdigest()

2.4. 完成工作量证明的书写

import hashlib
import jsonfrom time import time
from uuid import uuid4class Blockchain(object):...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 = 0while self.valid_proof(last_proof, proof) is False:proof += 1return proof@staticmethoddef 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"
http://www.lbrq.cn/news/1421353.html

相关文章:

  • BBS推广网站的步骤/百度总部投诉电话
  • 深圳网站 建设/seo全网优化指南
  • 网站建设客户分析调查表/驻马店百度seo
  • wordpress 主题 音乐/如何做好关键词的优化
  • 漳州公司建设网站/网站设计模板
  • 乾安网站建设公司电话/域名查询备案
  • 江西网站建设哪家好/b2b网站排名
  • 北京大型网站优化/线上营销推广渠道
  • 做网站买二手域名/谷歌商店下载官方正版
  • wordpress 终极优化/进行优化
  • 淮滨网站建设公司/温州seo招聘
  • 网站的例子/网站seo规划
  • 连云港网站建设哪家好/建立一个网站需要多少钱
  • 生物技术网站开发/seo外包方案
  • 响应式网站建设市场/爱站网长尾挖掘工具
  • 学校网站开发的背景/厦门seo总部电话
  • 台州优秀网站设计/谷歌浏览器搜索引擎入口
  • 可以做点赞的网站/推广运营平台
  • 基于php网站建设设计/windows优化大师
  • 县级政府网站建设方案/厦门seo推广优化
  • 网站顶部固定怎么做/杭州百度首页优化
  • 什么是分类信息网站营销/深圳网站设计
  • 设计师个人网站/磁力宝最佳搜索引擎入口
  • 免费推广网站搭建/企业查询官网
  • 泉州微信网站建设/全网营销推广
  • 天猫商城在线购物/汕头seo排名收费
  • 网站制作 合肥/营销型网站建设的价格
  • 哪里有网站建设电话/百度的主页
  • 网站制作怎么报价单/地推团队接单平台
  • 如何做专业的模板下载网站/做公司网站需要多少钱
  • 软件需求管理过程详解
  • 暴雨服务器:以定制化满足算力需求多样化
  • 【读论文】医疗AI大模型:百川开源Baichuan-M2
  • 18- 网络编程
  • C++第二十课:快递运费计算器 / 黑白配+石头剪刀布小游戏
  • 解决 MySQL 查询速度缓慢的问题