neo-go/pkg/rpc/server/server_test.go
2020-03-11 18:33:15 +03:00

1013 lines
32 KiB
Go

package server
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"testing"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/internal/random"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type executor struct {
chain *core.Blockchain
handler http.HandlerFunc
}
const (
defaultJSONRPC = "2.0"
defaultID = 1
)
type rpcTestCase struct {
name string
params string
fail bool
result func(e *executor) interface{}
check func(t *testing.T, e *executor, result interface{})
}
const testContractHash = "d864728bdbc88da799bc43862ae6aaa62adc3a87"
var rpcTestCases = map[string][]rpcTestCase{
"getapplicationlog": {
{
name: "positive",
params: `["440b84d1580e36e84379416b58d9a3ad978cc557e54fd7ec6a2648329975b333"]`,
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("440b84d1580e36e84379416b58d9a3ad978cc557e54fd7ec6a2648329975b333")
require.NoError(t, err)
assert.Equal(t, expectedTxHash, res.TxHash)
assert.Equal(t, 1, len(res.Executions))
assert.Equal(t, "Application", res.Executions[0].Trigger)
assert.Equal(t, "HALT", res.Executions[0].VMState)
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid address",
params: `["notahash"]`,
fail: true,
},
{
name: "invalid tx hash",
params: `["d24cc1d52b5c0216cbf3835bb5bac8ccf32639fa1ab6627ec4e2b9f33f7ec02f"]`,
fail: true,
},
{
name: "invalid tx type",
params: `["f9adfde059810f37b3d0686d67f6b29034e0c669537df7e59b40c14a0508b9ed"]`,
fail: true,
},
},
"getaccountstate": {
{
name: "positive",
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
result: func(e *executor) interface{} { return &result.AccountState{} },
check: func(t *testing.T, e *executor, acc interface{}) {
res, ok := acc.(*result.AccountState)
require.True(t, ok)
assert.Equal(t, 1, len(res.Balances))
assert.Equal(t, false, res.IsFrozen)
},
},
{
name: "positive null",
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
result: func(e *executor) interface{} { return &result.AccountState{} },
check: func(t *testing.T, e *executor, acc interface{}) {
res, ok := acc.(*result.AccountState)
require.True(t, ok)
assert.Equal(t, 0, len(res.Balances))
assert.Equal(t, false, res.IsFrozen)
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid address",
params: `["notabase58"]`,
fail: true,
},
},
"getcontractstate": {
{
name: "positive",
params: fmt.Sprintf(`["%s"]`, testContractHash),
result: func(e *executor) interface{} { return &result.ContractState{} },
check: func(t *testing.T, e *executor, cs interface{}) {
res, ok := cs.(*result.ContractState)
require.True(t, ok)
assert.Equal(t, byte(0), res.Version)
assert.Equal(t, testContractHash, res.ScriptHash.StringBE())
assert.Equal(t, "0.99", res.CodeVersion)
},
},
{
name: "negative",
params: `["6d1eeca891ee93de2b7a77eb91c26f3b3c04d6c3"]`,
fail: true,
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
},
"getnep5balances": {
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid address",
params: `["notahex"]`,
fail: true,
},
{
name: "positive",
params: `["a90f00d94349a320376b7cb86c884b53ad76aa2b"]`,
result: func(e *executor) interface{} { return &result.NEP5Balances{} },
check: func(t *testing.T, e *executor, acc interface{}) {
res, ok := acc.(*result.NEP5Balances)
require.True(t, ok)
require.Equal(t, "AKkkumHbBipZ46UMZJoFynJMXzSRnBvKcs", res.Address)
require.Equal(t, 1, len(res.Balances))
require.Equal(t, "8.77", res.Balances[0].Amount)
require.Equal(t, testContractHash, res.Balances[0].Asset.StringLE())
require.Equal(t, uint32(208), res.Balances[0].LastUpdated)
},
},
},
"getnep5transfers": {
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid address",
params: `["notahex"]`,
fail: true,
},
{
name: "positive",
params: `["AKkkumHbBipZ46UMZJoFynJMXzSRnBvKcs"]`,
result: func(e *executor) interface{} { return &result.NEP5Transfers{} },
check: func(t *testing.T, e *executor, acc interface{}) {
res, ok := acc.(*result.NEP5Transfers)
require.True(t, ok)
require.Equal(t, "AKkkumHbBipZ46UMZJoFynJMXzSRnBvKcs", res.Address)
assetHash, err := util.Uint160DecodeStringLE(testContractHash)
require.NoError(t, err)
require.Equal(t, 1, len(res.Received))
require.Equal(t, "10", res.Received[0].Amount)
require.Equal(t, assetHash, res.Received[0].Asset)
require.Equal(t, address.Uint160ToString(assetHash), res.Received[0].Address)
require.Equal(t, 1, len(res.Sent))
require.Equal(t, "1.23", res.Sent[0].Amount)
require.Equal(t, assetHash, res.Sent[0].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Sent[0].Address)
},
},
},
"getstorage": {
{
name: "positive",
params: fmt.Sprintf(`["%s", "746573746b6579"]`, testContractHash),
result: func(e *executor) interface{} {
v := hex.EncodeToString([]byte("testvalue"))
return &v
},
},
{
name: "missing key",
params: fmt.Sprintf(`["%s", "7465"]`, testContractHash),
result: func(e *executor) interface{} {
v := ""
return &v
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "no second parameter",
params: fmt.Sprintf(`["%s"]`, testContractHash),
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
{
name: "invalid key",
params: fmt.Sprintf(`["%s", "notahex"]`, testContractHash),
fail: true,
},
},
"getassetstate": {
{
name: "positive",
params: `["602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"]`,
result: func(e *executor) interface{} { return &result.AssetState{} },
check: func(t *testing.T, e *executor, as interface{}) {
res, ok := as.(*result.AssetState)
require.True(t, ok)
assert.Equal(t, "00", res.Owner)
assert.Equal(t, "AWKECj9RD8rS8RPcpCgYVjk1DeYyHwxZm3", res.Admin)
},
},
{
name: "negative",
params: `["602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de2"]`,
fail: true,
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
},
"getbestblockhash": {
{
params: "[]",
result: func(e *executor) interface{} {
v := "0x" + e.chain.CurrentBlockHash().StringLE()
return &v
},
},
{
params: "1",
fail: true,
},
},
"gettxout": {
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
{
name: "missing hash",
params: `["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 0]`,
fail: true,
},
{
name: "invalid index",
params: `["7aadf91ca8ac1e2c323c025a7e492bee2dd90c783b86ebfc3b18db66b530a76d", "string"]`,
fail: true,
},
{
name: "negative index",
params: `["7aadf91ca8ac1e2c323c025a7e492bee2dd90c783b86ebfc3b18db66b530a76d", -1]`,
fail: true,
},
{
name: "too big index",
params: `["7aadf91ca8ac1e2c323c025a7e492bee2dd90c783b86ebfc3b18db66b530a76d", 100]`,
fail: true,
},
},
"getblock": {
{
name: "positive",
params: "[2, 1]",
result: func(e *executor) interface{} { return &result.Block{} },
check: func(t *testing.T, e *executor, blockRes interface{}) {
res, ok := blockRes.(*result.Block)
require.True(t, ok)
block, err := e.chain.GetBlock(e.chain.GetHeaderHash(2))
require.NoErrorf(t, err, "could not get block")
assert.Equal(t, block.Hash(), res.Hash)
for i := range res.Tx {
tx := res.Tx[i]
require.Equal(t, transaction.MinerType, tx.Type)
miner, ok := block.Transactions[i].Data.(*transaction.MinerTX)
require.True(t, ok)
require.Equal(t, miner.Nonce, tx.Nonce)
require.Equal(t, block.Transactions[i].Hash(), tx.TxID)
}
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "bad params",
params: `[[]]`,
fail: true,
},
{
name: "invalid height",
params: `[-1]`,
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
{
name: "missing hash",
params: `["` + util.Uint256{}.String() + `"]`,
fail: true,
},
},
"getblockcount": {
{
params: "[]",
result: func(e *executor) interface{} {
v := int(e.chain.BlockHeight() + 1)
return &v
},
},
},
"getblockhash": {
{
params: "[1]",
result: func(e *executor) interface{} {
// We don't have `t` here for proper handling, but
// error here would lead to panic down below.
block, _ := e.chain.GetBlock(e.chain.GetHeaderHash(1))
expectedHash := "0x" + block.Hash().StringLE()
return &expectedHash
},
},
{
name: "string height",
params: `["first"]`,
fail: true,
},
{
name: "invalid number height",
params: `[-2]`,
fail: true,
},
},
"getblockheader": {
{
name: "positive, no verbose",
params: `["77259b951c7eef05fdd91f155c90c698894968c2eafc36c9ef62d6538597ba2d"]`,
result: func(e *executor) interface{} {
expected := "00000000c0c89580103dfc9e65777591b57aa64af3a499c62c9287329855a2398652ae60edb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf90815615e050000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140dd8367b66a733fd9ab90dfdf3a64f7393d68b69e69ad7f2c89fe9bb1633de2f53a329b56a68fc47284e172e3196b63da510d5d719e9484a5da2e0732d4e85d89406d701e53f89a2037c5247663d1aa81d19899b8212c0b1052c399df11cb2909c62e1367edeb24c990df566f16218de252b87fd664fb07a157138a89fd92df45854013d5a17988a32ef9a766fb3277a7d2466c6e1608e28a3b3102a95450003a70ac63fe406742d8eed96301c51f8eb8f60b0fe77805b1ae2d508ca73897ac7a162b40640c35a3e40415a4e604ebacc3ffd96dda2e6f55589a7e87cf04280cf412c9eff49e0858d001bad2473e0de8fd9ba53c0d1718aafc6b8c3fa4d13451875e01388b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"
return &expected
},
},
{
name: "positive, verbose 0",
params: `["77259b951c7eef05fdd91f155c90c698894968c2eafc36c9ef62d6538597ba2d", 0]`,
result: func(e *executor) interface{} {
expected := "00000000c0c89580103dfc9e65777591b57aa64af3a499c62c9287329855a2398652ae60edb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf90815615e050000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140dd8367b66a733fd9ab90dfdf3a64f7393d68b69e69ad7f2c89fe9bb1633de2f53a329b56a68fc47284e172e3196b63da510d5d719e9484a5da2e0732d4e85d89406d701e53f89a2037c5247663d1aa81d19899b8212c0b1052c399df11cb2909c62e1367edeb24c990df566f16218de252b87fd664fb07a157138a89fd92df45854013d5a17988a32ef9a766fb3277a7d2466c6e1608e28a3b3102a95450003a70ac63fe406742d8eed96301c51f8eb8f60b0fe77805b1ae2d508ca73897ac7a162b40640c35a3e40415a4e604ebacc3ffd96dda2e6f55589a7e87cf04280cf412c9eff49e0858d001bad2473e0de8fd9ba53c0d1718aafc6b8c3fa4d13451875e01388b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"
return &expected
},
},
{
name: "positive, verbose !=0",
params: `["77259b951c7eef05fdd91f155c90c698894968c2eafc36c9ef62d6538597ba2d", 2]`,
result: func(e *executor) interface{} {
hash, err := util.Uint256DecodeStringLE("77259b951c7eef05fdd91f155c90c698894968c2eafc36c9ef62d6538597ba2d")
if err != nil {
panic("can not decode hash parameter")
}
block, err := e.chain.GetBlock(hash)
if err != nil {
panic("unknown block (update block hash)")
}
header := block.Header()
expected := result.Header{
Hash: header.Hash(),
Size: io.GetVarSize(header),
Version: header.Version,
PrevBlockHash: header.PrevHash,
MerkleRoot: header.MerkleRoot,
Timestamp: header.Timestamp,
Index: header.Index,
Nonce: strconv.FormatUint(header.ConsensusData, 16),
NextConsensus: header.NextConsensus,
Script: header.Script,
Confirmations: e.chain.BlockHeight() - header.Index + 1,
}
nextHash := e.chain.GetHeaderHash(int(header.Index) + 1)
if !hash.Equals(util.Uint256{}) {
expected.NextBlockHash = &nextHash
}
return &expected
},
},
{
name: "invalid verbose type",
params: `["77259b951c7eef05fdd91f155c90c698894968c2eafc36c9ef62d6538597ba2d", true]`,
fail: true,
},
{
name: "invalid block hash",
params: `["notahash"]`,
fail: true,
},
{
name: "unknown block",
params: `["a6e526375a780335112299f2262501e5e9574c3ba61b16bbc1e282b344f6c141"]`,
fail: true,
},
{
name: "no params",
params: `[]`,
fail: true,
},
},
"getblocksysfee": {
{
name: "positive",
params: "[1]",
result: func(e *executor) interface{} {
block, _ := e.chain.GetBlock(e.chain.GetHeaderHash(1))
var expectedBlockSysFee util.Fixed8
for _, tx := range block.Transactions {
expectedBlockSysFee += e.chain.SystemFee(tx)
}
return &expectedBlockSysFee
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "string height",
params: `["first"]`,
fail: true,
},
{
name: "invalid number height",
params: `[-2]`,
fail: true,
},
},
"getclaimable": {
{
name: "no params",
params: "[]",
fail: true,
},
{
name: "invalid address",
params: `["invalid"]`,
fail: true,
},
{
name: "normal address",
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
result: func(*executor) interface{} {
// hash of the issueTx
h, _ := util.Uint256DecodeStringBE("6da730b566db183bfceb863b780cd92dee2b497e5a023c322c1eaca81cf9ad7a")
amount := util.Fixed8FromInt64(1 * 8) // (endHeight - startHeight) * genAmount[0]
return &result.ClaimableInfo{
Spents: []result.Claimable{
{
Tx: h,
Value: util.Fixed8FromInt64(100000000),
EndHeight: 1,
Generated: amount,
Unclaimed: amount,
},
},
Address: "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU",
Unclaimed: amount,
}
},
},
},
"getconnectioncount": {
{
params: "[]",
result: func(*executor) interface{} {
v := 0
return &v
},
},
},
"getpeers": {
{
params: "[]",
result: func(*executor) interface{} {
return &result.GetPeers{
Unconnected: []result.Peer{},
Connected: []result.Peer{},
Bad: []result.Peer{},
}
},
},
},
"getrawtransaction": {
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
{
name: "missing hash",
params: `["` + util.Uint256{}.String() + `"]`,
fail: true,
},
},
"gettransactionheight": {
{
name: "poositive",
params: `["3fee783413c27849c8ee2656fd757a7483de64f4e78bd7897f30ecdf42ce788b"]`,
result: func(e *executor) interface{} {
h := 202
return &h
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid hash",
params: `["notahex"]`,
fail: true,
},
{
name: "missing hash",
params: `["` + util.Uint256{}.String() + `"]`,
fail: true,
},
},
"getunclaimed": {
{
name: "no params",
params: "[]",
fail: true,
},
{
name: "invalid address",
params: `["invalid"]`,
fail: true,
},
{
name: "positive",
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
result: func(*executor) interface{} {
return &result.Unclaimed{}
},
check: func(t *testing.T, e *executor, uncl interface{}) {
res, ok := uncl.(*result.Unclaimed)
require.True(t, ok)
assert.Equal(t, res.Available, util.Fixed8FromInt64(8))
assert.True(t, res.Unavailable > 0)
assert.Equal(t, res.Available + res.Unavailable, res.Unclaimed)
},
},
},
"getunspents": {
{
name: "positive",
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
result: func(e *executor) interface{} { return &result.Unspents{} },
check: func(t *testing.T, e *executor, unsp interface{}) {
res, ok := unsp.(*result.Unspents)
require.True(t, ok)
require.Equal(t, 1, len(res.Balance))
assert.Equal(t, 1, len(res.Balance[0].Unspents))
},
},
{
name: "positive null",
params: `["AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y"]`,
result: func(e *executor) interface{} { return &result.Unspents{} },
check: func(t *testing.T, e *executor, unsp interface{}) {
res, ok := unsp.(*result.Unspents)
require.True(t, ok)
require.Equal(t, 0, len(res.Balance))
},
},
},
"getvalidators": {
{
params: "[]",
result: func(*executor) interface{} {
return &[]result.Validator{}
},
check: func(t *testing.T, e *executor, validators interface{}) {
var expected []result.Validator
sBValidators, err := e.chain.GetStandByValidators()
require.NoError(t, err)
for _, sbValidator := range sBValidators {
expected = append(expected, result.Validator{
PublicKey: *sbValidator,
Votes: 0,
Active: true,
})
}
actual, ok := validators.(*[]result.Validator)
require.True(t, ok)
assert.ElementsMatch(t, expected, *actual)
},
},
},
"getversion": {
{
params: "[]",
result: func(*executor) interface{} { return &result.Version{} },
check: func(t *testing.T, e *executor, ver interface{}) {
resp, ok := ver.(*result.Version)
require.True(t, ok)
require.Equal(t, "/NEO-GO:/", resp.UserAgent)
},
},
},
"invoke": {
{
name: "positive",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", [{"type": "String", "value": "qwerty"}]]`,
result: func(e *executor) interface{} { return &result.Invoke{} },
check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*result.Invoke)
require.True(t, ok)
assert.Equal(t, "06717765727479676f459162ceeb248b071ec157d9e4f6fd26fdbe50", res.Script)
assert.NotEqual(t, "", res.State)
assert.NotEqual(t, 0, res.GasConsumed)
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "not a string",
params: `[42, []]`,
fail: true,
},
{
name: "not a scripthash",
params: `["qwerty", []]`,
fail: true,
},
{
name: "not an array",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", 42]`,
fail: true,
},
{
name: "bad params",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", [{"type": "Integer", "value": "qwerty"}]]`,
fail: true,
},
},
"invokefunction": {
{
name: "positive",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", "test", []]`,
result: func(e *executor) interface{} { return &result.Invoke{} },
check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*result.Invoke)
require.True(t, ok)
assert.NotEqual(t, "", res.Script)
assert.NotEqual(t, "", res.State)
assert.NotEqual(t, 0, res.GasConsumed)
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "not a string",
params: `[42, "test", []]`,
fail: true,
},
{
name: "not a scripthash",
params: `["qwerty", "test", []]`,
fail: true,
},
{
name: "bad params",
params: `["50befd26fdf6e4d957c11e078b24ebce6291456f", "test", [{"type": "Integer", "value": "qwerty"}]]`,
fail: true,
},
},
"invokescript": {
{
name: "positive",
params: `["51c56b0d48656c6c6f2c20776f726c6421680f4e656f2e52756e74696d652e4c6f67616c7566"]`,
result: func(e *executor) interface{} { return &result.Invoke{} },
check: func(t *testing.T, e *executor, inv interface{}) {
res, ok := inv.(*result.Invoke)
require.True(t, ok)
assert.NotEqual(t, "", res.Script)
assert.NotEqual(t, "", res.State)
assert.NotEqual(t, 0, res.GasConsumed)
},
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "not a string",
params: `[42]`,
fail: true,
},
{
name: "bas string",
params: `["qwerty"]`,
fail: true,
},
},
"sendrawtransaction": {
{
name: "positive",
params: `["d1001b00046e616d6567d3d8602814a429a91afdbaa3914884a1c90c733101201cc9c05cefffe6cdd7b182816a9152ec218d2ec000000141403387ef7940a5764259621e655b3c621a6aafd869a611ad64adcc364d8dd1edf84e00a7f8b11b630a377eaef02791d1c289d711c08b7ad04ff0d6c9caca22cfe6232103cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6ac"]`,
result: func(e *executor) interface{} {
v := true
return &v
},
},
{
name: "negative",
params: `["0274d792072617720636f6e7472616374207472616e73616374696f6e206465736372697074696f6e01949354ea0a8b57dfee1e257a1aedd1e0eea2e5837de145e8da9c0f101bfccc8e0100029b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc500a3e11100000000ea610aa6db39bd8c8556c9569d94b5e5a5d0ad199b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5004f2418010000001cc9c05cefffe6cdd7b182816a9152ec218d2ec0014140dbd3cddac5cb2bd9bf6d93701f1a6f1c9dbe2d1b480c54628bbb2a4d536158c747a6af82698edf9f8af1cac3850bcb772bd9c8e4ac38f80704751cc4e0bd0e67232103cbb45da6072c14761c9da545749d9cfd863f860c351066d16df480602a2024c6ac"]`,
fail: true,
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "invalid string",
params: `["notahex"]`,
fail: true,
},
{
name: "invalid tx",
params: `["0274d792072617720636f6e747261637"]`,
fail: true,
},
},
"submitblock": {
{
name: "empty block",
params: `["00000000270dd14a8ddb4961cada75e9ec4cce906f9feeaef21b1d6cea5b2f0230baf92e00000000000000000000000000000000000000000000000000000000000000009f0e625ed10000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140bd2b961ca7df75d6d8896fd4ef487128ddd1ca9feda9cb09da58a6a24607fc71cd572ba072e4ec9d9847c7da6a33fc44dc9a64208fd62851d27d330012bd0bf640ee6e92653789b0d1c87e0cd485c5bbd84194479995930567df9ffec7ab370c965df320134632e53d139a785840cecb16c4fe801f3c6bed17a9dd83460f5f297440fbe5f9d3cc23f9c7b9f10e191eae541fcb71eb03799c09886aa04c0bc43e98c2c91686a5b4fb988c8855f4d217807f01ca9a61bc0d5536eb3149dce7ae66965c407af69085454fb39f0c27cf308219c09efbe902f0a092a065b38864c97529f1dd12fa54c346e8d9482106c7154b3170c150887170c78fe78a5c70e3956880489c8b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"]`,
fail: true,
},
{
name: "invalid block height",
params: `["000000005fb86f62eafe8e9246bc0d1648e4e5c8389dee9fb7fe03fcc6772ec8c5e4ec2aedb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf99bc05f5e030000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd0401406f299c82b513f59f5bd120317974852c9694c6e10db1ef2f1bb848b1a33e47a08f8dc03ee784166b2060a94cd4e7af88899b39787938f7f2763ea4d2182776ed40f3bafd85214fef38a4836ca97793001ea411f553c51e88781f7b916c59c145bff28314b6e7ea246789422a996fc4937e290a1b40f6b97c5222540f65b0d47aca40d2b3d19203d456428bfdb529e846285052105957385b65388b9a617f6e2d56a64ec41aa73439eafccb52987bb1975c9b67518b053d9e61b445e4a3377dbc206640bd688489bd62adf6bed9d61a73905b9591eb87053c6f0f4dd70f3bee7295541b490caef044b55b6f9f01dc4a05a756a3f2edd06f5adcbe4e984c1e552f9023f08b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae0100000000000000000000"]`,
fail: true,
},
{
name: "invalid hex",
params: `["000000005gb86f62eafe8e9246bc0d1648e4e5c8389dee9fb7fe03fcc6772ec8c5e4ec2aedb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf99bc05f5e030000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd0401406f299c82b513f59f5bd120317974852c9694c6e10db1ef2f1bb848b1a33e47a08f8dc03ee784166b2060a94cd4e7af88899b39787938f7f2763ea4d2182776ed40f3bafd85214fef38a4836ca97793001ea411f553c51e88781f7b916c59c145bff28314b6e7ea246789422a996fc4937e290a1b40f6b97c5222540f65b0d47aca40d2b3d19203d456428bfdb529e846285052105957385b65388b9a617f6e2d56a64ec41aa73439eafccb52987bb1975c9b67518b053d9e61b445e4a3377dbc206640bd688489bd62adf6bed9d61a73905b9591eb87053c6f0f4dd70f3bee7295541b490caef044b55b6f9f01dc4a05a756a3f2edd06f5adcbe4e984c1e552f9023f08b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae0100000000000000000000"]`,
fail: true,
},
{
name: "invalid block bytes",
params: `["0000000027"]`,
fail: true,
},
{
name: "no params",
params: `[]`,
fail: true,
},
{
name: "positive",
// If you are planning to modify test chain from `testblocks.acc`, please, update param value
params: `["00000000270dd14a8ddb4961cada75e9ec4cce906f9feeaef21b1d6cea5b2f0230baf92eedb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf9b93a615ed10000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd0401402dca64bfc20107819171fab7c636427e9634a58f560ff9ef94ff9005a82509dd5e1b054d195201013a8477529d023d794a3ca6efa717f5d7e541e3b81fbabff840b5395429f0da5a8d1023907dca3771c02eca31e520e1acc2851b373a6aedd5bf6ba75a80b0949ee0794156ae48356b98e761143b90ebaa816ca4b0b0f91b38ab40a6feb77e09c0c95c6f2c6571155843f388e1f7cb7ba5af2bc235bac1d57a8c316d817e54895bfdb98b9c34ed9550e7ccff2a06b46ca0f2b99ba720368d9934eb40dbbdc94154cc304296ccda73e4e0257cdc344c4bad4c70f419f59f5d6d862819cead91c55e73e7c30daff2cd38d74eae3e0e49a5226de3060843d0e3b9262dc08b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae0100000000000000000000"]`,
result: func(e *executor) interface{} {
v := true
return &v
},
},
},
"validateaddress": {
{
name: "positive",
params: `["AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i"]`,
result: func(*executor) interface{} { return &result.ValidateAddress{} },
check: func(t *testing.T, e *executor, va interface{}) {
res, ok := va.(*result.ValidateAddress)
require.True(t, ok)
assert.Equal(t, "AQVh2pG732YvtNaxEGkQUei3YA4cvo7d2i", res.Address)
assert.True(t, res.IsValid)
},
},
{
name: "negative",
params: "[1]",
result: func(*executor) interface{} {
return &result.ValidateAddress{
Address: float64(1),
IsValid: false,
}
},
},
},
}
func TestRPC(t *testing.T) {
chain, handler := initServerWithInMemoryChain(t)
defer chain.Close()
e := &executor{chain: chain, handler: handler}
for method, cases := range rpcTestCases {
t.Run(method, func(t *testing.T) {
rpc := `{"jsonrpc": "2.0", "id": 1, "method": "%s", "params": %s}`
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
body := doRPCCall(fmt.Sprintf(rpc, method, tc.params), handler, t)
result := checkErrGetResult(t, body, tc.fail)
if tc.fail {
return
}
expected, res := tc.getResultPair(e)
err := json.Unmarshal(result, res)
require.NoErrorf(t, err, "could not parse response: %s", result)
if tc.check == nil {
assert.Equal(t, expected, res)
} else {
tc.check(t, e, res)
}
})
}
})
}
t.Run("getrawtransaction", func(t *testing.T) {
block, _ := chain.GetBlock(chain.GetHeaderHash(0))
TXHash := block.Transactions[1].Hash()
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s"]}"`, TXHash.StringLE())
body := doRPCCall(rpc, handler, t)
result := checkErrGetResult(t, body, false)
var res string
err := json.Unmarshal(result, &res)
require.NoErrorf(t, err, "could not parse response: %s", result)
assert.Equal(t, "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000", res)
})
t.Run("getrawtransaction 2 arguments", func(t *testing.T) {
block, _ := chain.GetBlock(chain.GetHeaderHash(0))
TXHash := block.Transactions[1].Hash()
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getrawtransaction", "params": ["%s", 0]}"`, TXHash.StringLE())
body := doRPCCall(rpc, handler, t)
result := checkErrGetResult(t, body, false)
var res string
err := json.Unmarshal(result, &res)
require.NoErrorf(t, err, "could not parse response: %s", result)
assert.Equal(t, "400000455b7b226c616e67223a227a682d434e222c226e616d65223a22e5b08fe89a81e882a1227d2c7b226c616e67223a22656e222c226e616d65223a22416e745368617265227d5d0000c16ff28623000000da1745e9b549bd0bfa1a569971c77eba30cd5a4b00000000", res)
})
t.Run("gettxout", func(t *testing.T) {
block, _ := chain.GetBlock(chain.GetHeaderHash(0))
tx := block.Transactions[3]
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "gettxout", "params": [%s, %d]}"`,
`"`+tx.Hash().StringLE()+`"`, 0)
body := doRPCCall(rpc, handler, t)
res := checkErrGetResult(t, body, false)
var txOut result.TransactionOutput
err := json.Unmarshal(res, &txOut)
require.NoErrorf(t, err, "could not parse response: %s", res)
assert.Equal(t, 0, txOut.N)
assert.Equal(t, "0x9b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5", txOut.Asset)
assert.Equal(t, util.Fixed8FromInt64(100000000), txOut.Value)
assert.Equal(t, "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU", txOut.Address)
})
t.Run("getrawmempool", func(t *testing.T) {
mp := chain.GetMemPool()
// `expected` stores hashes of previously added txs
expected := make([]util.Uint256, 0)
for _, tx := range mp.GetVerifiedTransactions() {
expected = append(expected, tx.Tx.Hash())
}
for i := 0; i < 5; i++ {
tx := &transaction.Transaction{
Type: transaction.MinerType,
Data: &transaction.MinerTX{
Nonce: uint32(random.Int(0, 1000000000)),
},
}
assert.NoError(t, mp.Add(tx, &FeerStub{}))
expected = append(expected, tx.Hash())
}
rpc := `{"jsonrpc": "2.0", "id": 1, "method": "getrawmempool", "params": []}`
body := doRPCCall(rpc, handler, t)
res := checkErrGetResult(t, body, false)
var actual []util.Uint256
err := json.Unmarshal(res, &actual)
require.NoErrorf(t, err, "could not parse response: %s", res)
assert.ElementsMatch(t, expected, actual)
})
}
func (tc rpcTestCase) getResultPair(e *executor) (expected interface{}, res interface{}) {
expected = tc.result(e)
resVal := reflect.New(reflect.TypeOf(expected).Elem())
return expected, resVal.Interface()
}
func checkErrGetResult(t *testing.T, body []byte, expectingFail bool) json.RawMessage {
var resp response.Raw
err := json.Unmarshal(body, &resp)
require.Nil(t, err)
if expectingFail {
assert.NotEqual(t, 0, resp.Error.Code)
assert.NotEqual(t, "", resp.Error.Message)
} else {
assert.Nil(t, resp.Error)
}
return resp.Result
}
func doRPCCall(rpcCall string, handler http.HandlerFunc, t *testing.T) []byte {
req := httptest.NewRequest("POST", "http://0.0.0.0:20333/", strings.NewReader(rpcCall))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall)
return bytes.TrimSpace(body)
}