rpc: drop duplicating Invoke* structures

And use smartcontract.Parameter instead of vm.StackItem where
appropriate. Closes #689.
This commit is contained in:
Roman Khimov 2020-03-03 13:08:34 +03:00
parent 56e37ad6ba
commit 4c8d327353
7 changed files with 17 additions and 39 deletions

View file

@ -16,7 +16,7 @@ import (
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/rpc/client" "github.com/CityOfZion/neo-go/pkg/rpc/client"
"github.com/CityOfZion/neo-go/pkg/rpc/request" "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/smartcontract"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/CityOfZion/neo-go/pkg/vm" "github.com/CityOfZion/neo-go/pkg/vm"
@ -364,7 +364,7 @@ func invokeInternal(ctx *cli.Context, withMethod bool, signAndPush bool) error {
operation string operation string
params = make([]smartcontract.Parameter, 0) params = make([]smartcontract.Parameter, 0)
paramsStart = 1 paramsStart = 1
resp *response.InvokeResult resp *result.Invoke
wif *keys.WIF wif *keys.WIF
) )

View file

@ -7,7 +7,6 @@ import (
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/crypto/keys" "github.com/CityOfZion/neo-go/pkg/crypto/keys"
"github.com/CityOfZion/neo-go/pkg/rpc/request" "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/rpc/response/result"
"github.com/CityOfZion/neo-go/pkg/smartcontract" "github.com/CityOfZion/neo-go/pkg/smartcontract"
"github.com/CityOfZion/neo-go/pkg/util" "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. // 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. // 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 ( var (
params = request.NewRawParams(script) params = request.NewRawParams(script)
resp = &response.InvokeResult{} resp = &result.Invoke{}
) )
if err := c.performRequest("invokescript", params, resp); err != nil { if err := c.performRequest("invokescript", params, resp); err != nil {
return nil, err 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 // InvokeFunction returns the results after calling the smart contract scripthash
// with the given operation and parameters. // with the given operation and parameters.
// NOTE: this is test invoke and will not affect the blockchain. // 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 ( var (
p = request.NewRawParams(script, operation, params) p = request.NewRawParams(script, operation, params)
resp = &response.InvokeResult{} resp = &result.Invoke{}
) )
if err := c.performRequest("invokefunction", p, resp); err != nil { if err := c.performRequest("invokefunction", p, resp); err != nil {
return nil, err 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 // Invoke returns the results after calling the smart contract scripthash
// with the given parameters. // 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 ( var (
p = request.NewRawParams(script, params) p = request.NewRawParams(script, params)
resp = &response.InvokeResult{} resp = &result.Invoke{}
) )
if err := c.performRequest("invoke", p, resp); err != nil { if err := c.performRequest("invoke", p, resp); err != nil {
return nil, err return nil, err

View file

@ -1,7 +1,7 @@
package result package result
import ( 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 // Invoke represents code invocation result and is used by several RPC calls
@ -10,5 +10,5 @@ type Invoke struct {
State string `json:"state"` State string `json:"state"`
GasConsumed string `json:"gas_consumed"` GasConsumed string `json:"gas_consumed"`
Script string `json:"script"` Script string `json:"script"`
Stack *vm.Stack Stack []smartcontract.Parameter
} }

View file

@ -4,19 +4,8 @@ import (
"encoding/json" "encoding/json"
"github.com/CityOfZion/neo-go/pkg/rpc/response/result" "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). // Header is a generic JSON-RPC 2.0 response header (ID and JSON-RPC version).
type Header struct { type Header struct {
ID json.RawMessage `json:"id"` ID json.RawMessage `json:"id"`

View file

@ -624,7 +624,7 @@ func (s *Server) runScriptInVM(script []byte) *result.Invoke {
State: vm.State(), State: vm.State(),
GasConsumed: vm.GasConsumed().String(), GasConsumed: vm.GasConsumed().String(),
Script: hex.EncodeToString(script), Script: hex.EncodeToString(script),
Stack: vm.Estack(), Stack: vm.Estack().ToContractParameters(),
} }
return result return result
} }

View file

@ -12,21 +12,11 @@ import (
"github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/network" "github.com/CityOfZion/neo-go/pkg/network"
"github.com/CityOfZion/neo-go/pkg/rpc/request"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest" "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) { func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFunc) {
var nBlocks uint32 var nBlocks uint32

View file

@ -474,9 +474,9 @@ var rpcTestCases = map[string][]rpcTestCase{
{ {
name: "positive", name: "positive",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", [{"type": "String", "value": "qwerty"}]]`, 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{}) { check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*InvokeFunctionResult) res, ok := inv.(*result.Invoke)
require.True(t, ok) require.True(t, ok)
assert.Equal(t, "06717765727479676f459162ceeb248b071ec157d9e4f6fd26fdbe50", res.Script) assert.Equal(t, "06717765727479676f459162ceeb248b071ec157d9e4f6fd26fdbe50", res.Script)
assert.NotEqual(t, "", res.State) assert.NotEqual(t, "", res.State)
@ -513,9 +513,9 @@ var rpcTestCases = map[string][]rpcTestCase{
{ {
name: "positive", name: "positive",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", "test", []]`, 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{}) { check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*InvokeFunctionResult) res, ok := inv.(*result.Invoke)
require.True(t, ok) require.True(t, ok)
assert.NotEqual(t, "", res.Script) assert.NotEqual(t, "", res.Script)
assert.NotEqual(t, "", res.State) assert.NotEqual(t, "", res.State)
@ -547,9 +547,9 @@ var rpcTestCases = map[string][]rpcTestCase{
{ {
name: "positive", name: "positive",
params: `["51c56b0d48656c6c6f2c20776f726c6421680f4e656f2e52756e74696d652e4c6f67616c7566"]`, 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{}) { check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*InvokeFunctionResult) res, ok := inv.(*result.Invoke)
require.True(t, ok) require.True(t, ok)
assert.NotEqual(t, "", res.Script) assert.NotEqual(t, "", res.Script)
assert.NotEqual(t, "", res.State) assert.NotEqual(t, "", res.State)