Search in sources :

Example 21 with Block

use of org.aion.zero.impl.types.Block 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());
}
Also used : ImportResult(org.aion.zero.impl.core.ImportResult) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) AionTransaction(org.aion.base.AionTransaction) MiningBlock(org.aion.zero.impl.types.MiningBlock) Test(org.junit.Test)

Example 22 with Block

use of org.aion.zero.impl.types.Block 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();
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Example 23 with Block

use of org.aion.zero.impl.types.Block 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
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Example 24 with Block

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

the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnMainchainAfterSidechainFork.

@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnMainchainAfterSidechainFork() {
    /*
         * Visualization of this case (x is the block of beacon hash , * is the new block):
         *
         *   o--o--o---x--o     main chain
         *         |
         *          \--o--o--*  side chain
         */
    long unityForkNumber = 2;
    ForkUtility forkUtility = new ForkUtility();
    forkUtility.enableUnityFork(unityForkNumber);
    IAionBlockchain blockchain = mock(IAionBlockchain.class);
    // mocking up a block as if it were on the main chain but not the sidechain
    Block mainchainOnlyBlock = mock(Block.class);
    long mainchainOnlyBlockNum = 3l;
    byte[] mainchainOnlyBlockHash = ByteUtil.hexStringToBytes("0xcafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe");
    when(mainchainOnlyBlock.getNumber()).thenReturn(mainchainOnlyBlockNum);
    when(blockchain.isMainChain(mainchainOnlyBlockHash)).thenReturn(true);
    when(blockchain.getBlockByHash(mainchainOnlyBlockHash)).thenReturn(mainchainOnlyBlock);
    // need to set up the side chain so that its head block number is > mainchainOnlyBlockNum
    LinkedList<Block> sideChain = mockChain(unityForkNumber, 6, blockchain);
    byte[] beaconHash = new byte[32];
    System.arraycopy(mainchainOnlyBlockHash, 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 not on chain of new block should fail if block_number = unityFork_number").that(unit.validateTxForBlock(tx, sideChain.get(0))).isFalse();
// 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
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Example 25 with Block

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

the class BeaconHashValidatorTest method validateTxForBlockOnSidechainAndBeaconHashOnMainchainBeforeSidechainFork.

@Test
public void validateTxForBlockOnSidechainAndBeaconHashOnMainchainBeforeSidechainFork() {
    /*
         * Visualization of this case (x is the block of beacon hash , * is the new block):
         *
         *   o--x--o---o--o     main chain
         *         |
         *          \--o--o--*  side chain
         */
    long unityForkNumber = 2;
    ForkUtility forkUtility = new ForkUtility();
    forkUtility.enableUnityFork(unityForkNumber);
    IAionBlockchain blockchain = mock(IAionBlockchain.class);
    LinkedList<Block> sideChain = mockChain(unityForkNumber, 7, blockchain);
    when(blockchain.isMainChain(sideChain.get(2).getHash(), sideChain.get(2).getNumber())).thenReturn(true);
    when(blockchain.isMainChain(sideChain.get(4).getHash())).thenReturn(true);
    byte[] beaconHash = new byte[32];
    System.arraycopy(sideChain.get(4).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
}
Also used : AionAddress(org.aion.types.AionAddress) Block(org.aion.zero.impl.types.Block) IAionBlockchain(org.aion.zero.impl.blockchain.IAionBlockchain) AionTransaction(org.aion.base.AionTransaction) ForkUtility(org.aion.zero.impl.forks.ForkUtility) Test(org.junit.Test)

Aggregations

Block (org.aion.zero.impl.types.Block)283 MiningBlock (org.aion.zero.impl.types.MiningBlock)155 Test (org.junit.Test)148 AionTransaction (org.aion.base.AionTransaction)106 ImportResult (org.aion.zero.impl.core.ImportResult)86 ArrayList (java.util.ArrayList)63 AionAddress (org.aion.types.AionAddress)61 StakingBlock (org.aion.zero.impl.types.StakingBlock)58 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)57 BigInteger (java.math.BigInteger)55 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)34 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)30 AionTxReceipt (org.aion.base.AionTxReceipt)29 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)28 AccountState (org.aion.base.AccountState)26 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)26 JSONArray (org.json.JSONArray)26 JSONObject (org.json.JSONObject)26 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 AionTxExecSummary (org.aion.base.AionTxExecSummary)20