mirror of https://github.com/hyperledger/besu
add eth_getQuorumPayload (#2470)
* add eth_getQuorumPayload Signed-off-by: Stefan Pingel <stefan.pingel@consensys.net>pull/2475/head
parent
b55b076a91
commit
fd18b0a6fa
@ -0,0 +1,67 @@ |
||||
/* |
||||
* Copyright 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. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv; |
||||
|
||||
import static org.apache.logging.log4j.LogManager.getLogger; |
||||
|
||||
import org.hyperledger.besu.enclave.GoQuorumEnclave; |
||||
import org.hyperledger.besu.enclave.types.GoQuorumReceiveResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
|
||||
import org.apache.logging.log4j.Logger; |
||||
import org.apache.tuweni.bytes.Bytes; |
||||
|
||||
public class GoQuorumEthGetQuorumPayload implements JsonRpcMethod { |
||||
|
||||
private static final Logger LOG = getLogger(); |
||||
|
||||
private final GoQuorumEnclave enclave; |
||||
|
||||
public GoQuorumEthGetQuorumPayload(final GoQuorumEnclave enclave) { |
||||
this.enclave = enclave; |
||||
} |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return RpcMethod.GOQUORUM_ETH_GET_QUORUM_PAYLOAD.getMethodName(); |
||||
} |
||||
|
||||
@Override |
||||
public JsonRpcResponse response(final JsonRpcRequestContext requestContext) { |
||||
final String key = requestContext.getRequiredParameter(0, String.class); |
||||
final Bytes bytes; |
||||
try { |
||||
bytes = Bytes.fromHexString(key); |
||||
} catch (final IllegalArgumentException e) { |
||||
LOG.debug("Enclave key contains invalid hex character {}", key); |
||||
return new JsonRpcErrorResponse( |
||||
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS); |
||||
} |
||||
if (bytes.size() != 64) { |
||||
LOG.debug("Enclave key expected length 64, but length is {}", bytes.size()); |
||||
return new JsonRpcErrorResponse( |
||||
requestContext.getRequest().getId(), JsonRpcError.INVALID_PARAMS); |
||||
} |
||||
final GoQuorumReceiveResponse receive = enclave.receive(bytes.toBase64String()); |
||||
return new JsonRpcSuccessResponse( |
||||
requestContext.getRequest().getId(), Bytes.wrap(receive.getPayload()).toHexString()); |
||||
} |
||||
} |
@ -0,0 +1,133 @@ |
||||
/* |
||||
* Copyright 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. |
||||
* |
||||
* SPDX-License-Identifier: Apache-2.0 |
||||
*/ |
||||
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.goquorum; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy; |
||||
import static org.mockito.ArgumentMatchers.any; |
||||
import static org.mockito.Mockito.when; |
||||
|
||||
import org.hyperledger.besu.enclave.GoQuorumEnclave; |
||||
import org.hyperledger.besu.enclave.types.GoQuorumReceiveResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.exception.InvalidJsonRpcParameters; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.GoQuorumEthGetQuorumPayload; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse; |
||||
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse; |
||||
|
||||
import org.apache.tuweni.bytes.Bytes; |
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.junit.MockitoJUnitRunner; |
||||
|
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class GoQuorumEthGetQuorumPayloadTest { |
||||
|
||||
@Mock GoQuorumEthGetQuorumPayload method; |
||||
@Mock GoQuorumEnclave enclave; |
||||
|
||||
@Before |
||||
public void before() { |
||||
method = new GoQuorumEthGetQuorumPayload(enclave); |
||||
} |
||||
|
||||
@Test |
||||
public void correctRequest() { |
||||
final String hexString = Bytes.wrap(new byte[64]).toHexString(); |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("2.0", "eth_getQuorumPayload", new String[] {hexString})); |
||||
|
||||
when(enclave.receive(any())) |
||||
.thenReturn(new GoQuorumReceiveResponse(new byte[10], 0, null, null)); |
||||
|
||||
final JsonRpcResponse response = method.response(request); |
||||
assertThat(response).isInstanceOf(JsonRpcSuccessResponse.class); |
||||
assertThat(((JsonRpcSuccessResponse) response).getResult().toString()) |
||||
.isEqualTo("0x00000000000000000000"); |
||||
} |
||||
|
||||
@Test |
||||
public void requestIsMissingParameter() { |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("2.0", "eth_getQuorumPayload", new String[] {})); |
||||
|
||||
assertThatThrownBy(() -> method.response(request)) |
||||
.isInstanceOf(InvalidJsonRpcParameters.class) |
||||
.hasMessage("Missing required json rpc parameter at index 0"); |
||||
} |
||||
|
||||
@Test |
||||
public void requestHasNullObjectParameter() { |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext(new JsonRpcRequest("2.0", "eth_getQuorumPayload", null)); |
||||
|
||||
assertThatThrownBy(() -> method.response(request)) |
||||
.isInstanceOf(InvalidJsonRpcParameters.class) |
||||
.hasMessage("Missing required json rpc parameter at index 0"); |
||||
} |
||||
|
||||
@Test |
||||
public void requestHasNullArrayParameter() { |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("2.0", "eth_getQuorumPayload", new String[] {null})); |
||||
|
||||
assertThatThrownBy(() -> method.response(request)) |
||||
.isInstanceOf(InvalidJsonRpcParameters.class) |
||||
.hasMessage("Missing required json rpc parameter at index 0"); |
||||
} |
||||
|
||||
@Test |
||||
public void requestHasShortHex() { |
||||
final String hexString = Bytes.wrap(new byte[63]).toHexString(); |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("2.0", "eth_getQuorumPayload", new String[] {hexString})); |
||||
|
||||
final JsonRpcResponse response = method.response(request); |
||||
assertThat(response).isInstanceOf(JsonRpcErrorResponse.class); |
||||
assertThat(response.toString()).contains("INVALID_PARAMS"); |
||||
} |
||||
|
||||
@Test |
||||
public void requestHasLongHex() { |
||||
final String hexString = Bytes.wrap(new byte[65]).toHexString(); |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("2.0", "eth_getQuorumPayload", new String[] {hexString})); |
||||
|
||||
final JsonRpcResponse response = method.response(request); |
||||
assertThat(response).isInstanceOf(JsonRpcErrorResponse.class); |
||||
assertThat(response.toString()).contains("INVALID_PARAMS"); |
||||
} |
||||
|
||||
@Test |
||||
public void requestNonHexString() { |
||||
final String hexString = Bytes.wrap(new byte[63]).toHexString() + "f"; |
||||
final JsonRpcRequestContext request = |
||||
new JsonRpcRequestContext( |
||||
new JsonRpcRequest("2.0", "eth_getQuorumPayload", new String[] {hexString})); |
||||
|
||||
final JsonRpcResponse response = method.response(request); |
||||
assertThat(response).isInstanceOf(JsonRpcErrorResponse.class); |
||||
assertThat(response.toString()).contains("INVALID_PARAMS"); |
||||
} |
||||
} |
Loading…
Reference in new issue