Search in sources :

Example 76 with AionTransaction

use of org.aion.base.AionTransaction 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 77 with AionTransaction

use of org.aion.base.AionTransaction 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)

Example 78 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class AionTransactionTest method testBeaconHashAbsent.

@Test
public void testBeaconHashAbsent() {
    byte[] nonce = RandomUtils.nextBytes(16);
    AionAddress to = new AionAddress(RandomUtils.nextBytes(32));
    byte[] value = RandomUtils.nextBytes(16);
    byte[] data = RandomUtils.nextBytes(64);
    long nrg = 0;
    long nrgPrice = 0;
    byte type = 0;
    AionTransaction tx = AionTransaction.create(key, nonce, to, value, data, nrg, nrgPrice, type, null);
    assertNull("beacon hash should be null", tx.getBeaconHash());
    SharedRLPList decoded = (SharedRLPList) RLP.decode2SharedList(tx.getEncoded()).get(0);
    assertEquals("wrong number of elements in RLP encoding of AionTransaction without beacon hash", TxUtil.RLP_TX_SIG, decoded.size() - 1);
    AionTransaction tx2 = TxUtil.decodeUsingRlpSharedList(tx.getEncoded());
    assertNotNull(tx2);
    assertTransactionEquals(tx, tx2);
}
Also used : AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) SharedRLPList(org.aion.rlp.SharedRLPList) Test(org.junit.Test)

Example 79 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class TXValidatorTest method validateTxSignatureSwapForkEnabledReceiverAddressTest.

@Test
public void validateTxSignatureSwapForkEnabledReceiverAddressTest() {
    boolean signatureSwapEnabled = true;
    byte[] dest = RandomUtils.nextBytes(32);
    dest[0] = (byte) 0xa1;
    AionTransaction tx = AionTransaction.create(key, nonce, new AionAddress(dest), value, data, nrg, nrgPrice, type, null);
    TxResponse response = TXValidator.validateTx(tx, unityForkEnabled, signatureSwapEnabled);
    assertEquals(TxResponse.INVALID_TX_DESTINATION, response);
}
Also used : AionAddress(org.aion.types.AionAddress) AionTransaction(org.aion.base.AionTransaction) TxResponse(org.aion.zero.impl.types.TxResponse) Test(org.junit.Test)

Example 80 with AionTransaction

use of org.aion.base.AionTransaction in project aion by aionnetwork.

the class TXValidatorTest method validateTxSignatureSwapForkEnabledReceiverAddressIsPreCompiledTest.

@Test
public void validateTxSignatureSwapForkEnabledReceiverAddressIsPreCompiledTest() {
    boolean signatureSwapEnabled = true;
    for (ContractInfo contractInfo : ContractInfo.values()) {
        AionTransaction tx = AionTransaction.create(key, nonce, contractInfo.contractAddress, value, data, nrg, nrgPrice, type, null);
        TxResponse response = TXValidator.validateTx(tx, unityForkEnabled, signatureSwapEnabled);
        // Note: TOTAL CURRENCY contract has been disabled.
        if (contractInfo.contractAddress.equals(ContractInfo.TOTAL_CURRENCY.contractAddress)) {
            assertEquals(TxResponse.INVALID_TX_DESTINATION, response);
        } else {
            assertEquals(TxResponse.SUCCESS, response);
        }
    }
}
Also used : ContractInfo(org.aion.precompiled.ContractInfo) AionTransaction(org.aion.base.AionTransaction) TxResponse(org.aion.zero.impl.types.TxResponse) Test(org.junit.Test)

Aggregations

AionTransaction (org.aion.base.AionTransaction)437 Test (org.junit.Test)308 AionAddress (org.aion.types.AionAddress)273 BigInteger (java.math.BigInteger)174 MiningBlock (org.aion.zero.impl.types.MiningBlock)149 ArrayList (java.util.ArrayList)127 ImportResult (org.aion.zero.impl.core.ImportResult)115 AionTxExecSummary (org.aion.base.AionTxExecSummary)103 Block (org.aion.zero.impl.types.Block)102 RepositoryCache (org.aion.base.db.RepositoryCache)89 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)87 AionTxReceipt (org.aion.base.AionTxReceipt)75 ECKey (org.aion.crypto.ECKey)52 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)46 BlockContext (org.aion.zero.impl.types.BlockContext)43 PooledTransaction (org.aion.base.PooledTransaction)40 AccountState (org.aion.base.AccountState)39 Properties (java.util.Properties)35 HashMap (java.util.HashMap)33 DataWord (org.aion.util.types.DataWord)29