use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class AvmBulkTransactionTest method sendValueTransferTransactionsInBulkTest.
@Test
public void sendValueTransferTransactionsInBulkTest() {
int numTransactions = 50;
// Create the accounts.
List<ECKey> accounts = getRandomAccounts(numTransactions);
// Grab the initial data we need to track.
BigInteger expectedDeployerNonce = getNonce(this.deployerKey);
BigInteger initialBalanceDeployer = getBalance(this.deployerKey);
// Declare the various transfer amounts.
List<BigInteger> transferAmounts = getRandomValues(numTransactions, 500, 5_000_000);
printValuesUsed(transferAmounts);
// Make the transactions, then bundle them up together.
List<AionTransaction> transactions = new ArrayList<>();
for (int i = 0; i < numTransactions; i++) {
transactions.add(makeValueTransferTransaction(this.deployerKey, accounts.get(i), transferAmounts.get(i), expectedDeployerNonce));
expectedDeployerNonce = expectedDeployerNonce.add(BigInteger.ONE);
}
// Process the transactions in bulk.
AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
// Verify all transactions were successful.
assertEquals(numTransactions, blockSummary.getSummaries().size());
for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
assertTrue(transactionSummary.getReceipt().isSuccessful());
}
BigInteger expectedDeployerBalance = initialBalanceDeployer;
for (int i = 0; i < numTransactions; i++) {
BigInteger energyUsed = BigInteger.valueOf(blockSummary.getSummaries().get(i).getReceipt().getEnergyUsed());
expectedDeployerBalance = expectedDeployerBalance.subtract(energyUsed.multiply(BigInteger.valueOf(energyPrice))).subtract(transferAmounts.get(i));
}
// Verify account states after the transactions have been processed.
for (int i = 0; i < numTransactions; i++) {
assertEquals(transferAmounts.get(i), getBalance(accounts.get(i)));
assertEquals(BigInteger.ZERO, getNonce(accounts.get(i)));
}
assertEquals(expectedDeployerBalance, getBalance(this.deployerKey));
assertEquals(expectedDeployerNonce, getNonce(this.deployerKey));
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class AvmBulkTransactionTest method importBlockWithContractDeploysAndSubsequentCalls.
/**
* Ensures that AVM contracts can be deployed and called within the same block.
*/
@Test
public void importBlockWithContractDeploysAndSubsequentCalls() {
BigInteger expectedNonce = getNonce(deployerKey);
BigInteger initialBalance = getBalance(deployerKey);
// note: 3 contract deployments would pass the block energy limit
int nbCreateTransactions = 2;
int nbCallTransactions = 2;
int nbTransactions = nbCreateTransactions + nbCreateTransactions * nbCallTransactions;
List<AionTransaction> transactions = new ArrayList<>();
for (int j = 0; j < nbCreateTransactions; j++) {
// create contract transaction
AionTransaction deployTx = makeAvmContractCreateTransaction(deployerKey, expectedNonce);
expectedNonce = expectedNonce.add(BigInteger.ONE);
AionAddress deployedContract = TxUtil.calculateContractAddress(deployTx);
transactions.add(deployTx);
// subsequent call transactions
for (int i = 0; i < nbCallTransactions; i++) {
transactions.add(makeAvmContractCallTransaction(deployerKey, expectedNonce, deployedContract));
expectedNonce = expectedNonce.add(BigInteger.ONE);
}
}
// process the transactions in bulk
AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
// verify all transactions were successful
assertEquals(nbTransactions, blockSummary.getSummaries().size());
for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
System.out.println(transactionSummary.getReceipt());
assertTrue(transactionSummary.getReceipt().isSuccessful());
}
BigInteger expectedBalance = initialBalance;
for (int i = 0; i < nbTransactions; i++) {
BigInteger energyUsed = BigInteger.valueOf(blockSummary.getSummaries().get(i).getReceipt().getEnergyUsed());
expectedBalance = expectedBalance.subtract(energyUsed.multiply(BigInteger.valueOf(energyPrice)));
}
assertEquals(expectedBalance, getBalance(deployerKey));
assertEquals(expectedNonce, getNonce(deployerKey));
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class AvmInternalTxTest method makeCall.
private void makeCall(BigInteger nonce, AionAddress contract, byte[] call) {
AionTransaction transaction = AionTransaction.create(deployerKey, nonce.toByteArray(), contract, new byte[0], call, 2_000_000, minEnergyPrice, TransactionTypes.DEFAULT, null);
MiningBlock block = this.blockchain.createNewMiningBlock(this.blockchain.getBestBlock(), Collections.singletonList(transaction), false);
Pair<ImportResult, AionBlockSummary> connectResult = this.blockchain.tryToConnectAndFetchSummary(block);
AionTxReceipt receipt = connectResult.getRight().getReceipts().get(0);
// Check the block was imported and the transaction was successful.
assertThat(connectResult.getLeft()).isEqualTo(ImportResult.IMPORTED_BEST);
assertThat(receipt.isSuccessful()).isTrue();
System.out.println(block);
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class BeaconHashValidatorTest method validateTxForPendingStateWhenBeaconHashNotOnMainchain.
@Test
public void validateTxForPendingStateWhenBeaconHashNotOnMainchain() {
long unityForkNumber = 3;
ForkUtility forkUtility = new ForkUtility();
forkUtility.enableUnityFork(unityForkNumber);
IAionBlockchain blockchain = mock(IAionBlockchain.class);
Block bestBlock = mock(Block.class);
byte[] badBlockhash = ByteUtil.hexStringToBytes("0xbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbad0");
when(blockchain.isMainChain(badBlockhash)).thenReturn(false);
when(blockchain.getBestBlock()).thenReturn(bestBlock);
AionTransaction tx = AionTransaction.createWithoutKey(// nonce - any val
new byte[] { 1 }, new AionAddress(// sender - any val
ByteUtil.hexStringToBytes("0xa000000000000000000000000000000000000000000000000000000000000000")), new AionAddress(// destination - any val
ByteUtil.hexStringToBytes("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")), // value - any val
new byte[] { 1 }, // data - any val
new byte[] {}, // energyLimit - any val
1l, // energyPrice - any val
1l, // type - any val
(byte) 1, // beacon hash - does not exist in blockstore
badBlockhash);
BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
when(bestBlock.getNumber()).thenReturn(unityForkNumber - 2);
assertWithMessage("any beacon hash should fail for pending state if best_block_number+1 < unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
when(bestBlock.getNumber()).thenReturn(unityForkNumber - 1);
assertWithMessage("non-mainchain hash validation should fail for pending state if best_block_number+1 = unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
when(bestBlock.getNumber()).thenReturn(unityForkNumber);
assertWithMessage("non-mainchain hash validation should fail for pending state if best_block_number+1 > unityFork_number").that(unit.validateTxForPendingState(tx)).isFalse();
}
use of org.aion.base.AionTransaction in project aion by aionnetwork.
the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnSideChain.
@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnSideChain() {
/*
* Visualization of this case (x is the block of beacon hash , * is the new block):
*
* o--o--o---o--o main chain
* |
* \--x--o--* side chain
*/
long unityForkNumber = 5;
ForkUtility forkUtility = new ForkUtility();
forkUtility.enableUnityFork(unityForkNumber);
IAionBlockchain blockchain = mock(IAionBlockchain.class);
LinkedList<Block> sideChain = mockChain(unityForkNumber, 4, blockchain);
byte[] beaconHash = new byte[32];
System.arraycopy(sideChain.get(3).getHash(), 0, beaconHash, 0, 32);
AionTransaction tx = AionTransaction.createWithoutKey(// nonce - any val
new byte[] { 1 }, new AionAddress(// sender - any val
ByteUtil.hexStringToBytes("0xa000000000000000000000000000000000000000000000000000000000000000")), new AionAddress(// destination - any val
ByteUtil.hexStringToBytes("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")), // value - any val
new byte[] { 1 }, // data - any val
new byte[] {}, // energyLimit - any val
1l, // energyPrice - any val
1l, // type - any val
(byte) 1, // beacon hash
beaconHash);
BeaconHashValidator unit = new BeaconHashValidator(blockchain, forkUtility);
assertWithMessage("beacon hash on chain of new block should pass if block_number = unityFork_number").that(unit.validateTxForBlock(tx, sideChain.get(0))).isTrue();
// not doing all the "if new block > unityFork" and "new block < unityFork" cases
// for the side chain test cases because those paths already checked by the
// mainchain tests
}
Aggregations