Search in sources :

Example 1 with CallArguments

use of org.ethereum.rpc.CallArguments in project rskj by rsksmart.

the class Web3RskImplTest method web3_CallArguments_toString.

@Test
public void web3_CallArguments_toString() {
    CallArguments callArguments = new CallArguments();
    callArguments.setFrom("0x1");
    callArguments.setTo("0x2");
    callArguments.setGas("21000");
    callArguments.setGasLimit("21000");
    callArguments.setGasPrice("100");
    callArguments.setValue("1");
    callArguments.setData("data");
    callArguments.setNonce("0");
    callArguments.setChainId("0x00");
    callArguments.setType("0x00");
    assertEquals("CallArguments{from='0x1', to='0x2', gas='21000', gasLimit='21000', gasPrice='100', value='1', data='data', nonce='0', chainId='0x00', type='0x00'}", callArguments.toString());
}
Also used : CallArguments(org.ethereum.rpc.CallArguments) Test(org.junit.Test)

Example 2 with CallArguments

use of org.ethereum.rpc.CallArguments in project rskj by rsksmart.

the class EthModuleTest method sendTransactionWithGasLimitTest.

@Test
public void sendTransactionWithGasLimitTest() {
    Constants constants = Constants.regtest();
    Wallet wallet = new Wallet(new HashMapDB());
    RskAddress sender = wallet.addAccount();
    RskAddress receiver = wallet.addAccount();
    // Hash of the expected transaction
    CallArguments args = TransactionFactoryHelper.createArguments(sender, receiver);
    Transaction tx = TransactionFactoryHelper.createTransaction(args, constants.getChainId(), wallet.getAccount(sender));
    String txExpectedResult = tx.getHash().toJsonString();
    TransactionPoolAddResult transactionPoolAddResult = mock(TransactionPoolAddResult.class);
    when(transactionPoolAddResult.transactionsWereAdded()).thenReturn(true);
    TransactionGateway transactionGateway = mock(TransactionGateway.class);
    when(transactionGateway.receiveTransaction(any(Transaction.class))).thenReturn(transactionPoolAddResult);
    TransactionPool transactionPool = mock(TransactionPool.class);
    EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPool, transactionGateway);
    // Hash of the actual transaction builded inside the sendTransaction
    String txResult = ethModuleTransaction.sendTransaction(args);
    assertEquals(txExpectedResult, txResult);
}
Also used : TransactionPool(org.ethereum.core.TransactionPool) Transaction(org.ethereum.core.Transaction) Wallet(co.rsk.core.Wallet) RskAddress(co.rsk.core.RskAddress) CallArguments(org.ethereum.rpc.CallArguments) TransactionPoolAddResult(org.ethereum.core.TransactionPoolAddResult) BridgeConstants(co.rsk.config.BridgeConstants) Constants(org.ethereum.config.Constants) HashMapDB(org.ethereum.datasource.HashMapDB) TransactionGateway(co.rsk.net.TransactionGateway) Test(org.junit.Test)

Example 3 with CallArguments

use of org.ethereum.rpc.CallArguments in project rskj by rsksmart.

the class EthModuleTest method callWithoutReturn.

@Test
public void callWithoutReturn() {
    CallArguments args = new CallArguments();
    BlockResult blockResult = mock(BlockResult.class);
    Block block = mock(Block.class);
    ExecutionBlockRetriever retriever = mock(ExecutionBlockRetriever.class);
    when(retriever.retrieveExecutionBlock("latest")).thenReturn(blockResult);
    when(blockResult.getBlock()).thenReturn(block);
    byte[] hReturn = new byte[0];
    ProgramResult executorResult = mock(ProgramResult.class);
    when(executorResult.getHReturn()).thenReturn(hReturn);
    ReversibleTransactionExecutor executor = mock(ReversibleTransactionExecutor.class);
    when(executor.executeTransaction(eq(blockResult.getBlock()), any(), any(), any(), any(), any(), any(), any())).thenReturn(executorResult);
    EthModule eth = new EthModule(null, anyByte(), null, null, executor, retriever, null, null, null, new BridgeSupportFactory(null, null, null), config.getGasEstimationCap());
    String expectedResult = TypeConverter.toUnformattedJsonHex(hReturn);
    String actualResult = eth.call(args, "latest");
    assertEquals(expectedResult, actualResult);
}
Also used : BlockResult(co.rsk.core.bc.BlockResult) CallArguments(org.ethereum.rpc.CallArguments) ProgramResult(org.ethereum.vm.program.ProgramResult) Block(org.ethereum.core.Block) ReversibleTransactionExecutor(co.rsk.core.ReversibleTransactionExecutor) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) ExecutionBlockRetriever(co.rsk.rpc.ExecutionBlockRetriever) Test(org.junit.Test)

Example 4 with CallArguments

use of org.ethereum.rpc.CallArguments in project rskj by rsksmart.

the class EthModuleTest method sendTransaction_invalidSenderAccount_throwsRskJsonRpcRequestException.

@Test
public void sendTransaction_invalidSenderAccount_throwsRskJsonRpcRequestException() {
    // Given
    Constants constants = Constants.regtest();
    Wallet wallet = new Wallet(new HashMapDB());
    TransactionPool transactionPoolMock = mock(TransactionPool.class);
    TransactionGateway transactionGatewayMock = mock(TransactionGateway.class);
    CallArguments argsMock = mock(CallArguments.class);
    RskAddress addressFrom = new RskAddress(new ECKey().getAddress());
    // Address not in wallet
    doReturn(addressFrom.toJsonString()).when(argsMock).getFrom();
    EthModuleTransactionBase ethModuleTransaction = new EthModuleTransactionBase(constants, wallet, transactionPoolMock, transactionGatewayMock);
    // Then
    try {
        ethModuleTransaction.sendTransaction(argsMock);
        fail("RskJsonRpcRequestException should be thrown");
    } catch (RskJsonRpcRequestException ex) {
        verify(argsMock, times(2)).getFrom();
        assertEquals("Could not find account for address: " + addressFrom.toJsonString(), ex.getMessage());
    }
}
Also used : TransactionPool(org.ethereum.core.TransactionPool) RskJsonRpcRequestException(org.ethereum.rpc.exception.RskJsonRpcRequestException) Wallet(co.rsk.core.Wallet) CallArguments(org.ethereum.rpc.CallArguments) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) HashMapDB(org.ethereum.datasource.HashMapDB) TransactionGateway(co.rsk.net.TransactionGateway) Test(org.junit.Test)

Example 5 with CallArguments

use of org.ethereum.rpc.CallArguments in project rskj by rsksmart.

the class EthModuleGasEstimationDSLTest method estimateGas_nestedCallsWithValueGasRetainAndStorageRefund.

/**
 * Send 1 rBTC accross three contracts, then the last contract frees a storage cell and does a CALL with value
 * NOTE: each nested call retains 10000 gas to emit events
 */
@Test
public void estimateGas_nestedCallsWithValueGasRetainAndStorageRefund() throws FileNotFoundException, DslProcessorException {
    World world = World.processedWorld("dsl/eth_module/estimateGas/nestedCallsWithValueAndStorageRefund.txt");
    TransactionReceipt contractDeployA = world.getTransactionReceiptByName("tx01");
    String contractAddressA = "0x" + contractDeployA.getTransaction().getContractAddress().toHexString();
    byte[] status = contractDeployA.getStatus();
    assertNotNull(status);
    assertEquals(1, status.length);
    assertEquals(0x01, status[0]);
    assertEquals("0x6252703f5ba322ec64d3ac45e56241b7d9e481ad", contractAddressA);
    TransactionReceipt contractDeployB = world.getTransactionReceiptByName("tx02");
    String contractAddressB = "0x" + contractDeployB.getTransaction().getContractAddress().toHexString();
    byte[] status2 = contractDeployB.getStatus();
    assertNotNull(status2);
    assertEquals(1, status2.length);
    assertEquals(0x01, status2[0]);
    assertEquals("0x56aa252dd82173789984fa164ee26ce2da9336ff", contractAddressB);
    TransactionReceipt contractDeployC = world.getTransactionReceiptByName("tx03");
    String contractAddressC = "0x" + contractDeployC.getTransaction().getContractAddress().toHexString();
    byte[] status3 = contractDeployC.getStatus();
    assertNotNull(status3);
    assertEquals(1, status3.length);
    assertEquals(0x01, status3[0]);
    assertEquals("0x27444fbce96cb2d27b94e116d1506d7739c05862", contractAddressC);
    EthModuleTestUtils.EthModuleGasEstimation eth = EthModuleTestUtils.buildBasicEthModuleForGasEstimation(world);
    Block block = world.getBlockChain().getBestBlock();
    // call callAddressWithValue, it should start the nested calls
    final CallArguments args = new CallArguments();
    args.setTo(contractAddressA);
    args.setValue(TypeConverter.toQuantityJsonHex(1));
    args.setNonce(TypeConverter.toQuantityJsonHex(6));
    args.setGas(TypeConverter.toQuantityJsonHex(BLOCK_GAS_LIMIT));
    // callAddressWithValue()
    args.setData("0xfb60f709");
    ProgramResult callConstant = eth.callConstant(args, block);
    List<InternalTransaction> internalTransactions = callConstant.getInternalTransactions();
    assertTrue(internalTransactions.stream().allMatch(i -> i.getValue().equals(Coin.valueOf(1))));
    assertEquals(3, internalTransactions.size());
    assertEquals(3, callConstant.getLogInfoList().size());
    assertEvents(callConstant, "NestedCallWV", 2);
    assertEvents(callConstant, "LastCall", 1);
    assertTrue(callConstant.getMovedRemainingGasToChild());
    long callConstantGasUsed = callConstant.getGasUsed();
    long estimatedGas = estimateGas(eth, args);
    assertTrue(eth.getEstimationResult().getDeductedRefund() > 0);
    assertTrue(callConstant.getDeductedRefund() > 0);
    assertEquals(callConstant.getGasUsedBeforeRefunds() / 2, callConstant.getDeductedRefund());
    assertEquals(callConstantGasUsed + callConstant.getDeductedRefund(), estimatedGas);
    args.setGas(TypeConverter.toQuantityJsonHex(callConstantGasUsed));
    assertFalse(runWithArgumentsAndBlock(eth, args, block));
    args.setGas(TypeConverter.toQuantityJsonHex(estimatedGas));
    assertTrue(runWithArgumentsAndBlock(eth, args, block));
    args.setGas(TypeConverter.toQuantityJsonHex(estimatedGas - 1));
    assertFalse(runWithArgumentsAndBlock(eth, args, block));
}
Also used : LogInfo(org.ethereum.vm.LogInfo) TypeConverter(org.ethereum.rpc.TypeConverter) HashUtil(org.ethereum.crypto.HashUtil) DslProcessorException(co.rsk.test.dsl.DslProcessorException) CallArguments(org.ethereum.rpc.CallArguments) CallTransaction(org.ethereum.core.CallTransaction) RskAddress(co.rsk.core.RskAddress) TransactionReceipt(org.ethereum.core.TransactionReceipt) Test(org.junit.Test) Coin(co.rsk.core.Coin) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) World(co.rsk.test.World) Block(org.ethereum.core.Block) List(java.util.List) Stream(java.util.stream.Stream) TestSystemProperties(co.rsk.config.TestSystemProperties) ByteUtil(org.ethereum.util.ByteUtil) InternalTransaction(org.ethereum.vm.program.InternalTransaction) GasCost(org.ethereum.vm.GasCost) BigInteger(java.math.BigInteger) EthModuleTestUtils(org.ethereum.util.EthModuleTestUtils) ProgramResult(org.ethereum.vm.program.ProgramResult) Assert(org.junit.Assert) EthModuleTestUtils(org.ethereum.util.EthModuleTestUtils) TransactionReceipt(org.ethereum.core.TransactionReceipt) CallArguments(org.ethereum.rpc.CallArguments) ProgramResult(org.ethereum.vm.program.ProgramResult) World(co.rsk.test.World) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Block(org.ethereum.core.Block) Test(org.junit.Test)

Aggregations

CallArguments (org.ethereum.rpc.CallArguments)32 Test (org.junit.Test)30 Block (org.ethereum.core.Block)10 ProgramResult (org.ethereum.vm.program.ProgramResult)10 World (co.rsk.test.World)9 RskAddress (co.rsk.core.RskAddress)8 TransactionReceipt (org.ethereum.core.TransactionReceipt)8 EthModuleTestUtils (org.ethereum.util.EthModuleTestUtils)8 TestSystemProperties (co.rsk.config.TestSystemProperties)5 Wallet (co.rsk.core.Wallet)4 BigInteger (java.math.BigInteger)4 HashMapDB (org.ethereum.datasource.HashMapDB)4 RskJsonRpcRequestException (org.ethereum.rpc.exception.RskJsonRpcRequestException)4 GasCost (org.ethereum.vm.GasCost)4 Coin (co.rsk.core.Coin)3 ReversibleTransactionExecutor (co.rsk.core.ReversibleTransactionExecutor)3 BlockResult (co.rsk.core.bc.BlockResult)3 BridgeSupportFactory (co.rsk.peg.BridgeSupportFactory)3 ExecutionBlockRetriever (co.rsk.rpc.ExecutionBlockRetriever)3 DslProcessorException (co.rsk.test.dsl.DslProcessorException)3