ipld-eth-server/pkg/serve/service.go

187 lines
5.6 KiB
Go
Raw Normal View History

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"
"time"
2020-02-23 23:14:29 +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"
"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"
"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 {
// 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
// Underlying db connection pool
2022-03-10 09:53:03 +00:00
db *sqlx.DB
// rpc client for forwarding cache misses
client *rpc.Client
// whether the proxied client supports state diffing
supportsStateDiffing bool
// timeout for statediff RPC calls
stateDiffTimeout time.Duration
2020-10-27 19:08:10 +00:00
// backend for the server
backend *eth.Backend
// whether to forward eth_calls directly to proxy node
forwardEthCalls bool
// whether to forward eth_getStorageAt directly to proxy node
forwardGetStorageAt bool
// 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)
sap.client = settings.Client
sap.supportsStateDiffing = settings.SupportStateDiff
sap.stateDiffTimeout = settings.StateDiffTimeout
sap.forwardEthCalls = settings.ForwardEthCalls
sap.forwardGetStorageAt = settings.ForwardGetStorageAt
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, &eth.Config{
ChainConfig: settings.ChainConfig,
2021-09-29 05:27:11 +00:00
VMConfig: vm.Config{NoBaseFee: true},
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,
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,
},
}
conf := eth.APIConfig{
SupportsStateDiff: sap.supportsStateDiffing,
ForwardEthCalls: sap.forwardEthCalls,
ForwardGetStorageAt: sap.forwardGetStorageAt,
ProxyOnError: sap.proxyOnError,
StateDiffTimeout: sap.stateDiffTimeout,
}
ethAPI, err := eth.NewPublicEthAPI(sap.backend, sap.client, conf)
if err != nil {
log.Fatalf("unable to create public eth api: %v", err)
}
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
}()
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
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 {
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
}