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 Neo virtual machine

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The knowledge of this article "how to achieve Neo virtual machine" is not understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to achieve Neo virtual machine" article.

Frame

Location of the virtual machine

In the frame diagram, we can see that Virtual Machine has the following functions

Read Opcode (smart contract), execute in Execution Engine

Execution Engine can perform logical operations.

Interop Service can call External Data

System call (OP_SYSCALL) can access the information of the blockchain ledger

Let's first take a look at how the virtual machine reads Opcode.

VM object relationship

Several key objects

Execution Engine: execution engine

Execution Context: execution context

Stack Item: a piece of data in the stack

Encryption Library of Crypto:C#

Executive engine

IScriptTable stores other contract code that can be called by the AppCall command. This block needs to be studied for the implementation of the blockchain, which will be studied in detail later.

InteropService is specifically used to respond to SYSCALL, specifically which are system calls and what they are used for, which will be explained by examples later.

InvocationStack is the call stack, pass in parameters, call other contracts will have a new call stack

EvaluationStack is a computation stack that is used to perform operations

AltStack is the backup stack, and the intermediate results calculated by the calculation stack can be saved in the standby stack.

Execution context

Each variable is easy to understand, and the point is to see how it is used below.

Vm code execution process

Construction, you can input script container,script table at this time. Later, we will see where all these come from on the blockchain. Here, we only focus on the execution process of vm, not to delve into it for the time being.

two。 Load .avm, avm is a string of numbers compiled by the compiler, which can be loaded through engine.LoadScript.

Execute starts execution. Let's take a look at the code.

Public void Execute () {State & = ~ VMState.BREAK; while (! State.HasFlag (VMState.HALT) & &! State.HasFlag (VMState.FAULT) & &! State.HasFlag (VMState.BREAK)) StepInto ();} public void StepInto () {if (InvocationStack.Count = = 0) State | = VMState.HALT If (State.HasFlag (VMState.HALT) | | State.HasFlag (VMState.FAULT)) return; OpCode opcode = CurrentContext.InstructionPointer > = CurrentContext.Script.Length? OpCode.RET: (OpCode) CurrentContext.OpReader.ReadByte (); try {ExecuteOp (opcode, CurrentContext);} catch {State | = VMState.FAULT;}}

Take a look at this code, OpCode opcode = CurrentContext.InstructionPointer > = CurrentContext.Script.Length? OpCode.RET: (OpCode) CurrentContext.OpReader.ReadByte ()

After the code has been executed, insert OpCode.RET

If it is not RET, read a byte of opcode

The ExecuteOp function is the specific semantics of executing OpCode, which is illustrated by an example.

A specific example

It's the same code as last time.

Using Neo.SmartContract.Framework;using Neo.SmartContract.Framework.Services.Neo;public class Sum: SmartContract {public static int Main (int a, int b) {return a + b;}}

Test the code of the virtual machine

Using System;using System.IO;using System.Linq;using Neo;using Neo.VM;using Neo.Cryptography;namespace ConsoleApplication1 {class Program {static void Main (string [] args) {var engine = new ExecutionEngine (null, Crypto.Default); engine.LoadScript (@ "C:\. \ Test1.avm "); using (ScriptBuilder sb = new ScriptBuilder ()) {sb.EmitPush (4); / / corresponding parameter b sb.EmitPush (3); / / corresponding parameter an engine.LoadScript (sb.ToArray ());} engine.Execute () / / start executing var result = engine.EvaluationStack.Peek () .GetBigInteger (); / / set the return value Console.WriteLine ($"execution result {result}") here; Console.ReadLine () } the above is the content of this article on "how to realize the Neo virtual machine". I believe you all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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