Search in sources :

Example 1 with UpdateChannelConfiguration

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

the class UpdateChannelIT method setup.

@Test
public void setup() {
    try {
        // //////////////////////////
        // Setup client
        // Create instance of client.
        HFClient client = HFClient.createNewInstance();
        client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
        // //////////////////////////
        // Set up USERS
        // Persistence is not part of SDK. Sample file store is for demonstration purposes only!
        // MUST be replaced with more robust application implementation  (Database, LDAP)
        File sampleStoreFile = new File(System.getProperty("java.io.tmpdir") + "/HFCSampletest.properties");
        sampleStoreFile.deleteOnExit();
        final SampleStore sampleStore = new SampleStore(sampleStoreFile);
        for (SampleOrg sampleOrg : testSampleOrgs) {
            final String orgName = sampleOrg.getName();
            sampleOrg.setPeerAdmin(sampleStore.getMember(orgName + "Admin", orgName));
        }
        // //////////////////////////
        // Reconstruct and run the channels
        SampleOrg sampleOrg = testConfig.getIntegrationTestsSampleOrg("peerOrg1");
        Channel fooChannel = reconstructChannel(FOO_CHANNEL_NAME, client, sampleOrg);
        // Getting foo channels current configuration bytes.
        final byte[] channelConfigurationBytes = fooChannel.getChannelConfigurationBytes();
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(CONFIGTXLATOR_LOCATION + "/protolator/decode/common.Config");
        httppost.setEntity(new ByteArrayEntity(channelConfigurationBytes));
        HttpResponse response = httpclient.execute(httppost);
        int statuscode = response.getStatusLine().getStatusCode();
        out("Got %s status for decoding current channel config bytes", statuscode);
        assertEquals(200, statuscode);
        String responseAsString = EntityUtils.toString(response.getEntity());
        if (!responseAsString.contains(ORIGINAL_BATCH_TIMEOUT)) {
            fail(format("Did not find expected batch timeout '%s', in:%s", ORIGINAL_BATCH_TIMEOUT, responseAsString));
        }
        // Now modify the batch timeout
        String updateString = responseAsString.replace(ORIGINAL_BATCH_TIMEOUT, UPDATED_BATCH_TIMEOUT);
        httppost = new HttpPost(CONFIGTXLATOR_LOCATION + "/protolator/encode/common.Config");
        httppost.setEntity(new StringEntity(updateString));
        response = httpclient.execute(httppost);
        statuscode = response.getStatusLine().getStatusCode();
        out("Got %s status for encoding the new desired channel config bytes", statuscode);
        assertEquals(200, statuscode);
        byte[] newConfigBytes = EntityUtils.toByteArray(response.getEntity());
        // Now send to configtxlator multipart form post with original config bytes, updated config bytes and channel name.
        httppost = new HttpPost(CONFIGTXLATOR_LOCATION + "/configtxlator/compute/update-from-configs");
        HttpEntity multipartEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("original", channelConfigurationBytes, ContentType.APPLICATION_OCTET_STREAM, "originalFakeFilename").addBinaryBody("updated", newConfigBytes, ContentType.APPLICATION_OCTET_STREAM, "updatedFakeFilename").addBinaryBody("channel", fooChannel.getName().getBytes()).build();
        httppost.setEntity(multipartEntity);
        response = httpclient.execute(httppost);
        statuscode = response.getStatusLine().getStatusCode();
        out("Got %s status for updated config bytes needed for updateChannelConfiguration ", statuscode);
        assertEquals(200, statuscode);
        byte[] updateBytes = EntityUtils.toByteArray(response.getEntity());
        UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration(updateBytes);
        // To change the channel we need to sign with orderer admin certs which crypto gen stores:
        // private key: src/test/fixture/sdkintegration/e2e-2Orgs/channel/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/f1a9a940f57419a18a83a852884790d59b378281347dd3d4a88c2b820a0f70c9_sk
        // certificate:  src/test/fixture/sdkintegration/e2e-2Orgs/channel/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/Admin@example.com-cert.pem
        final String sampleOrgName = sampleOrg.getName();
        final SampleUser ordererAdmin = sampleStore.getMember(sampleOrgName + "OrderAdmin", sampleOrgName, "OrdererMSP", Util.findFileSk(Paths.get("src/test/fixture/sdkintegration/e2e-2Orgs/" + TestConfig.FAB_CONFIG_GEN_VERS + "/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/keystore/").toFile()), Paths.get("src/test/fixture/sdkintegration/e2e-2Orgs/" + TestConfig.FAB_CONFIG_GEN_VERS + "/crypto-config/ordererOrganizations/example.com/users/Admin@example.com/msp/signcerts/Admin@example.com-cert.pem").toFile());
        client.setUserContext(ordererAdmin);
        // Ok now do actual channel update.
        fooChannel.updateChannelConfiguration(updateChannelConfiguration, client.getUpdateChannelConfigurationSignature(updateChannelConfiguration, ordererAdmin));
        // Let's add some additional verification...
        client.setUserContext(sampleOrg.getPeerAdmin());
        final byte[] modChannelBytes = fooChannel.getChannelConfigurationBytes();
        // Now decode the new channel config bytes to json...
        httppost = new HttpPost(CONFIGTXLATOR_LOCATION + "/protolator/decode/common.Config");
        httppost.setEntity(new ByteArrayEntity(modChannelBytes));
        response = httpclient.execute(httppost);
        statuscode = response.getStatusLine().getStatusCode();
        assertEquals(200, statuscode);
        responseAsString = EntityUtils.toString(response.getEntity());
        if (!responseAsString.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, responseAsString));
        }
        if (responseAsString.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, responseAsString));
        }
        out("\n");
        out("That's all folks!");
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) Channel(org.hyperledger.fabric.sdk.Channel) UpdateChannelConfiguration(org.hyperledger.fabric.sdk.UpdateChannelConfiguration) HFClient(org.hyperledger.fabric.sdk.HFClient) HttpResponse(org.apache.http.HttpResponse) MalformedURLException(java.net.MalformedURLException) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpClient(org.apache.http.client.HttpClient) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpPost (org.apache.http.client.methods.HttpPost)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 StringEntity (org.apache.http.entity.StringEntity)1 Channel (org.hyperledger.fabric.sdk.Channel)1 HFClient (org.hyperledger.fabric.sdk.HFClient)1 UpdateChannelConfiguration (org.hyperledger.fabric.sdk.UpdateChannelConfiguration)1 Test (org.junit.Test)1