Search in sources :

Example 91 with AionTxReceipt

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

the class FvmBalanceTransferConsensusTest method testTransferAfterCreatingContractToPayableFunction.

@Test
public void testTransferAfterCreatingContractToPayableFunction() {
    BigInteger amount = BigInteger.TEN.pow(17).add(BigInteger.valueOf(38_193));
    BigInteger initialBalance = getBalance(new AionAddress(SENDER_ADDR));
    assertEquals(SENDER_BALANCE, initialBalance);
    assertArrayEquals(MINER, this.blockchain.getMinerCoinbase().toByteArray());
    // Make the create transaction.
    AionTransaction transaction = makeCreatePayableContractTx();
    AionAddress contractAddress = TxUtil.calculateContractAddress(transaction);
    assertArrayEquals(Hex.decode(CONTRACT), contractAddress.toByteArray());
    // 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();
    assertTrue(receipt.isSuccessful());
    byte[] stateRoot = blockSummary.getBlock().getStateRoot();
    byte[] blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    byte[] receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    // Verify the consensus information.
    assertArrayEquals(Hex.decode(STATE_ROOT3), stateRoot);
    assertArrayEquals(Hex.decode(BLOCK_RECEIPTS_ROOT3), blockReceiptsRoot);
    assertArrayEquals(Hex.decode(RECEIPT_TRIE3), receiptTrieEncoded);
    // Make the balance transfer transaction.
    transaction = makeCallPayableFunctionTx(contractAddress, amount);
    // Process the transaction.
    results = processTransaction(transaction, 1);
    // Collect the consensus information from the block & receipt.
    blockSummary = results.getRight();
    receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertTrue(receipt.isSuccessful());
    stateRoot = blockSummary.getBlock().getStateRoot();
    blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    // Verify the consensus information.
    assertArrayEquals(Hex.decode(STATE_ROOT5), stateRoot);
    assertArrayEquals(Hex.decode(BLOCK_RECEIPTS_ROOT5), blockReceiptsRoot);
    assertArrayEquals(Hex.decode(RECEIPT_TRIE5), receiptTrieEncoded);
    // Verify the sender's balance is as expected.
    BigInteger expectedBalance = new BigInteger("999999897458394815058678");
    assertEquals(expectedBalance, getBalance(new AionAddress(SENDER_ADDR)));
    // Verify that the contract has the expected balance.
    assertEquals(amount, getBalance(contractAddress));
    // Verify that the miner has the expected balance.
    BigInteger expectedMinerBalance = new BigInteger("1500539557347676526");
    assertEquals(expectedMinerBalance, getBalance(new AionAddress(MINER)));
}
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) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 92 with AionTxReceipt

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

the class FvmBalanceTransferConsensusTest method testTransferAfterCreatingContractToNonPayableFunction.

@Test
public void testTransferAfterCreatingContractToNonPayableFunction() {
    BigInteger amount = BigInteger.TEN.pow(9).add(BigInteger.valueOf(3_124_587));
    BigInteger initialBalance = getBalance(new AionAddress(SENDER_ADDR));
    assertEquals(SENDER_BALANCE, initialBalance);
    assertArrayEquals(MINER, this.blockchain.getMinerCoinbase().toByteArray());
    // Make the create transaction.
    AionTransaction transaction = makeCreatePayableContractTx();
    AionAddress contractAddress = TxUtil.calculateContractAddress(transaction);
    assertArrayEquals(Hex.decode(CONTRACT), contractAddress.toByteArray());
    // 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();
    assertTrue(receipt.isSuccessful());
    byte[] stateRoot = blockSummary.getBlock().getStateRoot();
    byte[] blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    byte[] receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    // Verify the consensus information.
    assertArrayEquals(Hex.decode(STATE_ROOT3), stateRoot);
    assertArrayEquals(Hex.decode(BLOCK_RECEIPTS_ROOT3), blockReceiptsRoot);
    assertArrayEquals(Hex.decode(RECEIPT_TRIE3), receiptTrieEncoded);
    // Make the balance transfer transaction.
    transaction = makeCallNonpayableFunctionTx(contractAddress, amount);
    // Process the transaction.
    results = processTransaction(transaction, 1);
    // Collect the consensus information from the block & receipt.
    blockSummary = results.getRight();
    receipt = blockSummary.getSummaries().get(0).getReceipt();
    assertFalse(receipt.isSuccessful());
    stateRoot = blockSummary.getBlock().getStateRoot();
    blockReceiptsRoot = blockSummary.getBlock().getReceiptsRoot();
    receiptTrieEncoded = receipt.getReceiptTrieEncoded();
    // Verify the consensus information.
    assertArrayEquals(Hex.decode(STATE_ROOT4), stateRoot);
    assertArrayEquals(Hex.decode(BLOCK_RECEIPTS_ROOT4), blockReceiptsRoot);
    assertArrayEquals(Hex.decode(RECEIPT_TRIE4), receiptTrieEncoded);
    // Verify the sender's balance is as expected.
    BigInteger expectedBalance = new BigInteger("999999997458455555837605");
    assertEquals(expectedBalance, getBalance(new AionAddress(SENDER_ADDR)));
    // Verify that the contract has the expected balance.
    assertEquals(BigInteger.ZERO, getBalance(contractAddress));
    // Verify that the miner has the expected balance.
    BigInteger expectedMinerBalance = new BigInteger("1500539496606935792");
    assertEquals(expectedMinerBalance, getBalance(new AionAddress(MINER)));
}
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) AionTxReceipt(org.aion.base.AionTxReceipt) Test(org.junit.Test)

Example 93 with AionTxReceipt

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

the class AionPendingStateImpl method clearPending.

private void clearPending(Block block, List<AionTxReceipt> receipts) {
    List<AionTransaction> txList = block.getTransactionsList();
    LOGGER_TX.info("clearPending block#[{}] tx#[{}]", block.getNumber(), txList.size());
    if (!txList.isEmpty()) {
        Map<AionAddress, BigInteger> accountNonce = new HashMap<>();
        int cnt = 0;
        for (AionTransaction tx : txList) {
            accountNonce.computeIfAbsent(tx.getSenderAddress(), this::bestRepoNonce);
            LOGGER_TX.debug("Clear pending transaction, addr: {} hash: {}", tx.getSenderAddress().toString(), Hex.toHexString(tx.getTransactionHash()));
            AionTxReceipt receipt = null;
            if (receipts != null) {
                receipt = receipts.get(cnt);
            } else {
                AionTxInfo info = getTransactionInfo(tx.getTransactionHash(), block.getHash());
                if (info != null) {
                    receipt = info.getReceipt();
                }
            }
            removeBackupDBPendingTx(tx.getTransactionHash());
            if (receipt != null) {
                fireTxUpdate(receipt, PendingTransactionState.INCLUDED, block);
            }
            cnt++;
        }
        if (!accountNonce.isEmpty()) {
            txPool.removeTxsWithNonceLessThan(accountNonce);
        }
    }
}
Also used : AionAddress(org.aion.types.AionAddress) HashMap(java.util.HashMap) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) AionTxReceipt(org.aion.base.AionTxReceipt)

Example 94 with AionTxReceipt

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

the class AionPendingStateImpl method updateCachedTxToTxPool.

private void updateCachedTxToTxPool(Map<AionAddress, BigInteger> nonceMap) {
    List<AionTransaction> newPendingTransactions = pendingTxCache.getNewPendingTransactions(nonceMap);
    LOGGER_TX.debug("flushCachedTx - newPendingTxs#[{}]", newPendingTransactions.size());
    Set<AionAddress> updatedAddress = new HashSet<>();
    for (AionTransaction tx : newPendingTransactions) {
        if (txPool.isFull()) {
            LOGGER_TX.debug("flushCachedTx txPool is full, cannot add new pending transactions from the cachedPool");
            break;
        }
        LOGGER_TX.debug("flushCachedTx - loop: {}", tx);
        AionTxExecSummary txSum = executeTx(tx);
        AionTxReceipt receipt = txSum.getReceipt();
        receipt.setTransaction(tx);
        if (txSum.isRejected()) {
            LOGGER_TX.debug("Invalid transaction in cachedPool: {}", tx);
            fireTxUpdate(receipt, PendingTransactionState.DROPPED, currentBestBlock.get());
            pendingTxCache.removeTransaction(tx.getSenderAddress(), tx.getNonceBI());
        } else {
            PooledTransaction pTx = txPool.add(new PooledTransaction(tx, receipt.getEnergyUsed()));
            if (pTx != null) {
                fireTxUpdate(receipt, PendingTransactionState.PENDING, currentBestBlock.get());
                updatedAddress.add(tx.getSenderAddress());
                pendingTxCache.removeTransaction(tx.getSenderAddress(), tx.getNonceBI());
            }
        }
    }
    for (AionAddress addr : updatedAddress) {
        nonceMap.put(addr, bestPendingStateNonce(addr));
    }
}
Also used : AionAddress(org.aion.types.AionAddress) AionTxExecSummary(org.aion.base.AionTxExecSummary) AionTransaction(org.aion.base.AionTransaction) PooledTransaction(org.aion.base.PooledTransaction) AionTxReceipt(org.aion.base.AionTxReceipt) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 95 with AionTxReceipt

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

the class AionPendingStateImpl method fireDroppedTx.

private void fireDroppedTx(AionTransaction tx, String error) {
    AionTxReceipt rp = new AionTxReceipt();
    rp.setTransaction(tx);
    rp.setError(error);
    fireTxUpdate(rp, PendingTransactionState.DROPPED, currentBestBlock.get());
}
Also used : AionTxReceipt(org.aion.base.AionTxReceipt)

Aggregations

AionTxReceipt (org.aion.base.AionTxReceipt)111 Test (org.junit.Test)76 AionTransaction (org.aion.base.AionTransaction)74 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)61 AionAddress (org.aion.types.AionAddress)56 BigInteger (java.math.BigInteger)53 ImportResult (org.aion.zero.impl.core.ImportResult)50 MiningBlock (org.aion.zero.impl.types.MiningBlock)43 ArrayList (java.util.ArrayList)24 Block (org.aion.zero.impl.types.Block)24 AionTxExecSummary (org.aion.base.AionTxExecSummary)17 RepositoryCache (org.aion.base.db.RepositoryCache)17 StandaloneBlockchain (org.aion.zero.impl.blockchain.StandaloneBlockchain)15 Builder (org.aion.zero.impl.blockchain.StandaloneBlockchain.Builder)14 ECKey (org.aion.crypto.ECKey)12 SolidityType (org.aion.solidity.SolidityType)10 AccountState (org.aion.base.AccountState)9 Log (org.aion.types.Log)9 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)7 StakingBlock (org.aion.zero.impl.types.StakingBlock)7