Skip to content

Commit c4c3663

Browse files
committed
fix: normalize ChainPosition ffi byte accounting
1 parent 8b7bb74 commit c4c3663

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

lib/bdk.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5615,9 +5615,17 @@ class FfiConverterChainPosition {
56155615
final subview = Uint8List.view(buf.buffer, buf.offsetInBytes + 4);
56165616
switch (index) {
56175617
case 1:
5618-
return ConfirmedChainPosition.read(subview);
5618+
final lifted = ConfirmedChainPosition.read(subview);
5619+
return LiftRetVal<ChainPosition>(
5620+
lifted.value,
5621+
lifted.bytesRead - subview.offsetInBytes + 4,
5622+
);
56195623
case 2:
5620-
return UnconfirmedChainPosition.read(subview);
5624+
final lifted = UnconfirmedChainPosition.read(subview);
5625+
return LiftRetVal<ChainPosition>(
5626+
lifted.value,
5627+
lifted.bytesRead - subview.offsetInBytes + 4,
5628+
);
56215629
default:
56225630
throw UniffiInternalError(
56235631
UniffiInternalError.unexpectedEnumCase,
@@ -5635,7 +5643,7 @@ class FfiConverterChainPosition {
56355643
}
56365644

56375645
static int write(ChainPosition value, Uint8List buf) {
5638-
return value.write(buf);
5646+
return value.write(buf) - buf.offsetInBytes;
56395647
}
56405648
}
56415649

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import "dart:typed_data";
2+
3+
import "package:bdk_dart/bdk.dart";
4+
import "package:test/test.dart";
5+
6+
void main() {
7+
group("ChainPosition ffi converter", () {
8+
test("read reports relative bytes from a non-zero buffer offset", () {
9+
final chainPosition = UnconfirmedChainPosition(1234567890);
10+
final encoded = Uint8List(chainPosition.allocationSize());
11+
final encodedLength = FfiConverterChainPosition.write(chainPosition, encoded);
12+
13+
expect(encodedLength, encoded.length);
14+
15+
const prefixLength = 11;
16+
final padded = Uint8List(prefixLength + encoded.length);
17+
padded.setRange(prefixLength, padded.length, encoded);
18+
19+
final lifted = FfiConverterChainPosition.read(
20+
Uint8List.view(padded.buffer, prefixLength),
21+
);
22+
23+
final unconfirmed = lifted.value as UnconfirmedChainPosition;
24+
expect(unconfirmed.timestamp, 1234567890);
25+
expect(lifted.bytesRead, encoded.length);
26+
});
27+
28+
test("write reports relative bytes from a non-zero buffer offset", () {
29+
final chainPosition = UnconfirmedChainPosition(1234567890);
30+
const prefixLength = 7;
31+
final buffer = Uint8List(prefixLength + chainPosition.allocationSize());
32+
33+
final bytesWritten = FfiConverterChainPosition.write(
34+
chainPosition,
35+
Uint8List.view(buffer.buffer, prefixLength),
36+
);
37+
38+
expect(bytesWritten, chainPosition.allocationSize());
39+
});
40+
});
41+
}

0 commit comments

Comments
 (0)