Implementing eth_chainId (fixes #206) (#219)

Lucas Saldanha 6 years ago committed by GitHub
parent 925f9d23cb
commit c71944f19b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java
  2. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthCallIntegrationTest.java
  3. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthEstimateGasIntegrationTest.java
  4. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java
  5. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java
  6. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java
  7. 2
      ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java
  8. 8
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java
  9. 37
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainId.java
  10. 4
      ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetVersion.java
  11. 2
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java
  12. 2
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java
  13. 10
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceTest.java
  14. 52
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainIdTest.java
  15. 2
      pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java

@ -49,7 +49,7 @@ public class JsonRpcTestMethodsFactory {
this.importer = importer; this.importer = importer;
} }
public Map<String, JsonRpcMethod> methods(final String chainId) { public Map<String, JsonRpcMethod> methods(final int chainId) {
final WorldStateArchive stateArchive = InMemoryTestFixture.createInMemoryWorldStateArchive(); final WorldStateArchive stateArchive = InMemoryTestFixture.createInMemoryWorldStateArchive();
importer.getGenesisConfig().writeStateTo(stateArchive.getMutable(Hash.EMPTY_TRIE_HASH)); importer.getGenesisConfig().writeStateTo(stateArchive.getMutable(Hash.EMPTY_TRIE_HASH));

@ -37,7 +37,7 @@ import org.junit.Test;
public class EthCallIntegrationTest { public class EthCallIntegrationTest {
private static final String CHAIN_ID = "6986785976597"; private static final int CHAIN_ID = 123;
private static JsonRpcTestMethodsFactory BLOCKCHAIN; private static JsonRpcTestMethodsFactory BLOCKCHAIN;
private JsonRpcMethod method; private JsonRpcMethod method;

@ -33,7 +33,7 @@ import org.junit.Test;
public class EthEstimateGasIntegrationTest { public class EthEstimateGasIntegrationTest {
private static final String CHAIN_ID = "6986785976597"; private static final int CHAIN_ID = 123;
private static JsonRpcTestMethodsFactory BLOCKCHAIN; private static JsonRpcTestMethodsFactory BLOCKCHAIN;
private JsonRpcMethod method; private JsonRpcMethod method;

@ -58,7 +58,7 @@ public class EthGetBlockByHashIntegrationTest {
private Map<String, JsonRpcMethod> methods; private Map<String, JsonRpcMethod> methods;
private static JsonRpcTestMethodsFactory BLOCKCHAIN; private static JsonRpcTestMethodsFactory BLOCKCHAIN;
private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils(); private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils();
private static final String CHAIN_ID = "6986785976597"; private static final int CHAIN_ID = 123;
private final String ETH_METHOD = "eth_getBlockByHash"; private final String ETH_METHOD = "eth_getBlockByHash";
private final String JSON_RPC_VERSION = "2.0"; private final String JSON_RPC_VERSION = "2.0";
private final String ZERO_HASH = String.valueOf(Hash.ZERO); private final String ZERO_HASH = String.valueOf(Hash.ZERO);

@ -59,7 +59,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class EthGetBlockByNumberIntegrationTest { public class EthGetBlockByNumberIntegrationTest {
private static final String CHAIN_ID = "6986785976597"; private static final int CHAIN_ID = 123;
private static final String ETH_METHOD = "eth_getBlockByNumber"; private static final String ETH_METHOD = "eth_getBlockByNumber";
private static final String JSON_RPC_VERSION = "2.0"; private static final String JSON_RPC_VERSION = "2.0";
private static JsonRpcTestMethodsFactory BLOCKCHAIN; private static JsonRpcTestMethodsFactory BLOCKCHAIN;

@ -52,7 +52,7 @@ import org.junit.Test;
public class EthGetUncleByBlockHashAndIndexIntegrationTest { public class EthGetUncleByBlockHashAndIndexIntegrationTest {
private static final String CHAIN_ID = "6986785976597"; private static final int CHAIN_ID = 123;
private static JsonRpcTestMethodsFactory BLOCKCHAIN; private static JsonRpcTestMethodsFactory BLOCKCHAIN;
private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils(); private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils();

@ -52,7 +52,7 @@ import org.junit.Test;
public class EthGetUncleByBlockNumberAndIndexIntegrationTest { public class EthGetUncleByBlockNumberAndIndexIntegrationTest {
private static final String CHAIN_ID = "6986785976597"; private static final int CHAIN_ID = 123;
private static JsonRpcTestMethodsFactory BLOCKCHAIN; private static JsonRpcTestMethodsFactory BLOCKCHAIN;
private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils(); private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils();

@ -24,6 +24,7 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.DebugTraceTransac
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthAccounts; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthAccounts;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthBlockNumber; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthBlockNumber;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthCall; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthCall;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthChainId;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthCoinbase; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthCoinbase;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthEstimateGas; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthEstimateGas;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthGasPrice; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthGasPrice;
@ -87,7 +88,7 @@ public class JsonRpcMethodsFactory {
public Map<String, JsonRpcMethod> methods( public Map<String, JsonRpcMethod> methods(
final String clientVersion, final String clientVersion,
final String chainId, final int chainId,
final P2PNetwork peerNetworkingService, final P2PNetwork peerNetworkingService,
final Blockchain blockchain, final Blockchain blockchain,
final WorldStateArchive worldStateArchive, final WorldStateArchive worldStateArchive,
@ -116,7 +117,7 @@ public class JsonRpcMethodsFactory {
public Map<String, JsonRpcMethod> methods( public Map<String, JsonRpcMethod> methods(
final String clientVersion, final String clientVersion,
final String chainId, final int chainId,
final P2PNetwork p2pNetwork, final P2PNetwork p2pNetwork,
final BlockchainQueries blockchainQueries, final BlockchainQueries blockchainQueries,
final Synchronizer synchronizer, final Synchronizer synchronizer,
@ -178,7 +179,8 @@ public class JsonRpcMethodsFactory {
new EthCoinbase(miningCoordinator), new EthCoinbase(miningCoordinator),
new EthProtocolVersion(supportedCapabilities), new EthProtocolVersion(supportedCapabilities),
new EthGasPrice(miningCoordinator), new EthGasPrice(miningCoordinator),
new EthGetWork(miningCoordinator)); new EthGetWork(miningCoordinator),
new EthChainId(chainId));
} }
if (rpcApis.contains(RpcApis.DEBUG)) { if (rpcApis.contains(RpcApis.DEBUG)) {
final BlockReplay blockReplay = final BlockReplay blockReplay =

@ -0,0 +1,37 @@
/*
* 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.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.Quantity;
public class EthChainId implements JsonRpcMethod {
private final int chainId;
public EthChainId(final int chainId) {
this.chainId = chainId;
}
@Override
public String getName() {
return "eth_chainId";
}
@Override
public JsonRpcResponse response(final JsonRpcRequest req) {
return new JsonRpcSuccessResponse(req.getId(), Quantity.create(chainId));
}
}

@ -25,8 +25,8 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessRe
public class NetVersion implements JsonRpcMethod { public class NetVersion implements JsonRpcMethod {
private final String chainId; private final String chainId;
public NetVersion(final String chainId) { public NetVersion(final int chainId) {
this.chainId = chainId; this.chainId = String.valueOf(chainId);
} }
@Override @Override

@ -92,7 +92,7 @@ public abstract class AbstractEthJsonRpcHttpServiceTest {
protected final String CLIENT_VERSION = "TestClientVersion/0.1.0"; protected final String CLIENT_VERSION = "TestClientVersion/0.1.0";
protected final String NET_VERSION = "6986785976597"; protected final int NET_VERSION = 123;
protected static final Collection<RpcApi> JSON_RPC_APIS = protected static final Collection<RpcApi> JSON_RPC_APIS =
Arrays.asList(RpcApis.ETH, RpcApis.NET, RpcApis.WEB3); Arrays.asList(RpcApis.ETH, RpcApis.NET, RpcApis.WEB3);

@ -61,7 +61,7 @@ public class JsonRpcHttpServiceRpcApisTest {
private static String baseUrl; private static String baseUrl;
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
private static final String CLIENT_VERSION = "TestClientVersion/0.1.0"; private static final String CLIENT_VERSION = "TestClientVersion/0.1.0";
private static final String NET_VERSION = "6986785976597"; private static final int NET_VERSION = 123;
private JsonRpcConfiguration configuration; private JsonRpcConfiguration configuration;
@Mock protected static BlockchainQueries blockchainQueries; @Mock protected static BlockchainQueries blockchainQueries;

@ -87,7 +87,7 @@ public class JsonRpcHttpServiceTest {
protected static String baseUrl; protected static String baseUrl;
protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); protected static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
protected static final String CLIENT_VERSION = "TestClientVersion/0.1.0"; protected static final String CLIENT_VERSION = "TestClientVersion/0.1.0";
protected static final String NET_VERSION = "6986785976597"; protected static final int NET_VERSION = 123;
protected static P2PNetwork peerDiscoveryMock; protected static P2PNetwork peerDiscoveryMock;
protected static BlockchainQueries blockchainQueries; protected static BlockchainQueries blockchainQueries;
protected static Synchronizer synchronizer; protected static Synchronizer synchronizer;
@ -266,7 +266,7 @@ public class JsonRpcHttpServiceTest {
testHelper.assertValidJsonRpcResult(json, id); testHelper.assertValidJsonRpcResult(json, id);
// Check result // Check result
final String result = json.getString("result"); final String result = json.getString("result");
assertThat(result).isEqualTo(NET_VERSION); assertThat(result).isEqualTo(String.valueOf(NET_VERSION));
} }
} }
@ -1544,7 +1544,7 @@ public class JsonRpcHttpServiceTest {
// Check result net_version // Check result net_version
final JsonObject jsonNetVersion = responses.get(netVersionRequestId); final JsonObject jsonNetVersion = responses.get(netVersionRequestId);
testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId); testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId);
assertThat(jsonNetVersion.getString("result")).isEqualTo(NET_VERSION); assertThat(jsonNetVersion.getString("result")).isEqualTo(String.valueOf(NET_VERSION));
} }
} }
@ -1600,7 +1600,7 @@ public class JsonRpcHttpServiceTest {
// Check result net_version // Check result net_version
final JsonObject jsonNetVersion = responses.get(netVersionRequestId); final JsonObject jsonNetVersion = responses.get(netVersionRequestId);
testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId); testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId);
assertThat(jsonNetVersion.getString("result")).isEqualTo(NET_VERSION); assertThat(jsonNetVersion.getString("result")).isEqualTo(String.valueOf(NET_VERSION));
} }
} }
@ -1661,7 +1661,7 @@ public class JsonRpcHttpServiceTest {
// Check result net_version // Check result net_version
final JsonObject jsonNetVersion = responses.get(netVersionRequestId); final JsonObject jsonNetVersion = responses.get(netVersionRequestId);
testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId); testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId);
assertThat(jsonNetVersion.getString("result")).isEqualTo(NET_VERSION); assertThat(jsonNetVersion.getString("result")).isEqualTo(String.valueOf(NET_VERSION));
} }
} }

@ -0,0 +1,52 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.JsonRpcRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessResponse;
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.results.Quantity;
import org.junit.Before;
import org.junit.Test;
public class EthChainIdTest {
private EthChainId method;
private final int CHAIN_ID = 1;
@Before
public void setUp() {
method = new EthChainId(CHAIN_ID);
}
@Test
public void shouldReturnCorrectMethodName() {
assertThat(method.getName()).isEqualTo("eth_chainId");
}
@Test
public void shouldReturnChainId() {
JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Quantity.create(CHAIN_ID));
JsonRpcResponse response = method.response(request());
assertThat(response).isEqualToComparingFieldByField(expectedResponse);
}
private JsonRpcRequest request() {
return new JsonRpcRequest(null, "eth_chainId", null);
}
}

@ -224,7 +224,7 @@ public class RunnerBuilder {
new JsonRpcMethodsFactory() new JsonRpcMethodsFactory()
.methods( .methods(
PantheonInfo.version(), PantheonInfo.version(),
String.valueOf(pantheonController.getGenesisConfig().getChainId()), pantheonController.getGenesisConfig().getChainId(),
networkRunner.getNetwork(), networkRunner.getNetwork(),
context.getBlockchain(), context.getBlockchain(),
context.getWorldStateArchive(), context.getWorldStateArchive(),

Loading…
Cancel
Save