From ad2a75a500daf800791973696b38c40758c31451 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 16 Jun 2020 11:54:48 +0300 Subject: [PATCH] core: move System.ExecutionEngine.* interops to System.Runtime.* --- examples/engine/engine.go | 9 +++-- examples/token/nep5/nep5.go | 3 +- pkg/compiler/syscall.go | 11 +++--- pkg/core/interops.go | 8 ++--- pkg/interop/engine/engine.go | 39 +--------------------- pkg/interop/runtime/engine.go | 38 +++++++++++++++++++++ pkg/rpc/server/server_test.go | 8 ++--- pkg/rpc/server/testdata/test_contract.avm | Bin 727 -> 727 bytes pkg/rpc/server/testdata/test_contract.go | 3 +- pkg/rpc/server/testdata/testblocks.acc | Bin 6784 -> 6784 bytes 10 files changed, 58 insertions(+), 61 deletions(-) create mode 100644 pkg/interop/runtime/engine.go diff --git a/examples/engine/engine.go b/examples/engine/engine.go index d5a914c68..e747863d1 100644 --- a/examples/engine/engine.go +++ b/examples/engine/engine.go @@ -1,22 +1,21 @@ package enginecontract import ( - "github.com/nspcc-dev/neo-go/pkg/interop/engine" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" ) // Main is that famous Main() function, you know. func Main() bool { - tx := engine.GetScriptContainer() + tx := runtime.GetScriptContainer() runtime.Notify(tx) - callingScriptHash := engine.GetCallingScriptHash() + callingScriptHash := runtime.GetCallingScriptHash() runtime.Notify(callingScriptHash) - execScriptHash := engine.GetExecutingScriptHash() + execScriptHash := runtime.GetExecutingScriptHash() runtime.Notify(execScriptHash) - entryScriptHash := engine.GetEntryScriptHash() + entryScriptHash := runtime.GetEntryScriptHash() runtime.Notify(entryScriptHash) return true diff --git a/examples/token/nep5/nep5.go b/examples/token/nep5/nep5.go index 742e0e3df..7082ad65b 100644 --- a/examples/token/nep5/nep5.go +++ b/examples/token/nep5/nep5.go @@ -1,7 +1,6 @@ package nep5 import ( - "github.com/nspcc-dev/neo-go/pkg/interop/engine" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" "github.com/nspcc-dev/neo-go/pkg/interop/util" @@ -85,7 +84,7 @@ func IsUsableAddress(addr []byte) bool { } // Check if a smart contract is calling scripthash - callingScriptHash := engine.GetCallingScriptHash() + callingScriptHash := runtime.GetCallingScriptHash() if util.Equals(callingScriptHash, addr) { return true } diff --git a/pkg/compiler/syscall.go b/pkg/compiler/syscall.go index 2a4b12fa9..d5c7c5908 100644 --- a/pkg/compiler/syscall.go +++ b/pkg/compiler/syscall.go @@ -24,6 +24,11 @@ var syscalls = map[string]map[string]string{ "Put": "System.Storage.Put", }, "runtime": { + "GetScriptContainer": "System.Runtime.GetScriptContainer", + "GetCallingScriptHash": "System.Runtime.GetCallingScriptHash", + "GetEntryScriptHash": "System.Runtime.GetEntryScriptHash", + "GetExecutingScriptHash": "System.Runtime.GetExecutingScriptHash", + "GetTrigger": "System.Runtime.GetTrigger", "CheckWitness": "System.Runtime.CheckWitness", "Notify": "System.Runtime.Notify", @@ -45,12 +50,6 @@ var syscalls = map[string]map[string]string{ "Destroy": "System.Contract.Destroy", "Update": "System.Contract.Update", }, - "engine": { - "GetScriptContainer": "System.ExecutionEngine.GetScriptContainer", - "GetCallingScriptHash": "System.ExecutionEngine.GetCallingScriptHash", - "GetEntryScriptHash": "System.ExecutionEngine.GetEntryScriptHash", - "GetExecutingScriptHash": "System.ExecutionEngine.GetExecutingScriptHash", - }, "iterator": { "Concat": "System.Iterator.Concat", "Create": "System.Iterator.Create", diff --git a/pkg/core/interops.go b/pkg/core/interops.go index e3af4d4ec..aa64a714c 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -99,10 +99,6 @@ var systemInterops = []interop.Function{ {Name: "System.Enumerator.Create", Func: enumerator.Create, Price: 400}, {Name: "System.Enumerator.Next", Func: enumerator.Next, Price: 1000000}, {Name: "System.Enumerator.Value", Func: enumerator.Value, Price: 400}, - {Name: "System.ExecutionEngine.GetCallingScriptHash", Func: engineGetCallingScriptHash, Price: 1}, - {Name: "System.ExecutionEngine.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 1}, - {Name: "System.ExecutionEngine.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 1}, - {Name: "System.ExecutionEngine.GetScriptContainer", Func: engineGetScriptContainer, Price: 1}, {Name: "System.Iterator.Concat", Func: iterator.Concat, Price: 400}, {Name: "System.Iterator.Create", Func: iterator.Create, Price: 400}, {Name: "System.Iterator.Key", Func: iterator.Key, Price: 400}, @@ -111,6 +107,10 @@ var systemInterops = []interop.Function{ {Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000}, {Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000}, {Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 200, RequiredFlags: smartcontract.AllowStates}, + {Name: "System.Runtime.GetCallingScriptHash", Func: engineGetCallingScriptHash, Price: 400}, + {Name: "System.Runtime.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 400}, + {Name: "System.Runtime.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 400}, + {Name: "System.Runtime.GetScriptContainer", Func: engineGetScriptContainer, Price: 250}, {Name: "System.Runtime.GetTime", Func: runtimeGetTime, Price: 1, AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates}, {Name: "System.Runtime.GetTrigger", Func: runtimeGetTrigger, Price: 1}, diff --git a/pkg/interop/engine/engine.go b/pkg/interop/engine/engine.go index ace2ceff3..d126a5b99 100644 --- a/pkg/interop/engine/engine.go +++ b/pkg/interop/engine/engine.go @@ -1,47 +1,10 @@ /* -Package engine provides access to VM execution metadata and allows to make contract calls. +Package engine allows to make contract calls. It's roughly similar in function to ExecutionEngine class in the Neo .net framework. */ package engine -import "github.com/nspcc-dev/neo-go/pkg/interop/blockchain" - -// GetScriptContainer returns the transaction that initially triggered current -// execution context. It never changes in a single execution, no matter how deep -// this execution goes. This function uses -// `System.ExecutionEngine.GetScriptContainer` syscall. -func GetScriptContainer() blockchain.Transaction { - return blockchain.Transaction{} -} - -// GetExecutingScriptHash returns script hash (160 bit in BE form represented -// as 20-byte slice) of the contract that is currently being executed. Any -// AppCall can change the value returned by this function if it calls a -// different contract. This function uses -// `System.ExecutionEngine.GetExecutingScriptHash` syscall. -func GetExecutingScriptHash() []byte { - return nil -} - -// GetCallingScriptHash returns script hash (160 bit in BE form represented -// as 20-byte slice) of the contract that started the execution of the currently -// running context (caller of current contract or function), so it's one level -// above the GetExecutingScriptHash in the call stack. It uses -// `System.ExecutionEngine.GetCallingScriptHash` syscall. -func GetCallingScriptHash() []byte { - return nil -} - -// GetEntryScriptHash returns script hash (160 bit in BE form represented -// as 20-byte slice) of the contract that initially started current execution -// (this is a script that is contained in a transaction returned by -// GetScriptContainer) execution from the start. This function uses -// `System.ExecutionEngine.GetEntryScriptHash` syscall. -func GetEntryScriptHash() []byte { - return nil -} - // AppCall executes previously deployed blockchain contract with specified hash // (160 bit in BE form represented as 20-byte slice) using provided arguments. // It returns whatever this contract returns. Even though this function accepts diff --git a/pkg/interop/runtime/engine.go b/pkg/interop/runtime/engine.go new file mode 100644 index 000000000..c71173c1b --- /dev/null +++ b/pkg/interop/runtime/engine.go @@ -0,0 +1,38 @@ +package runtime + +import "github.com/nspcc-dev/neo-go/pkg/interop/blockchain" + +// GetScriptContainer returns the transaction that initially triggered current +// execution context. It never changes in a single execution, no matter how deep +// this execution goes. This function uses +// `System.Runtime.GetScriptContainer` syscall. +func GetScriptContainer() blockchain.Transaction { + return blockchain.Transaction{} +} + +// GetExecutingScriptHash returns script hash (160 bit in BE form represented +// as 20-byte slice) of the contract that is currently being executed. Any +// AppCall can change the value returned by this function if it calls a +// different contract. This function uses +// `System.Runtime.GetExecutingScriptHash` syscall. +func GetExecutingScriptHash() []byte { + return nil +} + +// GetCallingScriptHash returns script hash (160 bit in BE form represented +// as 20-byte slice) of the contract that started the execution of the currently +// running context (caller of current contract or function), so it's one level +// above the GetExecutingScriptHash in the call stack. It uses +// `System.Runtime.GetCallingScriptHash` syscall. +func GetCallingScriptHash() []byte { + return nil +} + +// GetEntryScriptHash returns script hash (160 bit in BE form represented +// as 20-byte slice) of the contract that initially started current execution +// (this is a script that is contained in a transaction returned by +// GetScriptContainer) execution from the start. This function uses +// `System.Runtime.GetEntryScriptHash` syscall. +func GetEntryScriptHash() []byte { + return nil +} diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 60437d2b1..e206b6b03 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -50,18 +50,18 @@ type rpcTestCase struct { check func(t *testing.T, e *executor, result interface{}) } -const testContractHash = "279c86355b30d452386c1717e2ffa01c9dac31f3" +const testContractHash = "e65ff7b3a02d207b584a5c27057d4e9862ef01da" var rpcTestCases = map[string][]rpcTestCase{ "getapplicationlog": { { name: "positive", - params: `["136ef2ba8259d121a173da2588ae0fbb735f0f740820fded9cd8da8fee109b20"]`, + params: `["9352fa8d351635bb151e7e5a3a923bfe4d8fb90f05b605ec00af95c2410b594d"]`, result: func(e *executor) interface{} { return &result.ApplicationLog{} }, check: func(t *testing.T, e *executor, acc interface{}) { res, ok := acc.(*result.ApplicationLog) require.True(t, ok) - expectedTxHash, err := util.Uint256DecodeStringLE("136ef2ba8259d121a173da2588ae0fbb735f0f740820fded9cd8da8fee109b20") + expectedTxHash, err := util.Uint256DecodeStringLE("9352fa8d351635bb151e7e5a3a923bfe4d8fb90f05b605ec00af95c2410b594d") require.NoError(t, err) assert.Equal(t, expectedTxHash, res.TxHash) assert.Equal(t, 1, len(res.Executions)) @@ -483,7 +483,7 @@ var rpcTestCases = map[string][]rpcTestCase{ "gettransactionheight": { { name: "positive", - params: `["136ef2ba8259d121a173da2588ae0fbb735f0f740820fded9cd8da8fee109b20"]`, + params: `["9352fa8d351635bb151e7e5a3a923bfe4d8fb90f05b605ec00af95c2410b594d"]`, result: func(e *executor) interface{} { h := 0 return &h diff --git a/pkg/rpc/server/testdata/test_contract.avm b/pkg/rpc/server/testdata/test_contract.avm index 459d4e2218a10e24888c309d04009ca6d91f4c67..eec2f3ea44896e5efd3bca6d65da162028afd9bc 100755 GIT binary patch delta 16 Ycmcc4dYyGcB@@f-e=ABR*D^%`06&-qNdN!< delta 16 Xcmcc4dYyGcB@;{g;Y%r#Ynh?|JO>8U diff --git a/pkg/rpc/server/testdata/test_contract.go b/pkg/rpc/server/testdata/test_contract.go index 9eb87fe4e..c8d7609d0 100644 --- a/pkg/rpc/server/testdata/test_contract.go +++ b/pkg/rpc/server/testdata/test_contract.go @@ -1,7 +1,6 @@ package testdata import ( - "github.com/nspcc-dev/neo-go/pkg/interop/engine" "github.com/nspcc-dev/neo-go/pkg/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/interop/storage" ) @@ -71,7 +70,7 @@ func Main(operation string, args []interface{}) interface{} { return true case "init": ctx := storage.GetContext() - h := engine.GetExecutingScriptHash() + h := runtime.GetExecutingScriptHash() amount := totalSupply storage.Put(ctx, h, amount) runtime.Notify("transfer", []byte{}, h, amount) diff --git a/pkg/rpc/server/testdata/testblocks.acc b/pkg/rpc/server/testdata/testblocks.acc index 48261344581038f8c46b38b4dce64a704ccaeeaf..e175a3066ee5c7cbcc12965de385f2d1b5e047c9 100644 GIT binary patch delta 2595 zcmaKtX*|>m8-@*I45m|Li)>@fmh9W%Z>eVNF&IP4j4eW?(Tpj^5+Yd|WvmHF$1dx{ zo2|tzBzr}~*kvb9_4dA>&hI>*pHKJoyY4$wJybo>9Q^sCU44=@oYH5Zp=euwJ`1J1 zd;+Ygs^;{EHN9~j6{Gj2Ox|@1F<&mbHpXp(RgPT>i*lWwC@v@8NwXHR+pd2LZJ`+B zQ(!>nh%8?oiN++Tm~|`%&gRXz>g8EJHg9@SQW7K8e!yh>l#W|ei0E}3%liJ=ph?#b z#M?Vw(|^B}J4z>;BJI}z1H9>j@(l^K+Gs=N+bkSd=>6{;K`>@A+4F>!ZVIuZHZ%;L zl#;IGoKlyVcf%zp@=XTR2}WA;^z5%VKr#E`V1S-QW6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