2017-09-22 16:48:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
|
|
|
const proofTargetBits = 16
|
|
|
|
|
|
|
|
type ProofOfWork struct {
|
|
|
|
block *Block
|
|
|
|
target *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProofOfWork(b *Block) *ProofOfWork {
|
|
|
|
target := big.NewInt(1)
|
|
|
|
target.Lsh(target, uint(256-proofTargetBits))
|
|
|
|
|
|
|
|
pow := &ProofOfWork{b, target}
|
|
|
|
|
|
|
|
return pow
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pow *ProofOfWork) prepareData(nonce int) []byte {
|
|
|
|
data := bytes.Join(
|
|
|
|
[][]byte{
|
|
|
|
pow.block.PrevBlockHash,
|
2017-09-22 19:22:11 +00:00
|
|
|
pow.block.HashTransactions(),
|
2017-09-22 16:48:05 +00:00
|
|
|
IntToHex(pow.block.Timestamp),
|
|
|
|
IntToHex(int64(proofTargetBits)),
|
|
|
|
IntToHex(int64(nonce)),
|
|
|
|
},
|
|
|
|
[]byte{},
|
|
|
|
)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pow *ProofOfWork) Run() (int, []byte) {
|
|
|
|
var hashInt big.Int
|
|
|
|
var hash [32]byte
|
|
|
|
nonce := 0
|
|
|
|
|
2017-09-22 19:22:11 +00:00
|
|
|
fmt.Printf("Mining a new block")
|
2017-09-22 16:48:05 +00:00
|
|
|
for nonce < maxNonce {
|
|
|
|
data := pow.prepareData(nonce)
|
|
|
|
hash = sha256.Sum256(data)
|
|
|
|
fmt.Printf("\r%x", hash)
|
|
|
|
hashInt.SetBytes(hash[:])
|
|
|
|
|
|
|
|
if hashInt.Cmp(pow.target) == -1 {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
nonce++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Print("\n\n")
|
|
|
|
return nonce, hash[:]
|
|
|
|
}
|
2017-09-22 17:38:21 +00:00
|
|
|
|
|
|
|
func (pow *ProofOfWork) Validate() bool {
|
|
|
|
var hashInt big.Int
|
|
|
|
|
|
|
|
data := pow.prepareData(pow.block.Nonce)
|
|
|
|
hash := sha256.Sum256(data)
|
|
|
|
hashInt.SetBytes(hash[:])
|
|
|
|
|
|
|
|
isValid := hashInt.Cmp(pow.target) == -1
|
|
|
|
return isValid
|
|
|
|
}
|