2020-10-27 15:53:49 +00:00
package cli
import (
"fmt"
"strings"
"github.com/c2h5oh/datasize"
"github.com/ledgerwatch/turbo-geth/cmd/utils"
"github.com/ledgerwatch/turbo-geth/common/etl"
2021-03-23 09:00:07 +00:00
"github.com/ledgerwatch/turbo-geth/eth/ethconfig"
2020-10-27 15:53:49 +00:00
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
"github.com/ledgerwatch/turbo-geth/node"
2020-11-13 16:16:47 +00:00
"github.com/ledgerwatch/turbo-geth/turbo/snapshotsync"
2021-03-23 09:00:07 +00:00
"github.com/spf13/pflag"
2020-10-27 15:53:49 +00:00
"github.com/urfave/cli"
)
var (
DatabaseFlag = cli . StringFlag {
Name : "database" ,
2020-10-28 03:18:10 +00:00
Usage : "Which database software to use? Currently supported values: lmdb|mdbx" ,
2020-10-27 15:53:49 +00:00
Value : "lmdb" ,
}
2020-12-08 09:44:29 +00:00
CacheSizeFlag = cli . StringFlag {
Name : "cacheSize" ,
Usage : "Cache size for the execution stage" ,
Value : "0" ,
}
2020-10-27 15:53:49 +00:00
BatchSizeFlag = cli . StringFlag {
Name : "batchSize" ,
Usage : "Batch size for the execution stage" ,
Value : "512M" ,
}
EtlBufferSizeFlag = cli . StringFlag {
Name : "etl.bufferSize" ,
Usage : "Buffer size for ETL operations." ,
Value : etl . BufferOptimalSize . String ( ) ,
}
PrivateApiAddr = cli . StringFlag {
Name : "private.api.addr" ,
Usage : "private api network address, for example: 127.0.0.1:9090, empty string means not to start the listener. do not expose to public network. serves remote database interface" ,
Value : "" ,
}
2021-03-23 07:28:04 +00:00
PrivateApiRateLimit = cli . IntFlag {
Name : "private.api.ratelimit" ,
Usage : "Amount of requests server handle simultaneously - requests over this limit will wait. Increase it - if clients see 'request timeout' while server load is low - it means your 'hot data' is small or have much RAM. " ,
Value : 500 ,
}
2020-10-27 15:53:49 +00:00
StorageModeFlag = cli . StringFlag {
Name : "storage-mode" ,
Usage : ` Configures the storage mode of the app :
* h - write history to the DB
* r - write receipts to the DB
* t - write tx lookup index to the DB ` ,
Value : ethdb . DefaultStorageMode . ToString ( ) ,
}
SnapshotModeFlag = cli . StringFlag {
2020-11-13 16:16:47 +00:00
Name : "snapshot.mode" ,
2021-02-09 15:48:55 +00:00
Usage : ` Configures the snapshot mode of the app :
2020-10-27 15:53:49 +00:00
* h - download headers snapshot
* b - download bodies snapshot
* s - download state snapshot
* r - download receipts snapshot
` ,
2020-11-13 16:16:47 +00:00
Value : snapshotsync . DefaultSnapshotMode . ToString ( ) ,
2020-10-27 15:53:49 +00:00
}
SeedSnapshotsFlag = cli . BoolTFlag {
2020-11-13 16:16:47 +00:00
Name : "snapshot.seed" ,
Usage : ` Seed snapshot seeding(default: true) ` ,
}
ExternalSnapshotDownloaderAddrFlag = cli . StringFlag {
Name : "snapshot.downloader.addr" ,
Usage : ` enable external snapshot downloader ` ,
2020-10-27 15:53:49 +00:00
}
// LMDB flags
LMDBMapSizeFlag = cli . StringFlag {
Name : "lmdb.mapSize" ,
Usage : "Sets Memory map size. Lower it if you have issues with opening the DB" ,
Value : ethdb . LMDBDefaultMapSize . String ( ) ,
}
LMDBMaxFreelistReuseFlag = cli . UintFlag {
Name : "lmdb.maxFreelistReuse" ,
Usage : "Find a big enough contiguous page range for large values in freelist is hard just allocate new pages and even don't try to search if value is bigger than this limit. Measured in pages." ,
Value : ethdb . LMDBDefaultMaxFreelistReuse ,
}
// mTLS flags
TLSFlag = cli . BoolFlag {
Name : "tls" ,
Usage : "Enable TLS handshake" ,
}
TLSCertFlag = cli . StringFlag {
Name : "tls.cert" ,
Usage : "Specify certificate" ,
Value : "" ,
}
TLSKeyFlag = cli . StringFlag {
Name : "tls.key" ,
Usage : "Specify key file" ,
Value : "" ,
}
TLSCACertFlag = cli . StringFlag {
Name : "tls.cacert" ,
Usage : "Specify certificate authority" ,
Value : "" ,
}
2020-11-28 15:08:02 +00:00
SilkwormFlag = cli . StringFlag {
Name : "silkworm" ,
Usage : "File path of libsilkworm_tg_api dynamic library (default = do not use Silkworm)" ,
Value : "" ,
}
2020-10-27 15:53:49 +00:00
)
2021-03-23 09:00:07 +00:00
func ApplyFlagsForEthConfig ( ctx * cli . Context , cfg * ethconfig . Config ) {
2020-10-27 15:53:49 +00:00
mode , err := ethdb . StorageModeFromString ( ctx . GlobalString ( StorageModeFlag . Name ) )
if err != nil {
utils . Fatalf ( fmt . Sprintf ( "error while parsing mode: %v" , err ) )
}
cfg . StorageMode = mode
2020-11-13 16:16:47 +00:00
snMode , err := snapshotsync . SnapshotModeFromString ( ctx . GlobalString ( SnapshotModeFlag . Name ) )
2020-10-27 15:53:49 +00:00
if err != nil {
utils . Fatalf ( fmt . Sprintf ( "error while parsing mode: %v" , err ) )
}
cfg . SnapshotMode = snMode
cfg . SnapshotSeeding = ctx . GlobalBool ( SeedSnapshotsFlag . Name )
2020-12-08 09:44:29 +00:00
if ctx . GlobalString ( CacheSizeFlag . Name ) != "" {
err := cfg . CacheSize . UnmarshalText ( [ ] byte ( ctx . GlobalString ( CacheSizeFlag . Name ) ) )
if err != nil {
utils . Fatalf ( "Invalid cacheSize provided: %v" , err )
}
}
2020-10-27 15:53:49 +00:00
if ctx . GlobalString ( BatchSizeFlag . Name ) != "" {
err := cfg . BatchSize . UnmarshalText ( [ ] byte ( ctx . GlobalString ( BatchSizeFlag . Name ) ) )
if err != nil {
utils . Fatalf ( "Invalid batchSize provided: %v" , err )
}
}
2020-12-08 09:44:29 +00:00
if cfg . CacheSize != 0 && cfg . BatchSize >= cfg . CacheSize {
utils . Fatalf ( "batchSize %d >= cacheSize %d" , cfg . BatchSize , cfg . CacheSize )
}
2020-10-27 15:53:49 +00:00
if ctx . GlobalString ( EtlBufferSizeFlag . Name ) != "" {
sizeVal := datasize . ByteSize ( 0 )
size := & sizeVal
err := size . UnmarshalText ( [ ] byte ( ctx . GlobalString ( EtlBufferSizeFlag . Name ) ) )
if err != nil {
utils . Fatalf ( "Invalid batchSize provided: %v" , err )
}
etl . BufferOptimalSize = * size
}
2020-11-13 16:16:47 +00:00
cfg . ExternalSnapshotDownloaderAddr = ctx . GlobalString ( ExternalSnapshotDownloaderAddrFlag . Name )
2020-10-27 15:53:49 +00:00
}
2021-03-23 09:00:07 +00:00
func ApplyFlagsForEthConfigCobra ( f * pflag . FlagSet , cfg * ethconfig . Config ) {
if v := f . String ( StorageModeFlag . Name , StorageModeFlag . Value , StorageModeFlag . Usage ) ; v != nil {
mode , err := ethdb . StorageModeFromString ( * v )
if err != nil {
utils . Fatalf ( fmt . Sprintf ( "error while parsing mode: %v" , err ) )
}
cfg . StorageMode = mode
}
if v := f . String ( SnapshotModeFlag . Name , SnapshotModeFlag . Value , SnapshotModeFlag . Usage ) ; v != nil {
snMode , err := snapshotsync . SnapshotModeFromString ( * v )
if err != nil {
utils . Fatalf ( fmt . Sprintf ( "error while parsing mode: %v" , err ) )
}
cfg . SnapshotMode = snMode
}
if v := f . Bool ( SeedSnapshotsFlag . Name , false , SeedSnapshotsFlag . Usage ) ; v != nil {
cfg . SnapshotSeeding = * v
}
if v := f . String ( CacheSizeFlag . Name , CacheSizeFlag . Value , CacheSizeFlag . Usage ) ; v != nil {
err := cfg . CacheSize . UnmarshalText ( [ ] byte ( * v ) )
if err != nil {
utils . Fatalf ( "Invalid cacheSize provided: %v" , err )
}
}
if v := f . String ( BatchSizeFlag . Name , BatchSizeFlag . Value , BatchSizeFlag . Usage ) ; v != nil {
err := cfg . BatchSize . UnmarshalText ( [ ] byte ( * v ) )
if err != nil {
utils . Fatalf ( "Invalid batchSize provided: %v" , err )
}
}
if cfg . CacheSize != 0 && cfg . BatchSize >= cfg . CacheSize {
utils . Fatalf ( "batchSize %d >= cacheSize %d" , cfg . BatchSize , cfg . CacheSize )
}
if v := f . String ( EtlBufferSizeFlag . Name , EtlBufferSizeFlag . Value , EtlBufferSizeFlag . Usage ) ; v != nil {
sizeVal := datasize . ByteSize ( 0 )
size := & sizeVal
err := size . UnmarshalText ( [ ] byte ( * v ) )
if err != nil {
utils . Fatalf ( "Invalid batchSize provided: %v" , err )
}
etl . BufferOptimalSize = * size
}
if v := f . String ( ExternalSnapshotDownloaderAddrFlag . Name , ExternalSnapshotDownloaderAddrFlag . Value , ExternalSnapshotDownloaderAddrFlag . Usage ) ; v != nil {
cfg . ExternalSnapshotDownloaderAddr = * v
}
}
2020-10-27 15:53:49 +00:00
func ApplyFlagsForNodeConfig ( ctx * cli . Context , cfg * node . Config ) {
setPrivateApi ( ctx , cfg )
databaseFlag := ctx . GlobalString ( DatabaseFlag . Name )
2020-10-28 03:18:10 +00:00
cfg . MDBX = strings . EqualFold ( databaseFlag , "mdbx" ) //case insensitive
2020-10-27 15:53:49 +00:00
cfg . LMDB = strings . EqualFold ( databaseFlag , "lmdb" ) //case insensitive
if cfg . LMDB && ctx . GlobalString ( LMDBMapSizeFlag . Name ) != "" {
err := cfg . LMDBMapSize . UnmarshalText ( [ ] byte ( ctx . GlobalString ( LMDBMapSizeFlag . Name ) ) )
if err != nil {
log . Error ( "Invalid LMDB map size provided. Will use defaults" ,
"lmdb.mapSize" , ethdb . LMDBDefaultMapSize . HumanReadable ( ) ,
"err" , err ,
)
} else {
if cfg . LMDBMapSize < 1 * datasize . GB {
log . Error ( "Invalid LMDB map size provided. Will use defaults" ,
"lmdb.mapSize" , ethdb . LMDBDefaultMapSize . HumanReadable ( ) ,
"err" , "the value should be at least 1 GB" ,
)
}
}
}
if cfg . LMDB {
cfg . LMDBMaxFreelistReuse = ctx . GlobalUint ( LMDBMaxFreelistReuseFlag . Name )
if cfg . LMDBMaxFreelistReuse < 16 {
log . Error ( "Invalid LMDB MaxFreelistReuse provided. Will use defaults" ,
"lmdb.maxFreelistReuse" , ethdb . LMDBDefaultMaxFreelistReuse ,
"err" , "the value should be at least 16" ,
)
}
}
}
// setPrivateApi populates configuration fields related to the remote
// read-only interface to the databae
func setPrivateApi ( ctx * cli . Context , cfg * node . Config ) {
cfg . PrivateApiAddr = ctx . GlobalString ( PrivateApiAddr . Name )
2021-03-23 07:28:04 +00:00
cfg . PrivateApiRateLimit = uint32 ( ctx . GlobalUint64 ( PrivateApiRateLimit . Name ) )
maxRateLimit := uint32 ( ethdb . ReadersLimit - 16 )
if cfg . PrivateApiRateLimit > maxRateLimit {
log . Warn ( "private.api.ratelimit is too big" , "force" , maxRateLimit )
cfg . PrivateApiRateLimit = maxRateLimit
}
2020-10-27 15:53:49 +00:00
if ctx . GlobalBool ( TLSFlag . Name ) {
certFile := ctx . GlobalString ( TLSCertFlag . Name )
keyFile := ctx . GlobalString ( TLSKeyFlag . Name )
if certFile == "" {
log . Warn ( "Could not establish TLS grpc: missing certificate" )
return
} else if keyFile == "" {
log . Warn ( "Could not establish TLS grpc: missing key file" )
return
}
cfg . TLSConnection = true
cfg . TLSCertFile = certFile
cfg . TLSKeyFile = keyFile
cfg . TLSCACert = ctx . GlobalString ( TLSCACertFlag . Name )
}
}