|
| 1 | +package kernel |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestInvalidBlockData(t *testing.T) { |
| 10 | + // Test with empty data |
| 11 | + _, err := NewBlockFromRaw([]byte{}) |
| 12 | + if !errors.Is(err, ErrInvalidBlockData) { |
| 13 | + t.Errorf("Expected ErrInvalidBlockData, got %v", err) |
| 14 | + } |
| 15 | + |
| 16 | + // Test with invalid data |
| 17 | + _, err = NewBlockFromRaw([]byte{0x00, 0x01, 0x02}) |
| 18 | + if !errors.Is(err, ErrBlockCreation) { |
| 19 | + t.Errorf("Expected ErrBlockCreation, got %v", err) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestBlockFromRaw(t *testing.T) { |
| 24 | + // Complete Bitcoin mainnet genesis block (285 bytes) |
| 25 | + genesisHex := "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000" |
| 26 | + genesisBytes, err := hex.DecodeString(genesisHex) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("Failed to decode genesis hex: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + block, err := NewBlockFromRaw(genesisBytes) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("NewBlockFromRaw() error = %v", err) |
| 34 | + } |
| 35 | + defer block.Destroy() |
| 36 | + |
| 37 | + // Test getting block hash |
| 38 | + hash, err := block.Hash() |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("Block.Hash() error = %v", err) |
| 41 | + } |
| 42 | + defer hash.Destroy() |
| 43 | + |
| 44 | + hashBytes := hash.Bytes() |
| 45 | + if len(hashBytes) != 32 { |
| 46 | + t.Errorf("Expected hash length 32, got %d", len(hashBytes)) |
| 47 | + } |
| 48 | + |
| 49 | + // Expected genesis block hash (reversed byte order for display) |
| 50 | + expectedHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" |
| 51 | + actualHashHex := hex.EncodeToString(ReverseBytes(hashBytes)) |
| 52 | + if actualHashHex != expectedHash { |
| 53 | + t.Logf("Actual hash: %s", actualHashHex) |
| 54 | + t.Logf("Expected hash: %s", expectedHash) |
| 55 | + } |
| 56 | + |
| 57 | + // Test getting block data |
| 58 | + data, err := block.Data() |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("Block.Data() error = %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + if len(data) != len(genesisBytes) { |
| 64 | + t.Errorf("Expected data length %d, got %d", len(genesisBytes), len(data)) |
| 65 | + } |
| 66 | + |
| 67 | + hexStr := hex.EncodeToString(data) |
| 68 | + if hexStr != genesisHex { |
| 69 | + t.Logf("Expected data hex: %s, got %s", genesisHex, hexStr) |
| 70 | + } |
| 71 | +} |
0 commit comments