All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hyperledger.fabric.sdk.LifecycleCheckCommitReadinessProposalResponse Maven / Gradle / Ivy

Go to download

Java SDK for Hyperledger Fabric. Deprecated as of Fabric v2.5, replaced by org.hyperledger.fabric:fabric-gateway.

There is a newer version: 2.2.26
Show newest version
/*
 *
 * Copyright IBM Corp. All Rights Reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 * /
 */

package org.hyperledger.fabric.sdk;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.protobuf.ByteString;
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage;
import org.hyperledger.fabric.protos.peer.lifecycle.Lifecycle;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.transaction.TransactionContext;

import static java.lang.String.format;

/**
 * Returns the response for a LifecycleCheckCommitReadinessStatus showing what organizations have or have not approved yet.
 */
public final class LifecycleCheckCommitReadinessProposalResponse extends ProposalResponse {
    private Lifecycle.CheckCommitReadinessResult checkCommitReadinessResult;

    LifecycleCheckCommitReadinessProposalResponse(TransactionContext transactionContext, int status, String message) {
        super(transactionContext, status, message);
    }

    public Lifecycle.CheckCommitReadinessResult getApprovalStatusResults() throws ProposalException {
        if (null == checkCommitReadinessResult) {
            if (getStatus() != Status.SUCCESS) {
                throw new ProposalException(format("Fabric response failed on peer %s  %s", getPeer(), getMessage()));
            }

            ProposalResponsePackage.ProposalResponse fabricResponse = getProposalResponse();
            if (null == fabricResponse) {
                throw new ProposalException(format("Proposal has no Fabric response. %s", getPeer()));
            }

            ByteString payload = fabricResponse.getResponse().getPayload();
            if (payload == null) {
                throw new ProposalException(format("Fabric response has no payload  %s", getPeer()));
            }

            try {
                checkCommitReadinessResult = Lifecycle.CheckCommitReadinessResult.parseFrom(payload);
            } catch (Exception e) {
                throw new ProposalException(format("Failure on peer %s %s", getPeer(), e.getMessage()), e);
            }
        }

        return checkCommitReadinessResult;
    }

    /**
     * The set of organizations that hav approved this chaincode definition.
     *
     * @return
     * @throws ProposalException
     */
    public Set getApprovedOrgs() throws ProposalException {
        return getApprovalsMap().entrySet().stream()
                .filter(Map.Entry::getValue)
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());
    }

    /**
     * The set of organizations that have not approved this chaincode definition.
     *
     * @return
     * @throws ProposalException
     */
    public Set getUnApprovedOrgs() throws ProposalException {
        return getApprovalsMap().entrySet().stream()
                .filter(entry -> !entry.getValue())
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());
    }

    /**
     * A map of approved and not approved. The key contains name of org the value a Boolean if approved.
     *
     * @return
     * @throws ProposalException
     */
    public Map getApprovalsMap() throws ProposalException {
        final Lifecycle.CheckCommitReadinessResult approvalStatusResults = getApprovalStatusResults();
        return approvalStatusResults == null ? Collections.emptyMap() : approvalStatusResults.getApprovalsMap();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy