Skip to content

Commit 7f9e4cb

Browse files
authored
Merge pull request #7 from yuvicc/2025-11-some_compilation_issues
Fix: some compilation issues
2 parents f8cace8 + aee84c1 commit 7f9e4cb

File tree

4 files changed

+121
-17
lines changed

4 files changed

+121
-17
lines changed

build.gradle

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ plugins {
33
id 'application'
44
}
55

6+
dependencies {
7+
// JSON processing
8+
implementation 'com.google.code.gson:gson:2.10.1'
9+
10+
// Testing
11+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
12+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
13+
}
14+
615
java {
716
toolchain {
817
languageVersion = JavaLanguageVersion.of(24)
@@ -22,10 +31,24 @@ def bitcoinKernelHeader = file("${bitcoinCoreDir}/src/kernel/bitcoinkernel.h")
2231
tasks.register('buildBitcoinCore', Exec) {
2332
workingDir bitcoinCoreDir
2433

25-
// Configure Bitcoin Core build
34+
// Configure Bitcoin Core build (matching rust-bitcoinkernel approach)
2635
commandLine 'cmake', '-B', 'build',
36+
'-DCMAKE_BUILD_TYPE=RelWithDebInfo',
2737
'-DBUILD_KERNEL_LIB=ON',
28-
'-DBUILD_UTIL_CHAINSTATE=ON'
38+
'-DBUILD_TESTS=OFF',
39+
'-DBUILD_KERNEL_TEST=OFF',
40+
'-DBUILD_TX=OFF',
41+
'-DBUILD_WALLET_TOOL=OFF',
42+
'-DENABLE_WALLET=OFF',
43+
'-DENABLE_EXTERNAL_SIGNER=OFF',
44+
'-DBUILD_UTIL=OFF',
45+
'-DBUILD_BITCOIN_BIN=OFF',
46+
'-DBUILD_DAEMON=OFF',
47+
'-DBUILD_UTIL_CHAINSTATE=OFF',
48+
'-DBUILD_CLI=OFF',
49+
'-DBUILD_SHARED_LIBS=ON',
50+
'-DCMAKE_INSTALL_LIBDIR=lib',
51+
'-DENABLE_IPC=OFF'
2952

3053
// Only run if build directory doesn't exist or header has changed
3154
// inputs.file bitcoinKernelHeader
@@ -65,4 +88,62 @@ application {
6588
}
6689

6790
// Ensure proper build order
68-
compileJava.dependsOn compileBitcoinCore
91+
compileJava.dependsOn compileBitcoinCore
92+
93+
// Configure test task
94+
test {
95+
useJUnitPlatform()
96+
testLogging {
97+
events "passed", "skipped", "failed"
98+
exceptionFormat "full"
99+
}
100+
}
101+
102+
// Task to run conformance handler
103+
tasks.register('conformanceHandler', Exec) {
104+
group = 'conformance'
105+
description = 'Run the conformance test handler (reads from stdin, writes to stdout)'
106+
107+
classpath = sourceSets.main.runtimeClasspath
108+
mainClass = 'org.bitcoinkernel.conformance.ConformanceTestHandler'
109+
110+
// Required for FFM
111+
jvmArgs = ['--enable-native-access=ALL-UNNAMED']
112+
113+
// Connect stdin/stdout
114+
standardInput = System.in
115+
standardOutput = System.out
116+
standardError = System.err
117+
}
118+
119+
// Build uber JAR with all dependencies
120+
tasks.register('buildConformanceJar', Jar) {
121+
group = 'conformance'
122+
description = 'Build standalone conformance handler JAR with all dependencies'
123+
124+
archiveBaseName = 'java-bitcoinkernel-conformance-handler'
125+
archiveVersion = '0.1.0'
126+
127+
from sourceSets.main.output
128+
129+
// Include all dependencies
130+
from {
131+
configurations.runtimeClasspath.collect {
132+
it.isDirectory() ? it : zipTree(it)
133+
}
134+
}
135+
136+
// Avoid duplicate files
137+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
138+
139+
manifest {
140+
attributes(
141+
'Main-Class': 'org.bitcoinkernel.conformance.ConformanceTestHandler',
142+
'Implementation-Title': 'Java Bitcoin Kernel Conformance Handler',
143+
'Implementation-Version': archiveVersion.get()
144+
)
145+
}
146+
}
147+
148+
// Make conformance JAR depend on compilation
149+
buildConformanceJar.dependsOn compileJava

src/main/java/org/bitcoinkernel/KernelData.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ public int verify(long amount, Transaction txTo, TransactionOutput[] spentOutput
4949
txTo.checkClosed();
5050

5151
try (var arena = Arena.ofConfined()) {
52-
// Allocate array of output pointers
53-
MemorySegment outputPtrs = arena.allocate(
54-
ValueLayout.ADDRESS,
55-
spentOutputs.length
56-
);
57-
58-
for (int i = 0; i < spentOutputs.length; i++) {
59-
outputPtrs.setAtIndex(ValueLayout.ADDRESS, i, spentOutputs[i].getInner());
52+
// Prepare spent outputs array
53+
int numOutputs = (spentOutputs != null) ? spentOutputs.length : 0;
54+
MemorySegment outputPtrs;
55+
56+
if (numOutputs > 0) {
57+
// Allocate array of output pointers
58+
outputPtrs = arena.allocate(ValueLayout.ADDRESS, numOutputs);
59+
for (int i = 0; i < spentOutputs.length; i++) {
60+
outputPtrs.setAtIndex(ValueLayout.ADDRESS, i, spentOutputs[i].getInner());
61+
}
62+
} else {
63+
// Pass NULL for empty/null spent outputs
64+
outputPtrs = MemorySegment.NULL;
6065
}
6166

6267
MemorySegment statusPtr = arena.allocate(ValueLayout.JAVA_BYTE);
@@ -66,13 +71,14 @@ public int verify(long amount, Transaction txTo, TransactionOutput[] spentOutput
6671
amount,
6772
txTo.getInner(),
6873
outputPtrs,
69-
spentOutputs.length,
74+
numOutputs,
7075
inputIndex,
7176
flags,
7277
statusPtr
7378
);
7479

75-
if (result != 0) {
80+
// Note: return value 1 = success, 0 = error
81+
if (result == 0) {
7682
byte status = statusPtr.get(ValueLayout.JAVA_BYTE, 0);
7783
throw new KernelTypes.KernelException(
7884
KernelTypes.KernelException.ScriptVerifyError.fromNative(status)

src/main/java/org/bitcoinkernel/Transactions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ MemorySegment getInner() {
8484
}
8585

8686
@Override
87-
public void close() throws Exception {
87+
public void close() {
8888
if (inner != MemorySegment.NULL) {
8989
btck_transaction_destroy(inner);
9090
inner = MemorySegment.NULL;
@@ -264,7 +264,7 @@ MemorySegment getInner() {
264264
}
265265

266266
@Override
267-
public void close() throws Exception {
267+
public void close() {
268268
if (inner != MemorySegment.NULL && ownsMemory) {
269269
btck_transaction_output_destroy(inner);
270270
inner = MemorySegment.NULL;

src/main/java/org/bitcoinkernel/jextract/bitcoinkernel_h.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,25 @@ public class bitcoinkernel_h extends bitcoinkernel_h$shared {
2020

2121
static final Arena LIBRARY_ARENA = Arena.ofAuto();
2222

23-
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
24-
.or(Linker.nativeLinker().defaultLookup());
23+
static final SymbolLookup SYMBOL_LOOKUP;
24+
25+
static {
26+
// Load the bitcoinkernel library
27+
SymbolLookup lookup = null;
28+
try {
29+
System.loadLibrary("bitcoinkernel");
30+
lookup = SymbolLookup.loaderLookup();
31+
} catch (UnsatisfiedLinkError e) {
32+
// Fall back to default lookup (LD_LIBRARY_PATH)
33+
System.err.println("Warning: Could not load libbitcoinkernel via System.loadLibrary, trying LD_LIBRARY_PATH");
34+
}
35+
36+
if (lookup == null) {
37+
lookup = Linker.nativeLinker().defaultLookup();
38+
}
39+
40+
SYMBOL_LOOKUP = lookup;
41+
}
2542

2643
private static final int __WORDSIZE = (int)64L;
2744
/**

0 commit comments

Comments
 (0)