Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize JVM in Block chain

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve JVM in the block chain, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The virtual machine is a key component of the blockchain, which is used to execute intelligent contracts and needs to meet security and consistency. The so-called security generally means that the contract code needs to run in an isolated sandbox environment to avoid damage to the blockchain system caused by errors or malicious code. Consistency means that any honest node in the block chain network executes the same contract, and if the input parameters are the same, the output results should be consistent. At present, the mainstream virtual machine implementations include EVM, WASM, and other implementations, such as Google V8 Magi Nervos RISC-V,SOLANA BPF of Xingyun chain. The purpose of designing so many different virtual machines is to consider the factors such as blockchain system interoperability, security, execution performance, hardware compatibility and so on. For example, EVM pays more attention to security, although Solidity design is a complete language of Turing, but through its own limited minimum syntax set, to ensure the security and controllability of the contract, while the nebula chain uses V8, on the one hand, it also considers the interoperability of smart contracts, and the use of javascript reduces the threshold for writing contracts. Although Solidity is also a javascript-like language, and the entry barrier is not very high, for many programmers who want to enter the blockchain, there is some cost to learn a new language.

Why JVM is less used in the blockchain

JVM itself is a virtual machine, and the java language is one of the most popular languages, so why is it less used in blockchains? The personal summary mainly includes the following reasons:

1. The controllability of the language, similar to mainstream development languages such as c++/golang/rust, java is also a system-level language, which can access any resources of the http://www.jiasheng-chn.com operating system of Jiasheng Chinese Network, such as reading and writing files, accessing the network, etc.

2. Execution performance. Java is an interpretive language, and the class file loaded by jvm is an intermediate language, which will be further interpreted as machine code execution when it is really executed, so mainstream jvm will introduce JIT to compile hot code in time to improve execution efficiency.

3. The complexity of language, java has many complex syntax, such as interface, polymorphism, reflection and multithreading control that support OOP, which is of little use for intelligent contracts that pay more attention to process execution, and is easy to affect the consistency of contract execution.

If we can solve these problems and make a custom minimum collection tailoring for jvm, it will be quite attractive for many java developers.

How should the jvm scheme in the blockchain be implemented?

How to pass parameters

Java is OOP. If you write a contract, you need to define a contract class first. The class method defined in the contract class is the method that the contract can execute. The contract execution on the block chain is triggered by the transaction, and the contract address and input parameters need to be carried through the transaction. The first problem to be solved is how to transmit this information to the contract. It is relatively simple to handle the basic types. It can be encoded into a string for transmission, and when the contract is executed, it is resolved into the corresponding result according to the parameter type of the method, but how to deal with the objects of the custom class? You can use json, xml, proto and other coding methods to encode the class object field by field, and if the field is also a custom class object, then code it recursively. Some of these coding methods are good for reading, but the disadvantage is that they take up more space. For the redundant distributed storage mechanism such as block chain, the less space occupied, the better, so we can use the RLP (Recursive length prefix) coding method used in Ethernet Square.

How to load java system classes

Referring to the jvm.go code base, you can pre-install a jdk environment on the target node, and then preload the required java system class packages into a map in memory when the node application starts. Here, you can choose and tailor the imported system class packages according to the actual situation, and filter out the packages that are easy to cause inconsistent execution results, such as random numbers, multithreading, IO operations and so on.

How to simulate the offline sandboxie execution environment

The author adopts the method of cutting the system class package as above, which restricts the user contract to directly refer to the network, read and write, cmd and other operation rights, and does not allow the user contract to directly refer to the third-party jar package or native to execute the third-party library to ensure the security of the contract. This way is relatively simple and rough is not a particularly good solution, for malicious code to skip the system package, their own implementation of similar operations to access resources, can not be detected, is not a good solution, if there is a good plan can be discussed together.

How to shield the syntax execution of random numbers, multithreads, etc., which are easy to cause inconsistent execution results?

The same as above is to restrict the introduction of packages, which is also undetectable for malicious code to implement random numbers on its own.

How to start the execution of a contract

For jvm execution, you need to find the Main entry, and the contract code may not provide the Main function. We can automatically generate the corresponding Main function in the tool that generates the contract package, add the new contract object, and then call the code of the corresponding method.

How to read and write contract status variables

The instruction codes for java access are mainly load and store series instructions. However, for the contract to start execution, you need to first read the historical state from the database, such as the balance variable in the contract. The automatically generated Main function above will first new a contract object, so we can capture the constructor of the contract object, inject and read the database code, and restore all the member variables of the contract object. You can also refer to solidity's method to distinguish between memory and storage variables, which can be achieved through annotations. Jvm can parse the annotation of the variable, and if the annotation is Storge, access the database, otherwise get it from the current memory.

How to return the execution result

The execution of the user contract code may fail, such as insufficient balance, mismatch of permissions, and so on. We can associate the pointer of the transaction receipt to the main Thread of the contract execution, so that we can return at any time where the execution fails, and customize the relevant error messages.

How to pull back a deal

You can refer to the solution of eTaifang, and use MPT to save the history value of the state variable. The modification of the state variable is to modify the local storage in real time. If the transaction fails to roll back, you can directly modify it to the previous historical state.

How to improve execution performance

We can use native to implement functions similar to ethernet's system contract or replace more time-consuming operations in java, such as hashmap. With reference to the implementation of jvm.go, we can customize many native methods to improve execution efficiency, such as executing sha256 in a contract. If we use the method that comes with java, thousands of instructions will be added. We can also use the native method to complete some custom functions, such as zero knowledge proof, homomorphic encryption library and so on.

How to solve the problem of downtime

With reference to the gas mechanism of Ethernet Square, the burning gas is counted according to the instruction granularity, and the proportion of gas is increased according to the size of the stored data for the instructions related to storage. The advantage of this implementation is that the gas statistics are more accurate, but there are more java instructions, the code implementation is tedious, and the execution performance will be affected. In addition, we can see that the mainstream solution of using WASM in the industry is to inject statistical instructions into the compiled machine code. This scheme can also be tried, but you need to implement javac yourself.

Thank you for reading this article carefully. I hope the article "how to achieve JVM in Block chain" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report