yodaos-project / ShadowNode

Use Node.js in your end devices(QQ: 796448809)
https://yodaos-project.github.io/ShadowNode/
Other
588 stars 46 forks source link

To support smart contract in blockchain #512

Open moraleinside opened 5 years ago

moraleinside commented 5 years ago

Hey, talents of ShadowNode, my name is Allen Liu, the engineering director of Vite Labs. Yesterday we had a wonderful talk with Yorkie regarding some of thoughts on ShadowNode, and also possibility of further collaborations between us. Here we submit this issue as start.

Vite Labs is a blockchain startup and we are developing a public blockchain project similar to Bitcoin and Ethereum but much better. We have implemented EVM-based smart contracts, however, our recent testing shows it does not 100% meet the needs. The current threshold for developing smart contracts is relatively high, resulting in fewer developers. We would like to reduce the difficulty by leveraging a popular programming language and modern virtual machine.

What is smart contract?

In general, a smart contract consists of a piece of code and relevant storage. When user trades on blockchain, the contract code will be automatically triggered and starts execution, and in the end complete transferring assets and modifying storage. The code of smart contract is written by contract deployer and deployed on blockchain. Contract codes typically contain mathematical calculations, storage access, and instructions of interactions with blockchain such as querying account balance, obtaining random number, calling other contract, etc.

The module that executes smart contract is called blockchain virtual machine. Unlike traditional virtual machine, blockchain virtual machine must possess two essential characteristics:

  1. The execution results are stable. Blockchain is a distributed ledger system. Each node holding a copy of the ledger needs to calculate and verify the latest ledger state based on the transaction information, so that it is critical to guarantee the same piece of code executed on different hardwares will always yield the exact same result.
  2. The usage of resources in execution process is strictly controlled. Usually, there is a fee when trading on the blockchain, referred to as Gas in many circumstances. For example, in Vite users are required to stake a certain amount of tokens to get quota, while Ethereum tells people to pay ether for transactions. This process determines the amount of resources that the transaction can utilize, including cpu, storage, network bandwidth, etc., so a method for precisely calculating resources consumed in real time is desired urgently. Once the resource usage exceeds a pre-set limit, the execution will stop. Similar to #1, the measurement of resource also needs to be consistent on different machines.

The current smart contract programming language of Vite is called Solidity++, which is adopted from Solidity but with many more Vite-specific upgrades. However, Solidity is relatively narrow in practical use, and it is designed to be more oriented in security than other aspects such as performance, usability or popularity. The virtual machine in Vite is also developed on the basis of EVM. It is a stack-based 256-bit virtual machine, which ensures the execution of the same piece of code on different machines is identical and resulting in the same execution result. Gas calculation on Vite’s VM is performed at instruction level.

In order to reduce the development threshold of smart contracts and attract more people to the platform, we are assessing some popular programming languages. One possible choice is Javascript + v8 engine. Javascript has solid user group and mature ecosystem, while v8 is able to guarantee stable execution results. To migrate them into blockchain environment, two major problems are yet to be solved:

  1. Need to remove some semantics that may lead to inconsistent execution results, such as asynchronous ops and system calls.
  2. Consider how to implement resource consumption calculations at bytecode level.

Related documents: Solidity doc https://solidity.readthedocs.io/en/v0.5.7/ Solidity++ upgrades on Solidity https://vite.wiki/tutorial/contract/soliditypp.html EVM https://github.com/ethereum/wiki/wiki/Ethereum-Virtual-Machine-(EVM)-Awesome-List

osdio commented 5 years ago

More links about Vite:

Website: https://vite.org/ White Paper: https://github.com/vitelabs/whitepaper/blob/master/vite_cn.pdf

yorkie commented 5 years ago

@AllenLiuxt @soliury, thanks for your attentions on ShadowNode and general guidance about smart contract, the following is my answers:

Need to remove some semantics that may lead to inconsistent execution results, such as asynchronous ops and system calls.

JavaScript VM actually doesn't declare any asynchronous operations. See the project https://github.com/pando-project/jerryscript, which is the low-level VM of ShadowNode, too. That is a pure JavaScript environment that fits your need :)

Consider how to implement resource consumption calculations at bytecode level.

@AllenLiuxt take a look at https://github.com/pando-project/jerryscript/blob/master/jerry-core/vm/vm.c#L903, which executes every OPCODE in JerryScript, and at ShadowNode, we documented the opcode table at https://github.com/yodaos-project/ShadowNode/wiki/OP-Code-Table.

The current threshold for developing smart contracts is relatively high, resulting in fewer developers. We would like to reduce the difficulty by leveraging a popular programming language and modern virtual machine.

JerryScript is not a full-ES6 implementation, so if we choose that as the smart contract VM, developers could not use some features like: const/let, rest arguments... :(