2020-02-19 22:32:59 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-08-31 15:58:16 +00:00
|
|
|
package serve
|
2020-02-19 22:32:59 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-09 14:50:46 +00:00
|
|
|
"strconv"
|
2020-02-19 22:32:59 +00:00
|
|
|
"sync"
|
2023-01-13 01:18:12 +00:00
|
|
|
"time"
|
2020-02-23 23:14:29 +00:00
|
|
|
|
2023-04-14 06:26:46 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/log"
|
2021-08-31 11:32:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2022-09-21 11:45:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2020-06-30 00:16:52 +00:00
|
|
|
ethnode "github.com/ethereum/go-ethereum/node"
|
2020-05-30 03:02:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2020-02-19 22:32:59 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2022-03-10 09:53:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2021-09-21 12:10:55 +00:00
|
|
|
|
2023-04-14 06:26:46 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/debug"
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/eth"
|
|
|
|
"github.com/cerc-io/ipld-eth-server/v5/pkg/net"
|
2020-05-30 03:02:47 +00:00
|
|
|
)
|
2020-02-19 22:32:59 +00:00
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
const (
|
|
|
|
PayloadChanBufferSize = 2000
|
2020-02-19 22:32:59 +00:00
|
|
|
)
|
|
|
|
|
2020-08-31 15:58:16 +00:00
|
|
|
// Server is the top level interface for streaming, converting to IPLDs, publishing,
|
2020-05-30 03:02:47 +00:00
|
|
|
// and indexing all chain data; screening this data; and serving it up to subscribed clients
|
|
|
|
// This service is compatible with the Ethereum service interface (node.Service)
|
2020-08-31 15:58:16 +00:00
|
|
|
type Server interface {
|
2021-02-19 20:23:45 +00:00
|
|
|
// Start() and Stop()
|
|
|
|
ethnode.Lifecycle
|
|
|
|
APIs() []rpc.API
|
|
|
|
Protocols() []p2p.Protocol
|
2020-05-30 03:02:47 +00:00
|
|
|
// Pub-Sub handling event loop
|
2023-02-22 01:44:46 +00:00
|
|
|
Serve(wg *sync.WaitGroup)
|
2020-10-27 19:08:10 +00:00
|
|
|
// Backend exposes the server's backend
|
|
|
|
Backend() *eth.Backend
|
2020-02-19 22:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// Service is the underlying struct for the watcher
|
2020-02-19 22:32:59 +00:00
|
|
|
type Service struct {
|
2020-05-30 03:02:47 +00:00
|
|
|
// Used to sync access to the Subscriptions
|
|
|
|
sync.Mutex
|
|
|
|
// Used to signal shutdown of the service
|
|
|
|
QuitChan chan bool
|
2023-03-13 23:23:49 +00:00
|
|
|
// Underlying db connection pool
|
2022-03-10 09:53:03 +00:00
|
|
|
db *sqlx.DB
|
2020-10-26 13:58:37 +00:00
|
|
|
// rpc client for forwarding cache misses
|
|
|
|
client *rpc.Client
|
2021-02-24 16:50:26 +00:00
|
|
|
// whether the proxied client supports state diffing
|
|
|
|
supportsStateDiffing bool
|
2023-01-13 01:18:12 +00:00
|
|
|
// timeout for statediff RPC calls
|
|
|
|
stateDiffTimeout time.Duration
|
2020-10-27 19:08:10 +00:00
|
|
|
// backend for the server
|
|
|
|
backend *eth.Backend
|
2021-12-27 18:25:54 +00:00
|
|
|
// whether to forward eth_calls directly to proxy node
|
|
|
|
forwardEthCalls bool
|
2023-01-10 23:39:21 +00:00
|
|
|
// whether to forward eth_getStorageAt directly to proxy node
|
|
|
|
forwardGetStorageAt bool
|
2023-06-16 16:49:56 +00:00
|
|
|
// the maximum size of the block range to use in GetLogs
|
|
|
|
getLogsBlockLimit int64
|
2021-12-30 02:29:59 +00:00
|
|
|
// whether to forward all calls to proxy node if they throw an error locally
|
|
|
|
proxyOnError bool
|
2022-03-10 09:53:03 +00:00
|
|
|
// eth node network id
|
|
|
|
nodeNetworkId string
|
2020-05-30 03:02:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:58:16 +00:00
|
|
|
// NewServer creates a new Server using an underlying Service struct
|
|
|
|
func NewServer(settings *Config) (Server, error) {
|
2020-10-27 19:08:10 +00:00
|
|
|
sap := new(Service)
|
|
|
|
sap.db = settings.DB
|
|
|
|
sap.QuitChan = make(chan bool)
|
2021-02-24 16:50:26 +00:00
|
|
|
sap.client = settings.Client
|
|
|
|
sap.supportsStateDiffing = settings.SupportStateDiff
|
2023-01-13 01:18:12 +00:00
|
|
|
sap.stateDiffTimeout = settings.StateDiffTimeout
|
2021-12-27 18:25:54 +00:00
|
|
|
sap.forwardEthCalls = settings.ForwardEthCalls
|
2023-01-10 23:39:21 +00:00
|
|
|
sap.forwardGetStorageAt = settings.ForwardGetStorageAt
|
2023-06-16 16:49:56 +00:00
|
|
|
sap.getLogsBlockLimit = settings.GetLogsBlockLimit
|
2021-12-30 02:29:59 +00:00
|
|
|
sap.proxyOnError = settings.ProxyOnError
|
2022-03-10 09:53:03 +00:00
|
|
|
sap.nodeNetworkId = settings.NodeNetworkID
|
2020-10-27 19:08:10 +00:00
|
|
|
var err error
|
|
|
|
sap.backend, err = eth.NewEthBackend(sap.db, ð.Config{
|
2021-09-21 12:10:55 +00:00
|
|
|
ChainConfig: settings.ChainConfig,
|
2021-09-29 05:27:11 +00:00
|
|
|
VMConfig: vm.Config{NoBaseFee: true},
|
2021-09-21 12:10:55 +00:00
|
|
|
RPCGasCap: settings.RPCGasCap,
|
|
|
|
GroupCacheConfig: settings.GroupCache,
|
2020-10-27 19:08:10 +00:00
|
|
|
})
|
|
|
|
return sap, err
|
2020-05-30 03:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Protocols exports the services p2p protocols, this service has none
|
|
|
|
func (sap *Service) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{}
|
2020-02-19 22:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// APIs returns the RPC descriptors the watcher service offers
|
2020-05-30 03:02:47 +00:00
|
|
|
func (sap *Service) APIs() []rpc.API {
|
2022-03-10 09:53:03 +00:00
|
|
|
networkID, _ := strconv.ParseUint(sap.nodeNetworkId, 10, 64)
|
2020-05-30 03:02:47 +00:00
|
|
|
apis := []rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: APIName,
|
|
|
|
Version: APIVersion,
|
2022-03-16 11:39:32 +00:00
|
|
|
Service: NewPublicServerAPI(sap, sap.client),
|
2020-05-30 03:02:47 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
{
|
2021-04-09 14:50:46 +00:00
|
|
|
Namespace: net.APIName,
|
|
|
|
Version: net.APIVersion,
|
|
|
|
Service: net.NewPublicNetAPI(networkID, sap.client),
|
2020-05-30 03:02:47 +00:00
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
2023-01-13 01:18:12 +00:00
|
|
|
conf := eth.APIConfig{
|
|
|
|
SupportsStateDiff: sap.supportsStateDiffing,
|
|
|
|
ForwardEthCalls: sap.forwardEthCalls,
|
|
|
|
ForwardGetStorageAt: sap.forwardGetStorageAt,
|
|
|
|
ProxyOnError: sap.proxyOnError,
|
|
|
|
StateDiffTimeout: sap.stateDiffTimeout,
|
2023-06-16 16:49:56 +00:00
|
|
|
GetLogsBlockLimit: sap.getLogsBlockLimit,
|
2023-01-13 01:18:12 +00:00
|
|
|
}
|
|
|
|
ethAPI, err := eth.NewPublicEthAPI(sap.backend, sap.client, conf)
|
2021-12-27 18:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to create public eth api: %v", err)
|
|
|
|
}
|
2022-09-21 11:45:03 +00:00
|
|
|
|
|
|
|
debugTracerAPI := tracers.APIs(&debug.Backend{Backend: *sap.backend})[0]
|
|
|
|
|
|
|
|
return append(apis,
|
|
|
|
rpc.API{
|
|
|
|
Namespace: eth.APIName,
|
|
|
|
Version: eth.APIVersion,
|
|
|
|
Service: ethAPI,
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
debugTracerAPI,
|
|
|
|
)
|
2020-05-30 03:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve listens for incoming converter data off the screenAndServePayload from the Sync process
|
|
|
|
// It filters and sends this data to any subscribers to the service
|
|
|
|
// This process can also be stood up alone, without an screenAndServePayload attached to a Sync process
|
|
|
|
// and it will hang on the WaitGroup indefinitely, allowing the Service to serve historical data requests only
|
2023-02-22 01:44:46 +00:00
|
|
|
func (sap *Service) Serve(wg *sync.WaitGroup) {
|
2020-02-23 23:14:29 +00:00
|
|
|
go func() {
|
2020-05-30 03:02:47 +00:00
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Done()
|
2023-02-22 01:44:46 +00:00
|
|
|
<-sap.QuitChan
|
|
|
|
log.Info("quiting eth ipld server process")
|
2020-02-23 23:14:29 +00:00
|
|
|
}()
|
2023-04-14 06:26:46 +00:00
|
|
|
log.Debug("eth ipld server process successfully spun up")
|
2020-02-19 22:32:59 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
// Start is used to begin the service
|
|
|
|
// This is mostly just to satisfy the node.Service interface
|
2021-02-19 20:23:45 +00:00
|
|
|
func (sap *Service) Start() error {
|
2020-08-31 15:47:06 +00:00
|
|
|
log.Info("starting eth ipld server")
|
2020-05-30 03:02:47 +00:00
|
|
|
wg := new(sync.WaitGroup)
|
2023-02-22 01:44:46 +00:00
|
|
|
sap.Serve(wg)
|
2020-05-30 03:02:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is used to close down the service
|
|
|
|
// This is mostly just to satisfy the node.Service interface
|
|
|
|
func (sap *Service) Stop() error {
|
2023-04-14 06:26:46 +00:00
|
|
|
log.Info("stopping eth ipld server")
|
2020-05-30 03:02:47 +00:00
|
|
|
sap.Lock()
|
|
|
|
close(sap.QuitChan)
|
|
|
|
sap.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:08:10 +00:00
|
|
|
// Backend exposes the server's backend
|
|
|
|
func (sap *Service) Backend() *eth.Backend {
|
|
|
|
return sap.backend
|
|
|
|
}
|