Search in sources :

Example 6 with AionTxInfo

use of org.aion.zero.impl.types.AionTxInfo 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 7 with AionTxInfo

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

the class AionBlockchainImpl method storeBlock.

@Override
public synchronized void storeBlock(AionBlock block, List<AionTxReceipt> receipts) {
    if (fork) {
        getBlockStore().saveBlock(block, totalDifficulty, false);
    } else {
        getBlockStore().saveBlock(block, totalDifficulty, true);
    }
    for (int i = 0; i < receipts.size(); i++) {
        transactionStore.put(new AionTxInfo(receipts.get(i), block.getHash(), i));
    }
    ((AionRepositoryImpl) repository).commitBlock(block.getHeader());
    if (LOG.isDebugEnabled())
        LOG.debug("Block saved: number: {}, hash: {}, TD: {}", block.getNumber(), block.getShortHash(), totalDifficulty);
    setBestBlock(block);
    if (LOG.isDebugEnabled()) {
        LOG.debug("block added to the blockChain: index: [{}]", block.getNumber());
    }
}
Also used : AionTxInfo(org.aion.zero.impl.types.AionTxInfo) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl)

Example 8 with AionTxInfo

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

the class AionBlockchainImpl method add.

public synchronized AionBlockSummary add(AionBlock block, boolean rebuild) {
    if (block == null) {
        LOG.error("Attempting to add NULL block.");
        return null;
    }
    if (exitOn < block.getNumber()) {
        LOG.error("Exiting after block.number: ", bestBlock.getNumber());
        flush();
        System.exit(-1);
    }
    if (!isValid(block)) {
        LOG.error("Attempting to add INVALID block.");
        return null;
    }
    track = repository.startTracking();
    byte[] origRoot = repository.getRoot();
    // (if not reconstructing old blocks) keep chain continuity
    if (!rebuild && !Arrays.equals(bestBlock.getHash(), block.getParentHash())) {
        LOG.error("Attempting to add NON-SEQUENTIAL block.");
        return null;
    }
    AionBlockSummary summary = processBlock(block);
    List<AionTxReceipt> receipts = summary.getReceipts();
    // Sanity checks
    byte[] receiptHash = block.getReceiptsRoot();
    byte[] receiptListHash = calcReceiptsTrie(receipts);
    if (!Arrays.equals(receiptHash, receiptListHash)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("Block's given Receipt Hash doesn't match: {} != {}", receiptHash, receiptListHash);
            LOG.warn("Calculated receipts: " + receipts);
        }
        return null;
    }
    byte[] logBloomHash = block.getLogBloom();
    byte[] logBloomListHash = calcLogBloom(receipts);
    if (!Arrays.equals(logBloomHash, logBloomListHash)) {
        if (LOG.isWarnEnabled())
            LOG.warn("Block's given logBloom Hash doesn't match: {} != {}", ByteUtil.toHexString(logBloomHash), ByteUtil.toHexString(logBloomListHash));
        track.rollback();
        return null;
    }
    if (!rebuild) {
        byte[] blockStateRootHash = block.getStateRoot();
        byte[] worldStateRootHash = repository.getRoot();
        if (!Arrays.equals(blockStateRootHash, worldStateRootHash)) {
            LOG.warn("BLOCK: State conflict or received invalid block. block: {} worldstate {} mismatch", block.getNumber(), worldStateRootHash);
            LOG.warn("Conflict block dump: {}", Hex.toHexString(block.getEncoded()));
            track.rollback();
            // block is bad so 'rollback' the state root to the original
            // state
            ((AionRepositoryImpl) repository).setRoot(origRoot);
            if (this.config.getExitOnBlockConflict()) {
                chainStats.lostConsensus();
                System.out.println("CONFLICT: BLOCK #" + block.getNumber() + ", dump: " + Hex.toHexString(block.getEncoded()));
                System.exit(1);
            } else {
                return null;
            }
        }
    }
    // update corresponding account with the new balance
    track.flush();
    if (rebuild) {
        for (int i = 0; i < receipts.size(); i++) {
            transactionStore.put(new AionTxInfo(receipts.get(i), block.getHash(), i));
        }
        ((AionRepositoryImpl) repository).commitBlock(block.getHeader());
        if (LOG.isDebugEnabled())
            LOG.debug("Block rebuilt: number: {}, hash: {}, TD: {}", block.getNumber(), block.getShortHash(), totalDifficulty);
    }
    return summary;
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) AionRepositoryImpl(org.aion.zero.impl.db.AionRepositoryImpl)

Example 9 with AionTxInfo

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

the class AionPendingStateImpl method getTransactionInfo.

private AionTxInfo getTransactionInfo(byte[] txHash, byte[] blockHash) {
    AionTxInfo info = transactionStore.get(txHash, blockHash);
    AionTransaction tx = blockchain.getBlockByHash(info.getBlockHash()).getTransactionsList().get(info.getIndex());
    info.getReceipt().setTransaction(tx);
    return info;
}
Also used : AionTxInfo(org.aion.zero.impl.types.AionTxInfo)

Aggregations

AionTxInfo (org.aion.zero.impl.types.AionTxInfo)9 AionBlock (org.aion.zero.impl.types.AionBlock)4 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)2 AionTransaction (org.aion.zero.types.AionTransaction)2 BigInteger (java.math.BigInteger)1 TxRecpt (org.aion.api.server.types.TxRecpt)1 Bloom (org.aion.mcf.vm.types.Bloom)1 Log (org.aion.mcf.vm.types.Log)1 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)1 AionTxReceipt (org.aion.zero.types.AionTxReceipt)1