Search in sources :

Example 6 with ProposalResponse

use of org.hyperledger.fabric.sdk.ProposalResponse in project fabric-sdk-java by hyperledger.

the class End2endAndBackAgainIT method queryChaincodeForExpectedValue.

private void queryChaincodeForExpectedValue(HFClient client, Channel channel, final String expect, ChaincodeID chaincodeID) {
    out("Now query chaincode %s on channel %s for the value of b expecting to see: %s", chaincodeID, channel.getName(), expect);
    QueryByChaincodeRequest queryByChaincodeRequest = client.newQueryProposalRequest();
    // test using bytes as args. End2end uses Strings.
    queryByChaincodeRequest.setArgs("b".getBytes(UTF_8));
    queryByChaincodeRequest.setFcn("query");
    queryByChaincodeRequest.setChaincodeID(chaincodeID);
    Collection<ProposalResponse> queryProposals;
    try {
        queryProposals = channel.queryByChaincode(queryByChaincodeRequest);
    } catch (Exception e) {
        throw new CompletionException(e);
    }
    for (ProposalResponse proposalResponse : queryProposals) {
        if (!proposalResponse.isVerified() || proposalResponse.getStatus() != Status.SUCCESS) {
            fail("Failed query proposal from peer " + proposalResponse.getPeer().getName() + " status: " + proposalResponse.getStatus() + ". Messages: " + proposalResponse.getMessage() + ". Was verified : " + proposalResponse.isVerified());
        } else {
            String payload = proposalResponse.getProposalResponse().getResponse().getPayload().toStringUtf8();
            out("Query payload of b from peer %s returned %s", proposalResponse.getPeer().getName(), payload);
            assertEquals(format("Failed compare on channel %s chaincode id %s expected value:'%s', but got:'%s'", channel.getName(), chaincodeID, expect, payload), expect, payload);
        }
    }
}
Also used : QueryByChaincodeRequest(org.hyperledger.fabric.sdk.QueryByChaincodeRequest) CompletionException(java.util.concurrent.CompletionException) ProposalResponse(org.hyperledger.fabric.sdk.ProposalResponse) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) CompletionException(java.util.concurrent.CompletionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) MalformedURLException(java.net.MalformedURLException)

Example 7 with ProposalResponse

use of org.hyperledger.fabric.sdk.ProposalResponse in project fabric-sdk-java by hyperledger.

the class End2endAndBackAgainIT method runChannel.

// Disable MethodLength as this method is for instructional purposes and hence
// we don't want to split it into smaller pieces
// CHECKSTYLE:OFF: MethodLength
void runChannel(HFClient client, Channel channel, SampleOrg sampleOrg, final int delta) {
    final String channelName = channel.getName();
    try {
        client.setUserContext(sampleOrg.getUser(TESTUSER_1_NAME));
        // final boolean changeContext = false; // BAR_CHANNEL_NAME.equals(channel.getName()) ? true : false;
        final boolean changeContext = BAR_CHANNEL_NAME.equals(channel.getName());
        out("Running Channel %s with a delta %d", channelName, delta);
        out("ChaincodeID: ", chaincodeID);
        // //////////////////////////
        // Send Query Proposal to all peers see if it's what we expect from end of End2endIT
        // 
        queryChaincodeForExpectedValue(client, channel, "" + (300 + delta), chaincodeID);
        // Set user context on client but use explicit user contest on each call.
        if (changeContext) {
            client.setUserContext(sampleOrg.getUser(TESTUSER_1_NAME));
        }
        // exercise v1 of chaincode
        moveAmount(client, channel, chaincodeID, "25", changeContext ? sampleOrg.getPeerAdmin() : null).thenApply((BlockEvent.TransactionEvent transactionEvent) -> {
            try {
                waitOnFabric();
                client.setUserContext(sampleOrg.getUser(TESTUSER_1_NAME));
                queryChaincodeForExpectedValue(client, channel, "" + (325 + delta), chaincodeID);
                // ////////////////
                // Start of upgrade first must install it.
                client.setUserContext(sampleOrg.getPeerAdmin());
                // /////////////
                // //
                InstallProposalRequest installProposalRequest = client.newInstallProposalRequest();
                installProposalRequest.setChaincodeID(chaincodeID);
                // //For GO language and serving just a single user, chaincodeSource is mostly likely the users GOPATH
                installProposalRequest.setChaincodeSourceLocation(Paths.get(TEST_FIXTURES_PATH, CHAIN_CODE_FILEPATH).toFile());
                installProposalRequest.setChaincodeVersion(CHAIN_CODE_VERSION_11);
                installProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
                installProposalRequest.setChaincodeLanguage(CHAIN_CODE_LANG);
                if (changeContext) {
                    installProposalRequest.setUserContext(sampleOrg.getPeerAdmin());
                }
                out("Sending install proposal for channel: %s", channel.getName());
                // //////////////////////////
                // only a client from the same org as the peer can issue an install request
                int numInstallProposal = 0;
                Collection<ProposalResponse> responses;
                final Collection<ProposalResponse> successful = new LinkedList<>();
                final Collection<ProposalResponse> failed = new LinkedList<>();
                Collection<Peer> peersFromOrg = channel.getPeers();
                numInstallProposal = numInstallProposal + peersFromOrg.size();
                responses = client.sendInstallProposal(installProposalRequest, peersFromOrg);
                for (ProposalResponse response : responses) {
                    if (response.getStatus() == Status.SUCCESS) {
                        out("Successful install proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName());
                        successful.add(response);
                    } else {
                        failed.add(response);
                    }
                }
                out("Received %d install proposal responses. Successful+verified: %d . Failed: %d", numInstallProposal, successful.size(), failed.size());
                if (failed.size() > 0) {
                    ProposalResponse first = failed.iterator().next();
                    fail("Not enough endorsers for install :" + successful.size() + ".  " + first.getMessage());
                }
                if (changeContext) {
                    installProposalRequest.setUserContext(sampleOrg.getPeerAdmin());
                }
                UpgradeProposalRequest upgradeProposalRequest = client.newUpgradeProposalRequest();
                upgradeProposalRequest.setChaincodeID(chaincodeID_11);
                upgradeProposalRequest.setProposalWaitTime(testConfig.getProposalWaitTime());
                upgradeProposalRequest.setFcn("init");
                // no arguments don't change the ledger see chaincode.
                upgradeProposalRequest.setArgs(new String[] {});
                ChaincodeEndorsementPolicy chaincodeEndorsementPolicy;
                chaincodeEndorsementPolicy = new ChaincodeEndorsementPolicy();
                chaincodeEndorsementPolicy.fromYamlFile(new File(TEST_FIXTURES_PATH + "/sdkintegration/chaincodeendorsementpolicy.yaml"));
                upgradeProposalRequest.setChaincodeEndorsementPolicy(chaincodeEndorsementPolicy);
                Map<String, byte[]> tmap = new HashMap<>();
                tmap.put("test", "data".getBytes());
                upgradeProposalRequest.setTransientMap(tmap);
                if (changeContext) {
                    upgradeProposalRequest.setUserContext(sampleOrg.getPeerAdmin());
                }
                out("Sending upgrade proposal");
                Collection<ProposalResponse> responses2;
                responses2 = channel.sendUpgradeProposal(upgradeProposalRequest);
                successful.clear();
                failed.clear();
                for (ProposalResponse response : responses2) {
                    if (response.getStatus() == Status.SUCCESS) {
                        out("Successful upgrade proposal response Txid: %s from peer %s", response.getTransactionID(), response.getPeer().getName());
                        successful.add(response);
                    } else {
                        failed.add(response);
                    }
                }
                out("Received %d upgrade proposal responses. Successful+verified: %d . Failed: %d", channel.getPeers().size(), successful.size(), failed.size());
                if (failed.size() > 0) {
                    ProposalResponse first = failed.iterator().next();
                    throw new AssertionError("Not enough endorsers for upgrade :" + successful.size() + ".  " + first.getMessage());
                }
                if (changeContext) {
                    return channel.sendTransaction(successful, sampleOrg.getPeerAdmin()).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);
                } else {
                    return channel.sendTransaction(successful).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);
                }
            } catch (CompletionException e) {
                throw e;
            } catch (Exception e) {
                throw new CompletionException(e);
            }
        }).thenApply(transactionEvent -> {
            try {
                waitOnFabric(10000);
                out("Chaincode has been upgraded to version %s", CHAIN_CODE_VERSION_11);
                // Check to see if peers have new chaincode and old chaincode is gone.
                client.setUserContext(sampleOrg.getPeerAdmin());
                for (Peer peer : channel.getPeers()) {
                    if (!checkInstalledChaincode(client, peer, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION_11)) {
                        throw new AssertionError(format("Peer %s is missing chaincode name:%s, path:%s, version: %s", peer.getName(), CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_PATH));
                    }
                    // should be instantiated too..
                    if (!checkInstantiatedChaincode(channel, peer, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION_11)) {
                        throw new AssertionError(format("Peer %s is missing instantiated chaincode name:%s, path:%s, version: %s", peer.getName(), CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_PATH));
                    }
                    if (checkInstantiatedChaincode(channel, peer, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION)) {
                        throw new AssertionError(format("Peer %s still has old instantiated chaincode name:%s, path:%s, version: %s", peer.getName(), CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_PATH));
                    }
                }
                client.setUserContext(sampleOrg.getUser(TESTUSER_1_NAME));
                // /Check if we still get the same value on the ledger
                out("delta is %s", delta);
                queryChaincodeForExpectedValue(client, channel, "" + (325 + delta), chaincodeID);
                // Now lets run the new chaincode which should *double* the results we asked to move.
                return moveAmount(client, channel, chaincodeID_11, "50", changeContext ? sampleOrg.getPeerAdmin() : null).get(testConfig.getTransactionWaitTime(), // really move 100
                TimeUnit.SECONDS);
            } catch (CompletionException e) {
                throw e;
            } catch (Exception e) {
                throw new CompletionException(e);
            }
        }).thenApply(transactionEvent -> {
            waitOnFabric(10000);
            queryChaincodeForExpectedValue(client, channel, "" + (425 + delta), chaincodeID_11);
            return null;
        }).exceptionally(e -> {
            if (e instanceof CompletionException && e.getCause() != null) {
                e = e.getCause();
            }
            if (e instanceof TransactionEventException) {
                BlockEvent.TransactionEvent te = ((TransactionEventException) e).getTransactionEvent();
                if (te != null) {
                    e.printStackTrace(System.err);
                    fail(format("Transaction with txid %s failed. %s", te.getTransactionID(), e.getMessage()));
                }
            }
            e.printStackTrace(System.err);
            fail(format("Test failed with %s exception %s", e.getClass().getName(), e.getMessage()));
            return null;
        }).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    out("Running for Channel %s done", channelName);
}
Also used : UpgradeProposalRequest(org.hyperledger.fabric.sdk.UpgradeProposalRequest) BlockInfo(org.hyperledger.fabric.sdk.BlockInfo) Map(java.util.Map) TestConfigHelper(org.hyperledger.fabric.sdk.TestConfigHelper) Assert.fail(org.junit.Assert.fail) PeerOptions(org.hyperledger.fabric.sdk.Channel.PeerOptions) EnumSet(java.util.EnumSet) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) TestConfig(org.hyperledger.fabric.sdk.testutils.TestConfig) TestUtils(org.hyperledger.fabric.sdk.testutils.TestUtils) Collection(java.util.Collection) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) Channel(org.hyperledger.fabric.sdk.Channel) Set(java.util.Set) User(org.hyperledger.fabric.sdk.User) CompletionException(java.util.concurrent.CompletionException) ChaincodeInfo(org.hyperledger.fabric.protos.peer.Query.ChaincodeInfo) String.format(java.lang.String.format) List(java.util.List) Peer(org.hyperledger.fabric.sdk.Peer) Assert.assertFalse(org.junit.Assert.assertFalse) Status(org.hyperledger.fabric.sdk.ChaincodeResponse.Status) ChaincodeEvent(org.hyperledger.fabric.sdk.ChaincodeEvent) TransactionRequest(org.hyperledger.fabric.sdk.TransactionRequest) ChaincodeID(org.hyperledger.fabric.sdk.ChaincodeID) EventHub(org.hyperledger.fabric.sdk.EventHub) HFClient(org.hyperledger.fabric.sdk.HFClient) PeerRole(org.hyperledger.fabric.sdk.Peer.PeerRole) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) QueryByChaincodeRequest(org.hyperledger.fabric.sdk.QueryByChaincodeRequest) ArrayList(java.util.ArrayList) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) BlockchainInfo(org.hyperledger.fabric.sdk.BlockchainInfo) BlockEvent(org.hyperledger.fabric.sdk.BlockEvent) LinkedList(java.util.LinkedList) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) Before(org.junit.Before) InstallProposalRequest(org.hyperledger.fabric.sdk.InstallProposalRequest) Properties(java.util.Properties) ChaincodeEndorsementPolicy(org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy) ProposalResponse(org.hyperledger.fabric.sdk.ProposalResponse) MalformedURLException(java.net.MalformedURLException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) PeerOptions.createPeerOptions(org.hyperledger.fabric.sdk.Channel.PeerOptions.createPeerOptions) Configtx(org.hyperledger.fabric.protos.common.Configtx) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) CryptoSuite(org.hyperledger.fabric.sdk.security.CryptoSuite) Assert.assertNull(org.junit.Assert.assertNull) Paths(java.nio.file.Paths) TestUtils.resetConfig(org.hyperledger.fabric.sdk.testutils.TestUtils.resetConfig) TRANSACTION_ENVELOPE(org.hyperledger.fabric.sdk.BlockInfo.EnvelopeType.TRANSACTION_ENVELOPE) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TransactionProposalRequest(org.hyperledger.fabric.sdk.TransactionProposalRequest) ChaincodeEndorsementPolicy(org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy) HashMap(java.util.HashMap) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) Peer(org.hyperledger.fabric.sdk.Peer) LinkedList(java.util.LinkedList) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) ProposalException(org.hyperledger.fabric.sdk.exception.ProposalException) CompletionException(java.util.concurrent.CompletionException) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) MalformedURLException(java.net.MalformedURLException) InstallProposalRequest(org.hyperledger.fabric.sdk.InstallProposalRequest) UpgradeProposalRequest(org.hyperledger.fabric.sdk.UpgradeProposalRequest) CompletionException(java.util.concurrent.CompletionException) ProposalResponse(org.hyperledger.fabric.sdk.ProposalResponse) File(java.io.File)

Aggregations

ProposalResponse (org.hyperledger.fabric.sdk.ProposalResponse)7 ProposalException (org.hyperledger.fabric.sdk.exception.ProposalException)7 InvalidArgumentException (org.hyperledger.fabric.sdk.exception.InvalidArgumentException)6 TransactionEventException (org.hyperledger.fabric.sdk.exception.TransactionEventException)6 LinkedList (java.util.LinkedList)5 CompletionException (java.util.concurrent.CompletionException)5 MalformedURLException (java.net.MalformedURLException)4 QueryByChaincodeRequest (org.hyperledger.fabric.sdk.QueryByChaincodeRequest)4 File (java.io.File)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 ChaincodeEndorsementPolicy (org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy)3 ChaincodeID (org.hyperledger.fabric.sdk.ChaincodeID)3 InstallProposalRequest (org.hyperledger.fabric.sdk.InstallProposalRequest)3 TransactionProposalRequest (org.hyperledger.fabric.sdk.TransactionProposalRequest)3 String.format (java.lang.String.format)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2 Paths (java.nio.file.Paths)2 Collection (java.util.Collection)2 EnumSet (java.util.EnumSet)2