2020-08-31 15:58:16 +00:00
|
|
|
// Copyright © 2020 Vulcanize, Inc
|
|
|
|
//
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-01-18 09:18:35 +00:00
|
|
|
"encoding/json"
|
2021-04-16 13:02:04 +00:00
|
|
|
"errors"
|
2022-01-18 09:18:35 +00:00
|
|
|
"math"
|
2021-04-14 15:53:44 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-08-31 15:58:16 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2021-04-16 13:02:04 +00:00
|
|
|
"strings"
|
2020-09-02 15:19:25 +00:00
|
|
|
"sync"
|
2021-09-21 12:10:55 +00:00
|
|
|
"time"
|
2020-08-31 15:58:16 +00:00
|
|
|
|
2022-01-18 09:18:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-08-31 15:58:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-01-18 09:18:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2022-01-19 10:37:50 +00:00
|
|
|
sdtypes "github.com/ethereum/go-ethereum/statediff/types"
|
2021-09-21 12:10:55 +00:00
|
|
|
"github.com/mailgun/groupcache/v2"
|
2020-08-31 15:58:16 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
2021-06-18 11:43:46 +00:00
|
|
|
"github.com/vulcanize/gap-filler/pkg/mux"
|
2021-09-21 12:10:55 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/eth"
|
2021-06-18 11:43:46 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/graphql"
|
2020-10-19 15:00:55 +00:00
|
|
|
srpc "github.com/vulcanize/ipld-eth-server/pkg/rpc"
|
2020-09-02 15:19:25 +00:00
|
|
|
s "github.com/vulcanize/ipld-eth-server/pkg/serve"
|
2020-08-31 15:58:16 +00:00
|
|
|
v "github.com/vulcanize/ipld-eth-server/version"
|
|
|
|
)
|
|
|
|
|
2021-04-16 13:02:04 +00:00
|
|
|
var ErrNoRpcEndpoints = errors.New("no rpc endpoints is available")
|
|
|
|
|
2020-09-02 15:19:25 +00:00
|
|
|
// serveCmd represents the serve command
|
|
|
|
var serveCmd = &cobra.Command{
|
|
|
|
Use: "serve",
|
2020-08-31 15:58:16 +00:00
|
|
|
Short: "serve chain data from PG-IPFS",
|
|
|
|
Long: `This command configures a VulcanizeDB ipld-eth-server.
|
|
|
|
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
subCommand = cmd.CalledAs()
|
|
|
|
logWithCommand = *log.WithField("SubCommand", subCommand)
|
2020-09-02 15:19:25 +00:00
|
|
|
serve()
|
2020-08-31 15:58:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:19:25 +00:00
|
|
|
func serve() {
|
2020-08-31 15:58:16 +00:00
|
|
|
logWithCommand.Infof("running ipld-eth-server version: %s", v.VersionWithMeta)
|
|
|
|
|
|
|
|
var forwardPayloadChan chan eth.ConvertedPayload
|
2020-09-02 15:19:25 +00:00
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
logWithCommand.Debug("loading server configuration variables")
|
|
|
|
serverConfig, err := s.NewConfig()
|
2020-08-31 15:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2020-09-02 15:19:25 +00:00
|
|
|
logWithCommand.Infof("server config: %+v", serverConfig)
|
|
|
|
logWithCommand.Debug("initializing new server service")
|
|
|
|
server, err := s.NewServer(serverConfig)
|
2020-08-31 15:58:16 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:19:25 +00:00
|
|
|
logWithCommand.Info("starting up server servers")
|
|
|
|
forwardPayloadChan = make(chan eth.ConvertedPayload, s.PayloadChanBufferSize)
|
|
|
|
server.Serve(wg, forwardPayloadChan)
|
|
|
|
if err := startServers(server, serverConfig); err != nil {
|
2020-08-31 15:58:16 +00:00
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
graphQL, err := startEthGraphQL(server, serverConfig)
|
2020-10-27 19:08:30 +00:00
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
2020-08-31 15:58:16 +00:00
|
|
|
|
2021-04-16 13:02:04 +00:00
|
|
|
err = startIpldGraphQL(serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-09-21 12:10:55 +00:00
|
|
|
err = startGroupCacheService(serverConfig)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverConfig.StateValidationEnabled {
|
|
|
|
go startStateTrieValidator(serverConfig, server)
|
|
|
|
logWithCommand.Info("state validator enabled")
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("state validator disabled")
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:18:35 +00:00
|
|
|
if serverConfig.WatchedAddressGapFillerEnabled {
|
|
|
|
go startWatchedAddressGapFiller(serverConfig)
|
|
|
|
logWithCommand.Info("watched address gap filler enabled")
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("watched address gap filler disabled")
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:06:04 +00:00
|
|
|
shutdown := make(chan os.Signal, 1)
|
2020-08-31 15:58:16 +00:00
|
|
|
signal.Notify(shutdown, os.Interrupt)
|
|
|
|
<-shutdown
|
2020-10-27 19:08:30 +00:00
|
|
|
if graphQL != nil {
|
|
|
|
graphQL.Stop()
|
|
|
|
}
|
2020-09-02 15:19:25 +00:00
|
|
|
server.Stop()
|
2020-08-31 15:58:16 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:19:25 +00:00
|
|
|
func startServers(server s.Server, settings *s.Config) error {
|
2021-04-14 15:53:44 +00:00
|
|
|
if settings.IPCEnabled {
|
|
|
|
logWithCommand.Info("starting up IPC server")
|
|
|
|
_, _, err := srpc.StartIPCEndpoint(settings.IPCEndpoint, server.APIs())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("IPC server is disabled")
|
2020-08-31 15:58:16 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
|
|
|
|
if settings.WSEnabled {
|
|
|
|
logWithCommand.Info("starting up WS server")
|
|
|
|
_, _, err := srpc.StartWSEndpoint(settings.WSEndpoint, server.APIs(), []string{"vdb", "net"}, nil, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("WS server is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if settings.HTTPEnabled {
|
|
|
|
logWithCommand.Info("starting up HTTP server")
|
2022-01-18 09:18:35 +00:00
|
|
|
_, err := srpc.StartHTTPEndpoint(settings.HTTPEndpoint, server.APIs(), []string{"vdb", "eth", "net"}, nil, []string{"*"}, rpc.HTTPTimeouts{})
|
2021-04-14 15:53:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("HTTP server is disabled")
|
2020-08-31 15:58:16 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
|
|
|
|
return nil
|
2020-10-27 19:08:10 +00:00
|
|
|
}
|
2020-10-19 15:00:55 +00:00
|
|
|
|
2021-04-14 15:53:44 +00:00
|
|
|
func startEthGraphQL(server s.Server, settings *s.Config) (graphQLServer *graphql.Service, err error) {
|
|
|
|
if settings.EthGraphqlEnabled {
|
|
|
|
logWithCommand.Info("starting up ETH GraphQL server")
|
|
|
|
endPoint := settings.EthGraphqlEndpoint
|
2020-10-27 19:08:10 +00:00
|
|
|
if endPoint != "" {
|
2020-10-27 19:08:30 +00:00
|
|
|
graphQLServer, err = graphql.New(server.Backend(), endPoint, nil, []string{"*"}, rpc.HTTPTimeouts{})
|
2020-10-27 19:08:10 +00:00
|
|
|
if err != nil {
|
2020-10-27 19:08:30 +00:00
|
|
|
return
|
2020-10-27 19:08:10 +00:00
|
|
|
}
|
2020-10-27 19:08:30 +00:00
|
|
|
err = graphQLServer.Start(nil)
|
2020-10-27 19:08:10 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
} else {
|
|
|
|
logWithCommand.Info("ETH GraphQL server is disabled")
|
2020-10-27 19:08:10 +00:00
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
|
2020-10-27 19:08:30 +00:00
|
|
|
return
|
2020-08-31 15:58:16 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 15:53:44 +00:00
|
|
|
func startIpldGraphQL(settings *s.Config) error {
|
|
|
|
if settings.IpldGraphqlEnabled {
|
|
|
|
logWithCommand.Info("starting up IPLD GraphQL server")
|
|
|
|
|
2021-04-15 22:32:29 +00:00
|
|
|
gqlIpldAddr, err := url.Parse(settings.IpldPostgraphileEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gqlTracingAPIAddr, err := url.Parse(settings.TracingPostgraphileEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-16 13:56:02 +00:00
|
|
|
ethClients, err := parseRpcAddresses(settings.EthHttpEndpoint)
|
2021-04-15 22:32:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-16 13:56:02 +00:00
|
|
|
var tracingClients []*rpc.Client
|
|
|
|
tracingEndpoint := viper.GetString("tracing.httpPath")
|
|
|
|
if tracingEndpoint != "" {
|
|
|
|
tracingClients, err = parseRpcAddresses(tracingEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-14 15:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router, err := mux.NewServeMux(&mux.Options{
|
|
|
|
BasePath: "/",
|
|
|
|
EnableGraphiQL: true,
|
|
|
|
Postgraphile: mux.PostgraphileOptions{
|
2021-04-15 22:32:29 +00:00
|
|
|
Default: gqlIpldAddr,
|
2021-04-14 15:53:44 +00:00
|
|
|
TracingAPI: gqlTracingAPIAddr,
|
|
|
|
},
|
|
|
|
RPC: mux.RPCOptions{
|
2021-04-15 22:32:29 +00:00
|
|
|
DefaultClients: ethClients,
|
2021-04-14 15:53:44 +00:00
|
|
|
TracingClients: tracingClients,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-04-15 22:32:29 +00:00
|
|
|
return err
|
2021-04-14 15:53:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 13:56:02 +00:00
|
|
|
go http.ListenAndServe(settings.IpldGraphqlEndpoint, router)
|
2021-04-14 15:53:44 +00:00
|
|
|
} else {
|
|
|
|
logWithCommand.Info("IPLD GraphQL server is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-21 12:10:55 +00:00
|
|
|
func startGroupCacheService(settings *s.Config) error {
|
|
|
|
gcc := settings.GroupCache
|
|
|
|
|
|
|
|
if gcc.Pool.Enabled {
|
|
|
|
logWithCommand.Info("starting up groupcache pool HTTTP server")
|
|
|
|
|
|
|
|
pool := groupcache.NewHTTPPoolOpts(gcc.Pool.HttpEndpoint, &groupcache.HTTPPoolOptions{})
|
|
|
|
pool.Set(gcc.Pool.PeerHttpEndpoints...)
|
|
|
|
|
|
|
|
httpURL, err := url.Parse(gcc.Pool.HttpEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
server := http.Server{
|
|
|
|
Addr: httpURL.Host,
|
|
|
|
Handler: pool,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a HTTP server to listen for peer requests from the groupcache
|
|
|
|
go server.ListenAndServe()
|
|
|
|
|
|
|
|
logWithCommand.Infof("groupcache pool endpoint opened for url %s", httpURL)
|
|
|
|
} else {
|
|
|
|
logWithCommand.Info("Groupcache pool is disabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startStateTrieValidator(config *s.Config, server s.Server) {
|
|
|
|
validateEveryNthBlock := config.StateValidationEveryNthBlock
|
|
|
|
|
|
|
|
var lastBlockNumber uint64
|
|
|
|
backend := server.Backend()
|
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
block, err := backend.CurrentBlock()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Error fetching current block for state trie validator")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stateRoot := block.Root()
|
|
|
|
blockNumber := block.NumberU64()
|
|
|
|
blockHash := block.Hash()
|
|
|
|
|
|
|
|
if validateEveryNthBlock <= 0 || // Used for static replicas where block number doesn't progress.
|
|
|
|
(blockNumber > lastBlockNumber) && (blockNumber%validateEveryNthBlock == 0) {
|
|
|
|
|
|
|
|
// The validate trie call will take a long time on mainnet, e.g. a few hours.
|
|
|
|
if err = backend.ValidateTrie(stateRoot); err != nil {
|
|
|
|
log.Fatalf("Error validating trie for block number %d hash %s state root %s",
|
|
|
|
blockNumber,
|
|
|
|
blockHash,
|
|
|
|
stateRoot,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Successfully validated trie for block number %d hash %s state root %s",
|
|
|
|
blockNumber,
|
|
|
|
blockHash,
|
|
|
|
stateRoot,
|
|
|
|
)
|
|
|
|
|
|
|
|
if validateEveryNthBlock <= 0 {
|
|
|
|
// Static replica, sleep a long-ish time (1/2 of cache expiry time) since we only need to keep the cache warm.
|
|
|
|
time.Sleep((time.Minute * time.Duration(config.GroupCache.StateDB.CacheExpiryInMins)) / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBlockNumber = blockNumber
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:18:35 +00:00
|
|
|
type WatchedAddress struct {
|
|
|
|
Address string `db:"address"`
|
2022-01-19 10:37:50 +00:00
|
|
|
Kind int `db:"kind"`
|
2022-01-18 09:18:35 +00:00
|
|
|
CreatedAt uint64 `db:"created_at"`
|
|
|
|
WatchedAt uint64 `db:"watched_at"`
|
|
|
|
LastFilledAt uint64 `db:"last_filled_at"`
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
startBlock uint64
|
|
|
|
endBlock uint64
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startWatchedAddressGapFiller(config *s.Config) {
|
|
|
|
fillInterval := config.WatchedAddressGapFillInterval
|
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(fillInterval) * time.Second)
|
|
|
|
|
|
|
|
// Get watched addresses from the db
|
|
|
|
// Get the block number to start fill at
|
|
|
|
// Get the block number to end fill at
|
2022-01-19 09:42:46 +00:00
|
|
|
fillWatchedAddresses, minStartBlock, maxEndBlock := getFillAddresses(config)
|
2022-01-18 09:18:35 +00:00
|
|
|
|
|
|
|
// Fill the missing diffs
|
2022-01-19 09:42:46 +00:00
|
|
|
for blockNumber := minStartBlock; blockNumber <= maxEndBlock; blockNumber++ {
|
2022-01-18 09:18:35 +00:00
|
|
|
params := statediff.Params{
|
|
|
|
IntermediateStateNodes: true,
|
|
|
|
IntermediateStorageNodes: true,
|
|
|
|
IncludeBlock: true,
|
|
|
|
IncludeReceipts: true,
|
|
|
|
IncludeTD: true,
|
|
|
|
IncludeCode: true,
|
|
|
|
}
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
fillAddresses := []interface{}{}
|
2022-01-18 09:18:35 +00:00
|
|
|
for _, fillWatchedAddress := range fillWatchedAddresses {
|
2022-01-19 09:42:46 +00:00
|
|
|
if blockNumber >= fillWatchedAddress.startBlock && blockNumber <= fillWatchedAddress.endBlock {
|
2022-01-19 10:37:50 +00:00
|
|
|
switch fillWatchedAddress.Kind {
|
|
|
|
case sdtypes.WatchedAddress.Int():
|
|
|
|
params.WatchedAddresses = append(params.WatchedAddresses, common.HexToAddress(fillWatchedAddress.Address))
|
|
|
|
case sdtypes.WatchedStorageSlot.Int():
|
|
|
|
params.WatchedStorageSlots = append(params.WatchedStorageSlots, common.HexToHash(fillWatchedAddress.Address))
|
|
|
|
default:
|
|
|
|
log.Fatalf("Unexpected kind %d:", fillWatchedAddress.Kind)
|
|
|
|
}
|
|
|
|
|
2022-01-18 09:18:35 +00:00
|
|
|
fillAddresses = append(fillAddresses, fillWatchedAddress.Address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fillWatchedAddressGap(config, blockNumber, params, fillAddresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
// getFillAddresses gets the addresses and finds the encompassing range to perform the fill
|
|
|
|
// it also sets the address specific fill range
|
2022-01-18 09:18:35 +00:00
|
|
|
func getFillAddresses(config *s.Config) ([]WatchedAddress, uint64, uint64) {
|
|
|
|
rows := []WatchedAddress{}
|
2022-01-19 09:42:46 +00:00
|
|
|
pgStr := "SELECT * FROM eth.watched_addresses"
|
|
|
|
err := config.DB.Select(&rows, pgStr)
|
2022-01-18 09:18:35 +00:00
|
|
|
if err != nil {
|
2022-01-19 09:42:46 +00:00
|
|
|
log.Fatalf("Error fetching watched addreesses:", err.Error())
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fillWatchedAddresses := []WatchedAddress{}
|
2022-01-19 09:42:46 +00:00
|
|
|
minStartBlock := uint64(math.MaxUint64)
|
|
|
|
maxEndBlock := uint64(0)
|
2022-01-18 09:18:35 +00:00
|
|
|
|
|
|
|
for _, row := range rows {
|
2022-01-19 09:42:46 +00:00
|
|
|
// Check for a gap between created_at and watched_at
|
2022-01-18 09:18:35 +00:00
|
|
|
if row.CreatedAt > row.WatchedAt {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
var startBlock uint64 = 0
|
|
|
|
var endBlock uint64 = 0
|
2022-01-18 09:18:35 +00:00
|
|
|
|
|
|
|
// Check if some of the gap was filled earlier
|
|
|
|
if row.LastFilledAt > 0 {
|
|
|
|
if row.LastFilledAt < row.WatchedAt {
|
2022-01-19 09:42:46 +00:00
|
|
|
startBlock = row.LastFilledAt + 1
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-19 09:42:46 +00:00
|
|
|
startBlock = row.CreatedAt
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the address for filling
|
2022-01-19 09:42:46 +00:00
|
|
|
if startBlock > 0 {
|
|
|
|
row.startBlock = startBlock
|
|
|
|
if startBlock < minStartBlock {
|
|
|
|
minStartBlock = startBlock
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
endBlock = row.WatchedAt
|
|
|
|
row.endBlock = endBlock
|
|
|
|
if endBlock > maxEndBlock {
|
|
|
|
maxEndBlock = endBlock
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fillWatchedAddresses = append(fillWatchedAddresses, row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
return fillWatchedAddresses, minStartBlock, maxEndBlock
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
func fillWatchedAddressGap(config *s.Config, blockNumber uint64, params statediff.Params, fillAddresses []interface{}) {
|
2022-01-18 09:18:35 +00:00
|
|
|
// Make a RPC call to write the statediffs
|
|
|
|
var data json.RawMessage
|
|
|
|
err := config.Client.Call(&data, "statediff_writeStateDiffAt", blockNumber, params)
|
|
|
|
if err != nil {
|
2022-01-19 09:42:46 +00:00
|
|
|
log.Fatalf("Error making a RPC call to write statediff at block number %d: %s", blockNumber, err.Error())
|
2022-01-18 09:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the db
|
2022-01-19 09:42:46 +00:00
|
|
|
query := "UPDATE eth.watched_addresses SET last_filled_at=? WHERE address IN (?" + strings.Repeat(",?", len(fillAddresses)-1) + ")"
|
2022-01-18 09:18:35 +00:00
|
|
|
query = config.DB.Rebind(query)
|
2022-01-19 09:42:46 +00:00
|
|
|
args := []interface{}{blockNumber}
|
|
|
|
args = append(args, fillAddresses...)
|
2022-01-18 09:18:35 +00:00
|
|
|
_, err = config.DB.Exec(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 13:02:04 +00:00
|
|
|
func parseRpcAddresses(value string) ([]*rpc.Client, error) {
|
|
|
|
rpcAddresses := strings.Split(value, ",")
|
|
|
|
rpcClients := make([]*rpc.Client, 0, len(rpcAddresses))
|
|
|
|
for _, address := range rpcAddresses {
|
|
|
|
rpcClient, err := rpc.Dial(address)
|
|
|
|
if err != nil {
|
|
|
|
logWithCommand.Errorf("couldn't connect to %s. Error: %s", address, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcClients = append(rpcClients, rpcClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rpcClients) == 0 {
|
|
|
|
logWithCommand.Error(ErrNoRpcEndpoints)
|
|
|
|
return nil, ErrNoRpcEndpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcClients, nil
|
|
|
|
}
|
|
|
|
|
2020-08-31 15:58:16 +00:00
|
|
|
func init() {
|
2020-09-02 15:19:25 +00:00
|
|
|
rootCmd.AddCommand(serveCmd)
|
2020-08-31 15:58:16 +00:00
|
|
|
|
2021-09-21 12:10:55 +00:00
|
|
|
addDatabaseFlags(serveCmd)
|
2021-04-14 15:53:44 +00:00
|
|
|
|
2020-08-31 15:58:16 +00:00
|
|
|
// flags for all config variables
|
2021-04-14 15:53:44 +00:00
|
|
|
// eth graphql and json-rpc parameters
|
|
|
|
serveCmd.PersistentFlags().Bool("eth-server-graphql", false, "turn on the eth graphql server")
|
|
|
|
serveCmd.PersistentFlags().String("eth-server-graphql-path", "", "endpoint url for eth graphql server (host:port)")
|
|
|
|
serveCmd.PersistentFlags().Bool("eth-server-http", true, "turn on the eth http json-rpc server")
|
|
|
|
serveCmd.PersistentFlags().String("eth-server-http-path", "", "endpoint url for eth http json-rpc server (host:port)")
|
|
|
|
serveCmd.PersistentFlags().Bool("eth-server-ws", false, "turn on the eth websocket json-rpc server")
|
|
|
|
serveCmd.PersistentFlags().String("eth-server-ws-path", "", "endpoint url for eth websocket json-rpc server (host:port)")
|
|
|
|
serveCmd.PersistentFlags().Bool("eth-server-ipc", false, "turn on the eth ipc json-rpc server")
|
|
|
|
serveCmd.PersistentFlags().String("eth-server-ipc-path", "", "path for eth ipc json-rpc server")
|
|
|
|
|
|
|
|
// ipld and tracing graphql parameters
|
|
|
|
serveCmd.PersistentFlags().Bool("ipld-server-graphql", false, "turn on the ipld graphql server")
|
|
|
|
serveCmd.PersistentFlags().String("ipld-server-graphql-path", "", "endpoint url for ipld graphql server (host:port)")
|
|
|
|
serveCmd.PersistentFlags().String("ipld-postgraphile-path", "", "http url to postgraphile on top of ipld database")
|
|
|
|
serveCmd.PersistentFlags().String("tracing-http-path", "", "http url to tracing service")
|
|
|
|
serveCmd.PersistentFlags().String("tracing-postgraphile-path", "", "http url to postgraphile on top of tracing db")
|
2020-08-31 15:58:16 +00:00
|
|
|
|
2020-10-27 17:42:22 +00:00
|
|
|
serveCmd.PersistentFlags().String("eth-http-path", "", "http url for ethereum node")
|
|
|
|
serveCmd.PersistentFlags().String("eth-node-id", "", "eth node id")
|
|
|
|
serveCmd.PersistentFlags().String("eth-client-name", "Geth", "eth client name")
|
|
|
|
serveCmd.PersistentFlags().String("eth-genesis-block", "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3", "eth genesis block hash")
|
|
|
|
serveCmd.PersistentFlags().String("eth-network-id", "1", "eth network id")
|
2020-10-20 20:33:18 +00:00
|
|
|
serveCmd.PersistentFlags().String("eth-chain-id", "1", "eth chain id")
|
|
|
|
serveCmd.PersistentFlags().String("eth-default-sender", "", "default sender address")
|
|
|
|
serveCmd.PersistentFlags().String("eth-rpc-gas-cap", "", "rpc gas cap (for eth_Call execution)")
|
2021-03-10 16:18:32 +00:00
|
|
|
serveCmd.PersistentFlags().String("eth-chain-config", "", "json chain config file location")
|
2021-12-27 18:31:25 +00:00
|
|
|
serveCmd.PersistentFlags().Bool("eth-supports-state-diff", false, "whether the proxy ethereum client supports statediffing endpoints")
|
|
|
|
serveCmd.PersistentFlags().Bool("eth-forward-eth-calls", false, "whether to immediately forward eth_calls to proxy client")
|
2021-12-30 02:29:59 +00:00
|
|
|
serveCmd.PersistentFlags().Bool("eth-proxy-on-error", true, "whether to forward all failed calls to proxy client")
|
2020-08-31 15:58:16 +00:00
|
|
|
|
2021-09-21 12:10:55 +00:00
|
|
|
// groupcache flags
|
|
|
|
serveCmd.PersistentFlags().Bool("gcache-pool-enabled", false, "turn on the groupcache pool")
|
|
|
|
serveCmd.PersistentFlags().String("gcache-pool-http-path", "", "http url for groupcache node")
|
|
|
|
serveCmd.PersistentFlags().StringArray("gcache-pool-http-peers", []string{}, "http urls for groupcache peers")
|
|
|
|
serveCmd.PersistentFlags().Int("gcache-statedb-cache-size", 16, "state DB cache size in MB")
|
|
|
|
serveCmd.PersistentFlags().Int("gcache-statedb-cache-expiry", 60, "state DB cache expiry time in mins")
|
|
|
|
serveCmd.PersistentFlags().Int("gcache-statedb-log-stats-interval", 60, "state DB cache stats log interval in secs")
|
2021-04-14 15:53:44 +00:00
|
|
|
|
2021-09-21 12:10:55 +00:00
|
|
|
// state validator flags
|
|
|
|
serveCmd.PersistentFlags().Bool("validator-enabled", false, "turn on the state validator")
|
|
|
|
serveCmd.PersistentFlags().Uint("validator-every-nth-block", 1500, "only validate every Nth block")
|
|
|
|
|
2022-01-19 09:42:46 +00:00
|
|
|
// watched address gap filler flags
|
|
|
|
serveCmd.PersistentFlags().Bool("watched-address-gap-filler-enabled", false, "turn on the watched address gap filler")
|
|
|
|
serveCmd.PersistentFlags().Int("watched-address-gap-filler-interval", 60, "watched address gap fill interval in secs")
|
|
|
|
|
2021-09-21 12:10:55 +00:00
|
|
|
// and their bindings
|
2021-04-14 15:53:44 +00:00
|
|
|
// eth graphql server
|
|
|
|
viper.BindPFlag("eth.server.graphql", serveCmd.PersistentFlags().Lookup("eth-server-graphql"))
|
|
|
|
viper.BindPFlag("eth.server.graphqlPath", serveCmd.PersistentFlags().Lookup("eth-server-graphql-path"))
|
|
|
|
|
|
|
|
// eth http json-rpc server
|
|
|
|
viper.BindPFlag("eth.server.http", serveCmd.PersistentFlags().Lookup("eth-server-http"))
|
|
|
|
viper.BindPFlag("eth.server.httpPath", serveCmd.PersistentFlags().Lookup("eth-server-http-path"))
|
|
|
|
|
|
|
|
// eth websocket json-rpc server
|
|
|
|
viper.BindPFlag("eth.server.ws", serveCmd.PersistentFlags().Lookup("eth-server-ws"))
|
|
|
|
viper.BindPFlag("eth.server.wsPath", serveCmd.PersistentFlags().Lookup("eth-server-ws-path"))
|
|
|
|
|
|
|
|
// eth ipc json-rpc server
|
|
|
|
viper.BindPFlag("eth.server.ipc", serveCmd.PersistentFlags().Lookup("eth-server-ipc"))
|
|
|
|
viper.BindPFlag("eth.server.ipcPath", serveCmd.PersistentFlags().Lookup("eth-server-ipc-path"))
|
|
|
|
|
|
|
|
// ipld and tracing graphql parameters
|
|
|
|
viper.BindPFlag("ipld.server.graphql", serveCmd.PersistentFlags().Lookup("ipld-server-graphql"))
|
|
|
|
viper.BindPFlag("ipld.server.graphqlPath", serveCmd.PersistentFlags().Lookup("ipld-server-graphql-path"))
|
|
|
|
viper.BindPFlag("ipld.postgraphilePath", serveCmd.PersistentFlags().Lookup("ipld-postgraphile-path"))
|
|
|
|
viper.BindPFlag("tracing.httpPath", serveCmd.PersistentFlags().Lookup("tracing-http-path"))
|
|
|
|
viper.BindPFlag("tracing.postgraphilePath", serveCmd.PersistentFlags().Lookup("tracing-postgraphile-path"))
|
2020-09-02 15:19:25 +00:00
|
|
|
|
2020-10-27 17:42:22 +00:00
|
|
|
viper.BindPFlag("ethereum.httpPath", serveCmd.PersistentFlags().Lookup("eth-http-path"))
|
|
|
|
viper.BindPFlag("ethereum.nodeID", serveCmd.PersistentFlags().Lookup("eth-node-id"))
|
|
|
|
viper.BindPFlag("ethereum.clientName", serveCmd.PersistentFlags().Lookup("eth-client-name"))
|
|
|
|
viper.BindPFlag("ethereum.genesisBlock", serveCmd.PersistentFlags().Lookup("eth-genesis-block"))
|
|
|
|
viper.BindPFlag("ethereum.networkID", serveCmd.PersistentFlags().Lookup("eth-network-id"))
|
2020-10-21 11:22:43 +00:00
|
|
|
viper.BindPFlag("ethereum.chainID", serveCmd.PersistentFlags().Lookup("eth-chain-id"))
|
|
|
|
viper.BindPFlag("ethereum.defaultSender", serveCmd.PersistentFlags().Lookup("eth-default-sender"))
|
|
|
|
viper.BindPFlag("ethereum.rpcGasCap", serveCmd.PersistentFlags().Lookup("eth-rpc-gas-cap"))
|
2021-03-10 16:18:32 +00:00
|
|
|
viper.BindPFlag("ethereum.chainConfig", serveCmd.PersistentFlags().Lookup("eth-chain-config"))
|
2021-03-10 16:46:21 +00:00
|
|
|
viper.BindPFlag("ethereum.supportsStateDiff", serveCmd.PersistentFlags().Lookup("eth-supports-state-diff"))
|
2021-12-27 18:31:25 +00:00
|
|
|
viper.BindPFlag("ethereum.forwardEthCalls", serveCmd.PersistentFlags().Lookup("eth-forward-eth-calls"))
|
2021-12-30 02:29:59 +00:00
|
|
|
viper.BindPFlag("ethereum.proxyOnError", serveCmd.PersistentFlags().Lookup("eth-proxy-on-error"))
|
2021-09-21 12:10:55 +00:00
|
|
|
|
|
|
|
// groupcache flags
|
|
|
|
viper.BindPFlag("groupcache.pool.enabled", serveCmd.PersistentFlags().Lookup("gcache-pool-enabled"))
|
|
|
|
viper.BindPFlag("groupcache.pool.httpEndpoint", serveCmd.PersistentFlags().Lookup("gcache-pool-http-path"))
|
|
|
|
viper.BindPFlag("groupcache.pool.peerHttpEndpoints", serveCmd.PersistentFlags().Lookup("gcache-pool-http-peers"))
|
|
|
|
viper.BindPFlag("groupcache.statedb.cacheSizeInMB", serveCmd.PersistentFlags().Lookup("gcache-statedb-cache-size"))
|
|
|
|
viper.BindPFlag("groupcache.statedb.cacheExpiryInMins", serveCmd.PersistentFlags().Lookup("gcache-statedb-cache-expiry"))
|
|
|
|
viper.BindPFlag("groupcache.statedb.logStatsIntervalInSecs", serveCmd.PersistentFlags().Lookup("gcache-statedb-log-stats-interval"))
|
|
|
|
|
|
|
|
// state validator flags
|
|
|
|
viper.BindPFlag("validator.enabled", serveCmd.PersistentFlags().Lookup("validator-enabled"))
|
|
|
|
viper.BindPFlag("validator.everyNthBlock", serveCmd.PersistentFlags().Lookup("validator-every-nth-block"))
|
2022-01-19 09:42:46 +00:00
|
|
|
|
|
|
|
// watched address gap filler flags
|
2022-01-19 10:37:50 +00:00
|
|
|
viper.BindPFlag("watch.fill.enabled", serveCmd.PersistentFlags().Lookup("watched-address-gap-filler-enabled"))
|
|
|
|
viper.BindPFlag("watch.fill.interval", serveCmd.PersistentFlags().Lookup("watched-address-gap-filler-interval"))
|
2020-08-31 15:58:16 +00:00
|
|
|
}
|