use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class MultiVersionAvmTest method deployHelloWorldContract.
private AionAddress deployHelloWorldContract(AvmVersion version, BigInteger nonce) {
AionTransaction transaction = makeHelloWorldDeployTransaction(version, nonce);
Block parentBlock = this.blockchain.getBestBlock();
MiningBlock block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
byte[] o = connectResult.getRight().getReceipts().get(0).getTransactionOutput();
return new AionAddress(connectResult.getRight().getReceipts().get(0).getTransactionOutput());
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class MultiVersionAvmTest method testDeployVersion2ContractWithOnlyVersion1Support.
/**
* The transaction hash contract can only be deployed in version 2 because the functionality
* it makes use of does not exist in avm version 1.
*/
@Test
public void testDeployVersion2ContractWithOnlyVersion1Support() {
Assert.assertEquals(0, this.blockchain.getBestBlock().getNumber());
// Ensure we are at a block height where avm version 1 is enabled, then deploy.
buildBlockchainToHeight(BLOCK_VERSION1_ENABLED);
AionTransaction transaction = makeTransactionHashContract(BigInteger.ZERO);
Block parentBlock = this.blockchain.getBestBlock();
MiningBlock block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
AionAddress contract = new AionAddress(connectResult.getRight().getReceipts().get(0).getTransactionOutput());
transaction = makeTransactionHashCallTransaction(BigInteger.ONE, contract);
parentBlock = this.blockchain.getBestBlock();
block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
// We expect a failure here, because an exception will get thrown inside the contract!
Assert.assertFalse(connectResult.getRight().getReceipts().get(0).isSuccessful());
// Now we wait until avm version 2 is enabled before calling it again.
buildBlockchainToHeight(BLOCK_VERSION2_ENABLED);
transaction = makeTransactionHashCallTransaction(BigInteger.TWO, contract);
parentBlock = this.blockchain.getBestBlock();
block = this.blockchain.createBlock(parentBlock, Collections.singletonList(transaction), false, parentBlock.getTimestamp());
connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
Assert.assertEquals(1, connectResult.getRight().getReceipts().size());
Assert.assertTrue(connectResult.getRight().getReceipts().get(0).isSuccessful());
// Verify that the contract does indeed return the transaction hash.
Assert.assertArrayEquals(transaction.getTransactionHash(), connectResult.getRight().getReceipts().get(0).getTransactionOutput());
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class MultiVersionAvmTest method buildBlockchainToHeight.
private void buildBlockchainToHeight(long height) {
Block parentBlock = this.blockchain.getBestBlock();
while (parentBlock.getNumber() < height) {
MiningBlock block = this.blockchain.createBlock(parentBlock, new ArrayList<>(), false, parentBlock.getTimestamp());
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
Assert.assertEquals(ImportResult.IMPORTED_BEST, connectResult.getLeft());
parentBlock = this.blockchain.getBestBlock();
}
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class ContractIntegTest method testDeployFvmContractToAnExistedAccountBefore040Fork.
@Test
public void testDeployFvmContractToAnExistedAccountBefore040Fork() throws IOException {
if (txType == TransactionTypes.AVM_CREATE_CODE) {
return;
}
blockchain.forkUtility.enable040Fork(1000);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ONE;
AionAddress destinationAddr = TxUtil.calculateContractAddress(deployer.toByteArray(), deployerNonce);
// create a tx the sender send some balance to the account the deployer will deploy in the
// feature.
AionTransaction tx = AionTransaction.create(senderKey, senderNonce.toByteArray(), destinationAddr, value.toByteArray(), new byte[0], nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
assertEquals(1, block.getTransactionsList().size());
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
RepositoryCache repo = blockchain.getRepository().startTracking();
assertEquals(BigInteger.ONE, repo.getBalance(destinationAddr));
BigInteger txCost = BigInteger.valueOf(nrgPrice).multiply(BigInteger.valueOf(result.getRight().getReceipts().get(0).getEnergyUsed()));
assertEquals(senderBalance.subtract(BigInteger.ONE).subtract(txCost), repo.getBalance(sender));
senderNonce = senderNonce.add(BigInteger.ONE);
String contractName = "PayableConstructor";
byte[] deployCode = getDeployCode(contractName);
// to == null signals that this is contract creation.
tx = AionTransaction.create(deployerKey, deployerNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
block = makeBlock(tx);
assertEquals(1, block.getTransactionsList().size());
result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = result.getRight().getSummaries().get(0);
if (txType == TransactionTypes.DEFAULT) {
// "" == SUCCESS
assertEquals("FAILURE", summary.getReceipt().getError());
deployerNonce = deployerNonce.add(BigInteger.ONE);
checkStateOfDeployer(repo, summary, nrgPrice, BigInteger.ZERO, deployerNonce);
}
assertEquals(1000000, summary.getReceipt().getEnergyUsed());
}
use of org.aion.zero.impl.core.ImportResult in project aion by aionnetwork.
the class ContractIntegTest method testDeployFvmContractToAnExistedAccountWith040Fork.
@Test
public void testDeployFvmContractToAnExistedAccountWith040Fork() throws IOException {
if (txType == TransactionTypes.AVM_CREATE_CODE) {
return;
}
blockchain.forkUtility.enable040Fork(0);
long nrg = 1_000_000;
long nrgPrice = energyPrice;
BigInteger value = BigInteger.ONE;
AionAddress destinationAddr = TxUtil.calculateContractAddress(deployer.toByteArray(), deployerNonce);
// create a tx the sender send some balance to the account the deployer will deploy in the
// feature.
AionTransaction tx = AionTransaction.create(senderKey, senderNonce.toByteArray(), destinationAddr, value.toByteArray(), new byte[0], nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
MiningBlock block = makeBlock(tx);
assertEquals(1, block.getTransactionsList().size());
Pair<ImportResult, AionBlockSummary> result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
RepositoryCache repo = blockchain.getRepository().startTracking();
assertEquals(BigInteger.ONE, repo.getBalance(destinationAddr));
BigInteger txCost = BigInteger.valueOf(nrgPrice).multiply(BigInteger.valueOf(result.getRight().getReceipts().get(0).getEnergyUsed()));
assertEquals(senderBalance.subtract(BigInteger.ONE).subtract(txCost), repo.getBalance(sender));
senderNonce = senderNonce.add(BigInteger.ONE);
String contractName = "PayableConstructor";
byte[] deployCode = getDeployCode(contractName);
// to == null signals that this is contract creation.
tx = AionTransaction.create(deployerKey, deployerNonce.toByteArray(), null, BigInteger.ZERO.toByteArray(), deployCode, nrg, nrgPrice, txType, null);
assertTrue(tx.isContractCreationTransaction());
assertEquals(Builder.DEFAULT_BALANCE, blockchain.getRepository().getBalance(deployer));
assertEquals(BigInteger.ZERO, blockchain.getRepository().getNonce(deployer));
block = makeBlock(tx);
assertEquals(1, block.getTransactionsList().size());
result = blockchain.tryToConnectAndFetchSummary(block);
assertTrue(result.getLeft().isSuccessful());
repo = blockchain.getRepository().startTracking();
AionTxExecSummary summary = result.getRight().getSummaries().get(0);
if (txType == TransactionTypes.DEFAULT) {
// "" == SUCCESS
assertEquals("", summary.getReceipt().getError());
AionAddress contract = TxUtil.calculateContractAddress(tx);
checkStateOfNewContract(repo, contractName, contract, summary.getResult(), FastVmResultCode.SUCCESS, value);
deployerNonce = deployerNonce.add(BigInteger.ONE);
checkStateOfDeployer(repo, summary, nrgPrice, BigInteger.ZERO, deployerNonce);
}
assertEquals(225787, summary.getReceipt().getEnergyUsed());
}
Aggregations