mirror of https://github.com/hyperledger/besu
In process RPC service (#7395)
Signed-off-by: Fabio Di Fabio <fabio.difabio@consensys.net>pull/7416/head
parent
9d92ae87df
commit
a7ab1773e4
@ -0,0 +1,42 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.tests.acceptance.dsl.transaction.miner; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
import org.hyperledger.besu.tests.acceptance.dsl.transaction.NodeRequests; |
||||||
|
import org.hyperledger.besu.tests.acceptance.dsl.transaction.Transaction; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.math.BigInteger; |
||||||
|
|
||||||
|
import org.web3j.protocol.core.methods.response.EthGasPrice; |
||||||
|
|
||||||
|
public class MinerGetMinGasPriceTransaction implements Transaction<BigInteger> { |
||||||
|
|
||||||
|
@Override |
||||||
|
public BigInteger execute(final NodeRequests node) { |
||||||
|
try { |
||||||
|
final EthGasPrice result = node.miner().minerGetMinGasPrice().send(); |
||||||
|
assertThat(result).isNotNull(); |
||||||
|
assertThat(result.hasError()).isFalse(); |
||||||
|
|
||||||
|
return result.getGasPrice(); |
||||||
|
|
||||||
|
} catch (final IOException e) { |
||||||
|
throw new RuntimeException(e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,81 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.tests.acceptance.plugins; |
||||||
|
|
||||||
|
import org.hyperledger.besu.datatypes.Wei; |
||||||
|
import org.hyperledger.besu.plugin.BesuContext; |
||||||
|
import org.hyperledger.besu.plugin.BesuPlugin; |
||||||
|
import org.hyperledger.besu.plugin.services.PicoCLIOptions; |
||||||
|
import org.hyperledger.besu.plugin.services.RpcEndpointService; |
||||||
|
import org.hyperledger.besu.plugin.services.rpc.RpcResponseType; |
||||||
|
|
||||||
|
import com.google.auto.service.AutoService; |
||||||
|
import org.slf4j.Logger; |
||||||
|
import org.slf4j.LoggerFactory; |
||||||
|
import picocli.CommandLine; |
||||||
|
|
||||||
|
@AutoService(BesuPlugin.class) |
||||||
|
public class TestInProcessRpcServicePlugin implements BesuPlugin { |
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(TestInProcessRpcServicePlugin.class); |
||||||
|
|
||||||
|
private RpcEndpointService rpcEndpointService; |
||||||
|
|
||||||
|
@CommandLine.Option(names = {"--plugin-test-set-min-gas-price"}) |
||||||
|
long minGasPrice = -1; |
||||||
|
|
||||||
|
@Override |
||||||
|
public void register(final BesuContext context) { |
||||||
|
final PicoCLIOptions cmdlineOptions = |
||||||
|
context |
||||||
|
.getService(PicoCLIOptions.class) |
||||||
|
.orElseThrow( |
||||||
|
() -> |
||||||
|
new IllegalStateException( |
||||||
|
"Failed to obtain PicoCLI options from the BesuContext")); |
||||||
|
|
||||||
|
cmdlineOptions.addPicoCLIOptions("test", this); |
||||||
|
|
||||||
|
rpcEndpointService = |
||||||
|
context |
||||||
|
.getService(RpcEndpointService.class) |
||||||
|
.orElseThrow( |
||||||
|
() -> |
||||||
|
new RuntimeException( |
||||||
|
"Failed to obtain RpcEndpointService from the BesuContext.")); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void start() { |
||||||
|
LOG.info("TestInProcessRpcServicePlugin minGasPrice option: {}", minGasPrice); |
||||||
|
if (minGasPrice >= 0) { |
||||||
|
callSetMinGasPrice(minGasPrice); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void stop() {} |
||||||
|
|
||||||
|
private void callSetMinGasPrice(final long minGasPrice) { |
||||||
|
LOG.info("Setting minGasPrice via in-process RPC service"); |
||||||
|
final var minGasPriceWei = Wei.of(minGasPrice); |
||||||
|
final var resp = |
||||||
|
rpcEndpointService.call( |
||||||
|
"miner_setMinGasPrice", new Object[] {minGasPriceWei.toShortHexString()}); |
||||||
|
LOG.info("miner_setMinGasPrice response: {}", resp); |
||||||
|
if (!resp.getType().equals(RpcResponseType.SUCCESS)) { |
||||||
|
throw new RuntimeException("Internal setMinGasPrice method failed: " + resp); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,51 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.tests.acceptance.plugins; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
import org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase; |
||||||
|
import org.hyperledger.besu.tests.acceptance.dsl.node.BesuNode; |
||||||
|
|
||||||
|
import java.math.BigInteger; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
public class InProcessRpcServicePluginTest extends AcceptanceTestBase { |
||||||
|
private static final long MIN_GAS_PRICE = 123456; |
||||||
|
private BesuNode node; |
||||||
|
|
||||||
|
@BeforeEach |
||||||
|
public void setUp() throws Exception { |
||||||
|
node = |
||||||
|
besu.createPluginsNode( |
||||||
|
"node1", |
||||||
|
List.of("testPlugins"), |
||||||
|
List.of( |
||||||
|
"--Xin-process-rpc-enabled=true", |
||||||
|
"--Xin-process-rpc-apis=MINER", |
||||||
|
"--plugin-test-set-min-gas-price=" + MIN_GAS_PRICE), |
||||||
|
"MINER"); |
||||||
|
cluster.start(node); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void smokeTest() { |
||||||
|
final var currMinGasPrice = node.execute(minerTransactions.minerGetMinGasPrice()); |
||||||
|
assertThat(currMinGasPrice).isEqualTo(BigInteger.valueOf(MIN_GAS_PRICE)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,73 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.cli.options.unstable; |
||||||
|
|
||||||
|
import static org.hyperledger.besu.ethereum.api.jsonrpc.InProcessRpcConfiguration.DEFAULT_IN_PROCESS_RPC_APIS; |
||||||
|
import static org.hyperledger.besu.ethereum.api.jsonrpc.InProcessRpcConfiguration.DEFAULT_IN_PROCESS_RPC_ENABLED; |
||||||
|
|
||||||
|
import org.hyperledger.besu.cli.options.CLIOptions; |
||||||
|
import org.hyperledger.besu.cli.util.CommandLineUtils; |
||||||
|
import org.hyperledger.besu.ethereum.api.jsonrpc.ImmutableInProcessRpcConfiguration; |
||||||
|
import org.hyperledger.besu.ethereum.api.jsonrpc.InProcessRpcConfiguration; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import picocli.CommandLine; |
||||||
|
|
||||||
|
/** The in process RPC options. */ |
||||||
|
public class InProcessRpcOptions implements CLIOptions<InProcessRpcConfiguration> { |
||||||
|
|
||||||
|
/** Default constructor. */ |
||||||
|
InProcessRpcOptions() {} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create ipc options. |
||||||
|
* |
||||||
|
* @return the ipc options |
||||||
|
*/ |
||||||
|
public static InProcessRpcOptions create() { |
||||||
|
return new InProcessRpcOptions(); |
||||||
|
} |
||||||
|
|
||||||
|
@CommandLine.Option( |
||||||
|
names = {"--Xin-process-rpc-enabled"}, |
||||||
|
hidden = true, |
||||||
|
description = "Set to enalbe in-process RPC method call service (default: ${DEFAULT-VALUE})") |
||||||
|
private final Boolean enabled = DEFAULT_IN_PROCESS_RPC_ENABLED; |
||||||
|
|
||||||
|
@CommandLine.Option( |
||||||
|
names = {"--Xin-process-rpc-api", "--Xin-process-rpc-apis"}, |
||||||
|
hidden = true, |
||||||
|
paramLabel = "<api name>", |
||||||
|
split = " {0,1}, {0,1}", |
||||||
|
arity = "1..*", |
||||||
|
description = |
||||||
|
"Comma separated list of APIs to enable on in-process RPC method call service (default: ${DEFAULT-VALUE})") |
||||||
|
private final Set<String> inProcessRpcApis = DEFAULT_IN_PROCESS_RPC_APIS; |
||||||
|
|
||||||
|
@Override |
||||||
|
public InProcessRpcConfiguration toDomainObject() { |
||||||
|
return ImmutableInProcessRpcConfiguration.builder() |
||||||
|
.isEnabled(enabled) |
||||||
|
.inProcessRpcApis(inProcessRpcApis) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<String> getCLIOptions() { |
||||||
|
return CommandLineUtils.getCLIOptions(this, new InProcessRpcOptions()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.ethereum.api.jsonrpc; |
||||||
|
|
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import org.immutables.value.Value; |
||||||
|
|
||||||
|
@Value.Immutable |
||||||
|
public interface InProcessRpcConfiguration { |
||||||
|
boolean DEFAULT_IN_PROCESS_RPC_ENABLED = false; |
||||||
|
Set<String> DEFAULT_IN_PROCESS_RPC_APIS = new HashSet<>(RpcApis.DEFAULT_RPC_APIS); |
||||||
|
|
||||||
|
@Value.Default |
||||||
|
default boolean isEnabled() { |
||||||
|
return DEFAULT_IN_PROCESS_RPC_ENABLED; |
||||||
|
} |
||||||
|
|
||||||
|
@Value.Default |
||||||
|
default Set<String> getInProcessRpcApis() { |
||||||
|
return DEFAULT_IN_PROCESS_RPC_APIS; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,27 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.plugin.services.rpc; |
||||||
|
|
||||||
|
/** The interface Plugin rpc response. */ |
||||||
|
public interface PluginRpcResponse extends RpcResponse { |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the result, unfortunately there is no typing yet, so call must know how to interact with |
||||||
|
* the response |
||||||
|
* |
||||||
|
* @return the result |
||||||
|
*/ |
||||||
|
Object getResult(); |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
/* |
||||||
|
* Copyright contributors to Hyperledger Besu. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
* SPDX-License-Identifier: Apache-2.0 |
||||||
|
*/ |
||||||
|
package org.hyperledger.besu.plugin.services.rpc; |
||||||
|
|
||||||
|
/** Represent a Json RPC response */ |
||||||
|
public interface RpcResponse { |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the response type |
||||||
|
* |
||||||
|
* @return the response type |
||||||
|
*/ |
||||||
|
RpcResponseType getType(); |
||||||
|
} |
Loading…
Reference in new issue