Skip to content

Commit 0e8c009

Browse files
committed
Blocks: add blockhash class and initialize other blocks related classes
modified chainstate classes with BlockHash
1 parent 04ceed9 commit 0e8c009

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package org.bitcoinkernel;
2+
3+
import java.lang.foreign.*;
4+
5+
import static org.bitcoinkernel.BitcoinKernelBindings.*;
6+
7+
public class Blocks {
8+
9+
//todo!
10+
public static class BlockValidationState {
11+
12+
}
13+
14+
// ===== Block Hash =====
15+
public static class BlockHash implements AutoCloseable {
16+
private MemorySegment inner;
17+
private final Arena arena;
18+
19+
public BlockHash(byte[] hash) throws KernelTypes.KernelException {
20+
if (hash.length != 32) {
21+
throw new IllegalStateException("Block Hash length should be 32 bytes");
22+
}
23+
24+
this.arena = Arena.ofConfined();
25+
MemorySegment hashSegment = arena.allocateFrom(ValueLayout.JAVA_BYTE, hash);
26+
this.inner = btck_block_hash_create(hashSegment);
27+
if (inner == MemorySegment.NULL) {
28+
throw new KernelTypes.KernelException("Failed to instantiate Block Hash object");
29+
}
30+
}
31+
32+
// Internal structure for hashes returned by the API
33+
BlockHash(MemorySegment inner) {
34+
this.inner = inner;
35+
this.arena = null;
36+
}
37+
38+
public byte[] toBytes() {
39+
checkClosed();
40+
try (var tempArena = Arena.ofConfined()) {
41+
MemorySegment output = tempArena.allocate(32);
42+
btck_block_hash_to_bytes(inner, output);
43+
return output.toArray(ValueLayout.JAVA_BYTE);
44+
}
45+
}
46+
47+
public boolean equals(BlockHash other) {
48+
checkClosed();
49+
other.checkClosed();
50+
return btck_block_hash_equals(inner, other.inner) != 0;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
byte[] bytes = toBytes();
56+
int result = 1;
57+
for (byte b: bytes) {
58+
result = 31 * result + b;
59+
}
60+
return result;
61+
}
62+
63+
MemorySegment getInner() {
64+
return inner;
65+
}
66+
67+
private void checkClosed() {
68+
if (inner == MemorySegment.NULL) {
69+
throw new IllegalStateException("Block Hash has been closed");
70+
}
71+
}
72+
73+
@Override
74+
public void close() throws Exception {
75+
if (inner != MemorySegment.NULL) {
76+
btck_block_hash_destroy(inner);
77+
inner = MemorySegment.NULL;
78+
}
79+
80+
if (arena != null) {
81+
arena.close();
82+
}
83+
}
84+
}
85+
86+
//todo!
87+
public static class BlockTreeEntry {
88+
89+
}
90+
91+
//todo!
92+
public static class Block implements AutoCloseable {
93+
94+
95+
@Override
96+
public void close() throws Exception {
97+
98+
}
99+
}
100+
101+
//todo!
102+
public static class BlockSpentOutputs implements AutoCloseable {
103+
104+
105+
@Override
106+
public void close() throws Exception {
107+
108+
}
109+
}
110+
}

src/main/java/org/bitcoinkernel/Chainstate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import java.lang.foreign.*;
44

55
import static org.bitcoinkernel.BitcoinKernelBindings.*;
6+
import static org.bitcoinkernel.Blocks.*;
7+
import static org.bitcoinkernel.ContextManager.*;
68

7-
import java.lang.foreign.MemorySegment;
89

910
public class Chainstate {
1011

0 commit comments

Comments
 (0)