2020-03-12 18:52:23 +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 resync
|
|
|
|
|
|
|
|
import (
|
2020-03-17 12:51:00 +00:00
|
|
|
"fmt"
|
2020-04-19 23:26:23 +00:00
|
|
|
"time"
|
2020-03-17 12:51:00 +00:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
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"
|
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/utils"
|
2020-03-12 18:52:23 +00:00
|
|
|
)
|
|
|
|
|
2020-03-20 18:15:50 +00:00
|
|
|
// Env variables
|
|
|
|
const (
|
2020-04-03 19:29:28 +00:00
|
|
|
RESYNC_CHAIN = "RESYNC_CHAIN"
|
|
|
|
RESYNC_START = "RESYNC_START"
|
|
|
|
RESYNC_STOP = "RESYNC_STOP"
|
|
|
|
RESYNC_BATCH_SIZE = "RESYNC_BATCH_SIZE"
|
|
|
|
RESYNC_BATCH_NUMBER = "RESYNC_BATCH_NUMBER"
|
|
|
|
RESYNC_CLEAR_OLD_CACHE = "RESYNC_CLEAR_OLD_CACHE"
|
|
|
|
RESYNC_TYPE = "RESYNC_TYPE"
|
|
|
|
RESYNC_RESET_VALIDATION = "RESYNC_RESET_VALIDATION"
|
2020-03-20 18:15:50 +00:00
|
|
|
)
|
|
|
|
|
2020-03-12 18:52:23 +00:00
|
|
|
// Config holds the parameters needed to perform a resync
|
|
|
|
type Config struct {
|
2020-04-03 19:29:28 +00:00
|
|
|
Chain shared.ChainType // The type of resync to perform
|
|
|
|
ResyncType shared.DataType // The type of data to resync
|
|
|
|
ClearOldCache bool // Resync will first clear all the data within the range
|
|
|
|
ResetValidation bool // If true, resync will reset the validation level to 0 for the given range
|
2020-03-12 18:52:23 +00:00
|
|
|
|
|
|
|
// DB info
|
|
|
|
DB *postgres.DB
|
|
|
|
DBConfig config.Database
|
|
|
|
IPFSPath string
|
2020-04-23 20:56:37 +00:00
|
|
|
IPFSMode shared.IPFSMode
|
2020-03-12 18:52:23 +00:00
|
|
|
|
2020-04-19 23:26:23 +00:00
|
|
|
HTTPClient interface{} // Note this client is expected to support the retrieval of the specified data type(s)
|
2020-06-30 00:16:52 +00:00
|
|
|
NodeInfo node.Node // Info for the associated node
|
2020-04-19 23:26:23 +00:00
|
|
|
Ranges [][2]uint64 // The block height ranges to resync
|
|
|
|
BatchSize uint64 // BatchSize for the resync http calls (client has to support batch sizing)
|
|
|
|
Timeout time.Duration // HTTP connection timeout in seconds
|
2020-03-18 00:42:53 +00:00
|
|
|
BatchNumber uint64
|
2020-03-12 18:52:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 00:16:52 +00:00
|
|
|
// NewConfig fills and returns a resync config from toml parameters
|
|
|
|
func NewConfig() (*Config, error) {
|
2020-03-17 12:51:00 +00:00
|
|
|
c := new(Config)
|
|
|
|
var err error
|
2020-03-20 18:15:50 +00:00
|
|
|
|
|
|
|
viper.BindEnv("resync.start", RESYNC_START)
|
|
|
|
viper.BindEnv("resync.stop", RESYNC_STOP)
|
|
|
|
viper.BindEnv("resync.clearOldCache", RESYNC_CLEAR_OLD_CACHE)
|
|
|
|
viper.BindEnv("resync.type", RESYNC_TYPE)
|
|
|
|
viper.BindEnv("resync.chain", RESYNC_CHAIN)
|
|
|
|
viper.BindEnv("ethereum.httpPath", shared.ETH_HTTP_PATH)
|
|
|
|
viper.BindEnv("bitcoin.httpPath", shared.BTC_HTTP_PATH)
|
|
|
|
viper.BindEnv("resync.batchSize", RESYNC_BATCH_SIZE)
|
|
|
|
viper.BindEnv("resync.batchNumber", RESYNC_BATCH_NUMBER)
|
2020-04-03 19:29:28 +00:00
|
|
|
viper.BindEnv("resync.resetValidation", RESYNC_RESET_VALIDATION)
|
2020-04-19 23:26:23 +00:00
|
|
|
viper.BindEnv("resync.timeout", shared.HTTP_TIMEOUT)
|
|
|
|
|
|
|
|
timeout := viper.GetInt("resync.timeout")
|
2020-05-02 05:02:05 +00:00
|
|
|
if timeout < 5 {
|
|
|
|
timeout = 5
|
2020-04-19 23:26:23 +00:00
|
|
|
}
|
|
|
|
c.Timeout = time.Second * time.Duration(timeout)
|
2020-03-20 18:15:50 +00:00
|
|
|
|
2020-03-18 00:42:53 +00:00
|
|
|
start := uint64(viper.GetInt64("resync.start"))
|
|
|
|
stop := uint64(viper.GetInt64("resync.stop"))
|
|
|
|
c.Ranges = [][2]uint64{{start, stop}}
|
2020-03-17 12:51:00 +00:00
|
|
|
c.ClearOldCache = viper.GetBool("resync.clearOldCache")
|
2020-04-03 19:29:28 +00:00
|
|
|
c.ResetValidation = viper.GetBool("resync.resetValidation")
|
2020-03-20 18:15:50 +00:00
|
|
|
|
2020-04-23 20:56:37 +00:00
|
|
|
c.IPFSMode, err = shared.GetIPFSMode()
|
2020-03-20 18:15:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-23 20:56:37 +00:00
|
|
|
if c.IPFSMode == shared.LocalInterface || c.IPFSMode == shared.RemoteClient {
|
|
|
|
c.IPFSPath, err = shared.GetIPFSPath()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-03-17 12:51:00 +00:00
|
|
|
resyncType := viper.GetString("resync.type")
|
2020-05-02 05:02:05 +00:00
|
|
|
c.ResyncType, err = shared.GenerateDataTypeFromString(resyncType)
|
2020-03-17 12:51:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chain := viper.GetString("resync.chain")
|
|
|
|
c.Chain, err = shared.NewChainType(chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-02 05:02:05 +00:00
|
|
|
if ok, err := shared.SupportedDataType(c.ResyncType, c.Chain); !ok {
|
2020-03-17 12:51:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("chain type %s does not support data type %s", c.Chain.String(), c.ResyncType.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.Chain {
|
|
|
|
case shared.Ethereum:
|
2020-03-20 18:15:50 +00:00
|
|
|
ethHTTP := viper.GetString("ethereum.httpPath")
|
|
|
|
c.NodeInfo, c.HTTPClient, err = shared.GetEthNodeAndClient(fmt.Sprintf("http://%s", ethHTTP))
|
2020-03-17 12:51:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case shared.Bitcoin:
|
2020-03-20 18:15:50 +00:00
|
|
|
btcHTTP := viper.GetString("bitcoin.httpPath")
|
|
|
|
c.NodeInfo, c.HTTPClient = shared.GetBtcNodeAndClient(btcHTTP)
|
2020-03-17 12:51:00 +00:00
|
|
|
}
|
2020-03-20 18:15:50 +00:00
|
|
|
|
|
|
|
c.DBConfig.Init()
|
2020-03-17 12:51:00 +00:00
|
|
|
db := utils.LoadPostgres(c.DBConfig, c.NodeInfo)
|
|
|
|
c.DB = &db
|
2020-03-20 18:15:50 +00:00
|
|
|
|
2020-03-18 00:42:53 +00:00
|
|
|
c.BatchSize = uint64(viper.GetInt64("resync.batchSize"))
|
|
|
|
c.BatchNumber = uint64(viper.GetInt64("resync.batchNumber"))
|
2020-03-17 12:51:00 +00:00
|
|
|
return c, nil
|
|
|
|
}
|