Search in sources :

Example 41 with AionBlock

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

the class BlockchainIntegrationTest method testSimpleFailedTransactionInsufficientBalance.

@Test
public void testSimpleFailedTransactionInsufficientBalance() {
    // 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;
    // (byte[] nonce, byte[] from, byte[] to, byte[] value, byte[] data)
    AionTransaction tx = new AionTransaction(BigInteger.valueOf(1).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 1L, 1L);
    tx.sign(bundle.privateKeys.get(0));
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    assertThat(block.getTransactionsList()).isEmpty();
    assertThat(block.getTxTrieRoot()).isEqualTo(HashUtil.EMPTY_TRIE_HASH);
    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) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 42 with AionBlock

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

the class BlockchainIntegrationTest method testCreateNewEmptyBlock.

@Test
public void testCreateNewEmptyBlock() {
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    assertThat(block.getParentHash()).isEqualTo(bc.getGenesis().getHash());
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 43 with AionBlock

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

the class ApiAion method getTransactionCount.

public long getTransactionCount(Address addr, long blkNr) {
    AionBlock pBlk = this.getBlock(blkNr);
    if (pBlk == null) {
        LOG.error("ApiAion.getTransactionByBlockNumberAndIndex - can't find the block by the block number");
        return -1;
    }
    long cnt = 0;
    List<AionTransaction> txList = pBlk.getTransactionsList();
    for (AionTransaction tx : txList) {
        if (addr.equals(tx.getFrom())) {
            cnt++;
        }
    }
    return cnt;
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 44 with AionBlock

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

the class ApiAion method getBlockTemplate.

public AionBlock getBlockTemplate() {
    // TODO: Change to follow onBlockTemplate event mode defined in internal
    // miner
    // TODO: Track multiple block templates
    AionBlock bestPendingState = ((AionPendingStateImpl) ac.getAionHub().getPendingState()).getBestBlock();
    AionPendingStateImpl.TransactionSortedSet ret = new AionPendingStateImpl.TransactionSortedSet();
    ret.addAll(ac.getAionHub().getPendingState().getPendingTransactions());
    return ac.getAionHub().getBlockchain().createNewBlock(bestPendingState, new ArrayList<>(ret), false);
}
Also used : AionPendingStateImpl(org.aion.zero.impl.blockchain.AionPendingStateImpl) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 45 with AionBlock

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

the class AionBlockchainImpl method createNewBlock.

public synchronized AionBlock createNewBlock(AionBlock parent, List<AionTransaction> txs, boolean waitUntilBlockTime) {
    long time = System.currentTimeMillis() / THOUSAND_MS;
    if (parent.getTimestamp() >= time) {
        time = parent.getTimestamp() + 1;
        while (waitUntilBlockTime && System.currentTimeMillis() / THOUSAND_MS <= time) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
    long energyLimit = this.chainConfiguration.calcEnergyLimit(parent.getHeader());
    A0BlockHeader.Builder headerBuilder = new A0BlockHeader.Builder();
    headerBuilder.withParentHash(parent.getHash()).withCoinbase(minerCoinbase).withNumber(parent.getNumber() + 1).withTimestamp(time).withExtraData(minerExtraData).withTxTrieRoot(calcTxTrie(txs)).withEnergyLimit(energyLimit);
    AionBlock block = new AionBlock(headerBuilder.build(), txs);
    block.getHeader().setDifficulty(ByteUtil.bigIntegerToBytes(this.chainConfiguration.getDifficultyCalculator().calculateDifficulty(block.getHeader(), parent.getHeader()), DIFFICULTY_BYTES));
    /*
         * Begin execution phase
         */
    pushState(parent.getHash());
    track = repository.startTracking();
    track.rollback();
    RetValidPreBlock preBlock = generatePreBlock(block);
    /*
         * Calculate the gas used for the included transactions
         */
    long totalEnergyUsed = 0;
    for (AionTxExecSummary summary : preBlock.summaries) {
        totalEnergyUsed = totalEnergyUsed + summary.getNrgUsed().longValueExact();
    }
    byte[] stateRoot = getRepository().getRoot();
    popState();
    /*
         * End execution phase
         */
    Bloom logBloom = new Bloom();
    for (AionTxReceipt receipt : preBlock.receipts) {
        logBloom.or(receipt.getBloomFilter());
    }
    block.seal(preBlock.txs, calcTxTrie(preBlock.txs), stateRoot, logBloom.getData(), calcReceiptsTrie(preBlock.receipts), totalEnergyUsed);
    return block;
}
Also used : RetValidPreBlock(org.aion.zero.impl.types.RetValidPreBlock) Bloom(org.aion.mcf.vm.types.Bloom) 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