mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-30 09:33:36 +00:00
rpc: drop duplicating structures used in tests
We have proper results now, so use those. The only left is Invoke, but that depends on another issue at the moment.
This commit is contained in:
parent
6a3be6081c
commit
8da20055d6
2 changed files with 131 additions and 314 deletions
|
@ -12,166 +12,17 @@ import (
|
||||||
"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/rpc/request"
|
||||||
"github.com/CityOfZion/neo-go/pkg/rpc/response/result"
|
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrorResponse struct represents JSON-RPC error.
|
// InvokeFunctionResult struct for testing.
|
||||||
type ErrorResponse struct {
|
type InvokeFunctionResult struct {
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Error struct {
|
|
||||||
Code int `json:"code"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
} `json:"error"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendTXResponse struct for testing.
|
|
||||||
type SendTXResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result bool `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// InvokeFunctionResponse struct for testing.
|
|
||||||
type InvokeFunctionResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result struct {
|
|
||||||
Script string `json:"script"`
|
Script string `json:"script"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
GasConsumed string `json:"gas_consumed"`
|
GasConsumed string `json:"gas_consumed"`
|
||||||
Stack []request.FuncParam `json:"stack"`
|
Stack []request.FuncParam `json:"stack"`
|
||||||
TX string `json:"tx,omitempty"`
|
TX string `json:"tx,omitempty"`
|
||||||
} `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateAddrResponse struct for testing.
|
|
||||||
type ValidateAddrResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result result.ValidateAddress `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetPeersResponse struct for testing.
|
|
||||||
type GetPeersResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result struct {
|
|
||||||
Unconnected []int `json:"unconnected"`
|
|
||||||
Connected []int `json:"connected"`
|
|
||||||
Bad []int `json:"bad"`
|
|
||||||
} `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetVersionResponse struct for testing.
|
|
||||||
type GetVersionResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result result.Version `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// IntResultResponse struct for testing.
|
|
||||||
type IntResultResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result int `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringResultResponse struct for testing.
|
|
||||||
type StringResultResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result string `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBlockResponse struct for testing.
|
|
||||||
type GetBlockResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result result.Block `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAssetResponse struct for testing.
|
|
||||||
type GetAssetResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result struct {
|
|
||||||
AssetID string `json:"assetID"`
|
|
||||||
AssetType int `json:"assetType"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Amount string `json:"amount"`
|
|
||||||
Available string `json:"available"`
|
|
||||||
Precision int `json:"precision"`
|
|
||||||
Fee int `json:"fee"`
|
|
||||||
Address string `json:"address"`
|
|
||||||
Owner string `json:"owner"`
|
|
||||||
Admin string `json:"admin"`
|
|
||||||
Issuer string `json:"issuer"`
|
|
||||||
Expiration int `json:"expiration"`
|
|
||||||
IsFrozen bool `json:"is_frozen"`
|
|
||||||
} `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAccountStateResponse struct for testing.
|
|
||||||
type GetAccountStateResponse struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result struct {
|
|
||||||
Version int `json:"version"`
|
|
||||||
ScriptHash string `json:"script_hash"`
|
|
||||||
Frozen bool `json:"frozen"`
|
|
||||||
Votes []interface{} `json:"votes"`
|
|
||||||
Balances []struct {
|
|
||||||
Asset string `json:"asset"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
} `json:"balances"`
|
|
||||||
} `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUnspents struct for testing.
|
|
||||||
type GetUnspents struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result struct {
|
|
||||||
Balance []struct {
|
|
||||||
Unspents []struct {
|
|
||||||
TxID string `json:"txid"`
|
|
||||||
Index int `json:"n"`
|
|
||||||
Value string `json:"value"`
|
|
||||||
} `json:"unspent"`
|
|
||||||
AssetHash string `json:"asset_hash"`
|
|
||||||
Asset string `json:"asset"`
|
|
||||||
AssetSymbol string `json:"asset_symbol"`
|
|
||||||
Amount string `json:"amount"`
|
|
||||||
} `json:"balance"`
|
|
||||||
Address string `json:"address"`
|
|
||||||
} `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetContractStateResponse struct for testing.
|
|
||||||
type GetContractStateResponce struct {
|
|
||||||
Jsonrpc string `json:"jsonrpc"`
|
|
||||||
Result struct {
|
|
||||||
Version byte `json:"version"`
|
|
||||||
ScriptHash util.Uint160 `json:"hash"`
|
|
||||||
Script []byte `json:"script"`
|
|
||||||
ParamList interface{} `json:"parameters"`
|
|
||||||
ReturnType interface{} `json:"returntype"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
CodeVersion string `json:"code_version"`
|
|
||||||
Author string `json:"author"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
Properties struct {
|
|
||||||
HasStorage bool `json:"storage"`
|
|
||||||
HasDynamicInvoke bool `json:"dynamic_invoke"`
|
|
||||||
IsPayable bool `json:"is_payable"`
|
|
||||||
} `json:"properties"`
|
|
||||||
} `json:"result"`
|
|
||||||
ID int `json:"id"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFunc) {
|
func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFunc) {
|
||||||
|
|
|
@ -44,23 +44,23 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
|
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
|
||||||
result: func(e *executor) interface{} { return &GetAccountStateResponse{} },
|
result: func(e *executor) interface{} { return &result.AccountState{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, acc interface{}) {
|
||||||
res, ok := result.(*GetAccountStateResponse)
|
res, ok := acc.(*result.AccountState)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, 1, len(res.Result.Balances))
|
assert.Equal(t, 1, len(res.Balances))
|
||||||
assert.Equal(t, false, res.Result.Frozen)
|
assert.Equal(t, false, res.IsFrozen)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "positive null",
|
name: "positive null",
|
||||||
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
|
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
|
||||||
result: func(e *executor) interface{} { return &GetAccountStateResponse{} },
|
result: func(e *executor) interface{} { return &result.AccountState{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, acc interface{}) {
|
||||||
res, ok := result.(*GetAccountStateResponse)
|
res, ok := acc.(*result.AccountState)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, 0, len(res.Result.Balances))
|
assert.Equal(t, 0, len(res.Balances))
|
||||||
assert.Equal(t, false, res.Result.Frozen)
|
assert.Equal(t, false, res.IsFrozen)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -78,13 +78,13 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["1a696b32e239dd5eace3f025cac0a193a5746a27"]`,
|
params: `["1a696b32e239dd5eace3f025cac0a193a5746a27"]`,
|
||||||
result: func(e *executor) interface{} { return &GetContractStateResponce{} },
|
result: func(e *executor) interface{} { return &result.ContractState{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, cs interface{}) {
|
||||||
res, ok := result.(*GetContractStateResponce)
|
res, ok := cs.(*result.ContractState)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, byte(0), res.Result.Version)
|
assert.Equal(t, byte(0), res.Version)
|
||||||
assert.Equal(t, util.Uint160{0x1a, 0x69, 0x6b, 0x32, 0xe2, 0x39, 0xdd, 0x5e, 0xac, 0xe3, 0xf0, 0x25, 0xca, 0xc0, 0xa1, 0x93, 0xa5, 0x74, 0x6a, 0x27}, res.Result.ScriptHash)
|
assert.Equal(t, util.Uint160{0x1a, 0x69, 0x6b, 0x32, 0xe2, 0x39, 0xdd, 0x5e, 0xac, 0xe3, 0xf0, 0x25, 0xca, 0xc0, 0xa1, 0x93, 0xa5, 0x74, 0x6a, 0x27}, res.ScriptHash)
|
||||||
assert.Equal(t, "0.99", res.Result.CodeVersion)
|
assert.Equal(t, "0.99", res.CodeVersion)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -107,21 +107,17 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "746573746b6579"]`,
|
params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "746573746b6579"]`,
|
||||||
result: func(e *executor) interface{} { return &StringResultResponse{} },
|
result: func(e *executor) interface{} {
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
v := hex.EncodeToString([]byte("testvalue"))
|
||||||
res, ok := result.(*StringResultResponse)
|
return &v
|
||||||
require.True(t, ok)
|
|
||||||
assert.Equal(t, hex.EncodeToString([]byte("testvalue")), res.Result)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "missing key",
|
name: "missing key",
|
||||||
params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "7465"]`,
|
params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "7465"]`,
|
||||||
result: func(e *executor) interface{} { return &StringResultResponse{} },
|
result: func(e *executor) interface{} {
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
v := ""
|
||||||
res, ok := result.(*StringResultResponse)
|
return &v
|
||||||
require.True(t, ok)
|
|
||||||
assert.Equal(t, "", res.Result)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -149,12 +145,12 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"]`,
|
params: `["602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"]`,
|
||||||
result: func(e *executor) interface{} { return &GetAssetResponse{} },
|
result: func(e *executor) interface{} { return &result.AssetState{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, as interface{}) {
|
||||||
res, ok := result.(*GetAssetResponse)
|
res, ok := as.(*result.AssetState)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, "00", res.Result.Owner)
|
assert.Equal(t, "00", res.Owner)
|
||||||
assert.Equal(t, "AWKECj9RD8rS8RPcpCgYVjk1DeYyHwxZm3", res.Result.Admin)
|
assert.Equal(t, "AWKECj9RD8rS8RPcpCgYVjk1DeYyHwxZm3", res.Admin)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -177,7 +173,8 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
params: "[]",
|
params: "[]",
|
||||||
result: func(e *executor) interface{} {
|
result: func(e *executor) interface{} {
|
||||||
return "0x" + e.chain.CurrentBlockHash().StringLE()
|
v := "0x" + e.chain.CurrentBlockHash().StringLE()
|
||||||
|
return &v
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -221,17 +218,17 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: "[1, 1]",
|
params: "[1, 1]",
|
||||||
result: func(e *executor) interface{} { return &GetBlockResponse{} },
|
result: func(e *executor) interface{} { return &result.Block{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, blockRes interface{}) {
|
||||||
res, ok := result.(*GetBlockResponse)
|
res, ok := blockRes.(*result.Block)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
|
|
||||||
block, err := e.chain.GetBlock(e.chain.GetHeaderHash(1))
|
block, err := e.chain.GetBlock(e.chain.GetHeaderHash(1))
|
||||||
require.NoErrorf(t, err, "could not get block")
|
require.NoErrorf(t, err, "could not get block")
|
||||||
|
|
||||||
assert.Equal(t, block.Hash(), res.Result.Hash)
|
assert.Equal(t, block.Hash(), res.Hash)
|
||||||
for i := range res.Result.Tx {
|
for i := range res.Tx {
|
||||||
tx := res.Result.Tx[i]
|
tx := res.Tx[i]
|
||||||
require.Equal(t, transaction.MinerType, tx.Type)
|
require.Equal(t, transaction.MinerType, tx.Type)
|
||||||
|
|
||||||
miner, ok := block.Transactions[i].Data.(*transaction.MinerTX)
|
miner, ok := block.Transactions[i].Data.(*transaction.MinerTX)
|
||||||
|
@ -270,22 +267,21 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
"getblockcount": {
|
"getblockcount": {
|
||||||
{
|
{
|
||||||
params: "[]",
|
params: "[]",
|
||||||
result: func(e *executor) interface{} { return int(e.chain.BlockHeight() + 1) },
|
result: func(e *executor) interface{} {
|
||||||
|
v := int(e.chain.BlockHeight() + 1)
|
||||||
|
return &v
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"getblockhash": {
|
"getblockhash": {
|
||||||
{
|
{
|
||||||
params: "[1]",
|
params: "[1]",
|
||||||
result: func(e *executor) interface{} { return "" },
|
result: func(e *executor) interface{} {
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
// We don't have `t` here for proper handling, but
|
||||||
res, ok := result.(*StringResultResponse)
|
// error here would lead to panic down below.
|
||||||
require.True(t, ok)
|
block, _ := e.chain.GetBlock(e.chain.GetHeaderHash(1))
|
||||||
|
|
||||||
block, err := e.chain.GetBlock(e.chain.GetHeaderHash(1))
|
|
||||||
require.NoErrorf(t, err, "could not get block")
|
|
||||||
|
|
||||||
expectedHash := "0x" + block.Hash().StringLE()
|
expectedHash := "0x" + block.Hash().StringLE()
|
||||||
assert.Equal(t, expectedHash, res.Result)
|
return &expectedHash
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -302,25 +298,20 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
"getconnectioncount": {
|
"getconnectioncount": {
|
||||||
{
|
{
|
||||||
params: "[]",
|
params: "[]",
|
||||||
result: func(*executor) interface{} { return 0 },
|
result: func(*executor) interface{} {
|
||||||
|
v := 0
|
||||||
|
return &v
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"getpeers": {
|
"getpeers": {
|
||||||
{
|
{
|
||||||
params: "[]",
|
params: "[]",
|
||||||
result: func(*executor) interface{} {
|
result: func(*executor) interface{} {
|
||||||
return &GetPeersResponse{
|
return &result.GetPeers{
|
||||||
Jsonrpc: defaultJSONRPC,
|
Unconnected: []result.Peer{},
|
||||||
Result: struct {
|
Connected: []result.Peer{},
|
||||||
Unconnected []int `json:"unconnected"`
|
Bad: []result.Peer{},
|
||||||
Connected []int `json:"connected"`
|
|
||||||
Bad []int `json:"bad"`
|
|
||||||
}{
|
|
||||||
Unconnected: []int{},
|
|
||||||
Connected: []int{},
|
|
||||||
Bad: []int{},
|
|
||||||
},
|
|
||||||
ID: defaultID,
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -346,33 +337,33 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
|
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
|
||||||
result: func(e *executor) interface{} { return &GetUnspents{} },
|
result: func(e *executor) interface{} { return &result.Unspents{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, unsp interface{}) {
|
||||||
res, ok := result.(*GetUnspents)
|
res, ok := unsp.(*result.Unspents)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, 1, len(res.Result.Balance))
|
require.Equal(t, 1, len(res.Balance))
|
||||||
assert.Equal(t, 1, len(res.Result.Balance[0].Unspents))
|
assert.Equal(t, 1, len(res.Balance[0].Unspents))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "positive null",
|
name: "positive null",
|
||||||
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
|
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
|
||||||
result: func(e *executor) interface{} { return &GetUnspents{} },
|
result: func(e *executor) interface{} { return &result.Unspents{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, unsp interface{}) {
|
||||||
res, ok := result.(*GetUnspents)
|
res, ok := unsp.(*result.Unspents)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, 0, len(res.Result.Balance))
|
require.Equal(t, 0, len(res.Balance))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"getversion": {
|
"getversion": {
|
||||||
{
|
{
|
||||||
params: "[]",
|
params: "[]",
|
||||||
result: func(*executor) interface{} { return &GetVersionResponse{} },
|
result: func(*executor) interface{} { return &result.Version{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, ver interface{}) {
|
||||||
resp, ok := result.(*GetVersionResponse)
|
resp, ok := ver.(*result.Version)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, "/NEO-GO:/", resp.Result.UserAgent)
|
require.Equal(t, "/NEO-GO:/", resp.UserAgent)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -380,13 +371,13 @@ 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 &InvokeFunctionResponse{} },
|
result: func(e *executor) interface{} { return &InvokeFunctionResult{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, inv interface{}) {
|
||||||
res, ok := result.(*InvokeFunctionResponse)
|
res, ok := inv.(*InvokeFunctionResult)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, "06717765727479676f459162ceeb248b071ec157d9e4f6fd26fdbe50", res.Result.Script)
|
assert.Equal(t, "06717765727479676f459162ceeb248b071ec157d9e4f6fd26fdbe50", res.Script)
|
||||||
assert.NotEqual(t, "", res.Result.State)
|
assert.NotEqual(t, "", res.State)
|
||||||
assert.NotEqual(t, 0, res.Result.GasConsumed)
|
assert.NotEqual(t, 0, res.GasConsumed)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -419,13 +410,13 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", "test", []]`,
|
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", "test", []]`,
|
||||||
result: func(e *executor) interface{} { return &InvokeFunctionResponse{} },
|
result: func(e *executor) interface{} { return &InvokeFunctionResult{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, inv interface{}) {
|
||||||
res, ok := result.(*InvokeFunctionResponse)
|
res, ok := inv.(*InvokeFunctionResult)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.NotEqual(t, "", res.Result.Script)
|
assert.NotEqual(t, "", res.Script)
|
||||||
assert.NotEqual(t, "", res.Result.State)
|
assert.NotEqual(t, "", res.State)
|
||||||
assert.NotEqual(t, 0, res.Result.GasConsumed)
|
assert.NotEqual(t, 0, res.GasConsumed)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -453,13 +444,13 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["51c56b0d48656c6c6f2c20776f726c6421680f4e656f2e52756e74696d652e4c6f67616c7566"]`,
|
params: `["51c56b0d48656c6c6f2c20776f726c6421680f4e656f2e52756e74696d652e4c6f67616c7566"]`,
|
||||||
result: func(e *executor) interface{} { return &InvokeFunctionResponse{} },
|
result: func(e *executor) interface{} { return &InvokeFunctionResult{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, inv interface{}) {
|
||||||
res, ok := result.(*InvokeFunctionResponse)
|
res, ok := inv.(*InvokeFunctionResult)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.NotEqual(t, "", res.Result.Script)
|
assert.NotEqual(t, "", res.Script)
|
||||||
assert.NotEqual(t, "", res.Result.State)
|
assert.NotEqual(t, "", res.State)
|
||||||
assert.NotEqual(t, 0, res.Result.GasConsumed)
|
assert.NotEqual(t, 0, res.GasConsumed)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -482,11 +473,9 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["d1001b00046e616d6567d3d8602814a429a91afdbaa3914884a1c90c733101201cc9c05cefffe6cdd7b182816a9152ec218d2ec000000141403387ef7940a5764259621e655b3c621a6aafd869a611ad64adcc364d8dd1edf84e00a7f8b11b630a377eaef02791d1c289d711c08b7ad04ff0d6c9caca22cfe6232103cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6ac"]`,
|
params: `["d1001b00046e616d6567d3d8602814a429a91afdbaa3914884a1c90c733101201cc9c05cefffe6cdd7b182816a9152ec218d2ec000000141403387ef7940a5764259621e655b3c621a6aafd869a611ad64adcc364d8dd1edf84e00a7f8b11b630a377eaef02791d1c289d711c08b7ad04ff0d6c9caca22cfe6232103cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6ac"]`,
|
||||||
result: func(e *executor) interface{} { return &SendTXResponse{} },
|
result: func(e *executor) interface{} {
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
v := true
|
||||||
res, ok := result.(*SendTXResponse)
|
return &v
|
||||||
require.True(t, ok)
|
|
||||||
assert.True(t, res.Result)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -514,25 +503,21 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i"]`,
|
params: `["AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i"]`,
|
||||||
result: func(*executor) interface{} { return &ValidateAddrResponse{} },
|
result: func(*executor) interface{} { return &result.ValidateAddress{} },
|
||||||
check: func(t *testing.T, e *executor, result interface{}) {
|
check: func(t *testing.T, e *executor, va interface{}) {
|
||||||
res, ok := result.(*ValidateAddrResponse)
|
res, ok := va.(*result.ValidateAddress)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
assert.Equal(t, "AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i", res.Result.Address)
|
assert.Equal(t, "AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i", res.Address)
|
||||||
assert.True(t, res.Result.IsValid)
|
assert.True(t, res.IsValid)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "negative",
|
name: "negative",
|
||||||
params: "[1]",
|
params: "[1]",
|
||||||
result: func(*executor) interface{} {
|
result: func(*executor) interface{} {
|
||||||
return &ValidateAddrResponse{
|
return &result.ValidateAddress{
|
||||||
Jsonrpc: defaultJSONRPC,
|
|
||||||
Result: result.ValidateAddress{
|
|
||||||
Address: float64(1),
|
Address: float64(1),
|
||||||
IsValid: false,
|
IsValid: false,
|
||||||
},
|
|
||||||
ID: defaultID,
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -552,14 +537,14 @@ func TestRPC(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
body := doRPCCall(fmt.Sprintf(rpc, method, tc.params), handler, t)
|
body := doRPCCall(fmt.Sprintf(rpc, method, tc.params), handler, t)
|
||||||
checkErrResponse(t, body, tc.fail)
|
result := checkErrGetResult(t, body, tc.fail)
|
||||||
if tc.fail {
|
if tc.fail {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
expected, res := tc.getResultPair(e)
|
expected, res := tc.getResultPair(e)
|
||||||
err := json.Unmarshal(body, res)
|
err := json.Unmarshal(result, res)
|
||||||
require.NoErrorf(t, err, "could not parse response: %s", body)
|
require.NoErrorf(t, err, "could not parse response: %s", result)
|
||||||
|
|
||||||
if tc.check == nil {
|
if tc.check == nil {
|
||||||
assert.Equal(t, expected, res)
|
assert.Equal(t, expected, res)
|
||||||
|
@ -576,11 +561,11 @@ func TestRPC(t *testing.T) {
|
||||||
TXHash := block.Transactions[1].Hash()
|
TXHash := block.Transactions[1].Hash()
|
||||||
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s"]}"`, TXHash.StringLE())
|
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s"]}"`, TXHash.StringLE())
|
||||||
body := doRPCCall(rpc, handler, t)
|
body := doRPCCall(rpc, handler, t)
|
||||||
checkErrResponse(t, body, false)
|
result := checkErrGetResult(t, body, false)
|
||||||
var res StringResultResponse
|
var res string
|
||||||
err := json.Unmarshal(body, &res)
|
err := json.Unmarshal(result, &res)
|
||||||
require.NoErrorf(t, err, "could not parse response: %s", body)
|
require.NoErrorf(t, err, "could not parse response: %s", result)
|
||||||
assert.Equal(t, "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000", res.Result)
|
assert.Equal(t, "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000", res)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("getrawtransaction 2 arguments", func(t *testing.T) {
|
t.Run("getrawtransaction 2 arguments", func(t *testing.T) {
|
||||||
|
@ -588,11 +573,11 @@ func TestRPC(t *testing.T) {
|
||||||
TXHash := block.Transactions[1].Hash()
|
TXHash := block.Transactions[1].Hash()
|
||||||
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s", 0]}"`, TXHash.StringLE())
|
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s", 0]}"`, TXHash.StringLE())
|
||||||
body := doRPCCall(rpc, handler, t)
|
body := doRPCCall(rpc, handler, t)
|
||||||
checkErrResponse(t, body, false)
|
result := checkErrGetResult(t, body, false)
|
||||||
var res StringResultResponse
|
var res string
|
||||||
err := json.Unmarshal(body, &res)
|
err := json.Unmarshal(result, &res)
|
||||||
require.NoErrorf(t, err, "could not parse response: %s", body)
|
require.NoErrorf(t, err, "could not parse response: %s", result)
|
||||||
assert.Equal(t, "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000", res.Result)
|
assert.Equal(t, "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000", res)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("gettxout", func(t *testing.T) {
|
t.Run("gettxout", func(t *testing.T) {
|
||||||
|
@ -601,54 +586,35 @@ func TestRPC(t *testing.T) {
|
||||||
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "gettxout", "params": [%s, %d]}"`,
|
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "gettxout", "params": [%s, %d]}"`,
|
||||||
`"`+tx.Hash().StringLE()+`"`, 0)
|
`"`+tx.Hash().StringLE()+`"`, 0)
|
||||||
body := doRPCCall(rpc, handler, t)
|
body := doRPCCall(rpc, handler, t)
|
||||||
checkErrResponse(t, body, false)
|
res := checkErrGetResult(t, body, false)
|
||||||
|
|
||||||
var result response.GetTxOut
|
var txOut result.TransactionOutput
|
||||||
err := json.Unmarshal(body, &result)
|
err := json.Unmarshal(res, &txOut)
|
||||||
require.NoErrorf(t, err, "could not parse response: %s", body)
|
require.NoErrorf(t, err, "could not parse response: %s", res)
|
||||||
assert.Equal(t, 0, result.Result.N)
|
assert.Equal(t, 0, txOut.N)
|
||||||
assert.Equal(t, "0x9b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5", result.Result.Asset)
|
assert.Equal(t, "0x9b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5", txOut.Asset)
|
||||||
assert.Equal(t, util.Fixed8FromInt64(100000000), result.Result.Value)
|
assert.Equal(t, util.Fixed8FromInt64(100000000), txOut.Value)
|
||||||
assert.Equal(t, "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU", result.Result.Address)
|
assert.Equal(t, "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU", txOut.Address)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc rpcTestCase) getResultPair(e *executor) (expected interface{}, res interface{}) {
|
func (tc rpcTestCase) getResultPair(e *executor) (expected interface{}, res interface{}) {
|
||||||
expected = tc.result(e)
|
expected = tc.result(e)
|
||||||
switch exp := expected.(type) {
|
|
||||||
case string:
|
|
||||||
res = new(StringResultResponse)
|
|
||||||
expected = &StringResultResponse{
|
|
||||||
Jsonrpc: defaultJSONRPC,
|
|
||||||
Result: exp,
|
|
||||||
ID: defaultID,
|
|
||||||
}
|
|
||||||
case int:
|
|
||||||
res = new(IntResultResponse)
|
|
||||||
expected = &IntResultResponse{
|
|
||||||
Jsonrpc: defaultJSONRPC,
|
|
||||||
Result: exp,
|
|
||||||
ID: defaultID,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
resVal := reflect.New(reflect.TypeOf(expected).Elem())
|
resVal := reflect.New(reflect.TypeOf(expected).Elem())
|
||||||
res = resVal.Interface()
|
return expected, resVal.Interface()
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkErrResponse(t *testing.T, body []byte, expectingFail bool) {
|
func checkErrGetResult(t *testing.T, body []byte, expectingFail bool) json.RawMessage {
|
||||||
var errresp ErrorResponse
|
var resp response.Raw
|
||||||
err := json.Unmarshal(body, &errresp)
|
err := json.Unmarshal(body, &resp)
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
if expectingFail {
|
if expectingFail {
|
||||||
assert.NotEqual(t, 0, errresp.Error.Code)
|
assert.NotEqual(t, 0, resp.Error.Code)
|
||||||
assert.NotEqual(t, "", errresp.Error.Message)
|
assert.NotEqual(t, "", resp.Error.Message)
|
||||||
} else {
|
} else {
|
||||||
assert.Equal(t, 0, errresp.Error.Code)
|
assert.Nil(t, resp.Error)
|
||||||
assert.Equal(t, "", errresp.Error.Message)
|
|
||||||
}
|
}
|
||||||
|
return resp.Result
|
||||||
}
|
}
|
||||||
|
|
||||||
func doRPCCall(rpcCall string, handler http.HandlerFunc, t *testing.T) []byte {
|
func doRPCCall(rpcCall string, handler http.HandlerFunc, t *testing.T) []byte {
|
||||||
|
|
Loading…
Reference in a new issue