diff --git a/pkg/vm/compiler/analysis.go b/pkg/vm/compiler/analysis.go index 10ee90242..2c8422aa9 100644 --- a/pkg/vm/compiler/analysis.go +++ b/pkg/vm/compiler/analysis.go @@ -6,7 +6,6 @@ import ( "go/types" "log" - "github.com/CityOfZion/neo-go/pkg/vm" "golang.org/x/tools/go/loader" ) @@ -203,7 +202,7 @@ func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool { } func isSyscall(name string) bool { - _, ok := vm.Syscalls[name] + _, ok := syscalls[name] return ok } diff --git a/pkg/vm/compiler/codegen.go b/pkg/vm/compiler/codegen.go index e88e3a861..3756eaac3 100644 --- a/pkg/vm/compiler/codegen.go +++ b/pkg/vm/compiler/codegen.go @@ -532,7 +532,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { } func (c *codegen) convertSyscall(name string) { - api, ok := vm.Syscalls[name] + api, ok := syscalls[name] if !ok { log.Fatalf("unknown VM syscall api: %s", name) } diff --git a/pkg/vm/compiler/emit.go b/pkg/vm/compiler/emit.go index 7805a88c9..584979e5d 100644 --- a/pkg/vm/compiler/emit.go +++ b/pkg/vm/compiler/emit.go @@ -12,16 +12,16 @@ import ( "github.com/CityOfZion/neo-go/pkg/vm" ) -func emit(w *bytes.Buffer, op vm.Instruction, b []byte) error { - if err := w.WriteByte(byte(op)); err != nil { +func emit(w *bytes.Buffer, instr vm.Instruction, b []byte) error { + if err := w.WriteByte(byte(instr)); err != nil { return err } _, err := w.Write(b) return err } -func emitOpcode(w io.ByteWriter, op vm.Instruction) error { - return w.WriteByte(byte(op)) +func emitOpcode(w io.ByteWriter, instr vm.Instruction) error { + return w.WriteByte(byte(instr)) } func emitBool(w io.ByteWriter, ok bool) error { @@ -89,21 +89,21 @@ func emitSyscall(w *bytes.Buffer, api string) error { return emit(w, vm.SYSCALL, buf) } -func emitCall(w *bytes.Buffer, op vm.Instruction, label int16) error { - return emitJmp(w, op, label) +func emitCall(w *bytes.Buffer, instr vm.Instruction, label int16) error { + return emitJmp(w, instr, label) } -func emitJmp(w *bytes.Buffer, op vm.Instruction, label int16) error { - if !isInstructionJmp(op) { - return fmt.Errorf("opcode %s is not a jump or call type", op) +func emitJmp(w *bytes.Buffer, instr vm.Instruction, label int16) error { + if !isInstrJmp(instr) { + return fmt.Errorf("opcode %s is not a jump or call type", instr) } buf := make([]byte, 2) binary.LittleEndian.PutUint16(buf, uint16(label)) - return emit(w, op, buf) + return emit(w, instr, buf) } -func isInstructionJmp(op vm.Instruction) bool { - if op == vm.JMP || op == vm.JMPIFNOT || op == vm.JMPIF || op == vm.CALL { +func isInstrJmp(instr vm.Instruction) bool { + if instr == vm.JMP || instr == vm.JMPIFNOT || instr == vm.JMPIF || instr == vm.CALL { return true } return false diff --git a/pkg/vm/compiler/syscall.go b/pkg/vm/compiler/syscall.go new file mode 100644 index 000000000..ec545a51e --- /dev/null +++ b/pkg/vm/compiler/syscall.go @@ -0,0 +1,69 @@ +package compiler + +var syscalls = map[string]string{ + // + // Standard library API + // + + // Storage API + "GetContext": "System.Storage.GetContext", + "Put": "System.Storage.Put", + "Get": "System.Storage.Get", + "Delete": "System.Storage.Delete", + "Find": "System.Storage.Find", + + // Runtime API + "GetTrigger": "System.Runtime.GetTrigger", + "CheckWitness": "System.Runtime.CheckWitness", + "Notify": "System.Runtime.Notify", + "Log": "System.Runtime.Log", + "GetTime": "System.Runtime.GetTime", + "Serialize": "System.Runtime.Serialize", + "Deserialize": "System.Runtime.Deserialize", + + // Blockchain API + "GetHeight": "System.Blockchain.GetHeight", + "GetHeader": "System.Blockchain.GetHeader", + "GetBlock": "System.Blockchain.GetBlock", + "GetTransaction": "System.Blockchain.GetTransaction", + "GetTransactionHeight": "System.Blockchain.GetTransactionHeight", + "GetContract": "System.Blockchain.GetContract", + + // Header API + "GetIndex": "System.Header.GetContract", + "GetHash": "System.Header.GetHash", + "GetPrevHash": "System.Header.GetPrevHash", + "GetTimestamp": "System.Header.GetTimestamp", + + // Block API + "GetTransactionCount": "System.Block.GetTransactionCount", + "GetTransactions": "System.Block.GetTransactions", + // TODO: Find solution for duplicated map entry + "NGetTransaction": "System.Block.GetTransaction", + + // + // NEO specific API + // + + // Blockchain API + "GetAccount": "Neo.Blockchain.GetAccount", + "GetValidators": "Neo.Blockchain.GetValidators", + "GetAsset": "Neo.Blockchain.GetAsset", + + // Header API + "GetVersion": "Neo.Header.GetVersion", + "GetMerkleRoot": "Neo.Header.GetMerkleRoot", + "GetConsensusData": "Neo.Header.GetConsensusData", + "GetNextConsensus": "Neo.Header.GetNextConsensus", + + // Transaction API + "GetType": "Neo.Transaction.GetType", + "GetAttributes": "Neo.Transaction.GetAttributes", + "GetInputs": "Neo.Transaction.GetInputs", + "GetOutputs": "Neo.Transaction.GetOutputs", + "GetReferences": "Neo.Transaction.GetReferences", + "GetUnspentCoins": "Neo.Transaction.GetUnspentCoins", + "GetScript": "Neo.InvocationTransaction.GetScript", + + // TODO: Add the rest of the interop APIS +} diff --git a/pkg/vm/syscall.go b/pkg/vm/syscall.go deleted file mode 100644 index 69f2ee1e3..000000000 --- a/pkg/vm/syscall.go +++ /dev/null @@ -1,19 +0,0 @@ -package vm - -// Syscalls are a mapping between the syscall function name -// and the registered VM interop API. -var Syscalls = map[string]string{ - // Storage API - "GetContext": "Neo.Storage.GetContext", - "Put": "Neo.Storage.Put", - "Get": "Neo.Storage.Get", - "Delete": "Neo.Storage.Delete", - - // Runtime API - "GetTrigger": "Neo.Runtime.GetTrigger", - "CheckWitness": "Neo.Runtime.CheckWitness", - "GetCurrentBlock": "Neo.Runtime.GetCurrentBlock", - "GetTime": "Neo.Runtime.GetTime", - "Notify": "Neo.Runtime.Notify", - "Log": "Neo.Runtime.Log", -}