blockchain_go/proofofwork.go

87 lines
1.4 KiB
Go
Raw Normal View History

2017-08-21 05:50:41 -05:00
package main
import (
"bytes"
"crypto/sha256"
2017-08-21 08:51:17 -05:00
"fmt"
2017-08-21 05:50:41 -05:00
"math"
"math/big"
)
var (
maxNonce = math.MaxInt64
)
2017-09-19 03:03:30 -05:00
const targetBits = 16
2017-08-21 05:50:41 -05:00
2017-08-21 09:06:52 -05:00
// ProofOfWork represents a proof-of-work
type ProofOfWork struct {
block *Block
2017-08-22 05:26:12 -05:00
target *big.Int
2017-08-21 09:06:52 -05:00
}
// NewProofOfWork builds and returns a ProofOfWork
func NewProofOfWork(b *Block) *ProofOfWork {
2017-08-22 05:26:12 -05:00
target := big.NewInt(1)
target.Lsh(target, uint(256-targetBits))
2017-08-21 09:06:52 -05:00
pow := &ProofOfWork{b, target}
return pow
2017-08-21 05:50:41 -05:00
}
2017-08-21 09:06:52 -05:00
func (pow *ProofOfWork) prepareData(nonce int) []byte {
2017-08-21 05:50:41 -05:00
data := bytes.Join(
[][]byte{
2017-08-21 09:06:52 -05:00
pow.block.PrevBlockHash,
2017-09-02 21:56:14 -05:00
pow.block.HashTransactions(),
2017-08-21 09:06:52 -05:00
IntToHex(pow.block.Timestamp),
IntToHex(int64(targetBits)),
IntToHex(int64(nonce)),
2017-08-21 05:50:41 -05:00
},
[]byte{},
)
return data
}
2017-08-21 09:06:52 -05:00
// Run performs a proof-of-work
func (pow *ProofOfWork) Run() (int, []byte) {
2017-08-21 05:50:41 -05:00
var hashInt big.Int
var hash [32]byte
nonce := 0
2017-09-02 21:56:14 -05:00
fmt.Printf("Mining a new block")
2017-08-21 05:50:41 -05:00
for nonce < maxNonce {
2017-08-21 09:06:52 -05:00
data := pow.prepareData(nonce)
2017-08-21 05:50:41 -05:00
hash = sha256.Sum256(data)
if math.Remainder(float64(nonce), 100000) == 0 {
fmt.Printf("\r%x", hash)
}
2017-08-21 05:50:41 -05:00
hashInt.SetBytes(hash[:])
2017-08-22 05:26:12 -05:00
if hashInt.Cmp(pow.target) == -1 {
2017-08-21 05:50:41 -05:00
break
} else {
nonce++
}
}
2017-08-21 08:51:17 -05:00
fmt.Print("\n\n")
2017-08-21 05:50:41 -05:00
return nonce, hash[:]
}
// Validate validates block's PoW
func (pow *ProofOfWork) Validate() bool {
2017-08-21 05:50:41 -05:00
var hashInt big.Int
data := pow.prepareData(pow.block.Nonce)
2017-08-21 05:50:41 -05:00
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])
isValid := hashInt.Cmp(pow.target) == -1
2017-08-21 05:50:41 -05:00
return isValid
2017-08-21 05:50:41 -05:00
}