rosetta-lbry/configuration/configuration.go

221 lines
6.2 KiB
Go
Raw Permalink Normal View History

2020-09-16 21:03:14 +02:00
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package configuration
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"time"
2020-11-17 09:20:36 +01:00
"github.com/lbryio/rosetta-lbry/lbry"
2020-09-16 21:03:14 +02:00
"github.com/btcsuite/btcd/chaincfg"
2020-12-08 06:08:02 +01:00
"github.com/coinbase/rosetta-sdk-go/storage"
"github.com/coinbase/rosetta-sdk-go/types"
2020-09-16 21:03:14 +02:00
)
// Mode is the setting that determines if
// the implementation is "online" or "offline".
type Mode string
const (
// Online is when the implementation is permitted
// to make outbound connections.
Online Mode = "ONLINE"
// Offline is when the implementation is not permitted
// to make outbound connections.
Offline Mode = "OFFLINE"
2020-11-17 09:20:36 +01:00
// Mainnet is the lbry Mainnet.
2020-09-16 21:03:14 +02:00
Mainnet string = "MAINNET"
2020-11-17 09:20:36 +01:00
// Testnet is lbry Testnet3.
2020-09-16 21:03:14 +02:00
Testnet string = "TESTNET"
2020-11-17 09:20:36 +01:00
// mainnetConfigPath is the path of the lbry
2020-09-16 21:03:14 +02:00
// configuration file for mainnet.
2020-11-17 09:20:36 +01:00
mainnetConfigPath = "/app/lbry-mainnet.conf"
2020-09-16 21:03:14 +02:00
2020-11-17 09:20:36 +01:00
// testnetConfigPath is the path of the lbry
2020-09-16 21:03:14 +02:00
// configuration file for testnet.
2020-11-17 09:20:36 +01:00
testnetConfigPath = "/app/lbry-testnet.conf"
2020-09-16 21:03:14 +02:00
// Zstandard compression dictionaries
transactionNamespace = "transaction"
testnetTransactionDictionary = "/app/testnet-transaction.zstd"
mainnetTransactionDictionary = "/app/mainnet-transaction.zstd"
2020-11-17 09:20:36 +01:00
mainnetRPCPort = 9245
testnetRPCPort = 19245
2020-09-16 21:03:14 +02:00
// min prune depth is 288:
2020-11-17 09:20:36 +01:00
// https://github.com/lbry/lbry/blob/ad2952d17a2af419a04256b10b53c7377f826a27/src/validation.h#L84
2020-09-16 21:03:14 +02:00
pruneDepth = int64(10000) //nolint
// min prune height (on mainnet):
2020-11-17 09:20:36 +01:00
// https://github.com/lbry/lbry/blob/62d137ac3b701aae36c1aa3aa93a83fd6357fde6/src/chainparams.cpp#L102
2020-09-16 21:03:14 +02:00
minPruneHeight = int64(100000) //nolint
// attempt to prune once an hour
pruneFrequency = 60 * time.Minute
// DataDirectory is the default location for all
// persistent data.
DataDirectory = "/data"
2020-11-17 09:20:36 +01:00
LBRYcrdPath = "LBRYcrd"
indexerPath = "indexer"
2020-09-16 21:03:14 +02:00
// allFilePermissions specifies anyone can do anything
// to the file.
allFilePermissions = 0777
// ModeEnv is the environment variable read
// to determine mode.
ModeEnv = "MODE"
// NetworkEnv is the environment variable
// read to determine network.
NetworkEnv = "NETWORK"
// PortEnv is the environment variable
// read to determine the port for the Rosetta
// implementation.
PortEnv = "PORT"
)
// PruningConfiguration is the configuration to
// use for pruning in the indexer.
type PruningConfiguration struct {
Frequency time.Duration
Depth int64
MinHeight int64
}
// Configuration determines how
type Configuration struct {
Mode Mode
Network *types.NetworkIdentifier
Params *chaincfg.Params
Currency *types.Currency
GenesisBlockIdentifier *types.BlockIdentifier
Port int
RPCPort int
ConfigPath string
Pruning *PruningConfiguration
IndexerPath string
2020-11-17 09:20:36 +01:00
LBRYcrdPath string
2020-09-16 21:03:14 +02:00
Compressors []*storage.CompressorEntry
}
// LoadConfiguration attempts to create a new Configuration
// using the ENVs in the environment.
func LoadConfiguration(baseDirectory string) (*Configuration, error) {
config := &Configuration{}
config.Pruning = &PruningConfiguration{
Frequency: pruneFrequency,
Depth: pruneDepth,
MinHeight: minPruneHeight,
}
modeValue := Mode(os.Getenv(ModeEnv))
switch modeValue {
case Online:
config.Mode = Online
config.IndexerPath = path.Join(baseDirectory, indexerPath)
if err := ensurePathExists(config.IndexerPath); err != nil {
return nil, fmt.Errorf("%w: unable to create indexer path", err)
}
2020-11-17 09:20:36 +01:00
config.LBRYcrdPath = path.Join(baseDirectory, LBRYcrdPath)
if err := ensurePathExists(config.LBRYcrdPath); err != nil {
return nil, fmt.Errorf("%w: unable to create LBRYcrd path", err)
2020-09-16 21:03:14 +02:00
}
case Offline:
config.Mode = Offline
case "":
return nil, errors.New("MODE must be populated")
default:
return nil, fmt.Errorf("%s is not a valid mode", modeValue)
}
networkValue := os.Getenv(NetworkEnv)
switch networkValue {
case Mainnet:
config.Network = &types.NetworkIdentifier{
2020-11-17 09:20:36 +01:00
Blockchain: lbry.Blockchain,
Network: lbry.MainnetNetwork,
2020-09-16 21:03:14 +02:00
}
2020-11-17 09:20:36 +01:00
config.GenesisBlockIdentifier = lbry.MainnetGenesisBlockIdentifier
config.Params = lbry.MainnetParams
config.Currency = lbry.MainnetCurrency
2020-09-16 21:03:14 +02:00
config.ConfigPath = mainnetConfigPath
config.RPCPort = mainnetRPCPort
config.Compressors = []*storage.CompressorEntry{
{
Namespace: transactionNamespace,
DictionaryPath: mainnetTransactionDictionary,
},
}
case Testnet:
config.Network = &types.NetworkIdentifier{
2020-11-17 09:20:36 +01:00
Blockchain: lbry.Blockchain,
Network: lbry.TestnetNetwork,
2020-09-16 21:03:14 +02:00
}
2020-11-17 09:20:36 +01:00
config.GenesisBlockIdentifier = lbry.TestnetGenesisBlockIdentifier
config.Params = lbry.TestnetParams
config.Currency = lbry.TestnetCurrency
2020-09-16 21:03:14 +02:00
config.ConfigPath = testnetConfigPath
config.RPCPort = testnetRPCPort
config.Compressors = []*storage.CompressorEntry{
{
Namespace: transactionNamespace,
DictionaryPath: testnetTransactionDictionary,
},
}
case "":
return nil, errors.New("NETWORK must be populated")
default:
return nil, fmt.Errorf("%s is not a valid network", networkValue)
}
portValue := os.Getenv(PortEnv)
if len(portValue) == 0 {
return nil, errors.New("PORT must be populated")
}
port, err := strconv.Atoi(portValue)
if err != nil || len(portValue) == 0 || port <= 0 {
return nil, fmt.Errorf("%w: unable to parse port %s", err, portValue)
}
config.Port = port
return config, nil
}
// ensurePathsExist directories along
// a path if they do not exist.
func ensurePathExists(path string) error {
if err := os.MkdirAll(path, os.FileMode(allFilePermissions)); err != nil {
return fmt.Errorf("%w: unable to create %s directory", err, path)
}
return nil
}