Optimizations
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
package chain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/sha3"
|
||||
"log"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
@@ -29,23 +27,25 @@ func (b *block) AddTransaction(t transaction) {
|
||||
func (b block) GenerateHash(difficulty int) []byte {
|
||||
found := false
|
||||
var compBytes []byte
|
||||
var compString string
|
||||
for i := 0; i < difficulty; i++ {
|
||||
compBytes = append(compBytes, []byte("0x00")...)
|
||||
}
|
||||
for i := 0; i < difficulty; i++ {
|
||||
compString += "0"
|
||||
}
|
||||
for !found {
|
||||
random := make([]byte, 64)
|
||||
rand.Read(random)
|
||||
b.Nonce = random
|
||||
var buf bytes.Buffer
|
||||
err := gob.NewEncoder(&buf).Encode(b.Nonce)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
hasher := sha3.New512()
|
||||
hasher.Write(b.ToBytes())
|
||||
hash := hasher.Sum(nil)
|
||||
fmt.Printf("\033[2K\r%x\n", hash)
|
||||
if checkAllZero(XorSlice(hash[:difficulty], compBytes)) {
|
||||
//if checkAllZero(XorSlice(hash[:difficulty], compBytes)) {
|
||||
hexstring := hex.EncodeToString(hash)
|
||||
fmt.Printf("%x\n", hash)
|
||||
if hexstring[:difficulty] == compString {
|
||||
return hash
|
||||
}
|
||||
}
|
||||
@@ -57,19 +57,19 @@ func (b block) ToBytes() []byte {
|
||||
for i := 0; i < len(b.Transactions); i++ {
|
||||
bytes = append(bytes, b.Transactions[i].ToBytes()...)
|
||||
}
|
||||
bytes = append(bytes, b.HashPrevious...)
|
||||
bytes = append(bytes, b.Nonce...)
|
||||
return bytes
|
||||
}
|
||||
|
||||
func XorSlice(a []byte, b []byte) []byte {
|
||||
for i, _ := range a {
|
||||
for i := range a {
|
||||
a[i] = a[i] ^ b[i]
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func checkAllZero(a []byte) bool {
|
||||
fmt.Printf("Checking if all zero: %x\n", a)
|
||||
for i, _ := range a {
|
||||
if a[i] != 0x00 {
|
||||
return false
|
||||
|
||||
@@ -14,11 +14,11 @@ type transaction struct {
|
||||
Src string
|
||||
Dst string
|
||||
Amount int
|
||||
Timestamp time.Time
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
func NewTransaction(source string, dst string, amount int) transaction {
|
||||
return transaction{Src: source, Dst: dst, Amount: amount, Timestamp: time.Now()}
|
||||
return transaction{Src: source, Dst: dst, Amount: amount, Timestamp: time.Now().Unix()}
|
||||
}
|
||||
|
||||
func (ta transaction) SignTransaction(privKey *rsa.PrivateKey, d int) ([]byte, error) {
|
||||
@@ -36,8 +36,13 @@ func (ta transaction) HashTransaction() []byte {
|
||||
func (ta transaction) ToBytes() []byte {
|
||||
bytes := []byte(ta.Src)
|
||||
bytes = append(bytes, []byte(ta.Dst)...)
|
||||
//fmt.Printf("%s", ta.Dst)
|
||||
bytes = append(bytes, byte(ta.Amount))
|
||||
bytes = append(bytes, []byte(ta.Timestamp.String())...)
|
||||
//bytes = append(bytes, []byte(ta.Timestamp))
|
||||
return bytes
|
||||
}
|
||||
|
||||
func (ta transaction) ToBytesSerialize() []byte {
|
||||
var buf bytes.Buffer
|
||||
gob.NewEncoder(&buf).Encode(ta)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user