diff --git a/cmd/gochain/main.go b/cmd/gochain/main.go index 06ab7d0..4b09a79 100644 --- a/cmd/gochain/main.go +++ b/cmd/gochain/main.go @@ -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()) +} diff --git a/internal/blockchain/block_test.go b/internal/blockchain/block_test.go index 681d83f..97cafd2 100644 --- a/internal/blockchain/block_test.go +++ b/internal/blockchain/block_test.go @@ -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() @@ -33,8 +33,8 @@ 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") @@ -42,26 +42,14 @@ func TestNewBlock(t *testing.T) { } 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") } } diff --git a/internal/blockchain/blockchain_test.go b/internal/blockchain/blockchain_test.go index 7d7aec7..1db318c 100644 --- a/internal/blockchain/blockchain_test.go +++ b/internal/blockchain/blockchain_test.go @@ -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") } } diff --git a/internal/blockchain/validations_test.go b/internal/blockchain/validations_test.go index c13eac5..f171827 100644 --- a/internal/blockchain/validations_test.go +++ b/internal/blockchain/validations_test.go @@ -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") } @@ -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) {