package server import ( "bytes" "encoding/hex" "encoding/json" "fmt" "io/ioutil" "net/http" "net/http/httptest" "reflect" "strings" "testing" "github.com/CityOfZion/neo-go/pkg/core" "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/internal/random" "github.com/CityOfZion/neo-go/pkg/rpc/response" "github.com/CityOfZion/neo-go/pkg/rpc/response/result" "github.com/CityOfZion/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{}) } var rpcTestCases = map[string][]rpcTestCase{ "getapplicationlog": { { name: "positive", params: `["d5cf936296de912aa4d051531bd8d25c7a58fb68fc7f87c8d3e6e85475187c08"]`, 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 := util.Uint256{0x8, 0x7c, 0x18, 0x75, 0x54, 0xe8, 0xe6, 0xd3, 0xc8, 0x87, 0x7f, 0xfc, 0x68, 0xfb, 0x58, 0x7a, 0x5c, 0xd2, 0xd8, 0x1b, 0x53, 0x51, 0xd0, 0xa4, 0x2a, 0x91, 0xde, 0x96, 0x62, 0x93, 0xcf, 0xd5} 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: `["1a696b32e239dd5eace3f025cac0a193a5746a27"]`, 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, 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.CodeVersion) }, }, { name: "negative", params: `["6d1eeca891ee93de2b7a77eb91c26f3b3c04d6c3"]`, fail: true, }, { name: "no params", params: `[]`, fail: true, }, { name: "invalid hash", params: `["notahex"]`, fail: true, }, }, "getstorage": { { name: "positive", params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "746573746b6579"]`, result: func(e *executor) interface{} { v := hex.EncodeToString([]byte("testvalue")) return &v }, }, { name: "missing key", params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "7465"]`, result: func(e *executor) interface{} { v := "" return &v }, }, { name: "no params", params: `[]`, fail: true, }, { name: "no second parameter", params: `["1a696b32e239dd5eace3f025cac0a193a5746a27"]`, fail: true, }, { name: "invalid hash", params: `["notahex"]`, fail: true, }, { name: "invalid key", params: `["1a696b32e239dd5eace3f025cac0a193a5746a27", "notahex"]`, 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: "[1, 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(1)) 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, }, }, "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(52 * 8) // (endHeight - startHeight) * genAmount[0] return &result.ClaimableInfo{ Spents: []result.Claimable{ { Tx: h, Value: util.Fixed8FromInt64(100000000), EndHeight: 52, 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, }, }, "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)) }, }, }, "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, }, }, "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) }