Skip to content

Commit 3e1000f

Browse files
committed
cmd, eth, node, rpc, xeth: use single-instance services
1 parent 1e806c4 commit 3e1000f

17 files changed

Lines changed: 329 additions & 294 deletions

File tree

‎cmd/geth/js.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (self *jsre) AskPassword() (string, bool) {
345345
func (self *jsre) ConfirmTransaction(tx string) bool {
346346
// Retrieve the Ethereum instance from the node
347347
var ethereum *eth.Ethereum
348-
if _, err := self.stack.SingletonService(&ethereum); err != nil {
348+
if err := self.stack.Service(&ethereum); err != nil {
349349
return false
350350
}
351351
// If natspec is enabled, ask for permission
@@ -367,7 +367,7 @@ func (self *jsre) UnlockAccount(addr []byte) bool {
367367
}
368368
// TODO: allow retry
369369
var ethereum *eth.Ethereum
370-
if _, err := self.stack.SingletonService(&ethereum); err != nil {
370+
if err := self.stack.Service(&ethereum); err != nil {
371371
return false
372372
}
373373
if err := ethereum.AccountManager().Unlock(common.BytesToAddress(addr), pass); err != nil {

‎cmd/geth/js_test.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type testjethre struct {
6868

6969
func (self *testjethre) UnlockAccount(acc []byte) bool {
7070
var ethereum *eth.Ethereum
71-
self.stack.SingletonService(&ethereum)
71+
self.stack.Service(&ethereum)
7272

7373
err := ethereum.AccountManager().Unlock(common.BytesToAddress(acc), "")
7474
if err != nil {
@@ -79,7 +79,7 @@ func (self *testjethre) UnlockAccount(acc []byte) bool {
7979

8080
func (self *testjethre) ConfirmTransaction(tx string) bool {
8181
var ethereum *eth.Ethereum
82-
self.stack.SingletonService(&ethereum)
82+
self.stack.Service(&ethereum)
8383

8484
if ethereum.NatSpec {
8585
self.lastConfirm = natspec.GetNotice(self.xeth, tx, self.client)
@@ -118,7 +118,7 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
118118
if config != nil {
119119
config(ethConf)
120120
}
121-
if err := stack.Register("ethereum", func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
121+
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
122122
t.Fatalf("failed to register ethereum protocol: %v", err)
123123
}
124124
// Initialize all the keys for testing
@@ -138,7 +138,7 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
138138
t.Fatalf("failed to start test stack: %v", err)
139139
}
140140
var ethereum *eth.Ethereum
141-
stack.SingletonService(&ethereum)
141+
stack.Service(&ethereum)
142142

143143
assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
144144
client := comms.NewInProcClient(codec.JSON)
@@ -202,7 +202,7 @@ func TestBlockChain(t *testing.T) {
202202
tmpfileq := strconv.Quote(tmpfile)
203203

204204
var ethereum *eth.Ethereum
205-
node.SingletonService(&ethereum)
205+
node.Service(&ethereum)
206206
ethereum.BlockChain().Reset()
207207

208208
checkEvalJSON(t, repl, `admin.exportChain(`+tmpfileq+`)`, `true`)
@@ -436,7 +436,7 @@ multiply7 = Multiply7.at(contractaddress);
436436

437437
func pendingTransactions(repl *testjethre, t *testing.T) (txc int64, err error) {
438438
var ethereum *eth.Ethereum
439-
repl.stack.SingletonService(&ethereum)
439+
repl.stack.Service(&ethereum)
440440

441441
txs := ethereum.TxPool().GetTransactions()
442442
return int64(len(txs)), nil
@@ -464,7 +464,7 @@ func processTxs(repl *testjethre, t *testing.T, expTxc int) bool {
464464
return false
465465
}
466466
var ethereum *eth.Ethereum
467-
repl.stack.SingletonService(&ethereum)
467+
repl.stack.Service(&ethereum)
468468

469469
err = ethereum.StartMining(runtime.NumCPU(), "")
470470
if err != nil {

‎cmd/geth/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
489489

490490
// Unlock any account specifically requested
491491
var ethereum *eth.Ethereum
492-
if _, err := stack.SingletonService(&ethereum); err != nil {
492+
if err := stack.Service(&ethereum); err != nil {
493493
utils.Fatalf("ethereum service not running: %v", err)
494494
}
495495
accman := ethereum.AccountManager()

‎cmd/gethrpctest/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node
126126
TestGenesisBlock: test.Genesis,
127127
AccountManager: accman,
128128
}
129-
if err := stack.Register("ethereum", func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
129+
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) { return eth.New(ctx, ethConf) }); err != nil {
130130
return nil, err
131131
}
132132
// Initialize and register the Whisper protocol
133-
if err := stack.Register("whisper", func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
133+
if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
134134
return nil, err
135135
}
136136
return stack, nil
@@ -140,7 +140,7 @@ func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node
140140
// stack to ensure basic checks pass before running RPC tests.
141141
func RunTest(stack *node.Node, test *tests.BlockTest) error {
142142
var ethereum *eth.Ethereum
143-
stack.SingletonService(&ethereum)
143+
stack.Service(&ethereum)
144144
blockchain := ethereum.BlockChain()
145145

146146
// Process the blocks and verify the imported headers

‎cmd/utils/flags.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,13 +686,13 @@ func MakeSystemNode(name, version string, extra []byte, ctx *cli.Context) *node.
686686
if err != nil {
687687
Fatalf("Failed to create the protocol stack: %v", err)
688688
}
689-
if err := stack.Register("eth", func(ctx *node.ServiceContext) (node.Service, error) {
689+
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
690690
return eth.New(ctx, ethConf)
691691
}); err != nil {
692692
Fatalf("Failed to register the Ethereum service: %v", err)
693693
}
694694
if shhEnable {
695-
if err := stack.Register("shh", func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
695+
if err := stack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
696696
Fatalf("Failed to register the Whisper service: %v", err)
697697
}
698698
}
@@ -786,7 +786,11 @@ func StartIPC(stack *node.Node, ctx *cli.Context) error {
786786
}
787787

788788
initializer := func(conn net.Conn) (comms.Stopper, shared.EthereumApi, error) {
789-
fe := useragent.NewRemoteFrontend(conn, stack.Service("eth").(*eth.Ethereum).AccountManager())
789+
var ethereum *eth.Ethereum
790+
if err := stack.Service(&ethereum); err != nil {
791+
return nil, nil, err
792+
}
793+
fe := useragent.NewRemoteFrontend(conn, ethereum.AccountManager())
790794
xeth := xeth.New(stack, fe)
791795
apis, err := api.ParseApiString(ctx.GlobalString(IPCApiFlag.Name), codec.JSON, xeth, stack)
792796
if err != nil {

‎eth/backend.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
135135
ethdb.OpenFileLimit = 128 / (dbCount + 1)
136136

137137
// Open the chain database and perform any upgrades needed
138-
chainDb, err := ctx.Database("chaindata", config.DatabaseCache)
138+
chainDb, err := ctx.OpenDatabase("chaindata", config.DatabaseCache)
139139
if err != nil {
140140
return nil, err
141141
}
@@ -149,7 +149,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
149149
return nil, err
150150
}
151151

152-
dappDb, err := ctx.Database("dapp", config.DatabaseCache)
152+
dappDb, err := ctx.OpenDatabase("dapp", config.DatabaseCache)
153153
if err != nil {
154154
return nil, err
155155
}

‎node/errors.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,27 @@
1616

1717
package node
1818

19-
import "fmt"
19+
import (
20+
"fmt"
21+
"reflect"
22+
)
2023

21-
// StopError is returned if a node fails to stop either any of its registered
24+
// DuplicateServiceError is returned during Node startup if a registered service
25+
// constructor returns a service of the same type that was already started.
26+
type DuplicateServiceError struct {
27+
Kind reflect.Type
28+
}
29+
30+
// Error generates a textual representation of the duplicate service error.
31+
func (e *DuplicateServiceError) Error() string {
32+
return fmt.Sprintf("duplicate service: %v", e.Kind)
33+
}
34+
35+
// StopError is returned if a Node fails to stop either any of its registered
2236
// services or itself.
2337
type StopError struct {
2438
Server error
25-
Services map[string]error
39+
Services map[reflect.Type]error
2640
}
2741

2842
// Error generates a textual representation of the stop error.

0 commit comments

Comments
 (0)