Search in sources :

Example 6 with HFClient

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

the class NetworkConfigIT method deployChaincodeIfRequired.

// Determines whether or not the chaincode has been deployed and deploys it if necessary
private static void deployChaincodeIfRequired() throws Exception {
    // //////////////////////////
    // Setup client
    HFClient client = getTheClient();
    Channel channel = constructChannel(client, FOO_CHANNEL_NAME);
    // Use any old peer...
    Peer peer = channel.getPeers().iterator().next();
    if (!checkInstantiatedChaincode(channel, peer, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION)) {
        // The chaincode we require does not exist, so deploy it...
        deployChaincode(client, channel, CHAIN_CODE_NAME, CHAIN_CODE_PATH, CHAIN_CODE_VERSION);
    }
}
Also used : Channel(org.hyperledger.fabric.sdk.Channel) Peer(org.hyperledger.fabric.sdk.Peer) HFClient(org.hyperledger.fabric.sdk.HFClient)

Example 7 with HFClient

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

the class NetworkConfigIT method getTheClient.

// Returns a new client instance
private static HFClient getTheClient() throws Exception {
    HFClient client = HFClient.createNewInstance();
    client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
    User peerAdmin = getAdminUser(TEST_ORG);
    client.setUserContext(peerAdmin);
    return client;
}
Also used : MockUser(org.hyperledger.fabric.sdk.testutils.TestUtils.MockUser) User(org.hyperledger.fabric.sdk.User) HFClient(org.hyperledger.fabric.sdk.HFClient)

Example 8 with HFClient

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

the class NetworkConfigIT method testUpdate1.

@Test
public void testUpdate1() throws Exception {
    // Setup client and channel instances
    HFClient client = getTheClient();
    Channel channel = constructChannel(client, FOO_CHANNEL_NAME);
    final ChaincodeID chaincodeID = ChaincodeID.newBuilder().setName(CHAIN_CODE_NAME).setVersion(CHAIN_CODE_VERSION).setPath(CHAIN_CODE_PATH).build();
    final String channelName = channel.getName();
    out("Running testUpdate1 - Channel %s", channelName);
    int moveAmount = 5;
    String originalVal = queryChaincodeForCurrentValue(client, channel, chaincodeID);
    String newVal = "" + (Integer.parseInt(originalVal) + moveAmount);
    out("Original value = %s", originalVal);
    // Move some assets
    moveAmount(client, channel, chaincodeID, "a", "b", "" + moveAmount, null).thenApply(transactionEvent -> {
        // Check that they were moved
        queryChaincodeForExpectedValue(client, channel, newVal, chaincodeID);
        return null;
    }).thenApply(transactionEvent -> {
        // Move them back
        try {
            return moveAmount(client, channel, chaincodeID, "b", "a", "" + moveAmount, null).get(testConfig.getTransactionWaitTime(), TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }).thenApply(transactionEvent -> {
        // Check that they were moved back
        queryChaincodeForExpectedValue(client, channel, originalVal, chaincodeID);
        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);
    // Force channel to shutdown clean up resources.
    channel.shutdown(true);
    out("testUpdate1 - done");
}
Also used : InstantiateProposalRequest(org.hyperledger.fabric.sdk.InstantiateProposalRequest) Map(java.util.Map) TestConfigHelper(org.hyperledger.fabric.sdk.TestConfigHelper) Assert.fail(org.junit.Assert.fail) Orderer(org.hyperledger.fabric.sdk.Orderer) InvalidArgumentException(org.hyperledger.fabric.sdk.exception.InvalidArgumentException) MockUser(org.hyperledger.fabric.sdk.testutils.TestUtils.MockUser) 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) PrivateKey(java.security.PrivateKey) Peer(org.hyperledger.fabric.sdk.Peer) Status(org.hyperledger.fabric.sdk.ChaincodeResponse.Status) BeforeClass(org.junit.BeforeClass) ChaincodeID(org.hyperledger.fabric.sdk.ChaincodeID) HFClient(org.hyperledger.fabric.sdk.HFClient) TransactionEvent(org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) QueryByChaincodeRequest(org.hyperledger.fabric.sdk.QueryByChaincodeRequest) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) BlockEvent(org.hyperledger.fabric.sdk.BlockEvent) LinkedList(java.util.LinkedList) SDKUtils(org.hyperledger.fabric.sdk.SDKUtils) HFCAClient(org.hyperledger.fabric_ca.sdk.HFCAClient) NetworkConfig(org.hyperledger.fabric.sdk.NetworkConfig) InstallProposalRequest(org.hyperledger.fabric.sdk.InstallProposalRequest) HFCAInfo(org.hyperledger.fabric_ca.sdk.HFCAInfo) ChaincodeEndorsementPolicy(org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy) ProposalResponse(org.hyperledger.fabric.sdk.ProposalResponse) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CAInfo(org.hyperledger.fabric.sdk.NetworkConfig.CAInfo) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CryptoSuite(org.hyperledger.fabric.sdk.security.CryptoSuite) Assert.assertNull(org.junit.Assert.assertNull) TestUtils.resetConfig(org.hyperledger.fabric.sdk.testutils.TestUtils.resetConfig) Assert.assertEquals(org.junit.Assert.assertEquals) TransactionProposalRequest(org.hyperledger.fabric.sdk.TransactionProposalRequest) TransactionEvent(org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent) TransactionEventException(org.hyperledger.fabric.sdk.exception.TransactionEventException) Channel(org.hyperledger.fabric.sdk.Channel) CompletionException(java.util.concurrent.CompletionException) HFClient(org.hyperledger.fabric.sdk.HFClient) ChaincodeID(org.hyperledger.fabric.sdk.ChaincodeID) 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) Test(org.junit.Test)

Aggregations

HFClient (org.hyperledger.fabric.sdk.HFClient)8 Channel (org.hyperledger.fabric.sdk.Channel)7 File (java.io.File)4 Test (org.junit.Test)4 String.format (java.lang.String.format)3 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 Set (java.util.Set)3 TimeUnit (java.util.concurrent.TimeUnit)3 BlockEvent (org.hyperledger.fabric.sdk.BlockEvent)3 ChaincodeEndorsementPolicy (org.hyperledger.fabric.sdk.ChaincodeEndorsementPolicy)3 ChaincodeID (org.hyperledger.fabric.sdk.ChaincodeID)3 Peer (org.hyperledger.fabric.sdk.Peer)3 User (org.hyperledger.fabric.sdk.User)3 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2