区块链V2版本实现之四(V2版本完结)
部分代码(proofofwork.go文件中使用Bits推导难度值):
1 const Bits = 20 2 3 func NewProofOfWork(block *Block) *ProofOfWork { 4 pow := ProofOfWork{ 5 block: block, 6 } 7 8 //写难度值,难度值应该是推导出来的,但是我们为了简化,把难度值先写成固定的,一切完成之后,再去推导 9 //0000100000000000000000000000000000000000000000000000000000000000 10 11 ////固定的难度值 12 ////16制格式的字符串 13 //targetStr := "0001000000000000000000000000000000000000000000000000000000000000" 14 //var bigIntTmp big.Int 15 //bigIntTmp.SetString(targetStr,16) 16 // 17 //pow.target = &bigIntTmp 18 19 //难度值是有程序推导出来的,例如推导出3个0的难度值 20 // 0000100000000000000000000000000000000000000000000000000000000000 21 //初始化 22 // 0000000000000000000000000000000000000000000000000000000000000001 23 //向左移动,256位 24 //1 0000000000000000000000000000000000000000000000000000000000000000 25 //向右移动,4次, 26 // 0000100000000000000000000000000000000000000000000000000000000000 27 28 bigIntTmp := big.NewInt(1) 29 30 bigIntTmp.Lsh(bigIntTmp,256-Bits) 31 32 pow.target = bigIntTmp 33 34 return &pow 35 }
显示效果: