Search in sources :

Example 31 with AionBlock

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

the class ApiWeb3Aion method eth_getTransactionByBlockHashAndIndex.

public Object eth_getTransactionByBlockHashAndIndex(String _blockHash, String _index) {
    byte[] hash = ByteUtil.hexStringToBytes(_blockHash);
    if (_blockHash == null || hash == null)
        return null;
    AionBlock b = this.ac.getBlockchain().getBlockByHash(hash);
    if (b == null)
        return null;
    List<AionTransaction> txs = b.getTransactionsList();
    int idx = Integer.decode(_index);
    if (idx >= txs.size())
        return null;
    return Tx.AionTransactionToJSON(txs.get(idx), b, idx);
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 32 with AionBlock

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

the class ApiWeb3Aion method eth_call.

public Object eth_call(JSONObject _tx, Object _bnOrId) {
    ArgTxCall txParams = ArgTxCall.fromJSON(_tx, getRecommendedNrgPrice(), getDefaultNrgLimit());
    String bnOrId = "latest";
    if (_bnOrId != null && !_bnOrId.equals(null))
        bnOrId = _bnOrId + "";
    Long bn = parseBnOrId(bnOrId);
    if (bn == null || bn < 0)
        return null;
    AionTransaction tx = new AionTransaction(txParams.getNonce().toByteArray(), txParams.getTo(), txParams.getValue().toByteArray(), txParams.getData(), txParams.getNrg(), txParams.getNrgPrice());
    AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
    AionTxReceipt receipt = this.ac.callConstant(tx, b);
    return TypeConverter.toJsonHex(receipt.getExecutionResult());
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) ByteUtil.toHexString(org.aion.base.util.ByteUtil.toHexString) AionTxReceipt(org.aion.zero.types.AionTxReceipt) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 33 with AionBlock

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

the class NrgOracle method processBlock.

private void processBlock(AionBlockSummary blockSummary) {
    AionBlock blk = (AionBlock) blockSummary.getBlock();
    advisor.processBlock(blk);
    cacheFlushCounter--;
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Example 34 with AionBlock

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

the class ApiAion0 method getRsp_getBlockDetails.

private List<Message.t_BlockDetail> getRsp_getBlockDetails(List<Map.Entry<AionBlock, BigInteger>> blks) {
    List<Message.t_BlockDetail> bds = blks.parallelStream().filter(Objects::nonNull).map(blk -> {
        AionBlock b = blk.getKey();
        Message.t_BlockDetail.Builder builder = Message.t_BlockDetail.newBuilder().setBlockNumber(b.getNumber()).setDifficulty(ByteString.copyFrom(b.getDifficulty())).setExtraData(ByteString.copyFrom(b.getExtraData())).setHash(ByteString.copyFrom(b.getHash())).setLogsBloom(ByteString.copyFrom(b.getLogBloom())).setMinerAddress(ByteString.copyFrom(b.getCoinbase().toBytes())).setNonce(ByteString.copyFrom(b.getNonce())).setNrgConsumed(b.getNrgConsumed()).setNrgLimit(b.getNrgLimit()).setParentHash(ByteString.copyFrom(b.getParentHash())).setTimestamp(b.getTimestamp()).setTxTrieRoot(ByteString.copyFrom(b.getTxTrieRoot())).setReceiptTrieRoot(ByteString.copyFrom(b.getReceiptsRoot())).setStateRoot(ByteString.copyFrom(b.getStateRoot())).setSize(b.getEncoded().length).setSolution(ByteString.copyFrom(b.getHeader().getSolution())).setTotalDifficulty(ByteString.copyFrom(blk.getValue().toByteArray()));
        List<AionTransaction> txs = b.getTransactionsList();
        List<Message.t_TxDetail> tds = new ArrayList<>();
        long cumulativeNrg = 0L;
        // Can't use parallel streams here since we need to compute cumulativeNrg, which is done iteratively
        for (AionTransaction tx : txs) {
            AionTxInfo ti = this.ac.getAionHub().getBlockchain().getTransactionInfo(tx.getHash());
            cumulativeNrg += ti.getReceipt().getEnergyUsed();
            TxRecpt rt = new TxRecpt(b, ti, cumulativeNrg, true);
            List<Message.t_LgEle> tles = Arrays.asList(rt.logs).parallelStream().map(log -> {
                return Message.t_LgEle.newBuilder().setData(ByteString.copyFrom(ByteUtil.hexStringToBytes(log.data))).setAddress(ByteString.copyFrom(Address.wrap(log.address).toBytes())).addAllTopics(Arrays.asList(log.topics)).build();
            }).filter(Objects::nonNull).collect(Collectors.toList());
            Message.t_TxDetail.Builder tdBuilder = Message.t_TxDetail.newBuilder().setData(ByteString.copyFrom(tx.getData())).setTo(ByteString.copyFrom(tx.getTo().toBytes())).setFrom(ByteString.copyFrom(tx.getFrom().toBytes())).setNonce(ByteString.copyFrom(tx.getNonce())).setValue(ByteString.copyFrom(tx.getValue())).setNrgConsumed(rt.nrgUsed).setNrgPrice(tx.getNrgPrice()).setTxHash(ByteString.copyFrom(tx.getHash())).setTxIndex(rt.transactionIndex).addAllLogs(tles);
            if (rt.contractAddress != null)
                tdBuilder.setContract(ByteString.copyFrom(ByteUtil.hexStringToBytes(rt.contractAddress)));
            Message.t_TxDetail td = tdBuilder.build();
            tds.add(td);
        }
        return builder.addAllTx(tds).build();
    }).filter(Objects::nonNull).collect(Collectors.toList());
    return bds;
}
Also used : AionTxInfo(org.aion.zero.impl.types.AionTxInfo) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 35 with AionBlock

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

the class BlockchainConcurrencyTest method testPublishBestBlockSafely.

@Test
public void testPublishBestBlockSafely() {
    ExecutorService blockCreationService = Executors.newSingleThreadExecutor();
    ExecutorService getBlockService = Executors.newSingleThreadExecutor();
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = bundle.bc;
    final int MAX_COUNT = 100000;
    final CountDownLatch endLatch = new CountDownLatch(2);
    // this will not definitively prove
    try {
        blockCreationService.submit(() -> {
            int count = 0;
            List<AionTransaction> txList = Collections.emptyList();
            AionBlock block = bc.createNewBlock(bc.genesis, Collections.emptyList(), false);
            while (!Thread.currentThread().isInterrupted() && count < MAX_COUNT) {
                block = bc.createNewBlock(block, txList, false);
                count++;
            }
            System.out.println("completed block creation");
            endLatch.countDown();
        });
        getBlockService.submit(() -> {
            int count = 0;
            long prevNumber = bc.getBestBlock().getNumber();
            while (!Thread.currentThread().isInterrupted() && count < MAX_COUNT) {
                // all three of these methods use {@link AionBlockchainImpl#pubBestBlock}
                assertThat(bc.getBestBlockHash()).isNotNull();
                bc.getSize();
                AionBlock block = bc.getBestBlock();
                assertThat(block).isNotNull();
                assertThat(block.getNumber()).isAtLeast(prevNumber);
                prevNumber = block.getNumber();
                count++;
            }
            endLatch.countDown();
        });
    } finally {
        blockCreationService.shutdown();
        getBlockService.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) AionTransaction(org.aion.zero.types.AionTransaction) CountDownLatch(java.util.concurrent.CountDownLatch) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

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