Search in sources :

Example 6 with AionBlock

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

the class BlockchainIntegrationTest method testSimpleOneTokenBalanceTransfer.

@Test
public void testSimpleOneTokenBalanceTransfer() {
    // generate a recipient
    final Address receiverAddress = Address.wrap(ByteUtil.hexStringToBytes("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE"));
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    final ECKey sender = bundle.privateKeys.get(0);
    final BigInteger senderInitialBalance = bc.getRepository().getBalance(Address.wrap(sender.getAddress()));
    AionTransaction tx = new AionTransaction(BigInteger.valueOf(0).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 21000L, 1L);
    tx.sign(sender);
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    assertThat(block.getTransactionsList().size()).isEqualTo(1);
    assertThat(block.getTransactionsList().get(0)).isEqualTo(tx);
    ImportResult connection = bc.tryToConnect(block);
    assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
    // to be sure, perform some DB tests
    IRepository repo = bc.getRepository();
    assertThat(repo.getBalance(receiverAddress)).isEqualTo(BigInteger.valueOf(100));
    assertThat(repo.getBalance(Address.wrap(sender.getAddress()))).isEqualTo(senderInitialBalance.subtract(BigInteger.valueOf(21000)).subtract(BigInteger.valueOf(100)));
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) IRepository(org.aion.base.db.IRepository) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 7 with AionBlock

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

the class BlockchainIntegrationTest method testPruningEnabledBalanceTransfer.

@Ignore
@Test
public void testPruningEnabledBalanceTransfer() {
    // generate a recipient
    final Address receiverAddress = Address.wrap(ByteUtil.hexStringToBytes("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE"));
    // generate bc bundle with pruning enabled
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withBlockPruningEnabled().withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    // desginate the first account in our list of private keys as the sender
    // (each key in the bundle is preloaded with balance)
    final ECKey sender = bundle.privateKeys.get(0);
    // generate transaction that transfers 100 tokens from sender to receiver
    // pk[0] -> receiverAddress
    AionTransaction tx = new AionTransaction(BigInteger.valueOf(0).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 21000L, 1L);
    tx.sign(sender);
    // create a new block containing a single transaction (tx)
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    // import the block to our blockchain
    ImportResult connection = bc.tryToConnect(block);
    assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with AionBlock

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

the class BlockchainRewardTest method testBlockchainRewardMonotonicallyIncreasing.

/**
 * Test that blocks between the lower and upper bounds follow
 * a certain function [0, 259200]
 *
 * Note: this test is resource consuming!
 *
 * Check {@link org.aion.zero.impl.core.RewardsCalculator} for algorithm related
 * to the ramp-up block time
 */
@Ignore
@Test
public void testBlockchainRewardMonotonicallyIncreasing() {
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = bundle.bc;
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    ImportResult res = bc.tryToConnect(block);
    assertThat(res).isEqualTo(ImportResult.IMPORTED_BEST);
    Address coinbase = block.getCoinbase();
    BigInteger previousBalance = bc.getRepository().getBalance(coinbase);
    // first block already sealed
    for (int i = 2; i < 99999; i++) {
        AionBlock b = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
        ImportResult r = bc.tryToConnect(b);
        assertThat(r).isEqualTo(ImportResult.IMPORTED_BEST);
        // note the assumption here that blocks are mined by one coinbase
        BigInteger balance = bc.getRepository().getBalance(coinbase);
        assertThat(balance).isGreaterThan(previousBalance);
        previousBalance = balance;
        if (b.getNumber() % 1000 == 0)
            System.out.println("added block #: " + i);
    }
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with AionBlock

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

the class AionBlockLoader method loadBlocks.

public void loadBlocks() {
    exec1 = new ExecutorPipeline<AionBlock, AionBlock>(8, 1000, true, new Functional.Function<AionBlock, AionBlock>() {

        @Override
        public AionBlock apply(AionBlock b) {
            for (AionTransaction tx : b.getTransactionsList()) {
                tx.getFrom();
            }
            return b;
        }
    }, new Functional.Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) {
            logger.error("Unhandled exception: ", throwable);
        }
    });
    exec2 = exec1.add(1, 1000, new Functional.Consumer<AionBlock>() {

        @Override
        public void accept(AionBlock block) {
            try {
                blockWork(block);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    try {
        exec1.join();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    blockchain.flush();
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 10 with AionBlock

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

the class AionBlockchainImpl method getTransactionInfo.

@Override
public /* NOTE: only returns receipts from the main chain
     */
AionTxInfo getTransactionInfo(byte[] hash) {
    List<AionTxInfo> infos = transactionStore.get(hash);
    if (infos == null || infos.isEmpty()) {
        return null;
    }
    AionTxInfo txInfo = null;
    if (infos.size() == 1) {
        txInfo = infos.get(0);
    } else {
        // pick up the receipt from the block on the main chain
        for (AionTxInfo info : infos) {
            AionBlock block = getBlockStore().getBlockByHash(info.getBlockHash());
            if (block == null)
                continue;
            AionBlock mainBlock = getBlockStore().getChainBlockByNumber(block.getNumber());
            if (mainBlock == null)
                continue;
            if (FastByteComparisons.equal(info.getBlockHash(), mainBlock.getHash())) {
                txInfo = info;
                break;
            }
        }
    }
    if (txInfo == null) {
        LOG.warn("Can't find block from main chain for transaction " + Hex.toHexString(hash));
        return null;
    }
    AionTransaction tx = this.getBlockByHash(txInfo.getBlockHash()).getTransactionsList().get(txInfo.getIndex());
    txInfo.setTransaction(tx);
    return txInfo;
}
Also used : AionTxInfo(org.aion.zero.impl.types.AionTxInfo) AionBlock(org.aion.zero.impl.types.AionBlock)

Aggregations

AionBlock (org.aion.zero.impl.types.AionBlock)49 Test (org.junit.Test)16 ImportResult (org.aion.mcf.core.ImportResult)15 AionTransaction (org.aion.zero.types.AionTransaction)11 BigInteger (java.math.BigInteger)9 Address (org.aion.base.type.Address)6 ArrayList (java.util.ArrayList)5 JSONObject (org.json.JSONObject)5 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)3 TrieImpl (org.aion.mcf.trie.TrieImpl)3 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)3 A0BlockHeader (org.aion.zero.types.A0BlockHeader)3 Map (java.util.Map)2 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)2 ECKey (org.aion.crypto.ECKey)2 IEvent (org.aion.evtmgr.IEvent)2 LRUMap (org.apache.commons.collections4.map.LRUMap)2 JSONArray (org.json.JSONArray)2 Ignore (org.junit.Ignore)2