Skip to content

Commit 770b29f

Browse files
committed
Merge pull request bnb-chain#2175 from karalabe/refactor-http-rpc
cmd, common, node, rpc: move HTTP RPC into node, drop singleton aspect
2 parents b05e472 + df75dbf commit 770b29f

19 files changed

Lines changed: 654 additions & 540 deletions

File tree

‎cmd/geth/js_test.go‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,21 @@ import (
3737
"github.com/ethereum/go-ethereum/eth"
3838
"github.com/ethereum/go-ethereum/ethdb"
3939
"github.com/ethereum/go-ethereum/node"
40-
"github.com/ethereum/go-ethereum/cmd/utils"
4140
)
4241

4342
const (
4443
testSolcPath = ""
45-
solcVersion = "0.9.23"
44+
solcVersion = "0.9.23"
4645

47-
testKey = "e6fab74a43941f82d89cb7faa408e227cdad3153c4720e540e855c19b15e6674"
46+
testKey = "e6fab74a43941f82d89cb7faa408e227cdad3153c4720e540e855c19b15e6674"
4847
testAddress = "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
4948
testBalance = "10000000000000000000"
50-
// of empty string
49+
// of empty string
5150
testHash = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
5251
)
5352

5453
var (
55-
versionRE = regexp.MustCompile(strconv.Quote(`"compilerVersion":"` + solcVersion + `"`))
54+
versionRE = regexp.MustCompile(strconv.Quote(`"compilerVersion":"` + solcVersion + `"`))
5655
testNodeKey = crypto.ToECDSA(common.Hex2Bytes("4b50fa71f5c3eeb8fdc452224b2395af2fcc3d125e06c32c82e048c0559db03f"))
5756
testGenesis = `{"` + testAddress[2:] + `": {"balance": "` + testBalance + `"}}`
5857
)
@@ -141,8 +140,10 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
141140
stack.Service(&ethereum)
142141

143142
assetPath := filepath.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
144-
//client := comms.NewInProcClient(codec.JSON)
145-
client := utils.NewInProcRPCClient(stack)
143+
client, err := stack.Attach()
144+
if err != nil {
145+
t.Fatalf("failed to attach to node: %v", err)
146+
}
146147
tf := &testjethre{client: ethereum.HTTPClient()}
147148
repl := newJSRE(stack, assetPath, "", client, false)
148149
tf.jsre = repl
@@ -152,9 +153,6 @@ func testREPL(t *testing.T, config func(*eth.Config)) (string, *testjethre, *nod
152153
func TestNodeInfo(t *testing.T) {
153154
t.Skip("broken after p2p update")
154155
tmp, repl, ethereum := testJEthRE(t)
155-
if err := ethereum.Start(); err != nil {
156-
t.Fatalf("error starting ethereum: %v", err)
157-
}
158156
defer ethereum.Stop()
159157
defer os.RemoveAll(tmp)
160158

@@ -167,8 +165,8 @@ func TestAccounts(t *testing.T) {
167165
defer node.Stop()
168166
defer os.RemoveAll(tmp)
169167

170-
checkEvalJSON(t, repl, `eth.accounts`, `["` + testAddress + `"]`)
171-
checkEvalJSON(t, repl, `eth.coinbase`, `"` + testAddress + `"`)
168+
checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`"]`)
169+
checkEvalJSON(t, repl, `eth.coinbase`, `"`+testAddress+`"`)
172170
val, err := repl.re.Run(`jeth.newAccount("password")`)
173171
if err != nil {
174172
t.Errorf("expected no error, got %v", err)
@@ -178,7 +176,7 @@ func TestAccounts(t *testing.T) {
178176
t.Errorf("address not hex: %q", addr)
179177
}
180178

181-
checkEvalJSON(t, repl, `eth.accounts`, `["` + testAddress + `","` + addr + `"]`)
179+
checkEvalJSON(t, repl, `eth.accounts`, `["`+testAddress+`","`+addr+`"]`)
182180

183181
}
184182

@@ -206,13 +204,13 @@ func TestBlockChain(t *testing.T) {
206204
node.Service(&ethereum)
207205
ethereum.BlockChain().Reset()
208206

209-
checkEvalJSON(t, repl, `admin.exportChain(` + tmpfileq + `)`, `true`)
207+
checkEvalJSON(t, repl, `admin.exportChain(`+tmpfileq+`)`, `true`)
210208
if _, err := os.Stat(tmpfile); err != nil {
211209
t.Fatal(err)
212210
}
213211

214212
// check import, verify that dumpBlock gives the same result.
215-
checkEvalJSON(t, repl, `admin.importChain(` + tmpfileq + `)`, `true`)
213+
checkEvalJSON(t, repl, `admin.importChain(`+tmpfileq+`)`, `true`)
216214
checkEvalJSON(t, repl, `debug.dumpBlock(eth.blockNumber)`, beforeExport)
217215
}
218216

@@ -240,7 +238,7 @@ func TestCheckTestAccountBalance(t *testing.T) {
240238
defer os.RemoveAll(tmp)
241239

242240
repl.re.Run(`primary = "` + testAddress + `"`)
243-
checkEvalJSON(t, repl, `eth.getBalance(primary)`, `"` + testBalance + `"`)
241+
checkEvalJSON(t, repl, `eth.getBalance(primary)`, `"`+testBalance+`"`)
244242
}
245243

246244
func TestSignature(t *testing.T) {
@@ -301,11 +299,11 @@ func TestContract(t *testing.T) {
301299
*/
302300

303301
source := `contract test {\n` +
304-
" /// @notice Will multiply `a` by 7." + `\n` +
305-
` function multiply(uint a) returns(uint d) {\n` +
306-
` return a * 7;\n` +
307-
` }\n` +
308-
`}\n`
302+
" /// @notice Will multiply `a` by 7." + `\n` +
303+
` function multiply(uint a) returns(uint d) {\n` +
304+
` return a * 7;\n` +
305+
` }\n` +
306+
`}\n`
309307

310308
if checkEvalJSON(t, repl, `admin.stopNatSpec()`, `true`) != nil {
311309
return
@@ -315,10 +313,10 @@ func TestContract(t *testing.T) {
315313
if err != nil {
316314
t.Fatalf("%v", err)
317315
}
318-
if checkEvalJSON(t, repl, `primary = eth.accounts[0]`, `"` + testAddress + `"`) != nil {
316+
if checkEvalJSON(t, repl, `primary = eth.accounts[0]`, `"`+testAddress+`"`) != nil {
319317
return
320318
}
321-
if checkEvalJSON(t, repl, `source = "` + source + `"`, `"` + source + `"`) != nil {
319+
if checkEvalJSON(t, repl, `source = "`+source+`"`, `"`+source+`"`) != nil {
322320
return
323321
}
324322

@@ -396,7 +394,7 @@ multiply7 = Multiply7.at(contractaddress);
396394

397395
var contentHash = `"0x86d2b7cf1e72e9a7a3f8d96601f0151742a2f780f1526414304fbe413dc7f9bd"`
398396
if sol != nil && solcVersion != sol.Version() {
399-
modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"` + sol.Version() + `"`))
397+
modContractInfo := versionRE.ReplaceAll(contractInfo, []byte(`"compilerVersion":"`+sol.Version()+`"`))
400398
fmt.Printf("modified contractinfo:\n%s\n", modContractInfo)
401399
contentHash = `"` + common.ToHex(crypto.Sha3([]byte(modContractInfo))) + `"`
402400
}
@@ -481,7 +479,7 @@ func processTxs(repl *testjethre, t *testing.T, expTxc int) bool {
481479
repl.wait <- height
482480
select {
483481
case <-timer.C:
484-
// if times out make sure the xeth loop does not block
482+
// if times out make sure the xeth loop does not block
485483
go func() {
486484
select {
487485
case repl.wait <- nil:

‎cmd/geth/main.go‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ func attach(ctx *cli.Context) {
399399
// attach to a running geth instance
400400
client, err := utils.NewRemoteRPCClient(ctx)
401401
if err != nil {
402-
utils.Fatalf("Unable to attach to geth - %v", err)
402+
utils.Fatalf("Unable to attach to geth: %v", err)
403403
}
404404

405405
repl := newLightweightJSRE(
@@ -425,8 +425,10 @@ func console(ctx *cli.Context) {
425425
startNode(ctx, node)
426426

427427
// Attach to the newly started node, and either execute script or become interactive
428-
client := utils.NewInProcRPCClient(node)
429-
428+
client, err := node.Attach()
429+
if err != nil {
430+
utils.Fatalf("Failed to attach to the inproc geth: %v", err)
431+
}
430432
repl := newJSRE(node,
431433
ctx.GlobalString(utils.JSpathFlag.Name),
432434
ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
@@ -449,8 +451,10 @@ func execScripts(ctx *cli.Context) {
449451
startNode(ctx, node)
450452

451453
// Attach to the newly started node and execute the given scripts
452-
client := utils.NewInProcRPCClient(node)
453-
454+
client, err := node.Attach()
455+
if err != nil {
456+
utils.Fatalf("Failed to attach to the inproc geth: %v", err)
457+
}
454458
repl := newJSRE(node,
455459
ctx.GlobalString(utils.JSpathFlag.Name),
456460
ctx.GlobalString(utils.RPCCORSDomainFlag.Name),
@@ -503,16 +507,6 @@ func startNode(ctx *cli.Context, stack *node.Node) {
503507
}
504508
}
505509
// Start auxiliary services if enabled
506-
if ctx.GlobalBool(utils.RPCEnabledFlag.Name) {
507-
if err := utils.StartRPC(stack, ctx); err != nil {
508-
utils.Fatalf("Failed to start RPC: %v", err)
509-
}
510-
}
511-
if ctx.GlobalBool(utils.WSEnabledFlag.Name) {
512-
if err := utils.StartWS(stack, ctx); err != nil {
513-
utils.Fatalf("Failed to start WS: %v", err)
514-
}
515-
}
516510
if ctx.GlobalBool(utils.MiningEnabledFlag.Name) {
517511
if err := ethereum.StartMining(ctx.GlobalInt(utils.MinerThreadsFlag.Name), ctx.GlobalString(utils.MiningGPUFlag.Name)); err != nil {
518512
utils.Fatalf("Failed to start mining: %v", err)

‎cmd/geth/monitorcmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
var (
3737
monitorCommandAttachFlag = cli.StringFlag{
3838
Name: "attach",
39-
Value: "ipc:" + node.DefaultIpcEndpoint(),
39+
Value: "ipc:" + node.DefaultIPCEndpoint(),
4040
Usage: "API endpoint to attach to",
4141
}
4242
monitorCommandRowsFlag = cli.IntFlag{

‎cmd/gethrpctest/main.go‎

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
package main
1919

2020
import (
21-
"errors"
2221
"flag"
2322
"io/ioutil"
2423
"log"
2524
"os"
2625
"os/signal"
2726

2827
"github.com/ethereum/go-ethereum/accounts"
28+
"github.com/ethereum/go-ethereum/common"
2929
"github.com/ethereum/go-ethereum/crypto"
3030
"github.com/ethereum/go-ethereum/eth"
3131
"github.com/ethereum/go-ethereum/ethdb"
32-
"github.com/ethereum/go-ethereum/logger"
3332
"github.com/ethereum/go-ethereum/logger/glog"
3433
"github.com/ethereum/go-ethereum/node"
3534
"github.com/ethereum/go-ethereum/tests"
@@ -84,12 +83,6 @@ func main() {
8483
}
8584
log.Println("Initial test suite passed...")
8685

87-
// Start the RPC interface and wait until terminated
88-
if err := StartRPC(stack); err != nil {
89-
log.Fatalf("Failed to start RPC interface: %v", err)
90-
}
91-
log.Println("RPC Interface started, accepting requests...")
92-
9386
quit := make(chan os.Signal, 1)
9487
signal.Notify(quit, os.Interrupt)
9588
<-quit
@@ -99,7 +92,16 @@ func main() {
9992
// keystore path and initial pre-state.
10093
func MakeSystemNode(keydir string, privkey string, test *tests.BlockTest) (*node.Node, error) {
10194
// Create a networkless protocol stack
102-
stack, err := node.New(&node.Config{IpcPath: node.DefaultIpcEndpoint(), NoDiscovery: true})
95+
stack, err := node.New(&node.Config{
96+
IPCPath: node.DefaultIPCEndpoint(),
97+
HTTPHost: common.DefaultHTTPHost,
98+
HTTPPort: common.DefaultHTTPPort,
99+
HTTPModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
100+
WSHost: common.DefaultWSHost,
101+
WSPort: common.DefaultWSPort,
102+
WSModules: []string{"admin", "db", "eth", "debug", "miner", "net", "shh", "txpool", "personal", "web3"},
103+
NoDiscovery: true,
104+
})
103105
if err != nil {
104106
return nil, err
105107
}
@@ -164,23 +166,3 @@ func RunTest(stack *node.Node, test *tests.BlockTest) error {
164166
}
165167
return nil
166168
}
167-
168-
// StartRPC initializes an RPC interface to the given protocol stack.
169-
func StartRPC(stack *node.Node) error {
170-
/*
171-
web3 := NewPublicWeb3API(stack)
172-
server.RegisterName("web3", web3)
173-
net := NewPublicNetAPI(stack.Server(), ethereum.NetVersion())
174-
server.RegisterName("net", net)
175-
*/
176-
177-
for _, api := range stack.APIs() {
178-
if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok {
179-
_, err := adminApi.StartRPC("127.0.0.1", 8545, "", "admin,db,eth,debug,miner,net,shh,txpool,personal,web3")
180-
return err
181-
}
182-
}
183-
184-
glog.V(logger.Error).Infof("Unable to start RPC-HTTP interface, could not find admin API")
185-
return errors.New("Unable to start RPC-HTTP interface")
186-
}

0 commit comments

Comments
 (0)