Skip to content

internal/web3ext,node: migrate node admin API (Start|Stop)RPC->HTTP #22461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,24 @@ web3._extend({
call: 'admin_sleepBlocks',
params: 2
}),
new web3._extend.Method({
name: 'startHTTP',
call: 'admin_startHTTP',
params: 5,
inputFormatter: [null, null, null, null, null]
}),
new web3._extend.Method({
name: 'stopHTTP',
call: 'admin_stopHTTP'
}),
// This method is deprecated.
new web3._extend.Method({
name: 'startRPC',
call: 'admin_startRPC',
params: 4,
inputFormatter: [null, null, null, null]
params: 5,
inputFormatter: [null, null, null, null, null]
}),
// This method is deprecated.
new web3._extend.Method({
name: 'stopRPC',
call: 'admin_stopRPC'
Expand Down
23 changes: 19 additions & 4 deletions node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -162,8 +163,8 @@ func (api *privateAdminAPI) PeerEvents(ctx context.Context) (*rpc.Subscription,
return rpcSub, nil
}

// StartRPC starts the HTTP RPC API server.
func (api *privateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
// StartHTTP starts the HTTP RPC API server.
func (api *privateAdminAPI) StartHTTP(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
api.node.lock.Lock()
defer api.node.lock.Unlock()

Expand Down Expand Up @@ -216,12 +217,26 @@ func (api *privateAdminAPI) StartRPC(host *string, port *int, cors *string, apis
return true, nil
}

// StopRPC shuts down the HTTP server.
func (api *privateAdminAPI) StopRPC() (bool, error) {
// StartRPC starts the HTTP RPC API server.
// This method is deprecated. Use StartHTTP instead.
func (api *privateAdminAPI) StartRPC(host *string, port *int, cors *string, apis *string, vhosts *string) (bool, error) {
log.Warn("Deprecation warning", "method", "admin.StartRPC", "use-instead", "admin.StartHTTP")
return api.StartHTTP(host, port, cors, apis, vhosts)
}

// StopHTTP shuts down the HTTP server.
func (api *privateAdminAPI) StopHTTP() (bool, error) {
api.node.http.stop()
return true, nil
}

// StopRPC shuts down the HTTP server.
// This method is deprecated. Use StopHTTP instead.
func (api *privateAdminAPI) StopRPC() (bool, error) {
log.Warn("Deprecation warning", "method", "admin.StopRPC", "use-instead", "admin.StopHTTP")
return api.StopHTTP()
}

// StartWS starts the websocket RPC API server.
func (api *privateAdminAPI) StartWS(host *string, port *int, allowedOrigins *string, apis *string) (bool, error) {
api.node.lock.Lock()
Expand Down
20 changes: 10 additions & 10 deletions node/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestStartRPC(t *testing.T) {
name: "rpc enabled through API",
cfg: Config{},
fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
_, err := api.StartRPC(sp("127.0.0.1"), ip(0), nil, nil, nil)
_, err := api.StartHTTP(sp("127.0.0.1"), ip(0), nil, nil, nil)
assert.NoError(t, err)
},
wantReachable: true,
Expand All @@ -90,14 +90,14 @@ func TestStartRPC(t *testing.T) {
port := listener.Addr().(*net.TCPAddr).Port

// Now try to start RPC on that port. This should fail.
_, err = api.StartRPC(sp("127.0.0.1"), ip(port), nil, nil, nil)
_, err = api.StartHTTP(sp("127.0.0.1"), ip(port), nil, nil, nil)
if err == nil {
t.Fatal("StartRPC should have failed on port", port)
t.Fatal("StartHTTP should have failed on port", port)
}

// Try again after unblocking the port. It should work this time.
listener.Close()
_, err = api.StartRPC(sp("127.0.0.1"), ip(port), nil, nil, nil)
_, err = api.StartHTTP(sp("127.0.0.1"), ip(port), nil, nil, nil)
assert.NoError(t, err)
},
wantReachable: true,
Expand All @@ -109,7 +109,7 @@ func TestStartRPC(t *testing.T) {
name: "rpc stopped through API",
cfg: Config{HTTPHost: "127.0.0.1"},
fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
_, err := api.StopRPC()
_, err := api.StopHTTP()
assert.NoError(t, err)
},
wantReachable: false,
Expand All @@ -121,10 +121,10 @@ func TestStartRPC(t *testing.T) {
name: "rpc stopped twice",
cfg: Config{HTTPHost: "127.0.0.1"},
fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
_, err := api.StopRPC()
_, err := api.StopHTTP()
assert.NoError(t, err)

_, err = api.StopRPC()
_, err = api.StopHTTP()
assert.NoError(t, err)
},
wantReachable: false,
Expand Down Expand Up @@ -211,14 +211,14 @@ func TestStartRPC(t *testing.T) {
{
name: "rpc stopped with ws enabled",
fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
_, err := api.StartRPC(sp("127.0.0.1"), ip(0), nil, nil, nil)
_, err := api.StartHTTP(sp("127.0.0.1"), ip(0), nil, nil, nil)
assert.NoError(t, err)

wsport := n.http.port
_, err = api.StartWS(sp("127.0.0.1"), ip(wsport), nil, nil)
assert.NoError(t, err)

_, err = api.StopRPC()
_, err = api.StopHTTP()
assert.NoError(t, err)
},
wantReachable: false,
Expand All @@ -233,7 +233,7 @@ func TestStartRPC(t *testing.T) {
assert.NoError(t, err)

wsport := n.http.port
_, err = api.StartRPC(sp("127.0.0.1"), ip(wsport), nil, nil, nil)
_, err = api.StartHTTP(sp("127.0.0.1"), ip(wsport), nil, nil, nil)
assert.NoError(t, err)
},
wantReachable: true,
Expand Down