cmd/geth, cmd/utils: rename utils.Get* -> utils.Make*

The renaming should make it clearer that these functions create a new
instance for every call. @obscuren suggested this renaming a while ago.
This commit is contained in:
Felix Lange 2015-05-27 14:50:31 +02:00
parent 8e4512a5e7
commit 74706a0f02
3 changed files with 21 additions and 16 deletions

View File

@ -52,7 +52,7 @@ func importChain(ctx *cli.Context) {
if len(ctx.Args()) != 1 { if len(ctx.Args()) != 1 {
utils.Fatalf("This command requires an argument.") utils.Fatalf("This command requires an argument.")
} }
chain, blockDB, stateDB, extraDB := utils.GetChain(ctx) chain, blockDB, stateDB, extraDB := utils.MakeChain(ctx)
start := time.Now() start := time.Now()
if err := utils.ImportChain(chain, ctx.Args().First()); err != nil { if err := utils.ImportChain(chain, ctx.Args().First()); err != nil {
utils.Fatalf("Import error: %v\n", err) utils.Fatalf("Import error: %v\n", err)
@ -65,7 +65,7 @@ func exportChain(ctx *cli.Context) {
if len(ctx.Args()) != 1 { if len(ctx.Args()) != 1 {
utils.Fatalf("This command requires an argument.") utils.Fatalf("This command requires an argument.")
} }
chain, _, _, _ := utils.GetChain(ctx) chain, _, _, _ := utils.MakeChain(ctx)
start := time.Now() start := time.Now()
if err := utils.ExportChain(chain, ctx.Args().First()); err != nil { if err := utils.ExportChain(chain, ctx.Args().First()); err != nil {
utils.Fatalf("Export error: %v\n", err) utils.Fatalf("Export error: %v\n", err)
@ -95,7 +95,7 @@ func removeDB(ctx *cli.Context) {
func upgradeDB(ctx *cli.Context) { func upgradeDB(ctx *cli.Context) {
glog.Infoln("Upgrading blockchain database") glog.Infoln("Upgrading blockchain database")
chain, blockDB, stateDB, extraDB := utils.GetChain(ctx) chain, blockDB, stateDB, extraDB := utils.MakeChain(ctx)
v, _ := blockDB.Get([]byte("BlockchainVersion")) v, _ := blockDB.Get([]byte("BlockchainVersion"))
bcVersion := int(common.NewValue(v).Uint()) bcVersion := int(common.NewValue(v).Uint())
if bcVersion == 0 { if bcVersion == 0 {
@ -113,7 +113,7 @@ func upgradeDB(ctx *cli.Context) {
os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state")) os.RemoveAll(filepath.Join(ctx.GlobalString(utils.DataDirFlag.Name), "state"))
// Import the chain file. // Import the chain file.
chain, blockDB, stateDB, extraDB = utils.GetChain(ctx) chain, blockDB, stateDB, extraDB = utils.MakeChain(ctx)
blockDB.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes()) blockDB.Put([]byte("BlockchainVersion"), common.NewValue(core.BlockChainVersion).Bytes())
err := utils.ImportChain(chain, exportFile) err := utils.ImportChain(chain, exportFile)
flushAll(blockDB, stateDB, extraDB) flushAll(blockDB, stateDB, extraDB)
@ -126,7 +126,7 @@ func upgradeDB(ctx *cli.Context) {
} }
func dump(ctx *cli.Context) { func dump(ctx *cli.Context) {
chain, _, stateDB, _ := utils.GetChain(ctx) chain, _, stateDB, _ := utils.MakeChain(ctx)
for _, arg := range ctx.Args() { for _, arg := range ctx.Args() {
var block *types.Block var block *types.Block
if hashish(arg) { if hashish(arg) {

View File

@ -394,7 +394,7 @@ func startEth(ctx *cli.Context, eth *eth.Ethereum) {
} }
func accountList(ctx *cli.Context) { func accountList(ctx *cli.Context) {
am := utils.GetAccountManager(ctx) am := utils.MakeAccountManager(ctx)
accts, err := am.Accounts() accts, err := am.Accounts()
if err != nil { if err != nil {
utils.Fatalf("Could not list accounts: %v", err) utils.Fatalf("Could not list accounts: %v", err)
@ -436,7 +436,7 @@ func getPassPhrase(ctx *cli.Context, desc string, confirmation bool) (passphrase
} }
func accountCreate(ctx *cli.Context) { func accountCreate(ctx *cli.Context) {
am := utils.GetAccountManager(ctx) am := utils.MakeAccountManager(ctx)
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true) passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true)
acct, err := am.NewAccount(passphrase) acct, err := am.NewAccount(passphrase)
if err != nil { if err != nil {
@ -455,7 +455,7 @@ func importWallet(ctx *cli.Context) {
utils.Fatalf("Could not read wallet file: %v", err) utils.Fatalf("Could not read wallet file: %v", err)
} }
am := utils.GetAccountManager(ctx) am := utils.MakeAccountManager(ctx)
passphrase := getPassPhrase(ctx, "", false) passphrase := getPassPhrase(ctx, "", false)
acct, err := am.ImportPreSaleKey(keyJson, passphrase) acct, err := am.ImportPreSaleKey(keyJson, passphrase)
@ -470,7 +470,7 @@ func accountImport(ctx *cli.Context) {
if len(keyfile) == 0 { if len(keyfile) == 0 {
utils.Fatalf("keyfile must be given as argument") utils.Fatalf("keyfile must be given as argument")
} }
am := utils.GetAccountManager(ctx) am := utils.MakeAccountManager(ctx)
passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true) passphrase := getPassPhrase(ctx, "Your new account is locked with a password. Please give a password. Do not forget this password.", true)
acct, err := am.Import(keyfile, passphrase) acct, err := am.Import(keyfile, passphrase)
if err != nil { if err != nil {

View File

@ -256,7 +256,8 @@ var (
} }
) )
func GetNAT(ctx *cli.Context) nat.Interface { // MakeNAT creates a port mapper from set command line flags.
func MakeNAT(ctx *cli.Context) nat.Interface {
natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name)) natif, err := nat.Parse(ctx.GlobalString(NATFlag.Name))
if err != nil { if err != nil {
Fatalf("Option %s: %v", NATFlag.Name, err) Fatalf("Option %s: %v", NATFlag.Name, err)
@ -264,7 +265,8 @@ func GetNAT(ctx *cli.Context) nat.Interface {
return natif return natif
} }
func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) { // MakeNodeKey creates a node key from set command line flags.
func MakeNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name) hex, file := ctx.GlobalString(NodeKeyHexFlag.Name), ctx.GlobalString(NodeKeyFileFlag.Name)
var err error var err error
switch { switch {
@ -282,6 +284,7 @@ func GetNodeKey(ctx *cli.Context) (key *ecdsa.PrivateKey) {
return key return key
} }
// MakeEthConfig creates ethereum options from set command line flags.
func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config { func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
customName := ctx.GlobalString(IdentityFlag.Name) customName := ctx.GlobalString(IdentityFlag.Name)
if len(customName) > 0 { if len(customName) > 0 {
@ -299,15 +302,15 @@ func MakeEthConfig(clientID, version string, ctx *cli.Context) *eth.Config {
LogJSON: ctx.GlobalString(LogJSONFlag.Name), LogJSON: ctx.GlobalString(LogJSONFlag.Name),
Etherbase: ctx.GlobalString(EtherbaseFlag.Name), Etherbase: ctx.GlobalString(EtherbaseFlag.Name),
MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name), MinerThreads: ctx.GlobalInt(MinerThreadsFlag.Name),
AccountManager: GetAccountManager(ctx), AccountManager: MakeAccountManager(ctx),
VmDebug: ctx.GlobalBool(VMDebugFlag.Name), VmDebug: ctx.GlobalBool(VMDebugFlag.Name),
MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name), MaxPeers: ctx.GlobalInt(MaxPeersFlag.Name),
MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name), MaxPendingPeers: ctx.GlobalInt(MaxPendingPeersFlag.Name),
Port: ctx.GlobalString(ListenPortFlag.Name), Port: ctx.GlobalString(ListenPortFlag.Name),
NAT: GetNAT(ctx), NAT: MakeNAT(ctx),
NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name), NatSpec: ctx.GlobalBool(NatspecEnabledFlag.Name),
Discovery: !ctx.GlobalBool(NoDiscoverFlag.Name), Discovery: !ctx.GlobalBool(NoDiscoverFlag.Name),
NodeKey: GetNodeKey(ctx), NodeKey: MakeNodeKey(ctx),
Shh: ctx.GlobalBool(WhisperEnabledFlag.Name), Shh: ctx.GlobalBool(WhisperEnabledFlag.Name),
Dial: true, Dial: true,
BootNodes: ctx.GlobalString(BootnodesFlag.Name), BootNodes: ctx.GlobalString(BootnodesFlag.Name),
@ -325,7 +328,8 @@ func SetupLogger(ctx *cli.Context) {
glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name)) glog.SetLogDir(ctx.GlobalString(LogFileFlag.Name))
} }
func GetChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) { // MakeChain creates a chain manager from set command line flags.
func MakeChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, extraDB common.Database) {
dd := ctx.GlobalString(DataDirFlag.Name) dd := ctx.GlobalString(DataDirFlag.Name)
var err error var err error
if blockDB, err = ethdb.NewLDBDatabase(filepath.Join(dd, "blockchain")); err != nil { if blockDB, err = ethdb.NewLDBDatabase(filepath.Join(dd, "blockchain")); err != nil {
@ -347,7 +351,8 @@ func GetChain(ctx *cli.Context) (chain *core.ChainManager, blockDB, stateDB, ext
return chain, blockDB, stateDB, extraDB return chain, blockDB, stateDB, extraDB
} }
func GetAccountManager(ctx *cli.Context) *accounts.Manager { // MakeChain creates an account manager from set command line flags.
func MakeAccountManager(ctx *cli.Context) *accounts.Manager {
dataDir := ctx.GlobalString(DataDirFlag.Name) dataDir := ctx.GlobalString(DataDirFlag.Name)
ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore")) ks := crypto.NewKeyStorePassphrase(filepath.Join(dataDir, "keystore"))
return accounts.NewManager(ks) return accounts.NewManager(ks)