github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/docs/vm.md (about) 1 # NEO-GO-VM 2 3 A cross platform virtual machine implementation for `NEF` compatible programs. 4 5 # Installation 6 7 VM is provided as a part of neo-go binary, so usual neo-go build instructions 8 are applicable. 9 10 # Running the VM 11 12 Start the virtual machine: 13 14 ``` 15 $ ./bin/neo-go vm 16 17 _ ____________ __________ _ ____ ___ 18 / | / / ____/ __ \ / ____/ __ \ | | / / |/ / 19 / |/ / __/ / / / /_____/ / __/ / / /____| | / / /|_/ / 20 / /| / /___/ /_/ /_____/ /_/ / /_/ /_____/ |/ / / / / 21 /_/ |_/_____/\____/ \____/\____/ |___/_/ /_/ 22 23 24 NEO-GO-VM > 25 ``` 26 27 # Usage 28 29 ``` 30 _ ____________ __________ _ ____ ___ 31 / | / / ____/ __ \ / ____/ __ \ | | / / |/ / 32 / |/ / __/ / / / /_____/ / __/ / / /____| | / / /|_/ / 33 / /| / /___/ /_/ /_____/ /_/ / /_/ /_____/ |/ / / / / 34 /_/ |_/_____/\____/ \____/\____/ |___/_/ /_/ 35 36 37 NEO-GO-VM > help 38 39 Commands: 40 aslot Show arguments slot contents 41 break Place a breakpoint 42 clear clear the screen 43 cont Continue execution of the current loaded script 44 estack Show evaluation stack contents 45 exit Exit the VM prompt 46 help display help 47 ip Show current instruction 48 istack Show invocation stack contents 49 loadbase64 Load a base64-encoded script string into the VM 50 loadgo Compile and load a Go file with the manifest into the VM 51 loadhex Load a hex-encoded script string into the VM 52 loadnef Load a NEF-consistent script into the VM 53 lslot Show local slot contents 54 ops Dump opcodes of the current loaded program 55 parse Parse provided argument and convert it into other possible formats 56 run Execute the current loaded script 57 sslot Show static slot contents 58 step Step (n) instruction in the program 59 stepinto Stepinto instruction to take in the debugger 60 stepout Stepout instruction to take in the debugger 61 stepover Stepover instruction to take in the debugger 62 63 ``` 64 65 You can get help for each command and its parameters adding `help` as a 66 parameter to the command: 67 68 ``` 69 NEO-GO-VM > step help 70 71 Usage: step [<n>] 72 <n> is optional parameter to specify number of instructions to run, example: 73 > step 10 74 75 ``` 76 77 ## Loading in your script 78 79 To load an avm script in NEF format into the VM: 80 81 ``` 82 NEO-GO-VM > loadnef ../contract.nef 83 READY: loaded 36 instructions 84 ``` 85 86 Run the script: 87 88 ``` 89 NEO-GO-VM > run 90 [ 91 { 92 "value": 1, 93 "type": "BigInteger" 94 } 95 ] 96 ``` 97 98 You can also directly compile and load `.go` files: 99 100 ``` 101 NEO-GO-VM > loadgo ../contract.go 102 READY: loaded 36 instructions 103 ``` 104 105 To make it even more complete, you can directly load hex or base64 strings into the VM: 106 107 ``` 108 NEO-GO-VM > loadhex 54c56b006c766b00527ac46c766b00c391640b006203005a616c756662030000616c7566 109 READY: loaded 36 instructions 110 NEO-GO-VM > run 111 [ 112 { 113 "value": 10, 114 "type": "BigInteger" 115 } 116 ] 117 118 ``` 119 120 ## Running programs with arguments 121 You can invoke smart contracts with arguments. Take the following ***roll the dice*** smart contract as an example. 122 123 ``` 124 package rollthedice 125 126 import "github.com/nspcc-dev/neo-go/pkg/interop/runtime" 127 128 func RollDice(number int) { 129 if number == 0 { 130 runtime.Log("you rolled 0, better luck next time!") 131 } 132 if number == 1 { 133 runtime.Log("you rolled 1, still better then 0!") 134 } 135 if number == 2 { 136 runtime.Log("you rolled 2, coming closer..") 137 } 138 if number == 3 { 139 runtime.Log("Sweet you rolled 3. This dice has only 3 sides o_O") 140 } 141 } 142 ``` 143 144 To invoke this contract we need to specify both the method and the arguments. 145 146 The first parameter (called method or operation) is always of type 147 string. Notice that arguments can have different types. They can be inferred 148 automatically (please refer to the `run` command help), but if you need to 149 pass a parameter of a specific type you can specify it in `run`'s arguments: 150 151 ``` 152 NEO-GO-VM > run rollDice int:1 153 ``` 154 155 > The method is always of type string, hence we don't need to specify the type. 156 157 To add more than 1 argument: 158 159 ``` 160 NEO-GO-VM > run someMethod int:1 int:2 string:foo string:bar 161 ``` 162 163 Currently supported types: 164 - `bool (bool:false and bool:true)` 165 - `int (int:1 int:100)` 166 - `string (string:foo string:this is a string)` 167 168 ## Debugging 169 The `neo-go-vm` provides a debugger to inspect your program in-depth. 170 171 172 ### Stepping through the program 173 Step 4 instructions. 174 175 ``` 176 NEO-GO-VM > step 4 177 at breakpoint 3 (DUPFROMALTSTACK) 178 NEO-GO-VM 3 > 179 ``` 180 181 Using just `step` will execute 1 instruction at a time. 182 183 ``` 184 NEO-GO-VM 3 > step 185 at breakpoint 4 (PUSH0) 186 NEO-GO-VM 4 > 187 ``` 188 189 ### Breakpoints 190 191 To place breakpoints: 192 193 ``` 194 NEO-GO-VM > break 10 195 breakpoint added at instruction 10 196 NEO-GO-VM > cont 197 at breakpoint 10 (SETITEM) 198 NEO-GO-VM 10 > cont 199 ``` 200 201 ## Inspecting stack 202 203 Inspecting the evaluation stack: 204 205 ``` 206 NEO-GO-VM > estack 207 [ 208 { 209 "value": [ 210 null, 211 null, 212 null, 213 null, 214 null, 215 null, 216 null 217 ], 218 "type": "Array" 219 }, 220 { 221 "value": 4, 222 "type": "BigInteger" 223 } 224 ] 225 ``` 226 227 There is one more stack that you can inspect. 228 - `istack` invocation stack 229 230 There are slots that you can inspect. 231 - `aslot` dumps arguments slot contents. 232 - `lslot` dumps local slot contents. 233 - `sslot` dumps static slot contents. 234