diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java index 397978b519..3f23027ea6 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcTestMethodsFactory.java @@ -49,7 +49,7 @@ public class JsonRpcTestMethodsFactory { this.importer = importer; } - public Map methods(final String chainId) { + public Map methods(final int chainId) { final WorldStateArchive stateArchive = InMemoryTestFixture.createInMemoryWorldStateArchive(); importer.getGenesisConfig().writeStateTo(stateArchive.getMutable(Hash.EMPTY_TRIE_HASH)); diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthCallIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthCallIntegrationTest.java index d82da3bf20..04bd37583a 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthCallIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthCallIntegrationTest.java @@ -37,7 +37,7 @@ import org.junit.Test; public class EthCallIntegrationTest { - private static final String CHAIN_ID = "6986785976597"; + private static final int CHAIN_ID = 123; private static JsonRpcTestMethodsFactory BLOCKCHAIN; private JsonRpcMethod method; diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthEstimateGasIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthEstimateGasIntegrationTest.java index b3937e4755..427f9efbf2 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthEstimateGasIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthEstimateGasIntegrationTest.java @@ -33,7 +33,7 @@ import org.junit.Test; public class EthEstimateGasIntegrationTest { - private static final String CHAIN_ID = "6986785976597"; + private static final int CHAIN_ID = 123; private static JsonRpcTestMethodsFactory BLOCKCHAIN; private JsonRpcMethod method; diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java index d2eec237ec..7f39a1c063 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByHashIntegrationTest.java @@ -58,7 +58,7 @@ public class EthGetBlockByHashIntegrationTest { private Map methods; private static JsonRpcTestMethodsFactory BLOCKCHAIN; 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 JSON_RPC_VERSION = "2.0"; private final String ZERO_HASH = String.valueOf(Hash.ZERO); diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java index 34f54bd6ec..d662cf6e2d 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetBlockByNumberIntegrationTest.java @@ -59,7 +59,7 @@ import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) 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 JSON_RPC_VERSION = "2.0"; private static JsonRpcTestMethodsFactory BLOCKCHAIN; diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java index 226619c9cd..63f77fe4d1 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockHashAndIndexIntegrationTest.java @@ -52,7 +52,7 @@ import org.junit.Test; public class EthGetUncleByBlockHashAndIndexIntegrationTest { - private static final String CHAIN_ID = "6986785976597"; + private static final int CHAIN_ID = 123; private static JsonRpcTestMethodsFactory BLOCKCHAIN; private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils(); diff --git a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java index 6e5ace1e50..46f1f4555c 100644 --- a/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java +++ b/ethereum/jsonrpc/src/integration-test/java/tech/pegasys/pantheon/ethereum/jsonrpc/methods/EthGetUncleByBlockNumberAndIndexIntegrationTest.java @@ -52,7 +52,7 @@ import org.junit.Test; public class EthGetUncleByBlockNumberAndIndexIntegrationTest { - private static final String CHAIN_ID = "6986785976597"; + private static final int CHAIN_ID = 123; private static JsonRpcTestMethodsFactory BLOCKCHAIN; private final JsonRpcResponseUtils responseUtils = new JsonRpcResponseUtils(); diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java index c2e9031ac6..ffb6bf5da9 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcMethodsFactory.java @@ -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.EthBlockNumber; 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.EthEstimateGas; import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.EthGasPrice; @@ -87,7 +88,7 @@ public class JsonRpcMethodsFactory { public Map methods( final String clientVersion, - final String chainId, + final int chainId, final P2PNetwork peerNetworkingService, final Blockchain blockchain, final WorldStateArchive worldStateArchive, @@ -116,7 +117,7 @@ public class JsonRpcMethodsFactory { public Map methods( final String clientVersion, - final String chainId, + final int chainId, final P2PNetwork p2pNetwork, final BlockchainQueries blockchainQueries, final Synchronizer synchronizer, @@ -178,7 +179,8 @@ public class JsonRpcMethodsFactory { new EthCoinbase(miningCoordinator), new EthProtocolVersion(supportedCapabilities), new EthGasPrice(miningCoordinator), - new EthGetWork(miningCoordinator)); + new EthGetWork(miningCoordinator), + new EthChainId(chainId)); } if (rpcApis.contains(RpcApis.DEBUG)) { final BlockReplay blockReplay = diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainId.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainId.java new file mode 100644 index 0000000000..6ed0d39bb2 --- /dev/null +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainId.java @@ -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)); + } +} diff --git a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetVersion.java b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetVersion.java index 7646effe78..ca71ca8a3c 100644 --- a/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetVersion.java +++ b/ethereum/jsonrpc/src/main/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/NetVersion.java @@ -25,8 +25,8 @@ import tech.pegasys.pantheon.ethereum.jsonrpc.internal.response.JsonRpcSuccessRe public class NetVersion implements JsonRpcMethod { private final String chainId; - public NetVersion(final String chainId) { - this.chainId = chainId; + public NetVersion(final int chainId) { + this.chainId = String.valueOf(chainId); } @Override diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java index 6074be9473..cddb041bcd 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/AbstractEthJsonRpcHttpServiceTest.java @@ -92,7 +92,7 @@ public abstract class AbstractEthJsonRpcHttpServiceTest { 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 JSON_RPC_APIS = Arrays.asList(RpcApis.ETH, RpcApis.NET, RpcApis.WEB3); diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java index 774e374daa..dfbb1be539 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java @@ -61,7 +61,7 @@ public class JsonRpcHttpServiceRpcApisTest { private static String baseUrl; 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 NET_VERSION = "6986785976597"; + private static final int NET_VERSION = 123; private JsonRpcConfiguration configuration; @Mock protected static BlockchainQueries blockchainQueries; diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceTest.java index ce792c03f9..926132e856 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceTest.java @@ -87,7 +87,7 @@ public class JsonRpcHttpServiceTest { protected static String baseUrl; 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 NET_VERSION = "6986785976597"; + protected static final int NET_VERSION = 123; protected static P2PNetwork peerDiscoveryMock; protected static BlockchainQueries blockchainQueries; protected static Synchronizer synchronizer; @@ -266,7 +266,7 @@ public class JsonRpcHttpServiceTest { testHelper.assertValidJsonRpcResult(json, id); // Check 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 final JsonObject jsonNetVersion = responses.get(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 final JsonObject jsonNetVersion = responses.get(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 final JsonObject jsonNetVersion = responses.get(netVersionRequestId); testHelper.assertValidJsonRpcResult(jsonNetVersion, netVersionRequestId); - assertThat(jsonNetVersion.getString("result")).isEqualTo(NET_VERSION); + assertThat(jsonNetVersion.getString("result")).isEqualTo(String.valueOf(NET_VERSION)); } } diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainIdTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainIdTest.java new file mode 100644 index 0000000000..d6bfffd9d1 --- /dev/null +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/internal/methods/EthChainIdTest.java @@ -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); + } +} diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java b/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java index b81ede3c6e..dacdaf3e71 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/RunnerBuilder.java @@ -224,7 +224,7 @@ public class RunnerBuilder { new JsonRpcMethodsFactory() .methods( PantheonInfo.version(), - String.valueOf(pantheonController.getGenesisConfig().getChainId()), + pantheonController.getGenesisConfig().getChainId(), networkRunner.getNetwork(), context.getBlockchain(), context.getWorldStateArchive(),