Skip to content

Commit

Permalink
Merge pull request #155 from kennycud/master
Browse files Browse the repository at this point in the history
Foreign Blockchain Server Configuration Information Offered in the Core API
  • Loading branch information
AlphaX-Projects authored Dec 21, 2023
2 parents 6b775cc + 8607c30 commit e118f4a
Show file tree
Hide file tree
Showing 20 changed files with 429 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.qortal.crosschain.Bitcoin;
import org.qortal.crosschain.ForeignBlockchainException;
import org.qortal.crosschain.SimpleTransaction;
import org.qortal.crosschain.ServerConfigurationInfo;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -283,4 +284,24 @@ public String sendBitcoin(@HeaderParam(Security.API_KEY_HEADER) String apiKey, B
return spendTransaction.getTxId().toString();
}

@GET
@Path("/serverinfos")
@Operation(
summary = "Returns current Bitcoin server configuration",
description = "Returns current Bitcoin server locations and use status",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ServerConfigurationInfo.class
)
)
)
}
)
public ServerConfigurationInfo getServerConfiguration() {

return CrossChainUtils.buildServerConfigurationInfo(Bitcoin.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.qortal.crosschain.Digibyte;
import org.qortal.crosschain.ForeignBlockchainException;
import org.qortal.crosschain.SimpleTransaction;
import org.qortal.crosschain.ServerConfigurationInfo;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -283,4 +284,24 @@ public String sendBitcoin(@HeaderParam(Security.API_KEY_HEADER) String apiKey, D
return spendTransaction.getTxId().toString();
}

@GET
@Path("/serverinfos")
@Operation(
summary = "Returns current Digibyte server configuration",
description = "Returns current Digibyte server locations and use status",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ServerConfigurationInfo.class
)
)
)
}
)
public ServerConfigurationInfo getServerConfiguration() {

return CrossChainUtils.buildServerConfigurationInfo(Digibyte.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.qortal.crosschain.Dogecoin;
import org.qortal.crosschain.ForeignBlockchainException;
import org.qortal.crosschain.SimpleTransaction;
import org.qortal.crosschain.ServerConfigurationInfo;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -283,4 +284,24 @@ public String sendBitcoin(@HeaderParam(Security.API_KEY_HEADER) String apiKey, D
return spendTransaction.getTxId().toString();
}

@GET
@Path("/serverinfos")
@Operation(
summary = "Returns current Dogecoin server configuration",
description = "Returns current Dogecoin server locations and use status",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ServerConfigurationInfo.class
)
)
)
}
)
public ServerConfigurationInfo getServerConfiguration() {

return CrossChainUtils.buildServerConfigurationInfo(Dogecoin.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.qortal.crosschain.ForeignBlockchainException;
import org.qortal.crosschain.Litecoin;
import org.qortal.crosschain.SimpleTransaction;
import org.qortal.crosschain.ServerConfigurationInfo;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -283,4 +284,24 @@ public String sendBitcoin(@HeaderParam(Security.API_KEY_HEADER) String apiKey, L
return spendTransaction.getTxId().toString();
}

@GET
@Path("/serverinfos")
@Operation(
summary = "Returns current Litecoin server configuration",
description = "Returns current Litecoin server locations and use status",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ServerConfigurationInfo.class
)
)
)
}
)
public ServerConfigurationInfo getServerConfiguration() {

return CrossChainUtils.buildServerConfigurationInfo(Litecoin.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.qortal.crosschain.ForeignBlockchainException;
import org.qortal.crosschain.PirateChain;
import org.qortal.crosschain.SimpleTransaction;
import org.qortal.crosschain.ServerConfigurationInfo;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -329,4 +330,25 @@ public String getPirateChainSyncStatus(@HeaderParam(Security.API_KEY_HEADER) Str
throw ApiExceptionFactory.INSTANCE.createCustomException(request, ApiError.FOREIGN_BLOCKCHAIN_NETWORK_ISSUE, e.getMessage());
}
}

@GET
@Path("/serverinfos")
@Operation(
summary = "Returns current PirateChain server configuration",
description = "Returns current PirateChain server locations and use status",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ServerConfigurationInfo.class
)
)
)
}
)
public ServerConfigurationInfo getServerConfiguration() {

return CrossChainUtils.buildServerConfigurationInfo(PirateChain.getInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.qortal.crosschain.ForeignBlockchainException;
import org.qortal.crosschain.Ravencoin;
import org.qortal.crosschain.SimpleTransaction;
import org.qortal.crosschain.ServerConfigurationInfo;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -283,4 +284,24 @@ public String sendBitcoin(@HeaderParam(Security.API_KEY_HEADER) String apiKey, R
return spendTransaction.getTxId().toString();
}

@GET
@Path("/serverinfos")
@Operation(
summary = "Returns current Ravencoin server configuration",
description = "Returns current Ravencoin server locations and use status",
responses = {
@ApiResponse(
content = @Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(
implementation = ServerConfigurationInfo.class
)
)
)
}
)
public ServerConfigurationInfo getServerConfiguration() {

return CrossChainUtils.buildServerConfigurationInfo(Ravencoin.getInstance());
}
}
47 changes: 47 additions & 0 deletions src/main/java/org/qortal/api/resource/CrossChainUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.qortal.api.resource;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.qortal.crosschain.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CrossChainUtils {
private static final Logger LOGGER = LogManager.getLogger(CrossChainUtils.class);

public static ServerConfigurationInfo buildServerConfigurationInfo(Bitcoiny blockchain) {

BitcoinyBlockchainProvider blockchainProvider = blockchain.getBlockchainProvider();
ChainableServer currentServer = blockchainProvider.getCurrentServer();

return new ServerConfigurationInfo(
buildInfos(blockchainProvider.getServers(), currentServer),
buildInfos(blockchainProvider.getRemainingServers(), currentServer),
buildInfos(blockchainProvider.getUselessServers(), currentServer)
);
}

public static ServerInfo buildInfo(ChainableServer server, boolean isCurrent) {
return new ServerInfo(
server.averageResponseTime(),
server.getHostName(),
server.getPort(),
server.getConnectionType().toString(),
isCurrent);

}

public static List<ServerInfo> buildInfos(Collection<ChainableServer> servers, ChainableServer currentServer) {

List<ServerInfo> infos = new ArrayList<>( servers.size() );

for( ChainableServer server : servers )
{
infos.add(buildInfo(server, server.equals(currentServer)));
}

return infos;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/qortal/crosschain/Bitcoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.bitcoinj.params.RegTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.qortal.crosschain.ElectrumX.Server;
import org.qortal.crosschain.ElectrumX.Server.ConnectionType;
import org.qortal.crosschain.ChainableServer.ConnectionType;
import org.qortal.settings.Settings;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cash.z.wallet.sdk.rpc.CompactFormats.CompactBlock;

import java.util.List;
import java.util.Set;

public abstract class BitcoinyBlockchainProvider {

Expand Down Expand Up @@ -59,4 +60,11 @@ public abstract class BitcoinyBlockchainProvider {
/** Broadcasts raw, serialized, transaction bytes to network, returning success/failure. */
public abstract void broadcastTransaction(byte[] rawTransaction) throws ForeignBlockchainException;

public abstract Set<ChainableServer> getServers();

public abstract List<ChainableServer> getRemainingServers();

public abstract Set<ChainableServer> getUselessServers();

public abstract ChainableServer getCurrentServer();
}
15 changes: 15 additions & 0 deletions src/main/java/org/qortal/crosschain/ChainableServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.qortal.crosschain;

public interface ChainableServer {
public void addResponseTime(long responseTime);

public long averageResponseTime();

public String getHostName();

public int getPort();

public ConnectionType getConnectionType();

public enum ConnectionType {TCP, SSL}
}
2 changes: 1 addition & 1 deletion src/main/java/org/qortal/crosschain/Digibyte.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.bitcoinj.params.TestNet3Params;
import org.libdohj.params.DigibyteMainNetParams;
import org.qortal.crosschain.ElectrumX.Server;
import org.qortal.crosschain.ElectrumX.Server.ConnectionType;
import org.qortal.crosschain.ChainableServer.ConnectionType;
import org.qortal.settings.Settings;

import java.util.Arrays;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/qortal/crosschain/Dogecoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.libdohj.params.DogecoinMainNetParams;
import org.libdohj.params.DogecoinTestNet3Params;
import org.qortal.crosschain.ElectrumX.Server;
import org.qortal.crosschain.ElectrumX.Server.ConnectionType;
import org.qortal.crosschain.ChainableServer.ConnectionType;
import org.qortal.settings.Settings;

import java.util.Arrays;
Expand Down
Loading

0 comments on commit e118f4a

Please sign in to comment.