Search in sources :

Example 11 with InternalTransaction

use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.

the class EthModuleGasEstimationDSLTest method estimateGas_nestedCallsWithValueFixedGasRetainAndStorageRefund.

/**
 * Send 1 rBTC accross three contracts, then the last contract frees a storage cell and does a CALL with value
 * NOTE: this test uses a fixed amount of gas for each internal call
 */
@Test
public void estimateGas_nestedCallsWithValueFixedGasRetainAndStorageRefund() throws FileNotFoundException, DslProcessorException {
    World world = World.processedWorld("dsl/eth_module/estimateGas/nestedCallsWithValueStorageRefundAndFixedGas.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);
    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);
    assertTrue(callConstant.getMovedRemainingGasToChild());
    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 InternalTransaction

use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.

the class BridgeSupportTest method registerFastBridgeBtcTransaction_surpasses_locking_cap_and_tries_to_register_again.

@Test
public void registerFastBridgeBtcTransaction_surpasses_locking_cap_and_tries_to_register_again() throws IOException, BlockStoreException, BridgeIllegalArgumentException {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
    when(activations.isActive(ConsensusRule.RSKIP134)).thenReturn(true);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, co.rsk.core.Coin.valueOf(1));
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations);
    BtcLockSender btcLockSender = mock(BtcLockSender.class);
    BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
    when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
    Context btcContext = mock(Context.class);
    when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
    BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), btcLockSenderProvider, new PeginInstructionsProvider(), repository, mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations));
    doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
    doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
    doReturn(// The first time we simulate a lower locking cap than the value to register, to force the reimburse
    Coin.COIN, // The next time we simulate a hight locking cap, to verify the user can't attempt to register the already reimbursed tx
    Coin.FIFTY_COINS).when(bridgeSupport).getLockingCap();
    doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
    Address btcAddress = Address.fromBase58(btcParams, "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
    ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
    RskAddress lbcAddress = new RskAddress(key.getAddress());
    BtcTransaction tx = createBtcTransactionWithOutputToAddress(Coin.COIN, getFastBridgeFederationAddress());
    byte[] pmtSerialized = Hex.decode("ab");
    InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
    Keccak256 dHash = PegTestUtils.createHash3(0);
    BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, dHash, btcAddress, lbcAddress, btcAddress, false);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_REFUNDED_USER_ERROR_CODE), result);
    // Update repository
    bridgeSupport.save();
    result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, dHash, btcAddress, lbcAddress, btcAddress, false);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_UNPROCESSABLE_TX_ALREADY_PROCESSED_ERROR_CODE), result);
}
Also used : RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ECKey(org.ethereum.crypto.ECKey) Keccak256(co.rsk.crypto.Keccak256) InternalTransaction(org.ethereum.vm.program.InternalTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 13 with InternalTransaction

use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.

the class BridgeSupportTest method registerFastBridgeBtcTransaction_sender_is_not_lbc.

@Test
public void registerFastBridgeBtcTransaction_sender_is_not_lbc() throws IOException, BlockStoreException, BridgeIllegalArgumentException {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, mock(Repository.class), mock(BridgeEventLogger.class), null, mock(BtcBlockStoreWithCache.Factory.class), activations);
    InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, null, null, null, null, null);
    BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, Hex.decode("ab"), PegTestUtils.createHash3(0), mock(Address.class), mock(RskAddress.class), mock(Address.class), false);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_UNPROCESSABLE_TX_INVALID_SENDER_ERROR_CODE), result);
}
Also used : Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) RskAddress(co.rsk.core.RskAddress) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) InternalTransaction(org.ethereum.vm.program.InternalTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 14 with InternalTransaction

use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method releaseBtcFromContract.

@Test
public void releaseBtcFromContract() throws AddressFormatException, IOException {
    Repository repository = createRepository();
    Repository track = repository.startTracking();
    org.ethereum.core.Transaction tx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, BigIntegers.asUnsignedByteArray(NONCE), DataWord.valueOf(BigIntegers.asUnsignedByteArray(GAS_PRICE)), DataWord.valueOf(BigIntegers.asUnsignedByteArray(GAS_LIMIT)), new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(new BtcECKey().getPrivKey()).getAddress()).getBytes(), Hex.decode(TO_ADDRESS), BigIntegers.asUnsignedByteArray(AMOUNT), Hex.decode(DATA), "");
    track.saveCode(tx.getSender(), new byte[] { 0x1 });
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
    BridgeSupport bridgeSupport = getBridgeSupport(provider, track);
    try {
        bridgeSupport.releaseBtc(tx);
    } catch (Program.OutOfGasException e) {
        return;
    }
    Assert.fail();
}
Also used : co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) MutableRepository(org.ethereum.db.MutableRepository) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) InternalTransaction(org.ethereum.vm.program.InternalTransaction) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with InternalTransaction

use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.

the class BridgeSupportTest method registerFastBridgeBtcTransaction_TxWitnessAlreadySavedInStorage_returnsError.

@Test
public void registerFastBridgeBtcTransaction_TxWitnessAlreadySavedInStorage_returnsError() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
    Context btcContext = mock(Context.class);
    when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    when(provider.isFastBridgeFederationDerivationHashUsed(any(), any())).thenReturn(false).thenReturn(true);
    ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
    RskAddress lbcAddress = new RskAddress(key.getAddress());
    BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations));
    doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
    doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
    doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
    BtcTransaction tx = createBtcTransactionWithOutputToAddress(Coin.COIN, new BtcECKey().toAddress(btcParams));
    InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
    TransactionWitness txWit = new TransactionWitness(1);
    txWit.setPush(0, new byte[] {});
    tx.setWitness(0, txWit);
    BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, Hex.decode("ab"), PegTestUtils.createHash3(0), mock(Address.class), lbcAddress, mock(Address.class), false);
    Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_UNPROCESSABLE_TX_ALREADY_PROCESSED_ERROR_CODE), result);
}
Also used : RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ECKey(org.ethereum.crypto.ECKey) Keccak256(co.rsk.crypto.Keccak256) InternalTransaction(org.ethereum.vm.program.InternalTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

InternalTransaction (org.ethereum.vm.program.InternalTransaction)15 RskAddress (co.rsk.core.RskAddress)14 Test (org.junit.Test)13 BigInteger (java.math.BigInteger)12 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)10 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)9 ECKey (org.ethereum.crypto.ECKey)8 Keccak256 (co.rsk.crypto.Keccak256)7 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)6 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)6 MutableRepository (org.ethereum.db.MutableRepository)6 Repository (org.ethereum.core.Repository)5 TestSystemProperties (co.rsk.config.TestSystemProperties)3 Coin (co.rsk.core.Coin)3 BtcLockSender (co.rsk.peg.btcLockSender.BtcLockSender)3 World (co.rsk.test.World)3 DslProcessorException (co.rsk.test.dsl.DslProcessorException)3 FileNotFoundException (java.io.FileNotFoundException)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3