Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

区块链POW证明代码实现demo

朋友一起走
77 0 0
这里介绍工作量证明POW, POW是属于共识机制的内容。8 y- T  ?5 ]- X
PoW机制中根据矿工的工作量来执行货币的分配和记账权的确定。算力竞争的胜者将获得相应区块记账权和比特币奖励。因此,矿机芯片的算力越高,挖矿的时间更长,就可以获得更多的数字货币。
# r% Z2 E, f  g4 i: Z9 ~8 L优点:) I. ~% i8 |& Y
算法简单,容易实现;节点间无需交换额外的信息即可达成共识;破坏系统需要投入极大的成本。" @$ G# P4 B2 F; n
缺点:
( a3 p6 Q% R* p6 L' ^" o浪费能源;区块的确认时间难以缩短;新的区块链必须找到一种不同的散列算法,否则就会面临比特币的算力攻击;容易产生分叉,需要等待多个确认;永远没有最终性,需要检查点机制来弥补最终性。! Z& x+ y3 Y! e& M
目前基于PoW共识机制的数字货币有很多,比特币、莱特币、狗狗币、达士币、门罗币等初期的数字货币大多都是PoW共识机制。5 |' Z8 V# k6 }  ~5 k7 ?" o6 E. d
其他的共识机制还有9 K7 O& C* r2 _4 ]8 E" s7 u6 u
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共识机制
3 @$ P: z4 f. W# ]" V4 m% I

: V6 i3 \& E; E7 k; k这些共识机制,后面有时间会补充上的,今天主要介绍POW" [( B  k- B! V
pow很简单,原理就是 利用计算力,在选择一个nonce的值结合区块的数据算出hash,使得hash的前面多少位都是0.
( a$ M9 F+ x  \( O" g3 \! unonce是一个用来找到满足条件的hash值的数字,nonce值一直迭代,直到hash值有效为止。在我们案例中一个有效的hash值是最少有4个前导0。找到nonce值以满足合适条件的hash值的过程就叫做挖矿。( `4 q8 W* ]! o# a# I9 t' E
下面给出代码:/ j9 Z7 p4 _0 L, ^* w
  1. golang版* t; V. w( ]5 `0 G* s
  2. package mainimport (    "bytes"
    # T5 d1 j1 b# e5 h: @7 ~
  3.     "crypto/sha256"3 n1 C5 |8 N- D- C: s$ l7 J. Q; X
  4.     "fmt"
    ! C( ]/ \) w" L
  5.     "math"
    ) w% [% p% u" @( J
  6.     "math/big")// 前导0,难度const targetBits  = 8type ProofOfWork struct {0 S) p0 Y/ l: t
  7.     block *Block
    9 \4 g- c& Q; D# e0 K& m
  8.     targetBit *big.Int
    % _) w: k- V3 R+ M9 O, T6 J6 ?
  9. }func NewProofOfWork(block *Block) *ProofOfWork  {    // 设置64位全1
    2 J; q2 i- @5 \) |$ k9 r- Z
  10.     var IntTarget = big.NewInt(1)    //00000000000000000000000000001. @/ E0 {5 ~1 x
  11.     //10000000000000000000000000000
    & o+ h- W! L1 o3 [( I
  12.     //000000000001000000000000000009 ~9 Q' b  g+ i" j* B: P1 x) b
  13.     //00000015 `3 c: T9 C" a
  14.     // 右移 targetBits位
    6 R) M/ B( x' y7 x0 a2 u8 |3 p
  15.     IntTarget.Lsh(IntTarget, uint(256 - targetBits))    return &ProofOfWork{block:block, targetBit:IntTarget}. q5 n* l: x  ^: H; j( M1 ^# K9 @
  16. }func (pow *ProofOfWork)PrepareRawData(nonce int64)[]byte  {* A7 C) f7 f3 O0 V2 i8 O6 W* R
  17.     block := pow.block
    / ]3 }$ }+ ~) [# G
  18.     tmp := [][]byte{/ m6 s% O+ s6 F, |! \3 ^
  19.         Int2Byte(block.Version),9 x; ?! F. v; N+ P- d
  20.         block.PrevBlockHash,
    - `5 i: n7 L/ q% D1 K: V& }
  21.         Int2Byte(block.TimeStamp),
    ' a9 U$ t8 z6 N
  22.         block.MerkeRoot,9 ^- I% y& c, s. X  \3 s# i
  23.         Int2Byte(nonce),
    # b$ w. B8 s. y5 ]  P: H6 N
  24.         Int2Byte(targetBits),# ^$ Q- O  S0 C( I& b7 o( G
  25.         block.Data}
    3 i2 ]3 f+ Q8 f* G
  26.     data := bytes.Join(tmp, []byte{})    return data4 H- E) R) o7 A. t
  27. }func (pow *ProofOfWork)Run() (int64, []byte) {    var nonce int64: u0 x. k& I2 k5 A$ d% _" ^
  28.     var hash [32]byte( O& G, [& C& ?$ d. {4 ?) q
  29.     var HashInt big.Int3 Q( v% S, m5 z8 |/ a/ _: e# u$ ^- |
  30.     fmt.Printf("target hash:", pow.targetBit.Bytes())    for nonce
    + M, s) e& c: d, u" h
  31. python版
    ! K# O* S) R! [" c# ^
  32. function isValidHashDifficulty(hash, difficulty) {  for (var i = 0, b = hash.length; i = difficulty;# s, a' m1 D$ t% L' v5 g
  33. }import hashlib"""$ P# {8 X" X9 ^3 N9 X
  34. 工作量证明
    6 F9 ]1 y0 g/ o
  35. """class ProofofWork():! a: G; O! Q- v8 B* Y$ T8 F
  36.     """, p4 `6 A: p2 n6 g& F
  37.     pow' |% @! h7 F: i7 N
  38.     """
    8 r; P( J* x! x5 n5 I
  39.     def __init__(self, block):( w$ C, v$ H0 w! C; l
  40.         self.block = block    def mine(self):
      K9 F" h# z) \% c7 w* o
  41.         """) z# l' O* _! O* b  X8 D1 }
  42.         挖矿函数
    # H) O" [1 S! x7 f  U) t
  43.         :return:2 @  T9 i) p9 z3 ~5 \! k' k
  44.         """' i# t/ W& h1 G$ P. @- x" B
  45.         i = 0- z( ]3 ~$ |( t# e3 F: V
  46.         prefix = '0000'
    . P% o; W5 C/ g$ s/ `- y' @# ]
  47.         while True:
    ! r- P/ K; y! F  c9 [9 t
  48.             nonce = str(i)$ N# Q8 _5 g  _1 i, L& Z& D4 X6 ~
  49.             message = hashlib.sha256()5 W3 g7 w5 R) \  U) P
  50.             message.update(str(self.block.data).encode('utf-8'))$ G8 L' e% P7 x" I! s  O. F/ O
  51.             message.update(nonce.encode("utf-8"))3 N5 e) q1 W% }5 z4 S4 Q
  52.             digest = message.hexdigest()            if digest.startswith(prefix):                return nonce, digest* A6 \( i9 t8 s: B2 o2 T7 C
  53.             i += 1
    0 b' i3 V! j# J1 ?7 j
复制代码
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朋友一起走 初中生
  • 粉丝

    0

  • 关注

    0

  • 主题

    16