Skip to content

Commit

Permalink
Merge pull request ethereum#2909 from fjl/account-manager-cleanup
Browse files Browse the repository at this point in the history
all: clean up tech debt left behind by the API split
  • Loading branch information
fjl authored Aug 17, 2016
2 parents 3369783 + 3c09c5f commit 475521d
Show file tree
Hide file tree
Showing 22 changed files with 462 additions and 496 deletions.
22 changes: 0 additions & 22 deletions accounts/account_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)

var (
Expand Down Expand Up @@ -342,23 +340,3 @@ func zeroKey(k *ecdsa.PrivateKey) {
b[i] = 0
}
}

// APIs implements node.Service
func (am *Manager) APIs() []rpc.API {
return nil
}

// Protocols implements node.Service
func (am *Manager) Protocols() []p2p.Protocol {
return nil
}

// Start implements node.Service
func (am *Manager) Start(srvr *p2p.Server) error {
return nil
}

// Stop implements node.Service
func (am *Manager) Stop() error {
return nil
}
13 changes: 1 addition & 12 deletions cmd/abigen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,7 @@ func main() {
for _, kind := range strings.Split(*excFlag, ",") {
exclude[strings.ToLower(kind)] = true
}
// Build the Solidity source into bindable components
solc, err := compiler.New(*solcFlag)
if err != nil {
fmt.Printf("Failed to locate Solidity compiler: %v\n", err)
os.Exit(-1)
}
source, err := ioutil.ReadFile(*solFlag)
if err != nil {
fmt.Printf("Failed to read Soldity source code: %v\n", err)
os.Exit(-1)
}
contracts, err := solc.Compile(string(source))
contracts, err := compiler.CompileSolidity(*solcFlag, *solFlag)
if err != nil {
fmt.Printf("Failed to build Solidity contract: %v\n", err)
os.Exit(-1)
Expand Down
24 changes: 11 additions & 13 deletions cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ nodes.
)

func accountList(ctx *cli.Context) error {
accman := utils.MakeAccountManager(ctx)
for i, acct := range accman.Accounts() {
stack := utils.MakeNode(ctx, clientIdentifier, verString)
for i, acct := range stack.AccountManager().Accounts() {
fmt.Printf("Account #%d: {%x} %s\n", i, acct.Address, acct.File)
}
return nil
Expand Down Expand Up @@ -261,10 +261,10 @@ func ambiguousAddrRecovery(am *accounts.Manager, err *accounts.AmbiguousAddrErro

// accountCreate creates a new account into the keystore defined by the CLI flags.
func accountCreate(ctx *cli.Context) error {
accman := utils.MakeAccountManager(ctx)
stack := utils.MakeNode(ctx, clientIdentifier, verString)
password := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))

account, err := accman.NewAccount(password)
account, err := stack.AccountManager().NewAccount(password)
if err != nil {
utils.Fatalf("Failed to create account: %v", err)
}
Expand All @@ -278,11 +278,10 @@ func accountUpdate(ctx *cli.Context) error {
if len(ctx.Args()) == 0 {
utils.Fatalf("No accounts specified to update")
}
accman := utils.MakeAccountManager(ctx)

account, oldPassword := unlockAccount(ctx, accman, ctx.Args().First(), 0, nil)
stack := utils.MakeNode(ctx, clientIdentifier, verString)
account, oldPassword := unlockAccount(ctx, stack.AccountManager(), ctx.Args().First(), 0, nil)
newPassword := getPassPhrase("Please give a new password. Do not forget this password.", true, 0, nil)
if err := accman.Update(account, oldPassword, newPassword); err != nil {
if err := stack.AccountManager().Update(account, oldPassword, newPassword); err != nil {
utils.Fatalf("Could not update the account: %v", err)
}
return nil
Expand All @@ -298,10 +297,9 @@ func importWallet(ctx *cli.Context) error {
utils.Fatalf("Could not read wallet file: %v", err)
}

accman := utils.MakeAccountManager(ctx)
stack := utils.MakeNode(ctx, clientIdentifier, verString)
passphrase := getPassPhrase("", false, 0, utils.MakePasswordList(ctx))

acct, err := accman.ImportPreSaleKey(keyJson, passphrase)
acct, err := stack.AccountManager().ImportPreSaleKey(keyJson, passphrase)
if err != nil {
utils.Fatalf("%v", err)
}
Expand All @@ -318,9 +316,9 @@ func accountImport(ctx *cli.Context) error {
if err != nil {
utils.Fatalf("Failed to load the private key: %v", err)
}
accman := utils.MakeAccountManager(ctx)
stack := utils.MakeNode(ctx, clientIdentifier, verString)
passphrase := getPassPhrase("Your new account is locked with a password. Please give a password. Do not forget this password.", true, 0, utils.MakePasswordList(ctx))
acct, err := accman.ImportECDSA(key, passphrase)
acct, err := stack.AccountManager().ImportECDSA(key, passphrase)
if err != nil {
utils.Fatalf("Could not create the account: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/geth/consolecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
// same time.
func localConsole(ctx *cli.Context) error {
// Create and start the node based on the CLI flags
node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx)
node := makeFullNode(ctx)
startNode(ctx, node)
defer node.Stop()

Expand Down Expand Up @@ -149,7 +149,7 @@ 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 := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx)
node := makeFullNode(ctx)
startNode(ctx, node)
defer node.Stop()

Expand Down
62 changes: 34 additions & 28 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"time"

"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/console"
Expand Down Expand Up @@ -244,34 +243,13 @@ func main() {
}
}

func makeDefaultExtra() []byte {
var clientInfo = struct {
Version uint
Name string
GoVersion string
Os string
}{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
extra, err := rlp.EncodeToBytes(clientInfo)
if err != nil {
glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
}

if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
glog.V(logger.Debug).Infof("extra: %x\n", extra)
return nil
}
return extra
}

// geth is the main entry point into the system if no special subcommand is ran.
// 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 geth(ctx *cli.Context) error {
node := utils.MakeSystemNode(clientIdentifier, verString, relConfig, makeDefaultExtra(), ctx)
node := makeFullNode(ctx)
startNode(ctx, node)
node.Wait()

return nil
}

Expand Down Expand Up @@ -301,6 +279,38 @@ func initGenesis(ctx *cli.Context) error {
return nil
}

func makeFullNode(ctx *cli.Context) *node.Node {
node := utils.MakeNode(ctx, clientIdentifier, verString)
utils.RegisterEthService(ctx, node, relConfig, makeDefaultExtra())
// Whisper must be explicitly enabled, but is auto-enabled in --dev mode.
shhEnabled := ctx.GlobalBool(utils.WhisperEnabledFlag.Name)
shhAutoEnabled := !ctx.GlobalIsSet(utils.WhisperEnabledFlag.Name) && ctx.GlobalIsSet(utils.DevModeFlag.Name)
if shhEnabled || shhAutoEnabled {
utils.RegisterShhService(node)
}
return node
}

func makeDefaultExtra() []byte {
var clientInfo = struct {
Version uint
Name string
GoVersion string
Os string
}{uint(versionMajor<<16 | versionMinor<<8 | versionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
extra, err := rlp.EncodeToBytes(clientInfo)
if err != nil {
glog.V(logger.Warn).Infoln("error setting canonical miner information:", err)
}

if uint64(len(extra)) > params.MaximumExtraDataSize.Uint64() {
glog.V(logger.Warn).Infoln("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize)
glog.V(logger.Debug).Infof("extra: %x\n", extra)
return nil
}
return extra
}

// 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.
Expand All @@ -311,12 +321,8 @@ func startNode(ctx *cli.Context, stack *node.Node) {
utils.StartNode(stack)

// Unlock any account specifically requested
var accman *accounts.Manager
if err := stack.Service(&accman); err != nil {
utils.Fatalf("ethereum service not running: %v", err)
}
accman := stack.AccountManager()
passwords := utils.MakePasswordList(ctx)

accounts := strings.Split(ctx.GlobalString(utils.UnlockedAccountFlag.Name), ",")
for i, account := range accounts {
if trimmed := strings.TrimSpace(account); trimmed != "" {
Expand Down
32 changes: 12 additions & 20 deletions cmd/gethrpctest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ package main

import (
"flag"
"io/ioutil"
"log"
"os"
"os/signal"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -61,14 +59,8 @@ func main() {
if !found {
log.Fatalf("Requested test (%s) not found within suite", *testName)
}
// Create the protocol stack to run the test with
keydir, err := ioutil.TempDir("", "")
if err != nil {
log.Fatalf("Failed to create temporary keystore directory: %v", err)
}
defer os.RemoveAll(keydir)

stack, err := MakeSystemNode(keydir, *testKey, test)
stack, err := MakeSystemNode(*testKey, test)
if err != nil {
log.Fatalf("Failed to assemble test stack: %v", err)
}
Expand All @@ -92,23 +84,24 @@ func main() {

// MakeSystemNode configures a protocol stack for the RPC tests based on a given
// keystore path and initial pre-state.
func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) {
func MakeSystemNode(privkey string, test *tests.BlockTest) (*node.Node, error) {
// Create a networkless protocol stack
stack, err := node.New(&node.Config{
IPCPath: node.DefaultIPCEndpoint(),
HTTPHost: common.DefaultHTTPHost,
HTTPPort: common.DefaultHTTPPort,
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
WSHost: common.DefaultWSHost,
WSPort: common.DefaultWSPort,
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
NoDiscovery: true,
UseLightweightKDF: true,
IPCPath: node.DefaultIPCEndpoint(),
HTTPHost: common.DefaultHTTPHost,
HTTPPort: common.DefaultHTTPPort,
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
WSHost: common.DefaultWSHost,
WSPort: common.DefaultWSPort,
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
NoDiscovery: true,
})
if err != nil {
return nil, err
}
// Create the keystore and inject an unlocked account if requested
accman := accounts.NewPlaintextManager(keydir)
accman := stack.AccountManager()
if len(privkey) > 0 {
key, err := crypto.HexToECDSA(privkey)
if err != nil {
Expand All @@ -131,7 +124,6 @@ func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node
TestGenesisState: db,
TestGenesisBlock: test.Genesis,
ChainConfig: &core.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock},
AccountManager: accman,
}
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 475521d

Please sign in to comment.