blockchain_go/proofofwork.go

91 lines
1.4 KiB
Go
Raw Normal View History

2017-08-21 05:50:41 -05:00
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
2017-08-21 08:51:17 -05:00
"fmt"
2017-08-21 05:50:41 -05:00
"log"
"math"
"math/big"
)
var (
target big.Int
maxNonce = math.MaxInt64
)
const targetBits = 12
func setTarget() {
targetBytes := make([]byte, 32)
numOfZeros := targetBits / 4
targetBytes[numOfZeros-1] = 1
target.SetBytes(targetBytes)
}
func prepareData(block *Block, nonce int) []byte {
data := bytes.Join(
[][]byte{
block.PrevBlockHash,
block.Data,
intToHex(block.Timestamp),
intToHex(int64(targetBits)),
intToHex(int64(nonce)),
},
[]byte{},
)
return data
}
func intToHex(num int64) []byte {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
// Prove ...
func Prove(block *Block) (int, []byte) {
setTarget()
var hashInt big.Int
var hash [32]byte
nonce := 0
2017-08-21 08:51:17 -05:00
fmt.Printf("Mining the block containing \"%s\"\n", block.Data)
2017-08-21 05:50:41 -05:00
for nonce < maxNonce {
data := prepareData(block, nonce)
hash = sha256.Sum256(data)
2017-08-21 08:51:17 -05:00
fmt.Printf("\r%x", hash)
2017-08-21 05:50:41 -05:00
hashInt.SetBytes(hash[:])
if hashInt.Cmp(&target) == -1 {
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[:]
}
// ConfirmProof ..
func ConfirmProof(block *Block, nonce int) bool {
setTarget()
var hashInt big.Int
data := prepareData(block, nonce)
hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:])
confirmation := hashInt.Cmp(&target) == -1
return confirmation
}