Java Code Examples for org.hyperledger.fabric.sdk.Channel#updateChannelConfiguration()

The following examples show how to use org.hyperledger.fabric.sdk.Channel#updateChannelConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateChannelIT.java    From fabric-sdk-java with Apache License 2.0 2 votes vote down vote up
@Test
public void test02SystemChannel() {

    try {

        ////////////////////////////
        //Reconstruct and run the channels
        //    SampleOrg sampleOrg = testConfig.getIntegrationTestsSampleOrg("peerOrg1");
        Channel channel = reconstructChannel(true, SYSTEM_CHANNEL_NAME, client, sampleOrg);

        assertTrue(channel.getPeers().isEmpty()); // no peers

        client.setUserContext(baduser);

        // Getting foo channels current configuration bytes.
        byte[] channelConfigurationBytes = channel.getChannelConfigurationBytes(ordererAdmin, channel.getOrderers().iterator().next());

        String originalConfigJson = configTxlatorDecode(httpclient, channelConfigurationBytes);

        assertTrue(originalConfigJson.matches(REGX_IS_SYSTEM_CHANNEL));  // verify is system channel

        //responseAsString is JSON but use just string operations for this test.

        if (!originalConfigJson.contains(ORIGINAL_BATCH_TIMEOUT)) {

            fail(format("Did not find expected batch timeout '%s', in:%s", ORIGINAL_BATCH_TIMEOUT, originalConfigJson));
        }

        byte[] reEncodedOriginalConfig = configTxLatorEncode(httpclient, originalConfigJson); // we need to get this to make sure the compare has encoding in the same way!

        //Now modify the batch timeout
        String updateString = originalConfigJson.replace(ORIGINAL_BATCH_TIMEOUT, UPDATED_BATCH_TIMEOUT);

        byte[] updatedConfigBytes = configTxLatorEncode(httpclient, updateString);

        // Now send to configtxlator multipart form post with original config bytes, updated config bytes and channel name.
        byte[] updateBytes = getChannelUpdateBytes(channel, reEncodedOriginalConfig, updatedConfigBytes);

        UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration(updateBytes);

        //To change the channel we need to sign with orderer admin certs which crypto gen stores:

        // client.setUserContext(ordererAdmin);
        //Ok now do actual channel update.
        channel.updateChannelConfiguration(ordererAdmin, updateChannelConfiguration,
                channel.getOrderers().iterator().next(),
                client.getUpdateChannelConfigurationSignature(updateChannelConfiguration, ordererAdmin));

        Thread.sleep(3000); // give time for events to happen

        //Let's add some additional verification...

        // client.setUserContext(sampleOrg.getPeerAdmin());
        client.setUserContext(baduser);

        final byte[] modChannelBytes = channel.getChannelConfigurationBytes(ordererAdmin);

        originalConfigJson = configTxlatorDecode(httpclient, modChannelBytes);

        if (!originalConfigJson.contains(UPDATED_BATCH_TIMEOUT)) {
            //If it doesn't have the updated time out it failed.
            fail(format("Did not find updated expected batch timeout '%s', in:%s", UPDATED_BATCH_TIMEOUT, originalConfigJson));
        }

        if (originalConfigJson.contains(ORIGINAL_BATCH_TIMEOUT)) { //Should not have been there anymore!

            fail(format("Found original batch timeout '%s', when it was not expected in:%s", ORIGINAL_BATCH_TIMEOUT, originalConfigJson));
        }

        out("That's all folks!");

    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}