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