Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func (b *Block) GenerateHash() {
- index := strconv.Itoa(b.Index)
- nonce := strconv.Itoa(b.Nonce)
- b.Hash = fmt.Sprintf("%x", sha256.Sum256([]byte(index+b.PreviousHash+b.Data+b.Timestamp.String()+nonce)))
- }
- func (b *Block) Mine() {
- prefix := getPrefix(b.Difficulty)
- for {
- b.GenerateHash()
- if strings.HasPrefix(b.Hash, prefix) {
- break
- } else {
- b.Nonce = b.Nonce + 1
- b.GenerateHash()
- }
- }
- }
- func getPrefix(length int) string {
- letterBytes := "0"
- b := make([]byte, length)
- for i := range b {
- b[i] = letterBytes[0]
- }
- return string(b)
- }
Add Comment
Please, Sign In to add comment