Search in sources :

Example 11 with TransactionReceipt

use of org.ethereum.core.TransactionReceipt 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)

Example 12 with TransactionReceipt

use of org.ethereum.core.TransactionReceipt in project rskj by rsksmart.

the class EthModuleGasEstimationDSLTest method estimateGas_nestedCallsWithValueAndGasRetain.

/**
 * Sending one rBTC across three contracts, they will perfomrm 3 CALLs with value.
 * NOTE: each nested call retains 10000 gas to emit events
 */
@Test
public void estimateGas_nestedCallsWithValueAndGasRetain() throws FileNotFoundException, DslProcessorException {
    World world = World.processedWorld("dsl/eth_module/estimateGas/nestedCallsWithValue.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(2, 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);
    assertEquals(0, eth.getEstimationResult().getDeductedRefund());
    assertEquals(callConstant.getGasUsed(), estimatedGas);
    args.setGas(TypeConverter.toQuantityJsonHex(callConstantGasUsed));
    assertTrue(runWithArgumentsAndBlock(eth, args, block));
    assertEquals(callConstantGasUsed, estimatedGas);
    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)

Example 13 with TransactionReceipt

use of org.ethereum.core.TransactionReceipt in project rskj by rsksmart.

the class EthModuleGasEstimationDSLTest method testEstimateGas_contractCallsWithValueTransfer.

/**
 * A contract with an internal CALL with value transfer, it should take into account the STIPEND_CALL amount
 */
@Test
public void testEstimateGas_contractCallsWithValueTransfer() throws FileNotFoundException, DslProcessorException {
    World world = World.processedWorld("dsl/eth_module/estimateGas/callWithValue.txt");
    // Deploy Check
    TransactionReceipt deployTransactionReceipt = world.getTransactionReceiptByName("tx01");
    byte[] status = deployTransactionReceipt.getStatus();
    RskAddress contractAddress = deployTransactionReceipt.getTransaction().getContractAddress();
    assertNotNull(status);
    assertEquals(1, status.length);
    assertEquals(0x01, status[0]);
    assertEquals("0x6252703f5ba322ec64d3ac45e56241b7d9e481ad", "0x" + contractAddress.toHexString());
    TransactionReceipt callWithValueReceipt = world.getTransactionReceiptByName("tx02");
    byte[] status2 = callWithValueReceipt.getStatus();
    assertNotNull(status2);
    assertEquals(1, status2.length);
    assertEquals(0x01, status2[0]);
    // Call with value estimation
    EthModuleTestUtils.EthModuleGasEstimation eth = EthModuleTestUtils.buildBasicEthModuleForGasEstimation(world);
    final CallArguments args = new CallArguments();
    args.setTo("0x" + contractAddress.toHexString());
    // callWithValue()
    args.setData("0xc3cefd36");
    // some value
    args.setValue(TypeConverter.toQuantityJsonHex(10_000));
    args.setNonce(TypeConverter.toQuantityJsonHex(3));
    args.setGas(TypeConverter.toQuantityJsonHex(BLOCK_GAS_LIMIT));
    // block 2 contains 0 tx
    Block block = world.getBlockChain().getBlockByNumber(2);
    // Evaluate the gas used
    ProgramResult callConstant = eth.callConstant(args, block);
    long gasUsed = callConstant.getGasUsed();
    assertEquals(ByteUtil.byteArrayToLong(callWithValueReceipt.getGasUsed()), gasUsed);
    // it just moved STIPEND_CALL (2300) to child
    assertFalse(callConstant.getMovedRemainingGasToChild());
    // Estimate the gas to use
    long estimatedGas = estimateGas(eth, args);
    assertEquals(0, eth.getEstimationResult().getDeductedRefund());
    // The estimated gas should be greater than the gas used in the call
    assertTrue(gasUsed < estimatedGas);
    // Call same transaction with estimatedGas - 1, should fail
    args.setGas(TypeConverter.toQuantityJsonHex(gasUsed));
    assertFalse(runWithArgumentsAndBlock(eth, args, block));
    // Call same transaction with estimated gas
    args.setGas(TypeConverter.toQuantityJsonHex(estimatedGas));
    assertTrue(runWithArgumentsAndBlock(eth, args, block));
    // Call same transaction with estimated gas
    args.setGas(TypeConverter.toQuantityJsonHex(estimatedGas - 1));
    assertFalse(runWithArgumentsAndBlock(eth, args, block));
}
Also used : EthModuleTestUtils(org.ethereum.util.EthModuleTestUtils) TransactionReceipt(org.ethereum.core.TransactionReceipt) RskAddress(co.rsk.core.RskAddress) CallArguments(org.ethereum.rpc.CallArguments) ProgramResult(org.ethereum.vm.program.ProgramResult) Block(org.ethereum.core.Block) World(co.rsk.test.World) Test(org.junit.Test)

Example 14 with TransactionReceipt

use of org.ethereum.core.TransactionReceipt in project rskj by rsksmart.

the class TransactionReceiptDTOTest method testErrorStatusFieldUsingEmptyByteArray.

@Test
public void testErrorStatusFieldUsingEmptyByteArray() {
    RskAddress rskAddress = RskAddress.nullAddress();
    Keccak256 hash = Keccak256.ZERO_HASH;
    Bloom bloom = new Bloom();
    Block block = mock(Block.class);
    when(block.getHash()).thenReturn(hash);
    Transaction transaction = mock(Transaction.class);
    when(transaction.getHash()).thenReturn(hash);
    when(transaction.getSender()).thenReturn(rskAddress);
    when(transaction.getReceiveAddress()).thenReturn(rskAddress);
    TransactionReceipt txReceipt = mock(TransactionReceipt.class);
    when(txReceipt.getTransaction()).thenReturn(transaction);
    when(txReceipt.getLogInfoList()).thenReturn(Collections.emptyList());
    when(txReceipt.getBloomFilter()).thenReturn(bloom);
    when(txReceipt.getStatus()).thenReturn(ByteUtil.EMPTY_BYTE_ARRAY);
    TransactionInfo txInfo = new TransactionInfo(txReceipt, hash.getBytes(), 0);
    TransactionReceiptDTO transactionReceiptDTO = new TransactionReceiptDTO(block, txInfo);
    String actualStatus = transactionReceiptDTO.getStatus();
    assertNotNull(actualStatus);
    assertEquals("0x0", actualStatus);
}
Also used : Transaction(org.ethereum.core.Transaction) Bloom(org.ethereum.core.Bloom) RskAddress(co.rsk.core.RskAddress) TransactionReceipt(org.ethereum.core.TransactionReceipt) Block(org.ethereum.core.Block) TransactionInfo(org.ethereum.db.TransactionInfo) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 15 with TransactionReceipt

use of org.ethereum.core.TransactionReceipt in project rskj by rsksmart.

the class TransactionReceiptDTOTest method testOkStatusField.

@Test
public void testOkStatusField() {
    RskAddress rskAddress = RskAddress.nullAddress();
    Keccak256 hash = Keccak256.ZERO_HASH;
    Bloom bloom = new Bloom();
    Block block = mock(Block.class);
    when(block.getHash()).thenReturn(hash);
    Transaction transaction = mock(Transaction.class);
    when(transaction.getHash()).thenReturn(hash);
    when(transaction.getSender()).thenReturn(rskAddress);
    when(transaction.getReceiveAddress()).thenReturn(rskAddress);
    TransactionReceipt txReceipt = mock(TransactionReceipt.class);
    when(txReceipt.getTransaction()).thenReturn(transaction);
    when(txReceipt.getLogInfoList()).thenReturn(Collections.emptyList());
    when(txReceipt.getBloomFilter()).thenReturn(bloom);
    when(txReceipt.getStatus()).thenReturn(new byte[] { 0x01 });
    TransactionInfo txInfo = new TransactionInfo(txReceipt, hash.getBytes(), 0);
    TransactionReceiptDTO transactionReceiptDTO = new TransactionReceiptDTO(block, txInfo);
    String actualStatus = transactionReceiptDTO.getStatus();
    assertNotNull(actualStatus);
    assertEquals("0x1", actualStatus);
}
Also used : Transaction(org.ethereum.core.Transaction) Bloom(org.ethereum.core.Bloom) RskAddress(co.rsk.core.RskAddress) TransactionReceipt(org.ethereum.core.TransactionReceipt) Block(org.ethereum.core.Block) TransactionInfo(org.ethereum.db.TransactionInfo) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Aggregations

TransactionReceipt (org.ethereum.core.TransactionReceipt)39 Test (org.junit.Test)30 Block (org.ethereum.core.Block)17 Transaction (org.ethereum.core.Transaction)15 Keccak256 (co.rsk.crypto.Keccak256)13 World (co.rsk.test.World)13 TransactionInfo (org.ethereum.db.TransactionInfo)10 RskAddress (co.rsk.core.RskAddress)8 CallArguments (org.ethereum.rpc.CallArguments)8 ArrayList (java.util.ArrayList)7 EthModuleTestUtils (org.ethereum.util.EthModuleTestUtils)7 LogInfo (org.ethereum.vm.LogInfo)7 BigInteger (java.math.BigInteger)6 Bloom (org.ethereum.core.Bloom)6 ProgramResult (org.ethereum.vm.program.ProgramResult)6 TestSystemProperties (co.rsk.config.TestSystemProperties)5 DslParser (co.rsk.test.dsl.DslParser)4 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)4 Trie (co.rsk.trie.Trie)4 List (java.util.List)4