2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2017-11-09 19:34:58 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// 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.
|
2017-11-09 19:34:58 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// 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.
|
2017-12-04 21:12:27 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2018-01-25 19:21:55 +00:00
|
|
|
package utils
|
2017-12-11 21:08:00 +00:00
|
|
|
|
2017-11-09 19:34:58 +00:00
|
|
|
import (
|
2020-05-30 03:02:47 +00:00
|
|
|
"errors"
|
2018-01-25 19:21:55 +00:00
|
|
|
|
2019-05-17 04:15:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-06-22 18:12:32 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/config"
|
2020-06-30 00:16:52 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/node"
|
2020-06-22 18:12:32 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/postgres"
|
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/shared"
|
2017-11-09 19:34:58 +00:00
|
|
|
)
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
func LoadPostgres(database config.Database, node node.Node) postgres.DB {
|
2018-02-12 16:54:05 +00:00
|
|
|
db, err := postgres.NewDB(database, node)
|
2017-12-04 15:53:36 +00:00
|
|
|
if err != nil {
|
2019-10-28 10:48:31 +00:00
|
|
|
logrus.Fatal("Error loading postgres: ", err)
|
2017-12-04 15:53:36 +00:00
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
return *db
|
2017-12-04 15:53:36 +00:00
|
|
|
}
|
2017-12-04 21:12:27 +00:00
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
// GetBlockHeightBins splits a block range up into bins of block heights of the given batch size
|
|
|
|
func GetBlockHeightBins(startingBlock, endingBlock, batchSize uint64) ([][]uint64, error) {
|
|
|
|
if endingBlock < startingBlock {
|
|
|
|
return nil, errors.New("backfill: ending block number needs to be greater than starting block number")
|
2017-12-04 21:12:27 +00:00
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
if batchSize == 0 {
|
|
|
|
return nil, errors.New("backfill: batchsize needs to be greater than zero")
|
2018-01-23 18:43:35 +00:00
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
length := endingBlock - startingBlock + 1
|
|
|
|
numberOfBins := length / batchSize
|
2020-06-30 00:16:52 +00:00
|
|
|
if length%batchSize != 0 {
|
2020-05-30 03:02:47 +00:00
|
|
|
numberOfBins++
|
2017-12-07 15:58:06 +00:00
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
blockRangeBins := make([][]uint64, numberOfBins)
|
|
|
|
for i := range blockRangeBins {
|
|
|
|
nextBinStart := startingBlock + batchSize
|
|
|
|
blockRange := make([]uint64, 0, nextBinStart-startingBlock+1)
|
|
|
|
for j := startingBlock; j < nextBinStart && j <= endingBlock; j++ {
|
|
|
|
blockRange = append(blockRange, j)
|
|
|
|
}
|
|
|
|
startingBlock = nextBinStart
|
|
|
|
blockRangeBins[i] = blockRange
|
2017-12-07 15:58:06 +00:00
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
return blockRangeBins, nil
|
2017-12-07 15:58:06 +00:00
|
|
|
}
|
2017-12-11 21:08:00 +00:00
|
|
|
|
2020-05-30 03:02:47 +00:00
|
|
|
// MissingHeightsToGaps returns a slice of gaps from a slice of missing block heights
|
|
|
|
func MissingHeightsToGaps(heights []uint64) []shared.Gap {
|
|
|
|
if len(heights) == 0 {
|
|
|
|
return nil
|
2017-12-11 21:08:00 +00:00
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
validationGaps := make([]shared.Gap, 0)
|
|
|
|
start := heights[0]
|
|
|
|
lastHeight := start
|
|
|
|
for i, height := range heights[1:] {
|
|
|
|
if height != lastHeight+1 {
|
|
|
|
validationGaps = append(validationGaps, shared.Gap{
|
|
|
|
Start: start,
|
|
|
|
Stop: lastHeight,
|
|
|
|
})
|
|
|
|
start = height
|
|
|
|
}
|
|
|
|
if i+2 == len(heights) {
|
|
|
|
validationGaps = append(validationGaps, shared.Gap{
|
|
|
|
Start: start,
|
|
|
|
Stop: height,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
lastHeight = height
|
2019-10-28 10:48:31 +00:00
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
return validationGaps
|
2019-10-28 10:48:31 +00:00
|
|
|
}
|