Search in sources :

Example 91 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class PendingStateTest method replayTransactionWithDoubleEnergyPriceAfterSealing.

@Test
public void replayTransactionWithDoubleEnergyPriceAfterSealing() {
    AionTransaction tx1 = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 1000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionTransaction tx2 = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 1000_000L, energyPrice * 2, TransactionTypes.DEFAULT, null);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx1));
    assertEquals(TxResponse.REPAID, pendingState.addTransactionFromApiServer(tx2));
    assertEquals(1, pendingState.getPendingTxSize());
    // tx2 will get cached and will replace tx1 if tx1 is not included in the next block.
    assertEquals(pendingState.getPendingTransactions().get(0), tx1);
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), pendingState.getPendingTransactions(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    pendingState.applyBlockUpdate(block, connectResult.getRight().getReceipts());
    assertEquals(0, pendingState.getPendingTxSize());
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 92 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class PendingStateTest method replayTransactionWithDoubleEnergyPrice.

@Test
public void replayTransactionWithDoubleEnergyPrice() {
    AionTransaction tx1 = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 1000_000L, energyPrice, TransactionTypes.DEFAULT, null);
    AionTransaction tx2 = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 1000_000L, energyPrice * 2, TransactionTypes.DEFAULT, null);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx1));
    assertEquals(TxResponse.REPAID, pendingState.addTransactionFromApiServer(tx2));
    assertEquals(1, pendingState.getPendingTxSize());
    // tx2 will get cached and will replace tx1 if tx1 is not included in the next block.
    assertEquals(pendingState.getPendingTransactions().get(0), tx1);
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), Collections.emptyList(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    (pendingState).applyBlockUpdate(block, connectResult.getRight().getReceipts());
    assertEquals(pendingState.getPendingTransactions().get(0), tx2);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 93 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class PendingStateTest method replayTransactionThatUsesEntireBalance.

@Test
public void replayTransactionThatUsesEntireBalance() {
    BigInteger balance = blockchain.getRepository().getBalance(new AionAddress(deployerKey.getAddress()));
    BigInteger value = balance.subtract(BigInteger.valueOf(21000 * 3).multiply(BigInteger.valueOf(energyPrice)));
    AionTransaction tx1 = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), value.toByteArray(), new byte[0], 21000, energyPrice, TransactionTypes.DEFAULT, null);
    AionTransaction tx2 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), new byte[0], 21000, energyPrice, TransactionTypes.DEFAULT, null);
    /* tx1 and tx3 should use the entire balance. If tx2 is removed properly, tx3 should be
         * able to replace it in the pending state */
    AionTransaction tx3 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), new byte[0], 21000, energyPrice * 2, TransactionTypes.DEFAULT, null);
    // This would execute fine on top of tx2, but should have insufficient balance on top of tx3
    AionTransaction tx4 = AionTransaction.create(deployerKey, BigInteger.TWO.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), new byte[0], 21000, energyPrice, TransactionTypes.DEFAULT, null);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx1));
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx2));
    assertEquals(TxResponse.REPAID, pendingState.addTransactionFromApiServer(tx3));
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx4));
    assertEquals(3, pendingState.getPendingTxSize());
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), Collections.emptyList(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    pendingState.applyBlockUpdate(block, connectResult.getRight().getReceipts());
    // tx3 should replace tx2, and tx4 will now have insufficient funds so it will get dropped
    assertEquals(2, pendingState.getPendingTxSize());
    assertEquals(pendingState.getPendingTransactions().get(1), tx3);
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 94 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class PendingStateTest method replayInvalidTransactionInMiddle.

@Test
public void replayInvalidTransactionInMiddle() {
    BigInteger balance = blockchain.getRepository().getBalance(new AionAddress(deployerKey.getAddress()));
    BigInteger value = balance.subtract(BigInteger.valueOf(21000 * 3).multiply(BigInteger.valueOf(energyPrice)));
    AionTransaction tx1 = AionTransaction.create(deployerKey, BigInteger.ZERO.toByteArray(), new AionAddress(new byte[32]), value.toByteArray(), new byte[0], 21000, energyPrice, TransactionTypes.DEFAULT, null);
    AionTransaction tx2 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), new byte[0], 21000, energyPrice, TransactionTypes.DEFAULT, null);
    AionTransaction tx3 = AionTransaction.create(deployerKey, BigInteger.ONE.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), new byte[0], 21000, energyPrice * 4, TransactionTypes.DEFAULT, null);
    // This tx will get dropped after tx3 is rejected
    AionTransaction tx4 = AionTransaction.create(deployerKey, BigInteger.TWO.toByteArray(), new AionAddress(new byte[32]), BigInteger.ZERO.toByteArray(), new byte[0], 21000, energyPrice, TransactionTypes.DEFAULT, null);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx1));
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx2));
    assertEquals(TxResponse.REPAID, pendingState.addTransactionFromApiServer(tx3));
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(tx4));
    assertEquals(3, pendingState.getPendingTxSize());
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), Collections.emptyList(), false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    pendingState.applyBlockUpdate(block, connectResult.getRight().getReceipts());
    assertEquals(1, pendingState.getPendingTxSize());
}
Also used : AionAddress(org.aion.types.AionAddress) ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 95 with MiningBlock

use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.

the class PendingStateTest method updateCacheTransactionsTest.

@Test
public void updateCacheTransactionsTest() {
    List<AionTransaction> transactions = getMockTransaction(0, 2, 0);
    List<AionTransaction> cachedTx = getMockTransaction(2, 1, 0);
    assertEquals(TxResponse.SUCCESS, pendingState.addTransactionFromApiServer(transactions.get(0)));
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(TxResponse.CACHED_NONCE, pendingState.addTransactionFromApiServer(cachedTx.get(0)));
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(1, pendingState.getCachePoolSize());
    MiningBlock block = blockchain.createNewMiningBlock(blockchain.getBestBlock(), transactions, false);
    Pair<ImportResult, AionBlockSummary> connectResult = blockchain.tryToConnectAndFetchSummary(block);
    assertEquals(connectResult.getLeft(), ImportResult.IMPORTED_BEST);
    assertEquals(1, pendingState.getPendingTxSize());
    assertEquals(0, pendingState.getCachePoolSize());
    assertEquals(cachedTx.get(0), pendingState.getPendingTransactions().get(0));
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Aggregations

MiningBlock (org.aion.zero.impl.types.MiningBlock)185 Test (org.junit.Test)136 AionTransaction (org.aion.base.AionTransaction)128 ImportResult (org.aion.zero.impl.core.ImportResult)81 AionAddress (org.aion.types.AionAddress)79 BigInteger (java.math.BigInteger)64 Block (org.aion.zero.impl.types.Block)63 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)59 RepositoryCache (org.aion.base.db.RepositoryCache)41 AionTxReceipt (org.aion.base.AionTxReceipt)37 ArrayList (java.util.ArrayList)36 ECKey (org.aion.crypto.ECKey)27 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)27 AionTxExecSummary (org.aion.base.AionTxExecSummary)25 AionRepositoryCache (org.aion.zero.impl.db.AionRepositoryCache)24 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 BlockContext (org.aion.zero.impl.types.BlockContext)18 DataWord (org.aion.util.types.DataWord)13 MockDB (org.aion.db.impl.mockdb.MockDB)12 StakingBlock (org.aion.zero.impl.types.StakingBlock)11