Tencent Cloud Blockchain Service Platform (TBaaS) Changan Chain Experience Network Public Test Experience

A few days ago, the InfoQ community saw Tencent Cloud’s Tencent Cloud blockchain service platform (TBaaS) Chang’an chain experience activity, and the operation was as fierce as a tiger and reported a name. After the experience, it can be summed up in one word: strong. If I have to add a few more words, the overall feeling is that the assembly mode is flexible and efficient, and the supporting tools are fully explained. Without further ado, start the topic. The directory structure of this article is divided into three parts: blockchain classification , TBaaS platform introduction , and TBaaS platform on-chain tutorial.

1. Blockchain classification:

Generally speaking, blockchain can be divided into three types: public chain, alliance chain, and private chain:
Public Blockchain (Public Blockchain) public chain refers to a blockchain that anyone can participate in. The public chain is the blockchain with the highest degree of decentralization. It is not controlled by institutions, and the entire ledger is open and transparent to everyone. Anyone can query transactions, send transactions, and participate in bookkeeping on the public chain. Joining the public chain does not require anyone's authorization, and you can join or leave freely, so the public chain is also called a permissionless chain. The public chain where everyone can participate in bookkeeping is kept in an unfamiliar and trustless competitive environment, so the public chain needs to have a consensus mechanism to select bookkeeping nodes, which is what we usually say through "mining". "Mine" competes for bookkeeping rights. Because of the need for mining, public chain bookkeeping has the characteristics of high delay, high cost, and low efficiency. Most of the blockchain projects we come across are public chains. The well-known public chain projects include: Bitcoin, Ethereum, EOS, etc.

Private Blockchain (Private Blockchain) and public chain account books are open and transparent to everyone and everyone can keep accounts. In contrast, private blockchain means that the blockchain accounting authority is only in the hands of one person or organization, and participate in the bookkeeping. The authority of the account is formulated internally by the organization, and the read authority can be opened to the other party or restricted to any extent. For example, for a company's financial budget, the only people involved in bookkeeping may be the leader of the financial department and the boss of the company, and the read permission can be selected according to the needs of the company, so that only the company's decision-making layer or all employees can choose to know. Due to the small number of participating bookkeeping nodes and the lack of "mining" competition, the private chain has the advantages of fast bookkeeping, no bookkeeping costs, and high privacy. Since private chains are all internal nodes, the accounting environment is credible; blockchain technology can prevent a single node in the organization from tampering with data, and even if errors occur, they can be quickly discovered. Private chains are suitable for companies or organizations, and many large financial institutions tend to use private chains.

Consortium chain (ConsorTIum Blockchain) Consortium chain ledger openness is between public chain and private chain. A consortium chain refers to a blockchain managed and maintained by multiple institutions, and the nodes participating in the blockchain are selected in advance. The consortium chain is also only open to all or part of the functions within the consortium, and the reading, writing and accounting rules of information on the chain are all set in accordance with the consensus of the consortium. For example, if 100 universities have established a certain blockchain, the consensus stipulates that 67 universities must agree to reach a consensus. Like the private chain, nodes need to be authorized to join, so both the alliance chain and the private chain are called permission chains. Due to the good connection between nodes and a trusted network environment, the alliance chain has the characteristics of high bookkeeping efficiency, short consensus time, low bookkeeping cost and privacy. The alliance chain is mainly applicable to trade associations and large-scale chain enterprises' transactions and supervision of subordinate units and agencies. For example, the R3 blockchain alliance established in 2015 is a banking alliance chain, which has attracted the participation of more than 40 giant banks, including Wells Fargo, Bank of America, Bank of New York Mellon, and Citibank.

Chang'an Chain, the protagonist in this Tencent Cloud Blockchain Service Platform (TBaaS) public beta experience activity, belongs to the blockchain architecture, which is used for blockchain technology architecture and used to build blockchain networks.

Similar products include AntChain, ZhixinChain, etc. A brief introduction of the three:


Ant Chain: Alibaba system product, Ant Blockchain is a financial-level blockchain service project platform independently developed by Ant Financial Services. It is committed to creating a one-stop service, using blockchain technology to solve multiple scenarios such as finance, retail, and life Area application issues.


Zhixin Chain: Tencent system product, a judicial application ecological service platform based on digital cultural content scenarios initiated and established by Tencent, China Net Security and Beiming Software. The target is the Ali Ant Chain.


Changan Chain: The national team, an open source bottom chain platform jointly developed by Beijing Microchip Research Institute, Tsinghua University, Beijing University of Aeronautics and Astronautics, Tencent, Baidu and JD.com and other well-known universities and enterprises. The name "Chang'an Chain" means "long-term peace and stability, creating greater glories, and linking the world". Specific project background and introduction, contract document writing and other references, Portal: Introduction to Changan Chain’s underlying technology platform


2. Introduction to Tencent Cloud Blockchain Service Platform (TBaaS):
Tencent Cloud Blockchain Service Platform (TBaaS, Tencent Blockchain as a Service) relies on Tencent Cloud infrastructure to provide enterprises and developers with a one-stop, high-security, Simple and easy-to-use blockchain services. The TBaaS blockchain service platform integrates functions such as development, management, and operation and maintenance, and supports customers to quickly deploy the alliance blockchain network environment on the cloud. Based on the TBaaS blockchain service platform, customers can reduce the acquisition cost of the underlying technology of the blockchain, and focus on the innovation of blockchain business models and the development and operation of business applications.

The TBaaS blockchain service platform not only meets financial-level security compliance requirements, but also has the complete capabilities of Tencent Cloud. Users can quickly build their own IT infrastructure and blockchain services on an elastic and open cloud platform. The TBaaS blockchain service platform integrates many blockchain underlying technologies, and currently supports Changan Chain·ChainMaker, Hyperledger Fabric and FISCO-BCOS blockchain underlying engines, providing users with flexible options for building their own blockchain solutions.

Old Practice, Portal: TBaaS Product Introduction

3. On-chain experience of TBaaS platform

Tencent Cloud Blockchain Service Platform (TBaaS) provides an experience portal, you only need to write a smart contract and upload it to the chain quickly, portal: https://cloud.tencent.com/product/tbaas

Click to experience Chang'an chain for free and enter the blockchain network management module, as shown in the figure below:

There is already a trial network here, no need to create a new network, just click the red area to enter the detailed management page

At this time, it is time to prepare to upload the contract. The contract can choose Docker Go, Solidity, C++, Rust. Because of the development environment problem, I chose Go here, but in the process of using Docker to write the contract laboriously, I found that Changan Chain provides an IDE. , write online, compile and download the contract and upload it directly to use, IDE Portal: https://ide.chainmaker.org.cn/

First, we right-click WorkSpace - create a contract project, enter the name of the contract project, such as chinamakerDemo

Next, right click on the contract project - create a contract file and name it main.go

 

 

 After entering the contract code in the editing area on the right, ctrl + s to save, such as:

package main

import (
    "strconv"

    "chainmaker/pb/protogo"
    "chainmaker/shim"
)

// AgeContract save and get age
type AgeContract struct {
}

// InitContract 合约初始化方法,会在合约部署到链上时执行
func (ac *AgeContract) InitContract(stub shim.CMStubInterface) protogo.Response {
    return shim.Success([]byte("Init Success"))
}

// InvokeContract 调用合约,在链上执行合约时,实际调用的是这个方法
func (ac *AgeContract) InvokeContract(stub shim.CMStubInterface) protogo.Response {
    //获取要调用的合约方法
    method := string(stub.GetArgs()["method"])

    //case "saveAge" 关联方法名 "saveAge" 到 ac.saveAge()
    //case "getAge"  关联方法名 "getAge"  到 ac.getAge()
    switch method {
    case "saveAge":
        return ac.saveAge(stub)
    case "getAge":
        return ac.getAge(stub)
    default:
        return shim.Error("no contarct method")
    }
}

// saveAge 保存用户年龄信息
func (ac *AgeContract) saveAge(stub shim.CMStubInterface) protogo.Response {
    //获取所有的合约参数
    args := stub.GetArgs()

    name := string(args["name"])
    ageStr := string(args["age"])
    if name == "" || ageStr == "" {
        message := "name or age is empty"
        //打印日志,调试合约时,日志会在控制台输出中展示
        stub.Log(message)
        //返回合约执行错误信息
        return shim.Error(message)
    }

    _, err := strconv.Atoi(ageStr)
    if err != nil {
        message := "convert age to int fail. err: " + err.Error()
        //打印日志,调试合约时,日志会在控制台输出中展示
        stub.Log(message)
        //返回合约执行错误信息
        return shim.Error(message)
    }

    //保存用户年龄信息到链上
    err = stub.PutStateFromKey(name, ageStr)
    if err != nil {
        message := "put state from key fail. err: " + err.Error()
        //打印日志,调试合约时,日志会在控制台输出中展示
        stub.Log(message)
        //返回合约执行错误信息
        return shim.Error(message)
    }

    //返回合约执行成功信息
    return shim.Success([]byte("success"))
}

// getAge 获取用户年龄信息
func (ac *AgeContract) getAge(stub shim.CMStubInterface) protogo.Response {
    //获取所有的合约参数
    args := stub.GetArgs()

    name := string(args["name"])
    if name == "" {
        message := "name is empty"
        //打印日志,调试合约时,日志会在控制台输出中展示
        stub.Log(message)
        //返回合约执行错误信息
        return shim.Error("-1")
    }

    ageStr, err := stub.GetStateFromKey(name)
    if err != nil {
        message := "get state from key fail. err: " + err.Error()
        //打印日志,调试合约时,日志会在控制台输出中展示
        stub.Log(message)
        //返回合约执行错误信息
        return shim.Error("-1")
    }

    if ageStr == "" {
        message := "age not found"
        //打印日志,调试合约时,日志会在控制台输出中展示
        stub.Log(message)
        //返回合约执行错误信息
        return shim.Error("-1")
    }

    //返回用户年龄
    return shim.Success([]byte(ageStr))
}

func main() {
    //运行合约
    err := shim.Start(new(AgeContract))
    if err != nil {
        panic(err)
    }
}

 After the contract is written, you must press ctrl + s to save it, and then you can build and debug the contract online

After the debugging is correct, the contract can be compiled online and downloaded, such as

Once you have the contract file, you can upload the contract file on the TBaaS platform

Pay attention to stepping on the pit, and you will encounter strange problems if the contract name and contract file are inconsistent. Here, I am very grateful to the friends of the TBaaS team, who helped solve the problem in a timely manner, and manually like it~  

After uploading the contract, it can be quickly uploaded to the chain

Click the quick link button, select the contract, call method, input parameters, etc. Click OK to complete, isn't it very convenient~

Next, we can view the information of this blockchain in the blockchain browser, such as height, number of transactions, etc.

Well, this is the end of the experience tutorial, and interested friends should go and experience it!

Guess you like

Origin blog.csdn.net/susieweijs/article/details/125718689