From 523789ee1ca6cb8d62dcf7bc189d9cc2705c632e Mon Sep 17 00:00:00 2001 From: Anthony De Meulemeester Date: Tue, 21 Aug 2018 12:57:48 +0200 Subject: [PATCH] Compiler interop APIs (CityOfZion/neo-storm#5) * added draft of block and transaction interop api. * added header interop API * added attribute, transaction, input, output interop API * Added asset, attribute and account interop api. * added Runtime interop apis * added asset renew and create + contract and asset interop apis Imported from CityOfZion/neo-storm (b6810d58b98312a959980f344db24689839574c4). --- interop/account/account.go | 23 +++++++++++++ interop/asset/asset.go | 53 ++++++++++++++++++++++++++++++ interop/attribute/attribute.go | 17 ++++++++++ interop/block/block.go | 25 ++++++++++++++ interop/blockchain/blockchain.go | 53 ++++++++++++++++++++++++++++++ interop/contract/contract.go | 17 ++++++++++ interop/header/header.go | 47 ++++++++++++++++++++++++++ interop/input/input.go | 17 ++++++++++ interop/output/output.go | 22 +++++++++++++ interop/runtime/runtime.go | 34 ++++++++++++++----- interop/storage/storage.go | 3 ++ interop/transaction/transaction.go | 50 ++++++++++++++++++++++++++++ 12 files changed, 353 insertions(+), 8 deletions(-) create mode 100644 interop/account/account.go create mode 100644 interop/asset/asset.go create mode 100644 interop/attribute/attribute.go create mode 100644 interop/block/block.go create mode 100644 interop/blockchain/blockchain.go create mode 100644 interop/contract/contract.go create mode 100644 interop/header/header.go create mode 100644 interop/input/input.go create mode 100644 interop/output/output.go create mode 100644 interop/transaction/transaction.go diff --git a/interop/account/account.go b/interop/account/account.go new file mode 100644 index 000000000..04982aa1b --- /dev/null +++ b/interop/account/account.go @@ -0,0 +1,23 @@ +package account + +// Package account provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Account stubs a NEO account type. +type Account struct{} + +// GetScripHash returns the script hash of the given account. +func GetScriptHash(a Account) []byte { + return nil +} + +// GetVotes returns the votes of the given account which should be a slice of +// public key raw bytes. +func GetVotes(a Account) [][]byte { + return nil +} + +// GetBalance returns the balance of for the given account and asset id. +func GetBalance(a Account, assetID []byte) int { + return 0 +} diff --git a/interop/asset/asset.go b/interop/asset/asset.go new file mode 100644 index 000000000..6e8a436db --- /dev/null +++ b/interop/asset/asset.go @@ -0,0 +1,53 @@ +package asset + +// Package asset provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Asset stubs a NEO asset type. +type Asset struct{} + +// GetAssetID returns the id of the given asset. +func GetAssetID(a Asset) []byte { + return nil +} + +// GetAssetType returns the type of the given asset. +func GetAssetType(a Asset) byte { + return 0x00 +} + +// GetAmount returns the amount of the given asset. +func GetAmount(a Asset) int { + return 0 +} + +// GetAvailable returns the available of the given asset. +func GetAvailable(a Asset) int { + return 0 +} + +// GetPrecision returns the precision of the given asset. +func GetPrecision(a Asset) byte { + return 0x00 +} + +// GetOwner returns the owner of the given asset. +func GetOwner(a Asset) []byte { + return nil +} + +// GetAdmin returns the admin of the given asset. +func GetAdmin(a Asset) []byte { + return nil +} + +// GetIssuer returns the issuer of the given asset. +func GetIssuer(a Asset) []byte { + return nil +} + +// Create registers a new asset on the blockchain. +func Create(assetType byte, name string, amount int, precision byte, owner, admin, issuer []byte) {} + +// Renew renews the existance of an asset by the given years. +func Renew(asset Asset, years int) {} diff --git a/interop/attribute/attribute.go b/interop/attribute/attribute.go new file mode 100644 index 000000000..b1fb82667 --- /dev/null +++ b/interop/attribute/attribute.go @@ -0,0 +1,17 @@ +package attribute + +// Package attribute provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Attribute stubs a NEO transaction attribute type. +type Attribute struct{} + +// GetUsage returns the usage of the given attribute. +func GetUsage(attr Attribute) byte { + return 0x00 +} + +// GetData returns the data of the given attribute. +func GetData(attr Attribute) []byte { + return nil +} diff --git a/interop/block/block.go b/interop/block/block.go new file mode 100644 index 000000000..c1368a6ed --- /dev/null +++ b/interop/block/block.go @@ -0,0 +1,25 @@ +package block + +import "github.com/CityOfZion/neo-go-sc/interop/transaction" + +// Package block provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Block stubs a NEO block type. +type Block struct{} + +// GetTransactionCount return the number of recorded transactions in the given block. +func GetTransactionCount(b Block) int { + return 0 +} + +// GetTransactions returns a slice of transactions recorded in the given block. +func GetTransactions(b Block) []transaction.Transaction { + return []transaction.Transaction{} +} + +// GetTransaction returns a transaction from the given a block hash of the +// transaction. +func GetTransaction(b Block, hash []byte) transaction.Transaction { + return transaction.Transaction{} +} diff --git a/interop/blockchain/blockchain.go b/interop/blockchain/blockchain.go new file mode 100644 index 000000000..a3dc184c9 --- /dev/null +++ b/interop/blockchain/blockchain.go @@ -0,0 +1,53 @@ +package blockchain + +import ( + "github.com/CityOfZion/neo-go-sc/interop/account" + "github.com/CityOfZion/neo-go-sc/interop/asset" + "github.com/CityOfZion/neo-go-sc/interop/block" + "github.com/CityOfZion/neo-go-sc/interop/contract" + "github.com/CityOfZion/neo-go-sc/interop/header" + "github.com/CityOfZion/neo-go-sc/interop/transaction" +) + +// Package blockchain provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// GetHeight returns the height of te block recorded in the current execution scope. +func GetHeight() int { + return 0 +} + +// GetHeader returns the header found by the given hash or index. +func GetHeader(heightOrHash interface{}) header.Header { + return header.Header{} +} + +// GetBlock returns the block found by the given hash or index. +func GetBlock(heightOrHash interface{}) block.Block { + return block.Block{} +} + +// GetTransaction returns the transaction found by the given hash. +func GetTransaction(hash []byte) transaction.Transaction { + return transaction.Transaction{} +} + +// GetContract returns the contract found by the given script hash. +func GetContract(scriptHash []byte) contract.Contract { + return contract.Contract{} +} + +// GetAccount returns the account found by the given script hash. +func GetAccount(scriptHash []byte) account.Account { + return account.Account{} +} + +// GetValidators returns a slice of validator addresses. +func GetValidators() [][]byte { + return nil +} + +// GetAsset returns the asset found by the given asset id. +func GetAsset(assetID []byte) asset.Asset { + return asset.Asset{} +} diff --git a/interop/contract/contract.go b/interop/contract/contract.go new file mode 100644 index 000000000..2c6555751 --- /dev/null +++ b/interop/contract/contract.go @@ -0,0 +1,17 @@ +package contract + +// Package contract provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Contract stubs a NEO contract type. +type Contract struct{} + +// GetScript returns the script of the given contract. +func GetScript(c Contract) []byte { + return nil +} + +// IsPayable returns whether the given contract is payable. +func IsPayable(c Contract) bool { + return false +} diff --git a/interop/header/header.go b/interop/header/header.go new file mode 100644 index 000000000..cc7f23052 --- /dev/null +++ b/interop/header/header.go @@ -0,0 +1,47 @@ +package header + +// Package header provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Header stubs a NEO block header type. +type Header struct{} + +// GetIndex returns the index of the given header. +func GetIndex(h Header) int { + return 0 +} + +// GetHash returns the hash of the given header. +func GetHash(h Header) []byte { + return nil +} + +// GetPrevHash returns the previous hash of the given header. +func GetPrevHash(h Header) []byte { + return nil +} + +// GetTimestamp returns the timestamp of the given header. +func GetTimestamp(h Header) int { + return 0 +} + +// GetVersion returns the version of the given header. +func GetVersion(h Header) int { + return 0 +} + +// GetMerkleRoot returns the merkle root of the given header. +func GetMerkleRoot(h Header) []byte { + return nil +} + +// GetConsensusData returns the consensus data of the given header. +func GetConsensusData(h Header) int { + return 0 +} + +// GetNextConsensus returns the next consensus of the given header. +func GetNextConsensus(h Header) []byte { + return nil +} diff --git a/interop/input/input.go b/interop/input/input.go new file mode 100644 index 000000000..63f97a1c6 --- /dev/null +++ b/interop/input/input.go @@ -0,0 +1,17 @@ +package input + +// Package input provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Input stubs the input of a NEO transaction. +type Input struct{} + +// GetHash returns the hash of the given input. +func GetHash(in Input) []byte { + return nil +} + +// GetIndex returns the index of the given input. +func GetIndex(in Input) int { + return 0 +} diff --git a/interop/output/output.go b/interop/output/output.go new file mode 100644 index 000000000..90bd0d567 --- /dev/null +++ b/interop/output/output.go @@ -0,0 +1,22 @@ +package output + +// Package output provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Output stubs the output of a NEO transaction. +type Output struct{} + +// GetAssetID returns the asset id of the given output. +func GetAssetID(out Output) []byte { + return nil +} + +// GetValue returns the value of the given output. +func GetValue(out Output) int { + return 0 +} + +// GetScriptHash returns the script hash of the given output. +func GetScriptHash(out Output) []byte { + return nil +} diff --git a/interop/runtime/runtime.go b/interop/runtime/runtime.go index 3d85288c4..367f07600 100644 --- a/interop/runtime/runtime.go +++ b/interop/runtime/runtime.go @@ -1,17 +1,31 @@ package runtime -// CheckWitness verifies if the given hash is the invoker of the contract +// Package runtime provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// CheckWitness verifies if the given hash is the invoker of the contract. func CheckWitness(hash []byte) bool { return true } -// Notify passes data to the VM +// Log instucts the VM to log the given message. +func Log(message string) {} + +// Notify an event to the VM. func Notify(arg interface{}) int { return 0 } -// Log passes a message to the VM -func Log(message string) {} +// GetTime returns the timestamp of the most recent block. +func GetTime() int { + return 0 +} + +// GetTrigger returns the smart contract invoke trigger which can be either +// verification or application. +func GetTrigger() byte { + return 0x00 +} // Application returns the Application trigger type func Application() byte { @@ -23,8 +37,12 @@ func Verification() byte { return 0x00 } -// GetTrigger return the current trigger type. The return in this function -// Doesn't really matter, this is just an interop placeholder -func GetTrigger() interface{} { - return 0 +// Serialize serializes and item into a bytearray. +func Serialize(item interface{}) []byte { + return nil +} + +// Deserializes an item from a bytearray. +func Deserialize(b []byte) interface{} { + return nil } diff --git a/interop/storage/storage.go b/interop/storage/storage.go index 8e5c0b6d9..5f6d0b375 100644 --- a/interop/storage/storage.go +++ b/interop/storage/storage.go @@ -1,5 +1,8 @@ package storage +// Package storage provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + // Context represents the storage context type Context struct{} diff --git a/interop/transaction/transaction.go b/interop/transaction/transaction.go new file mode 100644 index 000000000..d02ee086e --- /dev/null +++ b/interop/transaction/transaction.go @@ -0,0 +1,50 @@ +package transaction + +import ( + "github.com/CityOfZion/neo-go-sc/interop/attribute" + "github.com/CityOfZion/neo-go-sc/interop/input" + "github.com/CityOfZion/neo-go-sc/interop/output" +) + +// Package transaction provides function signatures that can be used inside +// smart contracts that are written in the neo-go-sc framework. + +// Transaction stubs a NEO transaction type. +type Transaction struct{} + +// GetHash returns the hash of the given transaction. +func GetHash(t Transaction) []byte { + return nil +} + +// GetType returns the type of the given transaction. +func GetType(t Transaction) byte { + return 0x00 +} + +// GetAttributes returns a slice of attributes for the given transaction. +func GetAttributes(t Transaction) []attribute.Attribute { + return []attribute.Attribute{} +} + +// FIXME: What is the correct return type for this? +// GetReferences returns a slice of references for the given transaction. +func GetReferences(t Transaction) interface{} { + return 0 +} + +// FIXME: What is the correct return type for this? +// GetUnspentCoins returns the unspent coins for the given transaction. +func GetUnspentCoins(t Transaction) interface{} { + return 0 +} + +// GetInputs returns the inputs of the given transaction. +func GetInputs(t Transaction) []input.Input { + return []input.Input{} +} + +// GetOutputs returns the outputs of the given transaction. +func GetOutputs(t Transaction) []output.Output { + return []output.Output{} +}