Skip to content

Commit

Permalink
show inbound fees in channel policy
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Otto committed May 3, 2024
1 parent 93c40af commit 04aeaac
Show file tree
Hide file tree
Showing 10 changed files with 824 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
package de.cotto.lndmanagej.grpc;

import com.google.protobuf.ByteString;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.Policy;
import lnrpc.RoutingPolicy;
import org.springframework.stereotype.Component;

import java.nio.IntBuffer;

@Component
public class GrpcPolicy {

private static final int FEE_RECORD_TYPE = 55_555;

public GrpcPolicy() {
// default constructor
}

public Policy toPolicy(RoutingPolicy routingPolicy) {
long inboundFeeRate;
Coins inboundBaseFee;
if (routingPolicy.containsCustomRecords(FEE_RECORD_TYPE)) {
ByteString record = routingPolicy.getCustomRecordsOrThrow(FEE_RECORD_TYPE);
IntBuffer intBuffer = record.asReadOnlyByteBuffer().asIntBuffer();
inboundBaseFee = Coins.ofMilliSatoshis(intBuffer.get());
inboundFeeRate = intBuffer.get();
} else {
inboundFeeRate = 0;
inboundBaseFee = Coins.NONE;
}

return new Policy(
routingPolicy.getFeeRateMilliMsat(),
Coins.ofMilliSatoshis(routingPolicy.getFeeBaseMsat()),
inboundFeeRate,
inboundBaseFee,
!routingPolicy.getDisabled(),
routingPolicy.getTimeLockDelta(),
Coins.ofMilliSatoshis(routingPolicy.getMinHtlc()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.cotto.lndmanagej.grpc;

import com.google.protobuf.ByteString;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.Policy;
import de.cotto.lndmanagej.model.Pubkey;
Expand All @@ -20,6 +21,7 @@
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_3;
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_4;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -163,6 +165,49 @@ void getOtherPubkey_no_matching_pubkey() {
assertThat(grpcChannelPolicy.getOtherPubkey(CHANNEL_ID, PUBKEY_4)).isEmpty();
}

@Test
void getPolicy_with_unknown_custom_records() {
when(grpcService.getChannelEdge(CHANNEL_ID)).thenReturn(Optional.of(
edgeWithCustomRecord(123, ByteString.copyFromUtf8("test"))
));
Policy policy = grpcChannelPolicy.getPolicyTo(CHANNEL_ID, PUBKEY_2).orElseThrow();

assertSoftly(softly -> {
softly.assertThat(policy.inboundFeeRate()).isEqualTo(0L);
softly.assertThat(policy.inboundBaseFee()).isEqualTo(Coins.NONE);
});
}

@Test
void getPolicy_with_negative_inbound_fees() {
ByteString inboundBaseFee = ByteString.fromHex(Integer.toHexString(-456));
ByteString inboundFeeRate = ByteString.fromHex(Integer.toHexString(-123));
ByteString value = inboundBaseFee.concat(inboundFeeRate);

when(grpcService.getChannelEdge(CHANNEL_ID)).thenReturn(Optional.of(
edgeWithCustomRecord(55_555, value)
));
Policy policy = grpcChannelPolicy.getPolicyTo(CHANNEL_ID, PUBKEY_2).orElseThrow();

assertSoftly(softly -> {
softly.assertThat(policy.inboundBaseFee()).isEqualTo(Coins.ofMilliSatoshis(-456));
softly.assertThat(policy.inboundFeeRate()).isEqualTo(-123L);
});
}

@Test
void getPolicy_with_negative_inbound_fees_using_hex_string() {
when(grpcService.getChannelEdge(CHANNEL_ID)).thenReturn(Optional.of(
edgeWithCustomRecord(55_555, ByteString.fromHex("00000000fffffe0c"))
));
Policy policy = grpcChannelPolicy.getPolicyTo(CHANNEL_ID, PUBKEY_2).orElseThrow();

assertSoftly(softly -> {
softly.assertThat(policy.inboundBaseFee()).isEqualTo(Coins.NONE);
softly.assertThat(policy.inboundFeeRate()).isEqualTo(-500L);
});
}

private ChannelEdge channelEdge(Pubkey firstPubkey, Pubkey secondPubkey) {
return ChannelEdge.newBuilder()
.setNode1Pub(firstPubkey.toString())
Expand All @@ -172,6 +217,15 @@ private ChannelEdge channelEdge(Pubkey firstPubkey, Pubkey secondPubkey) {
.build();
}

private ChannelEdge edgeWithCustomRecord(int key, ByteString value) {
return ChannelEdge.newBuilder()
.setNode1Pub(PUBKEY.toString())
.setNode2Pub(PUBKEY_2.toString())
.setNode1Policy(policyWithCustomRecord(key, value))
.setNode2Policy(routingPolicy(FEE_RATE_SECOND))
.build();
}

private RoutingPolicy routingPolicy(int feeRate) {
return RoutingPolicy.newBuilder()
.setFeeRateMilliMsat(feeRate)
Expand All @@ -180,4 +234,14 @@ private RoutingPolicy routingPolicy(int feeRate) {
.setMaxHtlcMsat(MAX_HTLC.milliSatoshis())
.build();
}

private RoutingPolicy policyWithCustomRecord(long key, ByteString value) {
return RoutingPolicy.newBuilder()
.setFeeRateMilliMsat(FEE_RATE_FIRST)
.setTimeLockDelta(TIME_LOCK_DELTA)
.setMinHtlc(MIN_HTLC.milliSatoshis())
.setMaxHtlcMsat(MAX_HTLC.milliSatoshis())
.putCustomRecords(key, value)
.build();
}
}
Loading

0 comments on commit 04aeaac

Please sign in to comment.