Optimizations

This commit is contained in:
2022-04-22 18:11:33 +02:00
parent e779d8d5dc
commit 19347800c8
5 changed files with 39 additions and 23 deletions

View File

@@ -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

View File

@@ -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()
}

View File

@@ -16,7 +16,7 @@ type Address struct {
}
func GenerateAddress() *Address {
key, err := rsa.GenerateKey(rand.Reader, 1024)
key, err := rsa.GenerateKey(rand.Reader, 512)
if err != nil {
log.Fatal(err)

View File

@@ -2,7 +2,6 @@ package main
import (
"bufio"
"encoding/hex"
"fmt"
"monoblock/chain"
"monoblock/keymgmt"
@@ -12,11 +11,6 @@ import (
const AMOUNT int = 100
func main() {
by1, _ := hex.DecodeString("0001020304")
by2, _ := hex.DecodeString("0001020304")
fmt.Printf("%x", chain.XorSlice(by1, by2))
addr1 := keymgmt.GenerateAddress()
addr2 := keymgmt.GenerateAddress()
addr3 := keymgmt.GenerateAddress()

17
monoblock_test.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import (
"fmt"
"monoblock/chain"
"monoblock/keymgmt"
"testing"
)
func TestGenerateAddress(t *testing.T) {
addr1 := keymgmt.GenerateAddress()
addr2 := keymgmt.GenerateAddress()
ta1 := chain.NewTransaction(addr1.GetAddress(), addr2.GetAddress(), 100)
fmt.Printf("%x\n", ta1.ToBytes())
fmt.Printf("%x\n", ta1.ToBytesSerialize())
}