diff --git a/actuator/src/main/java/org/tron/core/vm/program/Program.java b/actuator/src/main/java/org/tron/core/vm/program/Program.java index 4de24d7cf4d..fc22f708e5f 100644 --- a/actuator/src/main/java/org/tron/core/vm/program/Program.java +++ b/actuator/src/main/java/org/tron/core/vm/program/Program.java @@ -1245,8 +1245,11 @@ public DataWord getCallerAddress() { } public DataWord getChainId() { - return new DataWord(Hex.toHexString(getContractState() - .getBlockByNum(0).getBlockId().getBytes())); + byte[] chainId = getContractState().getBlockByNum(0).getBlockId().getBytes(); + if (VMConfig.allowTvmCompatibleEvm()) { + chainId = Arrays.copyOfRange(chainId, chainId.length - 4, chainId.length); + } + return new DataWord(chainId); } public DataWord getDropPrice() { return new DataWord(1); diff --git a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java index a561e5d5576..31715937eae 100644 --- a/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java +++ b/framework/src/main/java/org/tron/core/services/jsonrpc/TronJsonRpcImpl.java @@ -239,7 +239,8 @@ public String getNetVersion() throws JsonRpcInternalException { public String ethChainId() throws JsonRpcInternalException { // return hash of genesis block try { - return ByteArray.toJsonHex(wallet.getBlockCapsuleByNum(0).getBlockId().getBytes()); + byte[] chainId = wallet.getBlockCapsuleByNum(0).getBlockId().getBytes(); + return ByteArray.toJsonHex(Arrays.copyOfRange(chainId, chainId.length - 4, chainId.length)); } catch (Exception e) { throw new JsonRpcInternalException(e.getMessage()); } diff --git a/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmCompatibleEvmTest.java b/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmCompatibleEvmTest.java index cba9ef7d7a5..0b24d357a8a 100644 --- a/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmCompatibleEvmTest.java +++ b/framework/src/test/java/org/tron/common/runtime/vm/AllowTvmCompatibleEvmTest.java @@ -6,6 +6,8 @@ import java.util.Collections; import lombok.extern.slf4j.Slf4j; import org.bouncycastle.util.encoders.Hex; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; import org.testng.Assert; import org.tron.common.runtime.TVMTestResult; @@ -35,9 +37,8 @@ function getRipemd160() public view returns(bytes32 output) { } }*/ - @Test - public void testEthRipemd160() throws ContractExeException, ReceiptCheckErrException, - VMIllegalException, ContractValidateException { + @BeforeClass + public static void beforeClass() { ConfigLoader.disable = true; VMConfig.initAllowTvmTransferTrc10(1); VMConfig.initAllowTvmConstantinople(1); @@ -45,8 +46,11 @@ public void testEthRipemd160() throws ContractExeException, ReceiptCheckErrExcep VMConfig.initAllowTvmIstanbul(1); VMConfig.initAllowTvmLondon(1); VMConfig.initAllowTvmCompatibleEvm(1); - manager.getDynamicPropertiesStore().saveChangeDelegation(1); + } + @Test + public void testEthRipemd160() throws ContractExeException, ReceiptCheckErrException, + VMIllegalException, ContractValidateException { String contractName = "testEthRipemd160"; byte[] address = Hex.decode(OWNER_ADDRESS); String abi = "[{\"inputs\":[],\"name\":\"getRipemd160\"," @@ -133,15 +137,6 @@ function callF() public view returns (bytes32[2] memory) { @Test public void testBlake2f() throws ContractExeException, ReceiptCheckErrException, VMIllegalException, ContractValidateException { - ConfigLoader.disable = true; - VMConfig.initAllowTvmTransferTrc10(1); - VMConfig.initAllowTvmConstantinople(1); - VMConfig.initAllowTvmSolidity059(1); - VMConfig.initAllowTvmIstanbul(1); - VMConfig.initAllowTvmLondon(1); - VMConfig.initAllowTvmCompatibleEvm(1); - manager.getDynamicPropertiesStore().saveChangeDelegation(1); - String contractName = "testBlake2f"; byte[] address = Hex.decode(OWNER_ADDRESS); String abi = "[{\"inputs\":[{\"internalType\":\"uint32\",\"name\":\"rounds\"," @@ -230,15 +225,6 @@ function getprice() public view returns(uint) { @Test public void testGasPrice() throws ContractExeException, ReceiptCheckErrException, VMIllegalException, ContractValidateException { - ConfigLoader.disable = true; - VMConfig.initAllowTvmTransferTrc10(1); - VMConfig.initAllowTvmConstantinople(1); - VMConfig.initAllowTvmSolidity059(1); - VMConfig.initAllowTvmIstanbul(1); - VMConfig.initAllowTvmLondon(1); - VMConfig.initAllowTvmCompatibleEvm(1); - manager.getDynamicPropertiesStore().saveChangeDelegation(1); - String contractName = "testGasPrice"; byte[] address = Hex.decode(OWNER_ADDRESS); String abi = "[{\"inputs\":[],\"name\":\"getprice\"," @@ -275,4 +261,51 @@ public void testGasPrice() throws ContractExeException, ReceiptCheckErrException longTo32Bytes(manager.getDynamicPropertiesStore().getEnergyFee())); } + @Test + public void testChainId() throws ContractExeException, ReceiptCheckErrException, + VMIllegalException, ContractValidateException { + String contractName = "TestChainId"; + byte[] address = Hex.decode(OWNER_ADDRESS); + String contractCode = "608060405234801561001057600080fd5b5060b58061001f6000" + + "396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c" + + "80633408e47014602d575b600080fd5b60336047565b604051603e9190605c565b6040" + + "5180910390f35b600046905090565b6056816075565b82525050565b60006020820190" + + "50606f6000830184604f565b92915050565b600081905091905056fea2646970667358" + + "2212203ccbe28f012f703b4369308e34d6dfc1a89a5f51e3ea42d531fcf3a2dba31150" + + "64736f6c63430008070033"; + long feeLimit = 100_000_000L; + + // deploy contract + Protocol.Transaction trx = TvmTestUtils.generateDeploySmartContractAndGetTransaction( + contractName, address, "[]", contractCode, 0, feeLimit, 0, null); + byte[] factoryAddress = WalletUtil.generateContractAddress(trx); + runtime = TvmTestUtils.processTransactionAndReturnRuntime(trx, rootDeposit, null); + Assert.assertNull(runtime.getRuntimeError()); + + // Trigger contract method: getChainId() + String methodSignature = "getChainId()"; + String hexInput = + AbiUtil.parseMethod(methodSignature, Collections.singletonList("")); + + TVMTestResult result = TvmTestUtils + .triggerContractAndReturnTvmTestResult(Hex.decode(OWNER_ADDRESS), + factoryAddress, Hex.decode(hexInput), 0, feeLimit, manager, null); + byte[] returnValue = result.getRuntime().getResult().getHReturn(); + Assert.assertNull(result.getRuntime().getRuntimeError()); + Assert.assertEquals(Hex.toHexString(returnValue), + "0000000000000000000000000000000000000000000000000000000028c12d1e"); + + VMConfig.initAllowTvmCompatibleEvm(0); + } + + @AfterClass + public static void afterClass() { + ConfigLoader.disable = false; + VMConfig.initAllowTvmTransferTrc10(0); + VMConfig.initAllowTvmConstantinople(0); + VMConfig.initAllowTvmSolidity059(0); + VMConfig.initAllowTvmIstanbul(0); + VMConfig.initAllowTvmCompatibleEvm(0); + } + }