|
| 1 | +using BitcoinKernel.Core.Exceptions; |
| 2 | +using BitcoinKernel.Interop; |
| 3 | + |
| 4 | +namespace BitcoinKernel.Core.Abstractions; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Represents a block header containing metadata about a block. |
| 8 | +/// </summary> |
| 9 | +public sealed class BlockHeader : IDisposable |
| 10 | +{ |
| 11 | + private IntPtr _handle; |
| 12 | + private bool _disposed; |
| 13 | + private readonly bool _ownsHandle; |
| 14 | + |
| 15 | + internal BlockHeader(IntPtr handle, bool ownsHandle = true) |
| 16 | + { |
| 17 | + _handle = handle != IntPtr.Zero |
| 18 | + ? handle |
| 19 | + : throw new ArgumentException("Invalid block header handle", nameof(handle)); |
| 20 | + _ownsHandle = ownsHandle; |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Creates a block header from raw serialized data (80 bytes). |
| 25 | + /// </summary> |
| 26 | + public static BlockHeader FromBytes(byte[] rawHeaderData) |
| 27 | + { |
| 28 | + ArgumentNullException.ThrowIfNull(rawHeaderData, nameof(rawHeaderData)); |
| 29 | + if (rawHeaderData.Length != 80) |
| 30 | + throw new ArgumentException("Block header must be exactly 80 bytes", nameof(rawHeaderData)); |
| 31 | + |
| 32 | + IntPtr headerPtr = NativeMethods.BlockHeaderCreate(rawHeaderData, (UIntPtr)rawHeaderData.Length); |
| 33 | + |
| 34 | + if (headerPtr == IntPtr.Zero) |
| 35 | + { |
| 36 | + throw new BlockException("Failed to create block header from raw data"); |
| 37 | + } |
| 38 | + |
| 39 | + return new BlockHeader(headerPtr); |
| 40 | + } |
| 41 | + |
| 42 | + internal IntPtr Handle |
| 43 | + { |
| 44 | + get |
| 45 | + { |
| 46 | + ThrowIfDisposed(); |
| 47 | + return _handle; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets the block hash of this header. |
| 53 | + /// </summary> |
| 54 | + public byte[] GetHash() |
| 55 | + { |
| 56 | + ThrowIfDisposed(); |
| 57 | + var hashPtr = NativeMethods.BlockHeaderGetHash(_handle); |
| 58 | + if (hashPtr == IntPtr.Zero) |
| 59 | + { |
| 60 | + throw new BlockException("Failed to get block hash from header"); |
| 61 | + } |
| 62 | + |
| 63 | + using var blockHash = new BlockHash(hashPtr); |
| 64 | + return blockHash.ToBytes(); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets the previous block hash from this header. |
| 69 | + /// </summary> |
| 70 | + public byte[] GetPrevHash() |
| 71 | + { |
| 72 | + ThrowIfDisposed(); |
| 73 | + var hashPtr = NativeMethods.BlockHeaderGetPrevHash(_handle); |
| 74 | + if (hashPtr == IntPtr.Zero) |
| 75 | + { |
| 76 | + throw new BlockException("Failed to get previous block hash from header"); |
| 77 | + } |
| 78 | + |
| 79 | + // The hash pointer is unowned and only valid for the lifetime of the header |
| 80 | + var bytes = new byte[32]; |
| 81 | + NativeMethods.BlockHashToBytes(hashPtr, bytes); |
| 82 | + return bytes; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Gets the timestamp from this header (Unix epoch seconds). |
| 87 | + /// </summary> |
| 88 | + public uint Timestamp |
| 89 | + { |
| 90 | + get |
| 91 | + { |
| 92 | + ThrowIfDisposed(); |
| 93 | + return NativeMethods.BlockHeaderGetTimestamp(_handle); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Gets the nBits difficulty target from this header (compact format). |
| 99 | + /// </summary> |
| 100 | + public uint Bits |
| 101 | + { |
| 102 | + get |
| 103 | + { |
| 104 | + ThrowIfDisposed(); |
| 105 | + return NativeMethods.BlockHeaderGetBits(_handle); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Gets the version from this header. |
| 111 | + /// </summary> |
| 112 | + public int Version |
| 113 | + { |
| 114 | + get |
| 115 | + { |
| 116 | + ThrowIfDisposed(); |
| 117 | + return NativeMethods.BlockHeaderGetVersion(_handle); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary> |
| 122 | + /// Gets the nonce from this header. |
| 123 | + /// </summary> |
| 124 | + public uint Nonce |
| 125 | + { |
| 126 | + get |
| 127 | + { |
| 128 | + ThrowIfDisposed(); |
| 129 | + return NativeMethods.BlockHeaderGetNonce(_handle); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void ThrowIfDisposed() |
| 134 | + { |
| 135 | + if (_disposed) |
| 136 | + throw new ObjectDisposedException(nameof(BlockHeader)); |
| 137 | + } |
| 138 | + |
| 139 | + public void Dispose() |
| 140 | + { |
| 141 | + if (!_disposed) |
| 142 | + { |
| 143 | + if (_handle != IntPtr.Zero && _ownsHandle) |
| 144 | + { |
| 145 | + NativeMethods.BlockHeaderDestroy(_handle); |
| 146 | + _handle = IntPtr.Zero; |
| 147 | + } |
| 148 | + _disposed = true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + ~BlockHeader() |
| 153 | + { |
| 154 | + if (_ownsHandle) |
| 155 | + Dispose(); |
| 156 | + } |
| 157 | +} |
0 commit comments