use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class ContractIntegTest method testRecursiveStackoverflow.
@Test
public void testRecursiveStackoverflow() throws Exception {
String contractName = "Recursive";
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);
if (txType == TransactionTypes.AVM_CREATE_CODE) {
assertNull(contract);
return;
}
deployerBalance = repo.getBalance(deployer);
deployerNonce = repo.getNonce(deployer);
// First recurse 1 time less than the max and verify this is ok.
// Note that 128 == FvmConstants.MAX_CALL_DEPTH
int numRecurses = 127;
byte[] input = ByteUtil.merge(Hex.decode("2d7df21a"), contract.toByteArray());
input = ByteUtil.merge(input, new DataWord(numRecurses + 1).getData());
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);
assertEquals("", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
BigInteger txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
deployerBalance = repo.getBalance(deployer);
deployerNonce = repo.getNonce(deployer);
repo.flushTo(blockchain.getRepository(), true);
repo = blockchain.getRepository().startTracking();
// Now recurse the max amount of times and ensure we fail.
// Note that 128 == FvmConstants.MAX_CALL_DEPTH
numRecurses = 128;
input = ByteUtil.merge(Hex.decode("2d7df21a"), contract.toByteArray());
input = ByteUtil.merge(input, new DataWord(numRecurses + 1).getData());
nonce = nonce.add(BigInteger.ONE);
tx = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, BigInteger.ZERO.toByteArray(), input, nrg, nrgPrice, txType, null);
assertFalse(tx.isContractCreationTransaction());
block = makeBlock(tx);
summary = executeTransaction(tx, block, repo);
assertEquals("reverted", summary.getReceipt().getError());
assertNotEquals(nrg, summary.getNrgUsed().longValue());
txCost = BigInteger.valueOf(summary.getNrgUsed().longValue()).multiply(BigInteger.valueOf(nrgPrice));
assertEquals(deployerBalance.subtract(txCost), repo.getBalance(deployer));
}
use of org.aion.zero.impl.types.MiningBlock 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.types.MiningBlock 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.types.MiningBlock 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();
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class SolidityTypeTest method testStaticArray2.
@Test
public void testStaticArray2() throws Exception {
List<byte[]> x = new ArrayList<>();
x.add(Hex.decode("1122334455667788112233445566778811223344"));
x.add(Hex.decode("2122334455667788112233445566778811223344"));
x.add(Hex.decode("3122334455667788112233445566778811223344"));
SolidityType type = new StaticArrayType("bytes20[3]");
byte[] params = type.encode(x);
System.out.println(Hex.toHexString(params));
AionTransaction tx = createTransaction(ByteUtil.merge(Hex.decode("e4bef5c9"), params));
MiningBlock block = createDummyBlock();
RepositoryCache repo = createRepository(tx);
AionTxReceipt receipt = executeTransaction(tx, block, repo).getReceipt();
System.out.println(receipt);
assertArrayEquals(params, receipt.getTransactionOutput());
Object[] decoded = (Object[]) type.decode(receipt.getTransactionOutput());
for (Object d : decoded) {
System.out.println(Hex.toHexString((byte[]) d));
}
}
Aggregations