Skip to content

Commit

Permalink
test: broke EncodeBinary and DecodeBinary in 2 | DecodeBinary func is…
Browse files Browse the repository at this point in the history
… buggy
  • Loading branch information
theghostmac committed Sep 17, 2023
1 parent 6c1e1e7 commit 1c367ef
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
23 changes: 22 additions & 1 deletion internal/core/blockchain/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ func (h *Header) DecodeBinary(r io.Reader) error {
// BlockHasher hashes the Block.
func (b *Block) BlockHasher() utils.Hash {
newBuffer := &bytes.Buffer{}
b.Header.EncodeBinary(newBuffer)
err := b.Header.EncodeBinary(newBuffer)
if err != nil {
return [32]uint8{}
}

if b.Hash.IsZero() {
b.Hash = utils.Hash(sha256.Sum256(newBuffer.Bytes()))
Expand Down Expand Up @@ -90,6 +93,24 @@ func (b *Block) DecodeBinary(r io.Reader) error {
return nil
}

//// The DecodeBinary method is used to decode the Block structure from binary format.
//// It first decodes the Header of the block, and then decodes each transaction in the Transactions field of the Block structure.
//func (b *Block) DecodeBinary(r io.Reader) error {
// // Decode the Header
// if err := b.Header.DecodeBinary(r); err != nil {
// return err
// }
//
// // Decode the Transactions
// for i := range b.Transactions {
// if err := b.Transactions[i].DecodeBinary(r); err != nil {
// return err
// }
// }
//
// return nil
//}

func (b *Block) Hasher() utils.Hash {
return utils.Hash{} // TODO
}
2 changes: 1 addition & 1 deletion internal/cryptography/keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GeneratePrivateKey() (PrivateKey, error) {
}, nil
}

func (pk PrivateKey) PublicKey() PublicKey {
func (pk PrivateKey) GeneratePublicKey() PublicKey {
return PublicKey{
publicKey: &pk.privateKey.PublicKey,
}
Expand Down
39 changes: 38 additions & 1 deletion tests/unit/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

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

func TestHeaderEncodeDecodeBinary(t *testing.T) {
// Create a sample Header with values.
Expand All @@ -30,6 +30,7 @@ func TestHeaderEncodeDecodeBinary(t *testing.T) {
if err := header.EncodeBinary(&encodedHeaderBuffer); err != nil {
t.Fatalf("Error encoding header: %v", err)
}

// Decode the encoded binary data back into the new Header.
decodedHeader := blockchain.Header{}
if err := decodedHeader.DecodeBinary(&encodedHeaderBuffer); err != nil {
Expand Down Expand Up @@ -92,6 +93,42 @@ func TestBlockEncodeDecodeBinary(t *testing.T) {
fmt.Print("The block is ", block)
}

func TestHeaderEncodeBinary(t *testing.T) {
// Create a sample Header with values.
header := blockchain.Header{
Version: 1,
PreviousBlockHash: utils.RandomHash(),
Timestamp: time.Now().UnixNano(),
Height: 10,
Nonce: 67890,
}

// Encode the Header to binary
var encodedHeaderBuffer bytes.Buffer
if err := header.EncodeBinary(&encodedHeaderBuffer); err != nil {
t.Fatalf("Error encoding header: %v", err)
}
}

func TestHeaderDecodeBinary(t *testing.T) {
encodedData := []byte{
0x01, 0x00, 0x00, 0x00, // Version (uint32, little-endian)
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, // PreviousBlockHash (20 bytes)
0x18, 0x00, 0x00, 0x00, // Timestamp (int64, little-endian)
0x0A, 0x00, 0x00, 0x00, // Height (uint32, little-endian)
0x32, 0x54, 0x76, 0x98, 0xBA, 0xDC, 0xFE, 0x00, // Nonce (uint64, little-endian)
}

// Create a new Header to decode into
var decodedHeader blockchain.Header

// Decode the binary data into the new Header
if err := decodedHeader.DecodeBinary(bytes.NewReader(encodedData)); err != nil {
t.Fatalf("Error decoding header: %v", err)
}
}

func TestBlockHash(t *testing.T) {
block := unit.RandomBlock()
hash := block.BlockHasher()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/keypair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGeneratePrivateKey(t *testing.T) {
}

// Generate Public Key from Private Key
publicKey := privateKey.PublicKey()
publicKey := privateKey.GeneratePublicKey()

// Generate Address from public Key
address := publicKey.Address()
Expand Down

0 comments on commit 1c367ef

Please sign in to comment.