2019-10-02 14:10:37 +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/>.
|
|
|
|
|
|
|
|
package super_node
|
|
|
|
|
|
|
|
import (
|
2020-02-25 22:38:27 +00:00
|
|
|
"fmt"
|
2019-10-02 14:10:37 +00:00
|
|
|
"sync"
|
2019-10-14 14:41:29 +00:00
|
|
|
"sync/atomic"
|
2019-10-02 14:10:37 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2019-11-04 19:14:05 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/storage/utils"
|
2020-01-31 18:03:37 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2019-10-02 14:10:37 +00:00
|
|
|
)
|
|
|
|
|
2019-10-14 14:41:29 +00:00
|
|
|
const (
|
2019-11-04 19:14:05 +00:00
|
|
|
DefaultMaxBatchSize uint64 = 100
|
2020-03-18 00:42:53 +00:00
|
|
|
DefaultMaxBatchNumber int64 = 50
|
2019-10-14 14:41:29 +00:00
|
|
|
)
|
2019-11-01 19:03:28 +00:00
|
|
|
|
2019-10-02 14:10:37 +00:00
|
|
|
// BackFillInterface for filling in gaps in the super node
|
|
|
|
type BackFillInterface interface {
|
|
|
|
// Method for the super node to periodically check for and fill in gaps in its data using an archival node
|
2020-02-19 04:42:53 +00:00
|
|
|
FillGapsInSuperNode(wg *sync.WaitGroup)
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BackFillService for filling in gaps in the super node
|
|
|
|
type BackFillService struct {
|
2020-01-17 23:16:01 +00:00
|
|
|
// Interface for converting payloads into IPLD object payloads
|
|
|
|
Converter shared.PayloadConverter
|
|
|
|
// Interface for publishing the IPLD payloads to IPFS
|
|
|
|
Publisher shared.IPLDPublisher
|
|
|
|
// Interface for indexing the CIDs of the published IPLDs in Postgres
|
|
|
|
Indexer shared.CIDIndexer
|
2019-10-02 14:10:37 +00:00
|
|
|
// Interface for searching and retrieving CIDs from Postgres index
|
2020-01-17 23:16:01 +00:00
|
|
|
Retriever shared.CIDRetriever
|
|
|
|
// Interface for fetching payloads over at historical blocks; over http
|
|
|
|
Fetcher shared.PayloadFetcher
|
2020-02-13 22:50:56 +00:00
|
|
|
// Channel for forwarding backfill payloads to the ScreenAndServe process
|
2020-02-20 22:12:52 +00:00
|
|
|
ScreenAndServeChan chan shared.ConvertedData
|
2019-10-02 14:10:37 +00:00
|
|
|
// Check frequency
|
|
|
|
GapCheckFrequency time.Duration
|
2020-01-17 23:16:01 +00:00
|
|
|
// Size of batch fetches
|
2019-11-01 19:03:28 +00:00
|
|
|
BatchSize uint64
|
2020-03-18 00:42:53 +00:00
|
|
|
// Number of goroutines
|
|
|
|
BatchNumber int64
|
2020-02-13 22:50:56 +00:00
|
|
|
// Channel for receiving quit signal
|
|
|
|
QuitChan chan bool
|
2020-02-25 22:38:27 +00:00
|
|
|
// Chain type
|
|
|
|
chain shared.ChainType
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackFillService returns a new BackFillInterface
|
2020-02-25 22:38:27 +00:00
|
|
|
func NewBackFillService(settings *Config, screenAndServeChan chan shared.ConvertedData) (BackFillInterface, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
publisher, err := NewIPLDPublisher(settings.Chain, settings.IPFSPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
indexer, err := NewCIDIndexer(settings.Chain, settings.DB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-31 18:03:37 +00:00
|
|
|
converter, err := NewPayloadConverter(settings.Chain)
|
2020-01-17 23:16:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
retriever, err := NewCIDRetriever(settings.Chain, settings.DB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fetcher, err := NewPaylaodFetcher(settings.Chain, settings.HTTPClient)
|
2019-10-08 19:51:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
batchSize := settings.BatchSize
|
|
|
|
if batchSize == 0 {
|
|
|
|
batchSize = DefaultMaxBatchSize
|
|
|
|
}
|
2020-03-18 00:42:53 +00:00
|
|
|
batchNumber := int64(settings.BatchNumber)
|
|
|
|
if batchNumber == 0 {
|
|
|
|
batchNumber = DefaultMaxBatchNumber
|
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
return &BackFillService{
|
2020-02-13 22:50:56 +00:00
|
|
|
Indexer: indexer,
|
|
|
|
Converter: converter,
|
|
|
|
Publisher: publisher,
|
|
|
|
Retriever: retriever,
|
|
|
|
Fetcher: fetcher,
|
|
|
|
GapCheckFrequency: settings.Frequency,
|
|
|
|
BatchSize: batchSize,
|
2020-03-18 00:42:53 +00:00
|
|
|
BatchNumber: int64(batchNumber),
|
2020-02-13 22:50:56 +00:00
|
|
|
ScreenAndServeChan: screenAndServeChan,
|
|
|
|
QuitChan: settings.Quit,
|
2020-02-25 22:38:27 +00:00
|
|
|
chain: settings.Chain,
|
2019-10-08 19:51:38 +00:00
|
|
|
}, nil
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 04:42:53 +00:00
|
|
|
// FillGapsInSuperNode periodically checks for and fills in gaps in the super node db
|
|
|
|
func (bfs *BackFillService) FillGapsInSuperNode(wg *sync.WaitGroup) {
|
2019-10-02 14:10:37 +00:00
|
|
|
ticker := time.NewTicker(bfs.GapCheckFrequency)
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2020-02-13 22:50:56 +00:00
|
|
|
case <-bfs.QuitChan:
|
2020-03-18 00:42:53 +00:00
|
|
|
log.Infof("quiting %s FillGapsInSuperNode process", bfs.chain.String())
|
2019-10-02 14:10:37 +00:00
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2020-02-25 22:38:27 +00:00
|
|
|
log.Infof("searching for gaps in the %s super node database", bfs.chain.String())
|
2020-01-17 23:16:01 +00:00
|
|
|
startingBlock, err := bfs.Retriever.RetrieveFirstBlockNumber()
|
|
|
|
if err != nil {
|
2020-03-08 16:43:29 +00:00
|
|
|
log.Errorf("super node db backfill RetrieveFirstBlockNumber error for chain %s: %v", bfs.chain.String(), err)
|
2019-10-02 14:10:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-02-10 17:16:57 +00:00
|
|
|
if startingBlock != 0 {
|
2020-02-25 22:38:27 +00:00
|
|
|
log.Infof("found gap at the beginning of the %s sync", bfs.chain.String())
|
2020-03-18 00:42:53 +00:00
|
|
|
if err := bfs.backFill(0, uint64(startingBlock-1)); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
gaps, err := bfs.Retriever.RetrieveGapsInData()
|
|
|
|
if err != nil {
|
2020-03-08 19:11:31 +00:00
|
|
|
log.Errorf("super node db backfill RetrieveGapsInData error for chain %s: %v", bfs.chain.String(), err)
|
2019-10-02 14:10:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, gap := range gaps {
|
2020-03-18 00:42:53 +00:00
|
|
|
if err := bfs.backFill(gap.Start, gap.Stop); err != nil {
|
2020-01-17 23:16:01 +00:00
|
|
|
log.Error(err)
|
|
|
|
}
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-02-25 22:38:27 +00:00
|
|
|
log.Infof("%s fillGaps goroutine successfully spun up", bfs.chain.String())
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 19:14:05 +00:00
|
|
|
// backFill fetches, processes, and returns utils.StorageDiffs over a range of blocks
|
2019-10-14 14:41:29 +00:00
|
|
|
// It splits a large range up into smaller chunks, batch fetching and processing those chunks concurrently
|
2020-03-18 00:42:53 +00:00
|
|
|
func (bfs *BackFillService) backFill(startingBlock, endingBlock uint64) error {
|
|
|
|
log.Infof("filling in %s gap from %d to %d", bfs.chain.String(), startingBlock, endingBlock)
|
2019-10-14 14:41:29 +00:00
|
|
|
if endingBlock < startingBlock {
|
2020-03-08 16:43:29 +00:00
|
|
|
return fmt.Errorf("super node %s db backfill: ending block number needs to be greater than starting block number", bfs.chain.String())
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
2019-11-04 19:14:05 +00:00
|
|
|
//
|
2019-10-14 14:41:29 +00:00
|
|
|
// break the range up into bins of smaller ranges
|
2019-11-04 19:14:05 +00:00
|
|
|
blockRangeBins, err := utils.GetBlockHeightBins(startingBlock, endingBlock, bfs.BatchSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-02 14:10:37 +00:00
|
|
|
}
|
2019-10-14 14:41:29 +00:00
|
|
|
// int64 for atomic incrementing and decrementing to track the number of active processing goroutines we have
|
|
|
|
var activeCount int64
|
|
|
|
// channel for processing goroutines to signal when they are done
|
2020-03-18 00:42:53 +00:00
|
|
|
processingDone := make(chan bool)
|
2019-10-14 14:41:29 +00:00
|
|
|
forwardDone := make(chan bool)
|
|
|
|
|
2020-03-18 00:42:53 +00:00
|
|
|
// for each block range bin spin up a goroutine to batch fetch and process data for that range
|
2019-10-14 14:41:29 +00:00
|
|
|
go func() {
|
|
|
|
for _, blockHeights := range blockRangeBins {
|
|
|
|
// if we have reached our limit of active goroutines
|
|
|
|
// wait for one to finish before starting the next
|
2020-03-18 00:42:53 +00:00
|
|
|
if atomic.AddInt64(&activeCount, 1) > bfs.BatchNumber {
|
2019-10-14 14:41:29 +00:00
|
|
|
// this blocks until a process signals it has finished
|
|
|
|
<-forwardDone
|
|
|
|
}
|
|
|
|
go func(blockHeights []uint64) {
|
2020-01-17 23:16:01 +00:00
|
|
|
payloads, err := bfs.Fetcher.FetchAt(blockHeights)
|
|
|
|
if err != nil {
|
2020-03-18 00:42:53 +00:00
|
|
|
log.Errorf("%s super node historical data fetcher error: %s", bfs.chain.String(), err.Error())
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
|
|
|
for _, payload := range payloads {
|
2020-01-17 23:16:01 +00:00
|
|
|
ipldPayload, err := bfs.Converter.Convert(payload)
|
|
|
|
if err != nil {
|
2020-03-18 00:42:53 +00:00
|
|
|
log.Errorf("%s super node historical data converter error: %s", bfs.chain.String(), err.Error())
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
2020-02-13 22:50:56 +00:00
|
|
|
// If there is a ScreenAndServe process listening, forward payload to it
|
|
|
|
select {
|
|
|
|
case bfs.ScreenAndServeChan <- ipldPayload:
|
|
|
|
default:
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
cidPayload, err := bfs.Publisher.Publish(ipldPayload)
|
|
|
|
if err != nil {
|
2020-03-18 00:42:53 +00:00
|
|
|
log.Errorf("%s super node historical data publisher error: %s", bfs.chain.String(), err.Error())
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
if err := bfs.Indexer.Index(cidPayload); err != nil {
|
2020-03-18 00:42:53 +00:00
|
|
|
log.Errorf("%s super node historical data indexer error: %s", bfs.chain.String(), err.Error())
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// when this goroutine is done, send out a signal
|
2020-03-18 00:42:53 +00:00
|
|
|
log.Infof("finished filling in %s gap from %d to %d", bfs.chain.String(), blockHeights[0], blockHeights[len(blockHeights)-1])
|
|
|
|
processingDone <- true
|
2019-10-14 14:41:29 +00:00
|
|
|
}(blockHeights)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-18 00:42:53 +00:00
|
|
|
// listen on the processingDone chan
|
2019-10-14 14:41:29 +00:00
|
|
|
// keeps track of the number of processing goroutines that have finished
|
2020-03-18 00:42:53 +00:00
|
|
|
// when they have all finished, return
|
|
|
|
goroutinesFinished := 0
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-processingDone:
|
|
|
|
atomic.AddInt64(&activeCount, -1)
|
2019-10-14 14:41:29 +00:00
|
|
|
select {
|
2020-03-18 00:42:53 +00:00
|
|
|
// if we are waiting for a process to finish, signal that one has
|
|
|
|
case forwardDone <- true:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
goroutinesFinished++
|
|
|
|
if goroutinesFinished >= len(blockRangeBins) {
|
|
|
|
return nil
|
2019-10-14 14:41:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-18 00:42:53 +00:00
|
|
|
}
|
2019-11-01 19:03:28 +00:00
|
|
|
}
|