From 4c8d327353c05d1ee83b499d293aa55a0793a35c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 3 Mar 2020 13:08:34 +0300 Subject: [PATCH] rpc: drop duplicating Invoke* structures And use smartcontract.Parameter instead of vm.StackItem where appropriate. Closes #689. --- cli/smartcontract/smart_contract.go | 4 ++-- pkg/rpc/client/rpc.go | 13 ++++++------- pkg/rpc/response/result/invoke.go | 4 ++-- pkg/rpc/response/types.go | 11 ----------- pkg/rpc/server/server.go | 2 +- pkg/rpc/server/server_helper_test.go | 10 ---------- pkg/rpc/server/server_test.go | 12 ++++++------ 7 files changed, 17 insertions(+), 39 deletions(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index db59a2844..aec4f901f 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -16,7 +16,7 @@ import ( "github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/rpc/client" "github.com/CityOfZion/neo-go/pkg/rpc/request" - "github.com/CityOfZion/neo-go/pkg/rpc/response" + "github.com/CityOfZion/neo-go/pkg/rpc/response/result" "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/vm" @@ -364,7 +364,7 @@ func invokeInternal(ctx *cli.Context, withMethod bool, signAndPush bool) error { operation string params = make([]smartcontract.Parameter, 0) paramsStart = 1 - resp *response.InvokeResult + resp *result.Invoke wif *keys.WIF ) diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index 5cb38dbeb..0fdf06ab7 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -7,7 +7,6 @@ import ( "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/rpc/request" - "github.com/CityOfZion/neo-go/pkg/rpc/response" "github.com/CityOfZion/neo-go/pkg/rpc/response/result" "github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/util" @@ -68,10 +67,10 @@ func (c *Client) GetUnspents(address string) (*result.Unspents, error) { // InvokeScript returns the result of the given script after running it true the VM. // NOTE: This is a test invoke and will not affect the blockchain. -func (c *Client) InvokeScript(script string) (*response.InvokeResult, error) { +func (c *Client) InvokeScript(script string) (*result.Invoke, error) { var ( params = request.NewRawParams(script) - resp = &response.InvokeResult{} + resp = &result.Invoke{} ) if err := c.performRequest("invokescript", params, resp); err != nil { return nil, err @@ -82,10 +81,10 @@ func (c *Client) InvokeScript(script string) (*response.InvokeResult, error) { // InvokeFunction returns the results after calling the smart contract scripthash // with the given operation and parameters. // NOTE: this is test invoke and will not affect the blockchain. -func (c *Client) InvokeFunction(script, operation string, params []smartcontract.Parameter) (*response.InvokeResult, error) { +func (c *Client) InvokeFunction(script, operation string, params []smartcontract.Parameter) (*result.Invoke, error) { var ( p = request.NewRawParams(script, operation, params) - resp = &response.InvokeResult{} + resp = &result.Invoke{} ) if err := c.performRequest("invokefunction", p, resp); err != nil { return nil, err @@ -95,10 +94,10 @@ func (c *Client) InvokeFunction(script, operation string, params []smartcontract // Invoke returns the results after calling the smart contract scripthash // with the given parameters. -func (c *Client) Invoke(script string, params []smartcontract.Parameter) (*response.InvokeResult, error) { +func (c *Client) Invoke(script string, params []smartcontract.Parameter) (*result.Invoke, error) { var ( p = request.NewRawParams(script, params) - resp = &response.InvokeResult{} + resp = &result.Invoke{} ) if err := c.performRequest("invoke", p, resp); err != nil { return nil, err diff --git a/pkg/rpc/response/result/invoke.go b/pkg/rpc/response/result/invoke.go index f79d4d365..171fd1b65 100644 --- a/pkg/rpc/response/result/invoke.go +++ b/pkg/rpc/response/result/invoke.go @@ -1,7 +1,7 @@ package result import ( - "github.com/CityOfZion/neo-go/pkg/vm" + "github.com/CityOfZion/neo-go/pkg/smartcontract" ) // Invoke represents code invocation result and is used by several RPC calls @@ -10,5 +10,5 @@ type Invoke struct { State string `json:"state"` GasConsumed string `json:"gas_consumed"` Script string `json:"script"` - Stack *vm.Stack + Stack []smartcontract.Parameter } diff --git a/pkg/rpc/response/types.go b/pkg/rpc/response/types.go index a655d6bab..7c53040fc 100644 --- a/pkg/rpc/response/types.go +++ b/pkg/rpc/response/types.go @@ -4,19 +4,8 @@ import ( "encoding/json" "github.com/CityOfZion/neo-go/pkg/rpc/response/result" - "github.com/CityOfZion/neo-go/pkg/smartcontract" - "github.com/CityOfZion/neo-go/pkg/vm" ) -// InvokeResult represents the outcome of a script that is -// executed by the NEO VM. -type InvokeResult struct { - State vm.State `json:"state"` - GasConsumed string `json:"gas_consumed"` - Script string `json:"script"` - Stack []smartcontract.Parameter -} - // Header is a generic JSON-RPC 2.0 response header (ID and JSON-RPC version). type Header struct { ID json.RawMessage `json:"id"` diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 119786933..9b5967554 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -624,7 +624,7 @@ func (s *Server) runScriptInVM(script []byte) *result.Invoke { State: vm.State(), GasConsumed: vm.GasConsumed().String(), Script: hex.EncodeToString(script), - Stack: vm.Estack(), + Stack: vm.Estack().ToContractParameters(), } return result } diff --git a/pkg/rpc/server/server_helper_test.go b/pkg/rpc/server/server_helper_test.go index 39198c317..6bfeaba4c 100644 --- a/pkg/rpc/server/server_helper_test.go +++ b/pkg/rpc/server/server_helper_test.go @@ -12,21 +12,11 @@ import ( "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/network" - "github.com/CityOfZion/neo-go/pkg/rpc/request" "github.com/CityOfZion/neo-go/pkg/util" "github.com/stretchr/testify/require" "go.uber.org/zap/zaptest" ) -// InvokeFunctionResult struct for testing. -type InvokeFunctionResult struct { - Script string `json:"script"` - State string `json:"state"` - GasConsumed string `json:"gas_consumed"` - Stack []request.FuncParam `json:"stack"` - TX string `json:"tx,omitempty"` -} - func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFunc) { var nBlocks uint32 diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 4686efabb..52f0ea008 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -474,9 +474,9 @@ var rpcTestCases = map[string][]rpcTestCase{ { name: "positive", params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", [{"type": "String", "value": "qwerty"}]]`, - result: func(e *executor) interface{} { return &InvokeFunctionResult{} }, + result: func(e *executor) interface{} { return &result.Invoke{} }, check: func(t *testing.T, e *executor, inv interface{}) { - res, ok := inv.(*InvokeFunctionResult) + res, ok := inv.(*result.Invoke) require.True(t, ok) assert.Equal(t, "06717765727479676f459162ceeb248b071ec157d9e4f6fd26fdbe50", res.Script) assert.NotEqual(t, "", res.State) @@ -513,9 +513,9 @@ var rpcTestCases = map[string][]rpcTestCase{ { name: "positive", params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", "test", []]`, - result: func(e *executor) interface{} { return &InvokeFunctionResult{} }, + result: func(e *executor) interface{} { return &result.Invoke{} }, check: func(t *testing.T, e *executor, inv interface{}) { - res, ok := inv.(*InvokeFunctionResult) + res, ok := inv.(*result.Invoke) require.True(t, ok) assert.NotEqual(t, "", res.Script) assert.NotEqual(t, "", res.State) @@ -547,9 +547,9 @@ var rpcTestCases = map[string][]rpcTestCase{ { name: "positive", params: `["51c56b0d48656c6c6f2c20776f726c6421680f4e656f2e52756e74696d652e4c6f67616c7566"]`, - result: func(e *executor) interface{} { return &InvokeFunctionResult{} }, + result: func(e *executor) interface{} { return &result.Invoke{} }, check: func(t *testing.T, e *executor, inv interface{}) { - res, ok := inv.(*InvokeFunctionResult) + res, ok := inv.(*result.Invoke) require.True(t, ok) assert.NotEqual(t, "", res.Script) assert.NotEqual(t, "", res.State)