graphql_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package graphql
  17. import (
  18. "fmt"
  19. "io"
  20. "math/big"
  21. "net/http"
  22. "strings"
  23. "testing"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/consensus/ethash"
  27. "github.com/ethereum/go-ethereum/core"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/core/vm"
  30. "github.com/ethereum/go-ethereum/crypto"
  31. "github.com/ethereum/go-ethereum/eth"
  32. "github.com/ethereum/go-ethereum/eth/ethconfig"
  33. "github.com/ethereum/go-ethereum/eth/filters"
  34. "github.com/ethereum/go-ethereum/node"
  35. "github.com/ethereum/go-ethereum/params"
  36. "github.com/stretchr/testify/assert"
  37. )
  38. func TestBuildSchema(t *testing.T) {
  39. ddir := t.TempDir()
  40. // Copy config
  41. conf := node.DefaultConfig
  42. conf.DataDir = ddir
  43. stack, err := node.New(&conf)
  44. if err != nil {
  45. t.Fatalf("could not create new node: %v", err)
  46. }
  47. defer stack.Close()
  48. // Make sure the schema can be parsed and matched up to the object model.
  49. if err := newHandler(stack, nil, nil, []string{}, []string{}); err != nil {
  50. t.Errorf("Could not construct GraphQL handler: %v", err)
  51. }
  52. }
  53. // Tests that a graphQL request is successfully handled when graphql is enabled on the specified endpoint
  54. func TestGraphQLBlockSerialization(t *testing.T) {
  55. stack := createNode(t, true, false)
  56. defer stack.Close()
  57. // start node
  58. if err := stack.Start(); err != nil {
  59. t.Fatalf("could not start node: %v", err)
  60. }
  61. for i, tt := range []struct {
  62. body string
  63. want string
  64. code int
  65. }{
  66. { // Should return latest block
  67. body: `{"query": "{block{number}}","variables": null}`,
  68. want: `{"data":{"block":{"number":10}}}`,
  69. code: 200,
  70. },
  71. { // Should return info about latest block
  72. body: `{"query": "{block{number,gasUsed,gasLimit}}","variables": null}`,
  73. want: `{"data":{"block":{"number":10,"gasUsed":0,"gasLimit":11500000}}}`,
  74. code: 200,
  75. },
  76. {
  77. body: `{"query": "{block(number:0){number,gasUsed,gasLimit}}","variables": null}`,
  78. want: `{"data":{"block":{"number":0,"gasUsed":0,"gasLimit":11500000}}}`,
  79. code: 200,
  80. },
  81. {
  82. body: `{"query": "{block(number:-1){number,gasUsed,gasLimit}}","variables": null}`,
  83. want: `{"data":{"block":null}}`,
  84. code: 200,
  85. },
  86. {
  87. body: `{"query": "{block(number:-500){number,gasUsed,gasLimit}}","variables": null}`,
  88. want: `{"data":{"block":null}}`,
  89. code: 200,
  90. },
  91. {
  92. body: `{"query": "{block(number:\"0\"){number,gasUsed,gasLimit}}","variables": null}`,
  93. want: `{"data":{"block":{"number":0,"gasUsed":0,"gasLimit":11500000}}}`,
  94. code: 200,
  95. },
  96. {
  97. body: `{"query": "{block(number:\"-33\"){number,gasUsed,gasLimit}}","variables": null}`,
  98. want: `{"data":{"block":null}}`,
  99. code: 200,
  100. },
  101. {
  102. body: `{"query": "{block(number:\"1337\"){number,gasUsed,gasLimit}}","variables": null}`,
  103. want: `{"data":{"block":null}}`,
  104. code: 200,
  105. },
  106. {
  107. body: `{"query": "{block(number:\"0xbad\"){number,gasUsed,gasLimit}}","variables": null}`,
  108. want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0xbad\": invalid syntax"}],"data":{}}`,
  109. code: 400,
  110. },
  111. { // hex strings are currently not supported. If that's added to the spec, this test will need to change
  112. body: `{"query": "{block(number:\"0x0\"){number,gasUsed,gasLimit}}","variables": null}`,
  113. want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0x0\": invalid syntax"}],"data":{}}`,
  114. code: 400,
  115. },
  116. {
  117. body: `{"query": "{block(number:\"a\"){number,gasUsed,gasLimit}}","variables": null}`,
  118. want: `{"errors":[{"message":"strconv.ParseInt: parsing \"a\": invalid syntax"}],"data":{}}`,
  119. code: 400,
  120. },
  121. {
  122. body: `{"query": "{bleh{number}}","variables": null}"`,
  123. want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
  124. code: 400,
  125. },
  126. // should return `estimateGas` as decimal
  127. {
  128. body: `{"query": "{block{ estimateGas(data:{}) }}"}`,
  129. want: `{"data":{"block":{"estimateGas":53000}}}`,
  130. code: 200,
  131. },
  132. // should return `status` as decimal
  133. {
  134. body: `{"query": "{block {number call (data : {from : \"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b\", to: \"0x6295ee1b4f6dd65047762f924ecd367c17eabf8f\", data :\"0x12a7b914\"}){data status}}}"}`,
  135. want: `{"data":{"block":{"number":10,"call":{"data":"0x","status":1}}}}`,
  136. code: 200,
  137. },
  138. } {
  139. resp, err := http.Post(fmt.Sprintf("%s/graphql", stack.HTTPEndpoint()), "application/json", strings.NewReader(tt.body))
  140. if err != nil {
  141. t.Fatalf("could not post: %v", err)
  142. }
  143. bodyBytes, err := io.ReadAll(resp.Body)
  144. if err != nil {
  145. t.Fatalf("could not read from response body: %v", err)
  146. }
  147. if have := string(bodyBytes); have != tt.want {
  148. t.Errorf("testcase %d %s,\nhave:\n%v\nwant:\n%v", i, tt.body, have, tt.want)
  149. }
  150. if tt.code != resp.StatusCode {
  151. t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code)
  152. }
  153. }
  154. }
  155. func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
  156. stack := createNode(t, true, true)
  157. defer stack.Close()
  158. // start node
  159. if err := stack.Start(); err != nil {
  160. t.Fatalf("could not start node: %v", err)
  161. }
  162. for i, tt := range []struct {
  163. body string
  164. want string
  165. code int
  166. }{
  167. {
  168. body: `{"query": "{block {number transactions { from { address } to { address } value hash type accessList { address storageKeys } index}}}"}`,
  169. want: `{"data":{"block":{"number":1,"transactions":[{"from":{"address":"0x71562b71999873db5b286df957af199ec94617f7"},"to":{"address":"0x0000000000000000000000000000000000000dad"},"value":"0x64","hash":"0xd864c9d7d37fade6b70164740540c06dd58bb9c3f6b46101908d6339db6a6a7b","type":0,"accessList":[],"index":0},{"from":{"address":"0x71562b71999873db5b286df957af199ec94617f7"},"to":{"address":"0x0000000000000000000000000000000000000dad"},"value":"0x32","hash":"0x19b35f8187b4e15fb59a9af469dca5dfa3cd363c11d372058c12f6482477b474","type":1,"accessList":[{"address":"0x0000000000000000000000000000000000000dad","storageKeys":["0x0000000000000000000000000000000000000000000000000000000000000000"]}],"index":1}]}}}`,
  170. code: 200,
  171. },
  172. } {
  173. resp, err := http.Post(fmt.Sprintf("%s/graphql", stack.HTTPEndpoint()), "application/json", strings.NewReader(tt.body))
  174. if err != nil {
  175. t.Fatalf("could not post: %v", err)
  176. }
  177. bodyBytes, err := io.ReadAll(resp.Body)
  178. if err != nil {
  179. t.Fatalf("could not read from response body: %v", err)
  180. }
  181. if have := string(bodyBytes); have != tt.want {
  182. t.Errorf("testcase %d %s,\nhave:\n%v\nwant:\n%v", i, tt.body, have, tt.want)
  183. }
  184. if tt.code != resp.StatusCode {
  185. t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code)
  186. }
  187. }
  188. }
  189. // Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint
  190. func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
  191. stack := createNode(t, false, false)
  192. defer stack.Close()
  193. if err := stack.Start(); err != nil {
  194. t.Fatalf("could not start node: %v", err)
  195. }
  196. body := strings.NewReader(`{"query": "{block{number}}","variables": null}`)
  197. resp, err := http.Post(fmt.Sprintf("%s/graphql", stack.HTTPEndpoint()), "application/json", body)
  198. if err != nil {
  199. t.Fatalf("could not post: %v", err)
  200. }
  201. // make sure the request is not handled successfully
  202. assert.Equal(t, http.StatusNotFound, resp.StatusCode)
  203. }
  204. func createNode(t *testing.T, gqlEnabled bool, txEnabled bool) *node.Node {
  205. stack, err := node.New(&node.Config{
  206. HTTPHost: "127.0.0.1",
  207. HTTPPort: 0,
  208. WSHost: "127.0.0.1",
  209. WSPort: 0,
  210. })
  211. if err != nil {
  212. t.Fatalf("could not create node: %v", err)
  213. }
  214. if !gqlEnabled {
  215. return stack
  216. }
  217. if !txEnabled {
  218. createGQLService(t, stack)
  219. } else {
  220. createGQLServiceWithTransactions(t, stack)
  221. }
  222. return stack
  223. }
  224. func createGQLService(t *testing.T, stack *node.Node) {
  225. // create backend
  226. ethConf := &ethconfig.Config{
  227. Genesis: &core.Genesis{
  228. Config: params.AllEthashProtocolChanges,
  229. GasLimit: 11500000,
  230. Difficulty: big.NewInt(1048576),
  231. },
  232. Ethash: ethash.Config{
  233. PowMode: ethash.ModeFake,
  234. },
  235. NetworkId: 1337,
  236. TrieCleanCache: 5,
  237. TrieCleanCacheJournal: "triecache",
  238. TrieCleanCacheRejournal: 60 * time.Minute,
  239. TrieDirtyCache: 5,
  240. TrieTimeout: 60 * time.Minute,
  241. SnapshotCache: 5,
  242. RPCGasCap: 50000000,
  243. }
  244. ethBackend, err := eth.New(stack, ethConf)
  245. if err != nil {
  246. t.Fatalf("could not create eth backend: %v", err)
  247. }
  248. // Create some blocks and import them
  249. chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
  250. ethash.NewFaker(), ethBackend.ChainDb(), 10, func(i int, gen *core.BlockGen) {})
  251. _, err = ethBackend.BlockChain().InsertChain(chain)
  252. if err != nil {
  253. t.Fatalf("could not create import blocks: %v", err)
  254. }
  255. // create gql service
  256. filterSystem := filters.NewFilterSystem(ethBackend.APIBackend, filters.Config{})
  257. err = New(stack, ethBackend.APIBackend, filterSystem, []string{}, []string{})
  258. if err != nil {
  259. t.Fatalf("could not create graphql service: %v", err)
  260. }
  261. }
  262. func createGQLServiceWithTransactions(t *testing.T, stack *node.Node) {
  263. // create backend
  264. key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  265. address := crypto.PubkeyToAddress(key.PublicKey)
  266. funds := big.NewInt(1000000000000000)
  267. dad := common.HexToAddress("0x0000000000000000000000000000000000000dad")
  268. ethConf := &ethconfig.Config{
  269. Genesis: &core.Genesis{
  270. Config: params.AllEthashProtocolChanges,
  271. GasLimit: 11500000,
  272. Difficulty: big.NewInt(1048576),
  273. Alloc: core.GenesisAlloc{
  274. address: {Balance: funds},
  275. // The address 0xdad sloads 0x00 and 0x01
  276. dad: {
  277. Code: []byte{
  278. byte(vm.PC),
  279. byte(vm.PC),
  280. byte(vm.SLOAD),
  281. byte(vm.SLOAD),
  282. },
  283. Nonce: 0,
  284. Balance: big.NewInt(0),
  285. },
  286. },
  287. BaseFee: big.NewInt(params.InitialBaseFee),
  288. },
  289. Ethash: ethash.Config{
  290. PowMode: ethash.ModeFake,
  291. },
  292. NetworkId: 1337,
  293. TrieCleanCache: 5,
  294. TrieCleanCacheJournal: "triecache",
  295. TrieCleanCacheRejournal: 60 * time.Minute,
  296. TrieDirtyCache: 5,
  297. TrieTimeout: 60 * time.Minute,
  298. SnapshotCache: 5,
  299. }
  300. ethBackend, err := eth.New(stack, ethConf)
  301. if err != nil {
  302. t.Fatalf("could not create eth backend: %v", err)
  303. }
  304. signer := types.LatestSigner(ethConf.Genesis.Config)
  305. legacyTx, _ := types.SignNewTx(key, signer, &types.LegacyTx{
  306. Nonce: uint64(0),
  307. To: &dad,
  308. Value: big.NewInt(100),
  309. Gas: 50000,
  310. GasPrice: big.NewInt(params.InitialBaseFee),
  311. })
  312. envelopTx, _ := types.SignNewTx(key, signer, &types.AccessListTx{
  313. ChainID: ethConf.Genesis.Config.ChainID,
  314. Nonce: uint64(1),
  315. To: &dad,
  316. Gas: 30000,
  317. GasPrice: big.NewInt(params.InitialBaseFee),
  318. Value: big.NewInt(50),
  319. AccessList: types.AccessList{{
  320. Address: dad,
  321. StorageKeys: []common.Hash{{0}},
  322. }},
  323. })
  324. // Create some blocks and import them
  325. chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
  326. ethash.NewFaker(), ethBackend.ChainDb(), 1, func(i int, b *core.BlockGen) {
  327. b.SetCoinbase(common.Address{1})
  328. b.AddTx(legacyTx)
  329. b.AddTx(envelopTx)
  330. })
  331. _, err = ethBackend.BlockChain().InsertChain(chain)
  332. if err != nil {
  333. t.Fatalf("could not create import blocks: %v", err)
  334. }
  335. // create gql service
  336. filterSystem := filters.NewFilterSystem(ethBackend.APIBackend, filters.Config{})
  337. err = New(stack, ethBackend.APIBackend, filterSystem, []string{}, []string{})
  338. if err != nil {
  339. t.Fatalf("could not create graphql service: %v", err)
  340. }
  341. }