Implement Proof-of-Work

This commit is contained in:
Ivan Kuznetsov 2017-08-21 17:50:41 +07:00
parent cc4a2d8b43
commit 0f23d64aa0
3 changed files with 94 additions and 13 deletions

View File

@ -1,9 +1,6 @@
package main package main
import ( import (
"bytes"
"crypto/sha256"
"strconv"
"time" "time"
) )
@ -13,21 +10,21 @@ type Block struct {
Data []byte Data []byte
PrevBlockHash []byte PrevBlockHash []byte
Hash []byte Hash []byte
Nonce int
} }
// SetHash calculates and sets block hash // Prove obtains proof of work
func (b *Block) SetHash() { func (b *Block) Prove() {
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10)) nonce, hash := Prove(b)
headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
hash := sha256.Sum256(headers)
b.Hash = hash[:] 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{}} block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}, 0}
block.SetHash() block.Prove()
return block return block
} }

View File

@ -1,8 +1,6 @@
package main package main
import ( import "fmt"
"fmt"
)
func main() { func main() {
bc := NewBlockchain() bc := NewBlockchain()

86
proofofwork.go Normal file
View File

@ -0,0 +1,86 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"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
for nonce < maxNonce {
data := prepareData(block, nonce)
hash = sha256.Sum256(data)
hashInt.SetBytes(hash[:])
if hashInt.Cmp(&target) == -1 {
break
} else {
nonce++
}
}
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
}