From 15f8675f9ddb1ecb9cb206070a07f0551aef851d Mon Sep 17 00:00:00 2001 From: "mark.lin" Date: Tue, 10 Apr 2018 16:05:06 +0800 Subject: [PATCH] cmd, node: add mysql flags --- cmd/geth/main.go | 12 ++++++++++ cmd/geth/usage.go | 4 ++++ cmd/utils/flags.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++ node/config.go | 4 ++++ node/node.go | 4 ++++ node/service.go | 9 +++++-- 6 files changed, 89 insertions(+), 2 deletions(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index d94154245f..7e76e286ac 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -140,6 +140,17 @@ var ( utils.WhisperMaxMessageSizeFlag, utils.WhisperMinPOWFlag, } + + sqlFlags = []cli.Flag{ + utils.MySQLFlag, + utils.MySQLAddressFlag, + utils.MySQLPortFlag, + utils.MySQLPasswordFlag, + utils.MySQLUserFlag, + utils.MySQLDatabaseFlag, + utils.MySQLProtocolFlag, + utils.MySQLNativeFlag, + } ) func init() { @@ -182,6 +193,7 @@ func init() { app.Flags = append(app.Flags, consoleFlags...) app.Flags = append(app.Flags, debug.Flags...) app.Flags = append(app.Flags, whisperFlags...) + app.Flags = append(app.Flags, sqlFlags...) app.Before = func(ctx *cli.Context) error { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index a1558c2330..95003902ec 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -215,6 +215,10 @@ var AppHelpFlagGroups = []flagGroup{ Name: "WHISPER (EXPERIMENTAL)", Flags: whisperFlags, }, + { + Name: "MySQL", + Flags: sqlFlags, + }, { Name: "DEPRECATED", Flags: []cli.Flag{ diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 4f3d81f5d2..c5573cad33 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -44,6 +44,7 @@ import ( "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/ethdb/mysql" "github.com/ethereum/go-ethereum/ethstats" "github.com/ethereum/go-ethereum/les" "github.com/ethereum/go-ethereum/log" @@ -532,6 +533,46 @@ var ( Usage: "Minimum POW accepted", Value: whisper.DefaultMinimumPoW, } + + // MySQL settings + MySQLFlag = cli.BoolFlag{ + Name: "mysql", + Usage: "Enables external MySQL as our geth database", + } + MySQLAddressFlag = cli.StringFlag{ + Name: "mysql.address", + Usage: "MySQL address", + Value: mysql.DefaultConfig.Address, + } + MySQLPortFlag = cli.StringFlag{ + Name: "mysql.port", + Usage: "MySQL port", + Value: mysql.DefaultConfig.Port, + } + MySQLPasswordFlag = cli.StringFlag{ + Name: "mysql.password", + Usage: "MySQL password", + Value: mysql.DefaultConfig.Password, + } + MySQLUserFlag = cli.StringFlag{ + Name: "mysql.user", + Usage: "MySQL user name", + Value: mysql.DefaultConfig.User, + } + MySQLDatabaseFlag = cli.StringFlag{ + Name: "mysql.database", + Usage: "MySQL database name", + Value: mysql.DefaultConfig.Database, + } + MySQLProtocolFlag = cli.StringFlag{ + Name: "mysql.protocol", + Usage: "MySQL protocol", + Value: mysql.DefaultConfig.Protocol, + } + MySQLNativeFlag = cli.BoolTFlag{ + Name: "mysql.native", + Usage: "Enable allow native passwords in MySQL", + } ) // MakeDataDir retrieves the currently requested data directory, terminating @@ -868,9 +909,26 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) { } } +// SetMySQL applies mysql-related command line flags to the config. +func setMySQL(ctx *cli.Context, cfg *node.Config) { + if !ctx.GlobalBool(MySQLFlag.Name) { + return + } + cfg.MySQL = &mysql.Config{ + Protocol: ctx.GlobalString(MySQLProtocolFlag.Name), + Address: ctx.GlobalString(MySQLAddressFlag.Name), + Port: ctx.GlobalString(MySQLPortFlag.Name), + User: ctx.GlobalString(MySQLUserFlag.Name), + Password: ctx.GlobalString(MySQLPasswordFlag.Name), + Database: ctx.GlobalString(MySQLDatabaseFlag.Name), + AllowNativePasswords: ctx.GlobalBoolT(MySQLNativeFlag.Name), + } +} + // SetNodeConfig applies node-related command line flags to the config. func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { SetP2PConfig(ctx, &cfg.P2P) + setMySQL(ctx, cfg) setIPC(ctx, cfg) setHTTP(ctx, cfg) setWS(ctx, cfg) diff --git a/node/config.go b/node/config.go index dda24583ee..270e6e2125 100644 --- a/node/config.go +++ b/node/config.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/usbwallet" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethdb/mysql" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" @@ -145,6 +146,9 @@ type Config struct { // private APIs to untrusted users is a major security risk. WSExposeAll bool `toml:",omitempty"` + // MySQL setting + MySQL *mysql.Config `toml:",omitempty"` + // Logger is a custom logger to use with the p2p.Server. Logger log.Logger `toml:",omitempty"` } diff --git a/node/node.go b/node/node.go index b02aecfad1..0901faa895 100644 --- a/node/node.go +++ b/node/node.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/ethdb/mysql" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/log" @@ -642,6 +643,9 @@ func (n *Node) EventMux() *event.TypeMux { // previous can be found) from within the node's instance directory. If the node is // ephemeral, a memory database is returned. func (n *Node) OpenDatabase(name string, cache, handles int) (ethdb.Database, error) { + if n.config.MySQL != nil { + return mysql.NewDatabase(name, n.config.MySQL) + } if n.config.DataDir == "" { return ethdb.NewMemDatabase() } diff --git a/node/service.go b/node/service.go index 55062a5004..7d63367203 100644 --- a/node/service.go +++ b/node/service.go @@ -21,6 +21,7 @@ import ( "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/ethdb/mysql" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" @@ -39,11 +40,15 @@ type ServiceContext struct { // OpenDatabase opens an existing database with the given name (or creates one // if no previous can be found) from within the node's data directory. If the // node is an ephemeral one, a memory database is returned. -func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (ethdb.Database, error) { +func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (db ethdb.Database, err error) { + if ctx.config.MySQL != nil { + db, err = mysql.NewDatabase(name, ctx.config.MySQL) + return + } if ctx.config.DataDir == "" { return ethdb.NewMemDatabase() } - db, err := ethdb.NewLDBDatabase(ctx.config.resolvePath(name), cache, handles) + db, err = ethdb.NewLDBDatabase(ctx.config.resolvePath(name), cache, handles) if err != nil { return nil, err }