Clean up and refactor proofofwork.go

This commit is contained in:
Ivan Kuznetsov 2017-08-21 21:06:52 +07:00
parent 94f3654c8b
commit 35449eb8a6
3 changed files with 53 additions and 40 deletions

View File

@ -13,18 +13,15 @@ type Block struct {
Nonce int Nonce int
} }
// Prove obtains proof of work
func (b *Block) Prove() {
nonce, hash := Prove(b)
b.Hash = hash[:]
b.Nonce = nonce
}
// NewBlock creates and returns Block // NewBlock creates and returns Block
func NewBlock(data string, prevBlockHash []byte) *Block { func NewBlock(data string, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}, 0} block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}, 0}
block.Prove() pow := NewProofOfWork(block)
nonce, hash := pow.Run()
block.Hash = hash[:]
block.Nonce = nonce
return block return block
} }

View File

@ -3,35 +3,45 @@ package main
import ( import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"encoding/binary"
"fmt" "fmt"
"log"
"math" "math"
"math/big" "math/big"
) )
var ( var (
target big.Int
maxNonce = math.MaxInt64 maxNonce = math.MaxInt64
) )
const targetBits = 12 const targetBits = 12
func setTarget() { // ProofOfWork represents a proof-of-work
type ProofOfWork struct {
block *Block
target big.Int
}
// NewProofOfWork builds and returns a ProofOfWork
func NewProofOfWork(b *Block) *ProofOfWork {
targetBytes := make([]byte, 32) targetBytes := make([]byte, 32)
target := big.Int{}
numOfZeros := targetBits / 4 numOfZeros := targetBits / 4
targetBytes[numOfZeros-1] = 1 targetBytes[numOfZeros-1] = 1
target.SetBytes(targetBytes) target.SetBytes(targetBytes)
pow := &ProofOfWork{b, target}
return pow
} }
func prepareData(block *Block, nonce int) []byte { func (pow *ProofOfWork) prepareData(nonce int) []byte {
data := bytes.Join( data := bytes.Join(
[][]byte{ [][]byte{
block.PrevBlockHash, pow.block.PrevBlockHash,
block.Data, pow.block.Data,
intToHex(block.Timestamp), IntToHex(pow.block.Timestamp),
intToHex(int64(targetBits)), IntToHex(int64(targetBits)),
intToHex(int64(nonce)), IntToHex(int64(nonce)),
}, },
[]byte{}, []byte{},
) )
@ -39,32 +49,21 @@ func prepareData(block *Block, nonce int) []byte {
return data return data
} }
func intToHex(num int64) []byte { // Run performs a proof-of-work
buff := new(bytes.Buffer) func (pow *ProofOfWork) Run() (int, []byte) {
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 hashInt big.Int
var hash [32]byte var hash [32]byte
nonce := 0 nonce := 0
fmt.Printf("Mining the block containing \"%s\"\n", block.Data) fmt.Printf("Mining the block containing \"%s\"\n", pow.block.Data)
for nonce < maxNonce { for nonce < maxNonce {
data := prepareData(block, nonce) data := pow.prepareData(nonce)
hash = sha256.Sum256(data) hash = sha256.Sum256(data)
fmt.Printf("\r%x", hash) fmt.Printf("\r%x", hash)
hashInt.SetBytes(hash[:]) hashInt.SetBytes(hash[:])
if hashInt.Cmp(&target) == -1 { if hashInt.Cmp(&pow.target) == -1 {
break break
} else { } else {
nonce++ nonce++
@ -75,16 +74,15 @@ func Prove(block *Block) (int, []byte) {
return nonce, hash[:] return nonce, hash[:]
} }
// ConfirmProof .. // ConfirmProof confirms that the proof is correct
func ConfirmProof(block *Block, nonce int) bool { func (pow *ProofOfWork) ConfirmProof(nonce int) bool {
setTarget()
var hashInt big.Int var hashInt big.Int
data := prepareData(block, nonce) data := pow.prepareData(nonce)
hash := sha256.Sum256(data) hash := sha256.Sum256(data)
hashInt.SetBytes(hash[:]) hashInt.SetBytes(hash[:])
confirmation := hashInt.Cmp(&target) == -1 confirmation := hashInt.Cmp(&pow.target) == -1
return confirmation return confirmation
} }

18
utils.go Normal file
View File

@ -0,0 +1,18 @@
package main
import (
"bytes"
"encoding/binary"
"log"
)
// IntToHex converts an int64 to a byte array
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()
}