Search in sources :

Example 1 with AionTransaction

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

the class PendingTxCacheV1 method removeSealedTransactions.

/**
 * @implNote remove the cached transactions base on the account nonce updated by the block import.
 * @param nonceMap The account with the latest nonce.
 * @return The transaction has been removed in the pending tx cache.
 */
public List<AionTransaction> removeSealedTransactions(Map<AionAddress, BigInteger> nonceMap) {
    Objects.requireNonNull(nonceMap);
    Objects.requireNonNull(nonceMap.entrySet());
    lock.lock();
    try {
        List<AionTransaction> txList = new ArrayList<>();
        for (Entry<AionAddress, BigInteger> e : nonceMap.entrySet()) {
            AionAddress address = e.getKey();
            SortedMap<BigInteger, AionTransaction> accountCachedTx = cacheTxMap.get(address);
            if (accountCachedTx != null) {
                BigInteger nonce = e.getValue();
                Map<BigInteger, AionTransaction> flushedTxMap = accountCachedTx.headMap(nonce);
                if (!flushedTxMap.isEmpty()) {
                    for (AionTransaction tx : flushedTxMap.values()) {
                        removeTxInTimeoutMap(tx);
                        addTransactionToRemovedTransactionForPoolBackup(tx);
                    }
                    txList.addAll(flushedTxMap.values());
                }
                cacheTxMap.get(address).headMap(nonce).clear();
                if (cacheTxMap.get(address).isEmpty()) {
                    cacheTxMap.remove(address);
                }
            }
        }
        // Update the timeout cached Tx
        txList.addAll(flushTimeoutTx(false));
        LOG.info("cacheTx.flush cacheTx# {}", cacheTxSize());
        return txList;
    } finally {
        lock.unlock();
    }
}
Also used : AionAddress(org.aion.types.AionAddress) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction)

Example 2 with AionTransaction

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

the class BlockUtil method newBlockFromSharedRLPList.

public static Block newBlockFromSharedRLPList(SharedRLPList rlpList) {
    // return null when given empty bytes
    if (rlpList == null || rlpList.isEmpty()) {
        return null;
    }
    try {
        SharedRLPList header = (SharedRLPList) rlpList.get(0);
        List<AionTransaction> txs = parseTransactions((SharedRLPList) rlpList.get(1));
        byte[] sealType = header.get(0).getRLPData();
        if (sealType[0] == Seal.PROOF_OF_WORK.getSealId()) {
            MiningBlockHeader miningHeader = MiningBlockHeader.Builder.newInstance().withRlpList(header).build();
            return new MiningBlock(miningHeader, txs);
        } else if (sealType[0] == Seal.PROOF_OF_STAKE.getSealId()) {
            StakingBlockHeader stakingHeader = StakingBlockHeader.Builder.newInstance().withRlpList(header).build();
            return new StakingBlock(stakingHeader, txs);
        } else {
            return null;
        }
    } catch (Exception e) {
        genLog.warn("Unable to decode block bytes " + Arrays.toString(SharedRLPList.getRLPDataCopy(rlpList)), e);
        return null;
    }
}
Also used : AionTransaction(org.aion.base.AionTransaction) SharedRLPList(org.aion.rlp.SharedRLPList)

Example 3 with AionTransaction

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

the class AbstractBlock method getTransactionsEncoded.

private byte[] getTransactionsEncoded() {
    byte[][] transactionsEncoded = new byte[transactionsList.size()][];
    int i = 0;
    for (AionTransaction tx : transactionsList) {
        transactionsEncoded[i] = tx.getEncoded();
        ++i;
    }
    return RLP.encodeList(transactionsEncoded);
}
Also used : AionTransaction(org.aion.base.AionTransaction)

Example 4 with AionTransaction

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

the class FvmBalanceTransferConsensusTest method testTransferToPrecompiledContract.

@Test
public void testTransferToPrecompiledContract() {
    BigInteger amount = BigInteger.TEN.pow(12).add(BigInteger.valueOf(293_865));
    BigInteger initialBalance = getBalance(new AionAddress(SENDER_ADDR));
    assertThat(initialBalance).isEqualTo(SENDER_BALANCE);
    assertThat(this.blockchain.getMinerCoinbase().toByteArray()).isEqualTo(MINER);
    // ensure bridge is viewed as precompiled contract
    AionAddress bridge = AddressUtils.wrapAddress("0000000000000000000000000000000000000000000000000000000000000200");
    assertThat(ContractInfo.isPrecompiledContract(bridge)).isTrue();
    // Make balance transfer transaction to precompiled contract.
    ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
    AionTransaction transaction = AionTransaction.create(key, BigInteger.ZERO.toByteArray(), bridge, amount.toByteArray(), new byte[] {}, 2_000_000, ENERGY_PRICE, TransactionTypes.DEFAULT, null);
    // Process the transaction.
    Pair<ImportResult, AionBlockSummary> results = processTransaction(transaction, 1);
    // Collect the consensus information from the block & receipt.
    AionBlockSummary blockSummary = results.getRight();
    AionTxReceipt receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertThat(receipt.isSuccessful()).isTrue();
    assertThat(receipt.getEnergyUsed()).isEqualTo(21000);
    byte[] stateRoot = blockSummary.getBlock().getStateRoot();
    byte[] blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    byte[] receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    // Verify the consensus information.
    String expectedRoot = "419F9A4C02612094A9B9A2EAC5CC6DF761214882C35267328297D09246206A9A";
    String expectedReceiptsRoot = "9A73184E9A5514164D980452E2283892D0487B6833D3F9F782C8686BFDA1B809";
    String expectedReceiptsTrie = "f90125a034b98e6a317bd93f39959d41cccafec864329cadfb0d8d7bfc5d30df5612e401b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0";
    assertThat(stateRoot).isEqualTo(Hex.decode(expectedRoot));
    assertThat(blockReceiptsRoot).isEqualTo(Hex.decode(expectedReceiptsRoot));
    assertThat(receiptTrieEncoded).isEqualTo(Hex.decode(expectedReceiptsTrie));
    // Verify that the sender has the expected balance.
    BigInteger expectedBalance = new BigInteger("999999999786407407137135");
    assertThat(getBalance(new AionAddress(SENDER_ADDR))).isEqualTo(expectedBalance);
    // Verify that the contract has the expected balance.
    BigInteger contractBalance = getBalance(bridge);
    assertThat(contractBalance).isEqualTo(amount);
    // Verify that the miner has the expected balance.
    BigInteger expectedMinerBalance = new BigInteger("749210123854045163");
    assertThat(getBalance(new AionAddress(MINER))).isEqualTo(expectedMinerBalance);
}
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) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 5 with AionTransaction

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

the class FvmBalanceTransferConsensusTest method testCallToPrecompiledContract.

@Test
public void testCallToPrecompiledContract() {
    BigInteger initialBalance = getBalance(new AionAddress(SENDER_ADDR));
    assertThat(initialBalance).isEqualTo(SENDER_BALANCE);
    assertThat(this.blockchain.getMinerCoinbase().toByteArray()).isEqualTo(MINER);
    // ensure bridge is viewed as precompiled contract
    AionAddress bridge = AddressUtils.wrapAddress("0000000000000000000000000000000000000000000000000000000000000200");
    assertThat(ContractInfo.isPrecompiledContract(bridge)).isTrue();
    // Make call transaction to precompiled contract.
    ECKey key = org.aion.crypto.ECKeyFac.inst().fromPrivate(SENDER_KEY);
    AionTransaction transaction = AionTransaction.create(key, BigInteger.ZERO.toByteArray(), bridge, BigInteger.ZERO.toByteArray(), Hex.decode("a6f9dae1a048613dd3cb89685cb3f9cfa410ecf606c7ec7320e721edacd194050828c6b0"), 2_000_000, ENERGY_PRICE, TransactionTypes.DEFAULT, null);
    // Process the transaction.
    Pair<ImportResult, AionBlockSummary> results = processTransaction(transaction, 1);
    // Collect the consensus information from the block & receipt.
    AionBlockSummary blockSummary = results.getRight();
    AionTxReceipt receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertThat(receipt.isSuccessful()).isTrue();
    assertThat(receipt.getEnergyUsed()).isEqualTo(23304);
    byte[] stateRoot = blockSummary.getBlock().getStateRoot();
    byte[] blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    byte[] receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    // Verify the consensus information.
    String expectedRoot = "7D91DD44FC7AABC0291BB6FD9CBCD70BEB3859FDD10E0F2356D2B5947EBC55D7";
    String expectedReceiptsRoot = "E897C7EB2531B5CC45293AE3EC0CC156B0FACEAEF3046D671A08D6C91BA88827";
    String expectedReceiptsTrie = "f90125a06ed1cbe0a46be351c90404aaeb7d12d9737d4c8a070e8c63dc1cfc0d7441fbdbb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0";
    assertThat(stateRoot).isEqualTo(Hex.decode(expectedRoot));
    assertThat(blockReceiptsRoot).isEqualTo(Hex.decode(expectedReceiptsRoot));
    assertThat(receiptTrieEncoded).isEqualTo(Hex.decode(expectedReceiptsTrie));
    // Verify that the sender has the expected balance.
    BigInteger expectedBalance = new BigInteger("999999999764082962989144");
    assertThat(getBalance(new AionAddress(SENDER_ADDR))).isEqualTo(expectedBalance);
    // Verify that the contract has the expected balance.
    BigInteger contractBalance = getBalance(bridge);
    assertThat(contractBalance).isEqualTo(BigInteger.ZERO);
    // Verify that the miner has the expected balance.
    BigInteger expectedMinerBalance = new BigInteger("749233448298487019");
    assertThat(getBalance(new AionAddress(MINER))).isEqualTo(expectedMinerBalance);
}
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) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) 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