mirror of https://github.com/hyperledger/besu
Remove Peer json RPC added (#1129)
When RemovePeer is invoked, the specified peer is removed from the "Maintained" connections in NettyP2p, and the connection to said peer is disconnected. The peer is not added to the blacklist, on the premise that this behaviour will typically be used when peer-discovery is disabled, thus no future connection to the peer in question will be attempted (except via an AddPeer JSON RPC call). Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>pull/2/head
parent
ca476e700d
commit
695f5fa2e1
@ -0,0 +1,59 @@ |
||||
/* |
||||
* Copyright 2019 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.exception.InvalidJsonRpcParameters; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcError; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.P2pDisabledException; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
public abstract class AdminModifyPeer implements JsonRpcMethod { |
||||
|
||||
protected final JsonRpcParameter parameters; |
||||
protected final P2PNetwork peerNetwork; |
||||
private static final Logger LOG = LogManager.getLogger(); |
||||
|
||||
public AdminModifyPeer(final P2PNetwork peerNetwork, final JsonRpcParameter parameters) { |
||||
this.peerNetwork = peerNetwork; |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
@Override |
||||
public JsonRpcResponse response(final JsonRpcRequest req) { |
||||
if (req.getParamLength() != 1) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_PARAMS); |
||||
} |
||||
try { |
||||
final String enodeString = parameters.required(req.getParams(), 0, String.class); |
||||
return performOperation(req.getId(), enodeString); |
||||
} catch (final InvalidJsonRpcParameters e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.INVALID_PARAMS); |
||||
} catch (final IllegalArgumentException e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.PARSE_ERROR); |
||||
} catch (final P2pDisabledException e) { |
||||
return new JsonRpcErrorResponse(req.getId(), JsonRpcError.P2P_DISABLED); |
||||
} catch (final Exception e) { |
||||
LOG.error(getName() + " - Error processing request: " + req, e); |
||||
throw e; |
||||
} |
||||
} |
||||
|
||||
protected abstract JsonRpcResponse performOperation(final Object id, final String enode); |
||||
} |
@ -0,0 +1,46 @@ |
||||
/* |
||||
* Copyright 2018 ConsenSys AG. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||
* the License. You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||
* specific language governing permissions and limitations under the License. |
||||
*/ |
||||
package tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods; |
||||
|
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.parameters.JsonRpcParameter; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse; |
||||
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
import tech.pegasys.pantheon.ethereum.p2p.api.P2PNetwork; |
||||
import tech.pegasys.pantheon.ethereum.p2p.peers.DefaultPeer; |
||||
import tech.pegasys.pantheon.util.enode.EnodeURL; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
public class AdminRemovePeer extends AdminModifyPeer { |
||||
|
||||
private static final Logger LOG = LogManager.getLogger(); |
||||
|
||||
public AdminRemovePeer(final P2PNetwork peerNetwork, final JsonRpcParameter parameters) { |
||||
super(peerNetwork, parameters); |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "admin_removePeer"; |
||||
} |
||||
|
||||
@Override |
||||
protected JsonRpcResponse performOperation(final Object id, final String enode) { |
||||
LOG.debug("Remove ({}) to peer cache", enode); |
||||
final EnodeURL enodeURL = new EnodeURL(enode); |
||||
final boolean result = |
||||
peerNetwork.removeMaintainedConnectionPeer(DefaultPeer.fromEnodeURL(enodeURL)); |
||||
return new JsonRpcSuccessResponse(id, result); |
||||
} |
||||
} |
Loading…
Reference in new issue