Skip to content

Add tests #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions cmd/gochain/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
package main

import (
"fmt"
"github.com/NicholasRodrigues/go-chain/internal/blockchain"
)

func main() {
bc := blockchain.NewBlockchain()

bc.AddBlock("First Block after Genesis")
bc.AddBlock("Second Block after Genesis")

for _, block := range bc.Blocks {
fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash)
fmt.Printf("Data: %s\n", block.Data)
fmt.Printf("Hash: %x\n", block.Hash)
fmt.Println()
}

fmt.Println("Is blockchain valid?", bc.IsValid())
}
32 changes: 10 additions & 22 deletions internal/blockchain/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestSetHash(t *testing.T) {
block := &Block{
Timestamp: time.Now().Unix(),
Data: []byte(data),
prevBlockHash: prevHash,
PrevBlockHash: prevHash,
Counter: 0,
}
block.SetHash()
Expand All @@ -33,35 +33,23 @@ func TestNewBlock(t *testing.T) {
if block.Data == nil || !bytes.Equal(block.Data, []byte(data)) {
t.Errorf("expected data %s, got %s", data, string(block.Data))
}
if block.prevBlockHash == nil || !bytes.Equal(block.prevBlockHash, prevHash) {
t.Errorf("expected previous hash %x, got %x", prevHash, block.prevBlockHash)
if block.PrevBlockHash == nil || !bytes.Equal(block.PrevBlockHash, prevHash) {
t.Errorf("expected previous hash %x, got %x", prevHash, block.PrevBlockHash)
}
if block.Hash == nil || len(block.Hash) == 0 {
t.Error("expected non-nil and non-empty hash")
}
}

func TestNewGenesisBlock(t *testing.T) {
block := NewGenesisBlock()

if block.Data == nil || string(block.Data) != "Genesis Block" {
t.Errorf("expected data 'Genesis Block', got %s", string(block.Data))
}
if block.prevBlockHash == nil || len(block.prevBlockHash) != 0 {
t.Error("expected empty previous hash")
}
if block.Hash == nil || len(block.Hash) == 0 {
t.Error("expected non-nil and non-empty hash")
genesisBlock := NewGenesisBlock()
if genesisBlock == nil {
t.Fatalf("Expected genesis block to be created")
}
}

func TestNewBlockchain(t *testing.T) {
bc := NewBlockchain()

if len(bc.blocks) != 1 {
t.Errorf("expected blockchain length 1, got %d", len(bc.blocks))
if len(genesisBlock.Hash) == 0 {
t.Errorf("Expected genesis block hash to be set")
}
if string(bc.blocks[0].Data) != "Genesis Block" {
t.Errorf("expected genesis block data 'Genesis Block', got %s", string(bc.blocks[0].Data))
if !bytes.Equal(genesisBlock.PrevBlockHash, []byte{}) {
t.Errorf("Expected genesis block to have no previous block hash")
}
}
66 changes: 56 additions & 10 deletions internal/blockchain/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,67 @@ import (
"testing"
)

func TestProofOfWork_Run(t *testing.T) {
block := NewBlock("test data", []byte{})
pow := NewProofOfWork(block)

hash, nonce := pow.Run()
block.Hash = hash[:]
block.Counter = nonce

if !pow.Validate() {
t.Errorf("Proof of work validation failed for block with hash %x", block.Hash)
}
}

func TestProofOfWork_Validate(t *testing.T) {
block := NewBlock("test data", []byte{})
pow := NewProofOfWork(block)

hash, nonce := pow.Run()
block.Hash = hash[:]
block.Counter = nonce

if !pow.Validate() {
t.Errorf("Expected block to be valid, but validation failed")
}
}

func TestNewBlockchain(t *testing.T) {
bc := NewBlockchain()
if bc == nil {
t.Fatalf("Expected blockchain to be created")
}
if len(bc.Blocks) != 1 {
t.Fatalf("Expected blockchain to have one block, got %d", len(bc.Blocks))
}
if !bytes.Equal(bc.Blocks[0].Hash, NewGenesisBlock().Hash) {
t.Errorf("Expected genesis block hash to match")
}
}

func TestAddBlock(t *testing.T) {
bc := NewBlockchain()
bc.AddBlock("New Block 1")
bc.AddBlock("New Block 2")
bc.AddBlock("First Block after Genesis")
bc.AddBlock("Second Block after Genesis")

if len(bc.blocks) != 3 {
t.Errorf("expected blockchain length 3, got %d", len(bc.blocks))
if len(bc.Blocks) != 3 {
t.Fatalf("Expected blockchain to have three Blocks, got %d", len(bc.Blocks))
}
if string(bc.blocks[1].Data) != "New Block 1" {
t.Errorf("expected block 1 data 'New Block 1', got %s", string(bc.blocks[1].Data))
if !bytes.Equal(bc.Blocks[1].PrevBlockHash, bc.Blocks[0].Hash) {
t.Errorf("Expected first block to reference genesis block hash")
}
if string(bc.blocks[2].Data) != "New Block 2" {
t.Errorf("expected block 2 data 'New Block 2', got %s", string(bc.blocks[2].Data))
if !bytes.Equal(bc.Blocks[2].PrevBlockHash, bc.Blocks[1].Hash) {
t.Errorf("Expected second block to reference first block hash")
}
if !bytes.Equal(bc.blocks[2].prevBlockHash, bc.blocks[1].Hash) {
t.Errorf("expected block 2 previous hash to match block 1 hash")
}

func TestBlockchain_IsValid(t *testing.T) {
bc := NewBlockchain()
bc.AddBlock("First Block after Genesis")
bc.AddBlock("Second Block after Genesis")

if !bc.IsValid() {
t.Errorf("Expected blockchain to be valid")
}
}
10 changes: 5 additions & 5 deletions internal/blockchain/validations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestContentValidatePredicate(t *testing.T) {
t.Error("expected blockchain to be valid")
}

bc.blocks[1].Hash = bc.blocks[0].Hash
bc.Blocks[1].Hash = bc.Blocks[0].Hash
if ContentValidatePredicate(bc) {
t.Error("expected blockchain to be invalid")
}
Expand All @@ -33,13 +33,13 @@ func TestInputContributionFunction(t *testing.T) {

InputContributionFunction(data, bc, round, input, receive)

if len(bc.blocks) != 2 {
t.Errorf("expected blockchain length 2, got %d", len(bc.blocks))
if len(bc.Blocks) != 2 {
t.Errorf("expected blockchain length 2, got %d", len(bc.Blocks))
}

concat_data := "input data" + "received data"
if string(bc.blocks[1].Data) != concat_data {
t.Errorf("expected block data %s, got %s", concat_data, string(bc.blocks[1].Data))
if string(bc.Blocks[1].Data) != concat_data {
t.Errorf("expected block data %s, got %s", concat_data, string(bc.Blocks[1].Data))
}

if !ContentValidatePredicate(bc) {
Expand Down
Loading