-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockTreeEntry.cs
More file actions
98 lines (85 loc) · 2.84 KB
/
BlockTreeEntry.cs
File metadata and controls
98 lines (85 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using BitcoinKernel.Core.Abstractions;
using BitcoinKernel.Core.Exceptions;
using BitcoinKernel.Interop;
namespace BitcoinKernel.Core.BlockProcessing;
/// <summary>
/// Represents an entry in the block tree (block index).
/// </summary>
public sealed class BlockTreeEntry : IEquatable<BlockTreeEntry>
{
private readonly IntPtr _handle;
internal BlockTreeEntry(IntPtr handle)
{
_handle = handle;
}
internal IntPtr Handle => _handle;
/// <summary>
/// Gets the block hash for this entry.
/// </summary>
public byte[] GetBlockHash()
{
var hashPtr = NativeMethods.BlockTreeEntryGetBlockHash(_handle);
if (hashPtr == IntPtr.Zero)
{
throw new BlockException("Failed to get block hash from tree entry");
}
using var blockHash = new BlockHash(hashPtr);
return blockHash.ToBytes();
}
/// <summary>
/// Gets the previous block tree entry (parent block).
/// </summary>
public BlockTreeEntry? GetPrevious()
{
var prevPtr = NativeMethods.BlockTreeEntryGetPrevious(_handle);
return prevPtr != IntPtr.Zero ? new BlockTreeEntry(prevPtr) : null;
}
/// <summary>
/// Gets the block height.
/// </summary>
public int GetHeight()
{
return NativeMethods.BlockTreeEntryGetHeight(_handle);
}
/// <summary>
/// Determines whether two block tree entries are equal.
/// Two block tree entries are equal when they point to the same block.
/// </summary>
public bool Equals(BlockTreeEntry? other)
{
if (other is null)
return false;
if (ReferenceEquals(this, other))
return true;
return NativeMethods.BlockTreeEntryEquals(_handle, other._handle) == 1;
}
/// <inheritdoc/>
public override bool Equals(object? obj) => Equals(obj as BlockTreeEntry);
/// <inheritdoc/>
public override int GetHashCode()
{
// Use the block hash bytes to compute hash code
// Read directly from native without wrapping in a BlockHash that would dispose
var hashPtr = NativeMethods.BlockTreeEntryGetBlockHash(_handle);
if (hashPtr == IntPtr.Zero)
{
return 0;
}
var hashBytes = new byte[32];
NativeMethods.BlockHashToBytes(hashPtr, hashBytes);
return BitConverter.ToInt32(hashBytes, 0);
}
/// <summary>
/// Determines whether two block tree entries are equal.
/// </summary>
public static bool operator ==(BlockTreeEntry? left, BlockTreeEntry? right)
{
if (left is null)
return right is null;
return left.Equals(right);
}
/// <summary>
/// Determines whether two block tree entries are not equal.
/// </summary>
public static bool operator !=(BlockTreeEntry? left, BlockTreeEntry? right) => !(left == right);
}