2019-07-22 04:17:27 -05:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2019-01-21 08:38:13 -06:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package graphql
|
|
|
|
|
|
|
|
import (
|
2020-08-03 12:40:46 -05:00
|
|
|
"fmt"
|
2022-05-16 04:59:35 -05:00
|
|
|
"io"
|
2021-01-05 04:22:32 -06:00
|
|
|
"math/big"
|
2020-08-03 12:40:46 -05:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2019-01-21 08:38:13 -06:00
|
|
|
"testing"
|
2020-11-25 03:19:36 -06:00
|
|
|
"time"
|
2020-08-03 12:40:46 -05:00
|
|
|
|
2021-04-06 08:58:36 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-11-25 03:19:36 -06:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2021-04-06 08:58:36 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-08-03 12:40:46 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2021-02-05 06:51:15 -06:00
|
|
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
2022-08-19 04:14:59 -05:00
|
|
|
"github.com/ethereum/go-ethereum/eth/filters"
|
2020-08-03 12:40:46 -05:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2021-01-05 04:22:32 -06:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2021-02-02 03:05:46 -06:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-21 08:38:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildSchema(t *testing.T) {
|
2022-04-08 08:44:55 -05:00
|
|
|
ddir := t.TempDir()
|
2021-01-13 04:42:26 -06:00
|
|
|
// Copy config
|
|
|
|
conf := node.DefaultConfig
|
|
|
|
conf.DataDir = ddir
|
|
|
|
stack, err := node.New(&conf)
|
2020-08-03 12:40:46 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create new node: %v", err)
|
|
|
|
}
|
2022-06-13 09:24:45 -05:00
|
|
|
defer stack.Close()
|
2019-01-21 08:38:13 -06:00
|
|
|
// Make sure the schema can be parsed and matched up to the object model.
|
2022-08-19 04:14:59 -05:00
|
|
|
if err := newHandler(stack, nil, nil, []string{}, []string{}); err != nil {
|
2019-01-21 08:38:13 -06:00
|
|
|
t.Errorf("Could not construct GraphQL handler: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 12:40:46 -05:00
|
|
|
|
|
|
|
// Tests that a graphQL request is successfully handled when graphql is enabled on the specified endpoint
|
2021-01-05 04:22:32 -06:00
|
|
|
func TestGraphQLBlockSerialization(t *testing.T) {
|
2021-04-06 08:58:36 -05:00
|
|
|
stack := createNode(t, true, false)
|
2020-08-03 12:40:46 -05:00
|
|
|
defer stack.Close()
|
|
|
|
// start node
|
|
|
|
if err := stack.Start(); err != nil {
|
|
|
|
t.Fatalf("could not start node: %v", err)
|
|
|
|
}
|
2021-01-05 04:22:32 -06:00
|
|
|
|
|
|
|
for i, tt := range []struct {
|
|
|
|
body string
|
|
|
|
want string
|
|
|
|
code int
|
|
|
|
}{
|
|
|
|
{ // Should return latest block
|
|
|
|
body: `{"query": "{block{number}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":{"number":10}}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{ // Should return info about latest block
|
|
|
|
body: `{"query": "{block{number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":{"number":10,"gasUsed":0,"gasLimit":11500000}}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:0){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":{"number":0,"gasUsed":0,"gasLimit":11500000}}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:-1){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":null}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:-500){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":null}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:\"0\"){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":{"number":0,"gasUsed":0,"gasLimit":11500000}}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:\"-33\"){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":null}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:\"1337\"){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"data":{"block":null}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:\"0xbad\"){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0xbad\": invalid syntax"}],"data":{}}`,
|
|
|
|
code: 400,
|
|
|
|
},
|
|
|
|
{ // hex strings are currently not supported. If that's added to the spec, this test will need to change
|
|
|
|
body: `{"query": "{block(number:\"0x0\"){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"errors":[{"message":"strconv.ParseInt: parsing \"0x0\": invalid syntax"}],"data":{}}`,
|
|
|
|
code: 400,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{block(number:\"a\"){number,gasUsed,gasLimit}}","variables": null}`,
|
|
|
|
want: `{"errors":[{"message":"strconv.ParseInt: parsing \"a\": invalid syntax"}],"data":{}}`,
|
|
|
|
code: 400,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
body: `{"query": "{bleh{number}}","variables": null}"`,
|
|
|
|
want: `{"errors":[{"message":"Cannot query field \"bleh\" on type \"Query\".","locations":[{"line":1,"column":2}]}]}`,
|
|
|
|
code: 400,
|
|
|
|
},
|
2021-01-06 10:19:16 -06:00
|
|
|
// should return `estimateGas` as decimal
|
|
|
|
{
|
|
|
|
body: `{"query": "{block{ estimateGas(data:{}) }}"}`,
|
|
|
|
want: `{"data":{"block":{"estimateGas":53000}}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
2021-01-25 04:31:18 -06:00
|
|
|
// should return `status` as decimal
|
|
|
|
{
|
|
|
|
body: `{"query": "{block {number call (data : {from : \"0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b\", to: \"0x6295ee1b4f6dd65047762f924ecd367c17eabf8f\", data :\"0x12a7b914\"}){data status}}}"}`,
|
|
|
|
want: `{"data":{"block":{"number":10,"call":{"data":"0x","status":1}}}}`,
|
|
|
|
code: 200,
|
|
|
|
},
|
2021-01-05 04:22:32 -06:00
|
|
|
} {
|
2021-01-13 15:43:07 -06:00
|
|
|
resp, err := http.Post(fmt.Sprintf("%s/graphql", stack.HTTPEndpoint()), "application/json", strings.NewReader(tt.body))
|
2021-01-05 04:22:32 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not post: %v", err)
|
|
|
|
}
|
2022-05-16 04:59:35 -05:00
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
2021-01-05 04:22:32 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not read from response body: %v", err)
|
|
|
|
}
|
|
|
|
if have := string(bodyBytes); have != tt.want {
|
|
|
|
t.Errorf("testcase %d %s,\nhave:\n%v\nwant:\n%v", i, tt.body, have, tt.want)
|
|
|
|
}
|
|
|
|
if tt.code != resp.StatusCode {
|
|
|
|
t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code)
|
|
|
|
}
|
2020-08-03 12:40:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 08:58:36 -05:00
|
|
|
func TestGraphQLBlockSerializationEIP2718(t *testing.T) {
|
|
|
|
stack := createNode(t, true, true)
|
|
|
|
defer stack.Close()
|
|
|
|
// start node
|
|
|
|
if err := stack.Start(); err != nil {
|
|
|
|
t.Fatalf("could not start node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range []struct {
|
|
|
|
body string
|
|
|
|
want string
|
|
|
|
code int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
body: `{"query": "{block {number transactions { from { address } to { address } value hash type accessList { address storageKeys } index}}}"}`,
|
2021-06-15 05:56:14 -05:00
|
|
|
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}]}}}`,
|
2021-04-06 08:58:36 -05:00
|
|
|
code: 200,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
resp, err := http.Post(fmt.Sprintf("%s/graphql", stack.HTTPEndpoint()), "application/json", strings.NewReader(tt.body))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not post: %v", err)
|
|
|
|
}
|
2022-05-16 04:59:35 -05:00
|
|
|
bodyBytes, err := io.ReadAll(resp.Body)
|
2021-04-06 08:58:36 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not read from response body: %v", err)
|
|
|
|
}
|
|
|
|
if have := string(bodyBytes); have != tt.want {
|
|
|
|
t.Errorf("testcase %d %s,\nhave:\n%v\nwant:\n%v", i, tt.body, have, tt.want)
|
|
|
|
}
|
|
|
|
if tt.code != resp.StatusCode {
|
|
|
|
t.Errorf("testcase %d %s,\nwrong statuscode, have: %v, want: %v", i, tt.body, resp.StatusCode, tt.code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:40:46 -05:00
|
|
|
// Tests that a graphQL request is not handled successfully when graphql is not enabled on the specified endpoint
|
|
|
|
func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) {
|
2021-04-06 08:58:36 -05:00
|
|
|
stack := createNode(t, false, false)
|
2020-08-03 12:40:46 -05:00
|
|
|
defer stack.Close()
|
|
|
|
if err := stack.Start(); err != nil {
|
|
|
|
t.Fatalf("could not start node: %v", err)
|
|
|
|
}
|
2021-01-05 04:22:32 -06:00
|
|
|
body := strings.NewReader(`{"query": "{block{number}}","variables": null}`)
|
2021-01-13 15:43:07 -06:00
|
|
|
resp, err := http.Post(fmt.Sprintf("%s/graphql", stack.HTTPEndpoint()), "application/json", body)
|
2020-08-03 12:40:46 -05:00
|
|
|
if err != nil {
|
2021-01-05 04:22:32 -06:00
|
|
|
t.Fatalf("could not post: %v", err)
|
2020-08-03 12:40:46 -05:00
|
|
|
}
|
|
|
|
// make sure the request is not handled successfully
|
2021-02-02 03:05:46 -06:00
|
|
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
2020-11-25 03:19:36 -06:00
|
|
|
}
|
|
|
|
|
2021-04-06 08:58:36 -05:00
|
|
|
func createNode(t *testing.T, gqlEnabled bool, txEnabled bool) *node.Node {
|
2020-08-03 12:40:46 -05:00
|
|
|
stack, err := node.New(&node.Config{
|
|
|
|
HTTPHost: "127.0.0.1",
|
2021-01-13 15:43:07 -06:00
|
|
|
HTTPPort: 0,
|
2020-08-03 12:40:46 -05:00
|
|
|
WSHost: "127.0.0.1",
|
2021-01-13 15:43:07 -06:00
|
|
|
WSPort: 0,
|
2020-08-03 12:40:46 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create node: %v", err)
|
|
|
|
}
|
|
|
|
if !gqlEnabled {
|
|
|
|
return stack
|
|
|
|
}
|
2021-04-06 08:58:36 -05:00
|
|
|
if !txEnabled {
|
|
|
|
createGQLService(t, stack)
|
|
|
|
} else {
|
|
|
|
createGQLServiceWithTransactions(t, stack)
|
|
|
|
}
|
2020-08-03 12:40:46 -05:00
|
|
|
return stack
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:43:07 -06:00
|
|
|
func createGQLService(t *testing.T, stack *node.Node) {
|
2021-01-05 04:22:32 -06:00
|
|
|
// create backend
|
2021-02-05 06:51:15 -06:00
|
|
|
ethConf := ðconfig.Config{
|
2021-01-05 04:22:32 -06:00
|
|
|
Genesis: &core.Genesis{
|
|
|
|
Config: params.AllEthashProtocolChanges,
|
|
|
|
GasLimit: 11500000,
|
|
|
|
Difficulty: big.NewInt(1048576),
|
2020-11-25 03:19:36 -06:00
|
|
|
},
|
|
|
|
Ethash: ethash.Config{
|
2021-01-05 04:22:32 -06:00
|
|
|
PowMode: ethash.ModeFake,
|
2020-11-25 03:19:36 -06:00
|
|
|
},
|
|
|
|
NetworkId: 1337,
|
|
|
|
TrieCleanCache: 5,
|
|
|
|
TrieCleanCacheJournal: "triecache",
|
|
|
|
TrieCleanCacheRejournal: 60 * time.Minute,
|
|
|
|
TrieDirtyCache: 5,
|
|
|
|
TrieTimeout: 60 * time.Minute,
|
|
|
|
SnapshotCache: 5,
|
|
|
|
}
|
|
|
|
ethBackend, err := eth.New(stack, ethConf)
|
2020-08-03 12:40:46 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create eth backend: %v", err)
|
|
|
|
}
|
2021-01-05 04:22:32 -06:00
|
|
|
// Create some blocks and import them
|
|
|
|
chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
|
|
|
|
ethash.NewFaker(), ethBackend.ChainDb(), 10, func(i int, gen *core.BlockGen) {})
|
|
|
|
_, err = ethBackend.BlockChain().InsertChain(chain)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create import blocks: %v", err)
|
|
|
|
}
|
2020-08-03 12:40:46 -05:00
|
|
|
// create gql service
|
2022-08-19 04:14:59 -05:00
|
|
|
filterSystem := filters.NewFilterSystem(ethBackend.APIBackend, filters.Config{})
|
|
|
|
err = New(stack, ethBackend.APIBackend, filterSystem, []string{}, []string{})
|
2020-08-03 12:40:46 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create graphql service: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 08:58:36 -05:00
|
|
|
|
|
|
|
func createGQLServiceWithTransactions(t *testing.T, stack *node.Node) {
|
|
|
|
// create backend
|
|
|
|
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
address := crypto.PubkeyToAddress(key.PublicKey)
|
2021-06-15 05:56:14 -05:00
|
|
|
funds := big.NewInt(1000000000000000)
|
2021-04-06 08:58:36 -05:00
|
|
|
dad := common.HexToAddress("0x0000000000000000000000000000000000000dad")
|
|
|
|
|
|
|
|
ethConf := ðconfig.Config{
|
|
|
|
Genesis: &core.Genesis{
|
|
|
|
Config: params.AllEthashProtocolChanges,
|
|
|
|
GasLimit: 11500000,
|
|
|
|
Difficulty: big.NewInt(1048576),
|
|
|
|
Alloc: core.GenesisAlloc{
|
|
|
|
address: {Balance: funds},
|
|
|
|
// The address 0xdad sloads 0x00 and 0x01
|
|
|
|
dad: {
|
|
|
|
Code: []byte{
|
|
|
|
byte(vm.PC),
|
|
|
|
byte(vm.PC),
|
|
|
|
byte(vm.SLOAD),
|
|
|
|
byte(vm.SLOAD),
|
|
|
|
},
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: big.NewInt(0),
|
|
|
|
},
|
|
|
|
},
|
2021-06-15 05:56:14 -05:00
|
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
2021-04-06 08:58:36 -05:00
|
|
|
},
|
|
|
|
Ethash: ethash.Config{
|
|
|
|
PowMode: ethash.ModeFake,
|
|
|
|
},
|
|
|
|
NetworkId: 1337,
|
|
|
|
TrieCleanCache: 5,
|
|
|
|
TrieCleanCacheJournal: "triecache",
|
|
|
|
TrieCleanCacheRejournal: 60 * time.Minute,
|
|
|
|
TrieDirtyCache: 5,
|
|
|
|
TrieTimeout: 60 * time.Minute,
|
|
|
|
SnapshotCache: 5,
|
|
|
|
}
|
|
|
|
|
|
|
|
ethBackend, err := eth.New(stack, ethConf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create eth backend: %v", err)
|
|
|
|
}
|
|
|
|
signer := types.LatestSigner(ethConf.Genesis.Config)
|
|
|
|
|
|
|
|
legacyTx, _ := types.SignNewTx(key, signer, &types.LegacyTx{
|
|
|
|
Nonce: uint64(0),
|
|
|
|
To: &dad,
|
|
|
|
Value: big.NewInt(100),
|
|
|
|
Gas: 50000,
|
2021-06-15 05:56:14 -05:00
|
|
|
GasPrice: big.NewInt(params.InitialBaseFee),
|
2021-04-06 08:58:36 -05:00
|
|
|
})
|
|
|
|
envelopTx, _ := types.SignNewTx(key, signer, &types.AccessListTx{
|
|
|
|
ChainID: ethConf.Genesis.Config.ChainID,
|
|
|
|
Nonce: uint64(1),
|
|
|
|
To: &dad,
|
|
|
|
Gas: 30000,
|
2021-06-15 05:56:14 -05:00
|
|
|
GasPrice: big.NewInt(params.InitialBaseFee),
|
2021-04-06 08:58:36 -05:00
|
|
|
Value: big.NewInt(50),
|
|
|
|
AccessList: types.AccessList{{
|
|
|
|
Address: dad,
|
|
|
|
StorageKeys: []common.Hash{{0}},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create some blocks and import them
|
|
|
|
chain, _ := core.GenerateChain(params.AllEthashProtocolChanges, ethBackend.BlockChain().Genesis(),
|
|
|
|
ethash.NewFaker(), ethBackend.ChainDb(), 1, func(i int, b *core.BlockGen) {
|
|
|
|
b.SetCoinbase(common.Address{1})
|
|
|
|
b.AddTx(legacyTx)
|
|
|
|
b.AddTx(envelopTx)
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = ethBackend.BlockChain().InsertChain(chain)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create import blocks: %v", err)
|
|
|
|
}
|
|
|
|
// create gql service
|
2022-08-19 04:14:59 -05:00
|
|
|
filterSystem := filters.NewFilterSystem(ethBackend.APIBackend, filters.Config{})
|
|
|
|
err = New(stack, ethBackend.APIBackend, filterSystem, []string{}, []string{})
|
2021-04-06 08:58:36 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create graphql service: %v", err)
|
|
|
|
}
|
|
|
|
}
|