new mapping for interop api (CityOfZion/neo-storm#10)

* new mapping for interop api

* Fixed interop API mapping + added missing apis

* added engine apis

Imported from CityOfZion/neo-storm (ec5e6c8e2b587704a1e071e83b633d2d3a235300).
This commit is contained in:
Anthony De Meulemeester 2018-08-22 09:51:35 +02:00 committed by Roman Khimov
parent 2fbb269c0d
commit 0b33cf3193
7 changed files with 181 additions and 83 deletions

22
examples/engine/engine.go Normal file
View file

@ -0,0 +1,22 @@
package engine_contract
import (
"github.com/CityOfZion/neo-storm/interop/engine"
"github.com/CityOfZion/neo-storm/interop/runtime"
)
func Main() bool {
tx := engine.GetScriptContainer()
runtime.Notify(tx)
callingScriptHash := engine.GetCallingScriptHash()
runtime.Notify(callingScriptHash)
execScriptHash := engine.GetExecutingScriptHash()
runtime.Notify(execScriptHash)
entryScriptHash := engine.GetEntryScriptHash()
runtime.Notify(entryScriptHash)
return true
}

View file

@ -1,5 +1,7 @@
package contract package contract
import "github.com/CityOfZion/neo-storm/interop/storage"
// Package contract provides function signatures that can be used inside // Package contract provides function signatures that can be used inside
// smart contracts that are written in the neo-storm framework. // smart contracts that are written in the neo-storm framework.
@ -15,3 +17,39 @@ func GetScript(c Contract) []byte {
func IsPayable(c Contract) bool { func IsPayable(c Contract) bool {
return false return false
} }
// GetStorageContext returns the storage context for the given contract.
func GetStorageContext(c Contract) storage.Context {
return storage.Context{}
}
// Create creates a new contract.
// @FIXME What is the type of the returnType here?
func Create(
script []byte,
params []interface{},
returnType byte,
properties interface{},
name,
version,
author,
email,
description string) {
}
// Migrate migrates a new contract.
// @FIXME What is the type of the returnType here?
func Migrate(
script []byte,
params []interface{},
returnType byte,
properties interface{},
name,
version,
author,
email,
description string) {
}
// Destroy deletes a contract that is registered on the blockchain.
func Destroy(c Contract) {}

29
interop/engine/engine.go Normal file
View file

@ -0,0 +1,29 @@
package engine
import "github.com/CityOfZion/neo-storm/interop/transaction"
// Package engine provides function signatures that can be used inside
// smart contracts that are written in the neo-storm framework.
// GetScriptContainer returns the transaction that is in the execution context.
func GetScriptContainer() transaction.Transaction {
return transaction.Transaction{}
}
// GetExecutingScriptHash returns the script hash of the contract that is
// currently being executed.
func GetExecutingScriptHash() []byte {
return nil
}
// GetCallingScriptHash returns the script hash of the contract that started
// the execution of the current script.
func GetCallingScriptHash() []byte {
return nil
}
// GetEntryScriptHash returns the script hash of the contract the started the
// execution from the start.
func GetEntryScriptHash() []byte {
return nil
}

View file

@ -201,19 +201,12 @@ func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool {
return false return false
} }
func isSyscall(name string) bool { func isSyscall(fun *funcScope) bool {
_, ok := syscalls[name] if fun.selector == nil {
return ok return false
}
// isNoRetSyscall checks if the syscall has a return value.
func isNoRetSyscall(name string) bool {
for _, s := range noRetSyscalls {
if s == name {
return true
}
} }
return false _, ok := syscalls[fun.selector.Name][fun.name]
return ok
} }
func isStringType(t types.Type) bool { func isStringType(t types.Type) bool {

View file

@ -185,7 +185,7 @@ func (c *codegen) convertFuncDecl(file ast.Node, decl *ast.FuncDecl) {
} }
// Load in all the global variables in to the scope of the function. // Load in all the global variables in to the scope of the function.
// This is not necessary for syscalls. // This is not necessary for syscalls.
if !isSyscall(f.name) { if !isSyscall(f) {
c.convertGlobals(file) c.convertGlobals(file)
} }
@ -394,7 +394,10 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
// Dont forget to add 1 extra argument when its a method. // Dont forget to add 1 extra argument when its a method.
numArgs++ numArgs++
} }
f, ok = c.funcs[fun.Sel.Name] f, ok = c.funcs[fun.Sel.Name]
// @FIXME this could cause runtime errors.
f.selector = fun.X.(*ast.Ident)
if !ok { if !ok {
log.Fatalf("could not resolve function %s", fun.Sel.Name) log.Fatalf("could not resolve function %s", fun.Sel.Name)
} }
@ -433,8 +436,8 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
// Use the ident to check, builtins are not in func scopes. // Use the ident to check, builtins are not in func scopes.
// We can be sure builtins are of type *ast.Ident. // We can be sure builtins are of type *ast.Ident.
c.convertBuiltin(n) c.convertBuiltin(n)
case isSyscall(f.name): case isSyscall(f):
c.convertSyscall(f.name) c.convertSyscall(f.selector.Name, f.name)
default: default:
emitCall(c.prog, vm.CALL, int16(f.label)) emitCall(c.prog, vm.CALL, int16(f.label))
} }
@ -531,8 +534,8 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return c return c
} }
func (c *codegen) convertSyscall(name string) { func (c *codegen) convertSyscall(api, name string) {
api, ok := syscalls[name] api, ok := syscalls[api][name]
if !ok { if !ok {
log.Fatalf("unknown VM syscall api: %s", name) log.Fatalf("unknown VM syscall api: %s", name)
} }

View file

@ -10,6 +10,10 @@ type funcScope struct {
// identifier of the function. // identifier of the function.
name string name string
// Selector of the function if there is any. Only functions imported
// from other packages should have a selector.
selector *ast.Ident
// The declaration of the function in the AST. Nil if this scope is not a function. // The declaration of the function in the AST. Nil if this scope is not a function.
decl *ast.FuncDecl decl *ast.FuncDecl

View file

@ -1,69 +1,78 @@
package compiler package compiler
var syscalls = map[string]string{ var syscalls = map[string]map[string]string{
// "storage": {
// Standard library API "GetContext": "System.Storage.GetContext",
// "Put": "System.Storage.Put",
"Get": "System.Storage.Get",
// Storage API "Delete": "System.Storage.Delete",
"GetContext": "System.Storage.GetContext", "Find": "System.Storage.Find",
"Put": "System.Storage.Put", },
"Get": "System.Storage.Get", "runtime": {
"Delete": "System.Storage.Delete", "GetTrigger": "System.Runtime.GetTrigger",
"Find": "System.Storage.Find", "CheckWitness": "System.Runtime.CheckWitness",
"Notify": "System.Runtime.Notify",
// Runtime API "Log": "System.Runtime.Log",
"GetTrigger": "System.Runtime.GetTrigger", "GetTime": "System.Runtime.GetTime",
"CheckWitness": "System.Runtime.CheckWitness", "Serialize": "System.Runtime.Serialize",
"Notify": "System.Runtime.Notify", "Deserialize": "System.Runtime.Deserialize",
"Log": "System.Runtime.Log", },
"GetTime": "System.Runtime.GetTime", "blockchain": {
"Serialize": "System.Runtime.Serialize", "GetHeight": "System.Blockchain.GetHeight",
"Deserialize": "System.Runtime.Deserialize", "GetHeader": "System.Blockchain.GetHeader",
"GetBlock": "System.Blockchain.GetBlock",
// Blockchain API "GetTransaction": "System.Blockchain.GetTransaction",
"GetHeight": "System.Blockchain.GetHeight", "GetContract": "System.Blockchain.GetContract",
"GetHeader": "System.Blockchain.GetHeader", "GetAccount": "Neo.Blockchain.GetAccount",
"GetBlock": "System.Blockchain.GetBlock", "GetValidators": "Neo.Blockchain.GetValidators",
"GetTransaction": "System.Blockchain.GetTransaction", "GetAsset": "Neo.Blockchain.GetAsset",
"GetTransactionHeight": "System.Blockchain.GetTransactionHeight", },
"GetContract": "System.Blockchain.GetContract", "header": {
"GetIndex": "System.Header.GetIndex",
// Header API "GetHash": "System.Header.GetHash",
"GetIndex": "System.Header.GetContract", "GetPrevHash": "System.Header.GetPrevHash",
"GetHash": "System.Header.GetHash", "GetTimestamp": "System.Header.GetTimestamp",
"GetPrevHash": "System.Header.GetPrevHash", "GetVersion": "Neo.Header.GetVersion",
"GetTimestamp": "System.Header.GetTimestamp", "GetMerkleRoot": "Neo.Header.GetMerkleRoot",
"GetConsensusData": "Neo.Header.GetConsensusData",
// Block API "GetNextConsensus": "Neo.Header.GetNextConsensus",
"GetTransactionCount": "System.Block.GetTransactionCount", },
"GetTransactions": "System.Block.GetTransactions", "block": {
// TODO: Find solution for duplicated map entry "GetTransactionCount": "System.Block.GetTransactionCount",
"NGetTransaction": "System.Block.GetTransaction", "GetTransactions": "System.Block.GetTransactions",
"GetTransaction": "System.Block.GetTransaction",
// },
// NEO specific API "transaction": {
// "GetType": "Neo.Transaction.GetType",
"GetAttributes": "Neo.Transaction.GetAttributes",
// Blockchain API "GetInputs": "Neo.Transaction.GetInputs",
"GetAccount": "Neo.Blockchain.GetAccount", "GetOutputs": "Neo.Transaction.GetOutputs",
"GetValidators": "Neo.Blockchain.GetValidators", "GetReferences": "Neo.Transaction.GetReferences",
"GetAsset": "Neo.Blockchain.GetAsset", "GetUnspentCoins": "Neo.Transaction.GetUnspentCoins",
"GetScript": "Neo.Transaction.GetScript",
// Header API },
"GetVersion": "Neo.Header.GetVersion", "asset": {
"GetMerkleRoot": "Neo.Header.GetMerkleRoot", "GetAssetID": "Neo.Asset.GetAssetID",
"GetConsensusData": "Neo.Header.GetConsensusData", "GetAssetType": "Neo.Asset.GetAssetType",
"GetNextConsensus": "Neo.Header.GetNextConsensus", "GetAmount": "Neo.Asset.GetAmount",
"Create": "Neo.Asset.Create",
// Transaction API "Renew": "Neo.Asset.Renew",
"GetType": "Neo.Transaction.GetType", },
"GetAttributes": "Neo.Transaction.GetAttributes", "contract": {
"GetInputs": "Neo.Transaction.GetInputs", "GetScript": "Neo.Contract.GetScript",
"GetOutputs": "Neo.Transaction.GetOutputs", "IsPayable": "Neo.Contract.IsPayable",
"GetReferences": "Neo.Transaction.GetReferences", "Create": "Neo.Contract.Create",
"GetUnspentCoins": "Neo.Transaction.GetUnspentCoins", "Destroy": "Neo.Contract.Destroy",
"GetScript": "Neo.InvocationTransaction.GetScript", "Migrate": "Neo.Contract.Migrate",
"GetStorageContext": "Neo.Contract.GetStorageContext",
// TODO: Add the rest of the interop APIS },
"input": {
"GetHash": "Neo.Input.GetHash",
"GetIndex": "Neo.Input.GetIndex",
},
"output": {
"GetAssetID": "Neo.Output.GetAssetID",
"GetValue": "Neo.Output.GetValue",
"GetScriptHash": "Neo.Output.GetScriptHash",
},
} }