cmd, node: add mysql flags

This commit is contained in:
mark.lin 2018-04-10 16:05:06 +08:00
parent e3fd8fc08b
commit 15f8675f9d
6 changed files with 89 additions and 2 deletions

View file

@ -140,6 +140,17 @@ var (
utils.WhisperMaxMessageSizeFlag, utils.WhisperMaxMessageSizeFlag,
utils.WhisperMinPOWFlag, utils.WhisperMinPOWFlag,
} }
sqlFlags = []cli.Flag{
utils.MySQLFlag,
utils.MySQLAddressFlag,
utils.MySQLPortFlag,
utils.MySQLPasswordFlag,
utils.MySQLUserFlag,
utils.MySQLDatabaseFlag,
utils.MySQLProtocolFlag,
utils.MySQLNativeFlag,
}
) )
func init() { func init() {
@ -182,6 +193,7 @@ func init() {
app.Flags = append(app.Flags, consoleFlags...) app.Flags = append(app.Flags, consoleFlags...)
app.Flags = append(app.Flags, debug.Flags...) app.Flags = append(app.Flags, debug.Flags...)
app.Flags = append(app.Flags, whisperFlags...) app.Flags = append(app.Flags, whisperFlags...)
app.Flags = append(app.Flags, sqlFlags...)
app.Before = func(ctx *cli.Context) error { app.Before = func(ctx *cli.Context) error {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View file

@ -215,6 +215,10 @@ var AppHelpFlagGroups = []flagGroup{
Name: "WHISPER (EXPERIMENTAL)", Name: "WHISPER (EXPERIMENTAL)",
Flags: whisperFlags, Flags: whisperFlags,
}, },
{
Name: "MySQL",
Flags: sqlFlags,
},
{ {
Name: "DEPRECATED", Name: "DEPRECATED",
Flags: []cli.Flag{ Flags: []cli.Flag{

View file

@ -44,6 +44,7 @@ import (
"github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/eth/gasprice" "github.com/ethereum/go-ethereum/eth/gasprice"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/mysql"
"github.com/ethereum/go-ethereum/ethstats" "github.com/ethereum/go-ethereum/ethstats"
"github.com/ethereum/go-ethereum/les" "github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
@ -532,6 +533,46 @@ var (
Usage: "Minimum POW accepted", Usage: "Minimum POW accepted",
Value: whisper.DefaultMinimumPoW, 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 // 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. // SetNodeConfig applies node-related command line flags to the config.
func SetNodeConfig(ctx *cli.Context, cfg *node.Config) { func SetNodeConfig(ctx *cli.Context, cfg *node.Config) {
SetP2PConfig(ctx, &cfg.P2P) SetP2PConfig(ctx, &cfg.P2P)
setMySQL(ctx, cfg)
setIPC(ctx, cfg) setIPC(ctx, cfg)
setHTTP(ctx, cfg) setHTTP(ctx, cfg)
setWS(ctx, cfg) setWS(ctx, cfg)

View file

@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/accounts/usbwallet" "github.com/ethereum/go-ethereum/accounts/usbwallet"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb/mysql"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
@ -145,6 +146,9 @@ type Config struct {
// private APIs to untrusted users is a major security risk. // private APIs to untrusted users is a major security risk.
WSExposeAll bool `toml:",omitempty"` WSExposeAll bool `toml:",omitempty"`
// MySQL setting
MySQL *mysql.Config `toml:",omitempty"`
// Logger is a custom logger to use with the p2p.Server. // Logger is a custom logger to use with the p2p.Server.
Logger log.Logger `toml:",omitempty"` Logger log.Logger `toml:",omitempty"`
} }

View file

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/mysql"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/log" "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 // previous can be found) from within the node's instance directory. If the node is
// ephemeral, a memory database is returned. // ephemeral, a memory database is returned.
func (n *Node) OpenDatabase(name string, cache, handles int) (ethdb.Database, error) { 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 == "" { if n.config.DataDir == "" {
return ethdb.NewMemDatabase() return ethdb.NewMemDatabase()
} }

View file

@ -21,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethdb/mysql"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc" "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 // 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 // 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. // 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 == "" { if ctx.config.DataDir == "" {
return ethdb.NewMemDatabase() 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 { if err != nil {
return nil, err return nil, err
} }