diff --git a/README.md b/README.md index 056a1655..cca12140 100644 --- a/README.md +++ b/README.md @@ -66,15 +66,23 @@ The corresponding CLI flags can be found with the `./ipld-eth-server serve --hel ipcPath = "~/.vulcanize/vulcanize.ipc" # $SERVER_IPC_PATH wsPath = "127.0.0.1:8081" # $SERVER_WS_PATH httpPath = "127.0.0.1:8082" # $SERVER_HTTP_PATH + graphql = true # $SERVER_GRAPHQL + graphqlEndpoint = "" # $SERVER_GRAPHQL_ENDPOINT [ethereum] chainID = "1" # $ETH_CHAIN_ID defaultSender = "" # $ETH_DEFAULT_SENDER_ADDR + rpcGasCap = "1000000000000" # $ETH_RPC_GAS_CAP + httpPath = "127.0.0.1:8545" # $ETH_HTTP_PATH + nodeID = "arch1" # $ETH_NODE_ID + clientName = "Geth" # $ETH_CLIENT_NAME + genesisBlock = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" # $ETH_GENESIS_BLOCK + networkID = "1" # $ETH_NETWORK_ID ``` -The `database` fields are for connecting to a Postgres database that has been/is being populated by [ipld-eth-indexer](https://github.com/vulcanize/ipld-eth-indexer). -The `server` fields set the paths for exposing the ipld-eth-server endpoints -The `ethereum` fields set the chainID and default sender address to use for EVM simulation +The `database` fields are for connecting to a Postgres database that has been/is being populated by [ipld-eth-indexer](https://github.com/vulcanize/ipld-eth-indexer) +The `server` fields set the paths for exposing the ipld-eth-server endpoints +The `ethereum` fields set the chainID and default sender address to use for EVM simulation, and can optionally be used to configure a remote eth node to forward cache misses to ### Endpoints diff --git a/cmd/serve.go b/cmd/serve.go index 5ec6f5f1..eb4b23e4 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -71,10 +71,17 @@ func serve() { if err := startServers(server, serverConfig); err != nil { logWithCommand.Fatal(err) } + graphQL, err := startGraphQL(server) + if err != nil { + logWithCommand.Fatal(err) + } shutdown := make(chan os.Signal) signal.Notify(shutdown, os.Interrupt) <-shutdown + if graphQL != nil { + graphQL.Stop() + } server.Stop() wg.Wait() } @@ -92,29 +99,24 @@ func startServers(server s.Server, settings *s.Config) error { } logWithCommand.Info("starting up HTTP server") _, _, err = srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"eth"}, nil, []string{"*"}, rpc.HTTPTimeouts{}) - if err != nil { - return err - } - return startGraphQL(server) + return err } -func startGraphQL(server s.Server) error { +func startGraphQL(server s.Server) (graphQLServer *graphql.Service, err error) { viper.BindEnv("server.graphql", "SERVER_GRAPHQL") if viper.GetBool("server.graphql") { logWithCommand.Info("starting up GraphQL server") viper.BindEnv("server.graphqlEndpoint", "SERVER_GRAPHQL_ENDPOINT") endPoint := viper.GetString("server.graphqlEndpoint") if endPoint != "" { - graphQLServer, err := graphql.New(server.Backend(), endPoint, nil, []string{"*"}, rpc.HTTPTimeouts{}) + graphQLServer, err = graphql.New(server.Backend(), endPoint, nil, []string{"*"}, rpc.HTTPTimeouts{}) if err != nil { - return err - } - if err := graphQLServer.Start(nil); err != nil { - return err + return } + err = graphQLServer.Start(nil) } } - return nil + return } func init() { diff --git a/go.mod b/go.mod index e1a7297a..b8759647 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/ethereum/go-ethereum v1.9.11 - github.com/graph-gophers/graphql-go v0.0.0-20201003130358-c5bdf3b1108e // indirect + github.com/graph-gophers/graphql-go v0.0.0-20201003130358-c5bdf3b1108e github.com/ipfs/go-block-format v0.0.2 github.com/ipfs/go-cid v0.0.5 github.com/ipfs/go-ipfs-blockstore v1.0.0 diff --git a/pkg/graphql/graphiql.go b/pkg/graphql/graphiql.go index 864ebf57..e57cb2a0 100644 --- a/pkg/graphql/graphiql.go +++ b/pkg/graphql/graphiql.go @@ -1,24 +1,18 @@ -// The MIT License (MIT) -// -// Copyright (c) 2016 Muhammed Thanish -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program 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 Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . package graphql diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index f0584ec5..a4f4a923 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -1,18 +1,18 @@ -// Copyright 2019 The go-ethereum Authors -// 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 +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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, + +// This program 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 . +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . // Package graphql provides a GraphQL interface to Ethereum node data. package graphql diff --git a/pkg/graphql/graphql_suite_test.go b/pkg/graphql/graphql_suite_test.go new file mode 100644 index 00000000..cdfd5329 --- /dev/null +++ b/pkg/graphql/graphql_suite_test.go @@ -0,0 +1,35 @@ +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program 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 Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package graphql_test + +import ( + "io/ioutil" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/sirupsen/logrus" +) + +func TestGraphQL(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "graphql test suite") +} + +var _ = BeforeSuite(func() { + logrus.SetOutput(ioutil.Discard) +}) diff --git a/pkg/graphql/graphql_test.go b/pkg/graphql/graphql_test.go index 40b13187..7315f530 100644 --- a/pkg/graphql/graphql_test.go +++ b/pkg/graphql/graphql_test.go @@ -1,28 +1,31 @@ -// Copyright 2019 The go-ethereum Authors -// 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 +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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 . -package graphql +// This program 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 Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package graphql_test import ( - "testing" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/vulcanize/ipld-eth-server/pkg/graphql" ) -func TestBuildSchema(t *testing.T) { - // Make sure the schema can be parsed and matched up to the object model. - if _, err := newHandler(nil); err != nil { - t.Errorf("Could not construct GraphQL handler: %v", err) - } -} +var _ = Describe("GraphQL", func() { + It("Builds the schema and creates a new handler", func() { + _, err := graphql.NewHandler(nil) + Expect(err).ToNot(HaveOccurred()) + }) +}) diff --git a/pkg/graphql/schema.go b/pkg/graphql/schema.go index b61b6174..8057eac9 100644 --- a/pkg/graphql/schema.go +++ b/pkg/graphql/schema.go @@ -1,18 +1,18 @@ -// Copyright 2019 The go-ethereum Authors -// 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 +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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, + +// This program 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 . +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . package graphql @@ -33,7 +33,6 @@ const schema string = ` schema { query: Query - mutation: Mutation } # Account is an Ethereum account at a particular block. @@ -206,9 +205,6 @@ const schema string = ` account(address: Address!): Account! # Call executes a local call operation at the current block's state. call(data: CallData!): CallResult - # EstimateGas estimates the amount of gas that will be required for - # successful execution of a transaction at the current block's state. - estimateGas(data: CallData!): Long! } # CallData represents the data associated with a local contract call. @@ -270,23 +266,9 @@ const schema string = ` # Blocks returns all the blocks between two numbers, inclusive. If # to is not supplied, it defaults to the most recent known block. blocks(from: Long!, to: Long): [Block!]! - # Pending returns the current pending state. - pending: Pending! # Transaction returns a transaction specified by its hash. transaction(hash: Bytes32!): Transaction # Logs returns log entries matching the provided filter. logs(filter: FilterCriteria!): [Log!]! - # GasPrice returns the node's estimate of a gas price sufficient to - # ensure a transaction is mined in a timely fashion. - gasPrice: BigInt! - # ProtocolVersion returns the current wire protocol version number. - protocolVersion: Int! - # Syncing returns information on the current synchronisation state. - syncing: SyncState - } - - type Mutation { - # SendRawTransaction sends an RLP-encoded transaction to the network. - sendRawTransaction(data: Bytes!): Bytes32! } ` diff --git a/pkg/graphql/service.go b/pkg/graphql/service.go index ff220319..04c69af4 100644 --- a/pkg/graphql/service.go +++ b/pkg/graphql/service.go @@ -1,18 +1,18 @@ -// Copyright 2019 The go-ethereum Authors -// 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 +// VulcanizeDB +// Copyright © 2020 Vulcanize + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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, + +// This program 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 . +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . package graphql @@ -21,11 +21,11 @@ import ( "net" "net/http" - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" "github.com/graph-gophers/graphql-go" "github.com/graph-gophers/graphql-go/relay" + "github.com/sirupsen/logrus" "github.com/vulcanize/ipld-eth-server/pkg/eth" ) @@ -62,7 +62,7 @@ func (s *Service) APIs() []rpc.API { return nil } // layer was also initialized to spawn any goroutines required by the service. func (s *Service) Start(server *p2p.Server) error { var err error - s.handler, err = newHandler(s.backend) + s.handler, err = NewHandler(s.backend) if err != nil { return err } @@ -70,13 +70,13 @@ func (s *Service) Start(server *p2p.Server) error { return err } go rpc.NewHTTPServer(s.cors, s.vhosts, s.timeouts, s.handler).Serve(s.listener) - log.Info("GraphQL endpoint opened", "url", fmt.Sprintf("http://%s", s.endpoint)) + logrus.Debugf("graphQL endpoint opened for url %s", fmt.Sprintf("http://%s", s.endpoint)) return nil } // newHandler returns a new `http.Handler` that will answer GraphQL queries. // It additionally exports an interactive query browser on the / endpoint. -func newHandler(backend *eth.Backend) (http.Handler, error) { +func NewHandler(backend *eth.Backend) (http.Handler, error) { q := Resolver{backend} s, err := graphql.ParseSchema(schema, &q) @@ -98,7 +98,7 @@ func (s *Service) Stop() error { if s.listener != nil { s.listener.Close() s.listener = nil - log.Info("GraphQL endpoint closed", "url", fmt.Sprintf("http://%s", s.endpoint)) + logrus.Debugf("graphQL endpoint closed for url %s", fmt.Sprintf("http://%s", s.endpoint)) } return nil }