Search in sources :

Example 1 with AionBlockSummary

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

the class AionBlockchainImpl method add.

@Override
public synchronized AionBlockSummary add(AionBlock block) {
    // typical use without rebuild
    AionBlockSummary summary = add(block, false);
    if (summary != null) {
        List<AionTxReceipt> receipts = summary.getReceipts();
        updateTotalDifficulty(block);
        summary.setTotalDifficulty(getTotalDifficulty());
        storeBlock(block, receipts);
        flush();
    }
    return summary;
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary)

Example 2 with AionBlockSummary

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

the class AionBlockchainImpl method applyBlock.

private AionBlockSummary applyBlock(IAionBlock block) {
    long saveTime = System.nanoTime();
    List<AionTxReceipt> receipts = new ArrayList<>();
    List<AionTxExecSummary> summaries = new ArrayList<>();
    for (AionTransaction tx : block.getTransactionsList()) {
        TransactionExecutor executor = new TransactionExecutor(tx, block, track);
        AionTxExecSummary summary = executor.execute();
        track.flush();
        AionTxReceipt receipt = summary.getReceipt();
        receipt.setPostTxState(repository.getRoot());
        receipts.add(receipt);
        summaries.add(summary);
    }
    Map<Address, BigInteger> rewards = addReward(block, summaries);
    long totalTime = System.nanoTime() - saveTime;
    chainStats.addBlockExecTime(totalTime);
    return new AionBlockSummary(block, rewards, receipts, summaries);
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) TransactionExecutor(org.aion.vm.TransactionExecutor)

Example 3 with AionBlockSummary

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

the class ApiAion0 method onBlock.

protected void onBlock(AionBlockSummary cbs) {
    Set<Long> keys = installedFilters.keySet();
    for (Long key : keys) {
        Fltr fltr = installedFilters.get(key);
        if (fltr.isExpired()) {
            LOG.debug("<fltr key={} expired removed>", key);
            installedFilters.remove(key);
        } else {
            List<AionTxReceipt> txrs = cbs.getReceipts();
            if (fltr.getType() == Fltr.Type.EVENT && !Optional.ofNullable(txrs).orElse(Collections.emptyList()).isEmpty()) {
                FltrCt _fltr = (FltrCt) fltr;
                for (AionTxReceipt txr : txrs) {
                    AionTransaction tx = txr.getTransaction();
                    Address contractAddress = Optional.ofNullable(tx.getTo()).orElse(tx.getContractAddress());
                    Integer cnt = 0;
                    txr.getLogInfoList().forEach(bi -> bi.getTopics().forEach(lg -> {
                        if (_fltr.isFor(contractAddress, ByteUtil.toHexString(lg))) {
                            IBlock<AionTransaction, ?> blk = (cbs).getBlock();
                            List<AionTransaction> txList = blk.getTransactionsList();
                            int insideCnt = 0;
                            for (AionTransaction t : txList) {
                                if (Arrays.equals(t.getHash(), tx.getHash())) {
                                    break;
                                }
                                insideCnt++;
                            }
                            EvtContract ec = new EvtContract(bi.getAddress().toBytes(), bi.getData(), blk.getHash(), blk.getNumber(), cnt, ByteUtil.toHexString(lg), false, insideCnt, tx.getHash());
                            _fltr.add(ec);
                        }
                    }));
                }
            }
        }
    }
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock) java.util(java.util) Version(org.aion.zero.impl.Version) LRUMap(org.apache.commons.collections4.map.LRUMap) org.aion.base.type(org.aion.base.type) ByteBuffer(java.nio.ByteBuffer) org.aion.api.server.types(org.aion.api.server.types) Abi(org.aion.solidity.Abi) IAionChain(org.aion.zero.impl.blockchain.IAionChain) AionTxReceipt(org.aion.zero.types.AionTxReceipt) INode(org.aion.p2p.INode) BigInteger(java.math.BigInteger) IApiAion(org.aion.api.server.IApiAion) ApiAion(org.aion.api.server.ApiAion) IHandler(org.aion.evtmgr.IHandler) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ApiUtil(org.aion.api.server.ApiUtil) LongStream(java.util.stream.LongStream) TypeConverter(org.aion.base.util.TypeConverter) EventTx(org.aion.evtmgr.impl.evt.EventTx) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) EventCallback(org.aion.evtmgr.impl.callback.EventCallback) ByteString(com.google.protobuf.ByteString) EquihashMiner(org.aion.equihash.EquihashMiner) AionHub(org.aion.zero.impl.AionHub) Keystore(org.aion.mcf.account.Keystore) Entry(java.util.Map.Entry) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxInfo(org.aion.zero.impl.types.AionTxInfo) Hex(org.aion.base.util.Hex) AionTransaction(org.aion.zero.types.AionTransaction) JSONArray(org.json.JSONArray) ByteUtil(org.aion.base.util.ByteUtil) AionTransaction(org.aion.zero.types.AionTransaction) BigInteger(java.math.BigInteger) AionTxReceipt(org.aion.zero.types.AionTxReceipt)

Example 4 with AionBlockSummary

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

the class FltrLg method onBlock.

// -------------------------------------------------------------------------------
@Override
public boolean onBlock(IBlockSummary bs) {
    List<AionTxReceipt> receipts = ((AionBlockSummary) bs).getReceipts();
    IBlock blk = bs.getBlock();
    if (matchBloom(new Bloom(((IAionBlock) blk).getLogBloom()))) {
        int txIndex = 0;
        for (AionTxReceipt receipt : receipts) {
            ITransaction tx = receipt.getTransaction();
            if (matchesContractAddress(tx.getTo().toBytes())) {
                if (matchBloom(receipt.getBloomFilter())) {
                    int logIndex = 0;
                    for (Log logInfo : receipt.getLogInfoList()) {
                        if (matchBloom(logInfo.getBloom()) && matchesExactly(logInfo)) {
                            add(new EvtLg(new TxRecptLg(logInfo, blk, txIndex, receipt.getTransaction(), logIndex, true)));
                        }
                        logIndex++;
                    }
                }
            }
            txIndex++;
        }
    }
    return true;
}
Also used : AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) Log(org.aion.mcf.vm.types.Log) Bloom(org.aion.mcf.vm.types.Bloom) IAionBlock(org.aion.zero.types.IAionBlock) AionTxReceipt(org.aion.zero.types.AionTxReceipt)

Example 5 with AionBlockSummary

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

Aggregations

AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)7 BigInteger (java.math.BigInteger)3 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)2 AionTxReceipt (org.aion.zero.types.AionTxReceipt)2 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1 Entry (java.util.Map.Entry)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Collectors (java.util.stream.Collectors)1 LongStream (java.util.stream.LongStream)1 ApiAion (org.aion.api.server.ApiAion)1 ApiUtil (org.aion.api.server.ApiUtil)1 IApiAion (org.aion.api.server.IApiAion)1 org.aion.api.server.types (org.aion.api.server.types)1 org.aion.base.type (org.aion.base.type)1 Address (org.aion.base.type.Address)1 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)1 ByteUtil (org.aion.base.util.ByteUtil)1