Welcome: Hunan Intelligent Applications Tecgnology CO.,ltd.-HNIAT.com
Language: Chinese ∷  English

News

Has the performance limit of smart contracts been breached?

People who follow the blockchain often hear discussions about "smart contracts." Smart contracts allow for trusted transactions without a third party. These transactions are traceable and irreversible, which is the key to blockchain technology. Baidu Super Chain has also conducted in-depth research and development on smart contracts to achieve high performance and support multi-language writing, which greatly improves the contract use efficiency.

This issue of the Superchain Academy online micro-classroom will take you beyond the performance limits of previous smart contracts! Star Lecturer Chao will focus on the following points:

1. What are the characteristics of a super chain smart contract?

2. What are the similarities and differences between superchain smart contracts and Ethereum?

3. What is the running mode of super chain smart contract execution?

4. How does the super chain smart contract achieve high performance?

5. What can superchain smart contracts provide developers?

Come and read on!

Q1: What is a smart contract?

Smart contract (English: Smart contract) is a computer agreement designed to spread, verify or execute contracts in an informational manner. Smart contracts allow trusted transactions to be made without third parties, which are traceable and irreversible. The smart contract concept was first proposed by Nick Szabo in 1995.

The purpose of smart contracts is to provide a secure method that is superior to traditional contracts and reduce other transaction costs associated with the contract.

Q2: What are the characteristics of smart contracts in the super chain?

1. Multi-language support, supporting high-level languages such as C ++ / Go.

2. High-performance, original XuperModel data model to maximize contract parallel execution capabilities.

3. Security, support contract resource audit and shield operating system interface to ensure contract security.

4. Scalability, can expand the multi-language of the contract and the access to resources on the chain.

5. Isolation, different contract calls do not affect each other.

Deterministic, consistent results with the same parameters and environment.

Q3: Differences and similarities between smart contracts and hyperledger fabric contracts

Same point:

1. Both the super chain and fabric smart contracts use a two-phase commit pre-execution model to maximize the execution of concurrent contracts.

2. Both superchain and fabric can use high-level languages, such as go, to write smart contracts, reducing the threshold for contract development.

Has the performance limit of smart contracts been breached?

difference:

1.fabric contract is a resident process running in docker. An irregular contract will cause multiple contract calls to affect each other, such as the use of global variables. Each contract call of the super chain will start a separate contract context. In combination with ModelCache, multiple contract calls are completely isolated from each other without affecting each other.

2.Fabric contract does not limit the contract's resources. In theory, an infinite loop in the contract will cause the contract to fail to execute smoothly. The smart contract of the super chain will set the upper limit of resources (cpu, memory, etc.). Once the execution of the contract exceeds the upper limit, it will automatically stop, preventing the situation of infinite loop.

3. The code in the contract will still access some resources of the system, such as the file system, random numbers, etc., which will cause uncertainty in the contract. The smart contract of the super chain isolates any system call that accesses system resources. It is a sandbox environment with completely controllable behavior, and there will be no uncertain behavior.

Q4: Similarities and Differences between Superchain Smart Contracts and Ethereum Contracts

Same point:

1. Both the superchain and Ethereum's smart contracts are deterministic, that is, the same input gets the same output.

2. Both the Superchain and Ethereum's smart contracts support contract resource auditing to ensure contract security.

difference:

1. At present, the contract virtual machine of Ethereum is EVM, and most of the instructions in it are 256bit, resulting in lower performance. The contract instruction used by the super chain is WASM, which is an assembly instruction that runs in the browser. It has the joint contributions of Google, Apple, Molliza, and Microsoft. The instructions are close to the machine assembly and have high performance.

2. The language currently running on Ethereum is mainly Sodility, which is compiled on EVM to run on Ethereum. However, because many EVM instructions are highly coupled with Ethereum's functions, it is difficult to compile common programming languages. EVM instructions, so multi-language support for Ethereum is relatively weak. Superchains benefit from WASM instructions. Many high-level languages can be compiled into WASM, such as C ++, Go, Rust, etc. In addition, the superchain uses XuperBridge to extend the ability of the contract to access the chain. It is better decoupled from the instructions. Scalability.

Q5: How does the super chain smart contract run the WASM instruction?

There are two modes of execution for superchain contract execution.

1. One is interpreted execution. After reading the contract code, this mode first translates the contract code into a memory data structure, and then executes one by one according to the instruction type, analogous to the interpreter of a traditional interpreted language. The advantage of this mode is that the deployment contract is faster, and the disadvantage is that the performance is slower.

2. The other is compilation and execution. After reading the contract code, this mode first translates the contract into native cpu instructions, such as x86 instructions, and then hands them to the cpu to run the compiled instructions. The advantage of this mode is that it runs very fast. The disadvantage is that because it needs to be compiled in advance, the deployment speed will be slower, but the compilation is a one-time action, and the subsequent execution directly reuses the compilation result.

Q6: How does the super chain smart contract support resource statistics?

There are three main steps:

The first step: analyze the bytecode of the contract and build a control flow graph based on the bytecode division function. What is a control flow graph? We roughly divide the instructions of the contract into two categories according to whether they will cause a jump. One is the instructions that are executed sequentially, such as add, load, etc .; the other is the instructions that will cause a jump, such as loop and if. It is a graph showing the instruction flow of a contract with control instructions as nodes.

In the second step, with the control flow graph, we can insert the resource check instructions. We consider all the control instruction sub-instruction sequences as a linked list, as shown in the following block of the if statement.

if (flag) {

n + = 1;

sum + = n;

}

Since non-jump instructions are executed sequentially, we only need to add an instruction to check resources at the beginning of the linked list, and there is no need to add a resource check after each instruction.

In the third step, when translating into machine code or the interpreter interprets the resource check instruction, the limit value is compared according to the currently accumulated resource counter. If the limit value is exceeded, the execution of the virtual machine is directly terminated, otherwise the execution is continued.

Q7: How does the super chain smart contract expand the on-chain capabilities through XuperBridge

XuperBridge has designed a set of standard interfaces and serialization methods. The contract virtual machine only needs to unify different access methods of different languages into the interface form of XuperBridge to access resources on the chain, which is similar in form to the RPC calls we usually use. When you need to expand the interface, you only need to add a new interface method. It is through this form that the super chain supports a variety of virtual machines, such as WASM, Docker, EVM, etc., without the need to design an interface for each virtual machine. To access resources on the chain.

Q8: How does the super chain smart contract achieve high performance?

Super chain contracts mainly achieve high performance from the following aspects:

1. The contract instruction selects WASM. WASM itself is closer to the underlying hardware, so the performance is better.

2. The contract virtual machine supports the translation of contract instructions to naTIve instructions, running at speeds close to naTIve programs.

3. The execution of the contract has two steps. The first step is pre-execution. When each contract runs, there is a unique context to isolate different contract operations. The context will collect the results of reading and writing data during the execution of the contract. After the execution is completed, a read-write set that modifies the data will be generated. In the second step, the read-write set is packaged into a transaction on-chain. At this time, the XuperModel will perform conflict detection on the read-write set. Any conflicts are passed directly, and those with conflicts will fail. It is in this way to maximize the parallel execution and verification capabilities of the contract.

Q9: What can smart contracts based on super chains do?

The current super chain smart contracts provide the following capabilities:

· Set ACLs for contract methods to control access to different contract methods.

· Store data on the chain and query data on the chain in the form of KV.

· Cross-contract calling ability, different contracts can directly call each other.

Transfers to and from contracts

Query historical transactions and blocks

Follow-up will gradually add some new features, such as SQL and file system interface to enrich the function of the contract

Q10: How to deploy a super chain smart contract

Taking the ERC20 contract in the superchain example directory as an example, the following steps are required:

1. Compile the contract, go to the contractsdk / cpp directory of the source code, and execute the build.sh script

2. Prepare a contract account and call xchain-cli account new --account 1111111111111111 to create a contract account. The contract must be deployed to the contract account

3. Ensure that the contract account has sufficient tokens, so we transfer some tokens to the contract account xchain-cli transfer --to XC1111111111111111 @ xuper --amount 100000000

4. Deploy the contract to the contract account just created xchain-cli wasm deploy $ path_to_contract / erc20.wasm -n erc20 --account XC1111111111111111 @ xuper -a ‘{“ totalSupply ”:“ 10000000 ”}’

At this point, an ERC20 contract has been deployed on the chain. Later, you can call the relevant interface of the contract to use the contract. For specific documents, see https://xuperchain.readthedocs.io/zh/latest/advanced_usage/create_contracts.html

Things to note are as follows:

1. Contracts in different languages need to specify their own runTIme when deploying, otherwise deployment will fail

2. The contract account needs sufficient tokens to deploy the contract

Q11: Where can I find more examples of super chain smart contracts

The super chain open source code contains some examples of using contracts, covering contract examples such as ERC20, ERC721, certificate of deposit, and more examples will be given later.

After the sharing, the wonderful questions that emerged in the group, extract some to share with you.

Q: Do all blockchain applications need to have smart contracts?

Answer: Since Ethereum invented smart contracts to run on the blockchain, smart contracts have almost become standard on the new chain. Because of the smart contract, developers can express complex business logic. Previously, these logics may have been off-chain behaviors, not enough "blockchain". It can be said that smart contracts have brought the blockchain to 2.0.

Q: Is there a direct query for linked data in smart contracts? Or look at other data through key fields? Because the C ++ language can have a similar primary key setting, but the Go language does not have such a setting.

Answer: At present, the data in smart contracts is stored in the form of KV. It can be considered that the only primary key is key. C ++ wraps a table data interface to assist in generating multiple indexes. There is currently no such data structure in Go. You can look forward to the support of SQL in the super chain contract.

Q: If there are three hard disks to store data, can different data services be assigned to the specified hard disk in a specified way?

Answer: At present, the super chain supports multi-disk deployment, but hard disks are not supported according to business division.

Q: When can smart contracts be written in Java?

Answer: Java language support is planned, and it may be provided in the form of a naTIve contract first. WASM support is under investigation.

Q: Can it be called across smart contracts? Can it be called if the smart contract is not on the same chain?

A: Multiple smart contracts can call each other, as long as they have corresponding permissions. However, at present, smart contracts of the super chain can only communicate on one chain, and multi-chain technology is being developed.

Q: At present, what kind of resource consumption statistics are supported by the super chain smart contract? How is their conversion ratio to gas measured?

A: The resource consumption statistics are supported: CPU, memory, disk, etc. The specific conversion ratio refers to the source code. Of course, developers can also change these parameters by voting on proposals.

CONTACT US

Contact: Manager Xu

Phone: 13907330718

Tel: 0731-22222718

Email: hniatcom@163.com

Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province

Scan the qr codeClose
the qr code