Search in sources :

Example 86 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class JournalPruneDataSource method put.

@Override
public void put(byte[] key, byte[] value) {
    checkNotNull(key);
    checkNotNull(value);
    lock.writeLock().lock();
    try {
        if (enabled.get()) {
            // pruning enabled
            ByteArrayWrapper keyW = ByteArrayWrapper.wrap(key);
            // Check to see the value exists.
            if (value != null) {
                // If it exists and pruning is enabled.
                currentUpdates.insertedKeys.add(keyW);
                incRef(keyW);
                // put to source database.
                src.put(key, value);
            } else {
                check();
                // Value does not exist, so we delete from current updates
                currentUpdates.deletedKeys.add(keyW);
            }
        } else {
            // pruning disabled
            if (value != null) {
                src.put(key, value);
            } else {
                check();
            }
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw e;
        } else {
            LOG.error("Could not put key-value pair due to ", e);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 87 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class JournalPruneDataSource method putBatch.

@Override
public void putBatch(Map<byte[], byte[]> inputMap) {
    checkNotNull(inputMap.keySet());
    checkNotNull(inputMap.values());
    lock.writeLock().lock();
    try {
        Map<byte[], byte[]> insertsOnly = new HashMap<>();
        if (enabled.get()) {
            for (Map.Entry<byte[], byte[]> entry : inputMap.entrySet()) {
                ByteArrayWrapper keyW = ByteArrayWrapper.wrap(entry.getKey());
                if (entry.getValue() != null) {
                    currentUpdates.insertedKeys.add(keyW);
                    incRef(keyW);
                    insertsOnly.put(entry.getKey(), entry.getValue());
                } else {
                    currentUpdates.deletedKeys.add(keyW);
                }
            }
        } else {
            for (Map.Entry<byte[], byte[]> entry : inputMap.entrySet()) {
                if (entry.getValue() != null) {
                    insertsOnly.put(entry.getKey(), entry.getValue());
                }
            }
        }
        src.putBatch(insertsOnly);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw e;
        } else {
            LOG.error("Could not put batch due to ", e);
        }
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 88 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class LruDataSource method get.

@Override
public V get(byte[] key) {
    ByteArrayWrapper wrappedKey = ByteArrayWrapper.wrap(key);
    if (cache.containsKey(wrappedKey)) {
        return cache.get(wrappedKey);
    } else {
        V val = super.get(key);
        cache.put(wrappedKey, val);
        return val;
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Example 89 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class TaskValidateAndAddBlocks method run.

@Override
public void run() {
    // TODO: re-evaluate priority when full functionality is implemented
    Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
    Block firstBlock = response.getBlocks().get(0);
    Thread.currentThread().setName("check-" + displayId + "-" + firstBlock.getShortHash());
    // log start of operation
    if (log.isDebugEnabled()) {
        log.debug("Starting block validation from peer={}, start-block={}, batch-size={}.", displayId, firstBlock.getShortHash(), response.getBlocks().size());
    }
    List<Block> filtered = new ArrayList<>();
    List<ByteArrayWrapper> batchHashes = new ArrayList<>();
    BlockHeader currentHeader, previousHeader = null;
    for (Block currentBlock : response.getBlocks()) {
        ByteArrayWrapper hash = currentBlock.getHashWrapper();
        if (// exclude imported
        importedBlockHashes.containsKey(hash) || receivedBlockHashes.containsKey(hash)) {
            // exclude known hashes
            previousHeader = currentBlock.getHeader();
            continue;
        }
        currentHeader = currentBlock.getHeader();
        // TODO: we could do partial evaluations here (as per fast sync specs)
        if (!this.blockHeaderValidator.validate(currentHeader, log)) {
            if (log.isDebugEnabled()) {
                log.debug("<invalid-header num={} hash={} from peer={}/{}>", currentHeader.getNumber(), currentHeader.getHash(), displayId, peerId);
            }
            if (log.isTraceEnabled()) {
                log.debug("<invalid-header: {}>", currentHeader.toString());
            }
            return;
        }
        // ignore batch if not ordered correctly
        if (previousHeader != null && (currentHeader.getNumber() != (previousHeader.getNumber() - 1) || !Arrays.equals(previousHeader.getParentHash(), currentHeader.getHash()))) {
            log.debug("<inconsistent-block-headers num={}, prev-1={}, p_hash={}, prev={} from peer={}/{}>", currentHeader.getNumber(), previousHeader.getNumber() - 1, ByteUtil.toHexString(previousHeader.getParentHash()), ByteUtil.toHexString(currentHeader.getHash()), displayId, peerId);
            return;
        }
        filtered.add(currentBlock);
        previousHeader = currentHeader;
        batchHashes.add(hash);
    }
    if (!filtered.isEmpty()) {
        ByteArrayWrapper first = batchHashes.get(0);
        downloadedBlocks.offer(new BlocksWrapper(peerId, displayId, filtered));
        batchHashes.forEach(k -> receivedBlockHashes.put(k, first));
    }
    // log end of operation
    if (log.isDebugEnabled()) {
        log.debug("Completed block validation from peer={}, start-block={}, batch-size={} with filtered-size={}.", displayId, firstBlock.getShortHash(), response.getBlocks().size(), filtered.size());
    }
}
Also used : ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper) ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block) BlockHeader(org.aion.zero.impl.types.BlockHeader)

Example 90 with ByteArrayWrapper

use of org.aion.util.types.ByteArrayWrapper in project aion by aionnetwork.

the class BlockPropagationHandler method propagateNewBlock.

// assumption here is that blocks propagated have unique hashes
public void propagateNewBlock(final Block block) {
    if (block == null)
        return;
    ByteArrayWrapper hashWrapped = block.getHashWrapper();
    synchronized (this.cacheMap) {
        this.cacheMap.put(hashWrapped, true);
    }
    this.p2pManager.getActiveNodes().values().forEach(n -> {
        if (log.isDebugEnabled())
            log.debug("<sending-new-block=" + block.getShortHash() + " to=" + n.getIdShort() + ">");
        this.p2pManager.send(n.getIdHash(), n.getIdShort(), new BroadcastNewBlock(block));
    });
}
Also used : BroadcastNewBlock(org.aion.zero.impl.sync.msg.BroadcastNewBlock) ByteArrayWrapper(org.aion.util.types.ByteArrayWrapper)

Aggregations

ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)130 Test (org.junit.Test)51 HashMap (java.util.HashMap)39 ArrayList (java.util.ArrayList)33 AionAddress (org.aion.types.AionAddress)26 Block (org.aion.zero.impl.types.Block)24 Map (java.util.Map)20 BigInteger (java.math.BigInteger)14 MiningBlock (org.aion.zero.impl.types.MiningBlock)14 IOException (java.io.IOException)13 MockDB (org.aion.db.impl.mockdb.MockDB)13 DataWord (org.aion.util.types.DataWord)13 PooledTransaction (org.aion.base.PooledTransaction)11 List (java.util.List)10 AionTransaction (org.aion.base.AionTransaction)10 Properties (java.util.Properties)8 HashSet (java.util.HashSet)5 Optional (java.util.Optional)5 ECKey (org.aion.crypto.ECKey)5 RLPElement (org.aion.rlp.RLPElement)5