use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class ContractIntegTest method tellFvmContractCallWithinDeployingBlock.
@Test
public void tellFvmContractCallWithinDeployingBlock() throws IOException {
if (txType == TransactionTypes.AVM_CREATE_CODE) {
return;
}
String contractName = "InternalCallContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = Constants.NRG_TRANSACTION_MAX;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
RepositoryCache repo = blockchain.getRepository().startTracking();
nonce = nonce.add(BigInteger.ONE);
assertTrue(tx.isContractCreationTransaction());
assertEquals(deployerBalance, repo.getBalance(deployer));
assertEquals(deployerNonce, repo.getNonce(deployer));
List<AionTransaction> ls = new ArrayList<>();
ls.add(tx);
byte[] input = Arrays.copyOfRange(HashUtil.keccak256("sendValueToContract()".getBytes()), 0, 4);
AionTransaction tx2 = AionTransaction.create(deployerKey, nonce.toByteArray(), TxUtil.calculateContractAddress(tx), BigInteger.TEN.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx2.isContractCreationTransaction());
ls.add(tx2);
BigInteger senderBalance = repo.getBalance(deployer);
Block parent = blockchain.getBestBlock();
MiningBlock block = blockchain.createBlock(parent, ls, false, parent.getTimestamp());
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
AionBlockSummary summary = result.getRight();
assertTrue(result.getLeft().isSuccessful());
AionAddress contractAddress = TxUtil.calculateContractAddress(tx);
assertEquals(BigInteger.TEN, blockchain.getRepository().getBalance(contractAddress));
assertEquals(senderBalance.subtract(BigInteger.TEN).subtract(BigInteger.valueOf(summary.getReceipts().get(0).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))).subtract(BigInteger.valueOf(summary.getReceipts().get(1).getEnergyUsed()).multiply(BigInteger.valueOf(nrgPrice))), blockchain.getRepository().getBalance(deployer));
repo = blockchain.getRepository().startTracking();
assertEquals(BigInteger.TWO, repo.getNonce(deployer));
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class ContractIntegTest method tellFvmContractCallAvmContract.
@Test
public void tellFvmContractCallAvmContract() throws Exception {
if (txType == TransactionTypes.AVM_CREATE_CODE) {
return;
}
String contractName = "InternalCallContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = Constants.NRG_TRANSACTION_MAX;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ZERO;
BigInteger nonce = BigInteger.ZERO;
AionTransaction tx = AionTransaction.create(deployerKey, nonce.toByteArray(), null, value.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
RepositoryCache repo = blockchain.getRepository().startTracking();
nonce = nonce.add(BigInteger.ONE);
AionAddress contract = deployContract(repo, tx, contractName, null, value, nrg, nrgPrice, nonce, true);
assertNotNull(contract);
repo = blockchain.getRepository().startTracking();
AionAddress avmAddress = deployAvmContract(AvmVersion.VERSION_1, nonce);
assertNotNull(avmAddress);
nonce = nonce.add(BigInteger.ONE);
byte[] input = Arrays.copyOfRange(HashUtil.keccak256("callAVM(address)".getBytes()), 0, 4);
input = ByteUtil.merge(input, avmAddress.toByteArray());
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
AionTxExecSummary summary = executeTransaction(tx, block, repo);
// The evmjit only return the the transaction success or failed when performing the function
// call.
assertEquals("reverted", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
assertTrue(result.getRight().getSummaries().get(0).isFailed());
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class AlternatingVmBlockTest method getAvmContractDeploymentCost.
private long getAvmContractDeploymentCost(AvmVersion version, BigInteger nonce) {
AionTransaction avmDeploy = makeAvmContractCreateTransaction(version, deployerKey, nonce);
Block parentBlock = blockchain.getBestBlock();
MiningBlock block = blockchain.createBlock(parentBlock, Collections.singletonList(avmDeploy), false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
return connectResult.getRight().getReceipts().get(0).getEnergyUsed();
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class AlternatingVmBlockTest method testOverflowBlockWithAlternatingVms2.
/**
* Tests a special case of the alternating transactions: the first 14 don't overflow the limit,
* the 15th does overflow it, but the 16th can fit into it.
*
* <p>The problem: the nonce no longer makes any sense because the 15th transaction is kicked
* out.
*/
@Test
public void testOverflowBlockWithAlternatingVms2() throws Exception {
List<AionTransaction> alternatingTransactions = makeAlternatingAvmFvmContractCreateTransactions(AvmVersion.VERSION_1, 16, BigInteger.ZERO);
Block parentBlock = blockchain.getBestBlock();
MiningBlock block = blockchain.createBlock(parentBlock, alternatingTransactions, false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
// A correct block is produced but it does not contain all of the transactions. The second
// last transaction is rejected
// because it would cause the block energy limit to be exceeded, and the last transaction
// now has an invalid nonce since
// the previous transaction was rejected, so neither of these are included in the block.
assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
assertEquals(14, connectResult.getRight().getReceipts().size());
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class AvmHelloWorldTest method testDeployAndCallContractInTheSameBlock.
@Test
public void testDeployAndCallContractInTheSameBlock() {
TransactionTypeRule.allowAVMContractTransaction();
// Deploy the contract.
byte[] jar = getJarBytes(AvmVersion.VERSION_1);
AionTransaction transaction = AionTransaction.create(deployerKey, new byte[0], null, new byte[0], jar, 5_000_000, energyPrice, TransactionTypes.AVM_CREATE_CODE, null);
List<AionTransaction> ls = new ArrayList<>();
ls.add(transaction);
byte[] call = getCallArguments(AvmVersion.VERSION_1);
AionTransaction transaction2 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), TxUtil.calculateContractAddress(transaction), new byte[0], call, 2_000_000, energyPrice, TransactionTypes.DEFAULT, null);
ls.add(transaction2);
MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), ls, false);
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(connectResult.getRight().getReceipts().size() == 2);
// Check the block was imported, the contract has the Avm prefix, and deployment succeeded.
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
assertThat(receipt.getTransactionOutput()[0]).isEqualTo(AddressSpecs.A0_IDENTIFIER);
assertThat(receipt.isSuccessful()).isTrue();
// Check the call success
receipt = connectResult.getRight().getReceipts().get(1);
assertThat(receipt.isSuccessful()).isTrue();
}
Aggregations