All Products
Search
Document Center

Blockchain as a Service:Quick Start Guide

Last Updated:Sep 07, 2023

This topic describes how to execute applications and provides an example execution procedure.

Execute an application

To execute an application, perform the following steps:

  1. Prepare the environment.

  2. Write the application.

  3. Compile the application.

  4. Execute the application.

Prepare an environment

  • Prepare files for establishing an SSL connection.

    To establish an SSL connection to the BaaS platform, you must prepare the following four certificate files: CA root certificate file (trustCa), certificate file of the client (client.crt), private key file of the client (client.key), and private key file of the user’s account (user.key). The following table describes the four files.

    File

    Description

    Source

    client.crt

    Certificate file of the client

    Use the BaaS-provided key generation tool to generate the certificate request file client.csr. Then, submit this file to the BaaS platform to apply for the certificate, and download the .crt file after the application is approved.

    client.key

    Private key file of the client

    Use the BaaS-provided key generation tool to generate the file.

    trustCa

    TrustStore to store the CA certificate

    Download the file from the BaaS platform. The password of the file is ‘mychain’.

    user.key

    Private key file of the account

    Use the BaaS-provided key generation tool to generate the file.

  • Obtain the IP address and port number of the node on the blockchain.

    This information is used for interactions with the blockchain. To obtain the information, view details of the node on the BaaS platform.

Write an application

  1. Use IntelliJ IDEA to create a Maven file-based demo project. After the project is created, the directory structure is as follows:

    1545295559042-521770ec-412b-49ed-935e-a6b644bb0949.png

  2. In the java directory, customize a package, for example, com.example.demo. Then, copy content in DemoSample.java to the new package, and store the client.crt, client.key, trustCa, and user.key files in resources. You can click DemoSample.java to download the file.

    1

    Note

    For the demo project, you must use the bytecode generated after the source code of the contract is compiled. For more information about the source code, see Solidity contract development. For more information about the tool used to compile Solidity contracts, see Solidity contract compilation tool.

  3. Add dependencies to the pom.xml file. Add the SDK and slf4j-log4j12 to pom.xml and the configuration file of log4j to resources.

    Note

    In this step, you must use the latest package on which the SDK depends.

    pom

    The following example shows a pom file with dependencies for slf4j-log4j12.

    <dependencies>
         <dependency>
             <groupId>com.alipay.mychainx</groupId>
             <artifactId>mychainx-sdk</artifactId>
                 <!--Use the latest SDK -->
             <version>0.10.2.6</version>
         </dependency>
    
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>1.8.0-alpha0</version>
         </dependency>
     </dependencies>
     
     <build>
         <extensions>
             <extension>
                 <groupId>kr.motd.maven</groupId>
                 <artifactId>os-maven-plugin</artifactId>
                 <version>1.6.1</version>
             </extension>
         </extensions>
     </build>

    log4j.properties file content is as follows:

     log4j.rootLogger=INFO, R
    
     # Log outputs are in the console.
     log4j.appender.stdout=org.apache.log4j.ConsoleAppender
     log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
     log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
    
     # Log outputs are in a file.
     log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
     log4j.appender.R.File=./sdk.log
     log4j.appender.R.layout=org.apache.log4j.PatternLayout
     log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
    
     # mychain-sdk log switch configuration
     log4j.logger.MychainClient=OFF

    If you are using the logging framework of logback, the configuration method of the log switch is as follows:

    <logger name="MychainClient" level="OFF"/>

Compile an application

Run mvn clean compile in the root directory of the project to compile the project.

Execute the application

Execute the project in DemoSample.java. The generated log is stored in root directory ./sdk.log. If the log contains the “Hand shake success” response, the project is connected to the BaaS platform.

The expected output is as follows:

create testAccount1 success.
create testAccount2 success.
deploy contract success.
issue success.
transfer success.
check account balance success.

Error message

If the execution result of the transaction indicates that it fails to be executed, you can use the error code to locate the failure.

  • ErrorCode: The errorCode field in MychainBaseResult indicates the reason for this failure.

If the request is for a transaction, you can obtain details about the failure based on the returned values. Take contract deployment for example:

MychainBaseResult<ReplyTransactionReceipt> result = sdk.getContractService()
    .deployContract(
        DeployContractRequest.build(adminAccount.getIdentity(),
            Utils.getIdentityByName(testContractId,env), contractCode, VMTypeEnum.EVM,
            contractParameters, BigInteger.ZERO, params));
assertTrue(result.isSuccess());
assertEquals(0, result.getData().getTransactionReceipt().getResult());
  • If result.getData().getTransactionReceipt().getResult() is 0, the transaction execution succeeds. You can use the value of result to find the reason for a failure from MychainErrorCodeEnum.

  • result.isSuccess() indicates whether the transaction is sent. If the transaction fails to be sent, you can use result.getErrorCode() to obtain the error code.

Example execution procedure

  1. Initialize the environment.

    //step 1: init logger.
     initLogger();
    
     //step 2:init mychain env.
     env = initMychainEnv();
  2. Create a transaction.

    //step 4: init account that will be created.
     initAccount();
    
     //step 5: init private key list which will be used during transaction.
     initPrivateKeyList();
    
     //step 6: execute create two accounts.
     createAccount();
  3. Deploy a smart contract.

    //step 7 : deploy a contract using testAccount1.
     deployContract();
  4. Purchase points.

    //step 8:issue 100 credits to testAccount2.
     issue();
  5. Conduct an account transfer.

    //step 9 : transfer 50 credits from testAccount2 to testAccount1
     transfer();
  6. Query the account balance.

    //step 10 : query testAccount2 whose balance should be 50.
     BigInteger balance = query(test2PrivateKeyArrayList,testAccount2);
    
     //step 11 : compare to expect balance.
     expect(balance,BigInteger.valueOf(50));
  7. Disconnect SDK.

    //step 12 : sdk shut down
     sdk.shutDown();