fix(hardcoded): extract hardcoded node aliases to "hardcoded" module (and make it configurable)

This commit is contained in:
danielbroll
2022-04-21 16:20:16 +02:00
committed by Carsten Otto
parent 96e21ae89b
commit c396bc351d
4 changed files with 23 additions and 8 deletions

6
example-lnd-manageJ.conf Normal file
View File

@@ -0,0 +1,6 @@
[resolutions]
732759x1405x0=OUTGOING_HTLC:TIMEOUT:e2c1c569b9d907878508333c7727fcae245f92d4c27568b8516ec74823ffb028
[aliases]
02f72978d40efeffca537139ad6ac9f09970c000a2dbc0d7aa55a71327c4577a80=Chivo_IBEX a0
037cc5f9f1da20ac0d60e83989729a204a33cc2d8e80438969fadf35c1c5f1233b=BlueWallet

View File

@@ -1,5 +1,6 @@
package de.cotto.lndmanagej.grpc;
import de.cotto.lndmanagej.hardcoded.HardcodedService;
import de.cotto.lndmanagej.model.Node;
import de.cotto.lndmanagej.model.Pubkey;
import lnrpc.LightningNode;
@@ -10,9 +11,11 @@ import org.springframework.stereotype.Component;
@Component
public class GrpcNodeInfo {
private final GrpcService grpcService;
private final HardcodedService hardcodedService;
public GrpcNodeInfo(GrpcService grpcService) {
public GrpcNodeInfo(GrpcService grpcService, HardcodedService hardcodedService) {
this.grpcService = grpcService;
this.hardcodedService = hardcodedService;
}
public Node getNode(Pubkey pubkey) {
@@ -21,9 +24,10 @@ public class GrpcNodeInfo {
return Node.forPubkey(pubkey);
}
LightningNode node = nodeInfo.getNode();
String alias = hardcodedService.getAliasOrDefault(pubkey, node.getAlias());
return Node.builder()
.withPubkey(pubkey)
.withAlias(node.getAlias())
.withAlias(alias)
.withLastUpdate(node.getLastUpdate())
.build();
}
@@ -38,9 +42,10 @@ public class GrpcNodeInfo {
.map(Peer::getPubKey)
.map(Pubkey::create)
.anyMatch(pubkey::equals);
String alias = hardcodedService.getAliasOrDefault(pubkey, node.getAlias());
return Node.builder()
.withPubkey(pubkey)
.withAlias(node.getAlias())
.withAlias(alias)
.withLastUpdate(node.getLastUpdate())
.withOnlineStatus(isPeer)
.build();

View File

@@ -2,6 +2,7 @@ package de.cotto.lndmanagej.hardcoded;
import com.google.common.base.Splitter;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Pubkey;
import de.cotto.lndmanagej.model.Resolution;
import de.cotto.lndmanagej.model.TransactionHash;
import org.springframework.stereotype.Component;
@@ -19,6 +20,7 @@ public class HardcodedService {
private static final int EXPECTED_NUMBER_OF_COMPONENTS = 3;
private static final String RESOLUTIONS_SECTION = "resolutions";
private static final Splitter SPLITTER = Splitter.on(":");
private static final String ALIASES_SECTION = "aliases";
private final IniFileReader iniFileReader;
@@ -26,6 +28,12 @@ public class HardcodedService {
this.iniFileReader = iniFileReader;
}
public String getAliasOrDefault(Pubkey pubkey, String defaultAlias) {
Map<String, Set<String>> values = iniFileReader.getValues(ALIASES_SECTION);
Set<String> alias = values.getOrDefault(pubkey.toString(), Set.of());
return alias.stream().findFirst().orElse(defaultAlias);
}
public Set<Resolution> getResolutions(ChannelId channelId) {
Map<String, Set<String>> values = iniFileReader.getValues(RESOLUTIONS_SECTION);
Set<String> forShortChannelId = values.getOrDefault(String.valueOf(channelId.getShortChannelId()), Set.of());

View File

@@ -13,14 +13,10 @@ public record Node(
int lastUpdate,
boolean online
) implements Comparable<Node> {
private static final Map<Pubkey, String> HARDCODED_ALIASES = Map.of(
Pubkey.create("02f72978d40efeffca537139ad6ac9f09970c000a2dbc0d7aa55a71327c4577a80"), "Chivo IBEX_a0",
Pubkey.create("037cc5f9f1da20ac0d60e83989729a204a33cc2d8e80438969fadf35c1c5f1233b"), "BlueWallet"
);
public Node(Pubkey pubkey, String alias, int lastUpdate, boolean online) {
this.online = online;
this.alias = HARDCODED_ALIASES.getOrDefault(pubkey, alias);
this.alias = alias;
this.lastUpdate = lastUpdate;
this.pubkey = pubkey;
}