Skip to content

Commit

Permalink
feature: implemented Address generation, bug in block hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
theghostmac committed Sep 17, 2023
1 parent a2830e7 commit 6c1e1e7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
13 changes: 12 additions & 1 deletion internal/core/utils/address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "fmt"
import (
"encoding/hex"
"fmt"
)

type Address [20]uint8

Expand All @@ -12,6 +15,14 @@ func (a Address) ToSlice() []byte {
return b
}

func (a Address) String() string {
// return hex.EncodeToString(a.ToSlice())
// do syntactic sugar with 0x

hexString := hex.EncodeToString(a.ToSlice())
return "0x" + hexString
}

func AddressFromBytes(b []byte) Address {
if len(b) != 20 {
panic(fmt.Sprintf("Invalid input length. Expected byte slice length of 20, but got %d.", len(b)))
Expand Down
49 changes: 26 additions & 23 deletions tests/unit/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/theghostmac/pluto/internal/core/blockchain"
"github.com/theghostmac/pluto/internal/core/transactions"
"github.com/theghostmac/pluto/internal/core/utils"
"github.com/theghostmac/pluto/tests/unit"
"reflect"
"testing"
"time"
)

// Note to self
// use already-made random block generator function: block := unit.RandomBlock()

func TestHeaderEncodeDecodeBinary(t *testing.T) {
// Create a sample Header with values.
header := blockchain.Header{
Expand Down Expand Up @@ -49,16 +52,7 @@ func headerEqual(h1, h2 blockchain.Header) bool {

func TestBlockEncodeDecodeBinary(t *testing.T) {
// Create a sample Block with header and Transaction.
block := blockchain.Block{
Header: blockchain.Header{
Version: 1,
PreviousBlockHash: utils.Hash{},
Timestamp: time.Now().UnixNano(),
Height: 15,
Nonce: 67890,
},
Transactions: []transactions.Transactions{},
}
block := unit.RandomBlock()

// Encode the Block to binary
var encodedBlockBuffer bytes.Buffer
Expand All @@ -76,22 +70,31 @@ func TestBlockEncodeDecodeBinary(t *testing.T) {
// Compare the original Block with the decoded Block
if !reflect.DeepEqual(block, decodedBlock) {
t.Errorf("Decoded Block does not match original Block.")

// Print details about the mismatched fields
if block.Header.Version != decodedBlock.Header.Version {
t.Errorf("Version: Original=%d, Decoded=%d", block.Header.Version, decodedBlock.Header.Version)
}

if block.Header.PreviousBlockHash != decodedBlock.Header.PreviousBlockHash {
t.Errorf("PreviousBlockHash: Original=%s, Decoded=%s", block.Header.PreviousBlockHash, decodedBlock.Header.PreviousBlockHash)
}

if block.Header.Timestamp != decodedBlock.Header.Timestamp {
t.Errorf("Timestamp: Original=%d, Decoded=%d", block.Header.Timestamp, decodedBlock.Header.Timestamp)
}

if block.Header.Height != decodedBlock.Header.Height {
t.Errorf("Height: Original=%d, Decoded=%d", block.Header.Height, decodedBlock.Header.Height)
}
}
fmt.Print(block)

fmt.Print("The block is ", block)
}

func TestBlockHash(t *testing.T) {
block := &blockchain.Block{
Header: blockchain.Header{
Version: 1,
PreviousBlockHash: utils.RandomHash(),
Timestamp: time.Now().UnixNano(),
Height: 10,
},
Transactions: []transactions.Transactions{},
Hash: utils.Hash{},
}
block := unit.RandomBlock()
hash := block.BlockHasher()
fmt.Println(hash)
assert.False(t, hash.IsZero())
assert.False(t, hash.IsZero(), "Block has should not be zero")
}
21 changes: 21 additions & 0 deletions tests/unit/mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package unit

import (
"github.com/theghostmac/pluto/internal/core/blockchain"
"github.com/theghostmac/pluto/internal/core/transactions"
"github.com/theghostmac/pluto/internal/core/utils"
"time"
)

func RandomBlock() blockchain.Block {
return blockchain.Block{
Header: blockchain.Header{
Version: 1,
PreviousBlockHash: utils.RandomHash(),
Timestamp: time.Now().UnixNano(),
Height: 10,
},
Transactions: []transactions.Transactions{},
Hash: utils.Hash{},
}
}

0 comments on commit 6c1e1e7

Please sign in to comment.