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