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();
}
}
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();
}
}
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;
}
}
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());
}
}
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));
});
}
Aggregations