Merge pull request #1418 from nspcc-dev/rpc/invoke_with_sender

rpc: add tx to invoke* results, support hex for uint160 params
This commit is contained in:
Roman Khimov 2020-10-17 17:54:12 +03:00 committed by GitHub
commit d58c50fb77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 22 deletions

View file

@ -347,7 +347,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return c.GetFeePerByte() return c.GetFeePerByte()
}, },
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c0d676574466565506572427974650c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"1000"}]}}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c0d676574466565506572427974650c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"1000"}],"tx":null}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return int64(1000) return int64(1000)
}, },
@ -359,7 +359,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return c.GetMaxTransactionsPerBlock() return c.GetMaxTransactionsPerBlock()
}, },
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c1a6765744d61785472616e73616374696f6e73506572426c6f636b0c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"512"}]}}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c1a6765744d61785472616e73616374696f6e73506572426c6f636b0c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"512"}],"tx":null}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return int64(512) return int64(512)
}, },
@ -371,7 +371,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return c.GetMaxBlockSize() return c.GetMaxBlockSize()
}, },
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c0f6765744d6178426c6f636b53697a650c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"262144"}]}}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c0f6765744d6178426c6f636b53697a650c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Integer","value":"262144"}],"tx":null}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return int64(262144) return int64(262144)
}, },
@ -383,7 +383,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return c.GetBlockedAccounts() return c.GetBlockedAccounts()
}, },
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c12676574426c6f636b65644163636f756e74730c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Array","value":[]}]}}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"state":"HALT","gasconsumed":"2007390","script":"10c00c12676574426c6f636b65644163636f756e74730c149a61a46eec97b89306d7ce81f15b462091d0093241627d5b52","stack":[{"type":"Array","value":[]}],"tx":null}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return native.BlockedAccounts{} return native.BlockedAccounts{}
}, },
@ -663,18 +663,22 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
Account: util.Uint160{1, 2, 3}, Account: util.Uint160{1, 2, 3},
}}) }})
}, },
serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"script":"1426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf","state":"HALT","gasconsumed":"31100000","stack":[{"type":"ByteString","value":"JivsCEQy"}],"tx":"d101361426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf000000000000000000000000"}}`, serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"script":"01e8030c14aa8acf859d4fe402b34e673f2156821796a488eb0c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb013c00c087472616e736665720c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb041627d5b5238","state":"HALT","gasconsumed":"31100000","stack":[{"type":"ByteString","value":"JivsCEQy"}],"tx":"000800000080969800000000000204130000000000b004000001aa8acf859d4fe402b34e673f2156821796a488eb01005701e8030c14aa8acf859d4fe402b34e673f2156821796a488eb0c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb013c00c087472616e736665720c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb041627d5b523801420c40d83408774d4bd8b19ae870561d06f2744eb7678f58d1b90cf2f50e98ae83f60b0824e2feeadef6de6418a4cfc43bbc1f916c33ec594cbe662a9d924786e17a14290c2102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc20b4195440d78"}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return &result.Invoke{}
},
check: func(t *testing.T, c *Client, uns interface{}) {
res, ok := uns.(*result.Invoke)
require.True(t, ok)
bytes, err := hex.DecodeString("262bec084432") bytes, err := hex.DecodeString("262bec084432")
if err != nil { if err != nil {
panic(err) panic(err)
} }
return &result.Invoke{ assert.Equal(t, "HALT", res.State)
State: "HALT", assert.Equal(t, int64(31100000), res.GasConsumed)
GasConsumed: 31100000, assert.Equal(t, "01e8030c14aa8acf859d4fe402b34e673f2156821796a488eb0c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb013c00c087472616e736665720c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb041627d5b5238", res.Script)
Script: "1426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf", assert.Equal(t, []stackitem.Item{stackitem.NewByteArray(bytes)}, res.Stack)
Stack: []stackitem.Item{stackitem.NewByteArray(bytes)}, assert.NotNil(t, res.Transaction)
}
}, },
}, },
{ {
@ -697,19 +701,22 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
Account: util.Uint160{1, 2, 3}, Account: util.Uint160{1, 2, 3},
}}) }})
}, },
serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"script":"1426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf","state":"FAULT","gasconsumed":"31100000","stack":[{"type":"ByteString","value":"JivsCEQy"}],"tx":"d101361426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf000000000000000000000000","exception":"gas limit exceeded"}}`, serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"script":"1426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf","state":"FAULT","gasconsumed":"31100000","stack":[{"type":"ByteString","value":"JivsCEQy"}],"tx":"000800000080969800000000000204130000000000b004000001aa8acf859d4fe402b34e673f2156821796a488eb01005701e8030c14aa8acf859d4fe402b34e673f2156821796a488eb0c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb013c00c087472616e736665720c14e79eb66d3c134a4a776ee807d2e5b846dda4fdb041627d5b523801420c40d83408774d4bd8b19ae870561d06f2744eb7678f58d1b90cf2f50e98ae83f60b0824e2feeadef6de6418a4cfc43bbc1f916c33ec594cbe662a9d924786e17a14290c2102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc20b4195440d78","exception":"gas limit exceeded"}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return &result.Invoke{}
},
check: func(t *testing.T, c *Client, uns interface{}) {
res, ok := uns.(*result.Invoke)
require.True(t, ok)
bytes, err := hex.DecodeString("262bec084432") bytes, err := hex.DecodeString("262bec084432")
if err != nil { if err != nil {
panic(err) panic(err)
} }
return &result.Invoke{ assert.Equal(t, "FAULT", res.State)
State: "FAULT", assert.Equal(t, int64(31100000), res.GasConsumed)
GasConsumed: 31100000, assert.Equal(t, "1426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf", res.Script)
Script: "1426ae7c6c9861ec418468c1f0fdc4a7f2963eb89151c10962616c616e63654f6667be39e7b562f60cbfe2aebca375a2e5ee28737caf", assert.Equal(t, []stackitem.Item{stackitem.NewByteArray(bytes)}, res.Stack)
Stack: []stackitem.Item{stackitem.NewByteArray(bytes)}, assert.NotNil(t, res.Transaction)
FaultException: "gas limit exceeded",
}
}, },
}, },
}, },
@ -725,7 +732,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
Account: util.Uint160{1, 2, 3}, Account: util.Uint160{1, 2, 3},
}}) }})
}, },
serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"script":"00046e616d656724058e5e1b6008847cd662728549088a9ee82191","state":"HALT","gasconsumed":"16100000","stack":[{"type":"ByteString","value":"TkVQNSBHQVM="}],"tx":"d1011b00046e616d656724058e5e1b6008847cd662728549088a9ee82191000000000000000000000000"}}`, serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"script":"00046e616d656724058e5e1b6008847cd662728549088a9ee82191","state":"HALT","gasconsumed":"16100000","stack":[{"type":"ByteString","value":"TkVQNSBHQVM="}],"tx":null}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
bytes, err := hex.DecodeString("4e45503520474153") bytes, err := hex.DecodeString("4e45503520474153")
if err != nil { if err != nil {

View file

@ -1,19 +1,28 @@
package result package result
import ( import (
"encoding/hex"
"encoding/json" "encoding/json"
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
) )
// Invoke represents code invocation result and is used by several RPC calls // Invoke represents code invocation result and is used by several RPC calls
// that invoke functions, scripts and generic bytecode. // that invoke functions, scripts and generic bytecode. Transaction is
// represented in raw serialized format, use transaction.NewTransactionFromBytes
// or GetTransaction method to deserialize it.
type Invoke struct { type Invoke struct {
State string State string
GasConsumed int64 GasConsumed int64
Script string Script string
Stack []stackitem.Item Stack []stackitem.Item
FaultException string FaultException string
// Transaction represents transaction bytes. Use GetTransaction method to decode it.
Transaction []byte
} }
type invokeAux struct { type invokeAux struct {
@ -22,6 +31,7 @@ type invokeAux struct {
Script string `json:"script"` Script string `json:"script"`
Stack json.RawMessage `json:"stack"` Stack json.RawMessage `json:"stack"`
FaultException string `json:"exception,omitempty"` FaultException string `json:"exception,omitempty"`
Transaction string `json:"tx,omitempty"`
} }
// MarshalJSON implements json.Marshaler. // MarshalJSON implements json.Marshaler.
@ -44,12 +54,20 @@ func (r Invoke) MarshalJSON() ([]byte, error) {
return nil, err return nil, err
} }
} }
var tx string
if r.Transaction != nil {
st := hex.EncodeToString(r.Transaction)
tx = st
}
return json.Marshal(&invokeAux{ return json.Marshal(&invokeAux{
GasConsumed: r.GasConsumed, GasConsumed: r.GasConsumed,
Script: r.Script, Script: r.Script,
State: r.State, State: r.State,
Stack: st, Stack: st,
FaultException: r.FaultException, FaultException: r.FaultException,
Transaction: tx,
}) })
} }
@ -72,9 +90,24 @@ func (r *Invoke) UnmarshalJSON(data []byte) error {
r.Stack = st r.Stack = st
} }
} }
if aux.Transaction != "" {
bytes, err := hex.DecodeString(aux.Transaction)
if err != nil {
return fmt.Errorf("failed to decode transaction bytes from hex: %w", err)
}
r.Transaction = bytes
}
r.GasConsumed = aux.GasConsumed r.GasConsumed = aux.GasConsumed
r.Script = aux.Script r.Script = aux.Script
r.State = aux.State r.State = aux.State
r.FaultException = aux.FaultException r.FaultException = aux.FaultException
return nil return nil
} }
// GetTransaction returns decoded transaction from Invoke.Transaction bytes.
func (r *Invoke) GetTransaction(magic netmode.Magic) (*transaction.Transaction, error) {
if r.Transaction == nil {
return nil, errors.New("empty transaction")
}
return transaction.NewTransactionFromBytes(magic, r.Transaction)
}

View file

@ -894,7 +894,7 @@ func (s *Server) getBlockHeader(reqParams request.Params) (interface{}, *respons
// getUnclaimedGas returns unclaimed GAS amount of the specified address. // getUnclaimedGas returns unclaimed GAS amount of the specified address.
func (s *Server) getUnclaimedGas(ps request.Params) (interface{}, *response.Error) { func (s *Server) getUnclaimedGas(ps request.Params) (interface{}, *response.Error) {
u, err := ps.ValueWithType(0, request.StringT).GetUint160FromAddress() u, err := ps.ValueWithType(0, request.StringT).GetUint160FromAddressOrHex()
if err != nil { if err != nil {
return nil, response.ErrInvalidParams return nil, response.ErrInvalidParams
} }