neoneo-go/pkg/vm/compiler
Anthony De Meulemeester 55966c7e07 Draft of iterator and enumerator (CityOfZion/neo-storm#26)
* Draft of iterator and enumerator

* Added iterator API to the syscall mapping

* Added draft of the enumerator.go file

* Added enumerator interop API.

* Updated the changelog

Imported from CityOfZion/neo-storm (156093318b8612e810965bb1ea26e1babfb46cdd).
2019-08-14 19:14:06 +03:00
..
analysis.go chore: change CompareBytes to EqualBytes (CityOfZion/neo-storm#22) 2019-08-14 19:14:06 +03:00
codegen.go chore: change CompareBytes to EqualBytes (CityOfZion/neo-storm#22) 2019-08-14 19:14:06 +03:00
compiler.go vm: switch from Opcode to Instruction 2019-08-14 15:40:31 +03:00
compiler_test.go fixed token example and fixed compiler test with multiple dirs (CityOfZion/neo-storm#20) 2019-08-14 19:14:05 +03:00
emit.go added partial syscall mapping 2019-08-14 18:55:29 +03:00
func_scope.go new mapping for interop api (CityOfZion/neo-storm#10) 2019-08-14 19:14:05 +03:00
README.md Add missing compiler interop API functions (#93) 2018-08-19 20:47:10 +02:00
syscall.go Draft of iterator and enumerator (CityOfZion/neo-storm#26) 2019-08-14 19:14:06 +03:00

NEO-GO smart contract compiler

The neo-go compiler compiles Go programs to bytecode that the NEO virtual machine can understand.

Currently supported

Go internals

  • type checking
  • multiple assignments
  • global variables
  • types int, string, byte and booleans
  • struct types + method receives
  • functions
  • composite literals []int, []string, []byte
  • basic if statements
  • binary expressions
  • return statements
  • for loops
  • imports

Go builtins

  • len
  • append

VM API (interop layer)

  • storage
  • runtime
  • block
  • header
  • transaction
  • asset
  • blockchain

VM utility helper functions

  • SHA1
  • SHA256
  • Hash256
  • Hash160
  • other..

Custom utility functions

  • FromAddress(address string) []byte

Not yet implemented

  • very small part of the interop layer (VM API)

Not supported

Due to the limitations of the NEO virtual machine, features listed below will not be supported.

  • channels
  • goroutines
  • multiple returns

Quick start

Compile a smart contract

./bin/neo-go contract compile -i mycontract.go

By default the filename will be the name of your .go file with the .avm extension, the file will be located in the same directory where your Go contract is. If you want another location for your compiled contract:

./bin/neo-go contract compile -i mycontract.go --out /Users/foo/bar/contract.avm

Debugging your smart contract

You can dump the opcodes generated by the compiler with the following command:

./bin/neo-go contract opdump -i mycontract.go

This will result in something like this:

INDEX    OPCODE    DESC
0        0x52      OpPush2
1        0xc5      OpNewArray
2        0x6b      OpToAltStack
3        0x 0      OpPush0
4        0x6c      OpFromAltStack
5        0x76      OpDup
6        0x6b      OpToAltStack
7        0x 0      OpPush0
8        0x52      OpPush2
9        0x7a      OpRoll
10       0xc4      OpSetItem

Test invoke a compiled contract

You can simulate a test invocation of your compiled contract by the VM, to know the total gas cost for example, with the following command:

./bin/neo-go contract testinvoke -i mycompiledcontract.avm

Will output something like:

{
  "state": "HALT, BREAK",
  "gas_consumed": "0.006",
  "Stack": [
    {
      "type": "Integer",
      "value": "9"
    }
  ]
}

Smart contract examples

Check if the invoker of the contract is the owning address

package mycontract

import (
    "github.com/CityOfZion/neo-go/pkg/vm/api/runtime"
    "github.com/CityOfZion/neo-go/pkg/vm/api/util"
)

var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg") 

func Main() bool {
    isOwner := runtime.CheckWitness(owner)

    if isOwner {
        runtime.Log("invoker is the owner")
        return true
    }

    return false
}

Simple token

package mytoken

import (
	"github.com/CityOfZion/neo-go/pkg/vm/api/runtime"
	"github.com/CityOfZion/neo-go/pkg/vm/api/storage"
)

var owner = util.FromAddress("AJX1jGfj3qPBbpAKjY527nPbnrnvSx9nCg") 

type Token struct {
	Name        string
	Symbol      string
	TotalSupply int
	Owner       []byte
}

func (t Token) AddToCirculation(amount int) bool {
	ctx := storage.Context()
	inCirc := storage.Get(ctx, "in_circ").(int)
	inCirc += amount
	storage.Put(ctx, "in_circ", inCirc)
	return true
}

func newToken() Token {
	return Token{
		Name:        "your awesome NEO token",
		Symbol:      "YANT",
		TotalSupply: 1000,
		Owner:       owner,
	}
}

func Main(operation string, args []interface{}) bool {
	token := newToken()
	trigger := runtime.GetTrigger()

	if trigger == runtime.Verification() {
		isOwner := runtime.CheckWitness(token.Owner)
		if isOwner {
			return true
		}
		return false
	}

	if trigger == runtime.Application() {
		if operation == "mintTokens" {
			token.AddToCirculation(100)
		}
	}

	return true
}

How to report compiler bugs

  1. Make a proper testcase (example testcases can be found in the tests folder)
  2. Create an issue on Github
  3. Make a PR with a reference to the created issue, containing the testcase that proves the bug
  4. Either you fix the bug yourself or wait for patch that solves the problem