From 19347800c8cf8da680d1db58b0c4d87019ee96c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Kl=C3=B6ck?= Date: Fri, 22 Apr 2022 18:11:33 +0200 Subject: [PATCH] Optimizations --- chain/block.go | 24 ++++++++++++------------ chain/transaction.go | 13 +++++++++---- keymgmt/keymgmt.go | 2 +- main.go | 6 ------ monoblock_test.go | 17 +++++++++++++++++ 5 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 monoblock_test.go diff --git a/chain/block.go b/chain/block.go index 6f83730..7d01751 100644 --- a/chain/block.go +++ b/chain/block.go @@ -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 diff --git a/chain/transaction.go b/chain/transaction.go index 1f8ac52..0e57469 100644 --- a/chain/transaction.go +++ b/chain/transaction.go @@ -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() +} diff --git a/keymgmt/keymgmt.go b/keymgmt/keymgmt.go index 3ce54e2..1ac4b6d 100644 --- a/keymgmt/keymgmt.go +++ b/keymgmt/keymgmt.go @@ -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) diff --git a/main.go b/main.go index ac43c4a..c676911 100644 --- a/main.go +++ b/main.go @@ -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() diff --git a/monoblock_test.go b/monoblock_test.go new file mode 100644 index 0000000..f8b967f --- /dev/null +++ b/monoblock_test.go @@ -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()) +}