Search in sources :

Example 61 with AionBlock

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

the class SyncMgr method validateAndAddBlocks.

/**
 * @param _nodeIdHashcode int
 * @param _displayId String
 * @param _bodies List<byte[]>
 * Assemble and validate blocks batch and add batch
 * to import queue from network response blocks bodies
 */
public void validateAndAddBlocks(int _nodeIdHashcode, String _displayId, final List<byte[]> _bodies) {
    if (importedBlocks.size() > blocksQueueMax) {
        log.debug("Imported blocks queue is full. Stop validating incoming bodies");
        return;
    }
    HeadersWrapper hw = this.sentHeaders.remove(_nodeIdHashcode);
    if (hw == null || _bodies == null)
        return;
    // assemble batch
    List<A0BlockHeader> headers = hw.getHeaders();
    List<AionBlock> blocks = new ArrayList<>(_bodies.size());
    Iterator<A0BlockHeader> headerIt = headers.iterator();
    Iterator<byte[]> bodyIt = _bodies.iterator();
    while (headerIt.hasNext() && bodyIt.hasNext()) {
        AionBlock block = AionBlock.createBlockFromNetwork(headerIt.next(), bodyIt.next());
        if (block == null) {
            log.error("<assemble-and-validate-blocks node={}>", _displayId);
            break;
        } else
            blocks.add(block);
    }
    int m = blocks.size();
    if (m == 0)
        return;
    if (log.isDebugEnabled()) {
        log.debug("<incoming-bodies from-num={} to-num={} node={}>", blocks.get(0).getNumber(), blocks.get(blocks.size() - 1).getNumber(), _displayId);
    }
    // add batch
    importedBlocks.add(new BlocksWrapper(_nodeIdHashcode, _displayId, blocks));
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 62 with AionBlock

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

the class TaskImportBlocks method run.

@Override
public void run() {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    while (start.get()) {
        BlocksWrapper bw;
        try {
            bw = importedBlocks.take();
        } catch (InterruptedException ex) {
            return;
        }
        List<AionBlock> batch = bw.getBlocks();
        for (AionBlock b : batch) {
            if (importedBlockHashes.containsKey(ByteArrayWrapper.wrap(b.getHash()))) {
                continue;
            }
            long t1 = System.currentTimeMillis();
            ImportResult importResult = this.chain.tryToConnect(b);
            long t2 = System.currentTimeMillis();
            log.info("<import-status: node = {}, number = {}, txs = {}, result = {}, time elapsed = {} ms>", bw.getDisplayId(), b.getNumber(), b.getTransactionsList().size(), importResult, t2 - t1);
            switch(importResult) {
                case IMPORTED_BEST:
                    importedBlockHashes.put(ByteArrayWrapper.wrap(b.getHash()), null);
                    break;
                case IMPORTED_NOT_BEST:
                    importedBlockHashes.put(ByteArrayWrapper.wrap(b.getHash()), null);
                    break;
                case EXIST:
                    importedBlockHashes.put(ByteArrayWrapper.wrap(b.getHash()), null);
                    break;
                case NO_PARENT:
                    break;
                case INVALID_BLOCK:
                    break;
                default:
                    break;
            }
        }
        this.statis.update(this.chain.getBestBlock().getNumber());
    }
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 63 with AionBlock

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

the class TaskShowStatus method run.

@Override
public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (this.start.get()) {
        AionBlock selfBest = this.chain.getBestBlock();
        String selfTd = this.chain.getTotalDifficulty().toString(10);
        String status = // 
        "[sync-status avg-import=" + String.format("%.2f", this.statics.getAvgBlocksPerSec()) + // 
        " b/s" + " td=" + selfTd + "/" + // 
        networkStatus.getTargetTotalDiff().toString(10) + " b-num=" + selfBest.getNumber() + "/" + // 
        this.networkStatus.getTargetBestBlockNumber() + " b-hash=" + // 
        Hex.toHexString(this.chain.getBestBlockHash()) + "/" + this.networkStatus.getTargetBestBlockHash() + "]";
        // print to std output
        System.out.println(status);
        // print to report file
        if (printReport) {
            try {
                Files.write(Paths.get(reportFolder, System.currentTimeMillis() + "-sync-report.out"), status.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e) {
            if (log.isDebugEnabled()) {
                log.debug("<sync-ss shutdown>");
            }
            return;
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("<sync-ss shutdown>");
    }
}
Also used : IOException(java.io.IOException) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 64 with AionBlock

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

the class BroadcastNewBlockHandler method receive.

@Override
public void receive(int _nodeIdHashcode, String _displayId, final byte[] _msgBytes) {
    if (_msgBytes == null)
        return;
    byte[] rawdata = BroadcastNewBlock.decode(_msgBytes);
    if (rawdata == null)
        return;
    AionBlock block = new AionBlock(rawdata);
    BlockPropagationHandler.PropStatus result = this.propHandler.processIncomingBlock(_nodeIdHashcode, block);
    if (this.log.isDebugEnabled()) {
        String hash = block.getShortHash();
        hash = hash != null ? hash : "null";
        this.log.debug("<block-prop node=" + _displayId + " block-hash=" + hash + " status=" + result.name() + ">");
    }
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Aggregations

AionBlock (org.aion.zero.impl.types.AionBlock)64 Test (org.junit.Test)20 AionTransaction (org.aion.zero.types.AionTransaction)16 BigInteger (java.math.BigInteger)15 ImportResult (org.aion.mcf.core.ImportResult)15 ArrayList (java.util.ArrayList)8 Address (org.aion.base.type.Address)6 ECKey (org.aion.crypto.ECKey)6 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)5 IAionBlock (org.aion.zero.types.IAionBlock)5 JSONObject (org.json.JSONObject)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)4 StandaloneBlockchain (org.aion.zero.impl.StandaloneBlockchain)4 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)4 A0BlockHeader (org.aion.zero.types.A0BlockHeader)4 Map (java.util.Map)3 TxRecpt (org.aion.api.server.types.TxRecpt)3 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 LRUMap (org.apache.commons.collections4.map.LRUMap)3