Skip to content

Commit

Permalink
Addition of API to get packageId for a chaincode
Browse files Browse the repository at this point in the history
- REST API and service to get current packageId installed in the peers

Signed-off-by: Puneet Joshi <[email protected]>
  • Loading branch information
jopuneet authored and arsulegai committed Mar 14, 2022
1 parent 59d6f00 commit 0f1d3b3
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,15 @@ public ResponseEntity<String> getCurrentSequence(
chaincodeOperationsService.getCurrentSequence(networkName, chaincodeName, chaincodeVersion),
HttpStatus.OK);
}

@GetMapping(value = "/chaincode/packageId")
public ResponseEntity<String> getCurrentPackageId(
@RequestParam("network_name") @Validated String networkName,
@RequestParam("chaincode_name") @Validated String chaincodeName,
@RequestParam("chaincode_version") @Validated String chaincodeVersion) {
return new ResponseEntity<>(
chaincodeOperationsService.getCurrentPackageId(
networkName, chaincodeName, chaincodeVersion),
HttpStatus.OK);
}
}
3 changes: 3 additions & 0 deletions src/main/java/hlf/java/rest/client/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public enum ErrorCode {
SEQUENCE_NUMBER_VALIDATION_FAILED(
1004, "The sequence number is not same in all the peers or not present."),

CHAINCODE_PACKAGE_ID_VALIDATION_FAILED(
1005, "The chaincode packageId is not same in all the peers or not present."),

HYPERLEDGER_FABRIC_CONNECTION_ERROR(5000, "Hyperledger Fabric connection related error"),

HYPERLEDGER_FABRIC_CHANNEL_TXN_ERROR(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,36 @@

public interface ChaincodeOperationsService {

/**
* Perform chaincode operations passed in the arguments.
*
* @param networkName the network name
* @param chaincodeOperationsModel the chaincode operations model
* @param operationsType the operations type
* @return the transactionId
*/
String performChaincodeOperation(
String networkName,
ChaincodeOperations chaincodeOperationsModel,
ChaincodeOperationsType operationsType);

/**
* Gets current sequence for the channel and chaincode.
*
* @param networkName the network name
* @param chaincodeName the chaincode name
* @param chaincodeVersion the chaincode version
* @return the current sequence
*/
String getCurrentSequence(String networkName, String chaincodeName, String chaincodeVersion);

/**
* Gets current package id for the passed channel and chaincode.
*
* @param networkName the network name
* @param chaincodeName the chaincode name
* @param chaincodeVersion the chaincode version
* @return the current package id
*/
String getCurrentPackageId(String networkName, String chaincodeName, String chaincodeVersion);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hlf.java.rest.client.service.impl;

import static hlf.java.rest.client.exception.ErrorCode.CHAINCODE_PACKAGE_ID_VALIDATION_FAILED;
import static hlf.java.rest.client.exception.ErrorCode.SEQUENCE_NUMBER_VALIDATION_FAILED;
import static hlf.java.rest.client.model.ChaincodeOperationsType.approve;
import static java.util.Objects.isNull;
Expand All @@ -25,6 +26,7 @@
import org.hyperledger.fabric.sdk.LifecycleCommitChaincodeDefinitionProposalResponse;
import org.hyperledger.fabric.sdk.LifecycleCommitChaincodeDefinitionRequest;
import org.hyperledger.fabric.sdk.LifecycleQueryChaincodeDefinitionProposalResponse;
import org.hyperledger.fabric.sdk.LifecycleQueryInstalledChaincodesProposalResponse;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.QueryLifecycleQueryChaincodeDefinitionRequest;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
Expand Down Expand Up @@ -113,6 +115,49 @@ public String getCurrentSequence(
}
}

@Override
public String getCurrentPackageId(
String networkName, String chaincodeName, String chaincodeVersion) {

Network network = gateway.getNetwork(networkName);
Channel channel = network.getChannel();

Collection<Peer> peers = channel.getPeers();

try {
Collection<LifecycleQueryInstalledChaincodesProposalResponse> results =
hfClient.sendLifecycleQueryInstalledChaincodes(
hfClient.newLifecycleQueryInstalledChaincodesRequest(), peers);
Set<String> packageIds = new HashSet<>();
for (LifecycleQueryInstalledChaincodesProposalResponse peerResults : results) {
for (LifecycleQueryInstalledChaincodesProposalResponse
.LifecycleQueryInstalledChaincodesResult
lifecycleQueryInstalledChaincodeResult :
peerResults.getLifecycleQueryInstalledChaincodesResult()) {
packageIds.add(lifecycleQueryInstalledChaincodeResult.getPackageId());
}
}

if (packageIds.size() == 0) {
throw new ServiceException(
CHAINCODE_PACKAGE_ID_VALIDATION_FAILED,
"Chaincode PackageId not present in peers for channel: " + networkName);
}

// packageIds will be same in all fabric peers as per design
if (packageIds.size() > 1) {
throw new ServiceException(
CHAINCODE_PACKAGE_ID_VALIDATION_FAILED,
"Different packageIds present in peers for channel: " + networkName);
}

return packageIds.stream().findFirst().get();
} catch (InvalidArgumentException | ProposalException e) {
throw new ServiceException(
ErrorCode.HYPERLEDGER_FABRIC_CHAINCODE_OPERATIONS_REQUEST_REJECTION, e.getMessage(), e);
}
}

private String approveChaincode(
HFClient hfClient, Channel channel, ChaincodeOperations chaincodeOperationsModel) {

Expand Down

0 comments on commit 0f1d3b3

Please sign in to comment.