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

382 lines
13 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 (
2020-05-30 03:02:47 +00:00
"fmt"
2021-04-09 14:50:46 +00:00
"strconv"
2020-02-19 22:32:59 +00:00
"sync"
2020-02-23 23:14:29 +00:00
2020-05-30 03:02:47 +00:00
"github.com/ethereum/go-ethereum/common"
2021-08-31 11:32:01 +00:00
"github.com/ethereum/go-ethereum/core/vm"
2020-05-30 03:02:47 +00:00
"github.com/ethereum/go-ethereum/crypto"
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-23 23:14:29 +00:00
"github.com/ethereum/go-ethereum/rlp"
2020-02-19 22:32:59 +00:00
"github.com/ethereum/go-ethereum/rpc"
2021-08-31 11:32:01 +00:00
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
2020-05-30 03:02:47 +00:00
log "github.com/sirupsen/logrus"
2020-08-31 15:47:06 +00:00
"github.com/vulcanize/ipld-eth-server/pkg/eth"
2021-08-31 11:32:01 +00:00
"github.com/vulcanize/ipld-eth-server/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
2021-08-12 06:23:41 +00:00
Serve(wg *sync.WaitGroup, screenAndServePayload <-chan eth.ConvertedPayload)
2020-05-30 03:02:47 +00:00
// Method to subscribe to the service
2020-08-31 15:47:06 +00:00
Subscribe(id rpc.ID, sub chan<- SubscriptionPayload, quitChan chan<- bool, params eth.SubscriptionSettings)
2020-05-30 03:02:47 +00:00
// Method to unsubscribe from the service
Unsubscribe(id rpc.ID)
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
// Interface for filtering and serving data according to subscribed clients according to their specification
2020-08-31 15:47:06 +00:00
Filterer eth.Filterer
2020-05-30 03:02:47 +00:00
// Interface for fetching IPLD objects from IPFS
2020-08-31 15:47:06 +00:00
IPLDFetcher eth.Fetcher
2020-05-30 03:02:47 +00:00
// Interface for searching and retrieving CIDs from Postgres index
2020-08-31 15:47:06 +00:00
Retriever eth.Retriever
2020-05-30 03:02:47 +00:00
// Used to signal shutdown of the service
QuitChan chan bool
// A mapping of rpc.IDs to their subscription channels, mapped to their subscription type (hash of the StreamFilters)
Subscriptions map[common.Hash]map[rpc.ID]Subscription
// A mapping of subscription params hash to the corresponding subscription params
2020-08-31 15:47:06 +00:00
SubscriptionTypes map[common.Hash]eth.SubscriptionSettings
2020-05-30 03:02:47 +00:00
// Underlying db
db *postgres.DB
// wg for syncing serve processes
serveWg *sync.WaitGroup
// rpc client for forwarding cache misses
client *rpc.Client
// whether the proxied client supports state diffing
supportsStateDiffing bool
2020-10-27 19:08:10 +00:00
// backend for the server
backend *eth.Backend
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.Retriever = eth.NewCIDRetriever(settings.DB)
sap.IPLDFetcher = eth.NewIPLDFetcher(settings.DB)
sap.Filterer = eth.NewResponseFilterer()
sap.db = settings.DB
sap.QuitChan = make(chan bool)
sap.Subscriptions = make(map[common.Hash]map[rpc.ID]Subscription)
sap.SubscriptionTypes = make(map[common.Hash]eth.SubscriptionSettings)
sap.client = settings.Client
sap.supportsStateDiffing = settings.SupportStateDiff
2020-10-27 19:08:10 +00:00
var err error
sap.backend, err = eth.NewEthBackend(sap.db, &eth.Config{
ChainConfig: settings.ChainConfig,
VMConfig: vm.Config{},
DefaultSender: settings.DefaultSender,
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 {
2021-04-09 14:50:46 +00:00
networkID, _ := strconv.ParseUint(sap.db.Node.NetworkID, 10, 64)
2020-05-30 03:02:47 +00:00
apis := []rpc.API{
{
Namespace: APIName,
Version: APIVersion,
2020-08-31 15:58:16 +00:00
Service: NewPublicServerAPI(sap),
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,
},
}
2020-08-31 15:47:06 +00:00
return append(apis, rpc.API{
Namespace: eth.APIName,
Version: eth.APIVersion,
Service: eth.NewPublicEthAPI(sap.backend, sap.client, sap.supportsStateDiffing),
2020-08-31 15:47:06 +00:00
Public: true,
})
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
2021-08-12 06:23:41 +00:00
func (sap *Service) Serve(wg *sync.WaitGroup, screenAndServePayload <-chan eth.ConvertedPayload) {
2020-05-30 03:02:47 +00:00
sap.serveWg = wg
2020-02-23 23:14:29 +00:00
go func() {
2020-05-30 03:02:47 +00:00
wg.Add(1)
defer wg.Done()
2020-02-23 23:14:29 +00:00
for {
select {
2020-05-30 03:02:47 +00:00
case payload := <-screenAndServePayload:
sap.filterAndServe(payload)
case <-sap.QuitChan:
2020-08-31 15:47:06 +00:00
log.Info("quiting eth ipld server process")
2020-02-23 23:14:29 +00:00
return
}
}
}()
2020-08-31 15:47:06 +00:00
log.Info("eth ipld server process successfully spun up")
2020-02-19 22:32:59 +00:00
}
2020-05-30 03:02:47 +00:00
// filterAndServe filters the payload according to each subscription type and sends to the subscriptions
2021-08-12 06:23:41 +00:00
func (sap *Service) filterAndServe(payload eth.ConvertedPayload) {
2020-08-31 15:47:06 +00:00
log.Debug("sending eth ipld payload to subscriptions")
2020-05-30 03:02:47 +00:00
sap.Lock()
sap.serveWg.Add(1)
defer sap.Unlock()
defer sap.serveWg.Done()
for ty, subs := range sap.Subscriptions {
// Retrieve the subscription parameters for this subscription type
subConfig, ok := sap.SubscriptionTypes[ty]
if !ok {
2020-08-31 15:47:06 +00:00
log.Errorf("eth ipld server subscription configuration for subscription type %s not available", ty.Hex())
2020-05-30 03:02:47 +00:00
sap.closeType(ty)
continue
}
2020-08-31 15:47:06 +00:00
if subConfig.End.Int64() > 0 && subConfig.End.Int64() < payload.Block.Number().Int64() {
2020-05-30 03:02:47 +00:00
// We are not out of range for this subscription type
// close it, and continue to the next
sap.closeType(ty)
continue
}
response, err := sap.Filterer.Filter(subConfig, payload)
if err != nil {
2020-08-31 15:47:06 +00:00
log.Errorf("eth ipld server filtering error: %v", err)
2020-05-30 03:02:47 +00:00
sap.closeType(ty)
continue
}
responseRLP, err := rlp.EncodeToBytes(response)
if err != nil {
2020-08-31 15:47:06 +00:00
log.Errorf("eth ipld server rlp encoding error: %v", err)
2020-05-30 03:02:47 +00:00
continue
}
for id, sub := range subs {
select {
2020-08-31 15:47:06 +00:00
case sub.PayloadChan <- SubscriptionPayload{Data: responseRLP, Err: "", Flag: EmptyFlag, Height: response.BlockNumber.Int64()}:
log.Debugf("sending eth ipld server payload to subscription %s", id)
2020-05-30 03:02:47 +00:00
default:
2020-08-31 15:47:06 +00:00
log.Infof("unable to send eth ipld payload to subscription %s; channel has no receiver", id)
2020-05-30 03:02:47 +00:00
}
}
}
}
// Subscribe is used by the API to remotely subscribe to the service loop
// The params must be rlp serializable and satisfy the SubscriptionSettings() interface
2020-08-31 15:47:06 +00:00
func (sap *Service) Subscribe(id rpc.ID, sub chan<- SubscriptionPayload, quitChan chan<- bool, params eth.SubscriptionSettings) {
2020-05-30 03:02:47 +00:00
sap.serveWg.Add(1)
defer sap.serveWg.Done()
2020-08-31 15:47:06 +00:00
log.Infof("new eth ipld subscription %s", id)
2020-05-30 03:02:47 +00:00
subscription := Subscription{
ID: id,
PayloadChan: sub,
QuitChan: quitChan,
}
// Subscription type is defined as the hash of the rlp-serialized subscription settings
by, err := rlp.EncodeToBytes(params)
if err != nil {
sendNonBlockingErr(subscription, err)
sendNonBlockingQuit(subscription)
return
}
subscriptionType := crypto.Keccak256Hash(by)
2020-08-31 15:47:06 +00:00
if !params.BackFillOnly {
2020-05-30 03:02:47 +00:00
// Add subscriber
sap.Lock()
if sap.Subscriptions[subscriptionType] == nil {
sap.Subscriptions[subscriptionType] = make(map[rpc.ID]Subscription)
}
sap.Subscriptions[subscriptionType][id] = subscription
sap.SubscriptionTypes[subscriptionType] = params
sap.Unlock()
}
// If the subscription requests a backfill, use the Postgres index to lookup and retrieve historical data
// Otherwise we only filter new data as it is streamed in from the state diffing geth node
2020-08-31 15:47:06 +00:00
if params.BackFill || params.BackFillOnly {
2020-05-30 03:02:47 +00:00
if err := sap.sendHistoricalData(subscription, id, params); err != nil {
2020-08-31 15:47:06 +00:00
sendNonBlockingErr(subscription, fmt.Errorf("eth ipld server subscription backfill error: %v", err))
2020-05-30 03:02:47 +00:00
sendNonBlockingQuit(subscription)
return
}
}
}
// sendHistoricalData sends historical data to the requesting subscription
2020-08-31 15:47:06 +00:00
func (sap *Service) sendHistoricalData(sub Subscription, id rpc.ID, params eth.SubscriptionSettings) error {
log.Infof("sending eth ipld historical data to subscription %s", id)
2020-05-30 03:02:47 +00:00
// Retrieve cached CIDs relevant to this subscriber
var endingBlock int64
var startingBlock int64
var err error
startingBlock, err = sap.Retriever.RetrieveFirstBlockNumber()
if err != nil {
return err
}
2020-08-31 15:47:06 +00:00
if startingBlock < params.Start.Int64() {
startingBlock = params.Start.Int64()
2020-05-30 03:02:47 +00:00
}
endingBlock, err = sap.Retriever.RetrieveLastBlockNumber()
if err != nil {
return err
}
2020-08-31 15:47:06 +00:00
if endingBlock > params.End.Int64() && params.End.Int64() > 0 && params.End.Int64() > startingBlock {
endingBlock = params.End.Int64()
2020-05-30 03:02:47 +00:00
}
2020-08-31 15:47:06 +00:00
log.Debugf("eth ipld historical data starting block: %d", params.Start.Int64())
log.Debugf("eth ipld historical data ending block: %d", endingBlock)
2020-02-19 22:32:59 +00:00
go func() {
2020-05-30 03:02:47 +00:00
sap.serveWg.Add(1)
defer sap.serveWg.Done()
for i := startingBlock; i <= endingBlock; i++ {
2020-02-19 22:32:59 +00:00
select {
2020-05-30 03:02:47 +00:00
case <-sap.QuitChan:
2020-09-02 15:19:25 +00:00
log.Infof("ethereum historical data feed to subscription %s closed", id)
2020-05-30 03:02:47 +00:00
return
default:
}
cidWrappers, empty, err := sap.Retriever.Retrieve(params, i)
if err != nil {
2020-08-31 15:47:06 +00:00
sendNonBlockingErr(sub, fmt.Errorf("eth ipld server cid retrieval error at block %d\r%s", i, err.Error()))
2020-05-30 03:02:47 +00:00
continue
}
if empty {
continue
}
for _, cids := range cidWrappers {
response, err := sap.IPLDFetcher.Fetch(cids)
if err != nil {
2020-08-31 15:47:06 +00:00
sendNonBlockingErr(sub, fmt.Errorf("eth ipld server ipld fetching error at block %d\r%s", i, err.Error()))
2020-02-19 22:32:59 +00:00
continue
}
2020-05-30 03:02:47 +00:00
responseRLP, err := rlp.EncodeToBytes(response)
if err != nil {
log.Error(err)
continue
2020-02-19 22:32:59 +00:00
}
2020-05-30 03:02:47 +00:00
select {
2020-08-31 15:47:06 +00:00
case sub.PayloadChan <- SubscriptionPayload{Data: responseRLP, Err: "", Flag: EmptyFlag, Height: response.BlockNumber.Int64()}:
log.Debugf("eth ipld server sending historical data payload to subscription %s", id)
2020-05-30 03:02:47 +00:00
default:
2020-08-31 15:47:06 +00:00
log.Infof("eth ipld server unable to send backFill payload to subscription %s; channel has no receiver", id)
2020-02-19 22:32:59 +00:00
}
}
}
2020-05-30 03:02:47 +00:00
// when we are done backfilling send an empty payload signifying so in the msg
select {
case sub.PayloadChan <- SubscriptionPayload{Data: nil, Err: "", Flag: BackFillCompleteFlag}:
2020-08-31 15:47:06 +00:00
log.Debugf("eth ipld server sending backFill completion notice to subscription %s", id)
2020-05-30 03:02:47 +00:00
default:
2020-09-02 15:19:25 +00:00
log.Infof("eth ipld server unable to send backFill completion notice to subscription %s", id)
2020-05-30 03:02:47 +00:00
}
2020-02-19 22:32:59 +00:00
}()
2020-05-30 03:02:47 +00:00
return nil
}
// Unsubscribe is used by the API to remotely unsubscribe to the StateDiffingService loop
func (sap *Service) Unsubscribe(id rpc.ID) {
2020-08-31 15:47:06 +00:00
log.Infof("unsubscribing %s from the eth ipld server", id)
2020-05-30 03:02:47 +00:00
sap.Lock()
for ty := range sap.Subscriptions {
delete(sap.Subscriptions[ty], id)
if len(sap.Subscriptions[ty]) == 0 {
// If we removed the last subscription of this type, remove the subscription type outright
delete(sap.Subscriptions, ty)
delete(sap.SubscriptionTypes, ty)
}
}
sap.Unlock()
}
// 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)
2021-08-12 06:23:41 +00:00
payloadChan := make(chan eth.ConvertedPayload, PayloadChanBufferSize)
2020-05-30 03:02:47 +00:00
sap.Serve(wg, payloadChan)
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 {
2020-08-31 15:47:06 +00:00
log.Infof("stopping eth ipld server")
2020-05-30 03:02:47 +00:00
sap.Lock()
close(sap.QuitChan)
sap.close()
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
}
2020-05-30 03:02:47 +00:00
// close is used to close all listening subscriptions
// close needs to be called with subscription access locked
func (sap *Service) close() {
2020-08-31 15:47:06 +00:00
log.Infof("closing all eth ipld server subscriptions")
2020-05-30 03:02:47 +00:00
for subType, subs := range sap.Subscriptions {
for _, sub := range subs {
sendNonBlockingQuit(sub)
}
delete(sap.Subscriptions, subType)
delete(sap.SubscriptionTypes, subType)
}
}
// closeType is used to close all subscriptions of given type
// closeType needs to be called with subscription access locked
func (sap *Service) closeType(subType common.Hash) {
2020-08-31 15:47:06 +00:00
log.Infof("closing all eth ipld server subscriptions of type %s", subType.String())
2020-05-30 03:02:47 +00:00
subs := sap.Subscriptions[subType]
for _, sub := range subs {
sendNonBlockingQuit(sub)
}
delete(sap.Subscriptions, subType)
delete(sap.SubscriptionTypes, subType)
2020-02-19 22:32:59 +00:00
}