PoW机制中根据矿工的工作量来执行货币的分配和记账权的确定。算力竞争的胜者将获得相应区块记账权和比特币奖励。因此,矿机芯片的算力越高,挖矿的时间更长,就可以获得更多的数字货币。
优点:" i- c* u+ X% n
算法简单,容易实现;节点间无需交换额外的信息即可达成共识;破坏系统需要投入极大的成本。$ C6 r1 Q6 ^6 { Q! l, i4 G
缺点:7 }. X8 ^+ c* w0 N& \+ d6 ~; n
浪费能源;区块的确认时间难以缩短;新的区块链必须找到一种不同的散列算法,否则就会面临比特币的算力攻击;容易产生分叉,需要等待多个确认;永远没有最终性,需要检查点机制来弥补最终性。
目前基于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.* ^" d- j2 F' P8 o8 p# z
nonce是一个用来找到满足条件的hash值的数字,nonce值一直迭代,直到hash值有效为止。在我们案例中一个有效的hash值是最少有4个前导0。找到nonce值以满足合适条件的hash值的过程就叫做挖矿。 x; n; T: y5 _% w5 Y
下面给出代码:
- golang版
- package mainimport ( "bytes"/ I% C' C) \: U* i7 R
- "crypto/sha256" i3 m1 B7 b1 l% ]
- "fmt"3 m) e2 h7 X7 T, Y% l: |
- "math"; M$ }- [! D9 G1 X
- "math/big")// 前导0,难度const targetBits = 8type ProofOfWork struct {, X' n# ?7 S/ \3 y, T0 B
- block *Block" _9 P6 q. W. ?( s: d
- targetBit *big.Int
- }func NewProofOfWork(block *Block) *ProofOfWork { // 设置64位全18 k( M( ]2 X; v( S* T
- var IntTarget = big.NewInt(1) //00000000000000000000000000001
- //10000000000000000000000000000
- //000000000001000000000000000008 h. c$ d( P4 q. ?' S; d
- //0000001. {- U6 T' Z3 b+ \8 W
- // 右移 targetBits位3 n# I) y, |( Z; [, C
- IntTarget.Lsh(IntTarget, uint(256 - targetBits)) return &ProofOfWork{block:block, targetBit:IntTarget} N, }5 f' ~+ R: Q' K" a( ^
- }func (pow *ProofOfWork)PrepareRawData(nonce int64)[]byte {
- block := pow.block
- tmp := [][]byte{
- Int2Byte(block.Version),
- block.PrevBlockHash,
- Int2Byte(block.TimeStamp),$ M. U! I m; a) S3 e
- block.MerkeRoot,- C y% _# T Q( h5 q1 F
- Int2Byte(nonce),$ N, z e7 u. F5 b3 V4 O, H% f
- Int2Byte(targetBits),
- block.Data}9 H6 M8 E7 c- r" e- X, |* t6 u) R
- data := bytes.Join(tmp, []byte{}) return data
- }func (pow *ProofOfWork)Run() (int64, []byte) { var nonce int64
- var hash [32]byte
- var HashInt big.Int. e3 ?- x" u! |6 S8 `) {1 l
- fmt.Printf("target hash:", pow.targetBit.Bytes()) for nonce
- python版
- function isValidHashDifficulty(hash, difficulty) { for (var i = 0, b = hash.length; i = difficulty;$ I6 K! K6 x9 ~
- }import hashlib"""3 @) I9 b8 C6 l5 T' f0 W8 R
- 工作量证明
- """class ProofofWork():
- """- @% v0 G2 K) k2 _+ h
- pow
- """0 i, M9 u; N5 f4 ^
- def __init__(self, block):1 ?$ K& |3 I7 A2 |9 Y* q( B* r
- self.block = block def mine(self):. z2 {5 U* J1 n; K0 f j
- """- V$ R7 k/ c/ Q6 I6 T! r
- 挖矿函数" ~6 I/ |. m. Q
- :return:7 e! }5 b% G3 W5 I
- """
- i = 0
- prefix = '0000'
- while True:4 W2 x, F( u1 K ]& m, y
- nonce = str(i)& X2 ~" D, j; Q9 c9 h8 \1 A
- 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 += 16 n0 l* \% O; c