Search in sources :

Example 1 with TransactionReceipt

use of org.ethereum.core.TransactionReceipt in project eth-propeller-ethj by adridadou.

the class EthJEventListener method toReceipt.

static org.adridadou.ethereum.propeller.values.TransactionReceipt toReceipt(TransactionReceipt transactionReceipt, EthHash blockHash) {
    Transaction tx = transactionReceipt.getTransaction();
    BigInteger txValue = tx.getValue().length > 0 ? new BigInteger(tx.getValue()) : BigInteger.ZERO;
    if (txValue.signum() == -1) {
        txValue = BigInteger.ZERO;
    }
    return new org.adridadou.ethereum.propeller.values.TransactionReceipt(EthHash.of(tx.getHash()), blockHash, EthAddress.of(tx.getSender()), EthAddress.of(tx.getReceiveAddress()), EthAddress.of(tx.getContractAddress()), transactionReceipt.getError(), EthData.of(transactionReceipt.getExecutionResult()), transactionReceipt.isSuccessful() && transactionReceipt.isValid(), createEventInfoList(EthHash.of(tx.getHash()), transactionReceipt.getLogInfoList()), ethValueDecoder.decode(0, EthData.of(txValue), EthValue.class));
}
Also used : Transaction(org.ethereum.core.Transaction) TransactionReceipt(org.ethereum.core.TransactionReceipt) BigInteger(java.math.BigInteger)

Example 2 with TransactionReceipt

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

the class BlockHashesHelperTest method calculateReceiptsTrieRootForDifferentTxHash.

@Test
public void calculateReceiptsTrieRootForDifferentTxHash() {
    World world = new World();
    // Creation of transactions
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx1 = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    Account acc3 = new AccountBuilder(world).name("acc3").balance(Coin.valueOf(2000000)).build();
    Account acc4 = new AccountBuilder().name("acc4").build();
    Transaction tx2 = new TransactionBuilder().sender(acc3).receiver(acc4).value(BigInteger.valueOf(500000)).build();
    Account acc5 = new AccountBuilder(world).name("acc5").balance(Coin.valueOf(2000000)).build();
    Account acc6 = new AccountBuilder().name("acc6").build();
    Transaction tx3 = new TransactionBuilder().sender(acc5).receiver(acc6).value(BigInteger.valueOf(800000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx1);
    txs.add(tx2);
    Block block = mock(Block.class);
    when(block.getTransactionsList()).thenReturn(txs);
    when(block.getHash()).thenReturn(new Keccak256(Hex.decode("0246c165ac839255aab76c1bc3df7842673ee3673e20dd908bba60862cf41326")));
    ReceiptStore receiptStore = mock(ReceiptStore.class);
    byte[] rskBlockHash = new byte[] { 0x2 };
    when(receiptStore.get(tx1.getHash().getBytes(), block.getHash().getBytes())).thenReturn(Optional.of(new TransactionInfo(new TransactionReceipt(), rskBlockHash, 0)));
    when(receiptStore.get(tx2.getHash().getBytes(), block.getHash().getBytes())).thenReturn(Optional.of(new TransactionInfo(new TransactionReceipt(), rskBlockHash, 0)));
    when(receiptStore.get(tx3.getHash().getBytes(), block.getHash().getBytes())).thenReturn(Optional.of(new TransactionInfo(new TransactionReceipt(), rskBlockHash, 0)));
    // Tx3 is not part of the transaction list of the block
    List<Trie> trie = BlockHashesHelper.calculateReceiptsTrieRootFor(block, receiptStore, tx3.getHash());
    assertNull(trie);
}
Also used : Account(org.ethereum.core.Account) ArrayList(java.util.ArrayList) TransactionReceipt(org.ethereum.core.TransactionReceipt) TransactionBuilder(co.rsk.test.builders.TransactionBuilder) Keccak256(co.rsk.crypto.Keccak256) World(co.rsk.test.World) Transaction(org.ethereum.core.Transaction) Block(org.ethereum.core.Block) TransactionInfo(org.ethereum.db.TransactionInfo) AccountBuilder(co.rsk.test.builders.AccountBuilder) Trie(co.rsk.trie.Trie) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 3 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 4 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 5 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)

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