Skip to content

Commit

Permalink
BlockChainInfo: methods to convert chain String to/from Network
Browse files Browse the repository at this point in the history
Add static methods for converting the (non-standard) chain name/id String
to/from a `BitcoinNetwork` enum.
  • Loading branch information
msgilligan committed Sep 27, 2023
1 parent 6d5997b commit 8782688
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.bitcoinj.base.BitcoinNetwork;
import org.bitcoinj.base.Network;
import org.bitcoinj.base.Sha256Hash;

import java.math.BigDecimal;
Expand Down Expand Up @@ -68,4 +69,53 @@ public BigDecimal getVerificationProgress() {
public byte[] getChainWork() {
return chainWork;
}

/**
* Map a BlockChainInfo chain string to a Network. These strings are different from the standard values
* in {@link BitcoinNetwork#toString()}.
* @param info {@code BlockChainInfo}
* @return the matching network.
*/
public static Network chainToNetwork(BlockChainInfo info) {
Network network;
switch(info.getChain()) {
case "main":
network = BitcoinNetwork.MAINNET;
break;
case "test":
network = BitcoinNetwork.TESTNET;
break;
case "signet":
network = BitcoinNetwork.SIGNET;
break;
case "regtest":
network = BitcoinNetwork.REGTEST;
break;
default:
throw new RuntimeException("BlockChainInfo contains unrecognized Bitcoin network");
}
return network;
}

/**
* Map {@link BitcoinNetwork} to a chain-id string.
* Bitcoin Core returns strings that differ from {@link BitcoinNetwork#toString()}.
* @param network bitcoinj enum type
* @return Bitcoin Core-compatible <q>chain</q> string
*/
public static String networkToChainName(BitcoinNetwork network) {
String name;
switch(network) {
case MAINNET:
name = "main";
break;
case TESTNET:
name = "test";
break;
default:
name = network.toString();
break;
};
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,27 +246,7 @@ synchronized int getServerVersion() {
* @return A future for the server-side {@code Network}
*/
private CompletableFuture<Network> getNetworkFromServer() {
return getBlockChainInfoAsync().thenApply(info -> {
Network network;
switch(info.getChain()) {
case "main":
network = BitcoinNetwork.MAINNET;
break;
case "test":
network = BitcoinNetwork.TESTNET;
break;
case "signet":
network = BitcoinNetwork.SIGNET;
break;
case "regtest":
network = BitcoinNetwork.REGTEST;
break;
default:
throw new RuntimeException("Server returned unrecognized Bitcoin network");

}
return network;
});
return getBlockChainInfoAsync().thenApply(BlockChainInfo::chainToNetwork);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public CompletableFuture<Integer> getconnectioncount() {

@Override
public CompletableFuture<BlockChainInfo> getblockchaininfo() {
return result(new BlockChainInfo(chainName(network), // Chain ID
return result(new BlockChainInfo(BlockChainInfo.networkToChainName(network), // Chain ID
kit.chain().getChainHead().getHeight(), // Block processed (same as headers for SPV)
kit.chain().getChainHead().getHeight(), // Headers validated
kit.chain().getChainHead().getHeader().getHash(), // Best block hash
Expand All @@ -283,20 +283,6 @@ public CompletableFuture<BlockChainInfo> getblockchaininfo() {
chainWork));
}

/**
* Map {@link BitcoinNetwork} to a chain-id string.
* Bitcoin Core returns strings that differ from {@link BitcoinNetwork#toString()}.
* @param network bitcoinj enum type
* @return Bitcoin Core-compatible <q>chain</q> string
*/
private String chainName(BitcoinNetwork network) {
return switch(network) {
case MAINNET -> "main";
case TESTNET -> "test";
case SIGNET, REGTEST -> network.toString();
};
}

@Override
public CompletableFuture<NetworkInfo> getnetworkinfo() {
byte[] localServices = {};
Expand Down

0 comments on commit 8782688

Please sign in to comment.