Skip to content

Commit

Permalink
Merge pull request ethereum#102 from nguyenbatam/move_argument_from_f…
Browse files Browse the repository at this point in the history
…lags_to_toml_file

Move argument from flags to config file
  • Loading branch information
ngtuna authored Aug 28, 2018
2 parents 80c7b4c + c363ad5 commit 46e8e17
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 31 deletions.
14 changes: 7 additions & 7 deletions cmd/tomo/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func initGenesis(ctx *cli.Context) error {
utils.Fatalf("invalid genesis file: %v", err)
}
// Open an initialise both full and light databases
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
for _, name := range []string{"chaindata", "lightchaindata"} {
chaindb, err := stack.OpenDatabase(name, 0, 0)
if err != nil {
Expand All @@ -209,7 +209,7 @@ func importChain(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
chain, chainDb := utils.MakeChain(ctx, stack)
defer chainDb.Close()

Expand Down Expand Up @@ -303,7 +303,7 @@ func exportChain(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
chain, _ := utils.MakeChain(ctx, stack)
start := time.Now()

Expand Down Expand Up @@ -336,7 +336,7 @@ func importPreimages(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
diskdb := utils.MakeChainDatabase(ctx, stack).(*ethdb.LDBDatabase)

start := time.Now()
Expand All @@ -352,7 +352,7 @@ func exportPreimages(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
utils.Fatalf("This command requires an argument.")
}
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
diskdb := utils.MakeChainDatabase(ctx, stack).(*ethdb.LDBDatabase)

start := time.Now()
Expand All @@ -369,7 +369,7 @@ func copyDb(ctx *cli.Context) error {
utils.Fatalf("Source chaindata directory path argument missing")
}
// Initialize a new chain for the running node to sync into
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
chain, chainDb := utils.MakeChain(ctx, stack)

syncmode := *utils.GlobalTextMarshaler(ctx, utils.SyncModeFlag.Name).(*downloader.SyncMode)
Expand Down Expand Up @@ -441,7 +441,7 @@ func removeDB(ctx *cli.Context) error {
}

func dump(ctx *cli.Context) error {
stack := makeFullNode(ctx)
stack, _ := makeFullNode(ctx)
chain, chainDb := utils.MakeChain(ctx, stack)
for _, arg := range ctx.Args() {
var block *types.Block
Expand Down
62 changes: 52 additions & 10 deletions cmd/tomo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"reflect"
"unicode"

cli "gopkg.in/urfave/cli.v1"
"gopkg.in/urfave/cli.v1"

"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/dashboard"
Expand All @@ -34,6 +34,7 @@ import (
"github.com/ethereum/go-ethereum/params"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"github.com/naoina/toml"
"strings"
)

var (
Expand Down Expand Up @@ -74,12 +75,25 @@ type ethstatsConfig struct {
URL string `toml:",omitempty"`
}

type account struct {
Unlocks []string
Passwords []string
}

type Bootnodes struct {
Mainnet []string
Testnet []string
}

type tomoConfig struct {
Eth eth.Config
Shh whisper.Config
Node node.Config
Ethstats ethstatsConfig
Dashboard dashboard.Config
Eth eth.Config
Shh whisper.Config
Node node.Config
Ethstats ethstatsConfig
Dashboard dashboard.Config
Account account
StakeEnable bool
Bootnodes Bootnodes
}

func loadConfig(file string, cfg *tomoConfig) error {
Expand All @@ -88,7 +102,6 @@ func loadConfig(file string, cfg *tomoConfig) error {
return err
}
defer f.Close()

err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
// Add file name to errors that have a line number.
if _, ok := err.(*toml.LineError); ok {
Expand All @@ -115,14 +128,30 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, tomoConfig) {
Node: defaultNodeConfig(),
Dashboard: dashboard.DefaultConfig,
}

// Load config file.
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
if err := loadConfig(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}

// read passwords from enviroment
passwords := []string{}
for _, env := range cfg.Account.Passwords {
if trimmed := strings.TrimSpace(env); trimmed != "" {
value := os.Getenv(trimmed)
for _, info := range strings.Split(value, ",") {
if trimmed2 := strings.TrimSpace(info); trimmed2 != "" {
passwords = append(passwords, trimmed2)
}
}
}
}
cfg.Account.Passwords = passwords
//Apply Bootnodes
applyValues(cfg.Bootnodes.Mainnet, &params.MainnetBootnodes)
applyValues(cfg.Bootnodes.Testnet, &params.TestnetBootnodes)

// Apply flags.
utils.SetNodeConfig(ctx, &cfg.Node)
stack, err := node.New(&cfg.Node)
Expand All @@ -140,6 +169,19 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, tomoConfig) {
return stack, cfg
}

func applyValues(values []string, params *[]string) {
data := []string{}
for _, value := range values {
if trimmed := strings.TrimSpace(value); trimmed != "" {
data = append(data, trimmed)
}
}
if len(data) > 0 {
*params = data
}

}

// enableWhisper returns true in case one of the whisper flags is set.
func enableWhisper(ctx *cli.Context) bool {
for _, flag := range whisperFlags {
Expand All @@ -150,7 +192,7 @@ func enableWhisper(ctx *cli.Context) bool {
return false
}

func makeFullNode(ctx *cli.Context) *node.Node {
func makeFullNode(ctx *cli.Context) (*node.Node, tomoConfig) {
stack, cfg := makeConfigNode(ctx)

utils.RegisterEthService(stack, &cfg.Eth)
Expand All @@ -175,7 +217,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
if cfg.Ethstats.URL != "" {
utils.RegisterEthStatsService(stack, cfg.Ethstats.URL)
}
return stack
return stack, cfg
}

// dumpConfig is the dumpconfig command.
Expand Down
9 changes: 5 additions & 4 deletions cmd/tomo/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Cons
// same time.
func localConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
node := makeFullNode(ctx)
startNode(ctx, node)
node, cfg := makeFullNode(ctx)
startNode(ctx, node, cfg)
defer node.Stop()

// Attach to the newly started node and start the JavaScript console
Expand Down Expand Up @@ -130,6 +130,7 @@ func remoteConsole(ctx *cli.Context) error {
}
endpoint = fmt.Sprintf("%s/tomo.ipc", path)
}

client, err := dialRPC(endpoint)
if err != nil {
utils.Fatalf("Unable to attach to remote tomo: %v", err)
Expand Down Expand Up @@ -178,8 +179,8 @@ func dialRPC(endpoint string) (*rpc.Client, error) {
// everything down.
func ephemeralConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
node := makeFullNode(ctx)
startNode(ctx, node)
node, cfg := makeFullNode(ctx)
startNode(ctx, node, cfg)
defer node.Stop()

// Attach to the newly started node and start the JavaScript console
Expand Down
26 changes: 16 additions & 10 deletions cmd/tomo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,33 @@ func main() {
// It creates a default node based on the command line arguments and runs it in
// blocking mode, waiting for it to be shut down.
func tomo(ctx *cli.Context) error {
node := makeFullNode(ctx)
startNode(ctx, node)
node, cfg := makeFullNode(ctx)
startNode(ctx, node, cfg)
node.Wait()
return nil
}

// startNode boots up the system node and all registered protocols, after which
// it unlocks any requested accounts, and starts the RPC/IPC interfaces and the
// miner.
func startNode(ctx *cli.Context, stack *node.Node) {
func startNode(ctx *cli.Context, stack *node.Node, cfg tomoConfig) {
// Start up the node itself
utils.StartNode(stack)

// Unlock any account specifically requested
ks := stack.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore)

passwords := utils.MakePasswordList(ctx)
unlocks := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
for i, account := range unlocks {
if ctx.GlobalIsSet(utils.UnlockedAccountFlag.Name) {
cfg.Account.Unlocks = strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
}

if ctx.GlobalIsSet(utils.PasswordFileFlag.Name) {
cfg.Account.Passwords = utils.MakePasswordList(ctx)
}

for i, account := range cfg.Account.Unlocks {
if trimmed := strings.TrimSpace(account); trimmed != "" {
unlockAccount(ctx, ks, trimmed, i, passwords)
unlockAccount(ctx, ks, trimmed, i, cfg.Account.Passwords)
}
}
// Register wallet event handlers to open and auto-derive wallets
Expand Down Expand Up @@ -278,7 +284,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
}
}()
// Start auxiliary services if enabled
if ctx.GlobalBool(utils.StakingEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) {
if cfg.StakeEnable || ctx.GlobalBool(utils.DeveloperFlag.Name) {
// Mining only makes sense if a full Ethereum node is running
if ctx.GlobalBool(utils.LightModeFlag.Name) || ctx.GlobalString(utils.SyncModeFlag.Name) == "light" {
utils.Fatalf("Light clients do not support staking")
Expand All @@ -305,7 +311,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
}
}
// Set the gas price to the limits from the CLI and start mining
ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice)
if err := ethereum.StartStaking(true); err != nil {
utils.Fatalf("Failed to start staking: %v", err)
}
Expand Down Expand Up @@ -341,7 +347,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
}
}
// Set the gas price to the limits from the CLI and start mining
ethereum.TxPool().SetGasPrice(utils.GlobalBig(ctx, utils.GasPriceFlag.Name))
ethereum.TxPool().SetGasPrice(cfg.Eth.GasPrice)
if err := ethereum.StartStaking(true); err != nil {
utils.Fatalf("Failed to start staking: %v", err)
}
Expand Down
35 changes: 35 additions & 0 deletions cmd/tomo/testdata/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
StakeEnable = true # flag --miner ( true : enable staker , false : disable )
[Eth]
NetworkId = 89 # flag --networkid
SyncMode = "full" # flag --syncmode
GasPrice = 1 # flag --gasprice

[Shh]

[Node]
DataDir = "node1/" # flag --datadir
HTTPHost = "localhost" # flag --rpcaddr
HTTPPort = 8501 # flag --rpcport
HTTPModules = ["personal","db","eth","net","web3","txpool","miner"] # flag --rpcapi
[Node.P2P]
ListenAddr = ":30311" # flag --port
BootstrapNodes = ["enode://a890c5762c406fe046fb93fd307577a8454d571b6bf789f7dbfbf3c559be751f5fa400bc10639691245a9b22be1cfce0bbf82b322a24d06c6dcf29bf7eeb930c@127.0.0.1:30310"]
# flag --bootnodes

[Ethstats]
[Dashboard]
[Account]
Unlocks = ["0x12f90a417f41bedd4bbcc99d52971803fb4c3f8b"] # list account slipt in flag --unlock
Passwords = ["PWD_DEVNET"] # list password in environment variable (split by ',') : ex : export PWD_DEVNET=123456,123456789


[Bootnodes]
Mainnet =[]
Testnet =[]







0 comments on commit 46e8e17

Please sign in to comment.