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));
}
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);
}
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);
}
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();
}
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);
}
Aggregations