Skip to content

Commit

Permalink
refactor(BloccConnections): encapsulate channels in BloccConnections
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyWu3027 committed Aug 20, 2023
1 parent f22c8c5 commit 136bd53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,13 +40,13 @@ public ApprovedTransactionConfiguration(ApprovedTransactionService service,
CommandLineRunner commandLineRunner() {
return args -> {

Gateway gateway = connections.getGateway();

// TODO: read from external file
List<Integer> 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<Block> blockEvents =
channel.newBlockEventsRequest().startBlock(0).build().getEvents();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +48,8 @@ public class BloccConnections implements DisposableBean {
private final Gateway gateway;
private final ManagedChannel grcpChannel;

private final Map<Integer, Network> 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:
Expand Down Expand Up @@ -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<Integer> channelNums) {
channelNums.forEach(this::connectToChannel);
}

@Override
Expand Down

0 comments on commit 136bd53

Please sign in to comment.