From 136bd532a8dec253012e96445f9a7c5864e80c70 Mon Sep 17 00:00:00 2001 From: Tony Wu Date: Sun, 20 Aug 2023 02:39:35 +0100 Subject: [PATCH] refactor(BloccConnections): encapsulate channels in BloccConnections --- .../ApprovedTransactionConfiguration.java | 7 +++--- .../dashboard/fabric/BloccConnections.java | 22 +++++++++++++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/uk/ac/ic/doc/blocc/dashboard/approvedtransaction/ApprovedTransactionConfiguration.java b/src/main/java/uk/ac/ic/doc/blocc/dashboard/approvedtransaction/ApprovedTransactionConfiguration.java index cd6646b..5a59ee4 100644 --- a/src/main/java/uk/ac/ic/doc/blocc/dashboard/approvedtransaction/ApprovedTransactionConfiguration.java +++ b/src/main/java/uk/ac/ic/doc/blocc/dashboard/approvedtransaction/ApprovedTransactionConfiguration.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.concurrent.CompletableFuture; import org.hyperledger.fabric.client.CloseableIterator; -import org.hyperledger.fabric.client.Gateway; import org.hyperledger.fabric.client.Network; import org.hyperledger.fabric.protos.common.Block; import org.hyperledger.fabric.protos.common.Envelope; @@ -41,13 +40,13 @@ public ApprovedTransactionConfiguration(ApprovedTransactionService service, CommandLineRunner commandLineRunner() { return args -> { - Gateway gateway = connections.getGateway(); - // TODO: read from external file List availableChannelNums = List.of(5); + connections.connectToChannels(availableChannelNums); + availableChannelNums.forEach(availableChannelNum -> { - Network channel = gateway.getNetwork(String.format("channel%d", availableChannelNum)); + Network channel = connections.getChannel(availableChannelNum); CloseableIterator blockEvents = channel.newBlockEventsRequest().startBlock(0).build().getEvents(); diff --git a/src/main/java/uk/ac/ic/doc/blocc/dashboard/fabric/BloccConnections.java b/src/main/java/uk/ac/ic/doc/blocc/dashboard/fabric/BloccConnections.java index 9c71e54..d75697d 100644 --- a/src/main/java/uk/ac/ic/doc/blocc/dashboard/fabric/BloccConnections.java +++ b/src/main/java/uk/ac/ic/doc/blocc/dashboard/fabric/BloccConnections.java @@ -13,9 +13,12 @@ import java.security.PrivateKey; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.stream.Stream; import org.hyperledger.fabric.client.Gateway; +import org.hyperledger.fabric.client.Network; import org.hyperledger.fabric.client.identity.Identities; import org.hyperledger.fabric.client.identity.Identity; import org.hyperledger.fabric.client.identity.Signer; @@ -45,6 +48,8 @@ public class BloccConnections implements DisposableBean { private final Gateway gateway; private final ManagedChannel grcpChannel; + private final Map channels = new HashMap<>(); + /** * Establish gRPC connection to create a Gateway, which can be used to connect to the BLOCC * network. The following environmental variables need to be provided: @@ -165,8 +170,21 @@ private Path getPrivateKeyPath() throws IOException { } } - public Gateway getGateway() { - return gateway; + public void connectToChannel(int channelNum) { + String channelName = String.format("channel%d", channelNum); + if (channels.containsKey(channelNum)) { + throw new IllegalArgumentException(String.format("%s already exists", channelName)); + } + + channels.put(channelNum, gateway.getNetwork(channelName)); + } + + public Network getChannel(int channelNum) { + return channels.get(channelNum); + } + + public void connectToChannels(Iterable channelNums) { + channelNums.forEach(this::connectToChannel); } @Override