Simplify 'target'
This commit is contained in:
parent
f94bbb4451
commit
d379fdeae2
|
@ -12,22 +12,18 @@ var (
|
||||||
maxNonce = math.MaxInt64
|
maxNonce = math.MaxInt64
|
||||||
)
|
)
|
||||||
|
|
||||||
const targetBits = 12
|
const targetBits = 24 // 3 bytes
|
||||||
|
|
||||||
// ProofOfWork represents a proof-of-work
|
// ProofOfWork represents a proof-of-work
|
||||||
type ProofOfWork struct {
|
type ProofOfWork struct {
|
||||||
block *Block
|
block *Block
|
||||||
target big.Int
|
target *big.Int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProofOfWork builds and returns a ProofOfWork
|
// NewProofOfWork builds and returns a ProofOfWork
|
||||||
func NewProofOfWork(b *Block) *ProofOfWork {
|
func NewProofOfWork(b *Block) *ProofOfWork {
|
||||||
targetBytes := make([]byte, 32)
|
target := big.NewInt(1)
|
||||||
target := big.Int{}
|
target.Lsh(target, uint(256-targetBits))
|
||||||
|
|
||||||
numOfZeros := targetBits / 4
|
|
||||||
targetBytes[numOfZeros-1] = 1
|
|
||||||
target.SetBytes(targetBytes)
|
|
||||||
|
|
||||||
pow := &ProofOfWork{b, target}
|
pow := &ProofOfWork{b, target}
|
||||||
|
|
||||||
|
@ -63,7 +59,7 @@ func (pow *ProofOfWork) Run() (int, []byte) {
|
||||||
fmt.Printf("\r%x", hash)
|
fmt.Printf("\r%x", hash)
|
||||||
hashInt.SetBytes(hash[:])
|
hashInt.SetBytes(hash[:])
|
||||||
|
|
||||||
if hashInt.Cmp(&pow.target) == -1 {
|
if hashInt.Cmp(pow.target) == -1 {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
nonce++
|
nonce++
|
||||||
|
@ -82,7 +78,7 @@ func (pow *ProofOfWork) ConfirmProof() bool {
|
||||||
hash := sha256.Sum256(data)
|
hash := sha256.Sum256(data)
|
||||||
hashInt.SetBytes(hash[:])
|
hashInt.SetBytes(hash[:])
|
||||||
|
|
||||||
confirmation := hashInt.Cmp(&pow.target) == -1
|
confirmation := hashInt.Cmp(pow.target) == -1
|
||||||
|
|
||||||
return confirmation
|
return confirmation
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue