分享
  1. 首页
  2. 文章

区块链POW证明代码实现demo

若与 · · 1233 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。


这里强调一下区块链的协议分层

  • 应用层
  • 合约层
  • 激励机制
  • 共识层
  • 网络层
  • 数据层

上 一篇主要实现了区块链的 数据层,数据层主要使用的技术就是对数据的校验,求hash。

这里介绍工作量证明POW, POW是属于共识机制的内容。

PoW机制中根据矿工的工作量来执行货币的分配和记账权的确定。算力竞争的胜者将获得相应区块记账权和比特币奖励。因此,矿机芯片的算力越高,挖矿的时间更长,就可以获得更多的数字货币。

优点:

算法简单,容易实现;节点间无需交换额外的信息即可达成共识;破坏系统需要投入极大的成本。

缺点:

浪费能源;区块的确认时间难以缩短;新的区块链必须找到一种不同的散列算法,否则就会面临比特币的算力攻击;容易产生分叉,需要等待多个确认;永远没有最终性,需要检查点机制来弥补最终性。

目前基于PoW共识机制的数字货币有很多,比特币、莱特币、狗狗币、达士币、门罗币等初期的数字货币大多都是PoW共识机制。

其他的共识机制还有

PoS(Proof of Stake)
DPOS(Delegated Proof-of-Stake)
DAG(Directed acyclic graph)
PBFT(Practical Byzantine Fault Tolerance)
Pool验证池
dBFT(delegated BFT)
PoA(Proof-of-Authority)
RPCA(Ripple Protocol consensus algorithm)
Hcash——PoW+PoS共识机制

这些共识机制,后面有时间会补充上的,今天主要介绍POW

pow很简单,原理就是 利用计算力,在选择一个nonce的值结合区块的数据算出hash,使得hash的前面多少位都是0.

nonce是一个用来找到满足条件的hash值的数字,nonce值一直迭代,直到hash值有效为止。在我们案例中一个有效的hash值是最少有4个前导0。找到nonce值以满足合适条件的hash值的过程就叫做挖矿。

下面给出代码:

golang版

package main
import (
 "bytes"
 "crypto/sha256"
 "fmt"
 "math"
 "math/big"
)
// 前导0,难度
const targetBits = 8
type ProofOfWork struct {
 block *Block
 targetBit *big.Int
}
func NewProofOfWork(block *Block) *ProofOfWork {
 // 设置64位全1
 var IntTarget = big.NewInt(1)
 //00000000000000000000000000001
 //10000000000000000000000000000
 //00000000000100000000000000000
 //0000001
 // 右移 targetBits位
 IntTarget.Lsh(IntTarget, uint(256 - targetBits))
 return &ProofOfWork{block:block, targetBit:IntTarget}
}
func (pow *ProofOfWork)PrepareRawData(nonce int64)[]byte {
 block := pow.block
 tmp := [][]byte{
 Int2Byte(block.Version),
 block.PrevBlockHash,
 Int2Byte(block.TimeStamp),
 block.MerkeRoot,
 Int2Byte(nonce),
 Int2Byte(targetBits),
 block.Data}
 data := bytes.Join(tmp, []byte{})
 return data
}
func (pow *ProofOfWork)Run() (int64, []byte) {
 var nonce int64
 var hash [32]byte
 var HashInt big.Int
 fmt.Printf("target hash:", pow.targetBit.Bytes())
 for nonce < math.MaxInt64 {
 data := pow.PrepareRawData(nonce)
 hash = sha256.Sum256(data)
 HashInt.SetBytes(hash[:])
 //fmt.Println(nonce)
 // 这里用于 判断算出的hash值(int)只要比最大的IntTarget小就是正确的。
 if HashInt.Cmp(pow.targetBit) == -1 {
 fmt.Printf("Found Hash: %x\n", hash)
 break
 } else {
 nonce++
 }
 }
 return nonce, hash[:]
}
// 对block的数据校验
func (pow *ProofOfWork)IsVaild() bool {
 data := pow.PrepareRawData(pow.block.Nonce)
 hash := sha256.Sum256(data)
 var IntHash big.Int
 IntHash.SetBytes(hash[:])
 return IntHash.Cmp(pow.targetBit) == -1
}

python版

function isValidHashDifficulty(hash, difficulty) {
 for (var i = 0, b = hash.length; i < b; i ++) {
 if (hash[i] !== '0') {
 break;
 }
 }
 return i >= difficulty;
}
import hashlib
"""
工作量证明
"""
class ProofofWork():
 """
 pow
 """
 def __init__(self, block):
 self.block = block
 def mine(self):
 """
 挖矿函数
 :return:
 """
 i = 0
 prefix = '0000'
 while True:
 nonce = str(i)
 message = hashlib.sha256()
 message.update(str(self.block.data).encode('utf-8'))
 message.update(nonce.encode("utf-8"))
 digest = message.hexdigest()
 if digest.startswith(prefix):
 return nonce, digest
 i += 1

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:若与

查看原文:区块链POW证明代码实现demo

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
1233 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏