-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pibigstar
committed
Oct 4, 2018
1 parent
4e50a6d
commit 9e7cf5c
Showing
6 changed files
with
174 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
.idea/workspace.xml |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package core | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"math" | ||
"math/big" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
maxNonce = math.MaxInt64 | ||
) | ||
// 困难度,数越大越困难 | ||
const targetBits = 8 | ||
|
||
type ProofOfWork struct { | ||
block *Block | ||
target *big.Int | ||
} | ||
// 创建一个新的工作量证明 | ||
func NewProofOfWork(block *Block) *ProofOfWork { | ||
target := big.NewInt(1) | ||
// 左移 | ||
target.Lsh(target, uint(256-targetBits)) | ||
pow := &ProofOfWork{block, target} | ||
return pow | ||
} | ||
// 准备数据 | ||
func (pow *ProofOfWork) prepareData(nonce int) []byte { | ||
data := bytes.Join([][]byte{ | ||
pow.block.PrevBlockHash, | ||
pow.block.Data, | ||
IntToHex(pow.block.Timestamp), | ||
IntToHex(targetBits), | ||
IntToHex(int64(nonce)), | ||
}, []byte{}) | ||
return data | ||
} | ||
// 挖矿 | ||
func (pow *ProofOfWork) Run() (int, []byte) { | ||
var hashInt big.Int | ||
var hash [32]byte | ||
nonce := 0 | ||
for nonce < maxNonce { | ||
data := pow.prepareData(nonce) | ||
hash = sha256.Sum256(data) | ||
fmt.Printf("\r%x",hash) | ||
hashInt.SetBytes(hash[:]) | ||
if hashInt.Cmp(pow.target) == -1 { | ||
break | ||
}else { | ||
nonce++ | ||
} | ||
} | ||
return nonce,hash[:] | ||
} | ||
|
||
// 验证 | ||
func (pow *ProofOfWork)Validate() bool { | ||
var hashInt big.Int | ||
data := pow.prepareData(pow.block.Nonce) | ||
hash := sha256.Sum256(data) | ||
hashInt.SetBytes(hash[:]) | ||
isValidate := hashInt.Cmp(pow.target) == -1 | ||
return isValidate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"log" | ||
) | ||
|
||
func IntToHex(num int64) []byte { | ||
buff := new(bytes.Buffer) | ||
err := binary.Write(buff, binary.BigEndian, num) | ||
if err!=nil { | ||
log.Panic("failed Int to hex:",err) | ||
} | ||
return buff.Bytes() | ||
} |